messages.h 7.26 KB
Newer Older
1
/*
2
 * Copyright (C) 2014-2018 CZ.NIC
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * This program 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.
 *
 * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations including
 * the two.
 */

24
#pragma once
25 26 27

#include <QObject>

28
#include "src/qml_isds/message_interface.h"
29

30 31 32 33 34 35 36 37
/*
 * Class Messages provides interface between QML and message database.
 * Class is initialised in the main function (main.cpp)
 */
class Messages : public QObject {
    Q_OBJECT

public:
38 39
	/* See class MessageDb. */
	enum MessageType {
40 41
		TYPE_RECEIVED = 0x01, /*!< One is received. */
		TYPE_SENT = 0x02 /*!< Two is sent. */
42
	};
43 44 45
	Q_ENUM(MessageType)
	Q_DECLARE_FLAGS(MessageTypes, MessageType)
	Q_FLAG(MessageTypes)
46

47 48 49 50 51 52
	/*!
	 * @brief Declare various properties to the QML system.
	 */
	static
	void declareQML(void);

53 54
	/*!
	 * @brief Constructor.
55 56
	 *
	 * @param[in] parent Parent object.
57
	 */
58
	explicit Messages(QObject *parent = Q_NULLPTR);
59

Martin Straka's avatar
Martin Straka committed
60 61 62 63 64 65 66 67 68 69 70
	/*!
	 * @brief Load contacts from database and fill QML listview via model.
	 *
	 * @param[in,out] dbModelVariant QVariant holding databox model to be set.
	 * @param[in] userName User name identifying account.
	 * @param[in] dbId Account databox ID.
	 */
	Q_INVOKABLE static
	void fillContactList(const QVariant &dbModelVariant,
	    const QString &userName, const QString &dbId);

71
	/*!
72
	 * @brief Load messages from database and fill QML listview via model.
73
	 *
74
	 * @param[in,out] msgModelVariant QVariant holding message model to be set.
75 76
	 * @param[in] userName User name identifying account.
	 * @param[in] msgType Message orientation.
77
	 */
78 79 80
	Q_INVOKABLE static
	void fillMessageList(const QVariant &msgModelVariant,
	    const QString &userName, enum MessageType msgType);
81 82

	/*!
83 84 85 86 87 88 89 90
	 * @brief Returns HTML-formatted string containing message description.
	 *
	 * @note QML has no notion about qint64, therefore the id is passed
	 *     via a string.
	 *
	 * @param[in] userName User name identifying account.
	 * @param[in] msgIdStr String containing message identifier.
	 * @return Non-empty string on success.
91
	 */
92 93 94
	Q_INVOKABLE static
	QString getMessageDetail(const QString &userName,
	    const QString &msgIdStr);
95

96 97 98 99 100 101 102 103 104
	/*!
	 * @brief Get message envelope data to QML and set recipient into model.
	 *
	 * @param[in] userName User name identifying the account.
	 * @param[in] dmId Message identifier.
	 * @param[in,out] dbModelVariant QVariant holding recipient model to be set.
	 * @return Pointer to message envelope data object.
	 */
	Q_INVOKABLE
105
	QmlIsdsEnvelope *getMsgEnvelopeDataAndSetRecipient(const QString &userName,
106 107
	    qint64 msgId, const QVariant &dbModelVariant);

108 109
	/*!
	 * @brief Set message in database as locally read.
110 111 112 113 114
	 *
	 * @param[in,out] msgModelVariant QVariant holding message model to be set.
	 * @param[in] userName User name identifying the account.
	 * @param[in] msgId Message identifier.
	 * @param[in] isRead True if read.
115
	 */
116 117 118
	Q_INVOKABLE static
	void markMessageAsLocallyRead(const QVariant &msgModelVariant,
	    const QString &userName, qint64 msgId, bool isRead);
119 120 121

	/*!
	 * @brief Set all messages in database as locally read/unread.
122 123 124 125 126
	 *
	 * @param[in,out] msgModelVariant QVariant holding message model to be set.
	 * @param[in] userName User name identifying the account.
	 * @param[in] msgType Message orientation.
	 * @param[in] isRead True if read.
127
	 */
128 129 130
	Q_INVOKABLE static
	void markMessagesAsLocallyRead(const QVariant &msgModelVariant,
	    const QString &userName, enum MessageType msgType, bool isRead);
131

132 133 134 135
	/*!
	 * @brief Update records management icon after message upload.
	 *
	 * @param[in,out] msgModelVariant QVariant holding message model to be set.
136
	 * @param[in] userName User name identifying the account.
137 138 139 140
	 * @param[in] dmId Message id.
	 * @param[in] isUploadRm Set whether to force records management
	 *                       upload state.
	 */
141 142 143
	Q_INVOKABLE
	void updateRmStatus(const QVariant &msgModelVariant,
	    const QString &userName, qint64 dmId, bool isUploadRm);
144

145 146
	/*!
	 * @brief Delete selected message from databases.
147 148 149 150 151
	 *
	 * @param[in] acntModelVariant Variant holding account model.
	 * @param[in] msgModelVariant Variant holding message model.
	 * @param[in] userName User name identifying the account.
	 * @param[in] msgId Identifier of the message to be deleted.
152
	 */
153
	Q_INVOKABLE static
154 155 156
	void deleteMessageFromDbs(const QVariant &acntModelVariant,
	    const QVariant &msgModelVariant, const QString &userName,
	    qint64 msgId);
157

158
	/*!
159 160
	 * @brief Move or create new databases to new location.
	 *
161 162
	 * @param[in] newLocation New databases location (folder).
	 * @return True on success, false of failure.
163 164
	 */
	Q_INVOKABLE
165
	bool moveOrCreateNewDbsToNewLocation(const QString &newLocation);
166

167 168 169
	/*!
	 * @brief Search messages via all accounts.
	 *
170 171 172
	 * @param[in,out] msgModelVariant QVariant holding message model to be set.
	 * @param[in] phrase Search phrase.
	 * @param[in] msgTypes Sought message types.
173 174
	 * @return Number of messages in the search result.
	 */
175
	Q_INVOKABLE static
176 177
	int searchMsg(const QVariant &msgModelVariant, const QString &phrase,
	    MessageTypes msgTypes);
178

179 180 181 182
	/*!
	 * @brief Delete messages from all message databases together with files
	 *        where lifetime expired.
	 *
183
	 * @param[in] days Lifetime in days.
184 185 186
	 */
	void deleteExpiredMessagesFromDbs(int days);

187 188 189 190 191 192 193 194 195
signals:

	/*!
	 * @brief Update message detail info.
	 *
	 * @param[in] msgHtmlInfo Message detail info string.
	 */
	void updateMessageDetail(QString msgHtmlInfo);

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
private:
	/*!
	 * @brief Specifies whether the files should be moved or created from
	 *     scratch.
	 */
	enum ReloactionAction {
		RA_NONE, /*!< Don't do anything. */
		RA_RELOCATE, /*!< relocate existing ones. */
		RA_CREATE_NEW /*!< Create new empty databases. .*/
	};

	/*!
	 * @brief Generates a dialogue and asks the user what he wants to do.
	 *
	 * @return The chosen action.
	 */
	static
	enum ReloactionAction askAction(void);

	/*!
	 * @brief Return size of all databases in bytes.
	 *
	 * @return Total size of all database files or -1 on error.
	 */
	static
	int getAllDbSize(void);

	/*!
	 * @brief Estimate new database size in new location.
	 *
	 * @return Estimated required size.
	 */
	static
	int estimateAllDbSize(void);

	/*!
	 * @brief Relocates account database files.
	 *
234
	 * @param[in] newLocationDir New location directory.
235 236 237 238
	 * @param[in] action Desired action to preform.
	 * @return True on success, false of failure.
	 */
	static
239
	bool relocateDatabases(const QString &newLocationDir,
240
	    enum ReloactionAction action);
241 242
};

243 244 245 246
Q_DECLARE_OPERATORS_FOR_FLAGS(Messages::MessageTypes)

/* QML passes its arguments via QVariant. */
Q_DECLARE_METATYPE(Messages::MessageType)