KNotifications

knotifyconfig.cpp
1 /*
2  SPDX-FileCopyrightText: 2005-2009 Olivier Goffart <ogoffart at kde.org>
3 
4  SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6 
7 #include "knotifyconfig.h"
8 
9 #include <KConfigGroup>
10 #include <QCache>
11 #include <QStandardPaths>
12 
14 Q_GLOBAL_STATIC_WITH_ARGS(ConfigCache, static_cache, (15))
15 
16 static KSharedConfig::Ptr retrieve_from_cache(const QString &filename, QStandardPaths::StandardLocation type = QStandardPaths::GenericConfigLocation)
17 {
18  QCache<QString, KSharedConfig::Ptr> &cache = *static_cache;
19  if (cache.contains(filename)) {
20  return *cache[filename];
21  }
22 
23  KSharedConfig::Ptr m = KSharedConfig::openConfig(filename, KConfig::NoGlobals, type);
24  // also search for event config files in qrc resources
26  m->addConfigSources({QStringLiteral(":/") + filename});
27  }
28  cache.insert(filename, new KSharedConfig::Ptr(m));
29 
30  return m;
31 }
32 
34 {
35  QCache<QString, KSharedConfig::Ptr> &cache = *static_cache;
36  const auto listFiles = cache.keys();
37  for (const QString &filename : listFiles) {
38  (*cache[filename])->reparseConfiguration();
39  }
40 }
41 
42 void KNotifyConfig::reparseSingleConfiguration(const QString &app)
43 {
44  QCache<QString, KSharedConfig::Ptr> &cache = *static_cache;
45  const QString appCacheKey = app + QStringLiteral(".notifyrc");
46  if (cache.contains(appCacheKey)) {
47  (*cache[appCacheKey])->reparseConfiguration();
48  }
49 }
50 
51 KNotifyConfig::KNotifyConfig(const QString &_appname, const ContextList &_contexts, const QString &_eventid)
52  : appname(_appname)
53  , contexts(_contexts)
54  , eventid(_eventid)
55 {
56  eventsfile = retrieve_from_cache(QLatin1String("knotifications5/") + _appname + QLatin1String(".notifyrc"), QStandardPaths::GenericDataLocation);
57  configfile = retrieve_from_cache(_appname + QStringLiteral(".notifyrc"));
58 }
59 
60 KNotifyConfig::~KNotifyConfig()
61 {
62 }
63 
64 KNotifyConfig *KNotifyConfig::copy() const
65 {
67  config->eventsfile = eventsfile;
68  config->configfile = configfile;
69  // appname, contexts, eventid already done in constructor
70  return config;
71 }
72 
73 QString KNotifyConfig::readEntry(const QString &entry, bool path)
74 {
75  for (const QPair<QString, QString> &context : std::as_const(contexts)) {
76  const QString group = QLatin1String("Event/") + eventid + QLatin1Char('/') + context.first + QLatin1Char('/') + context.second;
77 
78  if (configfile->hasGroup(group)) {
79  KConfigGroup cg(configfile, group);
80  QString p = path ? cg.readPathEntry(entry, QString()) : cg.readEntry(entry, QString());
81 
82  if (!p.isNull()) {
83  return p;
84  }
85  }
86 
87  if (eventsfile->hasGroup(group)) {
88  KConfigGroup cg(eventsfile, group);
89  QString p = path ? cg.readPathEntry(entry, QString()) : cg.readEntry(entry, QString());
90 
91  if (!p.isNull()) {
92  return p;
93  }
94  }
95  }
96  // kDebug() << entry << " not found in contexts ";
97  const QString group = QLatin1String("Event/") + eventid;
98 
99  if (configfile->hasGroup(group)) {
100  KConfigGroup cg(configfile, group);
101  QString p = path ? cg.readPathEntry(entry, QString()) : cg.readEntry(entry, QString());
102 
103  if (!p.isNull()) {
104  return p;
105  }
106  }
107  // kDebug() << entry << " not found in config ";
108  if (eventsfile->hasGroup(group)) {
109  KConfigGroup cg(eventsfile, group);
110  QString p = path ? cg.readPathEntry(entry, QString()) : cg.readEntry(entry, QString());
111 
112  if (!p.isNull()) {
113  return p;
114  }
115  }
116  // kDebug() << entry << " not found !!! ";
117 
118  return QString();
119 }
120 
121 QImage KNotifyImage::toImage()
122 {
123  if (dirty) {
124  if (source.size() > 4) { // no way an image can fit in less than 4 bytes
125  image.loadFromData(source);
126  }
127  dirty = false;
128  }
129  return image;
130 }
QString readEntry(const char *key, const char *aDefault=nullptr) const
bool isNull() const const
QString appname
the name of the application that triggered the notification
Definition: knotifyconfig.h:84
QString eventid
the name of the notification
Definition: knotifyconfig.h:95
KSharedConfig::Ptr eventsfile
Definition: knotifyconfig.h:89
QList< Key > keys() const const
bool loadFromData(const uchar *data, int len, const char *format)
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
static void reparseConfiguration()
reparse the cached configs.
QString readEntry(const QString &entry, bool path=false)
KSharedConfigPtr config()
int size() const const
QString readPathEntry(const char *key, const QString &aDefault) const
bool contains(const Key &key) const const
bool insert(const Key &key, T *object, int cost)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon May 8 2023 03:49:15 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.