Introduce CoreAccount and CoreAccountModel
[quassel.git] / src / client / clientsettings.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include <QStringList>
22
23
24 #include "clientsettings.h"
25
26 #include <QHostAddress>
27 #ifdef HAVE_SSL
28 #include <QSslSocket>
29 #endif
30
31
32 #include "client.h"
33 #include "quassel.h"
34
35 ClientSettings::ClientSettings(QString g) : Settings(g, Quassel::buildInfo().clientApplicationName) {
36 }
37
38 ClientSettings::~ClientSettings() {
39 }
40
41 /***********************************************************************************************/
42
43 CoreAccountSettings::CoreAccountSettings(const QString &subgroup)
44   : ClientSettings("CoreAccounts"),
45     _subgroup(subgroup)
46 {
47 }
48
49 void CoreAccountSettings::notify(const QString &key, QObject *receiver, const char *slot) {
50   ClientSettings::notify(QString("%1/%2/%3").arg(Client::currentCoreAccount().toInt()).arg(_subgroup).arg(key), receiver, slot);
51 }
52
53 QList<AccountId> CoreAccountSettings::knownAccounts() {
54   QList<AccountId> ids;
55   foreach(const QString &key, localChildGroups()) {
56     AccountId acc = key.toInt();
57     if(acc.isValid())
58       ids << acc;
59   }
60   return ids;
61 }
62
63 AccountId CoreAccountSettings::lastAccount() {
64   return localValue("LastAccount", 0).toInt();
65 }
66
67 void CoreAccountSettings::setLastAccount(AccountId account) {
68   setLocalValue("LastAccount", account.toInt());
69 }
70
71 AccountId CoreAccountSettings::autoConnectAccount() {
72   return localValue("AutoConnectAccount", 0).toInt();
73 }
74
75 void CoreAccountSettings::setAutoConnectAccount(AccountId account) {
76   setLocalValue("AutoConnectAccount", account.toInt());
77 }
78
79 void CoreAccountSettings::storeAccountData(AccountId id, const QVariantMap &data) {
80   QString base = QString::number(id.toInt());
81   foreach(const QString &key, data.keys()) {
82     setLocalValue(base + "/" + key, data.value(key));
83   }
84
85   // FIXME Migration from 0.5 -> 0.6
86   removeLocalKey(QString("%1/Connection").arg(base));
87 }
88
89 QVariantMap CoreAccountSettings::retrieveAccountData(AccountId id) {
90   QVariantMap map;
91   QString base = QString::number(id.toInt());
92   foreach(const QString &key, localChildKeys(base)) {
93     map[key] = localValue(base + "/" + key);
94   }
95
96   // FIXME Migration from 0.5 -> 0.6
97   if(!map.contains("Uuid") && map.contains("Connection")) {
98     QVariantMap oldmap = map.value("Connection").toMap();
99     map["AccountName"] = oldmap.value("AccountName");
100     map["HostName"] = oldmap.value("Host");
101     map["Port"] = oldmap.value("Port");
102     map["User"] = oldmap.value("User");
103     map["Password"] = oldmap.value("Password");
104     map["StorePassword"] = oldmap.value("RememberPasswd");
105     map["UseSSL"] = oldmap.value("useSsl");
106     map["UseProxy"] = oldmap.value("useProxy");
107     map["ProxyHostName"] = oldmap.value("proxyHost");
108     map["ProxyPort"] = oldmap.value("proxyPort");
109     map["ProxyUser"] = oldmap.value("proxyUser");
110     map["ProxyPassword"] = oldmap.value("proxyPassword");
111     map["ProxyType"] = oldmap.value("proxyType");
112
113     map["AccountId"] = id.toInt();
114     map["Uuid"] = QUuid::createUuid().toString();
115   }
116
117   return map;
118 }
119
120 void CoreAccountSettings::setAccountValue(const QString &key, const QVariant &value) {
121   if(!Client::currentCoreAccount().isValid())
122     return;
123   setLocalValue(QString("%1/%2/%3").arg(Client::currentCoreAccount().toInt()).arg(_subgroup).arg(key), value);
124 }
125
126 QVariant CoreAccountSettings::accountValue(const QString &key, const QVariant &def) {
127   if(!Client::currentCoreAccount().isValid())
128     return QVariant();
129   return localValue(QString("%1/%2/%3").arg(Client::currentCoreAccount().toInt()).arg(_subgroup).arg(key), def);
130 }
131
132 void CoreAccountSettings::setJumpKeyMap(const QHash<int, BufferId> &keyMap) {
133   QVariantMap variants;
134   QHash<int, BufferId>::const_iterator mapIter = keyMap.constBegin();
135   while(mapIter != keyMap.constEnd()) {
136     variants[QString::number(mapIter.key())] = qVariantFromValue(mapIter.value());
137     ++mapIter;
138   }
139   setAccountValue("JumpKeyMap", variants);
140 }
141
142 QHash<int, BufferId> CoreAccountSettings::jumpKeyMap() {
143   QHash<int, BufferId> keyMap;
144   QVariantMap variants = accountValue("JumpKeyMap", QVariant()).toMap();
145   QVariantMap::const_iterator mapIter = variants.constBegin();
146   while(mapIter != variants.constEnd()) {
147     keyMap[mapIter.key().toInt()] = mapIter.value().value<BufferId>();
148     ++mapIter;
149   }
150   return keyMap;
151 }
152
153 void CoreAccountSettings::removeAccount(AccountId id) {
154   removeLocalKey(QString("%1").arg(id.toInt()));
155 }
156
157
158 /***********************************************************************************************/
159 // NotificationSettings:
160
161 NotificationSettings::NotificationSettings() : ClientSettings("Notification") {
162 }
163
164 void NotificationSettings::setHighlightList(const QVariantList &highlightList) {
165   setLocalValue("Highlights/CustomList", highlightList);
166 }
167
168 QVariantList NotificationSettings::highlightList() {
169   return localValue("Highlights/CustomList").toList();
170 }
171
172 void NotificationSettings::setHighlightNick(NotificationSettings::HighlightNickType highlightNickType) {
173   setLocalValue("Highlights/HighlightNick", highlightNickType);
174 }
175
176 NotificationSettings::HighlightNickType NotificationSettings::highlightNick() {
177   return (NotificationSettings::HighlightNickType) localValue("Highlights/HighlightNick", CurrentNick).toInt();
178 }
179
180 void NotificationSettings::setNicksCaseSensitive(bool cs) {
181   setLocalValue("Highlights/NicksCaseSensitive", cs);
182 }
183
184 bool NotificationSettings::nicksCaseSensitive() {
185   return localValue("Highlights/NicksCaseSensitive", false).toBool();
186 }
187
188
189 // ========================================
190 //  KnownHostsSettings
191 // ========================================
192 KnownHostsSettings::KnownHostsSettings()
193   : ClientSettings("KnownHosts")
194 {
195 }
196
197 QByteArray KnownHostsSettings::knownDigest(const QHostAddress &address) {
198   return localValue(address.toString(), QByteArray()).toByteArray();
199 }
200
201 void KnownHostsSettings::saveKnownHost(const QHostAddress &address, const QByteArray &certDigest) {
202   setLocalValue(address.toString(), certDigest);
203 }
204
205 bool KnownHostsSettings::isKnownHost(const QHostAddress &address, const QByteArray &certDigest) {
206   return certDigest == localValue(address.toString(), QByteArray()).toByteArray();
207 }
208
209 #ifdef HAVE_SSL
210 QByteArray KnownHostsSettings::knownDigest(const QSslSocket *socket) {
211   return knownDigest(socket->peerAddress());
212 }
213
214 void KnownHostsSettings::saveKnownHost(const QSslSocket *socket) {
215   Q_ASSERT(socket);
216   saveKnownHost(socket->peerAddress(), socket->peerCertificate().digest());
217 }
218
219 bool KnownHostsSettings::isKnownHost(const QSslSocket *socket) {
220   Q_ASSERT(socket);
221   return isKnownHost(socket->peerAddress(), socket->peerCertificate().digest());
222 }
223 #endif
224
225
226 // ========================================
227 //  TabCompletionSettings
228 // ========================================
229
230 TabCompletionSettings::TabCompletionSettings() : ClientSettings("TabCompletion") {
231 }
232
233 void TabCompletionSettings::setCompletionSuffix(const QString &suffix) {
234   setLocalValue("CompletionSuffix", suffix);
235 }
236
237 QString TabCompletionSettings::completionSuffix() {
238   return localValue("CompletionSuffix", ": ").toString();
239 }
240
241 void TabCompletionSettings::setSortMode(SortMode mode) {
242   setLocalValue("SortMode", mode);
243 }
244
245 TabCompletionSettings::SortMode TabCompletionSettings::sortMode() {
246   return static_cast<SortMode>(localValue("SortMode"), LastActivity);
247 }
248
249 void TabCompletionSettings::setCaseSensitivity(Qt::CaseSensitivity cs) {
250   setLocalValue("CaseSensitivity", cs);
251 }
252
253 Qt::CaseSensitivity TabCompletionSettings::caseSensitivity() {
254   return (Qt::CaseSensitivity)localValue("CaseSensitivity", Qt::CaseInsensitive).toInt();
255 }
256
257 void TabCompletionSettings::setUseLastSpokenTo(bool use) {
258   setLocalValue("UseLastSpokenTo", use);
259 }
260
261 bool TabCompletionSettings::useLastSpokenTo() {
262   return localValue("UseLastSpokenTo", false).toBool();
263 }
264
265 // ========================================
266 //  ItemViewSettings
267 // ========================================
268
269 ItemViewSettings::ItemViewSettings(const QString &group) : ClientSettings(group) {
270
271 }
272
273 bool ItemViewSettings::displayTopicInTooltip() {
274   return localValue("DisplayTopicInTooltip", false).toBool();
275 }
276
277 bool ItemViewSettings::mouseWheelChangesBuffer() {
278   return localValue("MouseWheelChangesBuffer", false).toBool();
279 }