more default nick and realname improvements
[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, QHash<QString, QVariant> > Settings::settingsCache;
28 QHash<QString, 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(group, key), SIGNAL(valueChanged(const QVariant &)),
63                    receiver, slot);
64 }
65
66 uint Settings::version() {
67   // we don't cache this value, and we ignore the group
68   create_qsettings;
69   uint ver = s.value("Config/Version", 0).toUInt();
70   if(!ver) {
71     // No version, so create one
72     s.setValue("Config/Version", VERSION);
73     return VERSION;
74   }
75   return ver;
76 }
77
78 QStringList Settings::allLocalKeys() {
79   create_qsettings;
80   s.beginGroup(group);
81   QStringList res = s.allKeys();
82   s.endGroup();
83   return res;
84 }
85
86 QStringList Settings::localChildKeys(const QString &rootkey) {
87   QString g;
88   if(rootkey.isEmpty())
89     g = group;
90   else
91     g = QString("%1/%2").arg(group, rootkey);
92
93   create_qsettings;
94   s.beginGroup(g);
95   QStringList res = s.childKeys();
96   s.endGroup();
97   return res;
98 }
99
100 QStringList Settings::localChildGroups(const QString &rootkey) {
101   QString g;
102   if(rootkey.isEmpty())
103     g = group;
104   else
105     g = QString("%1/%2").arg(group, rootkey);
106
107   create_qsettings;
108   s.beginGroup(g);
109   QStringList res = s.childGroups();
110   s.endGroup();
111   return res;
112 }
113
114 void Settings::setLocalValue(const QString &key, const QVariant &data) {
115   create_qsettings;
116   s.beginGroup(group);
117   s.setValue(key, data);
118   s.endGroup();
119   setCacheValue(group, key, data);
120   if(hasNotifier(group, key)) {
121     emit notifier(group, key)->valueChanged(data);
122   }
123 }
124
125 const QVariant &Settings::localValue(const QString &key, const QVariant &def) {
126   if(!isCached(group, key)) {
127     create_qsettings;
128     s.beginGroup(group);
129     setCacheValue(group, key, s.value(key, def));
130     s.endGroup();
131   }
132   return cacheValue(group, key);
133 }
134
135 void Settings::removeLocalKey(const QString &key) {
136   create_qsettings;
137   s.beginGroup(group);
138   s.remove(key);
139   s.endGroup();
140   if(isCached(group, key))
141     settingsCache[group].remove(key);
142 }