log.cpp 3.51 KB
Newer Older
1
/*
2
 * Copyright (C) 2014-2019 CZ.NIC
3 4 5 6 7 8 9 10
 *
 * 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
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 13 14
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * 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 <QFile>
#include <QList>
#include <QSysInfo>

#include "src/auxiliaries/email_helper.h"
#include "src/datovka_shared/log/log.h"
30
#include "src/datovka_shared/log/memory_log.h"
31 32
#include "src/dialogues/dialogues.h"
#include "src/io/filesystem.h"
33
#include "src/log.h"
34

35 36 37
Log::Log(MemoryLog *memLog, QObject *parent)
    : QObject(parent),
    m_memLog(memLog)
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
{
	if (m_memLog != Q_NULLPTR) {
		connect(m_memLog, SIGNAL(logged(quint64)),
		    this, SLOT(appendNewLog(quint64)));
	}
}

Log::~Log(void)
{
	if (m_memLog != Q_NULLPTR) {
		m_memLog->disconnect(SIGNAL(logged(quint64)),
		    this, SLOT(appendNewLog(quint64)));
	}
}

53
QString Log::getLogFileLocation(void)
54 55 56 57 58 59 60 61 62 63
{
	return appLogDirPath();
}

QString Log::loadLogContent(const QString &filePath)
{
	debugFuncCall();

	QString log;

64
	if (filePath.isEmpty() && (m_memLog != Q_NULLPTR)) {
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 90 91 92 93 94 95 96
		const QList<quint64> keys(m_memLog->keys());
		foreach (quint64 key, keys) {
			log.append(m_memLog->message(key));
		}
	} else {
		QFile file(filePath);
		if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
			Q_ASSERT(0);
			logErrorNL("Cannot open file '%s'.",
			    filePath.toUtf8().constData());
			return QString();
		}
		log.append(file.readAll());
		file.close();
	}

	return log;
}

void Log::sendLogViaEmail(const QString &logContent)
{
	debugFuncCall();

	int msgResponse = Dialogues::message(Dialogues::QUESTION,
	    tr("Send log via email"),
	    tr("Do you want to send the log information to developers?"),
	    QString(), Dialogues::NO | Dialogues::YES, Dialogues::NO);
	if (msgResponse == Dialogues::NO) {
		return;
	}

	QString to("datove-schranky@labs.nic.cz");
97
	QString subject("Mobile Datovka: Log file");
98 99 100 101 102 103 104 105 106 107
	QString body("Mobile Datovka");
	body.append("\nVersion: " + QStringLiteral(VERSION));
	body.append("\nOS: " + QSysInfo::productType() + " "
	    + QSysInfo::productVersion());
	body.append("\nArch: " + QSysInfo::currentCpuArchitecture());

	removeDirFromDocLoc(DATOVKA_MAIL_DIR_NAME);

	QString targetPath(appEmailDirPath("Log"));
	QString filePath(writeFile(targetPath, "mobile-datovka.log",
108
	    logContent.toUtf8()));
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
	QStringList attachmentList;
	attachmentList.append(filePath);

	const QString boundary = generateBoundaryString();
	QString emailMessage = createEmailMessage(to, body, subject,  boundary);
	addAttachmentToEmailMessage(emailMessage, "mobile-datovka.log",
	    logContent.toUtf8().toBase64(), boundary);
	finishEmailMessage(emailMessage, boundary);
	sendEmail(emailMessage, attachmentList, to, subject, body, "Log");
}

void Log::appendNewLog(quint64 key)
{
	emit appendNewLogMessage(m_memLog->message(key));
}