Properly handle QGenericReturnArgument
[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 const int VERSION = 1;
26
27 QHash<QString, QVariant> Settings::settingsCache;
28 QHash<QString, SettingsChangeNotifier *> Settings::settingsChangeNotifier;
29
30 #ifdef Q_WS_MAC
31 #  define create_qsettings QSettings s(QCoreApplication::organizationDomain(), appName)
32 #else
33 #  define create_qsettings QSettings s(fileName(), format())
34 #endif
35
36 // Settings::Settings(QString group_, QString appName_)
37 //   : group(group_),
38 //     appName(appName_)
39 // {
40
41 // /* we need to call the constructor immediately in order to set the path...
42 // #ifndef Q_WS_QWS
43 //   QSettings(QCoreApplication::organizationName(), applicationName);
44 // #else
45 //   // FIXME sandboxDir() is not currently working correctly...
46 //   //if(Qtopia::sandboxDir().isEmpty()) QSettings();
47 //   //else QSettings(Qtopia::sandboxDir() + "/etc/QuasselIRC.conf", QSettings::NativeFormat);
48 //   // ...so we have to use a workaround:
49 //   QString appPath = QCoreApplication::applicationFilePath();
50 //   if(appPath.startsWith(Qtopia::packagePath())) {
51 //     QString sandboxPath = appPath.left(Qtopia::packagePath().length() + 32);
52 //     QSettings(sandboxPath + "/etc/QuasselIRC.conf", QSettings::IniFormat);
53 //     qDebug() << sandboxPath + "/etc/QuasselIRC.conf";
54 //   } else {
55 //     QSettings(QCoreApplication::organizationName(), applicationName);
56 //   }
57 // #endif
58 // */
59 // }
60
61 void Settings::notify(const QString &key, QObject *receiver, const char *slot) {
62   QObject::connect(notifier(normalizedKey(group, key)), SIGNAL(valueChanged(const QVariant &)),
63                    receiver, slot);
64 }
65
66 void Settings::initAndNotify(const QString &key, QObject *receiver, const char *slot, const QVariant &defaultValue) {
67   notify(key, receiver, slot);
68   emit notifier(normalizedKey(group, key))->valueChanged(localValue(key, defaultValue));
69 }
70
71 uint Settings::version() {
72   // we don't cache this value, and we ignore the group
73   create_qsettings;
74   uint ver = s.value("Config/Version", 0).toUInt();
75   if(!ver) {
76     // No version, so create one
77     s.setValue("Config/Version", VERSION);
78     return VERSION;
79   }
80   return ver;
81 }
82
83 QStringList Settings::allLocalKeys() {
84   create_qsettings;
85   s.beginGroup(group);
86   QStringList res = s.allKeys();
87   s.endGroup();
88   return res;
89 }
90
91 QStringList Settings::localChildKeys(const QString &rootkey) {
92   QString g;
93   if(rootkey.isEmpty())
94     g = group;
95   else
96     g = QString("%1/%2").arg(group, rootkey);
97
98   create_qsettings;
99   s.beginGroup(g);
100   QStringList res = s.childKeys();
101   s.endGroup();
102   return res;
103 }
104
105 QStringList Settings::localChildGroups(const QString &rootkey) {
106   QString g;
107   if(rootkey.isEmpty())
108     g = group;
109   else
110     g = QString("%1/%2").arg(group, rootkey);
111
112   create_qsettings;
113   s.beginGroup(g);
114   QStringList res = s.childGroups();
115   s.endGroup();
116   return res;
117 }
118
119 void Settings::setLocalValue(const QString &key, const QVariant &data) {
120   QString normKey = normalizedKey(group, key);
121   create_qsettings;
122   s.setValue(normKey, data);
123   setCacheValue(normKey, data);
124   if(hasNotifier(normKey)) {
125     emit notifier(normKey)->valueChanged(data);
126   }
127 }
128
129 const QVariant &Settings::localValue(const QString &key, const QVariant &def) {
130   QString normKey = normalizedKey(group, key);
131   if(!isCached(normKey)) {
132     create_qsettings;
133     setCacheValue(normKey, s.value(normKey, def));
134   }
135   return cacheValue(normKey);
136 }
137
138 void Settings::removeLocalKey(const QString &key) {
139   create_qsettings;
140   s.beginGroup(group);
141   s.remove(key);
142   s.endGroup();
143   QString normKey = normalizedKey(group, key);
144   if(isCached(normKey))
145     settingsCache.remove(normKey);
146 }