files.cpp 28.3 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 29 30
 *
 * 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.
 */

#include <QDebug>
#include <QDesktopServices>
#include <QUrl>
#include <QMimeDatabase>
#include <QFileDialog>
#include <QMessageBox>
#include <QMimeType>
31
#include <QStack>
32 33 34

#include "src/files.h"
#include "src/common.h"
35
#include "src/settings.h"
36 37 38 39 40
#include "src/io/filesystem.h"
#include "ios/src/url_opener.h"
#include "src/models/accountmodel.h"
#include "src/sqlite/file_db_container.h"
#include "src/sqlite/message_db_container.h"
41 42 43 44 45 46 47
#include "src/net/xml_layer.h"
#include "src/sqlite/dbs.h"

#ifndef Q_OS_WIN
#include "src/crypto/crypto.h"
#endif

48 49 50 51 52 53 54
/*!
 * @brief Creates email header and message body.
 *
 * @param[in,out] message String to append data to.
 * @param[in] body Email body.
 * @param[in] subj Subject text.
 * @param[in] boundary Boundary to be used,
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
 */
static
void createEmailMessage(QString &message, const QString &body,
    const QString &subj, const QString &boundary)
{
	message.clear();

	const QString newLine("\n"); /* "\r\n" ? */

	/* Rudimentary header. */
	message += "Subject: " + subj + newLine;
	message += "MIME-Version: 1.0" + newLine;
	message += "Content-Type: multipart/mixed;" + newLine +
	    " boundary=\"" + boundary + "\"" + newLine;

	/* Body. */
	message += newLine;
	message += "--" + boundary + newLine;
	message += "Content-Type: text/plain; charset=UTF-8" + newLine;
	message += "Content-Transfer-Encoding: 8bit" + newLine;

	message += newLine;
	message += "-- " + newLine; /* Must contain the space. */
	message += body + newLine;
}

81 82 83 84 85 86 87
/*!
 * @brief Adds attachment into email.
 *
 * @param[in,out] message String to append data to.
 * @param[in] attachName Attachment name.
 * @param[in] base64 Base64-encoded attachment.
 * @param[in] boundary Boundary to be used.
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
 */
static
void addAttachmentToEmailMessage(QString &message, const QString &attachName,
    const QByteArray &base64, const QString &boundary)
{
	const QString newLine("\n"); /* "\r\n" ? */

	QMimeDatabase mimeDb;

	QMimeType mimeType(
	    mimeDb.mimeTypeForData(QByteArray::fromBase64(base64)));

	message += newLine;
	message += "--" + boundary + newLine;
	message += "Content-Type: " + mimeType.name() + "; charset=UTF-8;"
	    + newLine + " name=\"" + attachName +  "\"" + newLine;
	message += "Content-Transfer-Encoding: base64" + newLine;
	message += "Content-Disposition: attachment;" + newLine +
	    " filename=\"" + attachName + "\"" + newLine;

	for (int i = 0; i < base64.size(); ++i) {
		if ((i % 60) == 0) {
			message += newLine;
		}
		message += base64.at(i);
	}
	message += newLine;
}

117 118 119 120 121
/*!
 * @brief Adds last line into email.
 *
 * @param[in,out] message String to append data to.
 * @param[in] boundary Boundary to be used.
122 123 124 125 126 127 128 129 130 131 132 133 134
 */
static
void finishEmailMessage(QString &message, const QString &boundary)
{
	const QString newLine("\n"); /* "\r\n" ? */
	message += newLine + "--" + boundary + "--" + newLine;
}


Files::Files(QObject *parent) : QObject(parent)
{
}

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
/*!
 * @brief Set attachment model content.
 *
 * @param[in,out] model Model to be set.
 * @param[in] fileList Attachment list to be added into the model.
 */
static
void setZfoFilesToModel(FileListModel &model,
    const QList<Files::File> &fileList)
{
	model.clearAll();

	foreach (const Files::File &file, fileList) {
		model.appendFileEntry(FileListModel::Entry(-1, file.dmFileDescr,
		    file._dmFileSize, file._icon, file.dmEncodedContent));
	}
}

153 154 155 156 157 158 159 160
void Files::deleteExpiredFilesFromDbs(int days)
{
	qDebug("%s()", __func__);

	QStringList msgIDList;
	QStringList userNameList = AccountListModel::globAccounts.keys();
	foreach (const QString &userName, userNameList) {
		FileDb *fDb = NULL;
161 162
		fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName,
		    AccountListModel::globAccounts[userName].storeToDisk());
163 164 165 166 167 168 169 170 171 172
		if (fDb == NULL) {
			qDebug() << "ERROR: File database cannot open!" << userName;
			return;
		}
		msgIDList = fDb->cleanFilesInDb(days);
		if (msgIDList.isEmpty()) {
			continue;
		}

		MessageDb *msgDb = NULL;
173 174
		msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation,
		    userName, AccountListModel::globAccounts[userName].storeToDisk());
175 176 177 178
		if (msgDb == NULL) {
			qDebug() << "ERROR: Message database cannot open!";
			return;
		}
179
		msgDb->beginTransaction();
180 181 182 183
		foreach (const QString &msgId, msgIDList) {
			msgDb->setAttachmentDownloaded(msgId.toLongLong(),
			    false);
		}
184
		msgDb->commitTransaction();
185 186 187
	}
}

188 189 190 191 192 193 194 195 196 197
/*!
 * @brief Obtain attachment from database.
 *
 * @param[out] file File data to be acquired.
 * @param[in] userName User name identifying the account.
 * @param[in] msgIdStr String with message id.
 * @param[in] fileId Attachment file identifier.
 */
static
void getAttchment(FileDb::FileData &file, const QString &userName,
Martin Straka's avatar
Martin Straka committed
198
    qint64 msgId, int fileId)
199
{
200 201
	file.filename = QString();
	file.content = QString();
202

203
	if (userName.isEmpty() || (msgId <= 0) || (fileId < 0)) {
204 205 206
		return;
	}

207 208
	FileDb *fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation,
	    userName, AccountListModel::globAccounts[userName].storeToDisk());
209

210 211
	if (fDb == Q_NULLPTR) {
		qCritical() << "Cannot open file database!";
212 213 214
		return;
	}

215 216
	file = fDb->getFileContentFromDb(fileId);
}
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
QByteArray Files::getAttachmentDb(const QString &userName,
    const QString &msgIdStr, int fileId)
{
	qDebug("%s()", __func__);

	/* Obtain message identifier. */
	bool ok = false;
	qint64 msgId = msgIdStr.toLongLong(&ok);
	if (!ok || (msgId < 0)) {
		return QByteArray();
	}

	FileDb::FileData file;
	getAttchment(file, userName, msgId, fileId);

	return QByteArray::fromBase64(file.content.toUtf8());
}

void Files::openAttachmentFromDb(const QString &userName,
    const QString &msgIdStr, int fileId)
{
	qDebug("%s()", __func__);

	/* Obtain message identifier. */
	bool ok = false;
	qint64 msgId = msgIdStr.toLongLong(&ok);
	if (!ok || (msgId < 0)) {
		return;
	}

	FileDb::FileData file;
	getAttchment(file, userName, msgId, fileId);

	Q_ASSERT(!file.filename.isEmpty());
	if (file.filename.isEmpty()) {
253 254 255 256
		qDebug() << "ERROR: File name is empty!";
		return;
	}

257
	openAttachment(file.filename, file.content.toUtf8());
258 259
}

260
void Files::openAttachment(const QString &fileName, const QByteArray &base64Data)
261 262 263
{

	Q_ASSERT(!fileName.isEmpty());
264
	Q_ASSERT(!base64Data.isEmpty());
265

266 267
	if (fileName.isEmpty() || base64Data.isEmpty()) {
		qCritical() << "File name or its content is empty!";
268 269 270
		return;
	}

271 272 273 274
	if (isZfoFile(fileName)) {
		/* Don't open zfo files from here. */
		Q_ASSERT(0);
		qCritical() << "This should open ZFO files by itself.";
275 276 277
		return;
	}

278 279
	QByteArray rawData(QByteArray::fromBase64(base64Data));
	QString filePath(writeFileToTmpDir(fileName, TEMP_DIR_NAME, rawData));
280 281

	if (!filePath.isEmpty()) {
282
		qInfo() << "Creating temporary file" << filePath;
283 284 285

#if defined Q_OS_IOS
		UrlOpener urlOpener;
286
		urlOpener.openFile(filePath);
287 288 289 290
		/*
		 * TODO - we may check exceptions form objective-C (openFile)
		 * and return as bool.
		 */
291
#else /* !defined Q_OS_IOS */
292
		if (!QDesktopServices::openUrl(QUrl::fromLocalFile(filePath))) {
293 294 295 296
			QMessageBox msgBox;
			msgBox.setIcon(QMessageBox::Critical);
			msgBox.setWindowTitle(tr("Open attachment error"));
			msgBox.setText(tr("There is no application to open this file format."));
297
			msgBox.setInformativeText(tr("File: '%1'").arg(filePath));
298 299 300 301
			msgBox.setStandardButtons(QMessageBox::Ok);
			msgBox.setDefaultButton(QMessageBox::Ok);
			msgBox.exec();
		}
302 303
#endif /* defined Q_OS_IOS */
	} else {
304
		qCritical() << "Cannot create temporary file for" << fileName;
305 306 307 308 309 310 311
		QMessageBox msgBox;
		msgBox.setIcon(QMessageBox::Critical);
		msgBox.setWindowTitle(tr("Open attachment error"));
		msgBox.setText(tr("Cannot save selected file to disk for opening."));
		msgBox.setStandardButtons(QMessageBox::Ok);
		msgBox.setDefaultButton(QMessageBox::Ok);
		msgBox.exec();
312 313 314
	}
}

315 316
void Files::sendAttachmentsWithEmailFromDb(const QString &userName,
    qint64 msgId)
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
{
	qDebug("%s()", __func__);

	if (userName.isEmpty() || msgId <= 0) {
		return;
	}

	QString body;
	QString subject;
	QStringList fileList;

	QString emailMessage;
	const QString boundary("-----123456789asdfghj_" +
	QDateTime::currentDateTimeUtc().toString(
	    "dd.MM.yyyy-HH:mm:ss.zzz"));

	MessageDb *msgDb = NULL;
334 335
	msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName,
	    AccountListModel::globAccounts[userName].storeToDisk());
336 337 338 339 340 341 342 343 344 345
	if (msgDb == NULL) {
		qDebug() << "ERROR: Message database cannot open!";
		return;
	}
	if (!msgDb->getMessageDataForEmail(msgId, body, subject)) {
		qDebug() << "ERROR: Message data mssing!";
		return;
	}

	FileDb *fDb = NULL;
346 347
	fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName,
	    AccountListModel::globAccounts[userName].storeToDisk());
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382

	if (fDb == NULL) {
		qDebug() << "ERROR: File database cannot open!";
		return;
	}

	QList<FileDb::FileData> filelist;

	filelist = fDb->getFilesFromDb(msgId);
	if (filelist.isEmpty()) {
		qDebug() << "ERROR: File list is empty!";
		return;
	}

	createEmailMessage(emailMessage, body, subject,  boundary);

	deleteFilesFromDir(EMAIL_DIR_NAME);

	QString fileName;
	foreach (const FileDb::FileData &file, filelist) {
		fileName = file.filename;
		if (fileName.isEmpty()) {
			qDebug() << "ERROR: File name is empty!";
			return;
		}
		QByteArray data = QByteArray::fromBase64(file.content.toUtf8());
		fileName = writeFileToEmailDir(fileName, msgId, data);
		fileList.append(fileName);
		addAttachmentToEmailMessage(emailMessage, file.filename,
		    file.content.toUtf8(), boundary);
		qDebug() << fileName;
	}

	finishEmailMessage(emailMessage, boundary);

383 384
	sendEmail(emailMessage, fileList, subject, body, msgId);
}
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403

void Files::deleteFileDb(const QString &userName)
{
	qDebug("%s()", __func__);

	QMessageBox msgBox;
	msgBox.setWindowTitle(tr("Delete files: %1").arg(userName));
	msgBox.setIcon(QMessageBox::Question);
	msgBox.setText(tr("Do you want to clean up the file database "
	    "of account '%1'?").arg(userName));
	msgBox.setInformativeText(tr("Note: All attachment files of messages "
	    "will be removed from the database."));
	msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
	msgBox.setDefaultButton(QMessageBox::No);
	if (msgBox.exec() == QMessageBox::No) {
		return;
	}

	FileDb *fDb = NULL;
404 405
	fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName,
	    AccountListModel::globAccounts[userName].storeToDisk());
406 407 408 409 410 411 412 413 414 415
	if (fDb == NULL) {
		qDebug() << "ERROR: File database open error!";
		return;
	}
	if (!globFileDbsPtr->deleteDb(fDb)) {
		qDebug() << "ERROR: File database could not delete!";
		return;
	}

	MessageDb *msgDb = NULL;
416 417
	msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation, userName,
	    AccountListModel::globAccounts[userName].storeToDisk());
418 419 420 421 422 423 424 425 426
	if (msgDb == NULL) {
		qDebug() << "ERROR: Message database cannot open!";
		return;
	}
	if (!msgDb->setAttachmentsDownloaded(false)) {
		qDebug() << "ERROR: Message data mssing!";
		return;
	}
}
427

Karel Slaný's avatar
Karel Slaný committed
428
void Files::vacuumFileDbs(void)
429 430 431
{
	qDebug("%s()", __func__);

Karel Slaný's avatar
Karel Slaný committed
432
	emit statusBarTextChanged(tr("Vacuum databases"), true);
433 434 435 436

	QStringList userNameList = AccountListModel::globAccounts.keys();
	foreach (const QString &userName, userNameList) {
		FileDb *fDb = NULL;
437 438
		fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName,
		    AccountListModel::globAccounts[userName].storeToDisk());
439 440 441 442
		if (fDb == NULL) {
			qDebug() << "ERROR: File database cannot open!" << userName;
			return;
		}
Karel Slaný's avatar
Karel Slaný committed
443
		fDb->vacuumFileDb();
444 445
	}

Karel Slaný's avatar
Karel Slaný committed
446
	emit statusBarTextChanged(tr("Operation Vacuum has finished"), false);
447
}
448 449 450 451 452 453

bool Files::deleteAttachmentsFromDb(const QString &userName, qint64 msgId)
{
	qDebug("%s()", __func__);

	FileDb *fDb = NULL;
454 455
	fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName,
	    AccountListModel::globAccounts[userName].storeToDisk());
456 457 458 459 460 461 462 463
	if (fDb == NULL) {
		qDebug() << "ERROR: File database cannot open!" << userName;
		return false;
	}

	if (fDb->deleteFilesFromDb(msgId)) {
		MessageDb *msgDb = NULL;
		msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation,
464
		    userName, AccountListModel::globAccounts[userName].storeToDisk());
465 466 467 468 469 470 471 472 473
		if (msgDb == NULL) {
			qDebug() << "ERROR: Message database cannot open!";
			return false;
		}
		return msgDb->setAttachmentDownloaded(msgId, false);
	}

	return false;
}
474

475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
bool Files::fileReadable(const QString &filePath)
{
	if (filePath.isEmpty()) {
		Q_ASSERT(0);
		qCritical() << "Target ZFO path is empty!";
		return false;
	}

	{
		QFileInfo fileInfo(filePath);
		if (!fileInfo.isFile() || !fileInfo.isReadable()) {
			qCritical() << "Cannot open ZFO file from" << filePath;
			return false;
		}
	}

	return true;
}

bool Files::isZfoFile(const QString &fileName)
{
	return QFileInfo(fileName).suffix().toLower() == QStringLiteral("zfo");
}

QByteArray Files::rawFileContent(const QString &filePath)
{
	if (!fileReadable(filePath)) {
		return QByteArray();
	}

	QFile file(filePath);
	if (!file.open(QIODevice::ReadOnly)) {
		Q_ASSERT(0);
		qCritical() << "Cannot open file" << filePath;
		return QByteArray();
	}

	QByteArray rawData(file.readAll());
	file.close();
	return rawData;
}

QByteArray Files::base64ToRaw(const QByteArray &base64Data)
{
	return QByteArray::fromBase64(base64Data);
}

522 523
MsgInfo *Files::zfoData(const QVariant &attachModelVariant,
    const QByteArray &rawZfoData)
524
{
525
	bool deliveryInfo = false;
526
	QString idStr, annot, htmlDescr, emailBody;
527

528
	bool ret = parseXmlData(&deliveryInfo, &idStr, &annot, &htmlDescr,
529 530
	    FileListModel::modelFromVariant(attachModelVariant),
	    &emailBody, getXmlFromCms(rawZfoData));
531

532
	return ret ?
533 534
	    new (std::nothrow) MsgInfo(deliveryInfo, idStr, annot, htmlDescr,
	        emailBody) :
535
	    new (std::nothrow) MsgInfo();
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
}

bool Files::setAttachmentModel(FileListModel &attachModel,
    const QString &userName, qint64 msgId)
{
	qDebug("%s()", __func__);

	FileDb *fDb = Q_NULLPTR;
	fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName,
	    AccountListModel::globAccounts[userName].storeToDisk());

	if (fDb == Q_NULLPTR) {
		qCritical() << "Cannot open file database!";
		return false;
	}
	attachModel.clearAll();
	fDb->getFileListFromDb(attachModel, msgId);
	return true;
}

void Files::sendAttachmentEmailZfo(const QVariant &attachModelVariant,
    const QString &msgIdStr, QString subject, QString body)
{
	qDebug("%s()", __func__);

	/* Obtain pointer to attachment model. */
562 563
	const FileListModel *attachModel =
	    FileListModel::modelFromVariant(attachModelVariant);
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
	if (attachModel == Q_NULLPTR) {
		Q_ASSERT(0);
		return;
	}

	/* Obtain message identifier. */
	bool ok = false;
	qint64 msgId = msgIdStr.toLongLong(&ok);
	if (!ok || (msgId < 0)) {
		return;
	}

	QStringList fileList;
	QString emailMessage;
	QString boundary("-----123456789asdfghj_" +
	QDateTime::currentDateTimeUtc().toString(
	    "dd.MM.yyyy-HH:mm:ss.zzz"));

	createEmailMessage(emailMessage, body, subject,  boundary);
	deleteFilesFromDir(EMAIL_DIR_NAME);

	QString filePath;
	QByteArray data;

	for (int row = 0; row < attachModel->rowCount(); ++row) {
		QModelIndex idx(attachModel->index(row));
		/*
		 * On Android the attachment must be saved and the explicitly
		 * add into the email message.
		 */
		QByteArray base64Data(attachModel->data(idx,
		    FileListModel::ROLE_FILE_DATA).toByteArray());
		QByteArray rawData(QByteArray::fromBase64(base64Data));
		QString attachName(attachModel->data(idx,
		    FileListModel::ROLE_FILE_NAME).toString());
		QString filePath(
		    writeFileToEmailDir(attachName, msgId, rawData));
		fileList.append(filePath);
		addAttachmentToEmailMessage(emailMessage, attachName,
		    base64Data, boundary);
	}

	finishEmailMessage(emailMessage, boundary);

	sendEmail(emailMessage, fileList, subject, body, msgId);
}

611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
/*!
 * @brief Obtain path for storing attachments.
 *
 * @return Path to location where attachment can be stored.
 */
static
QString obtainStoragePath(void)
{
	QString path(getBasePathBasedOnPlatform());

#if defined Q_OS_IOS
	/* There is only one location where files or data can store
	 * permanently and share with Desktop.
	 * See: https://developer.apple.com/library/content/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html
	 */

	path = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
#else
	/* Another platform can select destination for attachment saving. */
	path = QFileDialog::getExistingDirectory(Q_NULLPTR,
	    QObject::tr("Select directory"), path,
	    QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
#endif

	return path;
}

/*!
 * @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)
{
	QFileInfo fi(destPath);

	QMessageBox msgBox;
	msgBox.setWindowTitle(QObject::tr("Attachment saving"));
	msgBox.setStandardButtons(QMessageBox::Ok);
	msgBox.setDefaultButton(QMessageBox::Ok);

	if (!destPath.isEmpty()) {
		msgBox.setIcon(QMessageBox::Information);
		msgBox.setText(QObject::tr("Attachments have been saved."));
	} else {
		msgBox.setIcon(QMessageBox::Critical);
		msgBox.setText(QObject::tr("Attachments have not been saved!"));
	}
	msgBox.setInformativeText(
	    QObject::tr("Path: '%1'").arg(fi.absolutePath()));
	msgBox.exec();
}

void Files::saveAttachmentsToDiskDb(const QString &userName,
    const QString &msgIdStr)
{
	qDebug("%s()", __func__);

	/* User name must be supplied. */
	if (userName.isEmpty()) {
		Q_ASSERT(0);
		return;
	}

	/* Obtain message identifier. */
	bool ok = false;
	qint64 msgId = msgIdStr.toLongLong(&ok);
	if (!ok || (msgId < 0)) {
		return;
	}

	QList<FileDb::FileData> filelist;
	{
		FileDb *fDb = Q_NULLPTR;
		fDb = globFileDbsPtr->accessFileDb(globSet.dbsLocation, userName,
		    AccountListModel::globAccounts[userName].storeToDisk());

		if (fDb == Q_NULLPTR) {
			qCritical() << "Cannot open file database!";
			return;
		}

		filelist = fDb->getFilesFromDb(msgId);
		if (filelist.isEmpty()) {
			qCritical() << "File list is empty!";
			return;
		}

	}

	QString path(obtainStoragePath());

	QString destPath;
	foreach (const FileDb::FileData &file, filelist) {
		QByteArray data = QByteArray::fromBase64(file.content.toUtf8());
		destPath = writeFileToDir(path, file.filename, msgId, data);
	}

	attachmentSavingNotification(destPath);
}

void Files::saveAttachmentsToDiskZfo(const QVariant &attachModelVariant,
    const QString &msgIdStr)
{
	qDebug("%s()", __func__);

	/* Obtain pointer to attachment model. */
	const FileListModel *attachModel =
	    FileListModel::modelFromVariant(attachModelVariant);
	if (attachModel == Q_NULLPTR) {
		Q_ASSERT(0);
		return;
	}

	/* Obtain message identifier. */
	bool ok = false;
	qint64 msgId = msgIdStr.toLongLong(&ok);
	if (!ok || (msgId < 0)) {
		return;
	}

	QString path(obtainStoragePath());

	QString destPath;
	for (int row = 0; row < attachModel->rowCount(); ++row) {
		QModelIndex idx(attachModel->index(row));
		QByteArray data(QByteArray::fromBase64(attachModel->data(
		    idx, FileListModel::ROLE_FILE_DATA).toByteArray()));
		destPath = writeFileToDir(path,
		    attachModel->data(idx,
		        FileListModel::ROLE_FILE_NAME).toString(),
		    msgId, data);
	}

	attachmentSavingNotification(destPath);
}

750 751 752 753 754 755 756 757 758 759
void Files::deleteTmpFileFromStorage(const QString &filePath)
{
#if defined Q_OS_IOS
	QFile file(filePath);
	file.remove();
#else
	Q_UNUSED(filePath);
#endif /* defined Q_OS_IOS */
}

760 761 762 763 764
void Files::sendEmail(const QString &emailMessage, const QStringList &fileList,
    const QString &subject, const QString &body, qint64 msgId)
{
	Q_UNUSED(subject);
	Q_UNUSED(body);
765 766
	Q_UNUSED(emailMessage);
	Q_UNUSED(msgId);
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790

	if (!fileList.isEmpty()) {

#if defined Q_OS_IOS
		UrlOpener urlOpener;
		urlOpener.createEmail(body, subject, fileList);
#elif defined Q_OS_ANDROID
		QDesktopServices::openUrl(QUrl("mailto:?subject=" +
		    subject + "&body=" + body));
#elif defined Q_OS_WINRT
		QDesktopServices::openUrl(QUrl("mailto:?subject=" +
		    subject + "&body=" + body));
#else
		QString tmpEmailFile = writeFileToEmailDir(QString::number(msgId)
		    + "_mail.eml", msgId, emailMessage.toUtf8());
		QDesktopServices::openUrl(QUrl::fromLocalFile(tmpEmailFile));
#endif
		/* TODO -- Handle openUrl() return value. */

	} else {
		qDebug() << "Tmp file save error";
	}
}

791
QByteArray Files::getXmlFromCms(const QByteArray &rawData)
792 793 794
{
	qDebug("%s()", __func__);

795 796 797
	if (rawData.isEmpty()) {
		Q_ASSERT(0);
		qCritical() << "File content is empty!";
798 799 800
		return QByteArray();
	}

801
	/* Decode CMS and obtain message XML data - uses OpenSSL. */
802 803
	void *xmlContent = NULL;
	size_t xmlContentLen = 0;
804
	if (extract_cms_data(rawData.data(), rawData.length(), &xmlContent,
Martin Straka's avatar
Martin Straka committed
805
	        &xmlContentLen) != 0) {
806 807 808 809 810 811 812
		return QByteArray();
	}
	if (xmlContentLen == 0) {
		free(xmlContent); xmlContent = NULL;
		return QByteArray();
	}

813
	QByteArray soap((char *)xmlContent, xmlContentLen);
814 815 816 817 818
	free(xmlContent); xmlContent = NULL;

	return soap;
}

819
QByteArray Files::decodeZfoFile(const QByteArray &base64ZfoData)
Martin Straka's avatar
Martin Straka committed
820 821 822
{
	qDebug("%s()", __func__);

823 824 825
	if (base64ZfoData.isEmpty()) {
		Q_ASSERT(0);
		qCritical() << "File content is empty.";
Martin Straka's avatar
Martin Straka committed
826 827 828
		return QByteArray();
	}

829 830
	/* decode signature from base64 and obtain something CMS message */
	return getXmlFromCms(QByteArray::fromBase64(base64ZfoData));
Martin Straka's avatar
Martin Straka committed
831 832
}

833 834 835
bool Files::parseXmlData(bool *deliveryInfo, QString *idStr,
    QString *annotation, QString *msgDescrHtml, FileListModel *attachModel,
    QString *emailBody, QByteArray xmlData)
836 837 838
{
	qDebug("%s()", __func__);

839 840 841
	if (xmlData.isEmpty()) {
		Q_ASSERT(0);
		qCritical() << "XML content is empty!";
Martin Straka's avatar
Martin Straka committed
842
		return false;
843 844
	}

845 846
	//qDebug() << xmlData;

847
	xmlData.prepend("<?xml version='1.0' encoding='utf-8'?>"
848 849 850 851
	      "<SOAP-ENV:Envelope "
	      "xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" "
	      "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"
	      "<SOAP-ENV:Body>");
852
	xmlData.append("</SOAP-ENV:Body></SOAP-ENV:Envelope>");
853

Martin Straka's avatar
Martin Straka committed
854
	/* Test if zfo is message, delivery info or unknow format */
855
	if (xmlData.contains(QByteArray("MessageDownloadResponse"))) {
856 857 858
		if (deliveryInfo != Q_NULLPTR) {
			*deliveryInfo = false;
		}
Martin Straka's avatar
Martin Straka committed
859 860
		return parseAndShowXmlData(false, idStr, annotation,
		    msgDescrHtml, attachModel, emailBody, xmlData);
861
	} else if (xmlData.contains(QByteArray("GetDeliveryInfoResponse"))) {
862 863 864
		if (deliveryInfo != Q_NULLPTR) {
			*deliveryInfo = true;
		}
Martin Straka's avatar
Martin Straka committed
865 866
		return parseAndShowXmlData(true, idStr, annotation,
		    msgDescrHtml, attachModel, emailBody, xmlData);
867
	} else {
868
		qCritical() << "Unknow ZFO format";
869 870 871
		Q_ASSERT(0);
	}

Martin Straka's avatar
Martin Straka committed
872
	return false;
873 874
}

Martin Straka's avatar
Martin Straka committed
875 876 877
bool Files::parseAndShowXmlData(bool isDeliveryInfo, QString *idStr,
    QString *annotation, QString *msgDescrHtml, FileListModel *attachModel,
    QString *emailBody, QByteArray &xmlData)
878 879 880
{
	qDebug("%s()", __func__);

881 882 883 884
	QXmlStreamReader xml;
	XmlLayer xmlLayer;
	Messages::Message msg;
	QList<Files::File> fileList;
Martin Straka's avatar
Martin Straka committed
885
	QList<Messages::Event> eventList;
886

Martin Straka's avatar
Martin Straka committed
887 888
	/* parse message envelope and files */
	xml.addData(xmlData);
889 890 891
	while(!xml.atEnd() && !xml.hasError()) {
		QXmlStreamReader::TokenType token = xml.readNext();
		if (xml.error() != QXmlStreamReader::NoError) {
Martin Straka's avatar
Martin Straka committed
892
			return false;
893 894 895 896 897 898
		}
		if (token == QXmlStreamReader::StartDocument) {
			continue;
		}
		if (token == QXmlStreamReader::StartElement) {
			if (xml.name() == "dmDm") {
Martin Straka's avatar
Martin Straka committed
899
				xmlLayer.completeMessageParse(xml, msg, fileList);
900 901 902 903
			}
		}
	}

Martin Straka's avatar
Martin Straka committed
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
	if (isDeliveryInfo) {
		/* parse delivery info */
		xml.clear();
		xml.addData(xmlData);
		while(!xml.atEnd() && !xml.hasError()){
			QXmlStreamReader::TokenType token = xml.readNext();
			if (token == QXmlStreamReader::StartDocument) {
				continue;
			}
			if (token == QXmlStreamReader::StartElement) {
				if (xml.name() == "dmEvent") {
					eventList.append(xmlLayer.parseEvent(xml));
				}
			}
		}
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
	}

	QString html = divStart;

	html += "<h3>" + QObject::tr("General") + "</h3>";
	html += strongInfoLine(QObject::tr("Subject"), msg.dmAnnotation);
	QString size = QString::number(msg.dmAttachmentSize);
	html += strongInfoLine(QObject::tr("Attachment size"),
	    (size == "0") ? "&lt;1 KB" : "~" + size + " KB");
	html += strongInfoLine(QObject::tr("Personal delivery"),
	    (msg.dmPersonalDelivery) ? QObject::tr("Yes") : QObject::tr("No"));
	html += strongInfoLine(QObject::tr("Delivery by fiction"),
	    (msg.dmAllowSubstDelivery) ? QObject::tr("Yes") : QObject::tr("No"));

	html += "<h3>" + QObject::tr("Sender") + "</h3>";
	html += strongInfoLine(QObject::tr("Databox ID"), msg.dbIDSender);
	html += strongInfoLine(QObject::tr("Name"), msg.dmSender);
	html += strongInfoLine(QObject::tr("Address"),msg.dmSenderAddress);

	html += "<h3>" + QObject::tr("Recipient") + "</h3>";
	html += strongInfoLine(QObject::tr("Databox ID"), msg.dbIDRecipient);
	html += strongInfoLine(QObject::tr("Name"), msg.dmRecipient);
	html += strongInfoLine(QObject::tr("Address"),msg.dmRecipientAddress);
942 943 944 945
	if (!msg.dmToHands.isEmpty()) {
		html += strongInfoLine(QObject::tr("To hands"),
		    msg.dmToHands);
	}
946

947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
	QString tmpHtml;
	if (!msg.dmSenderIdent.isEmpty()) {
		tmpHtml += strongInfoLine(QObject::tr("Our file mark"),
		    msg.dmSenderIdent);
	}
	if (!msg.dmSenderRefNumber.isEmpty()) {
		tmpHtml += strongInfoLine(QObject::tr("Our reference number"),
		    msg.dmSenderRefNumber);
	}
	if (!msg.dmRecipientIdent.isEmpty()) {
		tmpHtml += strongInfoLine(QObject::tr("Your file mark"),
		    msg.dmRecipientIdent);
	}
	if (!msg.dmRecipientRefNumber.isEmpty()) {
		tmpHtml += strongInfoLine(QObject::tr("Your reference number"),
		    msg.dmRecipientRefNumber);
	}
	if (!msg.dmLegalTitleLaw.isEmpty()) {
		tmpHtml += strongInfoLine(QObject::tr("Law"),
		    msg.dmLegalTitleLaw);
	}
	if (!msg.dmLegalTitleYear.isEmpty()) {
		tmpHtml += strongInfoLine(QObject::tr("Year"),
		    msg.dmLegalTitleYear);
	}
	if (!msg.dmLegalTitleSect.isEmpty()) {
		tmpHtml += strongInfoLine(QObject::tr("Section"),
		    msg.dmLegalTitleSect);
	}
	if (!msg.dmLegalTitlePar.isEmpty()) {
		tmpHtml += strongInfoLine(QObject::tr("Paragraph"),
		    msg.dmLegalTitlePar);
	}
	if (!msg.dmLegalTitlePoint.isEmpty()) {
		tmpHtml += strongInfoLine(QObject::tr("Letter"),
		    msg.dmLegalTitlePoint);
	}
	if (!tmpHtml.isEmpty()) {
		html += "<h3>" + QObject::tr("Additional info") + "</h3>";
		html += tmpHtml;
	}
988 989 990 991 992 993 994 995 996 997 998 999 1000

	html += "<h3>" + QObject::tr("Message state") + "</h3>";
	html += strongInfoLine(QObject::tr("Delivery time"),
	    dateTimeStrFromDbFormat(
	        dateTimeStrToUTCDbFormat(msg.dmDeliveryTime),
	        DATETIME_QML_FORMAT));
	html += strongInfoLine(QObject::tr("Accetance time"),
	    dateTimeStrFromDbFormat(
	        dateTimeStrToUTCDbFormat(msg.dmAcceptanceTime),
	        DATETIME_QML_FORMAT));
	html += strongInfoLine(QObject::tr("Status"),
	    QString::number(msg.dmMessageStatus));

Martin Straka's avatar
Martin Straka committed
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
	if (isDeliveryInfo) {
		html += "<h3>" + QObject::tr("Events") + "</h3>";
		foreach (const Messages::Event &event, eventList) {
			html += divStart +
			    strongInfoLine(dateTimeStrFromDbFormat(
			        dateTimeStrToUTCDbFormat(event.dmEventTime),
			        DATETIME_QML_FORMAT), event.dmEventDescr)
			    + divEnd;
		}
	}

1012 1013
	html += divEnd;

1014 1015 1016 1017 1018 1019 1020 1021
	// Create body for email
	QString body = QObject::tr("ID") + ": ";
		body += QString::number(msg.dmID) + "\n";
		body += QObject::tr("FROM") + ": ";
		body += msg.dmSender + "\n";
		body += QObject::tr("TO") + ": ";
		body += msg.dmRecipient  + "\n";
		body += QObject::tr("DELIVERED") + ": ";
Martin Straka's avatar
Martin Straka committed
1022 1023 1024 1025
		body += dateTimeStrFromDbFormat(
		    dateTimeStrToUTCDbFormat(msg.dmAcceptanceTime),
		    DATETIME_QML_FORMAT)
		    + "\n\n---\n";
1026 1027 1028 1029 1030
		body += QObject::tr("This email has been generated with Datovka "
		    "application based on a data message (%1) delivered "
		    "to databox.").arg(msg.dmID);
		body += "\n";

1031 1032 1033
	if (idStr != Q_NULLPTR) {
		*idStr = QString::number(msg.dmID);
	}
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
	if (annotation != Q_NULLPTR) {
		*annotation = msg.dmAnnotation;
	}
	if (msgDescrHtml != Q_NULLPTR) {
		*msgDescrHtml = html;
	}
	if (attachModel != Q_NULLPTR) {
		setZfoFilesToModel(*attachModel, fileList);
	}
	if (emailBody != Q_NULLPTR) {
		*emailBody = body;
	}
1046

1047 1048
	return true;
}