Quassel IRC  Pre-Release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
settings.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2015 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  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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_OS_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 {
63  QObject::connect(notifier(normalizedKey(group, key)), SIGNAL(valueChanged(const QVariant &)),
64  receiver, slot);
65 }
66 
67 
68 void Settings::initAndNotify(const QString &key, QObject *receiver, const char *slot, const QVariant &defaultValue)
69 {
70  notify(key, receiver, slot);
71  emit notifier(normalizedKey(group, key))->valueChanged(localValue(key, defaultValue));
72 }
73 
74 
76 {
77  // we don't cache this value, and we ignore the group
79  uint ver = s.value("Config/Version", 0).toUInt();
80  if (!ver) {
81  // No version, so create one
82  s.setValue("Config/Version", VERSION);
83  return VERSION;
84  }
85  return ver;
86 }
87 
88 
90 {
92  s.beginGroup(group);
93  QStringList res = s.allKeys();
94  s.endGroup();
95  return res;
96 }
97 
98 
99 QStringList Settings::localChildKeys(const QString &rootkey)
100 {
101  QString g;
102  if (rootkey.isEmpty())
103  g = group;
104  else
105  g = QString("%1/%2").arg(group, rootkey);
106 
108  s.beginGroup(g);
109  QStringList res = s.childKeys();
110  s.endGroup();
111  return res;
112 }
113 
114 
115 QStringList Settings::localChildGroups(const QString &rootkey)
116 {
117  QString g;
118  if (rootkey.isEmpty())
119  g = group;
120  else
121  g = QString("%1/%2").arg(group, rootkey);
122 
124  s.beginGroup(g);
125  QStringList res = s.childGroups();
126  s.endGroup();
127  return res;
128 }
129 
130 
131 void Settings::setLocalValue(const QString &key, const QVariant &data)
132 {
133  QString normKey = normalizedKey(group, key);
135  s.setValue(normKey, data);
136  setCacheValue(normKey, data);
137  if (hasNotifier(normKey)) {
138  emit notifier(normKey)->valueChanged(data);
139  }
140 }
141 
142 
143 const QVariant &Settings::localValue(const QString &key, const QVariant &def)
144 {
145  QString normKey = normalizedKey(group, key);
146  if (!isCached(normKey)) {
148  setCacheValue(normKey, s.value(normKey, def));
149  }
150  return cacheValue(normKey);
151 }
152 
153 
154 void Settings::removeLocalKey(const QString &key)
155 {
157  s.beginGroup(group);
158  s.remove(key);
159  s.endGroup();
160  QString normKey = normalizedKey(group, key);
161  if (isCached(normKey))
162  settingsCache.remove(normKey);
163 }