87c0fc55261db4b55299c0f4ad9922b31d7eb6e6
[quassel.git] / src / client / clientsettings.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include <utility>
22
23 #include <QStringList>
24
25 #include "clientsettings.h"
26
27 #include <QHostAddress>
28 #ifdef HAVE_SSL
29 #include <QSslSocket>
30 #endif
31
32 #include "client.h"
33 #include "quassel.h"
34
35 ClientSettings::ClientSettings(QString g) : Settings(g, Quassel::buildInfo().clientApplicationName)
36 {
37 }
38
39
40 /***********************************************************************************************/
41
42 CoreAccountSettings::CoreAccountSettings(QString subgroup)
43     : ClientSettings("CoreAccounts"),
44     _subgroup(std::move(subgroup))
45 {
46 }
47
48
49 void CoreAccountSettings::notify(const QString &key, QObject *receiver, const char *slot) const
50 {
51     ClientSettings::notify(QString("%1/%2/%3").arg(Client::currentCoreAccount().accountId().toInt()).arg(_subgroup).arg(key), receiver, slot);
52 }
53
54
55 QList<AccountId> CoreAccountSettings::knownAccounts() const
56 {
57     QList<AccountId> ids;
58     foreach(const QString &key, localChildGroups()) {
59         AccountId acc = key.toInt();
60         if (acc.isValid())
61             ids << acc;
62     }
63     return ids;
64 }
65
66
67 AccountId CoreAccountSettings::lastAccount() const
68 {
69     return localValue("LastAccount", 0).toInt();
70 }
71
72
73 void CoreAccountSettings::setLastAccount(AccountId account)
74 {
75     setLocalValue("LastAccount", account.toInt());
76 }
77
78
79 AccountId CoreAccountSettings::autoConnectAccount() const
80 {
81     return localValue("AutoConnectAccount", 0).toInt();
82 }
83
84
85 void CoreAccountSettings::setAutoConnectAccount(AccountId account)
86 {
87     setLocalValue("AutoConnectAccount", account.toInt());
88 }
89
90
91 bool CoreAccountSettings::autoConnectOnStartup() const
92 {
93     return localValue("AutoConnectOnStartup", false).toBool();
94 }
95
96
97 void CoreAccountSettings::setAutoConnectOnStartup(bool b)
98 {
99     setLocalValue("AutoConnectOnStartup", b);
100 }
101
102
103 bool CoreAccountSettings::autoConnectToFixedAccount() const
104 {
105     return localValue("AutoConnectToFixedAccount", false).toBool();
106 }
107
108
109 void CoreAccountSettings::setAutoConnectToFixedAccount(bool b)
110 {
111     setLocalValue("AutoConnectToFixedAccount", b);
112 }
113
114
115 void CoreAccountSettings::storeAccountData(AccountId id, const QVariantMap &data)
116 {
117     QString base = QString::number(id.toInt());
118     foreach(const QString &key, data.keys()) {
119         setLocalValue(base + "/" + key, data.value(key));
120     }
121
122     // FIXME Migration from 0.5 -> 0.6
123     removeLocalKey(QString("%1/Connection").arg(base));
124 }
125
126
127 QVariantMap CoreAccountSettings::retrieveAccountData(AccountId id) const
128 {
129     QVariantMap map;
130     QString base = QString::number(id.toInt());
131     foreach(const QString &key, localChildKeys(base)) {
132         map[key] = localValue(base + "/" + key);
133     }
134
135     // FIXME Migration from 0.5 -> 0.6
136     if (!map.contains("Uuid") && map.contains("Connection")) {
137         QVariantMap oldmap = map.value("Connection").toMap();
138         map["AccountName"] = oldmap.value("AccountName");
139         map["HostName"] = oldmap.value("Host");
140         map["Port"] = oldmap.value("Port");
141         map["User"] = oldmap.value("User");
142         map["Password"] = oldmap.value("Password");
143         map["StorePassword"] = oldmap.value("RememberPasswd");
144         map["UseSSL"] = oldmap.value("useSsl");
145         map["UseProxy"] = oldmap.value("useProxy");
146         map["ProxyHostName"] = oldmap.value("proxyHost");
147         map["ProxyPort"] = oldmap.value("proxyPort");
148         map["ProxyUser"] = oldmap.value("proxyUser");
149         map["ProxyPassword"] = oldmap.value("proxyPassword");
150         map["ProxyType"] = oldmap.value("proxyType");
151         map["Internal"] = oldmap.value("InternalAccount");
152
153         map["AccountId"] = id.toInt();
154         map["Uuid"] = QUuid::createUuid().toString();
155     }
156
157     return map;
158 }
159
160
161 void CoreAccountSettings::setAccountValue(const QString &key, const QVariant &value)
162 {
163     if (!Client::currentCoreAccount().isValid())
164         return;
165     setLocalValue(QString("%1/%2/%3").arg(Client::currentCoreAccount().accountId().toInt()).arg(_subgroup).arg(key), value);
166 }
167
168
169 QVariant CoreAccountSettings::accountValue(const QString &key, const QVariant &def) const
170 {
171     if (!Client::currentCoreAccount().isValid())
172         return QVariant();
173     return localValue(QString("%1/%2/%3").arg(Client::currentCoreAccount().accountId().toInt()).arg(_subgroup).arg(key), def);
174 }
175
176
177 void CoreAccountSettings::setJumpKeyMap(const QHash<int, BufferId> &keyMap)
178 {
179     QVariantMap variants;
180     QHash<int, BufferId>::const_iterator mapIter = keyMap.constBegin();
181     while (mapIter != keyMap.constEnd()) {
182         variants[QString::number(mapIter.key())] = qVariantFromValue(mapIter.value());
183         ++mapIter;
184     }
185     setAccountValue("JumpKeyMap", variants);
186 }
187
188
189 QHash<int, BufferId> CoreAccountSettings::jumpKeyMap() const
190 {
191     QHash<int, BufferId> keyMap;
192     QVariantMap variants = accountValue("JumpKeyMap", QVariant()).toMap();
193     QVariantMap::const_iterator mapIter = variants.constBegin();
194     while (mapIter != variants.constEnd()) {
195         keyMap[mapIter.key().toInt()] = mapIter.value().value<BufferId>();
196         ++mapIter;
197     }
198     return keyMap;
199 }
200
201
202 void CoreAccountSettings::setBufferViewOverlay(const QSet<int> &viewIds)
203 {
204     QVariantList variants;
205     foreach(int viewId, viewIds) {
206         variants << qVariantFromValue(viewId);
207     }
208     setAccountValue("BufferViewOverlay", variants);
209 }
210
211
212 QSet<int> CoreAccountSettings::bufferViewOverlay() const
213 {
214     QSet<int> viewIds;
215     QVariantList variants = accountValue("BufferViewOverlay").toList();
216     for (QVariantList::const_iterator iter = variants.constBegin(); iter != variants.constEnd(); ++iter) {
217         viewIds << iter->toInt();
218     }
219     return viewIds;
220 }
221
222
223 void CoreAccountSettings::removeAccount(AccountId id)
224 {
225     removeLocalKey(QString("%1").arg(id.toInt()));
226 }
227
228
229 void CoreAccountSettings::clearAccounts()
230 {
231     foreach(const QString &key, localChildGroups())
232     removeLocalKey(key);
233 }
234
235
236 /***********************************************************************************************/
237 // CoreConnectionSettings:
238
239 CoreConnectionSettings::CoreConnectionSettings()
240     : ClientSettings("CoreConnection")
241 {}
242
243 void CoreConnectionSettings::setNetworkDetectionMode(NetworkDetectionMode mode)
244 {
245     setLocalValue("NetworkDetectionMode", mode);
246 }
247
248
249 CoreConnectionSettings::NetworkDetectionMode CoreConnectionSettings::networkDetectionMode() const
250 {
251     auto mode = localValue("NetworkDetectionMode", UseQNetworkConfigurationManager).toInt();
252     if (mode == 0)
253         mode = UseQNetworkConfigurationManager; // UseSolid is gone, map that to the new default
254     return static_cast<NetworkDetectionMode>(mode);
255 }
256
257
258 void CoreConnectionSettings::setAutoReconnect(bool autoReconnect)
259 {
260     setLocalValue("AutoReconnect", autoReconnect);
261 }
262
263
264 bool CoreConnectionSettings::autoReconnect() const
265 {
266     return localValue("AutoReconnect", true).toBool();
267 }
268
269
270 void CoreConnectionSettings::setPingTimeoutInterval(int interval)
271 {
272     setLocalValue("PingTimeoutInterval", interval);
273 }
274
275
276 int CoreConnectionSettings::pingTimeoutInterval() const
277 {
278     return localValue("PingTimeoutInterval", 60).toInt();
279 }
280
281
282 void CoreConnectionSettings::setReconnectInterval(int interval)
283 {
284     setLocalValue("ReconnectInterval", interval);
285 }
286
287
288 int CoreConnectionSettings::reconnectInterval() const
289 {
290     return localValue("ReconnectInterval", 60).toInt();
291 }
292
293
294 /***********************************************************************************************/
295 // NotificationSettings:
296
297 NotificationSettings::NotificationSettings()
298     : ClientSettings("Notification")
299 {
300 }
301
302
303 void NotificationSettings::setValue(const QString &key, const QVariant &data)
304 {
305     setLocalValue(key, data);
306 }
307
308
309 QVariant NotificationSettings::value(const QString &key, const QVariant &def) const
310 {
311     return localValue(key, def);
312 }
313
314
315 void NotificationSettings::remove(const QString &key)
316 {
317     removeLocalKey(key);
318 }
319
320
321 void NotificationSettings::setHighlightList(const QVariantList &highlightList)
322 {
323     setLocalValue("Highlights/CustomList", highlightList);
324 }
325
326
327 QVariantList NotificationSettings::highlightList() const
328 {
329     return localValue("Highlights/CustomList").toList();
330 }
331
332
333 void NotificationSettings::setHighlightNick(NotificationSettings::HighlightNickType highlightNickType)
334 {
335     setLocalValue("Highlights/HighlightNick", highlightNickType);
336 }
337
338
339 NotificationSettings::HighlightNickType NotificationSettings::highlightNick() const
340 {
341     return (NotificationSettings::HighlightNickType)localValue("Highlights/HighlightNick",
342                                                                CurrentNick).toInt();
343 }
344
345
346 void NotificationSettings::setNicksCaseSensitive(bool cs)
347 {
348     setLocalValue("Highlights/NicksCaseSensitive", cs);
349 }
350
351
352 bool NotificationSettings::nicksCaseSensitive() const
353 {
354     return localValue("Highlights/NicksCaseSensitive", false).toBool();
355 }
356
357
358 // ========================================
359 //  TabCompletionSettings
360 // ========================================
361
362 TabCompletionSettings::TabCompletionSettings()
363     : ClientSettings("TabCompletion")
364 {
365 }
366
367
368 void TabCompletionSettings::setCompletionSuffix(const QString &suffix)
369 {
370     setLocalValue("CompletionSuffix", suffix);
371 }
372
373
374 QString TabCompletionSettings::completionSuffix() const
375 {
376     return localValue("CompletionSuffix", ": ").toString();
377 }
378
379
380 void TabCompletionSettings::setAddSpaceMidSentence(bool space)
381 {
382     setLocalValue("AddSpaceMidSentence", space);
383 }
384
385
386 bool TabCompletionSettings::addSpaceMidSentence() const
387 {
388     return localValue("AddSpaceMidSentence", false).toBool();
389 }
390
391
392 void TabCompletionSettings::setSortMode(SortMode mode)
393 {
394     setLocalValue("SortMode", mode);
395 }
396
397
398 TabCompletionSettings::SortMode TabCompletionSettings::sortMode() const
399 {
400     return static_cast<SortMode>(localValue("SortMode"), LastActivity);
401 }
402
403
404 void TabCompletionSettings::setCaseSensitivity(Qt::CaseSensitivity cs)
405 {
406     setLocalValue("CaseSensitivity", cs);
407 }
408
409
410 Qt::CaseSensitivity TabCompletionSettings::caseSensitivity() const
411 {
412     return (Qt::CaseSensitivity)localValue("CaseSensitivity", Qt::CaseInsensitive).toInt();
413 }
414
415
416 void TabCompletionSettings::setUseLastSpokenTo(bool use)
417 {
418     setLocalValue("UseLastSpokenTo", use);
419 }
420
421
422 bool TabCompletionSettings::useLastSpokenTo() const
423 {
424     return localValue("UseLastSpokenTo", false).toBool();
425 }
426
427
428 // ========================================
429 //  ItemViewSettings
430 // ========================================
431
432 ItemViewSettings::ItemViewSettings(const QString &group) : ClientSettings(group)
433 {
434 }
435
436
437 bool ItemViewSettings::displayTopicInTooltip() const
438 {
439     return localValue("DisplayTopicInTooltip", false).toBool();
440 }
441
442
443 bool ItemViewSettings::mouseWheelChangesBuffer() const
444 {
445     return localValue("MouseWheelChangesBuffer", false).toBool();
446 }