eb4fc72a7241ae3947b540a4d284a690159f0eb2
[quassel.git] / src / common / settings.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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 <QCoreApplication>
22 #include <QSettings>
23 #include <QStringList>
24 #include <QDebug>
25
26 #ifdef Q_WS_QWS
27 #include <Qtopia>
28 #endif
29
30 #include "settings.h"
31
32 static QHash<QString, QHash<QString, QVariant> > __settingsCache__;
33
34 Settings::Settings(QString g, QString applicationName)
35
36 #ifdef Q_WS_MAC
37   : QSettings(QCoreApplication::organizationDomain(), applicationName),
38 #else
39   : QSettings(QCoreApplication::organizationName(), applicationName),
40 #endif
41     group(g)
42 {
43
44 /* we need to call the constructor immediately in order to set the path...
45 #ifndef Q_WS_QWS
46   QSettings(QCoreApplication::organizationName(), applicationName);
47 #else
48   // FIXME sandboxDir() is not currently working correctly...
49   //if(Qtopia::sandboxDir().isEmpty()) QSettings();
50   //else QSettings(Qtopia::sandboxDir() + "/etc/QuasselIRC.conf", QSettings::NativeFormat);
51   // ...so we have to use a workaround:
52   QString appPath = QCoreApplication::applicationFilePath();
53   if(appPath.startsWith(Qtopia::packagePath())) {
54     QString sandboxPath = appPath.left(Qtopia::packagePath().length() + 32);
55     QSettings(sandboxPath + "/etc/QuasselIRC.conf", QSettings::IniFormat);
56     qDebug() << sandboxPath + "/etc/QuasselIRC.conf";
57   } else {
58     QSettings(QCoreApplication::organizationName(), applicationName);
59   }
60 #endif
61 */
62 }
63
64 QStringList Settings::allLocalKeys() {
65   beginGroup(group);
66   QStringList res = allKeys();
67   endGroup();
68   return res;
69 }
70
71 QStringList Settings::localChildKeys(const QString &rootkey) {
72   QString g;
73   if(rootkey.isEmpty()) g = group;
74   else g = QString("%1/%2").arg(group, rootkey);
75   beginGroup(g);
76   QStringList res = childKeys();
77   endGroup();
78   return res;
79 }
80
81 QStringList Settings::localChildGroups(const QString &rootkey) {
82   QString g;
83   if(rootkey.isEmpty()) g = group;
84   else g = QString("%1/%2").arg(group, rootkey);
85   beginGroup(g);
86   QStringList res = childGroups();
87   endGroup();
88   return res;
89 }
90
91 void Settings::setLocalValue(const QString &key, const QVariant &data) {
92   beginGroup(group);
93   setValue(key, data);
94   setCacheValue(group, key, data);
95   endGroup();
96 }
97
98 const QVariant &Settings::localValue(const QString &key, const QVariant &def) {
99   if(!isCached(group, key)) {
100     beginGroup(group);
101     setCacheValue(group, key, value(key, def));
102     endGroup();
103   }
104   return cacheValue(group, key);
105 }
106
107 void Settings::removeLocalKey(const QString &key) {
108   beginGroup(group);
109   remove(key);
110   endGroup();
111 }
112
113
114 void Settings::setCacheValue(const QString &group, const QString &key, const QVariant &data) {
115   ::__settingsCache__[group][key] = data;
116 }
117
118 const QVariant &Settings::cacheValue(const QString &group, const QString &key) {
119   return ::__settingsCache__[group][key];
120 }
121
122 bool Settings::isCached(const QString &group, const QString &key) {
123   return ::__settingsCache__.contains(group) && ::__settingsCache__[group].contains(key);
124 }