Kaidan.h 9.88 KB
Newer Older
1
/*
2
 *  Kaidan - A user-friendly XMPP client for every device!
3
 *
Linus Jahn's avatar
Linus Jahn committed
4
 *  Copyright (C) 2016-2019 Kaidan developers and contributors
LNJ's avatar
LNJ committed
5
 *  (see the LICENSE file for a full list of copyright authors)
6 7 8 9 10 11
 *
 *  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.
 *
LNJ's avatar
LNJ committed
12 13 14 15 16 17 18 19 20 21
 *  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.
 *
22 23 24 25 26 27
 *  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
LNJ's avatar
LNJ committed
28
 *  along with Kaidan.  If not, see <http://www.gnu.org/licenses/>.
29 30
 */

31 32
#ifndef KAIDAN_H
#define KAIDAN_H
Georg's avatar
Georg committed
33

LNJ's avatar
LNJ committed
34
// Qt
Linus Jahn's avatar
Linus Jahn committed
35
#include <QColor>
Georg's avatar
Georg committed
36
#include <QObject>
LNJ's avatar
LNJ committed
37 38
#include <QString>
// Kaidan
39
#include "ClientWorker.h"
40
#include "Enums.h"
Linus Jahn's avatar
Linus Jahn committed
41
#include "Globals.h"
42

LNJ's avatar
LNJ committed
43
class QGuiApplication;
44
class Database;
45
class QXmppClient;
Linus Jahn's avatar
Linus Jahn committed
46
class QmlUtils;
47 48 49 50 51

using namespace Enums;

/**
 * @class Kaidan Kaidan's Back-End Class
52
 *
53 54
 * @brief This class will initiate the complete back-end, including the @see Database
 * connection, viewing models (@see MessageModel, @see RosterModel), etc.
55
 *
56 57
 * This class will run in the main thread, the XMPP connection and the database managers
 * run in other threads.
58 59
 */
class Kaidan : public QObject
Georg's avatar
Georg committed
60
{
61
	Q_OBJECT
62

Linus Jahn's avatar
Linus Jahn committed
63
	Q_PROPERTY(QmlUtils* utils READ getUtils CONSTANT)
64 65
	Q_PROPERTY(RosterModel* rosterModel READ getRosterModel CONSTANT)
	Q_PROPERTY(MessageModel* messageModel READ getMessageModel CONSTANT)
66
	Q_PROPERTY(AvatarFileStorage* avatarStorage READ getAvatarStorage NOTIFY avatarStorageChanged)
67 68
	Q_PROPERTY(PresenceCache* presenceCache READ getPresenceCache CONSTANT)
	Q_PROPERTY(TransferCache* transferCache READ getTransferCache CONSTANT)
69
	Q_PROPERTY(QSettings* settings READ getSettings CONSTANT)
70 71
	Q_PROPERTY(quint8 connectionState READ getConnectionState NOTIFY connectionStateChanged)
	Q_PROPERTY(quint8 disconnReason READ getDisconnReason NOTIFY disconnReasonChanged)
72
	Q_PROPERTY(QString jid READ getJid WRITE setJid NOTIFY jidChanged)
73
	Q_PROPERTY(QString jidResource READ getJidResource WRITE setJidResource NOTIFY jidResourceChanged)
74
	Q_PROPERTY(QString password READ getPassword WRITE setPassword NOTIFY passwordChanged)
Linus Jahn's avatar
Linus Jahn committed
75
	Q_PROPERTY(bool uploadServiceFound READ getUploadServiceFound NOTIFY uploadServiceFoundChanged)
Georg's avatar
Georg committed
76

Georg's avatar
Georg committed
77
public:
78
	Kaidan(QGuiApplication *app, bool enableLogging = true, QObject *parent = nullptr);
79
	~Kaidan();
LNJ's avatar
LNJ committed
80

81 82 83 84 85 86 87 88 89 90 91
	/**
	 * Start connection (called from QML when ready)
	 */
	Q_INVOKABLE void start();

	/**
	 * Connect to the XMPP server
	 *
	 * If you haven't set a username and password, they are used from the
	 * last successful login (the settings file).
	 */
92
	Q_INVOKABLE void mainConnect();
93 94 95 96 97 98

	/**
	 * Disconnect from XMPP server
	 *
	 * This will disconnect the client from the server. When disconnected,
	 * the connectionStateChanged signal will be emitted.
99
	 *
100 101 102 103 104 105 106 107
	 * @param openLogInPage If true, the newCredentialsNeeded signal will be
	 * emitted.
	 */
	Q_INVOKABLE void mainDisconnect(bool openLogInPage = false);

	/**
	 * Returns the current ConnectionState
	 */
108 109 110 111
	Q_INVOKABLE quint8 getConnectionState() const
	{
		return (quint8) connectionState;
	}
112 113 114 115 116 117 118 119 120 121 122 123

	/**
	 * Returns the last disconnection reason
	 */
	Q_INVOKABLE quint8 getDisconnReason() const;

	/**
	 * Set own JID used for connection
	 *
	 * To really change the JID of the current connection, you'll need to
	 * reconnect.
	 */
124
	void setJid(const QString &jid);
125 126 127 128 129 130 131 132 133 134 135 136

	/**
	 * Get the current JID
	 */
	QString getJid() const
	{
		return creds.jid;
	}

	/**
	 * Set a optional custom JID resource (device name)
	 */
137
	void setJidResource(const QString &jidResource);
138 139 140 141 142 143 144 145 146 147 148 149

	/**
	 * Get the JID resoruce
	 */
	QString getJidResource() const
	{
		return creds.jidResource;
	}

	/**
	 * Set the password for next connection
	 */
150
	void setPassword(const QString &password);
151 152 153 154 155 156 157 158 159 160 161

	/**
	 * Get the currently used password
	 */
	QString getPassword() const
	{
		return creds.password;
	}

	RosterModel* getRosterModel() const
	{
162
		return m_caches->rosterModel;
163 164 165 166
	}

	MessageModel* getMessageModel() const
	{
167
		return m_caches->msgModel;
168 169 170 171
	}

	AvatarFileStorage* getAvatarStorage() const
	{
172
		return m_caches->avatarStorage;
173
	}
174

175 176
	PresenceCache* getPresenceCache() const
	{
177
		return m_caches->presCache;
178 179
	}

180 181
	TransferCache* getTransferCache() const
	{
182
		return m_caches->transferCache;
183 184
	}

185 186
	QSettings* getSettings() const
	{
187
		return m_caches->settings;
188 189
	}

Linus Jahn's avatar
Linus Jahn committed
190
	QmlUtils* getUtils() const
191
	{
Linus Jahn's avatar
Linus Jahn committed
192
		return m_utils;
193
	}
194

195
	/**
196
	 * Adds XMPP URI to open as soon as possible
197
	 */
198
	void addOpenUri(const QByteArray &uri);
199

200
	/**
Linus Jahn's avatar
Linus Jahn committed
201
	 * Returns whether an HTTP File Upload service has been found
202
	 */
Linus Jahn's avatar
Linus Jahn committed
203
	bool getUploadServiceFound() const
204
	{
Linus Jahn's avatar
Linus Jahn committed
205
		return uploadServiceFound;
206 207
	}

208 209
	static Kaidan *instance();

210
signals:
211
	void avatarStorageChanged();
212 213 214 215 216

	/**
	 * Emitted, when the client's connection state has changed (e.g. when
	 * successfully connected or when disconnected)
	 */
217
	void connectionStateChanged();
218 219 220 221 222

	/**
	 * Emitted, when the client failed to connect and gives the reason in
	 * a DisconnectionReason enumatrion.
	 */
223
	void disconnReasonChanged();
224 225 226 227

	/**
	 * Emitted when the JID was changed
	 */
228
	void jidChanged();
229 230 231 232

	/**
	 * Emitted when the JID resouce (device name) has changed
	 */
233
	void jidResourceChanged();
234 235 236 237

	/**
	 * Emitted when the used password for logging in has changed
	 */
238
	void passwordChanged();
239 240 241 242 243 244 245 246 247 248 249 250 251 252

	/**
	 * Emitted when there are no (correct) credentials and new are needed
	 *
	 * The client will be in disconnected state, when this is emitted.
	 */
	void newCredentialsNeeded();

	/**
	 * Emitted when log in worked with new credentials
	 *
	 * The client will be in connected state, when this is emitted.
	 */
	void logInWorked();
253

254 255 256 257 258
	/**
	 * Show passive notification
	 */
	void passiveNotificationRequested(QString text);

259 260 261 262 263 264 265 266 267 268
	/**
	 * Emitted, whan a subscription request was received
	 */
	void subscriptionRequestReceived(QString from, QString msg);

	/**
	 * Incoming subscription request was accepted or declined by the user
	 */
	void subscriptionRequestAnswered(QString jid, bool accepted);

269
	/**
LNJ's avatar
LNJ committed
270
	 * Request vCard of any JID
271 272 273 274 275 276
	 *
	 * Is required when the avatar (or other information) of a JID are
	 * requested and the JID is not in the roster.
	 */
	void vCardRequested(QString jid);

277 278 279 280 281 282 283
	/**
	 * XMPP URI received
	 *
	 * Is called when Kaidan was used to open an XMPP URI (i.e. 'xmpp:kaidan@muc.kaidan.im?join')
	 */
	void xmppUriReceived(QString uri);

LNJ's avatar
LNJ committed
284 285 286
	/**
	 * The upload progress of a file upload has changed
	 */
Linus Jahn's avatar
Linus Jahn committed
287
	void uploadProgressMade(QString msgId, quint64 sent, quint64 total);
LNJ's avatar
LNJ committed
288

289
	/**
Linus Jahn's avatar
Linus Jahn committed
290
	 * An HTTP File Upload service was discovered
291
	 */
Linus Jahn's avatar
Linus Jahn committed
292
	void uploadServiceFoundChanged();
293

Linus Jahn's avatar
Linus Jahn committed
294 295 296 297 298 299 300 301
	/**
	 * Send a text message to any JID
	 *
	 * Currently only contacts are displayed on the RosterPage (there is no
	 * way to view a list of all chats -> for contacts and non-contacts), so
	 * you should only send messages to JIDs from your roster, otherwise you
	 * won't be able to see the message history.
	 */
302
	void sendMessage(QString jid, QString message, bool isSpoiler, QString spoilerHint);
Linus Jahn's avatar
Linus Jahn committed
303

304 305 306 307 308 309 310
	/**
	 * Correct the last message
	 *
	 * To get/check the last message id, use `kaidan.messageModel.lastMessageId(jid)`
	 */
	void correctMessage(QString toJid, QString msgId, QString message);

Linus Jahn's avatar
Linus Jahn committed
311 312 313 314 315
	/**
	 * Upload and send file
	 */
	void sendFile(QString jid, QString filePath, QString message);

316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
	/**
	 * Add a contact to your roster
	 *
	 * @param nick A simple nick name for the new contact, which should be
	 *             used to display in the roster.
	 */
	void addContact(QString jid, QString nick, QString msg);

	/**
	 * Remove a contact from your roster
	 *
	 * Only the JID is needed.
	 */
	void removeContact(QString jid);

331
	/**
332
	 * Downloads an attached media file of a message
333 334 335 336 337 338
	 *
	 * @param msgId The message
	 * @param url the media url from the message
	 */
	void downloadMedia(QString msgId, QString url);

339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
	/**
	 * Changes the user's password on the server
	 *
	 * @param newPassword The new password
	 */
	void changePassword(const QString &newPassword);

	/**
	 * Emitted, when changing the password has succeeded.
	 */
	void passwordChangeSucceeded();

	/**
	 * Emitted, when changing the password has failed.
	 */
	void passwordChangeFailed();

356
public slots:
357 358 359 360 361 362 363 364
	/**
	 * Set current connection state
	 */
	void setConnectionState(QXmppClient::State state);

	/**
	 * Sets the disconnection error/reason
	 */
Linus Jahn's avatar
Linus Jahn committed
365
	void setDisconnReason(Enums::DisconnectionReason reason);
366

367 368 369
	/**
	 * Receives messages from another instance of the application
	 */
Linus Jahn's avatar
Linus Jahn committed
370
	void receiveMessage(quint32, const QByteArray &msg)
371 372 373 374 375
	{
		// currently we only send XMPP URIs
		addOpenUri(msg);
	}

376
	/**
Linus Jahn's avatar
Linus Jahn committed
377
	 * Enables HTTP File Upload to be used (will be called from UploadManager)
378
	 */
Linus Jahn's avatar
Linus Jahn committed
379
	void setUploadServiceFound(bool enabled)
380
	{
Linus Jahn's avatar
Linus Jahn committed
381 382
		uploadServiceFound = enabled;
		emit uploadServiceFoundChanged();
383 384
	}

LNJ's avatar
LNJ committed
385
private:
386
	void connectDatabases();
Georg's avatar
Georg committed
387

Linus Jahn's avatar
Linus Jahn committed
388
	QmlUtils *m_utils;
389 390 391 392 393 394 395
	Database *m_database;
	QThread *m_dbThrd;
	MessageDb *m_msgDb;
	RosterDb *m_rosterDb;
	QThread *m_cltThrd;
	ClientWorker::Caches *m_caches;
	ClientWorker *m_client;
396

397
	ClientWorker::Credentials creds;
398
	QString openUriCache;
Linus Jahn's avatar
Linus Jahn committed
399
	bool uploadServiceFound = false;
400 401
	ConnectionState connectionState = ConnectionState::StateDisconnected;
	DisconnReason disconnReason = DisconnReason::ConnNoError;
402 403

	static Kaidan *s_instance;
Georg's avatar
Georg committed
404 405 406
};

#endif