1
0
forked from blue/squawk
squawk/ui/accounts.cpp

139 lines
4.5 KiB
C++
Raw Normal View History

2019-03-29 14:54:34 +00:00
#include "accounts.h"
#include "ui_accounts.h"
2019-03-30 20:13:13 +00:00
#include <QDebug>
Accounts::Accounts(Models::Accounts* p_model, QWidget *parent) :
m_ui(new Ui::Accounts),
model(p_model),
2019-05-29 15:05:54 +00:00
editing(false),
toDisconnect(false)
2019-03-29 14:54:34 +00:00
{
m_ui->setupUi(this);
2019-03-30 20:13:13 +00:00
connect(m_ui->addButton, SIGNAL(clicked(bool)), this, SLOT(onAddButton(bool)));
connect(m_ui->editButton, SIGNAL(clicked(bool)), this, SLOT(onEditButton(bool)));
2019-05-29 15:05:54 +00:00
connect(m_ui->connectButton, SIGNAL(clicked(bool)), this, SLOT(onConnectButton(bool)));
connect(m_ui->deleteButton, SIGNAL(clicked(bool)), this, SLOT(onDeleteButton(bool)));
2019-04-03 18:15:36 +00:00
m_ui->tableView->setModel(model);
connect(m_ui->tableView->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
2019-05-29 15:05:54 +00:00
connect(p_model, SIGNAL(changed()), this, SLOT(updateConnectButton()));
2019-03-29 14:54:34 +00:00
}
Accounts::~Accounts() = default;
2019-03-30 20:13:13 +00:00
void Accounts::onAddButton(bool clicked)
{
Account* acc = new Account();
connect(acc, SIGNAL(accepted()), this, SLOT(onAccountAccepted()));
connect(acc, SIGNAL(rejected()), this, SLOT(onAccountRejected()));
acc->exec();
}
void Accounts::onAccountAccepted()
{
Account* acc = static_cast<Account*>(sender());
QMap<QString, QVariant> map = acc->value();
const Models::Account* mAcc = model->getAccount(m_ui->tableView->selectionModel()->selectedRows().at(0).row());
if (editing) {
emit changeAccount(mAcc->getName(), map);
} else {
emit newAccount(map);
}
2019-03-30 20:13:13 +00:00
editing = false;
2019-03-30 20:13:13 +00:00
}
void Accounts::onAccountRejected()
{
Account* acc = static_cast<Account*>(sender());
acc->deleteLater();
editing = false;
}
void Accounts::onEditButton(bool clicked)
{
Account* acc = new Account();
const Models::Account* mAcc = model->getAccount(m_ui->tableView->selectionModel()->selectedRows().at(0).row());
acc->setData({
{"login", mAcc->getLogin()},
{"password", mAcc->getPassword()},
{"server", mAcc->getServer()},
{"name", mAcc->getName()},
{"resource", mAcc->getResource()}
});
acc->lockId();
connect(acc, SIGNAL(accepted()), this, SLOT(onAccountAccepted()));
connect(acc, SIGNAL(rejected()), this, SLOT(onAccountRejected()));
editing = true;
acc->exec();
}
void Accounts::onSelectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
{
2019-05-29 15:05:54 +00:00
int selectionSize = m_ui->tableView->selectionModel()->selection().size();
if (selectionSize == 0) {
m_ui->editButton->setEnabled(false);
2019-05-29 15:05:54 +00:00
m_ui->deleteButton->setEnabled(false);
} else if (selectionSize == 1) {
m_ui->editButton->setEnabled(true);
2019-05-29 15:05:54 +00:00
m_ui->deleteButton->setEnabled(true);
} else {
m_ui->editButton->setEnabled(false);
2019-05-29 15:05:54 +00:00
m_ui->deleteButton->setEnabled(true);
}
updateConnectButton();
}
void Accounts::updateConnectButton()
{
QItemSelectionModel* sm = m_ui->tableView->selectionModel();
if (sm->hasSelection()) {
m_ui->connectButton->setEnabled(true);
int selectionSize = sm->selection().size();
bool allConnected = true;
for (int i = 0; i < selectionSize && allConnected; ++i) {
const Models::Account* mAcc = model->getAccount(sm->selectedRows().at(i).row());
allConnected = mAcc->getState() == Shared::connected;
}
if (allConnected) {
toDisconnect = true;
m_ui->connectButton->setText("Disconnect");
} else {
toDisconnect = false;
m_ui->connectButton->setText("Connect");
}
} else {
m_ui->connectButton->setText("Connect");
toDisconnect = false;
m_ui->connectButton->setEnabled(false);
}
}
void Accounts::onConnectButton(bool clicked)
{
QItemSelectionModel* sm = m_ui->tableView->selectionModel();
int selectionSize = sm->selection().size();
for (int i = 0; i < selectionSize; ++i) {
const Models::Account* mAcc = model->getAccount(sm->selectedRows().at(i).row());
if (toDisconnect) {
emit disconnectAccount(mAcc->getName());
} else {
emit connectAccount(mAcc->getName());
}
}
}
void Accounts::onDeleteButton(bool clicked)
{
QItemSelectionModel* sm = m_ui->tableView->selectionModel();
int selectionSize = sm->selection().size();
for (int i = 0; i < selectionSize; ++i) {
const Models::Account* mAcc = model->getAccount(sm->selectedRows().at(i).row());
emit removeAccount(mAcc->getName());
}
2019-03-30 20:13:13 +00:00
}