2019-03-29 14:54:34 +00:00
|
|
|
#include "squawk.h"
|
|
|
|
#include "ui_squawk.h"
|
2019-04-02 15:46:18 +00:00
|
|
|
#include <QDebug>
|
2019-03-29 14:54:34 +00:00
|
|
|
|
|
|
|
Squawk::Squawk(QWidget *parent) :
|
|
|
|
QMainWindow(parent),
|
|
|
|
m_ui(new Ui::Squawk),
|
2019-03-30 20:13:13 +00:00
|
|
|
accounts(0),
|
2019-03-31 21:05:09 +00:00
|
|
|
accountsCache(),
|
2019-04-02 15:46:18 +00:00
|
|
|
accountsIndex(),
|
2019-03-31 21:05:09 +00:00
|
|
|
rosterModel()
|
2019-03-29 14:54:34 +00:00
|
|
|
{
|
|
|
|
m_ui->setupUi(this);
|
2019-03-31 21:05:09 +00:00
|
|
|
m_ui->roster->setModel(&rosterModel);
|
2019-03-29 14:54:34 +00:00
|
|
|
|
2019-04-02 15:46:18 +00:00
|
|
|
connect(m_ui->actionAccounts, SIGNAL(triggered()), this, SLOT(onAccounts()));
|
2019-03-31 21:05:09 +00:00
|
|
|
connect(m_ui->comboBox, SIGNAL(activated(int)), this, SLOT(onComboboxActivated(int)));
|
|
|
|
//m_ui->mainToolBar->addWidget(m_ui->comboBox);
|
2019-03-29 14:54:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Squawk::~Squawk() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Squawk::onAccounts()
|
|
|
|
{
|
|
|
|
if (accounts == 0) {
|
|
|
|
accounts = new Accounts(this);
|
|
|
|
accounts->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
connect(accounts, SIGNAL(destroyed(QObject*)), this, SLOT(onAccountsClosed(QObject*)));
|
2019-03-30 20:13:13 +00:00
|
|
|
connect(accounts, SIGNAL(newAccount(const QMap<QString, QVariant>&)), this, SIGNAL(newAccountRequest(const QMap<QString, QVariant>&)));
|
|
|
|
|
|
|
|
AC::const_iterator itr = accountsCache.begin();
|
|
|
|
AC::const_iterator end = accountsCache.end();
|
|
|
|
|
|
|
|
for (; itr != end; ++itr) {
|
|
|
|
accounts->addAccount(*itr);
|
|
|
|
}
|
|
|
|
|
2019-03-29 14:54:34 +00:00
|
|
|
accounts->show();
|
|
|
|
} else {
|
2019-03-30 20:13:13 +00:00
|
|
|
accounts->show();
|
|
|
|
accounts->raise();
|
|
|
|
accounts->activateWindow();
|
2019-03-29 14:54:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Squawk::closeEvent(QCloseEvent* event)
|
|
|
|
{
|
|
|
|
if (accounts != 0) {
|
|
|
|
accounts->close();
|
|
|
|
}
|
|
|
|
|
|
|
|
QMainWindow::closeEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Squawk::onAccountsClosed(QObject* parent)
|
|
|
|
{
|
|
|
|
accounts = 0;
|
|
|
|
}
|
2019-03-30 20:13:13 +00:00
|
|
|
|
|
|
|
void Squawk::newAccount(const QMap<QString, QVariant>& account)
|
|
|
|
{
|
|
|
|
accountsCache.push_back(account);
|
2019-04-02 15:46:18 +00:00
|
|
|
QMap<QString, QVariant>* acc = &accountsCache.back();
|
|
|
|
accountsIndex.insert(std::make_pair(acc->value("name").toString(), acc));
|
2019-03-31 21:05:09 +00:00
|
|
|
rosterModel.addAccount(account);
|
2019-03-30 20:13:13 +00:00
|
|
|
if (accounts != 0) {
|
|
|
|
accounts->addAccount(account);
|
|
|
|
}
|
|
|
|
}
|
2019-03-31 21:05:09 +00:00
|
|
|
|
|
|
|
void Squawk::onComboboxActivated(int index)
|
|
|
|
{
|
|
|
|
if (index == 0) {
|
|
|
|
if (accountsCache.size() > 0) {
|
|
|
|
AC::const_iterator itr = accountsCache.begin();
|
|
|
|
AC::const_iterator end = accountsCache.end();
|
|
|
|
|
|
|
|
for (; itr != end; ++itr) {
|
|
|
|
const QMap<QString, QVariant>& acc = *itr;
|
|
|
|
if (acc.value("state").toInt() == Shared::disconnected) {
|
|
|
|
emit connectAccount(acc.value("name").toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
m_ui->comboBox->setCurrentIndex(1);
|
|
|
|
}
|
|
|
|
} else if (index == 1) {
|
|
|
|
AC::const_iterator itr = accountsCache.begin();
|
|
|
|
AC::const_iterator end = accountsCache.end();
|
|
|
|
|
|
|
|
for (; itr != end; ++itr) {
|
|
|
|
const QMap<QString, QVariant>& acc = *itr;
|
|
|
|
if (acc.value("state").toInt() != Shared::disconnected) {
|
|
|
|
emit disconnectAccount(acc.value("name").toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Squawk::accountConnectionStateChanged(const QString& account, int state)
|
|
|
|
{
|
2019-04-02 15:46:18 +00:00
|
|
|
AI::iterator itr = accountsIndex.find(account);
|
|
|
|
if (itr != accountsIndex.end()) {
|
|
|
|
QMap<QString, QVariant>* acc = itr->second;
|
|
|
|
acc->insert("state", state);
|
|
|
|
|
2019-04-02 21:58:43 +00:00
|
|
|
rosterModel.updateAccount(account, "state", state);
|
2019-04-02 15:46:18 +00:00
|
|
|
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());
|
|
|
|
}
|
2019-03-31 21:05:09 +00:00
|
|
|
}
|