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