task_download_message_list.cpp 5.07 KB
Newer Older
1 2 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 31 32 33 34 35
/*
 * 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 <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 "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(),
36
    m_statusBarText(),
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    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,
77 78
	    m_msgDirect, m_dmStatusFilter, m_dmOffset, m_dmLimit, m_msgIds,
	    m_statusBarText);
79 80 81 82 83 84 85 86 87 88

	/* ### 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,
89
    uint dmOffset, uint dmLimit, QList<qint64> &msgIds, QString &statusBarText)
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 117 118 119 120
{
	if (ctx.username.isEmpty()) {
		Q_ASSERT(0);
		return DL_ERR;
	}

	QByteArray xmlDataOut;
	QList<Messages::Message> 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<qint64> 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;
	}

121 122 123
	/* Store info text about new messages (for showing in status bar) */
	statusBarText = ctx.last_isds_msg;

124 125
	/*
	 * For sent message where message status was changed
126
	 * we should update delivery info as well.
127 128 129 130 131 132 133
	 */
	if (Messages::TYPE_SENT == msgDirect) {

		foreach (qint64 msgId, messageChangedStatusList) {

			QList<Messages::Event> eventList;

134 135 136 137 138
			/*
			 * Following functions don't have to check return values
			 * because they are complementary actions for message
			 * list download (delivery info is not important).
			 */
139

140 141 142 143
			/* Send delivery info SOAP request */
			netLayer->sendSoapRequest(ctx, MESSAGE_SERVICE,
			    XmlLayer::xmlCreateSignedDeliveryInfoSoapRequest(msgId),
			    xmlDataOut);
144

145 146 147
			/* Parse delivery info SOAP response */
			XmlLayer::parseGetMsgDeliveryInfoResponse(xmlDataOut,
			    eventList, ctx.last_isds_msg);
148

149 150 151 152 153
			/* Store delivery info into db */
			if (!eventList.isEmpty()) {
				dbWrapper->insertMesasgeDeliveryInfoToDb(
				    ctx.username, eventList, msgId,
				    ctx.last_isds_msg);
154 155 156 157
			}
		}
	}

158
	/* Return message ID list for additional download of complete messages */
159 160 161 162 163 164
	foreach (const Messages::Message &msg, messages) {
		msgIds.append(msg.dmID);
	}

	return DL_SUCCESS;
}