connectivity, roster position size and state, expanded anccounts and groups restoration with the settings

This commit is contained in:
Blue 2019-12-25 13:24:20 +03:00
parent 0bcfd779b8
commit 3e594c7e13
5 changed files with 162 additions and 4 deletions

View File

@ -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;
}

View File

@ -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<QString, Account*>::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<QString, Account*>::const_iterator itr = accounts.find(account);
if (itr == accounts.end()) {
return QModelIndex();
} else {
std::map<ElId, Group*>::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);
}
}
}

View File

@ -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;

View File

@ -53,6 +53,7 @@ Squawk::Squawk(QWidget *parent) :
connect(m_ui->comboBox, qOverload<int>(&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<QString, QVariant>
void Squawk::addContact(const QString& account, const QString& jid, const QString& group, const QMap<QString, QVariant>& 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<QString> 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<Models::Item*>(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;
}
}

View File

@ -23,6 +23,8 @@
#include <QScopedPointer>
#include <QCloseEvent>
#include <QtDBus/QDBusInterface>
#include <QSettings>
#include <deque>
#include <map>
#include <set>
@ -50,6 +52,9 @@ public:
explicit Squawk(QWidget *parent = nullptr);
~Squawk() override;
void readSettings();
void writeSettings();
signals:
void newAccountRequest(const QMap<QString, QVariant>&);
void modifyAccountRequest(const QString&, const QMap<QString, QVariant>&);
@ -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<Models::Roster::ElId, Conversation*> Conversations;