KNotifications

knotifyconfig.cpp
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
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(key, QString()) : cg.readEntry(key, 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(key, QString()) : cg.readEntry(key, 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(filename)) {
56 return *cache[filename];
57 }
58
59 KSharedConfig::Ptr m = KSharedConfig::openConfig(filename, KConfig::NoGlobals, type);
60 // also search for event config files in qrc resources
62 m->addConfigSources({QStringLiteral(":/") + filename});
63 }
64 cache.insert(filename, new KSharedConfig::Ptr(m));
65
66 return m;
67}
68
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(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(QLatin1String("knotifications6/") + applicationName + QLatin1String(".notifyrc"), QStandardPaths::GenericDataLocation);
94 d->configFile = retrieve_from_cache(applicationName + QStringLiteral(".notifyrc"));
95}
96
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
111{
112 return d->applicationName;
113}
114
116{
117 return d->eventId;
118}
119
121{
122 const QString group = QLatin1String("Event/") + d->eventId;
123 return d->configFile->hasGroup(group) || d->eventsFile->hasGroup(group);
124}
125
127{
128 return d->readEntry(QStringLiteral("Global"), key, false);
129}
130
132{
133 const QString group = QLatin1String("Event/") + d->eventId;
134 return d->readEntry(group, key, false);
135}
136
138{
139 const QString group = QLatin1String("Event/") + d->eventId;
140 return d->readEntry(group, key, true);
141}
Represent the configuration for an event.
QString readEntry(const QString &key) const
QString applicationName() const
the name of the application that triggered the notification
QString readGlobalEntry(const QString &key) const
QString eventId() const
the name of the notification
QString readPathEntry(const QString &key) const
KNotifyConfig(const QString &applicationName, const QString &eventId)
Creates a notify config for the given application name and event id.
static void reparseConfiguration()
reparse the cached configs.
bool isValid() const
Whether there exists an event with the given id under the given application name.
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
QString path(const QString &relativePath)
bool contains(const Key &key) const const
bool insert(const Key &key, T *object, qsizetype cost)
QList< Key > keys() const const
bool isNull() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:12:53 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.