Commit b46e2b5f authored by Martin Straka's avatar Martin Straka

Merge branch 'send-zfo-email' into 'develop'

Send zfo message via email

See merge request !111
parents e01b6156 14fa24bb
......@@ -24,6 +24,7 @@
import QtQuick 2.7
import QtQuick.Controls 2.1
import cz.nic.mobileDatovka 1.0
import cz.nic.mobileDatovka.files 1.0
import cz.nic.mobileDatovka.messages 1.0
import cz.nic.mobileDatovka.models 1.0
......@@ -119,8 +120,12 @@ Component {
"messageModel": messageModel
}, StackView.Immediate)
},
"sendEmail": function callSendEmail() {
files.sendAttachmentsWithEmail(userName, msgId)
"sendAttachEmail": function callSendAttachEmail() {
files.sendMsgFilesWithEmail(userName, msgId, EmailAttachFlag.MSG_ATTACHS)
pageView.pop(StackView.Immediate)
},
"sendZfoEmail": function callSendZfoEmail() {
files.sendMsgFilesWithEmail(userName, msgId, EmailAttachFlag.MSG_ZFO)
pageView.pop(StackView.Immediate)
},
"saveAttachs": function callSaveAttachs() {
......@@ -174,12 +179,19 @@ Component {
name: qsTr("Upload to records management")
funcName: "uploadRM"
}
ListElement {
image: "qrc:/ui/attach-to-mail.svg"
showEntry: true
showNext: false
name: qsTr("Send message zfo by email")
funcName: "sendZfoEmail"
}
ListElement {
image: "qrc:/ui/attach-to-mail.svg"
showEntry: true
showNext: false
name: qsTr("Send attachments by email")
funcName: "sendEmail"
funcName: "sendAttachEmail"
}
ListElement {
image: "qrc:/ui/save-to-disk.svg"
......
......@@ -43,11 +43,14 @@
#include "src/sqlite/dbs.h"
#include "src/sqlite/file_db_container.h"
#include "src/sqlite/message_db_container.h"
#include "src/sqlite/zfo_db.h"
void Files::declareQML(void)
{
qmlRegisterType<Files>("cz.nic.mobileDatovka.files", 1, 0, "FileIdType");
qmlRegisterType<Files>("cz.nic.mobileDatovka.files", 1, 0, "EmailAttachFlag");
qRegisterMetaType<Files::FileIdType>();
qRegisterMetaType<Files::EmailAttachFlag>();
}
Files::Files(QObject *parent)
......@@ -236,13 +239,15 @@ void Files::openAttachmentFromPath(const QString &filePath)
#endif
}
void Files::sendAttachmentsWithEmail(const QString &userName, qint64 msgId)
void Files::sendMsgFilesWithEmail(const QString &userName, qint64 msgId,
EmailAttachFlags attachFlags)
{
qDebug("%s()", __func__);
if (Q_UNLIKELY((GlobInstcs::setPtr == Q_NULLPTR) ||
(GlobInstcs::messageDbsPtr == Q_NULLPTR) ||
(GlobInstcs::fileDbsPtr == Q_NULLPTR) ||
(GlobInstcs::zfoDbPtr == Q_NULLPTR) ||
(GlobInstcs::acntMapPtr == Q_NULLPTR))) {
Q_ASSERT(0);
return;
......@@ -254,57 +259,87 @@ void Files::sendAttachmentsWithEmail(const QString &userName, qint64 msgId)
QString body;
QString subject;
QStringList fileList;
/* Fill email subject and email body */
MessageDb *msgDb = GlobInstcs::messageDbsPtr->accessMessageDb(
GlobInstcs::setPtr->dbsLocation, userName,
(*GlobInstcs::acntMapPtr)[userName].storeToDisk());
if (msgDb == Q_NULLPTR) {
qCritical() << "ERROR: Message database cannot open!";
qCritical("Cannot open message database for '%s'.",
userName.toUtf8().constData());
return;
}
if (!msgDb->getMessageDataForEmail(msgId, body, subject)) {
qCritical() << "ERROR: Message data mssing!";
return;
}
FileDb *fDb = GlobInstcs::fileDbsPtr->accessFileDb(
GlobInstcs::setPtr->dbsLocation, userName,
(*GlobInstcs::acntMapPtr)[userName].storeToDisk());
if (fDb == Q_NULLPTR) {
qCritical() << "ERROR: File database cannot open!";
qCritical("Missing data of message '%s'.",
QString::number(msgId).toUtf8().constData());
return;
}
QList<FileDb::FileData> filelist;
filelist = fDb->getFilesFromDb(msgId);
if (filelist.isEmpty()) {
qCritical() << "ERROR: File list is empty!";
return;
/* Get attachment files from database if needed */
if (attachFlags & MSG_ATTACHS) {
FileDb *fDb = GlobInstcs::fileDbsPtr->accessFileDb(
GlobInstcs::setPtr->dbsLocation, userName,
(*GlobInstcs::acntMapPtr)[userName].storeToDisk());
if (fDb == Q_NULLPTR) {
qCritical("Cannot open file database for '%s'.",
userName.toUtf8().constData());
return;
}
filelist = fDb->getFilesFromDb(msgId);
if (filelist.isEmpty()) {
qCritical("Missing attachments for message '%s'.",
QString::number(msgId).toUtf8().constData());
return;
}
}
const QString boundary = generateBoundaryString();
QString emailMessage = createEmailMessage(body, subject, boundary);
/* Get zfo file from database if needed */
if (attachFlags & MSG_ZFO) {
FileDb::FileData msgData;
msgData.content = GlobInstcs::zfoDbPtr->getZfoContentFromDb(
msgId, (*GlobInstcs::acntMapPtr)[userName].isTestAccount());
if (msgData.content.isEmpty()) {
qCritical("Missing zfo data for message '%s'.",
QString::number(msgId).toUtf8().constData());
Dialogues::errorMessage(Dialogues::CRITICAL,
tr("ZFO missing"),
tr("ZFO message is not present in local database."),
tr("Download complete message again to obtain it."));
Q_ASSERT(0);
return;
}
msgData.filename = QString("DZ_%1.zfo").arg(msgId);
filelist.append(msgData);
}
/* Create email content, email attachment path, email eml content */
removeDirFromDocLoc(DATOVKA_MAIL_DIR_NAME);
QString targetPath(appEmailDirPath(QString::number(msgId)));
const QString boundary = generateBoundaryString();
QString emailMessage = createEmailMessage(body, subject, boundary);
QStringList filePathList;
/* Write attachment files to email directory */
foreach (const FileDb::FileData &file, filelist) {
QString fileName = file.filename;
if (fileName.isEmpty()) {
qCritical() << "ERROR: File name is empty!";
qCritical("%s", "File name is empty.");
return;
}
fileName = writeFile(targetPath, fileName,
base64ToRaw(file.content.toUtf8()));
fileList.append(fileName);
filePathList.append(fileName);
addAttachmentToEmailMessage(emailMessage, file.filename,
file.content.toUtf8(), boundary);
}
finishEmailMessage(emailMessage, boundary);
sendEmail(emailMessage, fileList, subject, body, msgId);
/* Send email */
sendEmail(emailMessage, filePathList, subject, body, msgId);
}
void Files::deleteFileDb(const QString &userName)
......
......@@ -38,6 +38,20 @@ class Files : public QObject {
public:
/* Defines attachment type to be sent via email */
enum EmailAttachFlag {
NO_FILES = 0x00,
MSG_ZFO = 0x01, /* Complete message in zfo format. */
MSG_ATTACHS = 0x02 /* Attachments of the data message. */
};
Q_ENUM(EmailAttachFlag)
/*
* Flags inside QML:
* https://forum.qt.io/topic/10060/q_enums-q_declare_metatype-and-qml/2
*/
Q_DECLARE_FLAGS(EmailAttachFlags, EmailAttachFlag)
Q_FLAG(EmailAttachFlags)
/* Defines missing file id vaule and zfo id */
enum FileIdType {
NO_FILE_ID = -1,
......@@ -130,14 +144,16 @@ public:
void openAttachmentFromPath(const QString &filePath);
/*!
* @brief Send attachments from database with email application.
* @brief Send message attachments or complete zfo message
* from database with email application.
*
* @param[in] userName User name of account.
* @param[in] msgId Message id.
* @param[in] attachFlags Specifies which attachments to send.
*/
Q_INVOKABLE
void sendAttachmentsWithEmail(const QString &userName,
qint64 msgId);
void sendMsgFilesWithEmail(const QString &userName,
qint64 msgId, EmailAttachFlags attachFlags);
/*!
* @brief Delete file database.
......@@ -357,7 +373,11 @@ private:
QByteArray &xmlData);
};
Q_DECLARE_OPERATORS_FOR_FLAGS(Files::EmailAttachFlags)
/* Declare FileIdType to QML. */
Q_DECLARE_METATYPE(Files::FileIdType)
Q_DECLARE_METATYPE(Files::EmailAttachFlag)
Q_DECLARE_METATYPE(Files::EmailAttachFlags)
#endif // FILES_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment