2019-08-14 14:54:46 +00:00
|
|
|
/*
|
|
|
|
* Squawk messenger.
|
|
|
|
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
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>
|
|
|
|
|
2019-05-23 20:19:53 +00:00
|
|
|
Accounts::Accounts(Models::Accounts* p_model, QWidget *parent) :
|
2020-04-04 16:40:32 +00:00
|
|
|
QWidget(parent),
|
2019-05-23 20:19:53 +00:00
|
|
|
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
|
|
|
|
2019-11-03 18:46:40 +00:00
|
|
|
connect(m_ui->addButton, &QPushButton::clicked, this, &Accounts::onAddButton);
|
|
|
|
connect(m_ui->editButton, &QPushButton::clicked, this, &Accounts::onEditButton);
|
|
|
|
connect(m_ui->connectButton, &QPushButton::clicked, this, &Accounts::onConnectButton);
|
|
|
|
connect(m_ui->deleteButton, &QPushButton::clicked, this, &Accounts::onDeleteButton);
|
2019-04-03 18:15:36 +00:00
|
|
|
m_ui->tableView->setModel(model);
|
2019-11-03 18:46:40 +00:00
|
|
|
connect(m_ui->tableView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &Accounts::onSelectionChanged);
|
|
|
|
connect(p_model, &Models::Accounts::changed, this, &Accounts::updateConnectButton);
|
2019-03-29 14:54:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Accounts::~Accounts() = default;
|
2019-03-30 20:13:13 +00:00
|
|
|
|
2020-04-04 16:40:32 +00:00
|
|
|
void Accounts::onAddButton()
|
2019-03-30 20:13:13 +00:00
|
|
|
{
|
|
|
|
Account* acc = new Account();
|
2019-11-03 18:46:40 +00:00
|
|
|
connect(acc, &Account::accepted, this, &Accounts::onAccountAccepted);
|
|
|
|
connect(acc, &Account::rejected, this, &Accounts::onAccountRejected);
|
2019-03-30 20:13:13 +00:00
|
|
|
acc->exec();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Accounts::onAccountAccepted()
|
|
|
|
{
|
|
|
|
Account* acc = static_cast<Account*>(sender());
|
|
|
|
QMap<QString, QVariant> map = acc->value();
|
2019-05-23 20:19:53 +00:00
|
|
|
if (editing) {
|
2019-05-30 09:36:21 +00:00
|
|
|
const Models::Account* mAcc = model->getAccount(m_ui->tableView->selectionModel()->selectedRows().at(0).row());
|
2019-05-24 14:46:34 +00:00
|
|
|
emit changeAccount(mAcc->getName(), map);
|
2019-05-23 20:19:53 +00:00
|
|
|
} else {
|
|
|
|
emit newAccount(map);
|
|
|
|
}
|
2019-03-30 20:13:13 +00:00
|
|
|
|
2019-06-15 15:29:15 +00:00
|
|
|
acc->deleteLater();
|
2019-05-23 20:19:53 +00:00
|
|
|
editing = false;
|
2019-03-30 20:13:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Accounts::onAccountRejected()
|
|
|
|
{
|
|
|
|
Account* acc = static_cast<Account*>(sender());
|
|
|
|
acc->deleteLater();
|
2019-05-23 20:19:53 +00:00
|
|
|
editing = false;
|
|
|
|
}
|
|
|
|
|
2020-04-04 16:40:32 +00:00
|
|
|
void Accounts::onEditButton()
|
2019-05-23 20:19:53 +00:00
|
|
|
{
|
|
|
|
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()},
|
2020-04-04 16:40:32 +00:00
|
|
|
{"resource", mAcc->getResource()},
|
|
|
|
{"passwordType", QVariant::fromValue(mAcc->getPasswordType())}
|
2019-05-23 20:19:53 +00:00
|
|
|
});
|
|
|
|
acc->lockId();
|
2019-11-03 18:46:40 +00:00
|
|
|
connect(acc, &Account::accepted, this, &Accounts::onAccountAccepted);
|
|
|
|
connect(acc, &Account::rejected, this, &Accounts::onAccountRejected);
|
2019-05-23 20:19:53 +00:00
|
|
|
editing = true;
|
|
|
|
acc->exec();
|
|
|
|
}
|
|
|
|
|
2020-04-04 16:40:32 +00:00
|
|
|
void Accounts::onSelectionChanged()
|
2019-05-23 20:19:53 +00:00
|
|
|
{
|
2019-05-29 15:05:54 +00:00
|
|
|
int selectionSize = m_ui->tableView->selectionModel()->selection().size();
|
|
|
|
if (selectionSize == 0) {
|
2019-05-23 20:19:53 +00:00
|
|
|
m_ui->editButton->setEnabled(false);
|
2019-05-29 15:05:54 +00:00
|
|
|
m_ui->deleteButton->setEnabled(false);
|
|
|
|
} else if (selectionSize == 1) {
|
2019-05-23 20:19:53 +00:00
|
|
|
m_ui->editButton->setEnabled(true);
|
2019-05-29 15:05:54 +00:00
|
|
|
m_ui->deleteButton->setEnabled(true);
|
2019-05-23 20:19:53 +00:00
|
|
|
} 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());
|
2020-04-03 22:28:15 +00:00
|
|
|
allConnected = mAcc->getState() == Shared::ConnectionState::connected;
|
2019-05-29 15:05:54 +00:00
|
|
|
}
|
|
|
|
if (allConnected) {
|
|
|
|
toDisconnect = true;
|
2019-10-05 11:27:39 +00:00
|
|
|
m_ui->connectButton->setText(tr("Disconnect"));
|
2019-05-29 15:05:54 +00:00
|
|
|
} else {
|
|
|
|
toDisconnect = false;
|
2019-10-05 11:27:39 +00:00
|
|
|
m_ui->connectButton->setText(tr("Connect"));
|
2019-05-29 15:05:54 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-10-05 11:27:39 +00:00
|
|
|
m_ui->connectButton->setText(tr("Connect"));
|
2019-05-29 15:05:54 +00:00
|
|
|
toDisconnect = false;
|
|
|
|
m_ui->connectButton->setEnabled(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-04 16:40:32 +00:00
|
|
|
void Accounts::onConnectButton()
|
2019-05-29 15:05:54 +00:00
|
|
|
{
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-04 16:40:32 +00:00
|
|
|
void Accounts::onDeleteButton()
|
2019-05-29 15:05:54 +00:00
|
|
|
{
|
|
|
|
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-05-23 20:19:53 +00:00
|
|
|
}
|
2019-03-30 20:13:13 +00:00
|
|
|
}
|