modernize: Remove old-style slot usage in NetworkModelController
[quassel.git] / src / uisupport / uisettings.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 "uisettings.h"
22
23 #include <utility>
24
25 #include "action.h"
26 #include "actioncollection.h"
27
28 UiSettings::UiSettings(QString group)
29     : ClientSettings(std::move(group))
30 {
31 }
32
33
34 void UiSettings::setValue(const QString &key, const QVariant &data)
35 {
36     setLocalValue(key, data);
37 }
38
39
40 QVariant UiSettings::value(const QString &key, const QVariant &def) const
41 {
42     return localValue(key, def);
43 }
44
45
46 bool UiSettings::valueExists(const QString &key) const
47 {
48     return localKeyExists(key);
49 }
50
51
52 void UiSettings::remove(const QString &key)
53 {
54     removeLocalKey(key);
55 }
56
57 /**************************************************************************/
58
59 UiStyleSettings::UiStyleSettings()
60     : UiSettings("UiStyle")
61 {
62 }
63
64
65 UiStyleSettings::UiStyleSettings(const QString &subGroup)
66     : UiSettings(QString("UiStyle/%1").arg(subGroup))
67 {
68 }
69
70
71 void UiStyleSettings::setCustomFormat(UiStyle::FormatType ftype, const QTextCharFormat &format)
72 {
73     setLocalValue(QString("Format/%1").arg(static_cast<quint32>(ftype)), format);
74 }
75
76
77 QTextCharFormat UiStyleSettings::customFormat(UiStyle::FormatType ftype) const
78 {
79     return localValue(QString("Format/%1").arg(static_cast<quint32>(ftype)), QTextFormat()).value<QTextFormat>().toCharFormat();
80 }
81
82
83 void UiStyleSettings::removeCustomFormat(UiStyle::FormatType ftype)
84 {
85     removeLocalKey(QString("Format/%1").arg(static_cast<quint32>(ftype)));
86 }
87
88
89 QList<UiStyle::FormatType> UiStyleSettings::availableFormats() const
90 {
91     QList<UiStyle::FormatType> formats;
92     QStringList list = localChildKeys("Format");
93     foreach(QString type, list) {
94         formats << (UiStyle::FormatType)type.toInt();
95     }
96     return formats;
97 }
98
99
100 /**************************************************************************
101  * SessionSettings
102  **************************************************************************/
103
104 SessionSettings::SessionSettings(QString sessionId, QString group)
105     : UiSettings(std::move(group))
106     , _sessionId(std::move(sessionId))
107 {
108 }
109
110
111 void SessionSettings::setValue(const QString &key, const QVariant &data)
112 {
113     setLocalValue(QString("%1/%2").arg(_sessionId, key), data);
114 }
115
116
117 QVariant SessionSettings::value(const QString &key, const QVariant &def) const
118 {
119     return localValue(QString("%1/%2").arg(_sessionId, key), def);
120 }
121
122
123 void SessionSettings::removeKey(const QString &key)
124 {
125     removeLocalKey(QString("%1/%2").arg(_sessionId, key));
126 }
127
128
129 void SessionSettings::cleanup()
130 {
131     QStringList sessions = localChildGroups();
132     QString str;
133     SessionSettings s(sessionId());
134     foreach(str, sessions) {
135         // load session and check age
136         s.setSessionId(str);
137         if (s.sessionAge() > 3) {
138             s.removeSession();
139         }
140     }
141 }
142
143
144 QString SessionSettings::sessionId() const
145 {
146     return _sessionId;
147 }
148
149
150 void SessionSettings::setSessionId(QString sessionId)
151 {
152     _sessionId = std::move(sessionId);
153 }
154
155
156 int SessionSettings::sessionAge()
157 {
158     QVariant val = localValue(QString("%1/_sessionAge").arg(_sessionId), 0);
159     bool b = false;
160     int i = val.toInt(&b);
161     if (b) {
162         return i;
163     }
164     else {
165         // no int saved, delete session
166         //qDebug() << QString("deleting invalid session %1 (invalid session age found)").arg(_sessionId);
167         removeSession();
168     }
169     return 10;
170 }
171
172
173 void SessionSettings::removeSession()
174 {
175     QStringList keys = localChildKeys(sessionId());
176     foreach(QString k, keys) {
177         removeKey(k);
178     }
179 }
180
181
182 void SessionSettings::setSessionAge(int age)
183 {
184     setValue(QString("_sessionAge"), age);
185 }
186
187
188 void SessionSettings::sessionAging()
189 {
190     QStringList sessions = localChildGroups();
191     QString str;
192     SessionSettings s(sessionId());
193     foreach(str, sessions) {
194         // load session and check age
195         s.setSessionId(str);
196         s.setSessionAge(s.sessionAge()+1);
197     }
198 }
199
200
201 /**************************************************************************
202  * ShortcutSettings
203  **************************************************************************/
204
205 ShortcutSettings::ShortcutSettings()
206     : UiSettings("Shortcuts")
207 {
208 }
209
210
211 void ShortcutSettings::clear()
212 {
213     for (auto &&key : allLocalKeys()) {
214         removeLocalKey(key);
215     }
216 }
217
218
219 QStringList ShortcutSettings::savedShortcuts() const
220 {
221     return localChildKeys();
222 }
223
224
225 QKeySequence ShortcutSettings::loadShortcut(const QString &name) const
226 {
227     return localValue(name, QKeySequence()).value<QKeySequence>();
228 }
229
230
231 void ShortcutSettings::saveShortcut(const QString &name, const QKeySequence &seq)
232 {
233     setLocalValue(name, seq);
234 }