1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
/***************************************************************************
File: option.h
Project: Kio-Sword -- An ioslave for SWORD and KDE
Copyright: Copyright (C) 2005 Luke Plant
***************************************************************************/
/***************************************************************************
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef option_h
#define option_h
#include <kconfig.h>
#include <qstring.h>
#include <qmap.h>
namespace KioSword
{
class OptionBase
{
public:
virtual void readFromQueryString(QMap<QString, QString> params, bool allowPropagating) = 0;
virtual void getQueryStringPair(QString& name, QString& val) = 0;
virtual void readFromConfig(const KConfig* config) = 0;
virtual void saveToConfig(KConfig* config) = 0;
virtual void copy(const OptionBase* other) = 0;
virtual ~OptionBase() {};
};
/**
* Template class for options that can be read/saved to a query string
* or config file and are used at run time to determine various things
*
*/
template <class T>
class Option : public OptionBase
{
protected:
T m_value; // current value
T m_propagate_value; // value we are going to propagate when creating URLs
T m_default_value; // KioSWord internal default
T m_config_value; // User's default
/** Convert a value from a string to the option's type */
static const T fromString(const QString& val)
{
// We will specialise this function later
// to work correctly
T result;
return result;
}
/** Convert the option to a string for use in query strings */
QString toString(T val)
{
// Will specialise this later
QString result;
return result;
}
/** return the config setting */
T readConfigSetting(const KConfig* config)
{
// We will specialise this later
T result;
return result;
}
public:
QString m_qsShortName; // short name in querystring
QString m_qsLongName; // long name in querystring
bool m_propagate; // true if this option can be propagated
QString m_configName; // name of config setting in config file
Option()
{
};
virtual ~Option()
{
};
/**
* Sets up the names and default value of the option
*
* Setting configName to "" means the option is never put into the config file
* Setting both qsShortName and qsLongName to "" means the option is never put
* in the query string.
*
* @param default_value the value the option if not set from anywhere else
* @param configName the name the option has in the config file, or "" to never save or read from config
* @param qsShortName the short name for the option when used in a query string
* @param qsLongName the long name for the option when use in a query string
* @param propagate true if this parameter can be propagated in generated query strings
*/
void setup(const T& default_value, const QString& configName, const QString& qsShortName, const QString& qsLongName, bool propagate)
{
m_value = default_value;
m_default_value = default_value;
m_config_value = default_value; // assume this for now
m_propagate_value = m_value;
m_configName = configName;
m_qsShortName = qsShortName;
m_qsLongName = qsLongName;
m_propagate = propagate;
}
/** Get the value of the option */
const T& operator() () const
{
return m_value;
}
/** Set the value of the option (including the value to propagate) */
void set(const T& value)
{
m_value = value;
m_propagate_value = value;
}
/** read and set the option from the querystring */
virtual void readFromQueryString(QMap<QString, QString> params, bool allowPropagating)
{
T newval;
bool found = false;
/* // Start with defaults. We have to do this
// because these objects are re-used from one request to the next
m_value = m_config_value;
m_propagate_value = m_config_value;*/
// Search for short name
QMap<QString, QString>::const_iterator it = params.find(m_qsShortName);
if (it != params.end())
{
newval = fromString(it.data());
found = true;
}
if (!found) {
// Search for long name
it = params.find(m_qsLongName);
if (it != params.end())
{
newval = fromString(it.data());
found = true;
}
}
if (found) {
m_value = newval;
if (m_propagate && allowPropagating) {
m_propagate_value = newval;
}
}
}
/** set the name and value of a query string pair */
virtual void getQueryStringPair(QString& name, QString& val)
{
// To keep things tidy, we don't propagate in the
// query string values that wouldn't make a difference
// i.e. if current value is the same as config,
// don't propagate
if (m_propagate_value != m_config_value) {
if (m_qsShortName.isEmpty())
name.append(m_qsLongName);
else
name.append(m_qsShortName.copy());
val.append(toString(m_propagate_value));
}
}
/** read and set the value from the config file, or set to default if no config setting */
virtual void readFromConfig(const KConfig* config)
{
if (!m_configName.isEmpty())
{
set(readConfigSetting(config));
}
else
{
set(m_default_value);
}
m_config_value = m_value;
}
/** save the value to the config file */
virtual void saveToConfig(KConfig* config)
{
if (!m_configName.isEmpty())
{
// overloads for KConfig::writeEntry cater
// for everything we need so far
if (m_value != m_default_value) // keep settings file tidy
{
config->writeEntry(m_configName, m_value);
m_config_value = m_value;
}
else
{
config->deleteEntry(m_configName);
}
}
}
/** copy value from another */
virtual void copy(const OptionBase* other)
{
// we must ensure that we only copy from an object
// of the same type.
const Option<T>* other2 = (Option<T>*)other;
// m_configName, m_default_value, m_qsShortName and m_qsLongName, and m_propagate
// have already been set up correctly (those don't change fromString
// the values given in the setup() method)
m_value = other2->m_value;
m_config_value = other2->m_config_value;
m_propagate_value = other2->m_propagate_value;
}
};
// Specialisations
// fromString specialisations
template<>
inline const bool Option<bool>::fromString(const QString& val)
{
if (val == "0")
return false;
else
return true;
}
template<>
inline const QString Option<QString>::fromString(const QString& val)
{
return val;
}
template<>
inline const int Option<int>::fromString(const QString& val)
{
return val.toInt();
}
// toString specialisations
template<>
inline QString Option<bool>::toString(bool val)
{
return val ? QString("1"): QString("0");
}
template<>
inline QString Option<QString>::toString(QString val)
{
return val;
}
template<>
inline QString Option<int>::toString(int val)
{
return QString::number(val);
}
// readConfigSetting specialisations
template<>
inline bool Option<bool>::readConfigSetting(const KConfig* config)
{
return config->readBoolEntry(m_configName, m_default_value);
}
template<>
inline QString Option<QString>::readConfigSetting(const KConfig* config)
{
return config->readEntry(m_configName, m_default_value);
}
template<>
inline int Option<int>::readConfigSetting(const KConfig* config)
{
return config->readNumEntry(m_configName, m_default_value);
}
}
#endif
|