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