Don't use a const ref for bool
[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     map["Internal"] = oldmap.value("InternalAccount");
129
130     map["AccountId"] = id.toInt();
131     map["Uuid"] = QUuid::createUuid().toString();
132   }
133
134   return map;
135 }
136
137 void CoreAccountSettings::setAccountValue(const QString &key, const QVariant &value) {
138   if(!Client::currentCoreAccount().isValid())
139     return;
140   setLocalValue(QString("%1/%2/%3").arg(Client::currentCoreAccount().accountId().toInt()).arg(_subgroup).arg(key), value);
141 }
142
143 QVariant CoreAccountSettings::accountValue(const QString &key, const QVariant &def) {
144   if(!Client::currentCoreAccount().isValid())
145     return QVariant();
146   return localValue(QString("%1/%2/%3").arg(Client::currentCoreAccount().accountId().toInt()).arg(_subgroup).arg(key), def);
147 }
148
149 void CoreAccountSettings::setJumpKeyMap(const QHash<int, BufferId> &keyMap) {
150   QVariantMap variants;
151   QHash<int, BufferId>::const_iterator mapIter = keyMap.constBegin();
152   while(mapIter != keyMap.constEnd()) {
153     variants[QString::number(mapIter.key())] = qVariantFromValue(mapIter.value());
154     ++mapIter;
155   }
156   setAccountValue("JumpKeyMap", variants);
157 }
158
159 QHash<int, BufferId> CoreAccountSettings::jumpKeyMap() {
160   QHash<int, BufferId> keyMap;
161   QVariantMap variants = accountValue("JumpKeyMap", QVariant()).toMap();
162   QVariantMap::const_iterator mapIter = variants.constBegin();
163   while(mapIter != variants.constEnd()) {
164     keyMap[mapIter.key().toInt()] = mapIter.value().value<BufferId>();
165     ++mapIter;
166   }
167   return keyMap;
168 }
169
170 void CoreAccountSettings::setBufferViewOverlay(const QSet<int> &viewIds) {
171   QVariantList variants;
172   foreach(int viewId, viewIds) {
173     variants << qVariantFromValue(viewId);
174   }
175   setAccountValue("BufferViewOverlay", variants);
176 }
177
178 QSet<int> CoreAccountSettings::bufferViewOverlay() {
179   QSet<int> viewIds;
180   QVariantList variants = accountValue("BufferViewOverlay").toList();
181   QVariantList::const_iterator iter = variants.constBegin();
182   for(QVariantList::const_iterator iter = variants.constBegin(); iter != variants.constEnd(); iter++) {
183     viewIds << iter->toInt();
184   }
185   return viewIds;
186 }
187
188 void CoreAccountSettings::removeAccount(AccountId id) {
189   removeLocalKey(QString("%1").arg(id.toInt()));
190 }
191
192 void CoreAccountSettings::clearAccounts() {
193   foreach(const QString &key, localChildGroups())
194     removeLocalKey(key);
195 }
196
197 /***********************************************************************************************/
198 // CoreConnectionSettings:
199
200 CoreConnectionSettings::CoreConnectionSettings() : ClientSettings("CoreConnection") {}
201
202 void CoreConnectionSettings::setNetworkDetectionMode(NetworkDetectionMode mode) {
203   setLocalValue("NetworkDetectionMode", mode);
204 }
205
206 CoreConnectionSettings::NetworkDetectionMode CoreConnectionSettings::networkDetectionMode() {
207 #ifdef HAVE_KDE
208   NetworkDetectionMode def = UseSolid;
209 #else
210   NetworkDetectionMode def = UsePingTimeout;
211 #endif
212   return (NetworkDetectionMode)localValue("NetworkDetectionMode", def).toInt();
213 }
214
215 void CoreConnectionSettings::setAutoReconnect(bool autoReconnect) {
216   setLocalValue("AutoReconnect", autoReconnect);
217 }
218
219 bool CoreConnectionSettings::autoReconnect() {
220   return localValue("AutoReconnect", true).toBool();
221 }
222
223 void CoreConnectionSettings::setPingTimeoutInterval(int interval) {
224   setLocalValue("PingTimeoutInterval", interval);
225 }
226
227 int CoreConnectionSettings::pingTimeoutInterval() {
228   return localValue("PingTimeoutInterval", 60).toInt();
229 }
230
231 void CoreConnectionSettings::setReconnectInterval(int interval) {
232   setLocalValue("ReconnectInterval", interval);
233 }
234
235 int CoreConnectionSettings::reconnectInterval() {
236   return localValue("ReconnectInterval", 60).toInt();
237 }
238
239 /***********************************************************************************************/
240 // NotificationSettings:
241
242 NotificationSettings::NotificationSettings() : ClientSettings("Notification") {
243 }
244
245 void NotificationSettings::setHighlightList(const QVariantList &highlightList) {
246   setLocalValue("Highlights/CustomList", highlightList);
247 }
248
249 QVariantList NotificationSettings::highlightList() {
250   return localValue("Highlights/CustomList").toList();
251 }
252
253 void NotificationSettings::setHighlightNick(NotificationSettings::HighlightNickType highlightNickType) {
254   setLocalValue("Highlights/HighlightNick", highlightNickType);
255 }
256
257 NotificationSettings::HighlightNickType NotificationSettings::highlightNick() {
258   return (NotificationSettings::HighlightNickType) localValue("Highlights/HighlightNick", CurrentNick).toInt();
259 }
260
261 void NotificationSettings::setNicksCaseSensitive(bool cs) {
262   setLocalValue("Highlights/NicksCaseSensitive", cs);
263 }
264
265 bool NotificationSettings::nicksCaseSensitive() {
266   return localValue("Highlights/NicksCaseSensitive", false).toBool();
267 }
268
269 // ========================================
270 //  TabCompletionSettings
271 // ========================================
272
273 TabCompletionSettings::TabCompletionSettings() : ClientSettings("TabCompletion") {
274 }
275
276 void TabCompletionSettings::setCompletionSuffix(const QString &suffix) {
277   setLocalValue("CompletionSuffix", suffix);
278 }
279
280 QString TabCompletionSettings::completionSuffix() {
281   return localValue("CompletionSuffix", ": ").toString();
282 }
283
284 void TabCompletionSettings::setAddSpaceMidSentence(bool space) {
285   setLocalValue("AddSpaceMidSentence", space);
286 }
287
288 bool TabCompletionSettings::addSpaceMidSentence() {
289   return localValue("AddSpaceMidSentence", false).toBool();
290 }
291
292 void TabCompletionSettings::setSortMode(SortMode mode) {
293   setLocalValue("SortMode", mode);
294 }
295
296 TabCompletionSettings::SortMode TabCompletionSettings::sortMode() {
297   return static_cast<SortMode>(localValue("SortMode"), LastActivity);
298 }
299
300 void TabCompletionSettings::setCaseSensitivity(Qt::CaseSensitivity cs) {
301   setLocalValue("CaseSensitivity", cs);
302 }
303
304 Qt::CaseSensitivity TabCompletionSettings::caseSensitivity() {
305   return (Qt::CaseSensitivity)localValue("CaseSensitivity", Qt::CaseInsensitive).toInt();
306 }
307
308 void TabCompletionSettings::setUseLastSpokenTo(bool use) {
309   setLocalValue("UseLastSpokenTo", use);
310 }
311
312 bool TabCompletionSettings::useLastSpokenTo() {
313   return localValue("UseLastSpokenTo", false).toBool();
314 }
315
316 // ========================================
317 //  ItemViewSettings
318 // ========================================
319
320 ItemViewSettings::ItemViewSettings(const QString &group) : ClientSettings(group) {
321
322 }
323
324 bool ItemViewSettings::displayTopicInTooltip() {
325   return localValue("DisplayTopicInTooltip", false).toBool();
326 }
327
328 bool ItemViewSettings::mouseWheelChangesBuffer() {
329   return localValue("MouseWheelChangesBuffer", false).toBool();
330 }