files.h 9.59 KB
Newer Older
1
/*
2
 * Copyright (C) 2014-2018 CZ.NIC
3 4 5 6 7 8 9 10
 *
 * 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
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 13 14 15 16 17 18 19 20 21 22 23
 * 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/models/filemodel.h"
29
#include "src/qml_interaction/message_info.h"
30

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

public:
Martin Straka's avatar
Martin Straka committed
39

40 41
	/* Defines file type to be saved or sent via email */
	enum MsgAttachFlag {
42 43 44
		NO_FILES = 0x00,
		MSG_ZFO = 0x01, /* Complete message in zfo format. */
		MSG_ATTACHS = 0x02 /* Attachments of the data message. */
45
	};
46
	Q_ENUM(MsgAttachFlag)
47 48 49 50
	/*
	 * Flags inside QML:
	 * https://forum.qt.io/topic/10060/q_enums-q_declare_metatype-and-qml/2
	 */
51 52
	Q_DECLARE_FLAGS(MsgAttachFlags, MsgAttachFlag)
	Q_FLAG(MsgAttachFlags)
53

Martin Straka's avatar
Martin Straka committed
54 55 56 57 58 59 60
	/* Defines missing file id vaule and zfo id */
	enum FileIdType {
		NO_FILE_ID = -1,
		DB_ZFO_ID = -2
	};
	Q_ENUM(FileIdType)

61 62 63 64 65 66
	/*!
	 * @brief Declare various properties to the QML system.
	 */
	static
	void declareQML(void);

67
	/*!
68
	 * @brief Constructor.
69 70 71 72
	 *
	 * @param[in] parent Parent object.
	 */
	explicit Files(QObject *parent = Q_NULLPTR);
73

74 75 76 77 78 79 80 81 82
	/*!
	 * @brief Generates a notification dialogue about the
	 *        result of attachment saving.
	 *
	 * @param[in] destPath Path to which the attachment was saved.
	 */
	static
	void attachmentSavingNotification(const QString &destPath);

Martin Straka's avatar
Martin Straka committed
83 84 85 86 87 88 89
	/*!
	 * @brief Delete files from all databases where file lifetime expired.
	 *
	 * @param[in] days Lifetime in days.
	 */
	void deleteExpiredFilesFromDbs(int days);

90 91 92 93 94 95
	/*!
	 * @brief Get attachment file icon from file name.
	 *
	 * @param[in] fileName File name.
	 * @return File icon resources string.
	 */
96
	Q_INVOKABLE static
97 98 99 100 101 102 103 104
	QString getAttachmentFileIcon(const QString &fileName);

	/*!
	 * @brief Get attachment file size in bytes.
	 *
	 * @param[in] filePath Path to file.
	 * @return File size in bytes.
	 */
105
	Q_INVOKABLE static
106 107
	qint64 getAttachmentSizeInBytes(const QString &filePath);

108 109 110 111 112 113 114 115
	/*!
	 * @brief Obtain attachment from database.
	 *
	 * @param[in] userName User name identifying the account.
	 * @param[in] fileId Attachment file identifier.
	 * @return Attachment content.
	 */
	Q_INVOKABLE static
116
	QByteArray getFileRawContentFromDb(const QString &userName, int fileId);
117

118 119
	/*!
	 * @brief Open attachment from database.
120 121 122
	 *
	 * @param[in] userName User name identifying the account.
	 * @param[in] fileId Attachment file identifier.
123
	 */
124
	Q_INVOKABLE static
125
	void openAttachmentFromDb(const QString &userName, int fileId);
126

127 128
	/*!
	 * @brief Open attachment in default application.
129 130
	 *
	 * @param[in] fileName File name.
131
	 * @param[in] binaryData Raw file content.
132
	 */
133 134
	Q_INVOKABLE static
	void openAttachment(const QString &fileName,
135
	    const QByteArray &binaryData);
136

137 138
	/*!
	 * @brief Open attachment from path in default application.
139 140
	 *
	 * @param[in] filePath File path.
141 142 143 144
	 */
	Q_INVOKABLE static
	void openAttachmentFromPath(const QString &filePath);

145
	/*!
146
	 * @brief Send message attachments or complete zfo message
147
	 *     from database with email application.
148
	 *
Martin Straka's avatar
Martin Straka committed
149
	 * @param[in] userName User name of account.
150
	 * @param[in] msgId Message id.
151
	 * @param[in] attachFlags Specifies which attachments to send.
152
	 */
153
	Q_INVOKABLE
154
	void sendMsgFilesWithEmail(const QString &userName,
155
	    qint64 msgId, MsgAttachFlags attachFlags);
156 157 158

	/*!
	 * @brief Delete file database.
159 160
	 *
	 * @param[in] userName User name of account.
161
	 */
162 163
	Q_INVOKABLE
	void deleteFileDb(const QString &userName);
164

165
	/*!
Karel Slaný's avatar
Karel Slaný committed
166
	 * @brief Vacuum all file databases.
167
	 */
168 169
	Q_INVOKABLE
	void vacuumFileDbs(void);
170

171 172 173
	/*!
	 * @brief Delete message attachments from databases.
	 *
Martin Straka's avatar
Martin Straka committed
174
	 * @param[in] userName User name of account.
Martin Straka's avatar
Martin Straka committed
175
	 * @param[in] msgId Message id.
176 177 178 179
	 * @return true if success.
	 */
	Q_INVOKABLE
	bool deleteAttachmentsFromDb(const QString &userName, qint64 msgId);
180

181 182 183 184 185 186 187 188 189
	/*!
	 * @brief Checks whether file is readable.
	 *
	 * @param[in] filePath Path to file.
	 * @return True if file exists and is readable.
	 */
	static
	bool fileReadable(const QString &filePath);

190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
	/*!
	 * @brief Tests whether attachment is ZFO file.
	 *
	 * @param[in] fileName File name.
	 * @return True if file has zfo suffix.
	 */
	Q_INVOKABLE static
	bool isZfoFile(const QString &fileName);

	/*!
	 * @brief Returns raw file content.
	 *
	 * @param[in] filePath Path to file.
	 * @return File content, QByteArray() on error.
	 */
	Q_INVOKABLE static
	QByteArray rawFileContent(const QString &filePath);

208
	/*!
209
	 * @brief Parse content of ZFO file.
210
	 *
211 212 213 214
	 * @note QML handles deallocation of returned objects. For mode retail
	 *     see section 'Data Ownerfhip'
	 *     of 'Data Type Conversion Between QML and C++'
	 *     at http://doc.qt.io/qt-5/qtqml-cppintegration-data.html .
215
	 *
216 217
	 * @param[in,out] attachModelVariant QVariant holding attachment model
	 *                                   to be set.
218
	 * @param[in] rawZfoData Raw ZFO data.
219 220
	 * @return Pointer to newly allocated structure containing message
	 *     information.
221 222
	 */
	Q_INVOKABLE static
223
	MsgInfo *zfoData(const QVariant &attachModelVariant,
224
	    const QByteArray &rawZfoData);
Martin Straka's avatar
Martin Straka committed
225

226 227 228 229 230 231
	/*!
	 * @brief Sets attachment model.
	 *
	 * @param[out] attachModel Attachment model to be set.
	 * @param[in] userName Identifies the account.
	 * @param[in] msgId Message identifier.
232
	 * @return True if success.
233 234 235
	 */
	static
	bool setAttachmentModel(FileListModel &attachModel,
236
	    const QString &userName, qint64 msgId);
237

238
	/*!
239
	 * @brief Send attachment from ZFO with email application.
240
	 *
241
	 * @note QML does not know qint64 therefore we use a QStrting as
242 243 244
	 *     message id.
	 *
	 * @param[in] attachModelVariant QVariant holding attachment model.
245
	 * @param[in] msgIdStr String with message id.
246
	 * @param[in] subject Email subject.
Martin Straka's avatar
Martin Straka committed
247
	 * @param[in] body Email body.
248 249 250 251 252
	 */
	Q_INVOKABLE static
	void sendAttachmentEmailZfo(const QVariant &attachModelVariant,
	    const QString &msgIdStr, QString subject, QString body);

253
	/*!
254 255
	 * @brief Save message attachments or complete zfo message from
	 *        database to external storage.
256 257 258
	 *
	 * @param[in] userName User name identifying the account.
	 * @param[in] msgIdStr String with message id.
259
	 * @param[in] attachFlags Specifies which attachments to save.
260 261
	 */
	Q_INVOKABLE static
262 263
	void saveMsgFilesToDisk(const QString &userName,
	    const QString &msgIdStr, MsgAttachFlags attachFlags);
264 265 266 267 268 269 270 271 272 273 274

	/*!
	 * @brief Save attachments from ZFO to disk.
	 *
	 * @param[in] attachModelVariant QVariant holding attachment model.
	 * @param[in] msgIdStr String with message id.
	 */
	Q_INVOKABLE static
	void saveAttachmentsToDiskZfo(const QVariant &attachModelVariant,
	    const QString &msgIdStr);

275 276 277 278 279 280 281 282 283 284 285 286
	/*!
	 * @brief Delete temporary file from storage.
	 *
	 * @note This function is used on iOS when external ZFO file is opened.
	 *       iOS moves target file to Datovka private folder. Here, after
	 *       reading of file content, Datovka must this file removed itself.
	 *
	 * @param[filePath] Path to file.
	 */
	Q_INVOKABLE
	void deleteTmpFileFromStorage(const QString &filePath);

287 288 289 290
signals:
	/*!
	 * @brief Set new statusbar text and active busy indicator to QML.
	 *
Martin Straka's avatar
Martin Straka committed
291 292
	 * @param[in] txt Text message for statusbar.
	 * @param[in] busy True means the statusbar busy indicator is active
293 294 295 296
	 *                   and shown, false = disabled and hidden
	 */
	void statusBarTextChanged(QString txt, bool busy);

297
private:
298

299
	/*!
Martin Straka's avatar
Martin Straka committed
300
	 * @brief Parse xml data of zfo file.
301
	 *
302 303 304
	 * @todo This function must be reworked as it is called multiple times
	 *     on a single message to obtain various data.
	 *
305
	 * @param[out] type ZFO type.
306
	 * @param[out] idStr Message identifier number held in a string.
307 308
	 * @param[out] annotation Annotation string.
	 * @param[out] msgDescrHtml Message text.
309
	 * @param[out] attachModel Attachment model to be set.
310
	 * @param[out] emailBody Email body.
311
	 * @param[in] xmlData Xml file data.
Martin Straka's avatar
Martin Straka committed
312
	 * @return True if success.
313
	 */
314
	static
315
	bool parseXmlData(enum MsgInfo::ZfoType *type, QString *idStr,
316 317
	    QString *annotation, QString *msgDescrHtml,
	    FileListModel *attachModel, QString *emailBody, QByteArray xmlData);
318 319

	/*!
320
	 * @brief Parse and show xml data of zfo file.
321
	 *
322
	 * @param[in] type ZFO file type.
323 324 325 326 327 328
	 * @param[out] idStr Message identifier number held in a string.
	 * @param[out] annotation Annotation string.
	 * @param[out] msgDescrHtml Message text.
	 * @param[out] attachModel Attachment model to be set.
	 * @param[out] emailBody Email body.
	 * @param[in] xmlData Xml file data.
Martin Straka's avatar
Martin Straka committed
329
	 * @return True if success.
330 331
	 */
	static
332
	bool parseAndShowXmlData(enum MsgInfo::ZfoType type, QString *idStr,
333
	    QString *annotation, QString *msgDescrHtml,
Martin Straka's avatar
Martin Straka committed
334 335
	    FileListModel *attachModel,  QString *emailBody,
	    QByteArray &xmlData);
336 337
};

338
Q_DECLARE_OPERATORS_FOR_FLAGS(Files::MsgAttachFlags)
339

Martin Straka's avatar
Martin Straka committed
340 341
/* Declare FileIdType to QML. */
Q_DECLARE_METATYPE(Files::FileIdType)
342 343
Q_DECLARE_METATYPE(Files::MsgAttachFlag)
Q_DECLARE_METATYPE(Files::MsgAttachFlags)