secondsToString() correct year calculation, remove double spaces
[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 <QSettings>
22 #include <QStringList>
23 #include <QDebug>
24
25 #ifdef Q_WS_QWS
26 #include <Qtopia>
27 #endif
28
29 #include "settings.h"
30
31 QHash<QString, QHash<QString, QVariant> > Settings::settingsCache;
32
33 // Settings::Settings(QString group_, QString appName_)
34 //   : group(group_),
35 //     appName(appName_)
36 // {
37
38 // /* we need to call the constructor immediately in order to set the path...
39 // #ifndef Q_WS_QWS
40 //   QSettings(QCoreApplication::organizationName(), applicationName);
41 // #else
42 //   // FIXME sandboxDir() is not currently working correctly...
43 //   //if(Qtopia::sandboxDir().isEmpty()) QSettings();
44 //   //else QSettings(Qtopia::sandboxDir() + "/etc/QuasselIRC.conf", QSettings::NativeFormat);
45 //   // ...so we have to use a workaround:
46 //   QString appPath = QCoreApplication::applicationFilePath();
47 //   if(appPath.startsWith(Qtopia::packagePath())) {
48 //     QString sandboxPath = appPath.left(Qtopia::packagePath().length() + 32);
49 //     QSettings(sandboxPath + "/etc/QuasselIRC.conf", QSettings::IniFormat);
50 //     qDebug() << sandboxPath + "/etc/QuasselIRC.conf";
51 //   } else {
52 //     QSettings(QCoreApplication::organizationName(), applicationName);
53 //   }
54 // #endif
55 // */
56 // }
57
58 QStringList Settings::allLocalKeys() {
59   QSettings s(org(), appName);
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(org(), appName);
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(org(), appName);
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(org(), appName);
96   s.beginGroup(group);
97   s.setValue(key, data);
98   s.endGroup();
99   setCacheValue(group, key, data);
100 }
101
102 const QVariant &Settings::localValue(const QString &key, const QVariant &def) {
103   if(!isCached(group, key)) {
104     QSettings s(org(), appName);
105     s.beginGroup(group);
106     setCacheValue(group, key, s.value(key, def));
107     s.endGroup();
108   }
109   return cacheValue(group, key);
110 }
111
112 void Settings::removeLocalKey(const QString &key) {
113   QSettings s(org(), appName);
114   s.beginGroup(group);
115   s.remove(key);
116   s.endGroup();
117   if(isCached(group, key))
118     settingsCache[group].remove(key);
119 }
120
121
122 // void Settings::setCacheValue(const QString &group, const QString &key, const QVariant &data) {
123 //   settingsCache[group][key] = data;
124 // }
125
126 // const QVariant &Settings::cacheValue(const QString &group, const QString &key) {
127 //   return settingsCache[group][key];
128 // }
129
130 // bool Settings::isCached(const QString &group, const QString &key) {
131 //   return settingsCache.contains(group) && settingsCache[group].contains(key);
132 // }