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