-rw-r--r-- | libopie2/opiecore/oconfig.h | 167 |
1 files changed, 167 insertions, 0 deletions
diff --git a/libopie2/opiecore/oconfig.h b/libopie2/opiecore/oconfig.h new file mode 100644 index 0000000..afe14b1 --- a/dev/null +++ b/libopie2/opiecore/oconfig.h | |||
@@ -0,0 +1,167 @@ | |||
1 | /* | ||
2 | This file is part of the Opie Project | ||
3 | |||
4 | (C) 2003 Michael 'Mickey' Lauer <mickey@tm.informatik.uni-frankfurt.de> | ||
5 | Inspired by the config classes from the KDE Project which are | ||
6 | =. (C) 1997 Matthias Kalle Dalheimer <kalle@kde.org> | ||
7 | .=l. | ||
8 | .>+-= | ||
9 | _;:, .> :=|. This program is free software; you can | ||
10 | .> <`_, > . <= redistribute it and/or modify it under | ||
11 | :`=1 )Y*s>-.-- : the terms of the GNU Library General Public | ||
12 | .="- .-=="i, .._ License as published by the Free Software | ||
13 | - . .-<_> .<> Foundation; either version 2 of the License, | ||
14 | ._= =} : or (at your option) any later version. | ||
15 | .%`+i> _;_. | ||
16 | .i_,=:_. -<s. This program is distributed in the hope that | ||
17 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; | ||
18 | : .. .:, . . . without even the implied warranty of | ||
19 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A | ||
20 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU | ||
21 | ..}^=.= = ; Library General Public License for more | ||
22 | ++= -. .` .: details. | ||
23 | : = ...= . :.=- | ||
24 | -. .:....=;==+<; You should have received a copy of the GNU | ||
25 | -_. . . )=. = Library General Public License along with | ||
26 | -- :-=` this library; see the file COPYING.LIB. | ||
27 | If not, write to the Free Software Foundation, | ||
28 | Inc., 59 Temple Place - Suite 330, | ||
29 | Boston, MA 02111-1307, USA. | ||
30 | */ | ||
31 | |||
32 | #ifndef OCONFIG_H | ||
33 | #define OCONFIG_H | ||
34 | |||
35 | //FIXME: Implement for X11 or reuse libqpe/Config there also? | ||
36 | |||
37 | #include <qpe/config.h> | ||
38 | |||
39 | class QColor; | ||
40 | class QFont; | ||
41 | |||
42 | /** | ||
43 | * A Configuration class based on the Qtopia @ref Config class | ||
44 | * featuring additional handling of color and font entries | ||
45 | */ | ||
46 | |||
47 | class OConfig : public Config | ||
48 | { | ||
49 | public: | ||
50 | |||
51 | /** | ||
52 | * Constructs a OConfig object. | ||
53 | * | ||
54 | * @param name A file to parse. | ||
55 | */ | ||
56 | OConfig( const QString &name, Domain domain = User ); | ||
57 | |||
58 | /** | ||
59 | * Destructs the OConfig object. | ||
60 | * | ||
61 | * Writes back any dirty configuration entries, and destroys | ||
62 | * dynamically created objects. | ||
63 | */ | ||
64 | virtual ~OConfig(); | ||
65 | |||
66 | /** | ||
67 | * Returns the name of the group in which we are | ||
68 | * searching for keys and from which we are retrieving entries. | ||
69 | * | ||
70 | * @return The current group. | ||
71 | */ | ||
72 | const QString& group() { return git.key(); }; | ||
73 | |||
74 | /** | ||
75 | * Reads a @ref QColor entry. | ||
76 | * | ||
77 | * Read the value of an entry specified by @p pKey in the current group | ||
78 | * and interpret it as a color. | ||
79 | * | ||
80 | * @param pKey The key to search for. | ||
81 | * @param pDefault A default value (null QColor by default) returned if the | ||
82 | * key was not found or if the value cannot be interpreted. | ||
83 | * @return The value for this key. | ||
84 | */ | ||
85 | QColor readColorEntry( const QString& key, const QColor* pDefault ) const; | ||
86 | |||
87 | /** | ||
88 | * Reads a @ref QFont value. | ||
89 | * | ||
90 | * Read the value of an entry specified by @p pKey in the current group | ||
91 | * and interpret it as a font object. | ||
92 | * | ||
93 | * @param pKey The key to search for. | ||
94 | * @param pDefault A default value (null QFont by default) returned if the | ||
95 | * key was not found or if the read value cannot be interpreted. | ||
96 | * @return The value for this key. | ||
97 | */ | ||
98 | QFont readFontEntry( const QString& key, const QFont* pDefault ) const; | ||
99 | |||
100 | }; | ||
101 | |||
102 | /** | ||
103 | * Helper class to facilitate working with @ref OConfig / @ref OSimpleConfig | ||
104 | * groups. | ||
105 | * | ||
106 | * Careful programmers always set the group of a | ||
107 | * @ref OConfig object to the group they want to read from | ||
108 | * and set it back to the old one of afterwards. This is usually | ||
109 | * written as: | ||
110 | * <pre> | ||
111 | * | ||
112 | * QString oldgroup config()->group(); | ||
113 | * config()->setGroup( "TheGroupThatIWant" ); | ||
114 | * ... | ||
115 | * config()->writeEntry( "Blah", "Blubb" ); | ||
116 | * | ||
117 | * config()->setGroup( oldgroup ); | ||
118 | * </pre> | ||
119 | * | ||
120 | * In order to facilitate this task, you can use | ||
121 | * OConfigGroupSaver. Simply construct such an object ON THE STACK | ||
122 | * when you want to switch to a new group. Then, when the object goes | ||
123 | * out of scope, the group will automatically be restored. If you | ||
124 | * want to use several different groups within a function or method, | ||
125 | * you can still use OConfigGroupSaver: Simply enclose all work with | ||
126 | * one group (including the creation of the OConfigGroupSaver object) | ||
127 | * in one block. | ||
128 | * | ||
129 | * @author Matthias Kalle Dalheimer <Kalle@kde.org> | ||
130 | * @version $Id$ | ||
131 | * @see OConfig | ||
132 | * @short Helper class for easier use of OConfig groups | ||
133 | */ | ||
134 | |||
135 | class OConfigGroupSaver | ||
136 | { | ||
137 | public: | ||
138 | /** | ||
139 | * Constructor. You pass a pointer to the OConfigBase-derived | ||
140 | * object you want to work with and a string indicating the _new_ | ||
141 | * group. | ||
142 | * @param config The OConfig-derived object this | ||
143 | * OConfigGroupSaver works on. | ||
144 | * @param group The new group that the config object should switch to. | ||
145 | */ | ||
146 | OConfigGroupSaver( OConfig* config, QString group ) :_config(config), _oldgroup(config->group() ) | ||
147 | { _config->setGroup( group ); } | ||
148 | |||
149 | OConfigGroupSaver( OConfig* config, const char *group ) :_config(config), _oldgroup(config->group()) | ||
150 | { _config->setGroup( group ); } | ||
151 | |||
152 | OConfigGroupSaver( OConfig* config, const QCString &group ) : _config(config), _oldgroup(config->group()) | ||
153 | { _config->setGroup( group ); } | ||
154 | |||
155 | ~OConfigGroupSaver() { _config->setGroup( _oldgroup ); } | ||
156 | |||
157 | OConfig* config() { return _config; }; | ||
158 | |||
159 | private: | ||
160 | OConfig* _config; | ||
161 | QString _oldgroup; | ||
162 | |||
163 | OConfigGroupSaver( const OConfigGroupSaver& ); | ||
164 | OConfigGroupSaver& operator=( const OConfigGroupSaver& ); | ||
165 | }; | ||
166 | |||
167 | #endif // OCONFIG_H | ||