Kaidan.h 7.92 KB
Newer Older
1
/*
2
 *  Kaidan - A user-friendly XMPP client for every device!
3
 *
Linus Jahn's avatar
Linus Jahn committed
4 5
 *  Copyright (C) 2016-2018 Kaidan developers and contributors
 *  (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.
 *
Linus Jahn's avatar
Linus Jahn 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
Linus Jahn's avatar
Linus Jahn 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

34
// Qt
Georg's avatar
Georg committed
35
#include <QObject>
36
#include <QString>
37
// gloox
38
#include <gloox/connectionlistener.h>
39
// Kaidan
40 41 42 43 44 45 46 47 48
#include "ClientThread.h"
#include "Globals.h"
#include "Enums.h"

class AvatarFileStorage;
class RosterManager;
class RosterModel;
class MessageModel;
class QSettings;
49
class QGuiApplication;
50 51 52 53 54 55 56 57 58 59 60 61 62 63
class Database;

using namespace Enums;

/**
 * @class Kaidan Kaidan's Back-End Class
 * 
 * @brief This class will initiate the complete back-end, including the @see Database
 * connection, viewing models (@see MessageModel, @see RosterModel), etc.
 * 
 * This class will run in the main thread, only the XMPP connection runs in another
 * thread (@see ClientThread).
 */
class Kaidan : public QObject
Georg's avatar
Georg committed
64
{
65
	Q_OBJECT
66

67
	Q_PROPERTY(RosterModel* rosterModel READ getRosterModel NOTIFY rosterModelChanged)
68
	Q_PROPERTY(MessageModel* messageModel READ getMessageModel NOTIFY messageModelChanged)
69
	Q_PROPERTY(AvatarFileStorage* avatarStorage READ getAvatarStorage NOTIFY avatarStorageChanged)
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(QString chatPartner READ getChatPartner WRITE setChatPartner NOTIFY chatPartnerChanged)
Georg's avatar
Georg committed
76

Georg's avatar
Georg committed
77
public:
78 79 80
	/**
	 * Constructor
	 */
81
	Kaidan(QGuiApplication *app, QObject *parent = 0);
82 83 84 85

	/**
	 * Destructor
	 */
86
	~Kaidan();
87

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
	/**
	 * 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).
	 */
	Q_INVOKABLE bool mainConnect();

	/**
	 * Disconnect from XMPP server
	 *
	 * This will disconnect the client from the server. When disconnected,
	 * the connectionStateChanged signal will be emitted.
	 * 
	 * @param openLogInPage If true, the newCredentialsNeeded signal will be
	 * emitted.
	 */
	Q_INVOKABLE void mainDisconnect(bool openLogInPage = false);

	/**
	 * 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.
	 */
120
	Q_INVOKABLE void sendMessage(QString jid, QString message);
121 122 123 124 125 126 127

	/**
	 * 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.
	 */
128
	Q_INVOKABLE void addContact(QString jid, QString nick);
129 130 131 132 133 134

	/**
	 * Remove a contact from your roster
	 *
	 * Only the JID is needed.
	 */
135
	Q_INVOKABLE void removeContact(QString jid);
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253

	/**
	 * Returns a URL to a given resource file name
	 *
	 * This will check various paths which could contain the searched file.
	 * If the file was found, it'll return a `file://` or a `qrc:/` url to
	 * the file.
	 */
	Q_INVOKABLE QString getResourcePath(QString resourceName) const;

	/**
	 * Returns the current ConnectionState
	 */
	Q_INVOKABLE quint8 getConnectionState() const;

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

	/**
	 * Returns a string of this build's Kaidan version
	 */
	Q_INVOKABLE QString getVersionString() const
	{
		return QString(VERSION_STRING);
	}

	/**
	 * Returns a string without new lines, unneeded spaces, etc.
	 *
	 * See QString::simplified for more information.
	 */
	Q_INVOKABLE QString removeNewLinesFromString(QString input) const
	{
		return input.simplified();
	}

	/**
	 * Set own JID used for connection
	 *
	 * To really change the JID of the current connection, you'll need to
	 * reconnect.
	 */
	void setJid(QString jid);

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

	/**
	 * Set a optional custom JID resource (device name)
	 */
	void setJidResource(QString jidResource);

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

	/**
	 * Set the password for next connection
	 */
	void setPassword(QString password);

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

	/**
	 * Set the currently opened chat
	 *
	 * This will set a filter on the database to only view the wanted messages.
	 */
	void setChatPartner(QString jid);

	/**
	 * Get the currrently opened chat
	 */
	QString getChatPartner() const
	{
		return chatPartner;
	}

	/**
	 * Get the roster model
	 */
	RosterModel* getRosterModel() const
	{
		return rosterModel;
	}

	/**
	 * Get the message model
	 */
	MessageModel* getMessageModel() const
	{
		return messageModel;
	}

	/**
	 * Get the avatar storage
	 */
	AvatarFileStorage* getAvatarStorage() const
	{
		return avatarStorage;
	}
254

255
signals:
256
	void rosterModelChanged();
257
	void messageModelChanged();
258
	void avatarStorageChanged();
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274

	/**
	 * Emitted, when the client's connection state has changed (e.g. when
	 * successfully connected or when disconnected)
	 */
	void connectionStateChanged(quint8 state);

	/**
	 * Emitted, when the client failed to connect and gives the reason in
	 * a DisconnectionReason enumatrion.
	 */
	void disconnReasonChanged(quint8 reason);

	/**
	 * Emitted when the JID was changed
	 */
275
	void jidChanged();
276 277 278 279

	/**
	 * Emitted when the JID resouce (device name) has changed
	 */
280
	void jidResourceChanged();
281 282 283 284

	/**
	 * Emitted when the used password for logging in has changed
	 */
285
	void passwordChanged();
286 287 288 289

	/**
	 * Emitted when the currently opnened chat has changed
	 */
Linus Jahn's avatar
Linus Jahn committed
290
	void chatPartnerChanged(QString chatPartner);
291

292 293 294 295 296 297 298 299 300 301 302 303 304
	/**
	 * 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();
305

306 307 308 309 310
	/**
	 * Show passive notification
	 */
	void passiveNotificationRequested(QString text);

311
private:
312
	void connectDatabases();
Georg's avatar
Georg committed
313

314
	ClientThread *client;
315
	Database *database;
316 317
	RosterModel *rosterModel;
	RosterManager *rosterManager;
318
	MessageModel *messageModel;
319
	AvatarFileStorage *avatarStorage;
320
	QSettings *settings;
321

322
	ClientThread::Credentials creds;
Linus Jahn's avatar
Linus Jahn committed
323
	QString chatPartner;
Georg's avatar
Georg committed
324 325 326
};

#endif