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