squawk/core/squawk.cpp

80 lines
2.0 KiB
C++
Raw Normal View History

2019-03-29 14:54:34 +00:00
#include "squawk.h"
2019-03-30 20:13:13 +00:00
#include <QDebug>
2019-03-29 14:54:34 +00:00
2019-03-30 20:13:13 +00:00
Core::Squawk::Squawk(QObject* parent):
2019-03-29 14:54:34 +00:00
QObject(parent),
accounts(),
amap()
2019-03-29 14:54:34 +00:00
{
}
2019-03-30 20:13:13 +00:00
Core::Squawk::~Squawk()
2019-03-29 14:54:34 +00:00
{
2019-03-30 20:13:13 +00:00
Accounts::const_iterator itr = accounts.begin();
Accounts::const_iterator end = accounts.end();
for (; itr != end; ++itr) {
(*itr)->deleteLater();
}
}
void Core::Squawk::start()
{
}
2019-03-29 14:54:34 +00:00
2019-03-30 20:13:13 +00:00
void Core::Squawk::newAccountRequest(const QMap<QString, QVariant>& map)
{
QString name = map.value("name").toString();
QString login = map.value("login").toString();
QString server = map.value("server").toString();
QString password = map.value("password").toString();
addAccount(login, server, password, name);
}
void Core::Squawk::addAccount(const QString& login, const QString& server, const QString& password, const QString& name)
{
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)));
2019-03-30 20:13:13 +00:00
QMap<QString, QVariant> map = {
{"login", login},
{"server", server},
{"name", name},
{"password", password},
{"state", Shared::disconnected}
2019-03-30 20:13:13 +00:00
};
emit newAccount(map);
2019-03-29 14:54:34 +00:00
}
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);
}