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