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