Improve data directory search
[quassel.git] / src / common / settings.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
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 of the License, or     *
8  *   (at your option) version 3.                                           *
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                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include <QStringList>
22
23 #include "settings.h"
24
25 QHash<QString, QHash<QString, QVariant> > Settings::settingsCache;
26 QHash<QString, QHash<QString, SettingsChangeNotifier *> > Settings::settingsChangeNotifier;
27
28 // Settings::Settings(QString group_, QString appName_)
29 //   : group(group_),
30 //     appName(appName_)
31 // {
32
33 // /* we need to call the constructor immediately in order to set the path...
34 // #ifndef Q_WS_QWS
35 //   QSettings(QCoreApplication::organizationName(), applicationName);
36 // #else
37 //   // FIXME sandboxDir() is not currently working correctly...
38 //   //if(Qtopia::sandboxDir().isEmpty()) QSettings();
39 //   //else QSettings(Qtopia::sandboxDir() + "/etc/QuasselIRC.conf", QSettings::NativeFormat);
40 //   // ...so we have to use a workaround:
41 //   QString appPath = QCoreApplication::applicationFilePath();
42 //   if(appPath.startsWith(Qtopia::packagePath())) {
43 //     QString sandboxPath = appPath.left(Qtopia::packagePath().length() + 32);
44 //     QSettings(sandboxPath + "/etc/QuasselIRC.conf", QSettings::IniFormat);
45 //     qDebug() << sandboxPath + "/etc/QuasselIRC.conf";
46 //   } else {
47 //     QSettings(QCoreApplication::organizationName(), applicationName);
48 //   }
49 // #endif
50 // */
51 // }
52
53 void Settings::notify(const QString &key, QObject *receiver, const char *slot) {
54   QObject::connect(notifier(group, key), SIGNAL(valueChanged(const QVariant &)),
55                    receiver, slot);
56 }
57
58 QStringList Settings::allLocalKeys() {
59   QSettings s(fileName(), format());
60   s.beginGroup(group);
61   QStringList res = s.allKeys();
62   s.endGroup();
63   return res;
64 }
65
66 QStringList Settings::localChildKeys(const QString &rootkey) {
67   QString g;
68   if(rootkey.isEmpty())
69     g = group;
70   else
71     g = QString("%1/%2").arg(group, rootkey);
72
73   QSettings s(fileName(), format());
74   s.beginGroup(g);
75   QStringList res = s.childKeys();
76   s.endGroup();
77   return res;
78 }
79
80 QStringList Settings::localChildGroups(const QString &rootkey) {
81   QString g;
82   if(rootkey.isEmpty())
83     g = group;
84   else
85     g = QString("%1/%2").arg(group, rootkey);
86
87   QSettings s(fileName(), format());
88   s.beginGroup(g);
89   QStringList res = s.childGroups();
90   s.endGroup();
91   return res;
92 }
93
94 void Settings::setLocalValue(const QString &key, const QVariant &data) {
95   QSettings s(fileName(), format());
96   s.beginGroup(group);
97   s.setValue(key, data);
98   s.endGroup();
99   setCacheValue(group, key, data);
100   if(hasNotifier(group, key)) {
101     emit notifier(group, key)->valueChanged(data);
102   }
103 }
104
105 const QVariant &Settings::localValue(const QString &key, const QVariant &def) {
106   if(!isCached(group, key)) {
107     QSettings s(fileName(), format());
108     s.beginGroup(group);
109     setCacheValue(group, key, s.value(key, def));
110     s.endGroup();
111   }
112   return cacheValue(group, key);
113 }
114
115 void Settings::removeLocalKey(const QString &key) {
116   QSettings s(fileName(), format());
117   s.beginGroup(group);
118   s.remove(key);
119   s.endGroup();
120   if(isCached(group, key))
121     settingsCache[group].remove(key);
122 }