Commit 4b57255a authored by Karel Slaný's avatar Karel Slaný

Passing message content into ZFO message detail page.

parent 314f9f0f
......@@ -288,12 +288,10 @@ ApplicationWindow {
* enabled.
*/
console.log("Showing zfo file: " + filePath)
var fileContent = files.rawFileContent(filePath)
mainStack.push(pageZfoMessageDetail, {
"pageView": mainStack,
"zfoFilePath": filePath,
"msgAnnotation": files.msgAnnotation(filePath),
"msgDescrHtml": files.msgDescriptionHtml(filePath),
"emailBody": files.msgEmailBody(filePath)
"rawZfoContent": fileContent
}, StackView.Immediate)
}
}
......
......@@ -28,27 +28,35 @@ import QtGraphicalEffects 1.0
import cz.nic.mobileDatovka.models 1.0
Component {
id: pageZfoMessageDetail
id: componentZfoMessageDetail
Item {
id: msgZfoDetailPage
/* These properties must be set by caller. */
property var pageView
property var statusBar
property var zfoFilePath
property var rawZfoContent /* Raw ZFO file content. */
/* These properties are set from within the page. */
property var msgAnnotation
property var msgDescrHtml /* Message HTML description. */
property string emailBody
property string zfoId
onRawZfoContentChanged: {
console.log("File path changed.")
msgAnnotation = files.msgAnnotation(rawZfoContent),
msgDescrHtml = files.msgDescriptionHtml(rawZfoContent),
emailBody = files.msgEmailBody(rawZfoContent)
attachmentModel.setZfo(rawZfoContent)
}
FileListModel {
id: attachmentModel
Component.onCompleted: {
/* Load content from ZFO file if such is supplied. */
if (typeof(zfoFilePath) != "undefined") {
setZfoFile(zfoFilePath) /* Model method. */
}
}
}
......@@ -258,7 +266,17 @@ Component {
anchors.fill: parent
onClicked: {
locker.ignoreNextSuspension()
files.openAttachment(rFileName, rFileContent)
if (files.isZfoFile(rFileName)) {
console.log("Attachment is ZFO: " + rFileName)
var fileContent = files.base64ToRaw(rFileContent)
pageView.push(pageZfoMessageDetail, {
"pageView": pageView,
"statusBar": statusBar,
"rawZfoContent": fileContent
}, StackView.Immediate)
} else {
files.openAttachment(rFileName, rFileContent)
}
}
}
Rectangle {
......
......@@ -232,37 +232,38 @@ void Files::openAttachmentFromDb(const QString &userName,
return;
}
if (isAttachmentZfoFile(fileName)) {
if (isZfoFile(fileName)) {
parseXmlData(Q_NULLPTR, Q_NULLPTR, &globFilesModelZfo,
Q_NULLPTR, decodeZfoFile(file.content));
Q_NULLPTR, decodeZfoFile(file.content.toUtf8()));
return;
}
openAttachment(fileName, file.content);
openAttachment(fileName, file.content.toUtf8());
}
void Files::openAttachment(const QString &fileName, const QString &content)
void Files::openAttachment(const QString &fileName, const QByteArray &base64Data)
{
Q_ASSERT(!fileName.isEmpty());
Q_ASSERT(!content.isEmpty());
Q_ASSERT(!base64Data.isEmpty());
if (fileName.isEmpty() || content.isEmpty()) {
qDebug() << "ERROR: File name or its content is empty!";
if (fileName.isEmpty() || base64Data.isEmpty()) {
qCritical() << "File name or its content is empty!";
return;
}
if (isAttachmentZfoFile(fileName)) {
parseXmlData(Q_NULLPTR, Q_NULLPTR, &globFilesModelZfo,
Q_NULLPTR, decodeZfoFile(content));
if (isZfoFile(fileName)) {
/* Don't open zfo files from here. */
Q_ASSERT(0);
qCritical() << "This should open ZFO files by itself.";
return;
}
QByteArray data = QByteArray::fromBase64(content.toUtf8());
QString filePath = writeFileToTmpDir(fileName, TEMP_DIR_NAME, data);
QByteArray rawData(QByteArray::fromBase64(base64Data));
QString filePath(writeFileToTmpDir(fileName, TEMP_DIR_NAME, rawData));
if (!filePath.isEmpty()) {
qDebug() << filePath;
qInfo() << "Creating temporary file" << filePath;
#if defined Q_OS_IOS
UrlOpener urlOpener;
......@@ -284,7 +285,7 @@ void Files::openAttachment(const QString &fileName, const QString &content)
}
#endif /* defined Q_OS_IOS */
} else {
qDebug() << "Tmp file save error";
qCritical() << "Cannot create temporary file for" << fileName;
QMessageBox msgBox;
msgBox.setIcon(QMessageBox::Critical);
msgBox.setWindowTitle(tr("Open attachment error"));
......@@ -597,28 +598,20 @@ void Files::sendEmail(const QString &emailMessage, const QStringList &fileList,
}
}
bool Files::isAttachmentZfoFile(const QString &fileName)
{
qDebug("%s()", __func__);
QFileInfo fi(fileName);
return (fi.suffix().toLower() == "zfo");
}
QByteArray Files::decodeCmsData(const QByteArray &cmsData)
QByteArray Files::getXmlFromCms(const QByteArray &rawData)
{
qDebug("%s()", __func__);
Q_ASSERT(!cmsData.isEmpty());
if (cmsData.isEmpty()) {
qDebug() << "ERROR: File content is empty!";
if (rawData.isEmpty()) {
Q_ASSERT(0);
qCritical() << "File content is empty!";
return QByteArray();
}
/* decode cms and obtain message xml data - used openssl */
/* Decode CMS and obtain message XML data - uses OpenSSL. */
void *xmlContent = NULL;
size_t xmlContentLen = 0;
if (extract_cms_data(cmsData.data(), cmsData.length(), &xmlContent,
if (extract_cms_data(rawData.data(), rawData.length(), &xmlContent,
&xmlContentLen) != 0) {
return QByteArray();
}
......@@ -627,26 +620,24 @@ QByteArray Files::decodeCmsData(const QByteArray &cmsData)
return QByteArray();
}
QByteArray soap((char*)xmlContent, xmlContentLen);
QByteArray soap((char *)xmlContent, xmlContentLen);
free(xmlContent); xmlContent = NULL;
return soap;
}
QByteArray Files::decodeZfoFile(const QString &content)
QByteArray Files::decodeZfoFile(const QByteArray &base64ZfoData)
{
qDebug("%s()", __func__);
Q_ASSERT(!content.isEmpty());
if (content.isEmpty()) {
qDebug() << "ERROR: File content is empty!";
if (base64ZfoData.isEmpty()) {
Q_ASSERT(0);
qCritical() << "File content is empty.";
return QByteArray();
}
/* decode signature from base64 and obtain something cms message */
QByteArray cmsData = QByteArray::fromBase64(content.toUtf8());
return decodeCmsData(cmsData);
/* decode signature from base64 and obtain something CMS message */
return getXmlFromCms(QByteArray::fromBase64(base64ZfoData));
}
bool Files::parseXmlData(QString *annotation, QString *msgDescrHtml,
......@@ -835,70 +826,64 @@ bool Files::fileReadable(const QString &filePath)
return true;
}
QString Files::msgAnnotation(const QString &filePath)
bool Files::isZfoFile(const QString &fileName)
{
return QFileInfo(fileName).suffix().toLower() == QStringLiteral("zfo");
}
QByteArray Files::rawFileContent(const QString &filePath)
{
if (!fileReadable(filePath)) {
return QString();
return QByteArray();
}
QFile file(filePath);
file.open(QIODevice::ReadOnly);
if (!file.open(QIODevice::ReadOnly)) {
Q_ASSERT(0);
qCritical() << "Cannot open file" << filePath;
return QByteArray();
}
QString annotation;
bool ret = parseXmlData(&annotation, Q_NULLPTR, Q_NULLPTR, Q_NULLPTR,
decodeCmsData(file.readAll()));
QByteArray rawData(file.readAll());
file.close();
return rawData;
}
return ret ? annotation : QString();
QByteArray Files::base64ToRaw(const QByteArray &base64Data)
{
return QByteArray::fromBase64(base64Data);
}
QString Files::msgDescriptionHtml(const QString &filePath)
QString Files::msgAnnotation(const QByteArray &rawZfoData)
{
if (!fileReadable(filePath)) {
return QString();
}
QString annotation;
bool ret = parseXmlData(&annotation, Q_NULLPTR, Q_NULLPTR, Q_NULLPTR,
getXmlFromCms(rawZfoData));
QFile file(filePath);
file.open(QIODevice::ReadOnly);
return ret ? annotation : QString();
}
QString Files::msgDescriptionHtml(const QByteArray &rawZfoData)
{
QString htmlDescr;
bool ret = parseXmlData(Q_NULLPTR, &htmlDescr, Q_NULLPTR, Q_NULLPTR,
decodeCmsData(file.readAll()));
file.close();
getXmlFromCms(rawZfoData));
return ret ? htmlDescr : QString();
}
QString Files::msgEmailBody(const QString &filePath)
QString Files::msgEmailBody(const QByteArray &rawZfoData)
{
if (!fileReadable(filePath)) {
return QString();
}
QFile file(filePath);
file.open(QIODevice::ReadOnly);
QString emailBody;
bool ret = parseXmlData(Q_NULLPTR, Q_NULLPTR, Q_NULLPTR, &emailBody,
decodeCmsData(file.readAll()));
file.close();
getXmlFromCms(rawZfoData));
return ret ? emailBody : QString();
}
bool Files::openZfoFileFromStorage(FileListModel &attachModel,
const QString &filePath)
bool Files::setAttachmentModel(FileListModel &attachModel,
const QByteArray &rawZfoData)
{
if (!fileReadable(filePath)) {
return false;
}
QFile file(filePath);
file.open(QIODevice::ReadOnly);
bool ret = parseXmlData(Q_NULLPTR, Q_NULLPTR, &attachModel, Q_NULLPTR,
decodeCmsData(file.readAll()));
file.close();
return ret;
return parseXmlData(Q_NULLPTR, Q_NULLPTR, &attachModel, Q_NULLPTR,
getXmlFromCms(rawZfoData));
}
......@@ -58,7 +58,7 @@ public:
* @brief Open attachment in default application.
*/
Q_INVOKABLE void openAttachment(const QString &fileName,
const QString &content);
const QByteArray &base64Data);
/*!
* @brief Send attachments from database with email application.
......@@ -127,6 +127,30 @@ public:
static
bool fileReadable(const QString &filePath);
/*!
* @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.
*/
Q_INVOKABLE static
QByteArray base64ToRaw(const QByteArray &base64Data);
/*!
* @brief Parses content of ZFO file.
*
......@@ -134,7 +158,7 @@ public:
* @return Annotation on success, empty string else.
*/
Q_INVOKABLE static
QString msgAnnotation(const QString &filePath);
QString msgAnnotation(const QByteArray &rawZfoData);
/*!
* @brief Parses content of ZFO file.
......@@ -143,7 +167,7 @@ public:
* @return Message description on success, empty string else.
*/
Q_INVOKABLE static
QString msgDescriptionHtml(const QString &filePath);
QString msgDescriptionHtml(const QByteArray &rawZfoData);
/*!
* @brief Parses content of ZFO file.
......@@ -152,18 +176,18 @@ public:
* @return Email containing message on success, empty string else.
*/
Q_INVOKABLE static
QString msgEmailBody(const QString &filePath);
QString msgEmailBody(const QByteArray &rawZfoData);
/*!
* @brief Open ZFO file from path.
* @brief Sets attachment model.
*
* @param[out] attachModel Attachment model to be set.
* @param[in] filePath File path with file name.
* @return true if success.
*/
static
bool openZfoFileFromStorage(FileListModel &attachModel,
const QString &filePath);
bool setAttachmentModel(FileListModel &attachModel,
const QByteArray &rawZfoData);
class File {
public:
......@@ -228,32 +252,23 @@ private:
void sendEmail(const QString &emailMessage, const QStringList &fileList,
const QString &subject, const QString &body, qint64 msgId);
/*!
* @brief Test, if attachment is ZFO file.
*
* @param[in] fileName File name.
* @return true if file is zfo.
*/
static
bool isAttachmentZfoFile(const QString &fileName);
/*!
* @brief Decode XML data from CMS.
*
* @param[in] content File content.
* @return decoded data or QByteArray().
* @param[in] rawData File content.
* @return Decoded XML data or QByteArray().
*/
static
QByteArray decodeCmsData(const QByteArray &cmsData);
QByteArray getXmlFromCms(const QByteArray &rawData);
/*!
* @brief Decode data from ZFO file.
*
* @param[in] content File content.
* @return decoded data or QByteArray().
* @param[in] base64ZfoData Base64-encoded zfo file content.
* @return Decoded raw data or QByteArray().
*/
static
QByteArray decodeZfoFile(const QString &content);
QByteArray decodeZfoFile(const QByteArray &base64ZfoData);
/*!
* @brief Parse xml data of zfo file.
......
......@@ -202,7 +202,7 @@ void FileListModel::clearAll(void)
endResetModel();
}
bool FileListModel::setZfoFile(const QString &filePath)
bool FileListModel::setZfo(const QByteArray &rawZfoData)
{
return Files::openZfoFileFromStorage(*this, filePath);
return Files::setAttachmentModel(*this, rawZfoData);
}
......@@ -135,10 +135,12 @@ public:
void clearAll(void);
/*!
* @brief Set content from ZFO file.
* @brief Set content from ZFO content.
*
* @param[in] rawZfoData Message content.
*/
Q_INVOKABLE
bool setZfoFile(const QString &filePath);
bool setZfo(const QByteArray &rawZfoData);
private:
QList<Entry> m_files; /*!< List of attachment entries. */
......
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