squawk/ui/squawk.cpp

88 lines
2.4 KiB
C++
Raw Normal View History

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),
rosterModel()
2019-03-29 14:54:34 +00:00
{
m_ui->setupUi(this);
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()));
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) {
2019-04-03 18:15:36 +00:00
accounts = new Accounts(rosterModel.accountsModel, this);
2019-03-29 14:54:34 +00:00
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>&)));
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)
{
rosterModel.addAccount(account);
2019-03-30 20:13:13 +00:00
}
void Squawk::onComboboxActivated(int index)
{
if (index == 0) {
2019-04-03 18:15:36 +00:00
int size = rosterModel.accountsModel->rowCount(QModelIndex());
if (size > 0) {
for (int i = 0; i < size; ++i) {
Models::Account* acc = rosterModel.accountsModel->getAccount(i);
if (acc->getState() == Shared::disconnected) {
emit connectAccount(acc->getName());
}
}
} else {
m_ui->comboBox->setCurrentIndex(1);
}
} else if (index == 1) {
2019-04-03 18:15:36 +00:00
int size = rosterModel.accountsModel->rowCount(QModelIndex());
for (int i = 0; i != size; ++i) {
Models::Account* acc = rosterModel.accountsModel->getAccount(i);
if (acc->getState() != Shared::disconnected) {
emit disconnectAccount(acc->getName());
}
}
}
}
void Squawk::accountConnectionStateChanged(const QString& account, int state)
{
2019-04-03 18:15:36 +00:00
rosterModel.updateAccount(account, "state", state);
}