files.h 9.28 KB
Newer Older
1
/*
2
 * Copyright (C) 2014-2017 CZ.NIC
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * 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.
 */

#ifndef FILES_H
#define FILES_H

#include <QObject>

29
#include "src/models/filemodel.h"
30
#include "src/qml_interaction/message_info.h"
31

32 33 34 35 36 37 38 39
/*
 * 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:
40
	/*!
41
	 * @brief Constructor.
42 43 44 45
	 *
	 * @param[in] parent Parent object.
	 */
	explicit Files(QObject *parent = Q_NULLPTR);
46

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
	/*!
	 * @brief Get attachment file icon from file name.
	 *
	 * @param[in] fileName File name.
	 * @return File icon resources string.
	 */
	Q_INVOKABLE
	QString getAttachmentFileIcon(const QString &fileName);

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

65 66 67 68 69 70 71 72 73 74 75 76
	/*!
	 * @brief Obtain attachment from database.
	 *
	 * @param[in] userName User name identifying the account.
	 * @param[in] msgIdStr String with message id.
	 * @param[in] fileId Attachment file identifier.
	 * @return Attachment content.
	 */
	Q_INVOKABLE static
	QByteArray getAttachmentDb(const QString &userName,
	    const QString &msgIdStr, int fileId);

77 78
	/*!
	 * @brief Open attachment from database.
79 80 81 82
	 *
	 * @param[in] userName User name identifying the account.
	 * @param[in] msgIdStr String with message id.
	 * @param[in] fileId Attachment file identifier.
83
	 */
84 85 86
	Q_INVOKABLE static
	void openAttachmentFromDb(const QString &userName,
	    const QString &msgIdStr, int fileId);
87

88 89 90
	/*!
	 * @brief Open attachment in default application.
	 */
91 92
	Q_INVOKABLE static
	void openAttachment(const QString &fileName,
93
	    const QByteArray &base64Data);
94

95 96 97 98 99 100
	/*!
	 * @brief Open attachment from path in default application.
	 */
	Q_INVOKABLE static
	void openAttachmentFromPath(const QString &filePath);

101
	/*!
102 103
	 * @brief Send attachments from database with email application.
	 *
Martin Straka's avatar
Martin Straka committed
104 105
	 * @param[in] userName User name of account.
	 * @param[in] msgId    Message id.
106
	 */
107
	Q_INVOKABLE void sendAttachmentsWithEmailFromDb(const QString &userName,
108 109 110 111 112 113 114 115 116 117 118
	    qint64 msgId);

	/*!
	 * @brief Delete file database.
	 */
	Q_INVOKABLE void deleteFileDb(const QString &userName);

	/*!
	 * @brief Delete files from all file databases
	 *        where file lifetime expired.
	 *
Martin Straka's avatar
Martin Straka committed
119
	 * @param[in] days Lifetime in days.
120 121 122
	 */
	void deleteExpiredFilesFromDbs(int days);

123
	/*!
Karel Slaný's avatar
Karel Slaný committed
124
	 * @brief Vacuum all file databases.
125
	 */
Karel Slaný's avatar
Karel Slaný committed
126
	Q_INVOKABLE void vacuumFileDbs(void);
127

128 129 130
	/*!
	 * @brief Delete message attachments from databases.
	 *
Martin Straka's avatar
Martin Straka committed
131 132
	 * @param[in] userName User name of account.
	 * @param[in] msgId    Message id.
133 134 135 136
	 * @return true if success.
	 */
	Q_INVOKABLE
	bool deleteAttachmentsFromDb(const QString &userName, qint64 msgId);
137

138 139 140 141 142 143 144 145 146
	/*!
	 * @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);

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
	/*!
	 * @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);

	/*!
	 * @brief Decodes base64-encoded content.
167 168 169
	 *
	 * @param[in] base64Data Base64-encoded data.
	 * @return Raw data.
170 171 172 173
	 */
	Q_INVOKABLE static
	QByteArray base64ToRaw(const QByteArray &base64Data);

174
	/*!
175
	 * @brief Parse content of ZFO file.
176
	 *
177 178 179 180
	 * @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 .
181
	 *
182 183
	 * @param[in,out] attachModelVariant QVariant holding attachment model
	 *                                   to be set.
184
	 * @param[in] rawZfoData Raw ZFO data.
185 186
	 * @return Pointer to newly allocated structure containing message
	 *     information.
187 188
	 */
	Q_INVOKABLE static
189
	MsgInfo *zfoData(const QVariant &attachModelVariant,
190
	    const QByteArray &rawZfoData);
Martin Straka's avatar
Martin Straka committed
191

192 193 194 195 196 197 198 199 200 201 202 203
	/*!
	 * @brief Sets attachment model.
	 *
	 * @param[out] attachModel Attachment model to be set.
	 * @param[in] userName Identifies the account.
	 * @param[in] msgId Message identifier.
	 * @return true if success.
	 */
	static
	bool setAttachmentModel(FileListModel &attachModel,
	    const QString &userName, qint64 msgId);

204
	/*!
205
	 * @brief Send attachment from ZFO with email application.
206
	 *
207
	 * @note QML does not know qint64 therefore we use a QStrting as
208 209 210
	 *     message id.
	 *
	 * @param[in] attachModelVariant QVariant holding attachment model.
211
	 * @param[in] msgIdStr String with message id.
212 213 214 215 216 217 218
	 * @param[in] subject Email subject.
	 * @param[in] body    Email body.
	 */
	Q_INVOKABLE static
	void sendAttachmentEmailZfo(const QVariant &attachModelVariant,
	    const QString &msgIdStr, QString subject, QString body);

219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
	/*!
	 * @brief Save attachments from database to disk.
	 *
	 * @param[in] userName User name identifying the account.
	 * @param[in] msgIdStr String with message id.
	 */
	Q_INVOKABLE static
	void saveAttachmentsToDiskDb(const QString &userName,
	    const QString &msgIdStr);

	/*!
	 * @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);

239 240 241 242 243 244 245 246 247 248 249 250
	/*!
	 * @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);

251 252 253 254
signals:
	/*!
	 * @brief Set new statusbar text and active busy indicator to QML.
	 *
Martin Straka's avatar
Martin Straka committed
255 256
	 * @param[in] txt Text message for statusbar.
	 * @param[in] busy True means the statusbar busy indicator is active
257 258 259 260
	 *                   and shown, false = disabled and hidden
	 */
	void statusBarTextChanged(QString txt, bool busy);

261
private:
262 263 264
	/*!
	 * @brief Send email.
	 *
Martin Straka's avatar
Martin Straka committed
265 266 267 268 269
	 * @param[in] emailMessage Eml email content.
	 * @param[in] fileList     File list for email.
	 * @param[in] subject      Email subject
	 * @param[in] body         Email body.
	 * @param[in] msgId        Message ID.
270
	 */
271
	static
272 273 274
	void sendEmail(const QString &emailMessage, const QStringList &fileList,
	    const QString &subject, const QString &body, qint64 msgId);

Martin Straka's avatar
Martin Straka committed
275 276
	/*!
	 * @brief Decode XML data from CMS.
277
	 *
278 279
	 * @param[in] rawData File content.
	 * @return Decoded XML data or QByteArray().
280
	 */
281
	static
282
	QByteArray getXmlFromCms(const QByteArray &rawData);
283 284 285 286

	/*!
	 * @brief Decode data from ZFO file.
	 *
287 288
	 * @param[in] base64ZfoData Base64-encoded zfo file content.
	 * @return Decoded raw data or QByteArray().
289
	 */
290
	static
291
	QByteArray decodeZfoFile(const QByteArray &base64ZfoData);
292 293

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

	/*!
314
	 * @brief Parse and show xml data of zfo file.
315
	 *
316
	 * @param[in] type ZFO file type.
317 318 319 320 321 322 323 324 325
	 * @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.
	 * @return true if success.
	 */
	static
326
	bool parseAndShowXmlData(enum MsgInfo::ZfoType type, QString *idStr,
327 328
	    QString *annotation, QString *msgDescrHtml,
	    FileListModel *attachModel, QString *emailBody, QByteArray &xmlData);
329 330 331
};

#endif // FILES_H