/*
* Copyright (C) 2014-2017 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 .
*
* 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 "src/log/log.h"
#include "src/net/isds_const.h"
#include "src/net/xml_layer.h"
#include "src/worker/pool.h" /* List with whole messages. */
#include "src/worker/task_download_message_list.h"
#include "src/sqlite/message_db_container.h"
TaskDownloadMessageList::TaskDownloadMessageList(IsdsSession::IsdsContext &ctx,
NetLayer *netLayer, DbWrapper *dbWrapper, enum Messages::MessageType msgDirect,
uint dmStatusFilter, uint dmOffset, uint dmLimit)
: m_result(DL_ERR),
m_msgIds(),
m_statusBarText(),
m_ctx(ctx),
m_netLayer(netLayer),
m_dbWrapper(dbWrapper),
m_msgDirect(msgDirect),
m_dmStatusFilter(dmStatusFilter),
m_dmOffset(dmOffset),
m_dmLimit(dmLimit)
{
}
void TaskDownloadMessageList::run(void)
{
if (Q_NULLPTR == m_netLayer) {
Q_ASSERT(0);
return;
}
if (Q_NULLPTR == m_dbWrapper) {
Q_ASSERT(0);
return;
}
if ((Messages::TYPE_RECEIVED != m_msgDirect)
&& (Messages::TYPE_SENT != m_msgDirect)) {
Q_ASSERT(0);
return;
}
logDebugLv0NL("Starting download message list task in thread '%p'",
(void *) QThread::currentThreadId());
/* ### Worker task begin. ### */
logDebugLv1NL("%s", "-----------------------------------------------");
logDebugLv1NL("Downloading %s message list for account '%s'.",
(Messages::TYPE_RECEIVED == m_msgDirect) ? "received" : "sent",
m_ctx.account_name.toUtf8().constData());
logDebugLv1NL("%s", "-----------------------------------------------");
m_result = downloadMessageList(m_ctx, m_netLayer, m_dbWrapper,
m_msgDirect, m_dmStatusFilter, m_dmOffset, m_dmLimit, m_msgIds,
m_statusBarText);
/* ### Worker task end. ### */
logDebugLv0NL("Download message list task finished in thread '%p'",
(void *) QThread::currentThreadId());
}
enum TaskDownloadMessageList::Result TaskDownloadMessageList::downloadMessageList(
IsdsSession::IsdsContext &ctx, NetLayer *netLayer, DbWrapper *dbWrapper,
enum Messages::MessageType msgDirect, uint dmStatusFilter,
uint dmOffset, uint dmLimit, QList &msgIds, QString &statusBarText)
{
if (ctx.username.isEmpty()) {
Q_ASSERT(0);
return DL_ERR;
}
QByteArray xmlDataOut;
QList messages;
/* Send SOAP request */
if (!netLayer->sendSoapRequest(ctx, MESSAGE_SERVICE,
XmlLayer::xmlCreateGetMessageListSoapRequest(msgDirect,
dmStatusFilter, dmOffset, dmLimit), xmlDataOut)) {
return DL_ISDS_ERROR;
}
/* Parse SOAP response */
if (!XmlLayer::parseGetListOfMessagesResponse(
(Messages::TYPE_RECEIVED == msgDirect) ? MessageDb::TYPE_RECEIVED : MessageDb::TYPE_SENT,
xmlDataOut, messages, ctx.last_isds_msg)) {
return DL_XML_ERROR;
}
/* Store data into db */
QList messageChangedStatusList;
if (!dbWrapper->insertMessageListToDb(ctx.username,
(Messages::TYPE_RECEIVED == msgDirect) ? MessageDb::TYPE_RECEIVED : MessageDb::TYPE_SENT,
messages, messageChangedStatusList, ctx.last_isds_msg)) {
return DL_DB_INS_ERR;
}
/* Store info text about new messages (for showing in status bar) */
statusBarText = ctx.last_isds_msg;
/*
* For sent message where message status was changed
* we should update delivery info as well.
*/
if (Messages::TYPE_SENT == msgDirect) {
foreach (qint64 msgId, messageChangedStatusList) {
QList eventList;
/*
* Following functions don't have to check return values
* because they are complementary actions for message
* list download (delivery info is not important).
*/
/* Send delivery info SOAP request */
netLayer->sendSoapRequest(ctx, MESSAGE_SERVICE,
XmlLayer::xmlCreateSignedDeliveryInfoSoapRequest(msgId),
xmlDataOut);
/* Parse delivery info SOAP response */
XmlLayer::parseGetMsgDeliveryInfoResponse(xmlDataOut,
eventList, ctx.last_isds_msg);
/* Store delivery info into db */
if (!eventList.isEmpty()) {
dbWrapper->insertMesasgeDeliveryInfoToDb(
ctx.username, eventList, msgId,
ctx.last_isds_msg);
}
}
}
/* Return message ID list for additional download of complete messages */
foreach (const Messages::Message &msg, messages) {
msgIds.append(msg.dmID);
}
return DL_SUCCESS;
}