diff --git a/src/ClientWorker.cpp b/src/ClientWorker.cpp
index fef666f..f9f1747 100644
--- a/src/ClientWorker.cpp
+++ b/src/ClientWorker.cpp
@@ -1,198 +1,200 @@
/*
* Kaidan - A user-friendly XMPP client for every device!
*
* Copyright (C) 2016-2019 Kaidan developers and contributors
* (see the LICENSE file for a full list of copyright authors)
*
* Kaidan is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition, as a special exception, the author of Kaidan gives
* permission to link the code of its release with the OpenSSL
* project's "OpenSSL" library (or with modified versions of it that
* use the same license as the "OpenSSL" library), and distribute the
* linked executables. You must obey the GNU General Public License in
* all respects for all of the code used other than "OpenSSL". If you
* modify this file, you may extend this exception to your version of
* the file, but you are not obligated to do so. If you do not wish to
* do so, delete this exception statement from your version.
*
* Kaidan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Kaidan. If not, see .
*/
#include "ClientWorker.h"
// Qt
#include
#include
#include
#include
// QXmpp
#include
#include
#include
#include
// Kaidan
#include "Kaidan.h"
#include "LogHandler.h"
#include "RegistrationManager.h"
#include "RosterManager.h"
#include "MessageHandler.h"
#include "DiscoveryManager.h"
#include "VCardManager.h"
#include "UploadManager.h"
#include "DownloadManager.h"
ClientWorker::ClientWorker(Caches *caches, Kaidan *kaidan, bool enableLogging, QGuiApplication *app,
QObject* parent)
: QObject(parent), caches(caches), kaidan(kaidan), enableLogging(enableLogging), app(app)
{
client = new QXmppClient(this);
logger = new LogHandler(client, this);
logger->enableLogging(enableLogging);
vCardManager = new VCardManager(client, caches->avatarStorage, this);
registrationManager = new RegistrationManager(kaidan, caches->settings);
rosterManager = new RosterManager(kaidan, client, caches->rosterModel,
caches->avatarStorage, vCardManager, this);
msgHandler = new MessageHandler(kaidan, client, caches->msgModel, this);
discoManager = new DiscoveryManager(client, this);
uploadManager = new UploadManager(kaidan, client, caches->msgModel, rosterManager,
caches->transferCache, this);
downloadManager = new DownloadManager(kaidan, caches->transferCache,
caches->msgModel, this);
client->addExtension(registrationManager);
connect(client, &QXmppClient::presenceReceived,
- caches->presCache, &PresenceCache::updatePresenceRequested);
+ caches->presCache, &PresenceCache::updatePresence);
+ connect(client, &QXmppClient::disconnected,
+ caches->presCache, &PresenceCache::clear);
connect(this, &ClientWorker::credentialsUpdated, this, &ClientWorker::setCredentials);
// publish kaidan version
client->versionManager().setClientName(APPLICATION_DISPLAY_NAME);
client->versionManager().setClientVersion(VERSION_STRING);
client->versionManager().setClientOs(QSysInfo::prettyProductName());
#if (QXMPP_VERSION) >= QT_VERSION_CHECK(1, 0, 0)
// Client State Indication
connect(app, &QGuiApplication::applicationStateChanged, this, &ClientWorker::setCsiState);
#endif
}
void ClientWorker::main()
{
// initialize random generator
qsrand(time(nullptr));
connect(client, &QXmppClient::stateChanged, kaidan, &Kaidan::setConnectionState);
connect(client, &QXmppClient::connected, this, &ClientWorker::onConnect);
connect(client, &QXmppClient::error, this, &ClientWorker::onConnectionError);
connect(this, &ClientWorker::connectRequested, this, &ClientWorker::xmppConnect);
connect(this, &ClientWorker::disconnectRequested, client, &QXmppClient::disconnectFromServer);
}
void ClientWorker::xmppConnect()
{
QXmppConfiguration config;
config.setJid(creds.jid);
config.setResource(creds.jidResource.append(".").append(generateRandomString()));
config.setPassword(creds.password);
config.setAutoAcceptSubscriptions(false);
config.setStreamSecurityMode(QXmppConfiguration::TLSRequired);
config.setAutoReconnectionEnabled(true); // will automatically reconnect
// on first try we must be sure that we connect successfully
// otherwise this could end in a reconnection loop
if (creds.isFirstTry)
config.setAutoReconnectionEnabled(false);
client->connectToServer(config, QXmppPresence(QXmppPresence::Available));
}
void ClientWorker::onConnect()
{
// no mutex needed, because this is called from updateClient()
qDebug() << "[client] Connected successfully to server";
// Emit signal, that logging in with these credentials has worked for the first time
if (creds.isFirstTry)
emit kaidan->logInWorked();
// accept credentials and save them
creds.isFirstTry = false;
caches->settings->setValue(KAIDAN_SETTINGS_AUTH_JID, creds.jid);
caches->settings->setValue(KAIDAN_SETTINGS_AUTH_PASSWD,
QString::fromUtf8(creds.password.toUtf8().toBase64()));
// after first log in we always want to automatically reconnect
client->configuration().setAutoReconnectionEnabled(true);
}
void ClientWorker::onConnectionError(QXmppClient::Error error)
{
// no mutex needed, because this is called from updateClient()
qDebug() << "[client] Disconnected:" << error;
// Check if first time connecting with these credentials
if (creds.isFirstTry || error == QXmppClient::XmppStreamError) {
// always request new credentials, when failed to connect on first time
emit kaidan->newCredentialsNeeded();
}
if (error == QXmppClient::NoError) {
emit disconnReasonChanged(DisconnReason::ConnUserDisconnected);
} else if (error == QXmppClient::KeepAliveError) {
emit disconnReasonChanged(DisconnReason::ConnKeepAliveError);
} else if (error == QXmppClient::XmppStreamError) {
QXmppStanza::Error::Condition xError = client->xmppStreamError();
qDebug() << xError;
if (xError == QXmppStanza::Error::NotAuthorized) {
emit disconnReasonChanged(DisconnReason::ConnAuthenticationFailed);
} else {
emit disconnReasonChanged(DisconnReason::ConnNotConnected);
}
} else if (error == QXmppClient::SocketError) {
QAbstractSocket::SocketError sError = client->socketError();
if (sError == QAbstractSocket::ConnectionRefusedError ||
sError == QAbstractSocket::RemoteHostClosedError) {
emit disconnReasonChanged(DisconnReason::ConnConnectionRefused);
} else if (sError == QAbstractSocket::HostNotFoundError) {
emit disconnReasonChanged(DisconnReason::ConnDnsError);
} else if (sError == QAbstractSocket::SocketAccessError) {
emit disconnReasonChanged(DisconnReason::ConnNoNetworkPermission);
} else if (sError == QAbstractSocket::SocketTimeoutError) {
emit disconnReasonChanged(DisconnReason::ConnKeepAliveError);
} else if (sError == QAbstractSocket::SslHandshakeFailedError ||
sError == QAbstractSocket::SslInternalError) {
emit disconnReasonChanged(DisconnReason::ConnTlsFailed);
} else {
emit disconnReasonChanged(DisconnReason::ConnNotConnected);
}
}
}
QString ClientWorker::generateRandomString(unsigned int length) const
{
QString randomString;
for (unsigned int i = 0; i < length; ++i)
randomString.append(KAIDAN_RESOURCE_RANDOM_CHARS.at(
qrand() % KAIDAN_RESOURCE_RANDOM_CHARS.length()));
return randomString;
}
#if (QXMPP_VERSION) >= QT_VERSION_CHECK(1, 0, 0)
void ClientWorker::setCsiState(Qt::ApplicationState state)
{
if (state == Qt::ApplicationActive)
client->setActive(true);
else
client->setActive(false);
}
#endif
diff --git a/src/PresenceCache.cpp b/src/PresenceCache.cpp
index 1ae5c97..3233b3b 100644
--- a/src/PresenceCache.cpp
+++ b/src/PresenceCache.cpp
@@ -1,98 +1,98 @@
/*
* Kaidan - A user-friendly XMPP client for every device!
*
* Copyright (C) 2016-2019 Kaidan developers and contributors
* (see the LICENSE file for a full list of copyright authors)
*
* Kaidan is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition, as a special exception, the author of Kaidan gives
* permission to link the code of its release with the OpenSSL
* project's "OpenSSL" library (or with modified versions of it that
* use the same license as the "OpenSSL" library), and distribute the
* linked executables. You must obey the GNU General Public License in
* all respects for all of the code used other than "OpenSSL". If you
* modify this file, you may extend this exception to your version of
* the file, but you are not obligated to do so. If you do not wish to
* do so, delete this exception statement from your version.
*
* Kaidan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Kaidan. If not, see .
*/
#include "PresenceCache.h"
#include "Enums.h"
#include
using namespace Enums;
-PresenceCache::PresenceCache(QObject *parent) : QObject(parent)
+PresenceCache::PresenceCache(QObject *parent)
+ : QObject(parent)
{
- connect(this, &PresenceCache::updatePresenceRequested, this, &PresenceCache::updatePresence);
}
void PresenceCache::updatePresence(QXmppPresence presence)
{
- if (presence.type() != QXmppPresence::Available &&
- presence.type() != QXmppPresence::Unavailable/* &&
- presence.type() != QXmppPresence::Error*/)
- return;
-
QString jid = QXmppUtils::jidToBareJid(presence.from());
QString resource = QXmppUtils::jidToResource(presence.from());
if (!presences.contains(jid))
presences[jid] = QMap();
presences[jid][resource] = presence;
emit presenceChanged(jid);
}
+void PresenceCache::clear()
+{
+ presences.clear();
+}
+
quint8 PresenceCache::getPresenceType(QString bareJid)
{
if (!presences.contains(bareJid))
return (quint8) AvailabilityTypes::PresUnavailable;
QXmppPresence pres = presences[bareJid].last();
if (pres.type() == QXmppPresence::Unavailable) {
return (quint8) AvailabilityTypes::PresUnavailable;
} else if (pres.type() == QXmppPresence::Available) {
switch (pres.availableStatusType()) {
case QXmppPresence::Online:
return (quint8) AvailabilityTypes::PresOnline;
case QXmppPresence::Away:
return (quint8) AvailabilityTypes::PresAway;
case QXmppPresence::XA:
return (quint8) AvailabilityTypes::PresXA;
case QXmppPresence::DND:
return (quint8) AvailabilityTypes::PresDND;
case QXmppPresence::Chat:
return (quint8) AvailabilityTypes::PresChat;
case QXmppPresence::Invisible:
return (quint8) AvailabilityTypes::PresInvisible;
default:
return (quint8) AvailabilityTypes::PresUnavailable;
}
} else if (pres.type() == QXmppPresence::Error) {
return (quint8) AvailabilityTypes::PresError;
}
return (quint8) AvailabilityTypes::PresUnavailable;
}
QString PresenceCache::getStatusText(QString bareJid)
{
if (!presences.contains(bareJid))
return "";
return presences[bareJid].last().statusText();
}
diff --git a/src/PresenceCache.h b/src/PresenceCache.h
index 850c5e9..160273c 100644
--- a/src/PresenceCache.h
+++ b/src/PresenceCache.h
@@ -1,80 +1,80 @@
/*
* Kaidan - A user-friendly XMPP client for every device!
*
* Copyright (C) 2016-2019 Kaidan developers and contributors
* (see the LICENSE file for a full list of copyright authors)
*
* Kaidan is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition, as a special exception, the author of Kaidan gives
* permission to link the code of its release with the OpenSSL
* project's "OpenSSL" library (or with modified versions of it that
* use the same license as the "OpenSSL" library), and distribute the
* linked executables. You must obey the GNU General Public License in
* all respects for all of the code used other than "OpenSSL". If you
* modify this file, you may extend this exception to your version of
* the file, but you are not obligated to do so. If you do not wish to
* do so, delete this exception statement from your version.
*
* Kaidan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Kaidan. If not, see .
*/
#ifndef PRESENCECACHE_H
#define PRESENCECACHE_H
#include
#include
#include
#include
/**
* @class PresenceCache A cache for presence holders for certain JIDs
*/
class PresenceCache : public QObject
{
Q_OBJECT
public:
PresenceCache(QObject *parent = nullptr);
/**
* Return one of the status texts from all resources
*/
Q_INVOKABLE QString getStatusText(QString bareJid);
/**
* Returns one of the presence types from all resources
*/
Q_INVOKABLE quint8 getPresenceType(QString bareJid);
-signals:
+public slots:
/**
- * Notifies about changed presences
+ * Updates the presence cache, it will ignore subscribe presences
*/
- void presenceChanged(QString jid);
+ void updatePresence(QXmppPresence presence);
/**
- * Is connected to updatePresence
+ * Clears all cached presences.
*/
- void updatePresenceRequested(QXmppPresence presence);
+ void clear();
-private slots:
+signals:
/**
- * @brief Updates the presence cache, it will ignore subscribe presences
+ * Notifies about changed presences
*/
- void updatePresence(QXmppPresence presence);
+ void presenceChanged(QString jid);
private:
QMap> presences;
};
#endif // PRESENCECACHE_H