Load Qt's default translations if available, fixes BR #400
[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
24 #ifdef Q_WS_QWS
25 #include <Qtopia>
26 #endif
27
28 #include "settings.h"
29
30 QHash<QString, QHash<QString, QVariant> > Settings::settingsCache;
31 QHash<QString, QHash<QString, SettingsChangeNotifier *> > Settings::settingsChangeNotifier;
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 void Settings::notify(const QString &key, QObject *receiver, const char *slot) {
59   QObject::connect(notifier(group, key), SIGNAL(valueChanged(const QVariant &)),
60                    receiver, slot);
61 }
62
63 QStringList Settings::allLocalKeys() {
64   QSettings s(org(), appName);
65   s.beginGroup(group);
66   QStringList res = s.allKeys();
67   s.endGroup();
68   return res;
69 }
70
71 QStringList Settings::localChildKeys(const QString &rootkey) {
72   QString g;
73   if(rootkey.isEmpty())
74     g = group;
75   else
76     g = QString("%1/%2").arg(group, rootkey);
77
78   QSettings s(org(), appName);
79   s.beginGroup(g);
80   QStringList res = s.childKeys();
81   s.endGroup();
82   return res;
83 }
84
85 QStringList Settings::localChildGroups(const QString &rootkey) {
86   QString g;
87   if(rootkey.isEmpty())
88     g = group;
89   else
90     g = QString("%1/%2").arg(group, rootkey);
91
92   QSettings s(org(), appName);
93   s.beginGroup(g);
94   QStringList res = s.childGroups();
95   s.endGroup();
96   return res;
97 }
98
99 void Settings::setLocalValue(const QString &key, const QVariant &data) {
100   QSettings s(org(), appName);
101   s.beginGroup(group);
102   s.setValue(key, data);
103   s.endGroup();
104   setCacheValue(group, key, data);
105   if(hasNotifier(group, key)) {
106     emit notifier(group, key)->valueChanged(data);
107   }
108 }
109
110 const QVariant &Settings::localValue(const QString &key, const QVariant &def) {
111   if(!isCached(group, key)) {
112     QSettings s(org(), appName);
113     s.beginGroup(group);
114     setCacheValue(group, key, s.value(key, def));
115     s.endGroup();
116   }
117   return cacheValue(group, key);
118 }
119
120 void Settings::removeLocalKey(const QString &key) {
121   QSettings s(org(), appName);
122   s.beginGroup(group);
123   s.remove(key);
124   s.endGroup();
125   if(isCached(group, key))
126     settingsCache[group].remove(key);
127 }