1/*
2 SPDX-FileCopyrightText: 2005-2009 Olivier Goffart <ogoffart at kde.org>
3 SPDX-FileCopyrightText: 2023 Kai Uwe Broulik <kde@broulik.de>
4
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6*/
7
8#include "knotifyconfig.h"
9
10#include <KConfigGroup>
11#include <KSharedConfig>
12
13#include <QCache>
14#include <QStandardPaths>
15
16typedef QCache<QString, KSharedConfig::Ptr> ConfigCache;
17Q_GLOBAL_STATIC_WITH_ARGS(ConfigCache, static_cache, (15))
18
19class KNotifyConfigPrivate : public QSharedData
20{
21public:
22 QString readEntry(const QString &group, const QString &key, bool path) const;
23
24 QString applicationName;
25 QString eventId;
26
27 KSharedConfig::Ptr eventsFile;
28 KSharedConfig::Ptr configFile;
29};
30
31QString KNotifyConfigPrivate::readEntry(const QString &group, const QString &key, bool path) const
32{
33 if (configFile->hasGroup(group)) {
34 KConfigGroup cg(configFile, group);
35 const QString value = path ? cg.readPathEntry(pKey: key, aDefault: QString()) : cg.readEntry(key, aDefault: QString());
36 if (!value.isNull()) {
37 return value;
38 }
39 }
40
41 if (eventsFile->hasGroup(group)) {
42 KConfigGroup cg(eventsFile, group);
43 const QString value = path ? cg.readPathEntry(pKey: key, aDefault: QString()) : cg.readEntry(key, aDefault: QString());
44 if (!value.isNull()) {
45 return value;
46 }
47 }
48
49 return QString();
50}
51
52static KSharedConfig::Ptr retrieve_from_cache(const QString &filename, QStandardPaths::StandardLocation type = QStandardPaths::GenericConfigLocation)
53{
54 QCache<QString, KSharedConfig::Ptr> &cache = *static_cache;
55 if (cache.contains(key: filename)) {
56 return *cache[filename];
57 }
58
59 KSharedConfig::Ptr m = KSharedConfig::openConfig(fileName: filename, mode: KConfig::NoGlobals, type);
60 // also search for event config files in qrc resources
61 if (type == QStandardPaths::GenericDataLocation) {
62 m->addConfigSources(sources: {QStringLiteral(":/") + filename});
63 }
64 cache.insert(key: filename, object: new KSharedConfig::Ptr(m));
65
66 return m;
67}
68
69void KNotifyConfig::reparseConfiguration()
70{
71 QCache<QString, KSharedConfig::Ptr> &cache = *static_cache;
72 const auto listFiles = cache.keys();
73 for (const QString &filename : listFiles) {
74 (*cache[filename])->reparseConfiguration();
75 }
76}
77
78void KNotifyConfig::reparseSingleConfiguration(const QString &app)
79{
80 QCache<QString, KSharedConfig::Ptr> &cache = *static_cache;
81 const QString appCacheKey = app + QStringLiteral(".notifyrc");
82 if (cache.contains(key: appCacheKey)) {
83 (*cache[appCacheKey])->reparseConfiguration();
84 }
85}
86
87KNotifyConfig::KNotifyConfig(const QString &applicationName, const QString &eventId)
88 : d(new KNotifyConfigPrivate)
89{
90 d->applicationName = applicationName;
91 d->eventId = eventId;
92
93 d->eventsFile = retrieve_from_cache(filename: QLatin1String("knotifications6/") + applicationName + QLatin1String(".notifyrc"), type: QStandardPaths::GenericDataLocation);
94 d->configFile = retrieve_from_cache(filename: applicationName + QStringLiteral(".notifyrc"));
95}
96
97KNotifyConfig::KNotifyConfig(const KNotifyConfig &other)
98 : d(other.d)
99{
100}
101
102KNotifyConfig &KNotifyConfig::operator=(const KNotifyConfig &other)
103{
104 d = other.d;
105 return *this;
106}
107
108KNotifyConfig::~KNotifyConfig() = default;
109
110QString KNotifyConfig::applicationName() const
111{
112 return d->applicationName;
113}
114
115QString KNotifyConfig::eventId() const
116{
117 return d->eventId;
118}
119
120bool KNotifyConfig::isValid() const
121{
122 const QString group = QLatin1String("Event/") + d->eventId;
123 return d->configFile->hasGroup(group) || d->eventsFile->hasGroup(group);
124}
125
126QString KNotifyConfig::readGlobalEntry(const QString &key) const
127{
128 return d->readEntry(QStringLiteral("Global"), key, path: false);
129}
130
131QString KNotifyConfig::readEntry(const QString &key) const
132{
133 const QString group = QLatin1String("Event/") + d->eventId;
134 return d->readEntry(group, key, path: false);
135}
136
137QString KNotifyConfig::readPathEntry(const QString &key) const
138{
139 const QString group = QLatin1String("Event/") + d->eventId;
140 return d->readEntry(group, key, path: true);
141}
142

source code of knotifications/src/knotifyconfig.cpp