Fix crash if key bindings specified in profile cannot be found. Improve
[personal-kdebase.git] / runtime / knotify / knotifyconfig.cpp
blob240ca1fcf20772181c04d35d5d00813b5512c45d
1 /*
2 Copyright (C) 2005-2006 by Olivier Goffart <ogoffart at kde.org>
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.
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.
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.
21 #include "knotifyconfig.h"
23 #include <ksharedconfig.h>
24 #include <kconfiggroup.h>
25 #include <kdebug.h>
26 #include <kglobal.h>
27 #include <QCache>
29 typedef QCache<QString, KSharedConfig::Ptr> ConfigCache;
30 K_GLOBAL_STATIC_WITH_ARGS(ConfigCache , static_cache, (15))
32 static KSharedConfig::Ptr retrieve_from_cache(const QString& filename, const char *resourceType="config")
34 QCache<QString, KSharedConfig::Ptr> &cache = *static_cache;
35 if (cache.contains(filename))
36 return *cache[filename];
37 KSharedConfig::Ptr m = KSharedConfig::openConfig (filename , KConfig::NoGlobals, resourceType );
38 cache.insert(filename, new KSharedConfig::Ptr(m));
39 return m;
42 void KNotifyConfig::clearCache()
44 static_cache->clear();
48 KNotifyConfig::KNotifyConfig( const QString & _appname, const ContextList & _contexts, const QString & _eventid )
49 : appname (_appname),
50 eventsfile(retrieve_from_cache(_appname+'/'+_appname + ".notifyrc" , "data" )),
51 configfile(retrieve_from_cache(_appname+QString::fromAscii( ".notifyrc" ))),
52 contexts(_contexts) , eventid(_eventid)
54 // kDebug(300) << appname << " , " << eventid;
57 KNotifyConfig::~KNotifyConfig()
61 QString KNotifyConfig::readEntry( const QString & entry, bool path )
63 QPair<QString , QString> context;
64 foreach( context , contexts )
66 const QString group="Event/" + eventid + '/' + context.first + '/' + context.second;
67 if( configfile->hasGroup( group ) )
69 KConfigGroup cg(configfile, group);
70 QString p=path ? cg.readPathEntry(entry, QString()) : cg.readEntry(entry,QString());
71 if(!p.isNull())
72 return p;
75 // kDebug(300) << entry << " not found in contexts ";
76 const QString group="Event/" + eventid ;
77 if(configfile->hasGroup( group ) )
79 KConfigGroup cg(configfile, group);
80 QString p=path ? cg.readPathEntry(entry, QString()) : cg.readEntry(entry,QString());
81 if(!p.isNull())
82 return p;
84 // kDebug(300) << entry << " not found in config ";
85 if(eventsfile->hasGroup( group ) )
87 KConfigGroup cg( eventsfile, group);
88 QString p=path ? cg.readPathEntry(entry, QString()) : cg.readEntry(entry, QString());
89 if(!p.isNull())
90 return p;
92 // kDebug(300) << entry << " not found !!! ";
94 return QString();