Commit 855bc84f authored by LNJ's avatar LNJ Committed by Jonah Brüchert

Add XEP-0352: Client State Indication

This will give the XMPP server the information if the user is currently actively
using Kaidan or it's runnning in the background. If it's running in the
background, the server will only send some important messages, not everything.
parent 4407f835
......@@ -57,7 +57,7 @@ static const unsigned int KAIDAN_CLIENT_LOOP_INTERVAL = 30;
ClientThread::ClientThread(RosterModel *rosterModel, MessageModel *messageModel,
AvatarFileStorage *avatarStorage, Credentials creds,
QSettings *settings, QObject *parent):
QSettings *settings, QGuiApplication *app, QObject *parent):
QThread(parent), rosterModel(rosterModel),
messageModel(messageModel), avatarStorage(avatarStorage),
creds(creds), settings(settings),
......@@ -71,7 +71,7 @@ ClientThread::ClientThread(RosterModel *rosterModel, MessageModel *messageModel,
client->bindResource(creds.jidResource.toStdString()); // set resource / device name
client->setTls(gloox::TLSRequired); // require encryption
worker = new ClientWorker(client, this);
worker = new ClientWorker(client, this, app);
connect(this, &ClientThread::connectRequested, worker, &ClientWorker::xmppConnect);
connect(this, &ClientThread::disconnectRequested, worker, &ClientWorker::xmppDisconnect);
connect(this, &ClientThread::stopWorkTimerRequested, worker, &ClientWorker::stopWorkTimer);
......
......@@ -54,6 +54,7 @@ class ServiceDiscoveryManager;
class VCardManager;
class XmlLogHandler;
class QSettings;
class QGuiApplication;
using namespace Enums;
......@@ -105,7 +106,8 @@ public:
*/
ClientThread(RosterModel *rosterModel, MessageModel *messageModel,
AvatarFileStorage *avatarStorage, Credentials creds,
QSettings *settings, QObject *parent = nullptr);
QSettings *settings, QGuiApplication *app,
QObject *parent = nullptr);
/*
* Will exit the event loop and waits until thread finishes and then
......
......@@ -34,6 +34,7 @@
// Qt
#include <QDebug>
#include <QSettings>
#include <QGuiApplication>
// Kaidan
#include "ClientThread.h"
......@@ -41,7 +42,8 @@
static const unsigned int RECONNECT_INTERVAL = 5000;
ClientWorker::ClientWorker(GlooxClient* client, ClientThread *controller,
QObject* parent) : QObject(parent), client(client), controller(controller)
QGuiApplication *app, QObject* parent)
: QObject(parent), client(client), controller(controller)
{
client->registerConnectionListener(this);
......@@ -52,6 +54,8 @@ ClientWorker::ClientWorker(GlooxClient* client, ClientThread *controller,
connect(&reconnectTimer, &QTimer::timeout, this, &ClientWorker::xmppConnect);
connect(this, &ClientWorker::stopReconnectTimerRequested,
&reconnectTimer, &QTimer::stop);
connect(app, &QGuiApplication::applicationStateChanged,
this, &ClientWorker::setApplicationState);
}
ClientWorker::~ClientWorker()
......@@ -146,3 +150,11 @@ void ClientWorker::reconnect()
qDebug().noquote() << QString("[client] Will do reconnect in %1 ms").arg(RECONNECT_INTERVAL);
reconnectTimer.start();
}
void ClientWorker::setApplicationState(Qt::ApplicationState state)
{
if (state == Qt::ApplicationActive)
controller->client->setActive();
else
controller->client->setInactive();
}
......@@ -39,6 +39,7 @@
class ClientThread;
class GlooxClient;
class QGuiApplication;
/**
* The ClientWorker is used as a QObject-based worker on the ClientThread.
......@@ -56,7 +57,7 @@ public:
* @param parent Optional QObject-based parent.
*/
ClientWorker(GlooxClient *client, ClientThread *contoller,
QObject *parent = nullptr);
QGuiApplication *app, QObject *parent = nullptr);
~ClientWorker();
......@@ -88,6 +89,11 @@ public slots:
*/
void stopWorkTimer();
/**
* Sets the application state used for XEP-0352: Client State Indication
*/
void setApplicationState(Qt::ApplicationState state);
private:
/**
* Notifys via signal that the client has connected.
......
......@@ -43,7 +43,7 @@
#include "MessageModel.h"
#include "Database.h"
Kaidan::Kaidan(QObject *parent) : QObject(parent)
Kaidan::Kaidan(QGuiApplication *app, QObject *parent) : QObject(parent)
{
//
// Database and components
......@@ -81,7 +81,8 @@ Kaidan::Kaidan(QObject *parent) : QObject(parent)
creds.isFirstTry = false;
// create new client and start thread's main loop (won't connect until requested)
client = new ClientThread(rosterModel, messageModel, avatarStorage, creds, settings);
client = new ClientThread(rosterModel, messageModel, avatarStorage, creds,
settings, app);
client->start();
connect(client, &ClientThread::connectionStateChanged, [=](ConnectionState state) {
......
......@@ -46,6 +46,7 @@ class RosterManager;
class RosterModel;
class MessageModel;
class QSettings;
class QGuiApplication;
class Database;
using namespace Enums;
......@@ -77,7 +78,7 @@ public:
/**
* Constructor
*/
Kaidan(QObject *parent = 0);
Kaidan(QGuiApplication *app, QObject *parent = 0);
/**
* Destructor
......
......@@ -107,6 +107,7 @@ int main(int argc, char *argv[])
qRegisterMetaType<MessageModel*>("MessageModel*");
qRegisterMetaType<AvatarFileStorage*>("AvatarFileStorage*");
qRegisterMetaType<ContactMap>("ContactMap");
qRegisterMetaType<Qt::ApplicationState>("Qt::ApplicationState");
qmlRegisterUncreatableMetaObject(Enums::staticMetaObject, APPLICATION_ID,
1, 0, "Kaidan", "Access to enums & flags only");
......@@ -127,7 +128,7 @@ int main(int argc, char *argv[])
// Kaidan back-end
//
Kaidan kaidan;
Kaidan kaidan(&app);
//
// Command line arguments
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment