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