AusweisApp2/src/settings/HistorySettings.cpp

173 lines
4.7 KiB
C++
Raw Normal View History

2017-07-03 09:30:10 +02:00
/*!
2018-03-28 15:10:51 +02:00
* \copyright Copyright (c) 2015-2018 Governikus GmbH & Co. KG, Germany
2017-07-03 09:30:10 +02:00
*/
#include "HistorySettings.h"
2018-03-28 15:10:51 +02:00
#include <QLoggingCategory>
2017-12-20 14:54:05 +01:00
#include <QSettings>
2017-07-03 09:30:10 +02:00
2017-12-20 14:54:05 +01:00
namespace
2017-07-03 09:30:10 +02:00
{
2017-12-20 14:54:05 +01:00
SETTINGS_NAME(SETTINGS_GROUP_NAME_CHRONIC, "history")
SETTINGS_NAME(SETTINGS_NAME_HISTORY_ITEMS, "items")
SETTINGS_NAME(SETTINGS_NAME_HISTORY_ENABLED, "enable")
SETTINGS_NAME(SETTINGS_NAME_CHRONIC_SUBJECTNAME, "subjectName")
SETTINGS_NAME(SETTINGS_NAME_CHRONIC_SUBJECTURL, "subjectUrl")
SETTINGS_NAME(SETTINGS_NAME_CHRONIC_USAGE, "usage")
SETTINGS_NAME(SETTINGS_NAME_CHRONIC_DATETIME, "dateTime")
SETTINGS_NAME(SETTINGS_NAME_CHRONIC_TOU, "termOfUsage")
SETTINGS_NAME(SETTINGS_NAME_CHRONIC_REQUESTED_DATA, "requestedData")
2017-07-03 09:30:10 +02:00
}
2017-12-20 14:54:05 +01:00
using namespace governikus;
2017-07-03 09:30:10 +02:00
2018-03-28 15:10:51 +02:00
Q_DECLARE_LOGGING_CATEGORY(settings)
2017-07-03 09:30:10 +02:00
2017-12-20 14:54:05 +01:00
HistorySettings::HistorySettings()
: AbstractSettings()
, mStore(getStore())
2017-07-03 09:30:10 +02:00
{
2017-12-20 14:54:05 +01:00
mStore->beginGroup(SETTINGS_GROUP_NAME_CHRONIC());
2017-07-03 09:30:10 +02:00
}
2017-12-20 14:54:05 +01:00
HistorySettings::~HistorySettings()
2017-07-03 09:30:10 +02:00
{
}
2017-12-20 14:54:05 +01:00
void HistorySettings::save()
2017-07-03 09:30:10 +02:00
{
2017-12-20 14:54:05 +01:00
mStore->sync();
2017-07-03 09:30:10 +02:00
}
2017-12-20 14:54:05 +01:00
bool HistorySettings::isEnabled() const
2017-07-03 09:30:10 +02:00
{
2017-12-20 14:54:05 +01:00
return mStore->value(SETTINGS_NAME_HISTORY_ENABLED(), true).toBool();
2017-07-03 09:30:10 +02:00
}
2017-12-20 14:54:05 +01:00
void HistorySettings::setEnabled(bool pEnabled)
2017-07-03 09:30:10 +02:00
{
2017-12-20 14:54:05 +01:00
if (isEnabled() != pEnabled)
{
mStore->setValue(SETTINGS_NAME_HISTORY_ENABLED(), pEnabled);
Q_EMIT fireEnabledChanged(pEnabled);
}
2017-07-03 09:30:10 +02:00
}
2017-12-20 14:54:05 +01:00
QVector<HistoryInfo> HistorySettings::getHistoryInfos() const
2017-07-03 09:30:10 +02:00
{
2017-12-20 14:54:05 +01:00
const int itemCount = mStore->beginReadArray(SETTINGS_NAME_HISTORY_ITEMS());
2017-07-03 09:30:10 +02:00
2017-12-20 14:54:05 +01:00
QVector<HistoryInfo> historyInfos;
historyInfos.reserve(itemCount);
for (int i = 0; i < itemCount; ++i)
{
mStore->setArrayIndex(i);
const QString subjectName = mStore->value(SETTINGS_NAME_CHRONIC_SUBJECTNAME(), QString()).toString();
const QString subjectUrl = mStore->value(SETTINGS_NAME_CHRONIC_SUBJECTURL(), QString()).toString();
const QString usage = mStore->value(SETTINGS_NAME_CHRONIC_USAGE(), QString()).toString();
const QDateTime dateTime = QDateTime::fromString(mStore->value(SETTINGS_NAME_CHRONIC_DATETIME(), QString()).toString(), Qt::ISODate);
const QString termsOfUsage = mStore->value(SETTINGS_NAME_CHRONIC_TOU(), QString()).toString();
const QString requestData = mStore->value(SETTINGS_NAME_CHRONIC_REQUESTED_DATA(), QString()).toString();
historyInfos += HistoryInfo(subjectName, subjectUrl, usage, dateTime, termsOfUsage, requestData);
}
2017-07-03 09:30:10 +02:00
2017-12-20 14:54:05 +01:00
mStore->endArray();
return historyInfos;
2017-07-03 09:30:10 +02:00
}
2017-12-20 14:54:05 +01:00
void HistorySettings::setHistoryInfos(const QVector<HistoryInfo>& pHistoryInfos)
2017-07-03 09:30:10 +02:00
{
2017-12-20 14:54:05 +01:00
mStore->beginGroup(SETTINGS_NAME_HISTORY_ITEMS());
mStore->remove(QString());
mStore->endGroup();
2017-07-03 09:30:10 +02:00
2017-12-20 14:54:05 +01:00
mStore->beginWriteArray(SETTINGS_NAME_HISTORY_ITEMS());
for (int i = 0; i < pHistoryInfos.size(); ++i)
2017-07-03 09:30:10 +02:00
{
2017-12-20 14:54:05 +01:00
const HistoryInfo& item = pHistoryInfos.at(i);
mStore->setArrayIndex(i);
mStore->setValue(SETTINGS_NAME_CHRONIC_SUBJECTNAME(), item.getSubjectName());
mStore->setValue(SETTINGS_NAME_CHRONIC_SUBJECTURL(), item.getSubjectUrl());
mStore->setValue(SETTINGS_NAME_CHRONIC_USAGE(), item.getPurpose());
mStore->setValue(SETTINGS_NAME_CHRONIC_DATETIME(), item.getDateTime().toString(Qt::ISODate));
mStore->setValue(SETTINGS_NAME_CHRONIC_TOU(), item.getTermOfUsage());
mStore->setValue(SETTINGS_NAME_CHRONIC_REQUESTED_DATA(), item.getRequestedData());
2017-07-03 09:30:10 +02:00
}
2017-12-20 14:54:05 +01:00
mStore->endArray();
2017-07-03 09:30:10 +02:00
2017-12-20 14:54:05 +01:00
Q_EMIT fireHistoryInfosChanged();
2017-07-03 09:30:10 +02:00
}
2017-12-20 14:54:05 +01:00
void HistorySettings::addHistoryInfo(const HistoryInfo& pHistoryInfo)
2017-07-03 09:30:10 +02:00
{
2017-12-20 14:54:05 +01:00
if (appIsBackgroundService())
2017-07-03 09:30:10 +02:00
{
2018-03-28 15:10:51 +02:00
qCDebug(settings) << "Running as a background service. Ignoring save request for history.";
2017-12-20 14:54:05 +01:00
return;
2017-07-03 09:30:10 +02:00
}
2017-12-20 14:54:05 +01:00
auto historyInfos = getHistoryInfos();
historyInfos.prepend(pHistoryInfo);
setHistoryInfos(historyInfos);
2017-07-03 09:30:10 +02:00
}
2018-03-28 15:10:51 +02:00
int HistorySettings::deleteSettings(const QDateTime& pLatestToKeep)
2017-07-03 09:30:10 +02:00
{
2017-12-20 14:54:05 +01:00
const auto historyInfos = getHistoryInfos();
QVector<HistoryInfo> remainingItems;
for (const auto& item : historyInfos)
2017-07-03 09:30:10 +02:00
{
if (!pLatestToKeep.isNull() && item.getDateTime() <= pLatestToKeep)
{
2017-07-03 09:33:28 +02:00
remainingItems += item;
2017-07-03 09:30:10 +02:00
}
}
2018-03-28 15:10:51 +02:00
int numberOfItemsToRemove = historyInfos.size() - remainingItems.size();
2017-12-20 14:54:05 +01:00
setHistoryInfos(remainingItems);
2018-03-28 15:10:51 +02:00
return numberOfItemsToRemove;
}
int HistorySettings::deleteSettings(const TimePeriod& pPeriodToRemove)
{
QDateTime latestToKeep = QDateTime::currentDateTime();
switch (pPeriodToRemove)
{
case TimePeriod::PAST_HOUR:
latestToKeep = latestToKeep.addSecs(-60 * 60);
break;
case TimePeriod::PAST_DAY:
latestToKeep = latestToKeep.addDays(-1);
break;
case TimePeriod::PAST_WEEK:
latestToKeep = latestToKeep.addDays(-7);
break;
case TimePeriod::LAST_FOUR_WEEKS:
latestToKeep = latestToKeep.addDays(-7 * 4);
break;
case TimePeriod::ALL_HISTORY:
latestToKeep = QDateTime();
break;
case TimePeriod::UNKNOWN:
return 0;
}
qCDebug(settings) << "Remove history entries until timestamp:" << latestToKeep;
return deleteSettings(latestToKeep);
2017-07-03 09:30:10 +02:00
}