Accounts saving, tree status, sigint catching

This commit is contained in:
Blue 2019-04-03 00:58:43 +03:00
parent d4afdd7a5f
commit 4a4ba47968
11 changed files with 251 additions and 15 deletions

View File

@ -11,6 +11,7 @@ set(CMAKE_AUTOUIC ON)
find_package(Qt5Widgets CONFIG REQUIRED) find_package(Qt5Widgets CONFIG REQUIRED)
set(squawk_SRC set(squawk_SRC
main.cpp main.cpp
signalcatcher.cpp
) )
# Tell CMake to create the helloworld executable # Tell CMake to create the helloworld executable

View File

@ -40,7 +40,7 @@ void Core::Account::connect()
void Core::Account::disconnect() void Core::Account::disconnect()
{ {
if (state != Shared::disconnected) { if (state != Shared::disconnected) {
client.disconnect(); client.disconnectFromServer();
state = Shared::disconnected; state = Shared::disconnected;
emit connectionStateChanged(state); emit connectionStateChanged(state);
} }
@ -71,4 +71,17 @@ QString Core::Account::getName() const
return name; return name;
} }
QString Core::Account::getLogin() const
{
return login;
}
QString Core::Account::getPassword() const
{
return password;
}
QString Core::Account::getServer() const
{
return server;
}

View File

@ -21,6 +21,9 @@ public:
Shared::ConnectionState getState() const; Shared::ConnectionState getState() const;
QString getName() const; QString getName() const;
QString getLogin() const;
QString getServer() const;
QString getPassword() const;
signals: signals:
void connectionStateChanged(int); void connectionStateChanged(int);

View File

@ -1,5 +1,6 @@
#include "squawk.h" #include "squawk.h"
#include <QDebug> #include <QDebug>
#include <QSettings>
Core::Squawk::Squawk(QObject* parent): Core::Squawk::Squawk(QObject* parent):
QObject(parent), QObject(parent),
@ -14,12 +15,46 @@ Core::Squawk::~Squawk()
Accounts::const_iterator itr = accounts.begin(); Accounts::const_iterator itr = accounts.begin();
Accounts::const_iterator end = accounts.end(); Accounts::const_iterator end = accounts.end();
for (; itr != end; ++itr) { for (; itr != end; ++itr) {
(*itr)->deleteLater(); delete (*itr);
} }
} }
void Core::Squawk::stop()
{
qDebug("Stopping squawk core..");
QSettings settings;
settings.beginGroup("core");
settings.beginWriteArray("accounts");
for (int i = 0; i < accounts.size(); ++i) {
settings.setArrayIndex(i);
Account* acc = accounts[i];
settings.setValue("name", acc->getName());
settings.setValue("server", acc->getServer());
settings.setValue("login", acc->getLogin());
settings.setValue("password", acc->getPassword());
}
settings.endArray();
settings.endGroup();
settings.sync();
emit quit();
}
void Core::Squawk::start() void Core::Squawk::start()
{ {
qDebug("Starting squawk core..");
QSettings settings;
settings.beginGroup("core");
int size = settings.beginReadArray("accounts");
for (int i = 0; i < size; ++i) {
settings.setArrayIndex(i);
addAccount(settings.value("login").toString(), settings.value("server").toString(), settings.value("password").toString(), settings.value("name").toString());
}
settings.endArray();
settings.endGroup();
} }
void Core::Squawk::newAccountRequest(const QMap<QString, QVariant>& map) void Core::Squawk::newAccountRequest(const QMap<QString, QVariant>& map)

View File

@ -22,11 +22,13 @@ public:
~Squawk(); ~Squawk();
signals: signals:
void quit();
void newAccount(const QMap<QString, QVariant>&); void newAccount(const QMap<QString, QVariant>&);
void accountConnectionStateChanged(const QString&, int); void accountConnectionStateChanged(const QString&, int);
public slots: public slots:
void start(); void start();
void stop();
void newAccountRequest(const QMap<QString, QVariant>& map); void newAccountRequest(const QMap<QString, QVariant>& map);
void connectAccount(const QString& account); void connectAccount(const QString& account);
void disconnectAccount(const QString& account); void disconnectAccount(const QString& account);

View File

@ -1,12 +1,21 @@
#include "ui/squawk.h" #include "ui/squawk.h"
#include "core/squawk.h" #include "core/squawk.h"
#include "signalcatcher.h"
#include <QtWidgets/QApplication> #include <QtWidgets/QApplication>
#include <QtCore/QThread> #include <QtCore/QThread>
#include <QtCore/QObject> #include <QtCore/QObject>
#include <QSettings>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QApplication app(argc, argv); QApplication app(argc, argv);
SignalCatcher sc(&app);
QCoreApplication::setOrganizationName("Macaw");
QCoreApplication::setOrganizationDomain("macaw.me");
QCoreApplication::setApplicationName("Squawk");
QCoreApplication::setApplicationVersion("0.0.1");
Squawk w; Squawk w;
w.show(); w.show();
@ -14,8 +23,10 @@ int main(int argc, char *argv[])
QThread* coreThread = new QThread(); QThread* coreThread = new QThread();
squawk->moveToThread(coreThread); squawk->moveToThread(coreThread);
QObject::connect(coreThread, SIGNAL(finished()), squawk, SLOT(deleteLater()));
QObject::connect(coreThread, SIGNAL(started()), squawk, SLOT(start())); QObject::connect(coreThread, SIGNAL(started()), squawk, SLOT(start()));
QObject::connect(&app, SIGNAL(aboutToQuit()), squawk, SLOT(stop()));
QObject::connect(squawk, SIGNAL(quit()), coreThread, SLOT(quit()));
QObject::connect(coreThread, SIGNAL(finished()), squawk, SLOT(deleteLater()));
QObject::connect(&w, SIGNAL(newAccountRequest(const QMap<QString, QVariant>&)), squawk, SLOT(newAccountRequest(const QMap<QString, QVariant>&))); QObject::connect(&w, SIGNAL(newAccountRequest(const QMap<QString, QVariant>&)), squawk, SLOT(newAccountRequest(const QMap<QString, QVariant>&)));
QObject::connect(&w, SIGNAL(connectAccount(const QString&)), squawk, SLOT(connectAccount(const QString&))); QObject::connect(&w, SIGNAL(connectAccount(const QString&)), squawk, SLOT(connectAccount(const QString&)));
@ -24,10 +35,11 @@ int main(int argc, char *argv[])
QObject::connect(squawk, SIGNAL(newAccount(const QMap<QString, QVariant>&)), &w, SLOT(newAccount(const QMap<QString, QVariant>&))); QObject::connect(squawk, SIGNAL(newAccount(const QMap<QString, QVariant>&)), &w, SLOT(newAccount(const QMap<QString, QVariant>&)));
QObject::connect(squawk, SIGNAL(accountConnectionStateChanged(const QString&, int)), &w, SLOT(accountConnectionStateChanged(const QString&, int))); QObject::connect(squawk, SIGNAL(accountConnectionStateChanged(const QString&, int)), &w, SLOT(accountConnectionStateChanged(const QString&, int)));
//QObject::connect(this, &Controller::operate, worker, &Worker::doWork);
//QObject::connect(worker, &Worker::resultReady, this, &Controller::handleResults);
coreThread->start(); coreThread->start();
return app.exec(); int result = app.exec();
coreThread->wait(500); //TODO hate doing that but settings for some reason don't get saved to the disk
return result;
} }

59
signalcatcher.cpp Normal file
View File

@ -0,0 +1,59 @@
#include "signalcatcher.h"
#include <signal.h>
#include <sys/socket.h>
#include <unistd.h>
int SignalCatcher::sigintFd[2] = {0,0};
SignalCatcher::SignalCatcher(QCoreApplication *p_app, QObject *parent):
QObject(parent),
app(p_app)
{
if (::socketpair(AF_UNIX, SOCK_STREAM, 0, sigintFd))
{
qFatal("Couldn't create INT socketpair");
}
if (setup_unix_signal_handlers() != 0)
{
qFatal("Couldn't install unix handlers");
}
snInt = new QSocketNotifier(sigintFd[1], QSocketNotifier::Read, this);
connect(snInt, SIGNAL(activated(int)), this, SLOT(handleSigInt()));
}
SignalCatcher::~SignalCatcher()
{}
void SignalCatcher::handleSigInt()
{
snInt->setEnabled(false);
char tmp;
::read(sigintFd[1], &tmp, sizeof(tmp));
app->quit();
snInt->setEnabled(true);
}
void SignalCatcher::intSignalHandler(int unused)
{
char a = 1;
::write(sigintFd[0], &a, sizeof(a));
}
int SignalCatcher::setup_unix_signal_handlers()
{
struct sigaction s_int;
s_int.sa_handler = SignalCatcher::intSignalHandler;
sigemptyset(&s_int.sa_mask);
s_int.sa_flags = 0;
s_int.sa_flags |= SA_RESTART;
if (sigaction(SIGINT, &s_int, 0) > 0)
return 1;
return 0;
}

30
signalcatcher.h Normal file
View File

@ -0,0 +1,30 @@
#ifndef SIGNALCATCHER_H
#define SIGNALCATCHER_H
#include <QtCore/QCoreApplication>
#include <QtCore/QObject>
#include <QtCore/QSocketNotifier>
class SignalCatcher: public QObject
{
Q_OBJECT
public:
SignalCatcher(QCoreApplication *p_app, QObject *parent = 0);
~SignalCatcher();
static void intSignalHandler(int unused);
public slots:
void handleSigInt();
private:
QCoreApplication *app;
static int sigintFd[2];
QSocketNotifier *snInt;
static int setup_unix_signal_handlers();
};
#endif // SIGNALCATCHER_H

View File

@ -1,4 +1,5 @@
#include "roster.h" #include "roster.h"
#include <QIcon>
using namespace Models; using namespace Models;
@ -16,7 +17,7 @@ Models::Roster::~Roster()
void Models::Roster::addAccount(const QMap<QString, QVariant>& data) void Models::Roster::addAccount(const QMap<QString, QVariant>& data)
{ {
Item* acc = new Item(Item::account, data, root); Account* acc = new Account(data, root);
beginInsertRows(QModelIndex(), root->childCount(), root->childCount()); beginInsertRows(QModelIndex(), root->childCount(), root->childCount());
root->appendChild(acc); root->appendChild(acc);
accounts.insert(std::make_pair(acc->name(), acc)); accounts.insert(std::make_pair(acc->name(), acc));
@ -31,13 +32,34 @@ QVariant Models::Roster::data (const QModelIndex& index, int role) const
QVariant result; QVariant result;
Item *item = static_cast<Item*>(index.internalPointer());
switch (role) { switch (role) {
case Qt::DisplayRole: case Qt::DisplayRole:
{ {
Item *item = static_cast<Item*>(index.internalPointer());
result = item->data(index.column()); result = item->data(index.column());
} }
break; break;
case Qt::DecorationRole:
switch (item->type) {
case Item::account:{
int state = item->data(1).toInt();
switch (state) {
case Shared::disconnected:
result = QIcon::fromTheme("im-user-offline");
break;
case Shared::connecting:
result = QIcon::fromTheme(Shared::ConnectionStateThemeIcons[state]);
break;
case Shared::connected:
result = QIcon::fromTheme("im-user-online");
break;
}
}
break;
default:
break;
}
break;
default: default:
break; break;
} }
@ -45,6 +67,11 @@ QVariant Models::Roster::data (const QModelIndex& index, int role) const
return result; return result;
} }
void Models::Roster::Item::setName(const QString& name)
{
itemData[0] = name;
}
int Models::Roster::columnCount (const QModelIndex& parent) const int Models::Roster::columnCount (const QModelIndex& parent) const
{ {
if (parent.isValid()) { if (parent.isValid()) {
@ -54,6 +81,26 @@ int Models::Roster::columnCount (const QModelIndex& parent) const
} }
} }
void Models::Roster::updateAccount(const QString& account, const QString& field, const QVariant& value)
{
std::map<QString, Account*>::iterator itr = accounts.find(account);
if (itr != accounts.end()) {
Account* acc = itr->second;
if (field == "name") {
acc->setName(value.toString());
accounts.erase(itr);
accounts.insert(std::make_pair(acc->name(), acc));
int row = acc->row();
emit dataChanged(createIndex(row, 0, acc), createIndex(row, 0, acc));
} else if (field == "state") {
acc->setState(value.toInt());
int row = acc->row();
emit dataChanged(createIndex(row, 0, acc), createIndex(row, 0, acc));
}
}
}
Qt::ItemFlags Models::Roster::flags(const QModelIndex& index) const Qt::ItemFlags Models::Roster::flags(const QModelIndex& index) const
{ {
if (!index.isValid()) { if (!index.isValid()) {
@ -92,6 +139,10 @@ QModelIndex Models::Roster::parent (const QModelIndex& child) const
} }
Item *childItem = static_cast<Item*>(child.internalPointer()); Item *childItem = static_cast<Item*>(child.internalPointer());
if (childItem == root) {
return QModelIndex();
}
Item *parentItem = childItem->parentItem(); Item *parentItem = childItem->parentItem();
if (parentItem == root) { if (parentItem == root) {
@ -210,3 +261,20 @@ bool Models::Roster::ElId::operator <(const Models::Roster::ElId& other) const
return account < other.account; return account < other.account;
} }
} }
Models::Roster::Account::Account(const QMap<QString, QVariant>& data, Models::Roster::Item* parentItem):
Item(account, data, parentItem)
{
itemData.push_back(data.value("state"));
}
Models::Roster::Account::~Account()
{
}
void Models::Roster::Account::setState(int state)
{
itemData[1] = state;
}

View File

@ -4,6 +4,7 @@
#include <qabstractitemmodel.h> #include <qabstractitemmodel.h>
#include <deque> #include <deque>
#include <map> #include <map>
#include "../../global.h"
namespace Models namespace Models
{ {
@ -12,24 +13,26 @@ class Roster : public QAbstractItemModel
{ {
class Item; class Item;
class ElId; class ElId;
class Account;
Q_OBJECT Q_OBJECT
public: public:
Roster(QObject* parent = 0); Roster(QObject* parent = 0);
~Roster(); ~Roster();
void addAccount(const QMap<QString, QVariant> &data); void addAccount(const QMap<QString, QVariant> &data);
void updateAccount(const QString& account, const QString& field, const QVariant& value);
QVariant data ( const QModelIndex& index, int role ) const; QVariant data ( const QModelIndex& index, int role ) const override;
Qt::ItemFlags flags(const QModelIndex &index) const override; Qt::ItemFlags flags(const QModelIndex &index) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
int columnCount ( const QModelIndex& parent ) const; int columnCount ( const QModelIndex& parent ) const override;
int rowCount ( const QModelIndex& parent ) const; int rowCount ( const QModelIndex& parent ) const override;
QModelIndex parent ( const QModelIndex& child ) const; QModelIndex parent ( const QModelIndex& child ) const override;
QModelIndex index ( int row, int column, const QModelIndex& parent ) const; QModelIndex index ( int row, int column, const QModelIndex& parent ) const override;
private: private:
Item* root; Item* root;
std::map<QString, Item*> accounts; std::map<QString, Account*> accounts;
std::map<ElId, Item*> elements; std::map<ElId, Item*> elements;
private: private:
@ -48,6 +51,7 @@ private:
void appendChild(Item *child); void appendChild(Item *child);
QString name() const; QString name() const;
void setName(const QString& name);
Item *child(int row); Item *child(int row);
int childCount() const; int childCount() const;
@ -58,12 +62,20 @@ private:
const Type type; const Type type;
private: protected:
std::deque<Item*> childItems; std::deque<Item*> childItems;
std::deque<QVariant> itemData; std::deque<QVariant> itemData;
Item* parent; Item* parent;
}; };
class Account : public Item {
public:
explicit Account(const QMap<QString, QVariant> &data, Item *parentItem = 0);
~Account();
void setState(int state);
};
class ElId { class ElId {
public: public:
ElId (const QString& p_account, const QString& p_name); ElId (const QString& p_account, const QString& p_name);

View File

@ -107,6 +107,7 @@ void Squawk::accountConnectionStateChanged(const QString& account, int state)
QMap<QString, QVariant>* acc = itr->second; QMap<QString, QVariant>* acc = itr->second;
acc->insert("state", state); acc->insert("state", state);
rosterModel.updateAccount(account, "state", state);
if (accounts != 0) { if (accounts != 0) {
accounts->updateAccount(account, "state", state); accounts->updateAccount(account, "state", state);
} }