Using the default config location to store history.

This commit is contained in:
Swapnil Tripathi 2021-08-19 02:33:56 +05:30
parent 9598108d43
commit b5163dfecc
2 changed files with 14 additions and 8 deletions

View File

@ -11,6 +11,8 @@
#include <QJsonDocument>
#include <QJsonObject>
const QString SEARCHHISTORY_CFG_GROUP = QStringLiteral("General"), SEARCHHISTORY_CFG_KEY = QStringLiteral("searches");
SearchHistory::SearchHistory(QObject *parent, const QString &searchTerm)
: QObject(parent)
, m_searchTerm(searchTerm)
@ -39,26 +41,24 @@ void SearchHistory::setSearchTerm(const QString &searchTerm)
emit propertyChanged();
}
//////////////// MODEL ///////////////////////////
SearchHistoryModel::SearchHistoryModel(QObject *parent)
: QAbstractListModel(parent)
{
m_settings = new QSettings(parent);
load();
}
SearchHistoryModel::~SearchHistoryModel()
{
save();
delete m_settings;
qDeleteAll(m_searches);
}
void SearchHistoryModel::load()
{
QJsonDocument doc = QJsonDocument::fromJson(m_settings->value(QStringLiteral("searches")).toString().toUtf8());
auto config = KSharedConfig::openConfig();
KConfigGroup group = config->group(SEARCHHISTORY_CFG_GROUP);
QJsonDocument doc = QJsonDocument::fromJson(group.readEntry(SEARCHHISTORY_CFG_KEY, "{}").toUtf8());
const auto array = doc.array();
std::transform(array.begin(), array.end(), std::back_inserter(m_searches), [](const QJsonValue &ser) {
@ -75,7 +75,11 @@ void SearchHistoryModel::save()
return QJsonValue(search->toJson());
});
m_settings->setValue(QStringLiteral("searches"), QString::fromUtf8(QJsonDocument(arr).toJson(QJsonDocument::Compact)));
auto config = KSharedConfig::openConfig();
KConfigGroup group = config->group(SEARCHHISTORY_CFG_GROUP);
group.writeEntry(SEARCHHISTORY_CFG_KEY, QJsonDocument(arr).toJson(QJsonDocument::Compact));
group.sync();
}
QHash<int, QByteArray> SearchHistoryModel::roleNames() const

View File

@ -11,7 +11,10 @@
#include <QAbstractListModel>
#include <QCoreApplication>
#include <QJsonObject>
#include <QSettings>
#include <KConfigGroup>
#include <KLocalizedString>
#include <KSharedConfig>
class SearchHistory : public QObject
{
@ -75,7 +78,6 @@ private:
explicit SearchHistoryModel(QObject *parent = nullptr);
~SearchHistoryModel();
QSettings *m_settings;
QList<SearchHistory *> m_searches;
};