Revert "Reset the input prior to processing it in order to prevent issues with per...
[quassel.git] / src / uisupport / uisettings.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2010 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 "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 UiStyleSettings::UiStyleSettings() : UiSettings("UiStyle") {}
34 UiStyleSettings::UiStyleSettings(const QString &subGroup) : UiSettings(QString("UiStyle/%1").arg(subGroup))
35 {
36 }
37
38 void UiStyleSettings::setCustomFormat(UiStyle::FormatType ftype, QTextCharFormat format) {
39   setLocalValue(QString("Format/%1").arg(ftype), format);
40 }
41
42 QTextCharFormat UiStyleSettings::customFormat(UiStyle::FormatType ftype) {
43   return localValue(QString("Format/%1").arg(ftype), QTextFormat()).value<QTextFormat>().toCharFormat();
44 }
45
46 void UiStyleSettings::removeCustomFormat(UiStyle::FormatType ftype) {
47   removeLocalKey(QString("Format/%1").arg(ftype));
48 }
49
50 QList<UiStyle::FormatType> UiStyleSettings::availableFormats() {
51   QList<UiStyle::FormatType> formats;
52   QStringList list = localChildKeys("Format");
53   foreach(QString type, list) {
54     formats << (UiStyle::FormatType)type.toInt();
55   }
56   return formats;
57 }
58
59 /**************************************************************************
60  * SessionSettings
61  **************************************************************************/
62
63 SessionSettings::SessionSettings(const QString & sessionId, const QString & group)
64 : UiSettings(group), _sessionId(sessionId)
65 {
66
67 }
68
69 void SessionSettings::setValue(const QString &key, const QVariant &data) {
70   setLocalValue(QString("%1/%2").arg(_sessionId, key), data);
71 }
72
73 QVariant SessionSettings::value(const QString &key, const QVariant &def) {
74   return localValue(QString("%1/%2").arg(_sessionId, key), def);
75 }
76
77 void SessionSettings::removeKey(const QString &key) {
78   removeLocalKey(QString("%1/%2").arg(_sessionId, key));
79 }
80
81 void SessionSettings::cleanup() {
82   QStringList sessions = localChildGroups();
83   QString str;
84   SessionSettings s(sessionId());
85   foreach(str, sessions) {
86     // load session and check age
87     s.setSessionId(str);
88     if(s.sessionAge() > 3) {
89       s.removeSession();
90     }
91   }
92 }
93
94 int SessionSettings::sessionAge() {
95   QVariant val = localValue(QString("%1/_sessionAge").arg(_sessionId), 0);
96   bool b = false;
97   int i = val.toInt(&b);
98   if(b) {
99     return i;
100   } else {
101     // no int saved, delete session
102     //qDebug() << QString("deleting invalid session %1 (invalid session age found)").arg(_sessionId);
103     removeSession();
104   }
105   return 10;
106 }
107
108 void SessionSettings::removeSession() {
109   QStringList keys = localChildKeys(sessionId());
110   foreach(QString k, keys) {
111     removeKey(k);
112   }
113 }
114
115 void SessionSettings::setSessionAge(int age) {
116   setValue(QString("_sessionAge"),age);
117 }
118
119 void SessionSettings::sessionAging() {
120   QStringList sessions = localChildGroups();
121   QString str;
122   SessionSettings s(sessionId());
123   foreach(str, sessions) {
124     // load session and check age
125     s.setSessionId(str);
126     s.setSessionAge(s.sessionAge()+1);
127   }
128 }
129
130 /**************************************************************************
131  * ShortcutSettings
132  **************************************************************************/
133
134 ShortcutSettings::ShortcutSettings() : UiSettings("Shortcuts") {
135
136 }
137
138 void ShortcutSettings::clear() {
139   foreach(const QString &key, allLocalKeys())
140     removeLocalKey(key);
141 }
142
143 QStringList ShortcutSettings::savedShortcuts() {
144   return localChildKeys();
145 }
146
147 QKeySequence ShortcutSettings::loadShortcut(const QString &name) {
148   return localValue(name, QKeySequence()).value<QKeySequence>();
149 }
150
151 void ShortcutSettings::saveShortcut(const QString &name, const QKeySequence &seq) {
152   setLocalValue(name, seq);
153 }