Commit 3f127a2c authored by Martin Straka's avatar Martin Straka

Added import zfo task.

parent 4d3eae86
......@@ -145,6 +145,7 @@ SOURCES += \
src/worker/task_download_message_list.cpp \
src/worker/task_find_databox.cpp \
src/worker/task_find_databox_fulltext.cpp \
src/worker/task_import_zfo.cpp \
src/worker/task_keep_alive.cpp \
src/worker/task_send_message.cpp \
src/worker/task_send_sms.cpp \
......@@ -209,6 +210,7 @@ HEADERS += \
src/worker/task_download_message_list.h \
src/worker/task_find_databox.h \
src/worker/task_find_databox_fulltext.h \
src/worker/task_import_zfo.h \
src/worker/task_keep_alive.h \
src/worker/task_send_message.h \
src/worker/task_send_sms.h \
......
/*
* Copyright (C) 2014-2017 CZ.NIC
* Copyright (C) 2014-2018 CZ.NIC
*
* 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
......
/*
* Copyright (C) 2014-2017 CZ.NIC
* Copyright (C) 2014-2018 CZ.NIC
*
* 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
......
/*
* Copyright (C) 2014-2017 CZ.NIC
* Copyright (C) 2014-2018 CZ.NIC
*
* 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
......@@ -79,6 +79,16 @@ signals:
bool success, const QString &statusBarText, const QString &errTxt,
bool isMsgReceived);
/*!
* @brief Do some actions when import of one ZFO has been finished.
*
* @param[in] zfoNumber ZFO number in import process.
* @param[in] zfoTotal Overall number of ZFO for import.
* @param[in] infoText Import result description (may be NULL).
*/
void importZFOFinishedSignal(int zfoNumber, int zfoTotal,
QString infoText);
/*!
* @brief Do some actions when send message finished.
*
......
/*
* Copyright (C) 2014-2018 CZ.NIC
*
* 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 <QByteArray>
#include <QFile>
#include <QThread>
#include "src/log/log.h"
#include "src/net/isds_const.h"
#include "src/net/xml_layer.h"
#include "src/sqlite/account_db.h"
#include "src/worker/emitter.h"
#include "src/worker/task_import_zfo.h"
TaskImportZfo::TaskImportZfo(IsdsSession *isdsSession, NetLayer *netLayer,
DbWrapper *dbWrapper, const QStringList &userNameList,
const QString &fileName, bool authenticate, int zfoNumber, int zfoTotal)
: m_result(IMP_ERR),
m_errText(),
m_isdsSession(isdsSession),
m_netLayer(netLayer),
m_dbWrapper(dbWrapper),
m_userNameList(userNameList),
m_fileName(fileName),
m_auth(authenticate),
m_zfoNumber(zfoNumber),
m_zfoTotal(zfoTotal)
{
}
void TaskImportZfo::run(void)
{
if (Q_NULLPTR == m_isdsSession) {
Q_ASSERT(0);
return;
}
if (Q_NULLPTR == m_netLayer) {
Q_ASSERT(0);
return;
}
if (Q_NULLPTR == m_dbWrapper) {
Q_ASSERT(0);
return;
}
if (m_fileName.isEmpty()) {
Q_ASSERT(0);
return;
}
if (m_userNameList.isEmpty()) {
Q_ASSERT(0);
return;
}
logDebugLv0NL("Starting import ZFO task in thread '%p'",
(void *) QThread::currentThreadId());
/* ### Worker task begin. ### */
m_result = importZfo(m_isdsSession, m_netLayer, m_dbWrapper,
m_userNameList, m_fileName, m_auth, m_errText);
emit globMsgProcEmitter.importZFOFinishedSignal(m_zfoNumber, m_zfoTotal,
m_errText);
/* ### Worker task end. ### */
logDebugLv0NL("Import ZFO task finished in thread '%p'",
(void *) QThread::currentThreadId());
}
enum TaskImportZfo::Result TaskImportZfo::importZfo(IsdsSession *isdsSession,
NetLayer *netLayer, DbWrapper *dbWrapper, const QStringList &userNameList,
const QString &fileName, bool authenticate, QString &errText)
{
/* Open file and read its content */
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) {
errText = setErrorTextAndColor(QObject::tr("Cannot open ZFO file '%1'.").arg(fileName));
return IMP_ZFO_ERR;
}
QByteArray zfoContent = file.readAll();
/* Parse zfo content and detect valid ZFO format */
MsgEnvelope msgEnvelope;
QList<AttachmentData> fileList;
if (!XmlLayer::parseZFO(zfoContent, msgEnvelope, fileList, errText)) {
errText = setErrorTextAndColor(QObject::tr("Wrong message format of ZFO file '%1'.").arg(fileName));
return IMP_ZFO_FORMAT_ERR;
}
if (fileList.isEmpty()) {
errText = setErrorTextAndColor(QObject::tr("Wrong message data of ZFO file '%1'.").arg(fileName));
return IMP_ZFO_DATA_ERR;
}
/* Recognise relevant accounts and message orientation */
QStringList usrNameList; // usrNameList holds relevant user names for import.
QList<bool> isSentList; // isSentList holds msg orientation for user
// names from uNameList.
foreach (const QString &user, userNameList) {
QString dbId = globAccountDbPtr->dbId(user);
if (dbId == msgEnvelope.dbIDRecipient()) {
isSentList.append(false);
usrNameList.append(user);
} else if (dbId == msgEnvelope.dbIDSender()) {
isSentList.append(true);
usrNameList.append(user);
}
}
/* If relevant account does not exist, return result info text and exit */
if (usrNameList.isEmpty()) {
errText = setErrorTextAndColor(QObject::tr("Relevant account does not exist for ZFO file '%1'.").arg(fileName));
return IMP_NO_ACCOUNT_ERR;
}
/* Both lists must have same number of items */
if (usrNameList.count() != isSentList.count()) {
errText = QObject::tr("Import internal error.");
return IMP_ERR;
}
/* Authenticate zfo message in the ISDS */
if (authenticate) {
/* Send SOAP request to verify message */
QByteArray xmlDataOut;
if (!netLayer->sendSoapRequest(
isdsSession->isdsCtxMap[usrNameList.at(0)],
LOGIN_SERVICE,
XmlLayer::xmlCreateAuthenticateMessageSoapRequest(zfoContent.toBase64()),
xmlDataOut)) {
errText = setErrorTextAndColor(QObject::tr("Cannot authenticate the ZFO file '%1'.").arg(fileName));
return IMP_ISDS_ERR;
}
/* Parse SOAP response */
if (!XmlLayer::parseAuthenticateMessage(xmlDataOut, errText)) {
errText = setErrorTextAndColor(QObject::tr("ZFO file '%1' is not authentic (returns ISDS)!").arg(fileName));
return IMP_AUTH_ERR;
}
}
/* Store message into accounts database */
for (int i = 0; i < usrNameList.count(); ++i) {
(isSentList.at(i)) ? msgEnvelope._setDmMessageType(MessageDb::MessageType::TYPE_SENT)
: msgEnvelope._setDmMessageType(MessageDb::MessageType::TYPE_RECEIVED);
if (!dbWrapper->insertCompleteMessageToDb(usrNameList.at(i),
msgEnvelope, fileList, errText)) {
errText = setErrorTextAndColor(QObject::tr("Cannot store the ZFO file '%1' to local database.").arg(fileName));
return IMP_DB_INS_ERR;
}
}
#if 0
/* Save ZFO to zfo database */
dbWrapper->insertZfoToDb(msgEnvelope.dmID(), true, zfoContent.size(),
zfoContent.toBase64());
#endif
errText = QObject::tr("ZFO file '%1' has been imported.").arg(fileName);
return IMP_SUCCESS;
}
QString TaskImportZfo::setErrorTextAndColor(const QString &text)
{
return "<font color=\"red\">" + text + " " +
QObject::tr("File has not been imported!") + "</font>";
}
/*
* Copyright (C) 2014-2018 CZ.NIC
*
* 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.
*/
#pragma once
#include "src/net/db_wrapper.h"
#include "src/net/isds_session.h"
#include "src/net/net_layer.h"
#include "src/worker/task.h"
/*!
* @brief Task describing ZFO file import.
*/
class TaskImportZfo : public Task {
public:
/*!
* @brief Return state describing what happened.
*/
enum Result {
IMP_SUCCESS, /*!< Import was successful. */
IMP_ZFO_ERR, /*!< Load ZFO file error. */
IMP_ZFO_FORMAT_ERR, /*!< ZFO format is wrong. Not valid message. */
IMP_ZFO_DATA_ERR, /*!< Data couldn't be read or do not match supplied type. */
IMP_NO_ACCOUNT_ERR, /*!< Relevant account does not exist for the message. */
IMP_ISDS_ERR, /*!< Error communicating with ISDS. */
IMP_AUTH_ERR, /*!< Data couldn't be authenticated. */
IMP_DB_INS_ERR, /*!< Error inserting into database. */
IMP_DB_EXISTS, /*!< Message already exists. */
IMP_ERR /*!< Other error. */
};
/*!
* @brief Constructor.
*
* @param[in] isdsSession Pointer to Isds session.
* @param[in] netLayer Pointer to network manager.
* @param[in] dbWrapper Pointer to database wrapper.
* @param[in] userNameList List of account user names.
* @param[in] fileName Full path to ZFO file.
* @param[in] authenticate True if you want to authenticate message
* before importing in ISDS.
* @param[in] zfoNumber ZFO number in import process.
* @param[in] zfoTotal Overall number of ZFO for import.
*/
explicit TaskImportZfo(IsdsSession *isdsSession, NetLayer *netLayer,
DbWrapper *dbWrapper, const QStringList &userNameList,
const QString &fileName, bool authenticate, int zfoNumber,
int zfoTotal);
/*!
* @brief Performs action.
*/
virtual
void run(void) Q_DECL_OVERRIDE;
/*!
* @brief Import ZFO file.
*
* @param[in] isdsSession Pointer to Isds session.
* @param[in] netLayer Pointer to network manager.
* @param[in] dbWrapper Pointer to database wrapper.
* @param[in] userNameList List of account user names.
* @param[in] fileName Full path to ZFO file.
* @param[in] authenticate True if you want to authenticate message
* before importing in ISDS.
* @param[out] errText Error/state description.
*/
static
enum Result importZfo(IsdsSession *isdsSession, NetLayer *netLayer,
DbWrapper *dbWrapper, const QStringList &userNameList,
const QString &fileName, bool authenticate, QString &errText);
enum Result m_result; /*!< Import outcome. */
QString m_errText; /*!< Result/error description */
private:
/*!
* @brief Returns html error import color text.
*
* @param[in] text Original error description.
* @return color error text string.
*/
static
QString setErrorTextAndColor(const QString &text);
/*!
* Disable copy and assignment.
*/
TaskImportZfo(const TaskImportZfo &);
TaskImportZfo &operator=(const TaskImportZfo &);
IsdsSession *m_isdsSession; /*!< Pointer to Isds sessions. */
NetLayer *m_netLayer; /*!< Pointer to network manager to send request. */
DbWrapper *m_dbWrapper; /*!< Pointer to database wrapper to store reply. */
const QStringList m_userNameList; /*!< List of account user names. */
const QString m_fileName; /*!< Full file path to imported file. */
bool m_auth; /*!< True if authentication before importing. */
int m_zfoNumber; /*!< ZFO number in import process. */
int m_zfoTotal; /*!< Overall number of ZFO for import. */
};
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