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