Roster model, account model, connection commena, connection report
This commit is contained in:
parent
6823b41f24
commit
3d947a0748
12 changed files with 551 additions and 15 deletions
|
@ -8,12 +8,67 @@ Account::Account(const QString& p_login, const QString& p_server, const QString&
|
|||
login(p_login),
|
||||
server(p_server),
|
||||
password(p_password),
|
||||
client()
|
||||
client(),
|
||||
state(Shared::disconnected)
|
||||
{
|
||||
|
||||
QObject::connect(&client, SIGNAL(connected()), this, SLOT(onClientConnected()));
|
||||
QObject::connect(&client, SIGNAL(disconnected()), this, SLOT(onClientDisconnected()));
|
||||
}
|
||||
|
||||
Account::~Account()
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
Shared::ConnectionState Core::Account::getState() const
|
||||
{
|
||||
return state;
|
||||
}
|
||||
|
||||
|
||||
void Core::Account::connect()
|
||||
{
|
||||
if (state == Shared::disconnected) {
|
||||
client.connectToServer(login + "@" + server, password);
|
||||
state = Shared::connecting;
|
||||
emit connectionStateChanged(state);
|
||||
} else {
|
||||
qDebug("An attempt to connect an account which is already connected, skipping");
|
||||
}
|
||||
}
|
||||
|
||||
void Core::Account::disconnect()
|
||||
{
|
||||
if (state != Shared::disconnected) {
|
||||
client.disconnect();
|
||||
state = Shared::disconnected;
|
||||
emit connectionStateChanged(state);
|
||||
}
|
||||
}
|
||||
|
||||
void Core::Account::onClientConnected()
|
||||
{
|
||||
if (state == Shared::connecting) {
|
||||
state = Shared::connected;
|
||||
emit connectionStateChanged(state);
|
||||
} else {
|
||||
qDebug("Something weird had happened - xmpp client reported about successful connection but account wasn't in connecting state");
|
||||
}
|
||||
}
|
||||
|
||||
void Core::Account::onClientDisonnected()
|
||||
{
|
||||
if (state != Shared::disconnected) {
|
||||
state = Shared::disconnected;
|
||||
emit connectionStateChanged(state);
|
||||
} else {
|
||||
qDebug("Something weird had happened - xmpp client reported about being disconnection but account was already in disconnected state");
|
||||
}
|
||||
}
|
||||
|
||||
QString Core::Account::getName() const
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -4,22 +4,39 @@
|
|||
#include <QtCore/QObject>
|
||||
|
||||
#include <qxmpp/QXmppClient.h>
|
||||
#include "../global.h"
|
||||
|
||||
namespace Core
|
||||
{
|
||||
|
||||
class Account : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
Account(const QString& p_login, const QString& p_server, const QString& p_password, const QString& p_name, QObject* parent = 0);
|
||||
~Account();
|
||||
|
||||
void connect();
|
||||
void disconnect();
|
||||
|
||||
Shared::ConnectionState getState() const;
|
||||
QString getName() const;
|
||||
|
||||
signals:
|
||||
void connectionStateChanged(int);
|
||||
|
||||
private:
|
||||
QString name;
|
||||
QString login;
|
||||
QString server;
|
||||
QString password;
|
||||
QXmppClient client;
|
||||
Shared::ConnectionState state;
|
||||
|
||||
private slots:
|
||||
void onClientConnected();
|
||||
void onClientDisonnected();
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -3,7 +3,8 @@
|
|||
|
||||
Core::Squawk::Squawk(QObject* parent):
|
||||
QObject(parent),
|
||||
accounts()
|
||||
accounts(),
|
||||
amap()
|
||||
{
|
||||
|
||||
}
|
||||
|
@ -35,13 +36,44 @@ void Core::Squawk::addAccount(const QString& login, const QString& server, const
|
|||
{
|
||||
Account* acc = new Account(login, server, password, name);
|
||||
accounts.push_back(acc);
|
||||
amap.insert(std::make_pair(name, acc));
|
||||
|
||||
connect(acc, SIGNAL(connectionStateChanged(int)), this, SLOT(onAccountConnectionStateChanged(int)));
|
||||
|
||||
QMap<QString, QVariant> map = {
|
||||
{"login", login},
|
||||
{"server", server},
|
||||
{"name", name},
|
||||
{"password", password},
|
||||
{"state", 0}
|
||||
{"state", Shared::disconnected}
|
||||
};
|
||||
emit newAccount(map);
|
||||
}
|
||||
|
||||
void Core::Squawk::connectAccount(const QString& account)
|
||||
{
|
||||
AccountsMap::const_iterator itr = amap.find(account);
|
||||
if (itr == amap.end()) {
|
||||
qDebug("An attempt to connect non existing account, skipping");
|
||||
return;
|
||||
}
|
||||
itr->second->connect();
|
||||
}
|
||||
|
||||
void Core::Squawk::disconnectAccount(const QString& account)
|
||||
{
|
||||
AccountsMap::const_iterator itr = amap.find(account);
|
||||
if (itr == amap.end()) {
|
||||
qDebug("An attempt to connect non existing account, skipping");
|
||||
return;
|
||||
}
|
||||
|
||||
itr->second->connect();
|
||||
}
|
||||
|
||||
void Core::Squawk::onAccountConnectionStateChanged(int state)
|
||||
{
|
||||
Account* acc = static_cast<Account*>(sender());
|
||||
emit accountConnectionStateChanged(acc->getName(), state);
|
||||
}
|
||||
|
||||
|
|
|
@ -6,8 +6,10 @@
|
|||
#include <QVariant>
|
||||
#include <QMap>
|
||||
#include <deque>
|
||||
#include <deque>
|
||||
|
||||
#include "account.h"
|
||||
#include "../global.h"
|
||||
|
||||
namespace Core
|
||||
{
|
||||
|
@ -21,18 +23,26 @@ public:
|
|||
|
||||
signals:
|
||||
void newAccount(const QMap<QString, QVariant>&);
|
||||
void accountConnectionStateChanged(const QString&, int);
|
||||
|
||||
public slots:
|
||||
void start();
|
||||
void newAccountRequest(const QMap<QString, QVariant>& map);
|
||||
void connectAccount(const QString& account);
|
||||
void disconnectAccount(const QString& account);
|
||||
|
||||
private:
|
||||
typedef std::deque<Account*> Accounts;
|
||||
typedef std::map<QString, Account*> AccountsMap;
|
||||
|
||||
Accounts accounts;
|
||||
AccountsMap amap;
|
||||
|
||||
private:
|
||||
void addAccount(const QString& login, const QString& server, const QString& password, const QString& name);
|
||||
|
||||
private slots:
|
||||
void onAccountConnectionStateChanged(int state);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue