Lock Dock Positions has been replaced by Lock Layout
[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(QString key, localChildGroups()) {
56     AccountId acc = key.toInt();
57     if(acc.isValid()) ids << acc;
58   }
59   return ids;
60 }
61
62 AccountId CoreAccountSettings::lastAccount() {
63   return localValue("LastAccount", 0).toInt();
64 }
65
66 void CoreAccountSettings::setLastAccount(AccountId account) {
67   setLocalValue("LastAccount", account.toInt());
68 }
69
70 AccountId CoreAccountSettings::autoConnectAccount() {
71   return localValue("AutoConnectAccount", 0).toInt();
72 }
73
74 void CoreAccountSettings::setAutoConnectAccount(AccountId account) {
75   setLocalValue("AutoConnectAccount", account.toInt());
76 }
77
78 void CoreAccountSettings::storeAccountData(AccountId id, const QVariantMap &data) {
79   setLocalValue(QString("%1/Connection").arg(id.toInt()), data);
80 }
81
82 QVariantMap CoreAccountSettings::retrieveAccountData(AccountId id) {
83   return localValue(QString("%1/Connection").arg(id.toInt()), QVariant()).toMap();
84 }
85
86 void CoreAccountSettings::setAccountValue(const QString &key, const QVariant &value) {
87   if(!Client::currentCoreAccount().isValid()) return;
88   setLocalValue(QString("%1/%2/%3").arg(Client::currentCoreAccount().toInt()).arg(_subgroup).arg(key), value);
89 }
90
91 QVariant CoreAccountSettings::accountValue(const QString &key, const QVariant &def) {
92   if(!Client::currentCoreAccount().isValid()) return QVariant();
93   return localValue(QString("%1/%2/%3").arg(Client::currentCoreAccount().toInt()).arg(_subgroup).arg(key), def);
94 }
95
96 void CoreAccountSettings::setJumpKeyMap(const QHash<int, BufferId> &keyMap) {
97   QVariantMap variants;
98   QHash<int, BufferId>::const_iterator mapIter = keyMap.constBegin();
99   while(mapIter != keyMap.constEnd()) {
100     variants[QString::number(mapIter.key())] = qVariantFromValue(mapIter.value());
101     mapIter++;
102   }
103   setLocalValue("JumpKeyMap", variants);
104 }
105
106 QHash<int, BufferId> CoreAccountSettings::jumpKeyMap() {
107   QHash<int, BufferId> keyMap;
108   QVariantMap variants = localValue("JumpKeyMap", QVariant()).toMap();
109   QVariantMap::const_iterator mapIter = variants.constBegin();
110   while(mapIter != variants.constEnd()) {
111     keyMap[mapIter.key().toInt()] = mapIter.value().value<BufferId>();
112     mapIter++;
113   }
114   return keyMap;
115 }
116
117 void CoreAccountSettings::removeAccount(AccountId id) {
118   removeLocalKey(QString("%1").arg(id.toInt()));
119 }
120
121
122 /***********************************************************************************************/
123 // NotificationSettings:
124
125 NotificationSettings::NotificationSettings() : ClientSettings("Notification") {
126 }
127
128 void NotificationSettings::setHighlightList(const QVariantList &highlightList) {
129   setLocalValue("Highlights/CustomList", highlightList);
130 }
131
132 QVariantList NotificationSettings::highlightList() {
133   return localValue("Highlights/CustomList").toList();
134 }
135
136 void NotificationSettings::setHighlightNick(NotificationSettings::HighlightNickType highlightNickType) {
137   setLocalValue("Highlights/HighlightNick", highlightNickType);
138 }
139
140 NotificationSettings::HighlightNickType NotificationSettings::highlightNick() {
141   return (NotificationSettings::HighlightNickType) localValue("Highlights/HighlightNick", CurrentNick).toInt();
142 }
143
144 void NotificationSettings::setNicksCaseSensitive(bool cs) {
145   setLocalValue("Highlights/NicksCaseSensitive", cs);
146 }
147
148 bool NotificationSettings::nicksCaseSensitive() {
149   return localValue("Highlights/NicksCaseSensitive", false).toBool();
150 }
151
152
153 // ========================================
154 //  KnownHostsSettings
155 // ========================================
156 KnownHostsSettings::KnownHostsSettings()
157   : ClientSettings("KnownHosts")
158 {
159 }
160
161 QByteArray KnownHostsSettings::knownDigest(const QHostAddress &address) {
162   return localValue(address.toString(), QByteArray()).toByteArray();
163 }
164
165 void KnownHostsSettings::saveKnownHost(const QHostAddress &address, const QByteArray &certDigest) {
166   setLocalValue(address.toString(), certDigest);
167 }
168
169 bool KnownHostsSettings::isKnownHost(const QHostAddress &address, const QByteArray &certDigest) {
170   return certDigest == localValue(address.toString(), QByteArray()).toByteArray();
171 }
172
173 #ifdef HAVE_SSL
174 QByteArray KnownHostsSettings::knownDigest(const QSslSocket *socket) {
175   return knownDigest(socket->peerAddress());
176 }
177
178 void KnownHostsSettings::saveKnownHost(const QSslSocket *socket) {
179   Q_ASSERT(socket);
180   saveKnownHost(socket->peerAddress(), socket->peerCertificate().digest());
181 }
182
183 bool KnownHostsSettings::isKnownHost(const QSslSocket *socket) {
184   Q_ASSERT(socket);
185   return isKnownHost(socket->peerAddress(), socket->peerCertificate().digest());
186 }
187 #endif