diff --git a/core/account.cpp b/core/account.cpp index 1bf4664..169d9e1 100644 --- a/core/account.cpp +++ b/core/account.cpp @@ -56,7 +56,7 @@ void Core::Account::onClientConnected() } } -void Core::Account::onClientDisonnected() +void Core::Account::onClientDisconnected() { if (state != Shared::disconnected) { state = Shared::disconnected; diff --git a/core/account.h b/core/account.h index 5e3c612..c4a211f 100644 --- a/core/account.h +++ b/core/account.h @@ -35,7 +35,7 @@ private: private slots: void onClientConnected(); - void onClientDisonnected(); + void onClientDisconnected(); }; diff --git a/core/squawk.cpp b/core/squawk.cpp index 1a8cfe0..6537ec1 100644 --- a/core/squawk.cpp +++ b/core/squawk.cpp @@ -68,7 +68,7 @@ void Core::Squawk::disconnectAccount(const QString& account) return; } - itr->second->connect(); + itr->second->disconnect(); } void Core::Squawk::onAccountConnectionStateChanged(int state) diff --git a/global.h b/global.h index 5b7d0bd..c4b5950 100644 --- a/global.h +++ b/global.h @@ -1,6 +1,9 @@ #ifndef GLOBAL_H #define GLOBAL_H +#include +#include + namespace Shared { enum ConnectionState { @@ -10,6 +13,9 @@ enum ConnectionState { error }; +static const std::deque ConnectionStateNames = {"Disconnected", "Connecting", "Connected", "Error"}; +static const std::deque ConnectionStateThemeIcons = {"network-disconnect", "view-refresh", "network-connect", "state-error"}; + }; #endif // GLOBAL_H diff --git a/ui/account.ui b/ui/account.ui index 5dc7f9a..3d1722a 100644 --- a/ui/account.ui +++ b/ui/account.ui @@ -116,6 +116,12 @@ + + name + login + server + password + diff --git a/ui/accounts.cpp b/ui/accounts.cpp index c6d9380..5dc4d5c 100644 --- a/ui/accounts.cpp +++ b/ui/accounts.cpp @@ -41,3 +41,9 @@ void Accounts::addAccount(const QMap& map) { tableModel.addAccount(map); } + +void Accounts::updateAccount(const QString& account, const QString& field, const QVariant& value) +{ + tableModel.updateAccount(account, field, value); +} + diff --git a/ui/accounts.h b/ui/accounts.h index 61fbe03..afedb66 100644 --- a/ui/accounts.h +++ b/ui/accounts.h @@ -20,6 +20,7 @@ public: ~Accounts() override; void addAccount(const QMap&); + void updateAccount(const QString& account, const QString& field, const QVariant& value); signals: void newAccount(const QMap&); diff --git a/ui/models/accounts.cpp b/ui/models/accounts.cpp index 5b976e3..4e1db22 100644 --- a/ui/models/accounts.cpp +++ b/ui/models/accounts.cpp @@ -1,4 +1,7 @@ #include "accounts.h" +#include "../../global.h" + +#include std::deque Models::Accounts::columns = { "name", @@ -32,11 +35,16 @@ QVariant Models::Accounts::data ( const QModelIndex& index, int role ) const answer = acc.server; break; case 2: - answer = acc.state; + answer = Shared::ConnectionStateNames[acc.state]; break; } } break; + case Qt::DecorationRole: + if (index.column() == 2) { + answer = QIcon::fromTheme(Shared::ConnectionStateThemeIcons[accs[index.row()].state]); + } + break; default: break; } @@ -75,3 +83,27 @@ void Models::Accounts::addAccount(const QMap& map) }); endInsertRows(); } + +void Models::Accounts::updateAccount(const QString& account, const QString& field, const QVariant& value) +{ + for (int i = 0; i < accs.size(); ++i) { + Account& acc = accs[i]; + if (acc.name == account) { + if (field == "name") { + acc.name = value.toString(); + emit dataChanged(createIndex(i, 0), createIndex(i, 0)); + } else if (field == "server") { + acc.server = value.toString(); + emit dataChanged(createIndex(i, 1), createIndex(i, 1)); + } else if (field == "login") { + acc.login = value.toString(); + } else if (field == "password") { + acc.password = value.toString(); + } else if (field == "state") { + acc.state = value.toInt(); + emit dataChanged(createIndex(i, 2), createIndex(i, 2)); + } + } + } +} + diff --git a/ui/models/accounts.h b/ui/models/accounts.h index 1637b2e..892cec4 100644 --- a/ui/models/accounts.h +++ b/ui/models/accounts.h @@ -16,7 +16,7 @@ public: ~Accounts(); void addAccount(const QMap& map); - + void updateAccount(const QString& account, const QString& field, const QVariant& value); QVariant data ( const QModelIndex& index, int role ) const override; int columnCount ( const QModelIndex& parent ) const override; diff --git a/ui/squawk.cpp b/ui/squawk.cpp index f16d412..3554d1c 100644 --- a/ui/squawk.cpp +++ b/ui/squawk.cpp @@ -1,16 +1,19 @@ #include "squawk.h" #include "ui_squawk.h" +#include Squawk::Squawk(QWidget *parent) : QMainWindow(parent), m_ui(new Ui::Squawk), accounts(0), accountsCache(), + accountsIndex(), rosterModel() { m_ui->setupUi(this); m_ui->roster->setModel(&rosterModel); + connect(m_ui->actionAccounts, SIGNAL(triggered()), this, SLOT(onAccounts())); connect(m_ui->comboBox, SIGNAL(activated(int)), this, SLOT(onComboboxActivated(int))); //m_ui->mainToolBar->addWidget(m_ui->comboBox); } @@ -60,6 +63,8 @@ void Squawk::onAccountsClosed(QObject* parent) void Squawk::newAccount(const QMap& account) { accountsCache.push_back(account); + QMap* acc = &accountsCache.back(); + accountsIndex.insert(std::make_pair(acc->value("name").toString(), acc)); rosterModel.addAccount(account); if (accounts != 0) { accounts->addAccount(account); @@ -97,5 +102,17 @@ void Squawk::onComboboxActivated(int index) void Squawk::accountConnectionStateChanged(const QString& account, int state) { - + AI::iterator itr = accountsIndex.find(account); + if (itr != accountsIndex.end()) { + QMap* acc = itr->second; + acc->insert("state", state); + + if (accounts != 0) { + accounts->updateAccount(account, "state", state); + } + } else { + QString msg("A notification about connection state change of an unknown account "); + msg += account + ", skipping"; + qDebug("%s", msg.toStdString().c_str()); + } } diff --git a/ui/squawk.h b/ui/squawk.h index 8ba47c7..32da189 100644 --- a/ui/squawk.h +++ b/ui/squawk.h @@ -5,6 +5,7 @@ #include #include #include +#include #include "accounts.h" #include "models/roster.h" @@ -34,10 +35,12 @@ public slots: private: typedef std::deque> AC; + typedef std::map*> AI; QScopedPointer m_ui; Accounts* accounts; AC accountsCache; + AI accountsIndex; Models::Roster rosterModel; protected: