1
0
forked from blue/squawk

testing connection

This commit is contained in:
Blue 2019-04-02 18:46:18 +03:00
parent 3d947a0748
commit d4afdd7a5f
11 changed files with 77 additions and 6 deletions

View File

@ -56,7 +56,7 @@ void Core::Account::onClientConnected()
}
}
void Core::Account::onClientDisonnected()
void Core::Account::onClientDisconnected()
{
if (state != Shared::disconnected) {
state = Shared::disconnected;

View File

@ -35,7 +35,7 @@ private:
private slots:
void onClientConnected();
void onClientDisonnected();
void onClientDisconnected();
};

View File

@ -68,7 +68,7 @@ void Core::Squawk::disconnectAccount(const QString& account)
return;
}
itr->second->connect();
itr->second->disconnect();
}
void Core::Squawk::onAccountConnectionStateChanged(int state)

View File

@ -1,6 +1,9 @@
#ifndef GLOBAL_H
#define GLOBAL_H
#include <QString>
#include <deque>
namespace Shared {
enum ConnectionState {
@ -10,6 +13,9 @@ enum ConnectionState {
error
};
static const std::deque<QString> ConnectionStateNames = {"Disconnected", "Connecting", "Connected", "Error"};
static const std::deque<QString> ConnectionStateThemeIcons = {"network-disconnect", "view-refresh", "network-connect", "state-error"};
};
#endif // GLOBAL_H

View File

@ -116,6 +116,12 @@
</item>
</layout>
</widget>
<tabstops>
<tabstop>name</tabstop>
<tabstop>login</tabstop>
<tabstop>server</tabstop>
<tabstop>password</tabstop>
</tabstops>
<resources/>
<connections>
<connection>

View File

@ -41,3 +41,9 @@ void Accounts::addAccount(const QMap<QString, QVariant>& map)
{
tableModel.addAccount(map);
}
void Accounts::updateAccount(const QString& account, const QString& field, const QVariant& value)
{
tableModel.updateAccount(account, field, value);
}

View File

@ -20,6 +20,7 @@ public:
~Accounts() override;
void addAccount(const QMap<QString, QVariant>&);
void updateAccount(const QString& account, const QString& field, const QVariant& value);
signals:
void newAccount(const QMap<QString, QVariant>&);

View File

@ -1,4 +1,7 @@
#include "accounts.h"
#include "../../global.h"
#include <QIcon>
std::deque<QString> 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<QString, QVariant>& 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));
}
}
}
}

View File

@ -16,7 +16,7 @@ public:
~Accounts();
void addAccount(const QMap<QString, QVariant>& 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;

View File

@ -1,16 +1,19 @@
#include "squawk.h"
#include "ui_squawk.h"
#include <QDebug>
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<QString, QVariant>& account)
{
accountsCache.push_back(account);
QMap<QString, QVariant>* 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<QString, QVariant>* 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());
}
}

View File

@ -5,6 +5,7 @@
#include <QScopedPointer>
#include <QCloseEvent>
#include <deque>
#include <map>
#include "accounts.h"
#include "models/roster.h"
@ -34,10 +35,12 @@ public slots:
private:
typedef std::deque<QMap<QString, QVariant>> AC;
typedef std::map<QString, QMap<QString, QVariant>*> AI;
QScopedPointer<Ui::Squawk> m_ui;
Accounts* accounts;
AC accountsCache;
AI accountsIndex;
Models::Roster rosterModel;
protected: