mirror of
https://github.com/KDE/kasts.git
synced 2025-01-29 08:39:36 +01:00
Using the default config location to store history.
This commit is contained in:
parent
9598108d43
commit
b5163dfecc
@ -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
|
||||
|
@ -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;
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user