File indexing completed on 2024-04-28 11:43:45

0001 /*
0002     SPDX-FileCopyrightText: 2005-2009 Olivier Goffart <ogoffart at kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "knotifyconfig.h"
0008 
0009 #include <KConfigGroup>
0010 #include <QCache>
0011 #include <QStandardPaths>
0012 
0013 typedef QCache<QString, KSharedConfig::Ptr> ConfigCache;
0014 Q_GLOBAL_STATIC_WITH_ARGS(ConfigCache, static_cache, (15))
0015 
0016 static KSharedConfig::Ptr retrieve_from_cache(const QString &filename, QStandardPaths::StandardLocation type = QStandardPaths::GenericConfigLocation)
0017 {
0018     QCache<QString, KSharedConfig::Ptr> &cache = *static_cache;
0019     if (cache.contains(filename)) {
0020         return *cache[filename];
0021     }
0022 
0023     KSharedConfig::Ptr m = KSharedConfig::openConfig(filename, KConfig::NoGlobals, type);
0024     // also search for event config files in qrc resources
0025     if (type == QStandardPaths::GenericDataLocation) {
0026         m->addConfigSources({QStringLiteral(":/") + filename});
0027     }
0028     cache.insert(filename, new KSharedConfig::Ptr(m));
0029 
0030     return m;
0031 }
0032 
0033 void KNotifyConfig::reparseConfiguration()
0034 {
0035     QCache<QString, KSharedConfig::Ptr> &cache = *static_cache;
0036     const auto listFiles = cache.keys();
0037     for (const QString &filename : listFiles) {
0038         (*cache[filename])->reparseConfiguration();
0039     }
0040 }
0041 
0042 void KNotifyConfig::reparseSingleConfiguration(const QString &app)
0043 {
0044     QCache<QString, KSharedConfig::Ptr> &cache = *static_cache;
0045     const QString appCacheKey = app + QStringLiteral(".notifyrc");
0046     if (cache.contains(appCacheKey)) {
0047         (*cache[appCacheKey])->reparseConfiguration();
0048     }
0049 }
0050 
0051 KNotifyConfig::KNotifyConfig(const QString &_appname, const ContextList &_contexts, const QString &_eventid)
0052     : appname(_appname)
0053     , contexts(_contexts)
0054     , eventid(_eventid)
0055 {
0056     eventsfile = retrieve_from_cache(QLatin1String("knotifications5/") + _appname + QLatin1String(".notifyrc"), QStandardPaths::GenericDataLocation);
0057     configfile = retrieve_from_cache(_appname + QStringLiteral(".notifyrc"));
0058 }
0059 
0060 KNotifyConfig::~KNotifyConfig()
0061 {
0062 }
0063 
0064 KNotifyConfig *KNotifyConfig::copy() const
0065 {
0066     KNotifyConfig *config = new KNotifyConfig(appname, contexts, eventid);
0067     config->eventsfile = eventsfile;
0068     config->configfile = configfile;
0069     // appname, contexts, eventid already done in constructor
0070     return config;
0071 }
0072 
0073 QString KNotifyConfig::readEntry(const QString &entry, bool path)
0074 {
0075     for (const QPair<QString, QString> &context : std::as_const(contexts)) {
0076         const QString group = QLatin1String("Event/") + eventid + QLatin1Char('/') + context.first + QLatin1Char('/') + context.second;
0077 
0078         if (configfile->hasGroup(group)) {
0079             KConfigGroup cg(configfile, group);
0080             QString p = path ? cg.readPathEntry(entry, QString()) : cg.readEntry(entry, QString());
0081 
0082             if (!p.isNull()) {
0083                 return p;
0084             }
0085         }
0086 
0087         if (eventsfile->hasGroup(group)) {
0088             KConfigGroup cg(eventsfile, group);
0089             QString p = path ? cg.readPathEntry(entry, QString()) : cg.readEntry(entry, QString());
0090 
0091             if (!p.isNull()) {
0092                 return p;
0093             }
0094         }
0095     }
0096     //    kDebug() << entry << " not found in contexts ";
0097     const QString group = QLatin1String("Event/") + eventid;
0098 
0099     if (configfile->hasGroup(group)) {
0100         KConfigGroup cg(configfile, group);
0101         QString p = path ? cg.readPathEntry(entry, QString()) : cg.readEntry(entry, QString());
0102 
0103         if (!p.isNull()) {
0104             return p;
0105         }
0106     }
0107     //    kDebug() << entry << " not found in config ";
0108     if (eventsfile->hasGroup(group)) {
0109         KConfigGroup cg(eventsfile, group);
0110         QString p = path ? cg.readPathEntry(entry, QString()) : cg.readEntry(entry, QString());
0111 
0112         if (!p.isNull()) {
0113             return p;
0114         }
0115     }
0116     //    kDebug() << entry << " not found !!! ";
0117 
0118     return QString();
0119 }
0120 
0121 QImage KNotifyImage::toImage()
0122 {
0123     if (dirty) {
0124         if (source.size() > 4) { // no way an image can fit in less than 4 bytes
0125             image.loadFromData(source);
0126         }
0127         dirty = false;
0128     }
0129     return image;
0130 }