modernize: Use '= default' instead of empty ctor/dtor bodies
[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)
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()
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()
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()
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()
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()
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)
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)
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()
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()
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() : ClientSettings("CoreConnection") {}
240
241 void CoreConnectionSettings::setNetworkDetectionMode(NetworkDetectionMode mode)
242 {
243     setLocalValue("NetworkDetectionMode", mode);
244 }
245
246
247 CoreConnectionSettings::NetworkDetectionMode CoreConnectionSettings::networkDetectionMode()
248 {
249     auto mode = localValue("NetworkDetectionMode", UseQNetworkConfigurationManager).toInt();
250     if (mode == 0)
251         mode = UseQNetworkConfigurationManager; // UseSolid is gone, map that to the new default
252     return static_cast<NetworkDetectionMode>(mode);
253 }
254
255
256 void CoreConnectionSettings::setAutoReconnect(bool autoReconnect)
257 {
258     setLocalValue("AutoReconnect", autoReconnect);
259 }
260
261
262 bool CoreConnectionSettings::autoReconnect()
263 {
264     return localValue("AutoReconnect", true).toBool();
265 }
266
267
268 void CoreConnectionSettings::setPingTimeoutInterval(int interval)
269 {
270     setLocalValue("PingTimeoutInterval", interval);
271 }
272
273
274 int CoreConnectionSettings::pingTimeoutInterval()
275 {
276     return localValue("PingTimeoutInterval", 60).toInt();
277 }
278
279
280 void CoreConnectionSettings::setReconnectInterval(int interval)
281 {
282     setLocalValue("ReconnectInterval", interval);
283 }
284
285
286 int CoreConnectionSettings::reconnectInterval()
287 {
288     return localValue("ReconnectInterval", 60).toInt();
289 }
290
291
292 /***********************************************************************************************/
293 // NotificationSettings:
294
295 NotificationSettings::NotificationSettings() : ClientSettings("Notification")
296 {
297 }
298
299
300 void NotificationSettings::setHighlightList(const QVariantList &highlightList)
301 {
302     setLocalValue("Highlights/CustomList", highlightList);
303 }
304
305
306 QVariantList NotificationSettings::highlightList()
307 {
308     return localValue("Highlights/CustomList").toList();
309 }
310
311
312 void NotificationSettings::setHighlightNick(NotificationSettings::HighlightNickType highlightNickType)
313 {
314     setLocalValue("Highlights/HighlightNick", highlightNickType);
315 }
316
317
318 NotificationSettings::HighlightNickType NotificationSettings::highlightNick()
319 {
320     return (NotificationSettings::HighlightNickType)localValue("Highlights/HighlightNick",
321                                                                CurrentNick).toInt();
322 }
323
324
325 void NotificationSettings::setNicksCaseSensitive(bool cs)
326 {
327     setLocalValue("Highlights/NicksCaseSensitive", cs);
328 }
329
330
331 bool NotificationSettings::nicksCaseSensitive()
332 {
333     return localValue("Highlights/NicksCaseSensitive", false).toBool();
334 }
335
336
337 // ========================================
338 //  TabCompletionSettings
339 // ========================================
340
341 TabCompletionSettings::TabCompletionSettings() : ClientSettings("TabCompletion")
342 {
343 }
344
345
346 void TabCompletionSettings::setCompletionSuffix(const QString &suffix)
347 {
348     setLocalValue("CompletionSuffix", suffix);
349 }
350
351
352 QString TabCompletionSettings::completionSuffix()
353 {
354     return localValue("CompletionSuffix", ": ").toString();
355 }
356
357
358 void TabCompletionSettings::setAddSpaceMidSentence(bool space)
359 {
360     setLocalValue("AddSpaceMidSentence", space);
361 }
362
363
364 bool TabCompletionSettings::addSpaceMidSentence()
365 {
366     return localValue("AddSpaceMidSentence", false).toBool();
367 }
368
369
370 void TabCompletionSettings::setSortMode(SortMode mode)
371 {
372     setLocalValue("SortMode", mode);
373 }
374
375
376 TabCompletionSettings::SortMode TabCompletionSettings::sortMode()
377 {
378     return static_cast<SortMode>(localValue("SortMode"), LastActivity);
379 }
380
381
382 void TabCompletionSettings::setCaseSensitivity(Qt::CaseSensitivity cs)
383 {
384     setLocalValue("CaseSensitivity", cs);
385 }
386
387
388 Qt::CaseSensitivity TabCompletionSettings::caseSensitivity()
389 {
390     return (Qt::CaseSensitivity)localValue("CaseSensitivity", Qt::CaseInsensitive).toInt();
391 }
392
393
394 void TabCompletionSettings::setUseLastSpokenTo(bool use)
395 {
396     setLocalValue("UseLastSpokenTo", use);
397 }
398
399
400 bool TabCompletionSettings::useLastSpokenTo()
401 {
402     return localValue("UseLastSpokenTo", false).toBool();
403 }
404
405
406 // ========================================
407 //  ItemViewSettings
408 // ========================================
409
410 ItemViewSettings::ItemViewSettings(const QString &group) : ClientSettings(group)
411 {
412 }
413
414
415 bool ItemViewSettings::displayTopicInTooltip()
416 {
417     return localValue("DisplayTopicInTooltip", false).toBool();
418 }
419
420
421 bool ItemViewSettings::mouseWheelChangesBuffer()
422 {
423     return localValue("MouseWheelChangesBuffer", false).toBool();
424 }