records_management.cpp 9.18 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
/*
 * 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 <QMessageBox>

Martin Straka's avatar
Martin Straka committed
26
#include "src/datovka_shared/graphics/graphics.h"
27 28
#include "src/datovka_shared/io/records_management_db.h"
#include "src/datovka_shared/records_management/json/service_info.h"
29 30
#include "src/datovka_shared/records_management/json/upload_file.h"
#include "src/datovka_shared/records_management/json/upload_hierarchy.h"
31
#include "src/models/accountmodel.h"
32
#include "src/records_management.h"
33 34 35
#include "src/settings.h"
#include "src/worker/pool.h"
#include "src/worker/task_records_management_stored_messages.h"
36

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 77 78 79 80 81 82 83 84 85 86 87 88 89
/*!
 * @brief Process upload file service response.
 *
 * @note Stores location into database.
 *
 * @param[in] ifRes Response structure.
 * @param[in] dmId Message identifier.
 * @patam[in] parent Parent window for potential error dialogues.
 * @return True if response could be processed and location has been saved.
 */
static
bool processUploadFileResponse(const UploadFileResp &ufRes, qint64 dmId,
    QWidget *parent = Q_NULLPTR)
{
	if (ufRes.id().isEmpty()) {
		QString errorMessage(
		    QObject::tr("Message '%1' could not be uploaded.")
		        .arg(dmId));
		errorMessage += QLatin1String("\n");
		errorMessage += QObject::tr("Received error") +
		    QLatin1String(": ") + ufRes.error().trVerbose();
		errorMessage += QLatin1String("\n");
		errorMessage += ufRes.error().description();

		QMessageBox::critical(parent, QObject::tr("File Upload Error"),
		    errorMessage);
		return false;
	}

	QMessageBox::information(parent, QObject::tr("Successful File Upload"),
	    QObject::tr("Message '%1' was successfully uploaded into the records management service.").arg(dmId) +
	    QStringLiteral("\n") +
	    QObject::tr("It can be now found in the records management service in these locations:") +
	    QStringLiteral("\n") +
	    ufRes.locations().join(QStringLiteral("\n")));

	if (!ufRes.locations().isEmpty()) {
		qCritical("%s", "Message has been stored into records management service.");
		if (Q_NULLPTR != globRecordsManagementDbPtr) {
			return globRecordsManagementDbPtr->updateStoredMsg(dmId,
			    ufRes.locations());
		} else {
			Q_ASSERT(0);
			return true;
		}
	} else {
		qCritical("%s",
		    "Received empty location list when uploading message.");
	}

	return false;
}

90
RecordsManagement::RecordsManagement(QObject *parent)
91
    : QObject(parent),
92 93 94
    m_uploadModel(),
    m_uploadProxyModel(),
    m_rmc(RecordsManagementConnection::ignoreSslErrorsDflt, this),
95
    m_logoSvg()
96
{
97 98 99 100 101 102 103
	globWorkPool.start();
}

RecordsManagement::~RecordsManagement(void)
{
	globWorkPool.wait();
	globWorkPool.stop();
104 105
}

106 107
void RecordsManagement::callServiceInfo(const QString &urlStr,
   const QString &tokenStr)
108 109
{
	QByteArray response;
110

111 112
	m_rmc.setConnection(urlStr.trimmed(), tokenStr.trimmed());

113 114
	if (m_rmc.communicate(RecordsManagementConnection::SRVC_SERVICE_INFO,
	        QByteArray(), response)) {
115 116 117 118 119
		if (!response.isEmpty()) {
			bool ok = false;
			ServiceInfoResp siRes(
			    ServiceInfoResp::fromJson(response, &ok));
			if (!ok || !siRes.isValid()) {
120 121 122
				QMessageBox::critical(Q_NULLPTR,
				    tr("Communication Error"),
				    tr("Received invalid response."));
123 124
				return;
			}
125 126

			m_logoSvg = siRes.logoSvg();
Martin Straka's avatar
Martin Straka committed
127 128
			loadRecordsManagementPixmap(m_logoSvg);
			emit serviceInfo(siRes.name(), siRes.tokenName());
129

130
		} else {
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
			QMessageBox::critical(Q_NULLPTR,
			    tr("Communication Error"),
			    tr("Received empty response."));
			return;
		}
	} else {
		return;
	}
}

void RecordsManagement::callUploadHierarchy(const QString &urlStr,
    const QString &tokenStr)
{
	QByteArray response;

	/* Clear model. */
147
	m_uploadModel.setHierarchy(UploadHierarchyResp());
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

	m_rmc.setConnection(urlStr.trimmed(), tokenStr.trimmed());

	if (m_rmc.communicate(RecordsManagementConnection::SRVC_UPLOAD_HIERARCHY,
	        QByteArray(), response)) {
		if (!response.isEmpty()) {
			bool ok = false;
			UploadHierarchyResp uhRes(
			    UploadHierarchyResp::fromJson(response, &ok));
			if (!ok || !uhRes.isValid()) {
				QMessageBox::critical(Q_NULLPTR,
				    tr("Communication Error"),
				    tr("Received invalid response."));
				return;
			}

164
			m_uploadModel.setHierarchy(uhRes);
165 166 167 168

		} else {
			QMessageBox::critical(Q_NULLPTR, tr("Communication Error"),
			    tr("Received empty response."));
169 170 171 172 173 174 175
			return;
		}
	} else {
		return;
	}
}

176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
void RecordsManagement::getStoredMsgInfoFromRecordsManagement(
    const QString &urlStr, const QString &tokenStr)
{

	TaskRecordsManagementStoredMessages *task =
	    new (::std::nothrow) TaskRecordsManagementStoredMessages(
		urlStr, tokenStr,
		TaskRecordsManagementStoredMessages::RM_UPDATE_STORED,
		Q_NULLPTR);
	if (Q_NULLPTR == task) {
		qCritical("%s", "Cannot create stored_files update task.");
		return;
	}
	task->setAutoDelete(true);
	/* Run in background. */
	globWorkPool.assignHi(task);

	foreach (const QString &userName, AccountListModel::globAccounts.keys()) {
		MessageDb *msgDb = globMessageDbsPtr->accessMessageDb(
		    globSet.dbsLocation, userName,
		    AccountListModel::globAccounts[userName].storeToDisk());
		if (msgDb == Q_NULLPTR) {
			qWarning("%s", "Cannot open message database.");
			return;
		}

		TaskRecordsManagementStoredMessages *task =
		    new (::std::nothrow) TaskRecordsManagementStoredMessages(
		        urlStr, tokenStr,
		        TaskRecordsManagementStoredMessages::RM_DOWNLOAD_ALL,
		        msgDb);
		if (Q_NULLPTR == task) {
			qWarning("Cannot create stored_files task for '%s'.",
			    userName.toUtf8().constData());
			continue;
		}
		task->setAutoDelete(true);
		/* Run in background. */
		globWorkPool.assignHi(task);

	}

	/* TODO - update message model */
}

221 222 223 224 225 226 227 228 229 230 231 232
void RecordsManagement::loadStoredServiceInfo(void)
{
	if (Q_NULLPTR == globRecordsManagementDbPtr) {
		return;
	}

	RecordsManagementDb::ServiceInfoEntry entry(
	    globRecordsManagementDbPtr->serviceInfo());
	if (!entry.isValid()) {
		return;
	}

233
	m_logoSvg = entry.logoSvg;
Martin Straka's avatar
Martin Straka committed
234 235
	loadRecordsManagementPixmap(entry.logoSvg);
	emit serviceInfo(entry.name, entry.tokenName);
236
}
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283

bool RecordsManagement::uploadMessage(const QString &urlStr,
    const QString &tokenStr, qint64 dmId, const QString &msgFileName,
    const QByteArray &msgData)
{
	if (msgFileName.isEmpty() || msgData.isEmpty()) {
		Q_ASSERT(0);
		return false;
	}

	QStringList uploadIds;

	UploadFileReq ufReq(uploadIds, msgFileName, msgData);
	if (!ufReq.isValid()) {
		Q_ASSERT(0);
		return false;
	}

	QByteArray response;

	m_rmc.setConnection(urlStr.trimmed(), tokenStr.trimmed());

	if (m_rmc.communicate(RecordsManagementConnection::SRVC_UPLOAD_FILE,
	        ufReq.toJson(), response)) {
		if (!response.isEmpty()) {
			bool ok = false;
			UploadFileResp ufRes(
			    UploadFileResp::fromJson(response, &ok));
			if (!ok || !ufRes.isValid()) {
				QMessageBox::critical(Q_NULLPTR,
				    tr("Communication Error"),
				    tr("Received invalid response."));
				qWarning("Received invalid response '%s'.",
				    QString(response).toUtf8().constData());
				return false;
			}
			return processUploadFileResponse(ufRes, dmId);
		} else {
			QMessageBox::critical(Q_NULLPTR, tr("Communication Error"),
			    tr("Received empty response."));
			return false;
		}
	} else {
		return false;
	}
}

Martin Straka's avatar
Martin Straka committed
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
bool RecordsManagement::updateServiceInfo(const QString &urlStr,
    const QString &urlStrSettings, const QString &srName, const QString &srToken)
{
	if (Q_NULLPTR == globRecordsManagementDbPtr) {
		return false;
	}

	if (!urlStr.trimmed().isEmpty()) {

		Q_ASSERT(!urlStr.trimmed().isEmpty());
		RecordsManagementDb::ServiceInfoEntry entry;
		entry.url = urlStr.trimmed();
		entry.name = srName;
		entry.tokenName = srToken;
		entry.logoSvg = m_logoSvg;
		globRecordsManagementDbPtr->updateServiceInfo(entry);

		if (urlStrSettings != urlStr) {
			globRecordsManagementDbPtr->deleteAllStoredMsg();
		}
	} else {
		globRecordsManagementDbPtr->deleteAllEntries();
	}

	return true;
}

void RecordsManagement::loadRecordsManagementPixmap(const QByteArray &logoSvg)
312
{
Martin Straka's avatar
Martin Straka committed
313 314 315
	if (Q_NULLPTR == globRecordsManagementDbPtr) {
		return;
	}
316

Martin Straka's avatar
Martin Straka committed
317
	QByteArray logo = logoSvg;
318

Martin Straka's avatar
Martin Straka committed
319
	if (logo.isNull()) {
320

Martin Straka's avatar
Martin Straka committed
321 322 323 324 325 326 327
		RecordsManagementDb::ServiceInfoEntry entry(
		    globRecordsManagementDbPtr->serviceInfo());
		if (!entry.isValid() || entry.logoSvg.isEmpty()) {
			return;
		}
		logo = entry.logoSvg;
	}
328

Martin Straka's avatar
Martin Straka committed
329 330 331 332 333
	QPixmap pixmap(Graphics::pixmapFromSvg(logo, LOGO_EDGE));
	if (!pixmap.isNull()) {
		/* TODO - set Pixmap into QML */
		//m_ui->pixmapLabel->setPixmap(pixmap);
	}
334
}