1/*
2 Copyright (C) 2005-2009 by Olivier Goffart <ogoffart at kde.org>
3
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 */
20
21#include "knotifyconfig.h"
22
23#include <ksharedconfig.h>
24#include <kconfiggroup.h>
25#include <kdebug.h>
26#include <kglobal.h>
27#include <QCache>
28#include <QDataStream>
29
30typedef QCache<QString, KSharedConfig::Ptr> ConfigCache;
31K_GLOBAL_STATIC_WITH_ARGS(ConfigCache , static_cache, (15))
32
33static KSharedConfig::Ptr retrieve_from_cache(const QString& filename, const char *resourceType="config")
34{
35 QCache<QString, KSharedConfig::Ptr> &cache = *static_cache;
36 if (cache.contains(filename))
37 return *cache[filename];
38 KSharedConfig::Ptr m = KSharedConfig::openConfig (filename , KConfig::NoGlobals, resourceType );
39 cache.insert(filename, new KSharedConfig::Ptr(m));
40 return m;
41}
42
43void KNotifyConfig::reparseConfiguration()
44{
45 QCache<QString, KSharedConfig::Ptr> &cache = *static_cache;
46 foreach (const QString& filename, cache.keys())
47 (*cache[filename])->reparseConfiguration();
48}
49
50
51KNotifyConfig::KNotifyConfig( const QString & _appname, const ContextList & _contexts, const QString & _eventid )
52 : appname (_appname),
53 eventsfile(retrieve_from_cache(_appname+'/'+_appname + ".notifyrc" , "data" )),
54 configfile(retrieve_from_cache(_appname+QString::fromAscii( ".notifyrc" ))),
55 contexts(_contexts) , eventid(_eventid)
56{
57// kDebug() << appname << " , " << eventid;
58}
59
60KNotifyConfig::~KNotifyConfig()
61{
62}
63
64KNotifyConfig *KNotifyConfig::copy() const
65{
66 KNotifyConfig *config = new KNotifyConfig( appname, contexts, eventid );
67 config->title = title;
68 config->text = text;
69 config->image = KNotifyImage( image.data() );
70 config->timeout = timeout;
71 config->winId = winId;
72 config->actions = actions;
73 config->eventsfile = eventsfile;
74 config->configfile = configfile;
75 // appname, contexts, eventid already done in constructor
76
77 return config;
78}
79
80QString KNotifyConfig::readEntry( const QString & entry, bool path )
81{
82 QPair<QString , QString> context;
83 foreach( context , contexts )
84 {
85 const QString group="Event/" + eventid + '/' + context.first + '/' + context.second;
86 if( configfile->hasGroup( group ) )
87 {
88 KConfigGroup cg(configfile, group);
89 QString p=path ? cg.readPathEntry(entry, QString()) : cg.readEntry(entry,QString());
90 if(!p.isNull())
91 return p;
92 }
93
94 if( eventsfile->hasGroup( group ) )
95 {
96 KConfigGroup cg(eventsfile, group);
97 QString p=path ? cg.readPathEntry(entry, QString()) : cg.readEntry(entry,QString());
98 if(!p.isNull())
99 return p;
100 }
101 }
102// kDebug() << entry << " not found in contexts ";
103 const QString group="Event/" + eventid ;
104 if(configfile->hasGroup( group ) )
105 {
106 KConfigGroup cg(configfile, group);
107 QString p=path ? cg.readPathEntry(entry, QString()) : cg.readEntry(entry,QString());
108 if(!p.isNull())
109 return p;
110 }
111// kDebug() << entry << " not found in config ";
112 if(eventsfile->hasGroup( group ) )
113 {
114 KConfigGroup cg( eventsfile, group);
115 QString p=path ? cg.readPathEntry(entry, QString()) : cg.readEntry(entry, QString());
116 if(!p.isNull())
117 return p;
118 }
119// kDebug() << entry << " not found !!! ";
120
121 return QString();
122}
123
124QImage KNotifyImage::toImage()
125{
126 if (dirty)
127 {
128 if (source.size() > 4) // no way an image can fit in less than 4 bytes
129 {
130 image.loadFromData(source);
131 }
132 dirty = false;
133 }
134 return image;
135}
136