Improve marker line behavior; allow manual setting (Ctrl+R)
[quassel.git] / src / uisupport / uisettings.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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 UiSettings::UiSettings(const QString &group)
24   : ClientSettings(group)
25 {
26 }
27
28 /**************************************************************************/
29
30 UiStyleSettings::UiStyleSettings() : UiSettings("UiStyle") {}
31 UiStyleSettings::UiStyleSettings(const QString &subGroup) : UiSettings(QString("UiStyle/%1").arg(subGroup))
32 {
33 }
34
35 void UiStyleSettings::setCustomFormat(UiStyle::FormatType ftype, QTextCharFormat format) {
36   setLocalValue(QString("Format/%1").arg(ftype), format);
37 }
38
39 QTextCharFormat UiStyleSettings::customFormat(UiStyle::FormatType ftype) {
40   return localValue(QString("Format/%1").arg(ftype), QTextFormat()).value<QTextFormat>().toCharFormat();
41 }
42
43 void UiStyleSettings::removeCustomFormat(UiStyle::FormatType ftype) {
44   removeLocalKey(QString("Format/%1").arg(ftype));
45 }
46
47 QList<UiStyle::FormatType> UiStyleSettings::availableFormats() {
48   QList<UiStyle::FormatType> formats;
49   QStringList list = localChildKeys("Format");
50   foreach(QString type, list) {
51     formats << (UiStyle::FormatType)type.toInt();
52   }
53   return formats;
54 }
55
56 /**************************************************************************
57  * SessionSettings
58  **************************************************************************/
59
60 SessionSettings::SessionSettings(const QString & sessionId, const QString & group)
61 : UiSettings(group), _sessionId(sessionId)
62 {
63
64 }
65
66 void SessionSettings::setValue(const QString &key, const QVariant &data) {
67   setLocalValue(QString("%1/%2").arg(_sessionId, key), data);
68 }
69
70 QVariant SessionSettings::value(const QString &key, const QVariant &def) {
71   return localValue(QString("%1/%2").arg(_sessionId, key), def);
72 }
73
74 void SessionSettings::removeKey(const QString &key) {
75   removeLocalKey(QString("%1/%2").arg(_sessionId, key));
76 }
77
78 void SessionSettings::cleanup() {
79   QStringList sessions = localChildGroups();
80   QString str;
81   SessionSettings s(sessionId());
82   foreach(str, sessions) {
83     // load session and check age
84     s.setSessionId(str);
85     if(s.sessionAge() > 3) {
86       s.removeSession();
87     }
88   }
89 }
90
91 int SessionSettings::sessionAge() {
92   QVariant val = localValue(QString("%1/_sessionAge").arg(_sessionId), 0);
93   bool b = false;
94   int i = val.toInt(&b);
95   if(b) {
96     return i;
97   } else {
98     // no int saved, delete session
99     //qDebug() << QString("deleting invalid session %1 (invalid session age found)").arg(_sessionId);
100     removeSession();
101   }
102   return 10;
103 }
104
105 void SessionSettings::removeSession() {
106   QStringList keys = localChildKeys(sessionId());
107   foreach(QString k, keys) {
108     removeKey(k);
109   }
110 }
111
112 void SessionSettings::setSessionAge(int age) {
113   setValue(QString("_sessionAge"),age);
114 }
115
116 void SessionSettings::sessionAging() {
117   QStringList sessions = localChildGroups();
118   QString str;
119   SessionSettings s(sessionId());
120   foreach(str, sessions) {
121     // load session and check age
122     s.setSessionId(str);
123     s.setSessionAge(s.sessionAge()+1);
124   }
125 }
126