only sending heartbeat on socket connections
[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 QHash<QString, QHash<QString, SettingsChangeNotifier *> > Settings::settingsChangeNotifier;
33
34 // Settings::Settings(QString group_, QString appName_)
35 //   : group(group_),
36 //     appName(appName_)
37 // {
38
39 // /* we need to call the constructor immediately in order to set the path...
40 // #ifndef Q_WS_QWS
41 //   QSettings(QCoreApplication::organizationName(), applicationName);
42 // #else
43 //   // FIXME sandboxDir() is not currently working correctly...
44 //   //if(Qtopia::sandboxDir().isEmpty()) QSettings();
45 //   //else QSettings(Qtopia::sandboxDir() + "/etc/QuasselIRC.conf", QSettings::NativeFormat);
46 //   // ...so we have to use a workaround:
47 //   QString appPath = QCoreApplication::applicationFilePath();
48 //   if(appPath.startsWith(Qtopia::packagePath())) {
49 //     QString sandboxPath = appPath.left(Qtopia::packagePath().length() + 32);
50 //     QSettings(sandboxPath + "/etc/QuasselIRC.conf", QSettings::IniFormat);
51 //     qDebug() << sandboxPath + "/etc/QuasselIRC.conf";
52 //   } else {
53 //     QSettings(QCoreApplication::organizationName(), applicationName);
54 //   }
55 // #endif
56 // */
57 // }
58
59 void Settings::notify(const QString &key, QObject *receiver, const char *slot) {
60   QObject::connect(notifier(group, key), SIGNAL(valueChanged(const QVariant &)),
61                    receiver, slot);
62 }
63
64 QStringList Settings::allLocalKeys() {
65   QSettings s(org(), appName);
66   s.beginGroup(group);
67   QStringList res = s.allKeys();
68   s.endGroup();
69   return res;
70 }
71
72 QStringList Settings::localChildKeys(const QString &rootkey) {
73   QString g;
74   if(rootkey.isEmpty())
75     g = group;
76   else
77     g = QString("%1/%2").arg(group, rootkey);
78
79   QSettings s(org(), appName);
80   s.beginGroup(g);
81   QStringList res = s.childKeys();
82   s.endGroup();
83   return res;
84 }
85
86 QStringList Settings::localChildGroups(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   QSettings s(org(), appName);
94   s.beginGroup(g);
95   QStringList res = s.childGroups();
96   s.endGroup();
97   return res;
98 }
99
100 void Settings::setLocalValue(const QString &key, const QVariant &data) {
101   QSettings s(org(), appName);
102   s.beginGroup(group);
103   s.setValue(key, data);
104   s.endGroup();
105   setCacheValue(group, key, data);
106   if(hasNotifier(group, key)) {
107     emit notifier(group, key)->valueChanged(data);
108   }
109 }
110
111 const QVariant &Settings::localValue(const QString &key, const QVariant &def) {
112   if(!isCached(group, key)) {
113     QSettings s(org(), appName);
114     s.beginGroup(group);
115     setCacheValue(group, key, s.value(key, def));
116     s.endGroup();
117   }
118   return cacheValue(group, key);
119 }
120
121 void Settings::removeLocalKey(const QString &key) {
122   QSettings s(org(), appName);
123   s.beginGroup(group);
124   s.remove(key);
125   s.endGroup();
126   if(isCached(group, key))
127     settingsCache[group].remove(key);
128 }