diff --git a/src/ClientWorker.cpp b/src/ClientWorker.cpp index 48adcaf..711d230 100644 --- a/src/ClientWorker.cpp +++ b/src/ClientWorker.cpp @@ -1,216 +1,215 @@ /* * Kaidan - A user-friendly XMPP client for every device! * * Copyright (C) 2016-2020 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 "DiscoveryManager.h" #include "DownloadManager.h" #include "Kaidan.h" #include "LogHandler.h" #include "MessageHandler.h" #include "RegistrationManager.h" #include "RosterManager.h" #include "UploadManager.h" #include "VCardManager.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::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()); // Client State Indication connect(app, &QGuiApplication::applicationStateChanged, this, &ClientWorker::setCsiState); } 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->loggedInWithNewCredentials(); // 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(); } QXmppStanza::Error::Condition xmppStreamError; QAbstractSocket::SocketError socketError; switch (error) { case QXmppClient::NoError: - emit connectionErrorChanged(ClientWorker::UserDisconnected); + emit connectionErrorChanged(ClientWorker::NoError); break; case QXmppClient::KeepAliveError: emit connectionErrorChanged(ClientWorker::KeepAliveError); break; case QXmppClient::XmppStreamError: xmppStreamError = client->xmppStreamError(); - qDebug() << xmppStreamError; if (xmppStreamError == QXmppStanza::Error::NotAuthorized) { emit connectionErrorChanged(ClientWorker::AuthenticationFailed); } else { emit connectionErrorChanged(ClientWorker::NotConnected); } break; case QXmppClient::SocketError: socketError = client->socketError(); switch (socketError) { case QAbstractSocket::ConnectionRefusedError: case QAbstractSocket::RemoteHostClosedError: emit connectionErrorChanged(ClientWorker::ConnectionRefused); break; case QAbstractSocket::HostNotFoundError: emit connectionErrorChanged(ClientWorker::DnsError); break; case QAbstractSocket::SocketAccessError: emit connectionErrorChanged(ClientWorker::NoNetworkPermission); break; case QAbstractSocket::SocketTimeoutError: emit connectionErrorChanged(ClientWorker::KeepAliveError); break; case QAbstractSocket::SslHandshakeFailedError: case QAbstractSocket::SslInternalError: emit connectionErrorChanged(ClientWorker::TlsFailed); break; default: emit connectionErrorChanged(ClientWorker::NotConnected); } break; } } QString ClientWorker::generateRandomString(unsigned int length) const { const QString resourceChars(KAIDAN_RESOURCE_RANDOM_CHARS); QString randomString; for (unsigned int i = 0; i < length; ++i) randomString.append(resourceChars.at(qrand() % resourceChars.length())); return randomString; } VCardManager *ClientWorker::getVCardManager() const { return vCardManager; } void ClientWorker::setCsiState(Qt::ApplicationState state) { if (state == Qt::ApplicationActive) client->setActive(true); else client->setActive(false); } diff --git a/src/ClientWorker.h b/src/ClientWorker.h index cd56fab..e2238f2 100644 --- a/src/ClientWorker.h +++ b/src/ClientWorker.h @@ -1,214 +1,213 @@ /* * Kaidan - A user-friendly XMPP client for every device! * * Copyright (C) 2016-2020 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 CLIENTWORKER_H #define CLIENTWORKER_H // Qt #include #include #include class QGuiApplication; // QXmpp #include // Kaidan #include "AvatarFileStorage.h" #include "Database.h" #include "Enums.h" #include "Globals.h" #include "MessageModel.h" #include "PresenceCache.h" #include "RosterModel.h" #include "TransferCache.h" class LogHandler; class Kaidan; class ClientWorker; class RegistrationManager; class RosterManager; class MessageHandler; class DiscoveryManager; class VCardManager; class UploadManager; class DownloadManager; /** * The ClientWorker is used as a QObject-based worker on the ClientThread. */ class ClientWorker : public QObject { Q_OBJECT public: /** * enumeration of possible connection errors */ enum ConnectionError { NoError, - UserDisconnected, AuthenticationFailed, NotConnected, TlsFailed, TlsNotAvailable, DnsError, ConnectionRefused, NoSupportedAuth, KeepAliveError, NoNetworkPermission, RegistrationUnsupported }; Q_ENUM(ConnectionError) struct Caches { Caches(Kaidan *kaidan, RosterDb *rosterDb, MessageDb *msgDb, QObject *parent = nullptr) : msgModel(new MessageModel(kaidan, msgDb, parent)), rosterModel(new RosterModel(rosterDb, parent)), avatarStorage(new AvatarFileStorage(parent)), presCache(new PresenceCache(parent)), transferCache(new TransferCache(parent)), settings(new QSettings(APPLICATION_NAME, APPLICATION_NAME)) { rosterModel->setMessageModel(msgModel); } ~Caches() { delete msgModel; delete rosterModel; delete avatarStorage; delete presCache; delete transferCache; delete settings; } MessageModel *msgModel; RosterModel *rosterModel; AvatarFileStorage *avatarStorage; PresenceCache *presCache; TransferCache* transferCache; QSettings *settings; }; struct Credentials { QString jid; QString jidResource; QString password; // if never connected successfully before with these credentials bool isFirstTry; }; /** * @param caches All caches running in the main thread for communication with the UI. * @param kaidan Main back-end class, running in the main thread. * @param enableLogging If logging of the XMPP stream should be done. * @param app The QGuiApplication to determine if the window is active. * @param parent Optional QObject-based parent. */ ClientWorker(Caches *caches, Kaidan *kaidan, bool enableLogging, QGuiApplication *app, QObject *parent = nullptr); VCardManager *getVCardManager() const; public slots: /** * Main function of the client thread */ void main(); /** * Sets the new credentials for next connect. * * @param creds The new credentials for the next connect */ void setCredentials(ClientWorker::Credentials creds) { this->creds = creds; } /** * Connects the client with the server. */ void xmppConnect(); signals: // emitted by 'Kaidan' to us: void connectRequested(); void disconnectRequested(); void credentialsUpdated(ClientWorker::Credentials creds); /** * Emitted when the client failed to connect to the server. * * @param error new connection error */ void connectionErrorChanged(ClientWorker::ConnectionError error); private slots: /** * Notifys via signal that the client has connected. */ void onConnect(); /** * Sets a new connection error. */ void onConnectionError(QXmppClient::Error error); /** * Uses the QGuiApplication state to reduce network traffic when window is minimized */ void setCsiState(Qt::ApplicationState state); private: /** * Generates a random alphanumeric string * * @param length The length of the generated string */ QString generateRandomString(unsigned int length = 4) const; Caches *caches; Kaidan *kaidan; QXmppClient *client; LogHandler *logger; Credentials creds; bool enableLogging; QGuiApplication *app; RegistrationManager *registrationManager; RosterManager *rosterManager; MessageHandler *msgHandler; DiscoveryManager *discoManager; VCardManager *vCardManager; UploadManager *uploadManager; DownloadManager *downloadManager; }; #endif // CLIENTWORKER_H