From f64e5c2df079aedcfb452be226930339f5fdac72 Mon Sep 17 00:00:00 2001 From: blue Date: Tue, 12 Apr 2022 23:33:10 +0300 Subject: [PATCH] account connect/disconnect now activate/deactivate, it's a bit less contraversial; async account password asking new concept --- core/account.cpp | 62 +++++++--- core/account.h | 7 ++ core/squawk.cpp | 194 +++++++++++++++---------------- core/squawk.h | 6 +- ui/dialogqueue.cpp | 2 +- ui/models/account.cpp | 26 ++++- ui/models/account.h | 4 + ui/models/accounts.cpp | 4 + ui/models/roster.cpp | 12 ++ ui/squawk.cpp | 74 +++--------- ui/widgets/accounts/account.cpp | 1 + ui/widgets/accounts/account.ui | 45 ++++--- ui/widgets/accounts/accounts.cpp | 11 +- 13 files changed, 248 insertions(+), 200 deletions(-) diff --git a/core/account.cpp b/core/account.cpp index 3d782cd..4d0480f 100644 --- a/core/account.cpp +++ b/core/account.cpp @@ -22,7 +22,7 @@ using namespace Core; -Account::Account(const QString& p_login, const QString& p_server, const QString& p_password, const QString& p_name, NetworkAccess* p_net, QObject* parent): +Account::Account(const QString& p_login, const QString& p_server, const QString& p_password, const QString& p_name, bool p_active, NetworkAccess* p_net, QObject* parent): QObject(parent), name(p_name), archiveQueries(), @@ -44,6 +44,8 @@ Account::Account(const QString& p_login, const QString& p_server, const QString& network(p_net), passwordType(Shared::AccountPassword::plain), pepSupport(false), + active(p_active), + notReadyPassword(false), mh(new MessageHandler(this)), rh(new RosterHandler(this)), vh(new VCardHandler(this)) @@ -89,13 +91,15 @@ Account::Account(const QString& p_login, const QString& p_server, const QString& reconnectTimer->setSingleShot(true); QObject::connect(reconnectTimer, &QTimer::timeout, this, &Account::onReconnectTimer); -// QXmppLogger* logger = new QXmppLogger(this); -// logger->setLoggingType(QXmppLogger::SignalLogging); -// client.setLogger(logger); -// -// QObject::connect(logger, &QXmppLogger::message, this, [](QXmppLogger::MessageType type, const QString& text){ -// qDebug() << text; -// }); + if (name == "Test") { + QXmppLogger* logger = new QXmppLogger(this); + logger->setLoggingType(QXmppLogger::SignalLogging); + client.setLogger(logger); + + QObject::connect(logger, &QXmppLogger::message, this, [](QXmppLogger::MessageType type, const QString& text){ + qDebug() << text; + }); + } } Account::~Account() @@ -135,7 +139,12 @@ void Core::Account::connect() reconnectTimer->stop(); } if (state == Shared::ConnectionState::disconnected) { - client.connectToServer(config, presence); + if (notReadyPassword) { + emit needPassword(); + } else { + client.connectToServer(config, presence); + } + } else { qDebug("An attempt to connect an account which is already connected, skipping"); } @@ -205,12 +214,14 @@ void Core::Account::onClientStateChange(QXmppClient::State st) void Core::Account::reconnect() { - if (state == Shared::ConnectionState::connected && !reconnectScheduled) { - reconnectScheduled = true; - reconnectTimer->start(500); - client.disconnectFromServer(); - } else { - qDebug() << "An attempt to reconnect account" << getName() << "which was not connected"; + if (!reconnectScheduled) { //TODO define behavior if It was connection or disconnecting + if (state == Shared::ConnectionState::connected) { + reconnectScheduled = true; + reconnectTimer->start(500); + client.disconnectFromServer(); + } else { + qDebug() << "An attempt to reconnect account" << getName() << "which was not connected"; + } } } @@ -636,6 +647,19 @@ void Core::Account::onContactHistoryResponse(const std::list& l emit responseArchive(contact->jid, list, last); } +bool Core::Account::getActive() const { + return active;} + +void Core::Account::setActive(bool p_active) { + if (active != p_active) { + active = p_active; + + emit changed({ + {"active", active} + }); + } +} + QString Core::Account::getResource() const { return config.resource();} @@ -673,7 +697,9 @@ void Core::Account::setName(const QString& p_name) { name = p_name;} void Core::Account::setPassword(const QString& p_password) { - config.setPassword(p_password);} + config.setPassword(p_password); + notReadyPassword = false; +} void Core::Account::setServer(const QString& p_server) { config.setDomain(p_server);} @@ -720,3 +746,7 @@ void Core::Account::renameContactRequest(const QString& jid, const QString& newN rm->renameItem(jid, newName); } } + +void Core::Account::invalidatePassword() { + notReadyPassword = true;} + diff --git a/core/account.h b/core/account.h index c8e6e41..aa65b27 100644 --- a/core/account.h +++ b/core/account.h @@ -66,6 +66,7 @@ public: const QString& p_server, const QString& p_password, const QString& p_name, + bool p_active, NetworkAccess* p_net, QObject* parent = 0); ~Account(); @@ -81,6 +82,7 @@ public: QString getFullJid() const; Shared::Availability getAvailability() const; Shared::AccountPassword getPasswordType() const; + bool getActive() const; void setName(const QString& p_name); void setLogin(const QString& p_login); @@ -90,6 +92,7 @@ public: void setAvailability(Shared::Availability avail); void setPasswordType(Shared::AccountPassword pt); void sendMessage(const Shared::Message& data); + void setActive(bool p_active); void requestArchive(const QString& jid, int count, const QString& before); void subscribeToContact(const QString& jid, const QString& reason); void unsubscribeFromContact(const QString& jid, const QString& reason); @@ -107,6 +110,7 @@ public: void uploadVCard(const Shared::VCard& card); void resendMessage(const QString& jid, const QString& id); void replaceMessage(const QString& originalId, const Shared::Message& data); + void invalidatePassword(); public slots: void connect(); @@ -139,6 +143,7 @@ signals: void receivedVCard(const QString& jid, const Shared::VCard& card); void uploadFile(const QFileInfo& file, const QUrl& set, const QUrl& get, QMap headers); void uploadFileError(const QString& jid, const QString& messageId, const QString& error); + void needPassword(); private: QString name; @@ -162,6 +167,8 @@ private: NetworkAccess* network; Shared::AccountPassword passwordType; bool pepSupport; + bool active; + bool notReadyPassword; MessageHandler* mh; RosterHandler* rh; diff --git a/core/squawk.cpp b/core/squawk.cpp index af131d5..d594553 100644 --- a/core/squawk.cpp +++ b/core/squawk.cpp @@ -26,8 +26,8 @@ Core::Squawk::Squawk(QObject* parent): QObject(parent), accounts(), amap(), + state(Shared::Availability::offline), network(), - waitingForAccounts(0), isInitialized(false) #ifdef WITH_KWALLET ,kwallet() @@ -42,7 +42,7 @@ Core::Squawk::Squawk(QObject* parent): if (kwallet.supportState() == PSE::KWallet::success) { connect(&kwallet, &PSE::KWallet::opened, this, &Squawk::onWalletOpened); connect(&kwallet, &PSE::KWallet::rejectPassword, this, &Squawk::onWalletRejectPassword); - connect(&kwallet, &PSE::KWallet::responsePassword, this, &Squawk::onWalletResponsePassword); + connect(&kwallet, &PSE::KWallet::responsePassword, this, &Squawk::responsePassword); Shared::Global::setSupported("KWallet", true); } @@ -97,6 +97,7 @@ void Core::Squawk::stop() settings.setValue("password", password); settings.setValue("resource", acc->getResource()); settings.setValue("passwordType", static_cast(ap)); + settings.setValue("active", acc->getActive()); } settings.endArray(); settings.endGroup(); @@ -124,8 +125,9 @@ void Core::Squawk::newAccountRequest(const QMap& map) QString password = map.value("password").toString(); QString resource = map.value("resource").toString(); int passwordType = map.value("passwordType").toInt(); + bool active = map.value("active").toBool(); - addAccount(login, server, password, name, resource, Shared::Global::fromInt(passwordType)); + addAccount(login, server, password, name, resource, active, Shared::Global::fromInt(passwordType)); } void Core::Squawk::addAccount( @@ -133,13 +135,13 @@ void Core::Squawk::addAccount( const QString& server, const QString& password, const QString& name, - const QString& resource, - Shared::AccountPassword passwordType -) + const QString& resource, + bool active, + Shared::AccountPassword passwordType) { QSettings settings; - Account* acc = new Account(login, server, password, name, &network); + Account* acc = new Account(login, server, password, name, active, &network); acc->setResource(resource); acc->setPasswordType(passwordType); accounts.push_back(acc); @@ -148,6 +150,8 @@ void Core::Squawk::addAccount( connect(acc, &Account::connectionStateChanged, this, &Squawk::onAccountConnectionStateChanged); connect(acc, &Account::changed, this, &Squawk::onAccountChanged); connect(acc, &Account::error, this, &Squawk::onAccountError); + connect(acc, &Account::needPassword, this, &Squawk::onAccountNeedPassword); + connect(acc, &Account::availabilityChanged, this, &Squawk::onAccountAvailabilityChanged); connect(acc, &Account::addContact, this, &Squawk::onAccountAddContact); connect(acc, &Account::addGroup, this, &Squawk::onAccountAddGroup); @@ -185,20 +189,42 @@ void Core::Squawk::addAccount( {"offline", QVariant::fromValue(Shared::Availability::offline)}, {"error", ""}, {"avatarPath", acc->getAvatarPath()}, - {"passwordType", QVariant::fromValue(passwordType)} + {"passwordType", QVariant::fromValue(passwordType)}, + {"active", active} }; emit newAccount(map); + + switch (passwordType) { + case Shared::AccountPassword::alwaysAsk: + case Shared::AccountPassword::kwallet: + acc->invalidatePassword(); + break; + default: + break; + } + + if (state != Shared::Availability::offline) { + acc->setAvailability(state); + if (acc->getActive()) { + acc->connect(); + } + } } void Core::Squawk::changeState(Shared::Availability p_state) { if (state != p_state) { + for (std::deque::iterator itr = accounts.begin(), end = accounts.end(); itr != end; ++itr) { + Account* acc = *itr; + acc->setAvailability(p_state); + if (state == Shared::Availability::offline && acc->getActive()) { + acc->connect(); + } + } state = p_state; - } - - for (std::deque::iterator itr = accounts.begin(), end = accounts.end(); itr != end; ++itr) { - (*itr)->setAvailability(state); + + emit stateChanged(p_state); } } @@ -209,7 +235,10 @@ void Core::Squawk::connectAccount(const QString& account) qDebug("An attempt to connect non existing account, skipping"); return; } - itr->second->connect(); + itr->second->setActive(true); + if (state != Shared::Availability::offline) { + itr->second->connect(); + } } void Core::Squawk::disconnectAccount(const QString& account) @@ -220,6 +249,7 @@ void Core::Squawk::disconnectAccount(const QString& account) return; } + itr->second->setActive(false); itr->second->disconnect(); } @@ -227,7 +257,7 @@ void Core::Squawk::onAccountConnectionStateChanged(Shared::ConnectionState p_sta { Account* acc = static_cast(sender()); emit changeAccount(acc->getName(), {{"state", QVariant::fromValue(p_state)}}); - + #ifdef WITH_KWALLET if (p_state == Shared::ConnectionState::connected) { if (acc->getPasswordType() == Shared::AccountPassword::kwallet && kwallet.supportState() == PSE::KWallet::success) { @@ -235,33 +265,6 @@ void Core::Squawk::onAccountConnectionStateChanged(Shared::ConnectionState p_sta } } #endif - - Accounts::const_iterator itr = accounts.begin(); - bool es = true; - bool ea = true; - Shared::ConnectionState cs = (*itr)->getState(); - Shared::Availability av = (*itr)->getAvailability(); - itr++; - for (Accounts::const_iterator end = accounts.end(); itr != end; itr++) { - Account* item = *itr; - if (item->getState() != cs) { - es = false; - } - if (item->getAvailability() != av) { - ea = false; - } - } - - if (es) { - if (cs == Shared::ConnectionState::disconnected) { - state = Shared::Availability::offline; - emit stateChanged(state); - } else if (ea) { - state = av; - emit stateChanged(state); - } - } - } void Core::Squawk::onAccountAddContact(const QString& jid, const QString& group, const QMap& data) @@ -416,8 +419,15 @@ void Core::Squawk::modifyAccountRequest(const QString& name, const QMapreconnect(); + bool activeChanged = false; + mItr = map.find("active"); + if (mItr == map.end() || mItr->toBool() == acc->getActive()) { + if (needToReconnect && st != Shared::ConnectionState::disconnected) { + acc->reconnect(); + } + } else { + acc->setActive(mItr->toBool()); + activeChanged = true; } mItr = map.find("login"); @@ -454,6 +464,10 @@ void Core::Squawk::modifyAccountRequest(const QString& name, const QMapgetActive() && state != Shared::Availability::offline) { + acc->connect(); + } + emit changeAccount(name, map); } @@ -675,85 +689,62 @@ void Core::Squawk::uploadVCard(const QString& account, const Shared::VCard& card itr->second->uploadVCard(card); } -void Core::Squawk::responsePassword(const QString& account, const QString& password) -{ - AccountsMap::const_iterator itr = amap.find(account); - if (itr == amap.end()) { - qDebug() << "An attempt to set password to non existing account" << account << ", skipping"; - return; - } - itr->second->setPassword(password); - emit changeAccount(account, {{"password", password}}); - accountReady(); -} - void Core::Squawk::readSettings() { QSettings settings; settings.beginGroup("core"); int size = settings.beginReadArray("accounts"); - waitingForAccounts = size; for (int i = 0; i < size; ++i) { settings.setArrayIndex(i); - parseAccount( + Shared::AccountPassword passwordType = + Shared::Global::fromInt( + settings.value("passwordType", static_cast(Shared::AccountPassword::plain)).toInt() + ); + + QString password = settings.value("password", "").toString(); + if (passwordType == Shared::AccountPassword::jammed) { + SimpleCrypt crypto(passwordHash); + password = crypto.decryptToString(password); + } + + addAccount( settings.value("login").toString(), settings.value("server").toString(), - settings.value("password", "").toString(), + password, settings.value("name").toString(), settings.value("resource").toString(), - Shared::Global::fromInt(settings.value("passwordType", static_cast(Shared::AccountPassword::plain)).toInt()) + settings.value("active").toBool(), + passwordType ); } settings.endArray(); settings.endGroup(); + + qDebug() << "Squawk core is ready"; + emit ready(); } -void Core::Squawk::accountReady() +void Core::Squawk::onAccountNeedPassword() { - --waitingForAccounts; - - if (waitingForAccounts == 0) { - emit ready(); - } -} - -void Core::Squawk::parseAccount( - const QString& login, - const QString& server, - const QString& password, - const QString& name, - const QString& resource, - Shared::AccountPassword passwordType -) -{ - switch (passwordType) { - case Shared::AccountPassword::plain: - addAccount(login, server, password, name, resource, passwordType); - accountReady(); - break; - case Shared::AccountPassword::jammed: { - SimpleCrypt crypto(passwordHash); - QString decrypted = crypto.decryptToString(password); - addAccount(login, server, decrypted, name, resource, passwordType); - accountReady(); - } - break; - case Shared::AccountPassword::alwaysAsk: - addAccount(login, server, QString(), name, resource, passwordType); - emit requestPassword(name); + Account* acc = static_cast(sender()); + switch (acc->getPasswordType()) { + case Shared::AccountPassword::alwaysAsk: + emit requestPassword(acc->getName()); break; case Shared::AccountPassword::kwallet: { - addAccount(login, server, QString(), name, resource, passwordType); #ifdef WITH_KWALLET if (kwallet.supportState() == PSE::KWallet::success) { - kwallet.requestReadPassword(name); + kwallet.requestReadPassword(acc->getName()); } else { #endif - emit requestPassword(name); + emit requestPassword(acc->getName()); #ifdef WITH_KWALLET } #endif + break; } + default: + break; //should never happen; } } @@ -762,16 +753,19 @@ void Core::Squawk::onWalletRejectPassword(const QString& login) emit requestPassword(login); } -void Core::Squawk::onWalletResponsePassword(const QString& login, const QString& password) +void Core::Squawk::responsePassword(const QString& account, const QString& password) { - AccountsMap::const_iterator itr = amap.find(login); + AccountsMap::const_iterator itr = amap.find(account); if (itr == amap.end()) { - qDebug() << "An attempt to set password to non existing account" << login << ", skipping"; + qDebug() << "An attempt to set password to non existing account" << account << ", skipping"; return; } - itr->second->setPassword(password); - emit changeAccount(login, {{"password", password}}); - accountReady(); + Account* acc = itr->second; + acc->setPassword(password); + emit changeAccount(account, {{"password", password}}); + if (state != Shared::Availability::offline && acc->getActive()) { + acc->connect(); + } } void Core::Squawk::onAccountUploadFileError(const QString& jid, const QString id, const QString& errorText) diff --git a/core/squawk.h b/core/squawk.h index 6cd251f..6cb3115 100644 --- a/core/squawk.h +++ b/core/squawk.h @@ -134,7 +134,6 @@ private: AccountsMap amap; Shared::Availability state; NetworkAccess network; - uint8_t waitingForAccounts; bool isInitialized; #ifdef WITH_KWALLET @@ -148,6 +147,7 @@ private slots: const QString& password, const QString& name, const QString& resource, + bool active, Shared::AccountPassword passwordType ); @@ -172,22 +172,22 @@ private slots: void onAccountChangeRoomPresence(const QString& jid, const QString& nick, const QMap& data); void onAccountRemoveRoomPresence(const QString& jid, const QString& nick); void onAccountChangeMessage(const QString& jid, const QString& id, const QMap& data); + void onAccountNeedPassword(); void onAccountUploadFileError(const QString& jid, const QString id, const QString& errorText); void onWalletOpened(bool success); - void onWalletResponsePassword(const QString& login, const QString& password); void onWalletRejectPassword(const QString& login); private: void readSettings(); - void accountReady(); void parseAccount( const QString& login, const QString& server, const QString& password, const QString& name, const QString& resource, + bool active, Shared::AccountPassword passwordType ); diff --git a/ui/dialogqueue.cpp b/ui/dialogqueue.cpp index 1887b28..f5be82b 100644 --- a/ui/dialogqueue.cpp +++ b/ui/dialogqueue.cpp @@ -107,7 +107,7 @@ void DialogQueue::onPropmtRejected() case none: break; case askPassword: - emit squawk->responsePassword(currentSource, prompt->textValue()); + emit squawk->disconnectAccount(currentSource); break; } actionDone(); diff --git a/ui/models/account.cpp b/ui/models/account.cpp index 43cb3ed..cf1efb4 100644 --- a/ui/models/account.cpp +++ b/ui/models/account.cpp @@ -32,7 +32,8 @@ Models::Account::Account(const QMap& data, Models::Item* pare state(Shared::ConnectionState::disconnected), availability(Shared::Availability::offline), passwordType(Shared::AccountPassword::plain), - wasEverConnected(false) + wasEverConnected(false), + active(false) { QMap::const_iterator sItr = data.find("state"); if (sItr != data.end()) { @@ -46,6 +47,10 @@ Models::Account::Account(const QMap& data, Models::Item* pare if (pItr != data.end()) { setPasswordType(pItr.value().toUInt()); } + QMap::const_iterator acItr = data.find("active"); + if (acItr != data.end()) { + setActive(acItr.value().toBool()); + } } Models::Account::~Account() @@ -176,6 +181,8 @@ QVariant Models::Account::data(int column) const return avatarPath; case 9: return Shared::Global::getName(passwordType); + case 10: + return active; default: return QVariant(); } @@ -183,7 +190,7 @@ QVariant Models::Account::data(int column) const int Models::Account::columnCount() const { - return 10; + return 11; } void Models::Account::update(const QString& field, const QVariant& value) @@ -208,6 +215,8 @@ void Models::Account::update(const QString& field, const QVariant& value) setAvatarPath(value.toString()); } else if (field == "passwordType") { setPasswordType(value.toUInt()); + } else if (field == "active") { + setActive(value.toBool()); } } @@ -281,3 +290,16 @@ void Models::Account::setPasswordType(unsigned int pt) { setPasswordType(Shared::Global::fromInt(pt)); } + +bool Models::Account::getActive() const +{ + return active; +} + +void Models::Account::setActive(bool p_active) +{ + if (active != p_active) { + active = p_active; + changed(10); + } +} diff --git a/ui/models/account.h b/ui/models/account.h index 3d2310f..ab2b629 100644 --- a/ui/models/account.h +++ b/ui/models/account.h @@ -58,6 +58,9 @@ namespace Models { void setAvatarPath(const QString& path); QString getAvatarPath() const; + + void setActive(bool active); + bool getActive() const; void setAvailability(Shared::Availability p_avail); void setAvailability(unsigned int p_avail); @@ -91,6 +94,7 @@ namespace Models { Shared::Availability availability; Shared::AccountPassword passwordType; bool wasEverConnected; + bool active; protected slots: void toOfflineState() override; diff --git a/ui/models/accounts.cpp b/ui/models/accounts.cpp index 4343481..463ab40 100644 --- a/ui/models/accounts.cpp +++ b/ui/models/accounts.cpp @@ -48,6 +48,10 @@ QVariant Models::Accounts::data (const QModelIndex& index, int role) const answer = Shared::connectionStateIcon(accs[index.row()]->getState()); } break; + case Qt::ForegroundRole: + if (!accs[index.row()]->getActive()) { + answer = qApp->palette().brush(QPalette::Disabled, QPalette::Text); + } default: break; } diff --git a/ui/models/roster.cpp b/ui/models/roster.cpp index 588fb1d..1355fe3 100644 --- a/ui/models/roster.cpp +++ b/ui/models/roster.cpp @@ -276,6 +276,18 @@ QVariant Models::Roster::data (const QModelIndex& index, int role) const break; } break; + case Qt::ForegroundRole: + switch (item->type) { + case Item::account: { + Account* acc = static_cast(item); + if (!acc->getActive()) { + result = qApp->palette().brush(QPalette::Disabled, QPalette::Text); + } + } + break; + default: + break; + } default: break; } diff --git a/ui/squawk.cpp b/ui/squawk.cpp index 335b8d0..3a3d1d9 100644 --- a/ui/squawk.cpp +++ b/ui/squawk.cpp @@ -234,29 +234,7 @@ void Squawk::newAccount(const QMap& account) void Squawk::onComboboxActivated(int index) { Shared::Availability av = Shared::Global::fromInt(index); - if (av != Shared::Availability::offline) { - int size = rosterModel.accountsModel->rowCount(QModelIndex()); - if (size > 0) { - emit changeState(av); - for (int i = 0; i < size; ++i) { - Models::Account* acc = rosterModel.accountsModel->getAccount(i); - if (acc->getState() == Shared::ConnectionState::disconnected) { - emit connectAccount(acc->getName()); - } - } - } else { - m_ui->comboBox->setCurrentIndex(static_cast(Shared::Availability::offline)); - } - } else { - emit changeState(av); - int size = rosterModel.accountsModel->rowCount(QModelIndex()); - for (int i = 0; i != size; ++i) { - Models::Account* acc = rosterModel.accountsModel->getAccount(i); - if (acc->getState() != Shared::ConnectionState::disconnected) { - emit disconnectAccount(acc->getName()); - } - } - } + emit changeState(av); } void Squawk::changeAccount(const QString& account, const QMap& data) @@ -573,17 +551,12 @@ void Squawk::onRosterContextMenu(const QPoint& point) hasMenu = true; QString name = acc->getName(); - if (acc->getState() != Shared::ConnectionState::disconnected) { - QAction* con = contextMenu->addAction(Shared::icon("network-disconnect"), tr("Disconnect")); - con->setEnabled(active); - connect(con, &QAction::triggered, [this, name]() { - emit disconnectAccount(name); - }); + if (acc->getActive()) { + QAction* con = contextMenu->addAction(Shared::icon("network-disconnect"), tr("Deactivate")); + connect(con, &QAction::triggered, std::bind(&Squawk::disconnectAccount, this, name)); } else { - QAction* con = contextMenu->addAction(Shared::icon("network-connect"), tr("Connect")); - connect(con, &QAction::triggered, [this, name]() { - emit connectAccount(name); - }); + QAction* con = contextMenu->addAction(Shared::icon("network-connect"), tr("Activate")); + connect(con, &QAction::triggered, std::bind(&Squawk::connectAccount, this, name)); } QAction* card = contextMenu->addAction(Shared::icon("user-properties"), tr("VCard")); @@ -591,11 +564,7 @@ void Squawk::onRosterContextMenu(const QPoint& point) connect(card, &QAction::triggered, std::bind(&Squawk::onActivateVCard, this, name, acc->getBareJid(), true)); QAction* remove = contextMenu->addAction(Shared::icon("edit-delete"), tr("Remove")); - remove->setEnabled(active); - connect(remove, &QAction::triggered, [this, name]() { - emit removeAccount(name); - }); - + connect(remove, &QAction::triggered, std::bind(&Squawk::removeAccount, this, name)); } break; case Models::Item::contact: { @@ -839,20 +808,16 @@ void Squawk::readSettings() { QSettings settings; settings.beginGroup("ui"); - + int avail; if (settings.contains("availability")) { - int avail = settings.value("availability").toInt(); - m_ui->comboBox->setCurrentIndex(avail); - emit stateChanged(Shared::Global::fromInt(avail)); - - int size = settings.beginReadArray("connectedAccounts"); - for (int i = 0; i < size; ++i) { - settings.setArrayIndex(i); - emit connectAccount(settings.value("name").toString()); //TODO this is actually not needed, stateChanged event already connects everything you have - } // need to fix that - settings.endArray(); + avail = settings.value("availability").toInt(); + } else { + avail = static_cast(Shared::Availability::online); } settings.endGroup(); + m_ui->comboBox->setCurrentIndex(avail); + + emit changeState(Shared::Global::fromInt(avail)); } void Squawk::writeSettings() @@ -867,19 +832,10 @@ void Squawk::writeSettings() settings.setValue("splitter", m_ui->splitter->saveState()); settings.setValue("availability", m_ui->comboBox->currentIndex()); - settings.beginWriteArray("connectedAccounts"); - int size = rosterModel.accountsModel->rowCount(QModelIndex()); - for (int i = 0; i < size; ++i) { - Models::Account* acc = rosterModel.accountsModel->getAccount(i); - if (acc->getState() != Shared::ConnectionState::disconnected) { - settings.setArrayIndex(i); - settings.setValue("name", acc->getName()); - } - } - settings.endArray(); settings.remove("roster"); settings.beginGroup("roster"); + int size = rosterModel.accountsModel->rowCount(QModelIndex()); for (int i = 0; i < size; ++i) { QModelIndex acc = rosterModel.index(i, 0, QModelIndex()); Models::Account* account = rosterModel.accountsModel->getAccount(i); diff --git a/ui/widgets/accounts/account.cpp b/ui/widgets/accounts/account.cpp index ba3af6b..164af6c 100644 --- a/ui/widgets/accounts/account.cpp +++ b/ui/widgets/accounts/account.cpp @@ -53,6 +53,7 @@ QMap Account::value() const map["name"] = m_ui->name->text(); map["resource"] = m_ui->resource->text(); map["passwordType"] = m_ui->passwordType->currentIndex(); + map["active"] = m_ui->active->isChecked(); return map; } diff --git a/ui/widgets/accounts/account.ui b/ui/widgets/accounts/account.ui index a1879bc..b7f9f26 100644 --- a/ui/widgets/accounts/account.ui +++ b/ui/widgets/accounts/account.ui @@ -7,7 +7,7 @@ 0 0 438 - 342 + 345 @@ -34,7 +34,7 @@ 6 - + Your account login @@ -44,14 +44,14 @@ - + Server - + A server address of your account. Like 404.city or macaw.me @@ -61,21 +61,21 @@ - + Login - + Password - + Password of your account @@ -97,14 +97,14 @@ - + Name - + Just a name how would you call this account, doesn't affect anything @@ -114,14 +114,14 @@ - + Resource - + A resource name like "Home" or "Work" @@ -131,17 +131,17 @@ - + Password storage - + - + @@ -157,6 +157,23 @@ + + + + Active + + + + + + + enable + + + true + + + diff --git a/ui/widgets/accounts/accounts.cpp b/ui/widgets/accounts/accounts.cpp index 7f4a135..82a8ca0 100644 --- a/ui/widgets/accounts/accounts.cpp +++ b/ui/widgets/accounts/accounts.cpp @@ -83,7 +83,8 @@ void Accounts::onEditButton() {"server", mAcc->getServer()}, {"name", mAcc->getName()}, {"resource", mAcc->getResource()}, - {"passwordType", QVariant::fromValue(mAcc->getPasswordType())} + {"passwordType", QVariant::fromValue(mAcc->getPasswordType())}, + {"active", mAcc->getActive()} }); acc->lockId(); connect(acc, &Account::accepted, this, &Accounts::onAccountAccepted); @@ -118,17 +119,17 @@ void Accounts::updateConnectButton() 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::ConnectionState::connected; + allConnected = allConnected && mAcc->getActive(); } if (allConnected) { toDisconnect = true; - m_ui->connectButton->setText(tr("Disconnect")); + m_ui->connectButton->setText(tr("Deactivate")); } else { toDisconnect = false; - m_ui->connectButton->setText(tr("Connect")); + m_ui->connectButton->setText(tr("Activate")); } } else { - m_ui->connectButton->setText(tr("Connect")); + m_ui->connectButton->setText(tr("Activate")); toDisconnect = false; m_ui->connectButton->setEnabled(false); }