settings.cpp 8.67 KB
Newer Older
1
/*
2
 * Copyright (C) 2014-2019 CZ.NIC
3 4 5 6 7 8 9 10 11 12 13 14
 *
 * 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
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20 21 22 23
 *
 * 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.
 */

24
#include <QDateTime>
25 26
#include <QDir>

27
#include "src/datovka_shared/localisation/localisation.h"
28
#include "src/datovka_shared/log/log.h"
29
#include "src/datovka_shared/settings/records_management.h"
30
#include "src/global.h"
31 32 33
#include "src/io/filesystem.h"
#include "src/settings.h"

34 35 36 37 38
/* Name of Datovka configuration file */
#define SETTINGS_FILE_NAME "datovka.conf"
/* Supported languages. Default is system */
/* Default value which define how long will have
 * attachment files stored in local database (in days)*/
39 40
#define DEFAULT_MSG_LIFETIME 0 // 0 means mesasges do not delete
#define DEFAULT_FILE_LIFETIME 0 // 0 means files do not delete
41 42 43
#define DEFAULT_ZFO_LIMIT_MB 200
#define DEFAULT_PIN_INACT_TIMEOUT 0
#define DEFAULT_FONT_SIZE 16
44 45
#define DEFAULT_DEBUG_VERBOSITY_LEVEL 1
#define DEFAULT_LOG_VERBOSITY_LEVEL 1
46 47 48 49 50 51 52 53 54 55 56 57
/* Names of setting items in GLOBALS group in the datovka.conf file */
#define SETTINGS_GLOBAL_GROUP "GLOBALS"
#define SETTINGS_ONLY_NEW_MSGS "only_new_msgs"
#define SETTINGS_COMPLETE_MSG "complete_msgs"
#define SETTINGS_MSG_LIFETIME_IN_DAYS "msg_lifetime"
#define SETTINGS_FILE_LIFETIME_IN_DAYS "file_lifetime"
#define SETTINGS_ZFO_LIMIT_MB "zfo_db_max_size"
#define SETTINGS_PIN_INACT_TIMEOUT_IN_SECS "pin_inact_timeout"
#define SETTINGS_LAST_UPDATE "last_update"
#define SETTINGS_LANGUAGE "language"
#define SETTINGS_FONTSIZE "font_size"
#define SETTINGS_DBS_LOCATION "dbs_location"
58 59
#define SETTINGS_DEBUG_VERBOSITY_LEVEL "debug_verbosity_level"
#define SETTINGS_LOG_VERBOSITY_LEVEL "log_verbosity_level"
60 61 62 63 64 65 66 67 68

/*
 * These names are kept for backward compatibility purposes. They were formerly
 * in GLOBALS, now they are in separate group.
 */
#define SETTINGS_PIN_ALG "pin_alg"
#define SETTINGS_PIN_SALT "pin_salt"
#define SETTINGS_PIN_CODE "pin_code"

69
Settings::Settings(void)
70
    : PinSettings(),
71
    language(Localisation::langSystem),
72
    fontSize(DEFAULT_FONT_SIZE),
73 74
    downloadOnlyNewMsgs(true),
    downloadCompleteMsgs(false),
75
    msgLifeTimeInDays(DEFAULT_MSG_LIFETIME),
76
    fileLifeTimeInDays(DEFAULT_FILE_LIFETIME),
77
    pinInactTimeoutInSecs(DEFAULT_PIN_INACT_TIMEOUT),
78
    dbsLocation(),
79
    zfoDbSizeMBs(DEFAULT_ZFO_LIMIT_MB),
80 81
    debugVerbosityLevel(DEFAULT_DEBUG_VERBOSITY_LEVEL),
    logVerbosityLevel(DEFAULT_LOG_VERBOSITY_LEVEL),
82
    m_lastUpdate()
83 84 85
{
}

86
void Settings::saveToSettings(QSettings &settings) const
87
{
88
	debugFuncCall();
89 90

	settings.beginGroup(SETTINGS_GLOBAL_GROUP);
91 92 93 94 95 96
	settings.setValue(SETTINGS_LANGUAGE, language);
	settings.setValue(SETTINGS_FONTSIZE, fontSize);
	settings.setValue(SETTINGS_ONLY_NEW_MSGS, downloadOnlyNewMsgs);
	settings.setValue(SETTINGS_COMPLETE_MSG, downloadCompleteMsgs);
	settings.setValue(SETTINGS_MSG_LIFETIME_IN_DAYS, msgLifeTimeInDays);
	settings.setValue(SETTINGS_FILE_LIFETIME_IN_DAYS, fileLifeTimeInDays);
97
	settings.setValue(SETTINGS_ZFO_LIMIT_MB, zfoDbSizeMBs);
98 99
	settings.setValue(SETTINGS_DEBUG_VERBOSITY_LEVEL, debugVerbosityLevel);
	settings.setValue(SETTINGS_LOG_VERBOSITY_LEVEL, logVerbosityLevel);
100 101

	if (!_pinVal.isEmpty()) {
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
		{
			/*
			 * Code in this block needs to be kept for a while
			 * (until the end of 2018?) because of keeping
			 * backward compatibility.
			 */
			Q_ASSERT(!pinAlg.isEmpty());
			settings.setValue(SETTINGS_PIN_ALG, pinAlg);
			Q_ASSERT(!pinSalt.isEmpty());
			settings.setValue(SETTINGS_PIN_SALT,
			    QString::fromUtf8(pinSalt.toBase64()));
			Q_ASSERT(!pinCode.isEmpty());
			settings.setValue(SETTINGS_PIN_CODE,
			    QString::fromUtf8(pinCode.toBase64()));
		}
117 118 119 120 121 122

		if (pinInactTimeoutInSecs > 0) {
			/* Only when some value is used. */
			settings.setValue(SETTINGS_PIN_INACT_TIMEOUT_IN_SECS,
			    pinInactTimeoutInSecs);
		}
123
	}
124

125
	settings.setValue(SETTINGS_LAST_UPDATE, m_lastUpdate);
126

127
	if (dbsLocation != dfltDbAndConfigLoc()) {
128
		settings.setValue(SETTINGS_DBS_LOCATION, dbsLocation);
129 130
	}

131
	settings.endGroup();
132 133 134 135 136

	/*
	 * The PIN settings are newly kept in a separate group.
	 */
	PinSettings::saveToSettings(settings);
137 138

	/* Records management settings. */
139 140 141
	if (GlobInstcs::recMgmtSetPtr != Q_NULLPTR) {
		GlobInstcs::recMgmtSetPtr->saveToSettings(_pinVal, settings);
	}
142 143
}

144
void Settings::loadFromSettings(const QSettings &settings)
145
{
146
	debugFuncCall();
147

148
	language = settings.value(SETTINGS_GLOBAL_GROUP "/" SETTINGS_LANGUAGE,
149
	    Localisation::langSystem).toString();
150
	fontSize = settings.value(SETTINGS_GLOBAL_GROUP "/" SETTINGS_FONTSIZE,
151
	    DEFAULT_FONT_SIZE).toInt();
152
	downloadOnlyNewMsgs = settings.value(
153
	    SETTINGS_GLOBAL_GROUP "/" SETTINGS_ONLY_NEW_MSGS, true).toBool();
154
	downloadCompleteMsgs = settings.value(
155
	    SETTINGS_GLOBAL_GROUP "/" SETTINGS_COMPLETE_MSG, false).toBool();
156 157 158 159
	msgLifeTimeInDays = settings.value(
	    SETTINGS_GLOBAL_GROUP "/" SETTINGS_MSG_LIFETIME_IN_DAYS,
	    DEFAULT_MSG_LIFETIME).toInt();
	fileLifeTimeInDays = settings.value(
160 161
	    SETTINGS_GLOBAL_GROUP "/" SETTINGS_FILE_LIFETIME_IN_DAYS,
	    DEFAULT_FILE_LIFETIME).toInt();
162 163 164
	zfoDbSizeMBs = settings.value(
	    SETTINGS_GLOBAL_GROUP "/" SETTINGS_ZFO_LIMIT_MB,
	    DEFAULT_ZFO_LIMIT_MB).toInt();
165 166 167 168 169 170
	debugVerbosityLevel = settings.value(SETTINGS_GLOBAL_GROUP "/"
	    SETTINGS_DEBUG_VERBOSITY_LEVEL,
	    DEFAULT_DEBUG_VERBOSITY_LEVEL).toInt();
	logVerbosityLevel = settings.value(SETTINGS_GLOBAL_GROUP "/"
	    SETTINGS_LOG_VERBOSITY_LEVEL,
	    DEFAULT_LOG_VERBOSITY_LEVEL).toInt();
171

172 173 174 175
	/*
	 * The PIN settings are newly kept in a separate group.
	 */
	PinSettings::loadFromSettings(settings);
Karel Slaný's avatar
Karel Slaný committed
176
	/* Clear read PIN settings if not entire data present. */
177 178 179
	if (!PinSettings::pinConfigured()) {
		PinSettings::updatePinSettings(*this, QString());

180
		logWarningNL("Trying to read PIN settings from '%s' group.",
181 182 183
		    SETTINGS_GLOBAL_GROUP);

		/*
Karel Slaný's avatar
Karel Slaný committed
184
		 * First the PIN settings were stored in 'GLOBALS' group.
185 186 187 188 189 190 191 192 193 194 195 196 197
		 * Therefore we need to keep this code functional at least for
		 * a while (until the end of 2018?).
		 */
		pinAlg = settings.value(
		    SETTINGS_GLOBAL_GROUP "/" SETTINGS_PIN_ALG,
		    QString()).toString();
		pinSalt = QByteArray::fromBase64(settings.value(
		    SETTINGS_GLOBAL_GROUP "/" SETTINGS_PIN_SALT,
		    QString()).toString().toUtf8());
		pinCode = QByteArray::fromBase64(settings.value(
		    SETTINGS_GLOBAL_GROUP "/" SETTINGS_PIN_CODE,
		    QString()).toString().toUtf8());
	}
198

199 200 201 202
	pinInactTimeoutInSecs = settings.value(
	    SETTINGS_GLOBAL_GROUP "/" SETTINGS_PIN_INACT_TIMEOUT_IN_SECS,
	    DEFAULT_PIN_INACT_TIMEOUT).toInt();

203 204
	setLastUpdate(settings.value(
	    SETTINGS_GLOBAL_GROUP "/" SETTINGS_LAST_UPDATE).toString());
205

206
	dbsLocation = settings.value(
207
	    SETTINGS_GLOBAL_GROUP "/" SETTINGS_DBS_LOCATION).toString();
208

209
	if (dbsLocation.isEmpty()) {
210
		dbsLocation = dfltDbAndConfigLoc();
211
	}
212 213

	/* Records management settings. */
214 215 216
	if (GlobInstcs::recMgmtSetPtr != Q_NULLPTR) {
		GlobInstcs::recMgmtSetPtr->loadFromSettings(settings);
	}
217 218 219 220
}

QString Settings::settingsPath(void)
{
221
	QString settingsPath(dfltDbAndConfigLoc());
222 223 224 225 226 227 228 229

	if (settingsPath.isEmpty()) {
		return QString();
	}

	return settingsPath + QDir::separator() +
	    QStringLiteral(SETTINGS_FILE_NAME);
}
230

231 232 233 234 235 236 237 238 239 240
void Settings::setLastUpdate(const QString &str)
{
	QDateTime dateTime(QDateTime::fromString(str, Qt::ISODate));
	if (dateTime.isValid()) {
		m_lastUpdate = dateTime.toString(Qt::ISODate);
	} else {
		m_lastUpdate.clear();
	}
}

241
QString Settings::nowStr(void)
242
{
243
	return QDateTime::currentDateTime().toString(Qt::ISODate);
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
}

QString Settings::lastUpdateStr(void) const
{
#define DATE_FMT QStringLiteral("yyyy-MM-dd")
	QDateTime lastUpdateDateTime(
	    QDateTime::fromString(m_lastUpdate, Qt::ISODate));
	if (!lastUpdateDateTime.isValid()) {
		return QString();
	}
	QDateTime currentDateTime(QDateTime::currentDateTime());

	if (lastUpdateDateTime.toString(DATE_FMT) ==
	    currentDateTime.toString(DATE_FMT)) {
		/* Only time. */
		return lastUpdateDateTime.toString("HH:mm:ss");
	} else {
		/* Only date. */
		return lastUpdateDateTime.toString("dd.MM.yyyy");
	}
#undef DATE_FMT
}