From 3e594c7e13cd7c73dbaaaeffab39f90cb0cddda3 Mon Sep 17 00:00:00 2001 From: blue Date: Wed, 25 Dec 2019 13:24:20 +0300 Subject: [PATCH] connectivity, roster position size and state, expanded anccounts and groups restoration with the settings --- main.cpp | 7 ++- ui/models/roster.cpp | 29 ++++++++-- ui/models/roster.h | 2 + ui/squawk.cpp | 122 +++++++++++++++++++++++++++++++++++++++++++ ui/squawk.h | 6 +++ 5 files changed, 162 insertions(+), 4 deletions(-) diff --git a/main.cpp b/main.cpp index ce23acc..a38852b 100644 --- a/main.cpp +++ b/main.cpp @@ -39,7 +39,7 @@ int main(int argc, char *argv[]) QApplication::setApplicationName("squawk"); QApplication::setApplicationDisplayName("Squawk"); - QApplication::setApplicationVersion("0.1.1"); + QApplication::setApplicationVersion("0.1.2"); QTranslator qtTranslator; qtTranslator.load("qt_" + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath)); @@ -81,6 +81,7 @@ int main(int argc, char *argv[]) QObject::connect(coreThread, &QThread::started, squawk, &Core::Squawk::start); QObject::connect(&app, &QApplication::aboutToQuit, squawk, &Core::Squawk::stop); + QObject::connect(&app, &QApplication::aboutToQuit, &w, &QMainWindow::close); QObject::connect(squawk, &Core::Squawk::quit, coreThread, &QThread::quit); QObject::connect(coreThread, &QThread::finished, squawk, &Core::Squawk::deleteLater); @@ -141,10 +142,14 @@ int main(int argc, char *argv[]) QObject::connect(squawk, &Core::Squawk::responseVCard, &w, &Squawk::responseVCard); coreThread->start(); + w.readSettings(); int result = app.exec(); + + w.writeSettings(); coreThread->wait(500); //TODO hate doing that but settings for some reason don't get saved to the disk + return result; } diff --git a/ui/models/roster.cpp b/ui/models/roster.cpp index 903a3a8..97aa192 100644 --- a/ui/models/roster.cpp +++ b/ui/models/roster.cpp @@ -310,9 +310,6 @@ Qt::ItemFlags Models::Roster::flags(const QModelIndex& index) const int Models::Roster::rowCount (const QModelIndex& parent) const { Item *parentItem; - if (parent.column() > 0) { - return 0; - } if (!parent.isValid()) { parentItem = root; @@ -966,3 +963,29 @@ Models::Account * Models::Roster::getAccount(const QString& name) { return accounts.find(name)->second; } + +QModelIndex Models::Roster::getAccountIndex(const QString& name) +{ + std::map::const_iterator itr = accounts.find(name); + if (itr == accounts.end()) { + return QModelIndex(); + } else { + return index(itr->second->row(), 0, QModelIndex()); + } +} + +QModelIndex Models::Roster::getGroupIndex(const QString& account, const QString& name) +{ + std::map::const_iterator itr = accounts.find(account); + if (itr == accounts.end()) { + return QModelIndex(); + } else { + std::map::const_iterator gItr = groups.find({account, name}); + if (gItr == groups.end()) { + return QModelIndex(); + } else { + QModelIndex accIndex = index(itr->second->row(), 0, QModelIndex()); + return index(gItr->second->row(), 0, accIndex); + } + } +} diff --git a/ui/models/roster.h b/ui/models/roster.h index 07ab495..a2ffdc2 100644 --- a/ui/models/roster.h +++ b/ui/models/roster.h @@ -75,6 +75,8 @@ public: bool groupHasContact(const QString& account, const QString& group, const QString& contactJID) const; QString getContactIconPath(const QString& account, const QString& jid); Account* getAccount(const QString& name); + QModelIndex getAccountIndex(const QString& name); + QModelIndex getGroupIndex(const QString& account, const QString& name); Accounts* accountsModel; diff --git a/ui/squawk.cpp b/ui/squawk.cpp index 167b8ed..ce648ba 100644 --- a/ui/squawk.cpp +++ b/ui/squawk.cpp @@ -53,6 +53,7 @@ Squawk::Squawk(QWidget *parent) : connect(m_ui->comboBox, qOverload(&QComboBox::activated), this, &Squawk::onComboboxActivated); connect(m_ui->roster, &QTreeView::doubleClicked, this, &Squawk::onRosterItemDoubleClicked); connect(m_ui->roster, &QTreeView::customContextMenuRequested, this, &Squawk::onRosterContextMenu); + connect(m_ui->roster, &QTreeView::collapsed, this, &Squawk::onItemCollepsed); connect(rosterModel.accountsModel, &Models::Accounts::sizeChanged, this, &Squawk::onAccountsSizeChanged); //m_ui->mainToolBar->addWidget(m_ui->comboBox); @@ -205,11 +206,40 @@ void Squawk::changeAccount(const QString& account, const QMap void Squawk::addContact(const QString& account, const QString& jid, const QString& group, const QMap& data) { rosterModel.addContact(account, jid, group, data); + + QSettings settings; + settings.beginGroup("ui"); + settings.beginGroup("roster"); + settings.beginGroup(account); + if (settings.value("expanded", false).toBool()) { + QModelIndex ind = rosterModel.getAccountIndex(account); + qDebug() << "expanding account " << ind.data(); + m_ui->roster->expand(ind); + } + settings.endGroup(); + settings.endGroup(); + settings.endGroup(); } void Squawk::addGroup(const QString& account, const QString& name) { rosterModel.addGroup(account, name); + + QSettings settings; + settings.beginGroup("ui"); + settings.beginGroup("roster"); + settings.beginGroup(account); + if (settings.value("expanded", false).toBool()) { + QModelIndex ind = rosterModel.getAccountIndex(account); + qDebug() << "expanding account " << ind.data(); + m_ui->roster->expand(ind); + if (settings.value(name + "/expanded", false).toBool()) { + m_ui->roster->expand(rosterModel.getGroupIndex(account, name)); + } + } + settings.endGroup(); + settings.endGroup(); + settings.endGroup(); } void Squawk::removeGroup(const QString& account, const QString& name) @@ -813,3 +843,95 @@ void Squawk::onVCardSave(const Shared::VCard& card, const QString& account) widget->deleteLater(); } + +void Squawk::readSettings() +{ + QSettings settings; + settings.beginGroup("ui"); + settings.beginGroup("window"); + if (settings.contains("geometry")) { + restoreGeometry(settings.value("geometry").toByteArray()); + } + if (settings.contains("state")) { + restoreState(settings.value("state").toByteArray()); + } + settings.endGroup(); + + if (settings.contains("availability")) { + int avail = settings.value("availability").toInt(); + m_ui->comboBox->setCurrentIndex(avail); + emit stateChanged(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(); + } + + settings.endGroup(); +} + +void Squawk::writeSettings() +{ + QSettings settings; + settings.beginGroup("ui"); + settings.beginGroup("window"); + settings.setValue("geometry", saveGeometry()); + settings.setValue("state", saveState()); + settings.endGroup(); + + 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::disconnected) { + settings.setArrayIndex(i); + settings.setValue("name", acc->getName()); + } + } + settings.endArray(); + + settings.remove("roster"); + settings.beginGroup("roster"); + for (int i = 0; i < size; ++i) { + QModelIndex acc = rosterModel.index(i, 0, QModelIndex()); + Models::Account* account = rosterModel.accountsModel->getAccount(i); + QString accName = account->getName(); + settings.beginGroup(accName); + + settings.setValue("expanded", m_ui->roster->isExpanded(acc)); + std::deque groups = rosterModel.groupList(accName); + for (const QString& groupName : groups) { + settings.beginGroup(groupName); + QModelIndex gIndex = rosterModel.getGroupIndex(accName, groupName); + settings.setValue("expanded", m_ui->roster->isExpanded(gIndex)); + settings.endGroup(); + } + + settings.endGroup(); + } + settings.endGroup(); + settings.endGroup(); +} + +void Squawk::onItemCollepsed(const QModelIndex& index) +{ + QSettings settings; + Models::Item* item = static_cast(index.internalPointer()); + switch (item->type) { + case Models::Item::account: + settings.setValue("ui/roster/" + item->getName() + "/expanded", false); + break; + case Models::Item::group: { + QModelIndex accInd = rosterModel.parent(index); + Models::Account* account = rosterModel.accountsModel->getAccount(accInd.row()); + settings.setValue("ui/roster/" + account->getName() + "/" + item->getName() + "/expanded", false); + } + break; + default: + break; + } +} diff --git a/ui/squawk.h b/ui/squawk.h index b464fd2..f0b21c7 100644 --- a/ui/squawk.h +++ b/ui/squawk.h @@ -23,6 +23,8 @@ #include #include #include +#include + #include #include #include @@ -50,6 +52,9 @@ public: explicit Squawk(QWidget *parent = nullptr); ~Squawk() override; + void readSettings(); + void writeSettings(); + signals: void newAccountRequest(const QMap&); void modifyAccountRequest(const QString&, const QMap&); @@ -101,6 +106,7 @@ public slots: void fileError(const QString& messageId, const QString& error); void fileProgress(const QString& messageId, qreal value); void responseVCard(const QString& jid, const Shared::VCard& card); + void onItemCollepsed(const QModelIndex& index); private: typedef std::map Conversations;