Make monolithic client work again
[quassel.git] / src / client / clientsettings.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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 <QStringList>
22
23
24 #include "clientsettings.h"
25
26 #include <QHostAddress>
27 #ifdef HAVE_SSL
28 #include <QSslSocket>
29 #endif
30
31
32 #include "client.h"
33 #include "quassel.h"
34
35 ClientSettings::ClientSettings(QString g) : Settings(g, Quassel::buildInfo().clientApplicationName) {
36 }
37
38 ClientSettings::~ClientSettings() {
39 }
40
41 /***********************************************************************************************/
42
43 CoreAccountSettings::CoreAccountSettings(const QString &subgroup)
44   : ClientSettings("CoreAccounts"),
45     _subgroup(subgroup)
46 {
47 }
48
49 void CoreAccountSettings::notify(const QString &key, QObject *receiver, const char *slot) {
50   ClientSettings::notify(QString("%1/%2/%3").arg(Client::currentCoreAccount().accountId().toInt()).arg(_subgroup).arg(key), receiver, slot);
51 }
52
53 QList<AccountId> CoreAccountSettings::knownAccounts() {
54   QList<AccountId> ids;
55   foreach(const QString &key, localChildGroups()) {
56     AccountId acc = key.toInt();
57     if(acc.isValid())
58       ids << acc;
59   }
60   return ids;
61 }
62
63 AccountId CoreAccountSettings::lastAccount() {
64   return localValue("LastAccount", 0).toInt();
65 }
66
67 void CoreAccountSettings::setLastAccount(AccountId account) {
68   setLocalValue("LastAccount", account.toInt());
69 }
70
71 AccountId CoreAccountSettings::autoConnectAccount() {
72   return localValue("AutoConnectAccount", 0).toInt();
73 }
74
75 void CoreAccountSettings::setAutoConnectAccount(AccountId account) {
76   setLocalValue("AutoConnectAccount", account.toInt());
77 }
78
79 bool CoreAccountSettings::autoConnectOnStartup() {
80   return localValue("AutoConnectOnStartup", false).toBool();
81 }
82
83 void CoreAccountSettings::setAutoConnectOnStartup(bool b) {
84   setLocalValue("AutoConnectOnStartup", b);
85 }
86
87 bool CoreAccountSettings::autoConnectToFixedAccount() {
88   return localValue("AutoConnectToFixedAccount", false).toBool();
89 }
90
91 void CoreAccountSettings::setAutoConnectToFixedAccount(bool b) {
92   setLocalValue("AutoConnectToFixedAccount", b);
93 }
94
95 void CoreAccountSettings::storeAccountData(AccountId id, const QVariantMap &data) {
96   QString base = QString::number(id.toInt());
97   foreach(const QString &key, data.keys()) {
98     setLocalValue(base + "/" + key, data.value(key));
99   }
100
101   // FIXME Migration from 0.5 -> 0.6
102   removeLocalKey(QString("%1/Connection").arg(base));
103 }
104
105 QVariantMap CoreAccountSettings::retrieveAccountData(AccountId id) {
106   QVariantMap map;
107   QString base = QString::number(id.toInt());
108   foreach(const QString &key, localChildKeys(base)) {
109     map[key] = localValue(base + "/" + key);
110   }
111
112   // FIXME Migration from 0.5 -> 0.6
113   if(!map.contains("Uuid") && map.contains("Connection")) {
114     QVariantMap oldmap = map.value("Connection").toMap();
115     map["AccountName"] = oldmap.value("AccountName");
116     map["HostName"] = oldmap.value("Host");
117     map["Port"] = oldmap.value("Port");
118     map["User"] = oldmap.value("User");
119     map["Password"] = oldmap.value("Password");
120     map["StorePassword"] = oldmap.value("RememberPasswd");
121     map["UseSSL"] = oldmap.value("useSsl");
122     map["UseProxy"] = oldmap.value("useProxy");
123     map["ProxyHostName"] = oldmap.value("proxyHost");
124     map["ProxyPort"] = oldmap.value("proxyPort");
125     map["ProxyUser"] = oldmap.value("proxyUser");
126     map["ProxyPassword"] = oldmap.value("proxyPassword");
127     map["ProxyType"] = oldmap.value("proxyType");
128
129     map["AccountId"] = id.toInt();
130     map["Uuid"] = QUuid::createUuid().toString();
131   }
132
133   return map;
134 }
135
136 void CoreAccountSettings::setAccountValue(const QString &key, const QVariant &value) {
137   if(!Client::currentCoreAccount().isValid())
138     return;
139   setLocalValue(QString("%1/%2/%3").arg(Client::currentCoreAccount().accountId().toInt()).arg(_subgroup).arg(key), value);
140 }
141
142 QVariant CoreAccountSettings::accountValue(const QString &key, const QVariant &def) {
143   if(!Client::currentCoreAccount().isValid())
144     return QVariant();
145   return localValue(QString("%1/%2/%3").arg(Client::currentCoreAccount().accountId().toInt()).arg(_subgroup).arg(key), def);
146 }
147
148 void CoreAccountSettings::setJumpKeyMap(const QHash<int, BufferId> &keyMap) {
149   QVariantMap variants;
150   QHash<int, BufferId>::const_iterator mapIter = keyMap.constBegin();
151   while(mapIter != keyMap.constEnd()) {
152     variants[QString::number(mapIter.key())] = qVariantFromValue(mapIter.value());
153     ++mapIter;
154   }
155   setAccountValue("JumpKeyMap", variants);
156 }
157
158 QHash<int, BufferId> CoreAccountSettings::jumpKeyMap() {
159   QHash<int, BufferId> keyMap;
160   QVariantMap variants = accountValue("JumpKeyMap", QVariant()).toMap();
161   QVariantMap::const_iterator mapIter = variants.constBegin();
162   while(mapIter != variants.constEnd()) {
163     keyMap[mapIter.key().toInt()] = mapIter.value().value<BufferId>();
164     ++mapIter;
165   }
166   return keyMap;
167 }
168
169 void CoreAccountSettings::removeAccount(AccountId id) {
170   removeLocalKey(QString("%1").arg(id.toInt()));
171 }
172
173 void CoreAccountSettings::clearAccounts() {
174   foreach(const QString &key, localChildGroups())
175     removeLocalKey(key);
176 }
177
178 /***********************************************************************************************/
179 // NotificationSettings:
180
181 NotificationSettings::NotificationSettings() : ClientSettings("Notification") {
182 }
183
184 void NotificationSettings::setHighlightList(const QVariantList &highlightList) {
185   setLocalValue("Highlights/CustomList", highlightList);
186 }
187
188 QVariantList NotificationSettings::highlightList() {
189   return localValue("Highlights/CustomList").toList();
190 }
191
192 void NotificationSettings::setHighlightNick(NotificationSettings::HighlightNickType highlightNickType) {
193   setLocalValue("Highlights/HighlightNick", highlightNickType);
194 }
195
196 NotificationSettings::HighlightNickType NotificationSettings::highlightNick() {
197   return (NotificationSettings::HighlightNickType) localValue("Highlights/HighlightNick", CurrentNick).toInt();
198 }
199
200 void NotificationSettings::setNicksCaseSensitive(bool cs) {
201   setLocalValue("Highlights/NicksCaseSensitive", cs);
202 }
203
204 bool NotificationSettings::nicksCaseSensitive() {
205   return localValue("Highlights/NicksCaseSensitive", false).toBool();
206 }
207
208
209 // ========================================
210 //  KnownHostsSettings
211 // ========================================
212 KnownHostsSettings::KnownHostsSettings()
213   : ClientSettings("KnownHosts")
214 {
215 }
216
217 QByteArray KnownHostsSettings::knownDigest(const QHostAddress &address) {
218   return localValue(address.toString(), QByteArray()).toByteArray();
219 }
220
221 void KnownHostsSettings::saveKnownHost(const QHostAddress &address, const QByteArray &certDigest) {
222   setLocalValue(address.toString(), certDigest);
223 }
224
225 bool KnownHostsSettings::isKnownHost(const QHostAddress &address, const QByteArray &certDigest) {
226   return certDigest == localValue(address.toString(), QByteArray()).toByteArray();
227 }
228
229 #ifdef HAVE_SSL
230 QByteArray KnownHostsSettings::knownDigest(const QSslSocket *socket) {
231   return knownDigest(socket->peerAddress());
232 }
233
234 void KnownHostsSettings::saveKnownHost(const QSslSocket *socket) {
235   Q_ASSERT(socket);
236   saveKnownHost(socket->peerAddress(), socket->peerCertificate().digest());
237 }
238
239 bool KnownHostsSettings::isKnownHost(const QSslSocket *socket) {
240   Q_ASSERT(socket);
241   return isKnownHost(socket->peerAddress(), socket->peerCertificate().digest());
242 }
243 #endif
244
245
246 // ========================================
247 //  TabCompletionSettings
248 // ========================================
249
250 TabCompletionSettings::TabCompletionSettings() : ClientSettings("TabCompletion") {
251 }
252
253 void TabCompletionSettings::setCompletionSuffix(const QString &suffix) {
254   setLocalValue("CompletionSuffix", suffix);
255 }
256
257 QString TabCompletionSettings::completionSuffix() {
258   return localValue("CompletionSuffix", ": ").toString();
259 }
260
261 void TabCompletionSettings::setSortMode(SortMode mode) {
262   setLocalValue("SortMode", mode);
263 }
264
265 TabCompletionSettings::SortMode TabCompletionSettings::sortMode() {
266   return static_cast<SortMode>(localValue("SortMode"), LastActivity);
267 }
268
269 void TabCompletionSettings::setCaseSensitivity(Qt::CaseSensitivity cs) {
270   setLocalValue("CaseSensitivity", cs);
271 }
272
273 Qt::CaseSensitivity TabCompletionSettings::caseSensitivity() {
274   return (Qt::CaseSensitivity)localValue("CaseSensitivity", Qt::CaseInsensitive).toInt();
275 }
276
277 void TabCompletionSettings::setUseLastSpokenTo(bool use) {
278   setLocalValue("UseLastSpokenTo", use);
279 }
280
281 bool TabCompletionSettings::useLastSpokenTo() {
282   return localValue("UseLastSpokenTo", false).toBool();
283 }
284
285 // ========================================
286 //  ItemViewSettings
287 // ========================================
288
289 ItemViewSettings::ItemViewSettings(const QString &group) : ClientSettings(group) {
290
291 }
292
293 bool ItemViewSettings::displayTopicInTooltip() {
294   return localValue("DisplayTopicInTooltip", false).toBool();
295 }
296
297 bool ItemViewSettings::mouseWheelChangesBuffer() {
298   return localValue("MouseWheelChangesBuffer", false).toBool();
299 }