summaryrefslogtreecommitdiff
path: root/noncore/applets/zkbapplet/keyzcfg/cfgfile.cpp
Unidiff
Diffstat (limited to 'noncore/applets/zkbapplet/keyzcfg/cfgfile.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/applets/zkbapplet/keyzcfg/cfgfile.cpp349
1 files changed, 349 insertions, 0 deletions
diff --git a/noncore/applets/zkbapplet/keyzcfg/cfgfile.cpp b/noncore/applets/zkbapplet/keyzcfg/cfgfile.cpp
new file mode 100644
index 0000000..be7150e
--- a/dev/null
+++ b/noncore/applets/zkbapplet/keyzcfg/cfgfile.cpp
@@ -0,0 +1,349 @@
1#include "cfgfile.h"
2
3/* OPIE */
4#include <opie2/odebug.h>
5using namespace Opie::Core;
6
7/* QT */
8#include <qmessagebox.h>
9
10// CfgEntry implementation
11CfgEntry::CfgEntry() {
12}
13
14CfgEntry::CfgEntry(const QString& f, const QString& l):
15 file(f), label(l) {
16}
17
18const QString& CfgEntry::getFile() const {
19 return file;
20}
21
22void CfgEntry::setFile(const QString& f) {
23 file = f;
24}
25
26const QString& CfgEntry::getLabel() const {
27 return label;
28}
29
30void CfgEntry::setLabel(const QString& f) {
31 label = f;
32}
33
34// CfgFile implementation
35CfgFile::CfgFile():ardelay(400), arperiod(80) {
36}
37
38CfgFile::~CfgFile() {
39}
40
41QList<CfgEntry>& CfgFile::getEntries() {
42 return entries;
43}
44
45bool CfgFile::replaceEntry(const QString& file, const QString& label, int index) {
46 deleteEntry(file);
47
48 CfgEntry* entry = new CfgEntry(file, label);
49 if (index >= 0) {
50 entries.insert(index, entry);
51 } else {
52 entries.append(entry);
53 }
54
55 return true;
56}
57
58bool CfgFile::deleteEntry(const QString& file) {
59 for(int i = 0; i < (int) entries.count(); i++) {
60 CfgEntry* entry = entries.at(i);
61 if (entry->getFile() == file) {
62 entries.remove(i);
63 return true;
64 }
65 }
66 return false;
67}
68
69int CfgFile::getAutorepeatDelay() const {
70 return ardelay;
71}
72
73void CfgFile::setAutorepeatDelay(int n) {
74 ardelay = n;
75}
76
77int CfgFile::getAutorepeatPeriod() const {
78 return arperiod;
79}
80
81void CfgFile::setAutorepeatPeriod(int n) {
82 arperiod = n;
83}
84
85// CfgParser implementation
86CfgParser::CfgParser() {
87}
88
89bool CfgParser::load(QString file, CfgFile& cfg) {
90 QFile f(file);
91 QXmlInputSource is(f);
92 QXmlSimpleReader reader;
93 CfgHandler p(*this);
94
95 reader.setErrorHandler(this);
96 reader.setContentHandler(&p);
97
98 err = "";
99 ardelay = -1;
100 arperiod = -1;
101 reader.parse(is);
102
103 if (!err.isEmpty()) {
104 odebug << err << oendl;
105 return false;
106 }
107
108 QMap<QString, QString>::Iterator fit, lit;
109 for(uint i = 0; i < includeList.count(); i++) {
110 QString file = *includeList.at(i);
111 fit = includes.find(file);
112 QString prefix = fit.data();
113 QString label = "";
114
115 odebug << "include: file=" + fit.key() + ", prefix=" + fit.data() << oendl;
116 lit = labels.find(prefix+":*");
117 if (lit != labels.end()) {
118 label = lit.data();
119 }
120
121 cfg.replaceEntry(file, label);
122 }
123
124 if (ardelay != -1) {
125 cfg.setAutorepeatDelay(ardelay);
126 }
127
128 if (arperiod != -1) {
129 cfg.setAutorepeatPeriod(arperiod);
130 }
131
132 return true;
133}
134
135bool CfgParser::save(QString file, CfgFile& cfg) {
136 FILE* f = fopen((const char*) file.local8Bit(), "w");
137 if (!f) {
138 oerr << "Could not write config file!" << oendl;
139 return false;
140 }
141
142 fprintf(f, "<keymap autorepeat-delay=\"%d\" autorepeat-period=\"%d\" "
143 "author=\"keyzcfg\">\n", cfg.getAutorepeatDelay(),
144 cfg.getAutorepeatPeriod());
145
146 QList<CfgEntry>& entries = cfg.getEntries();
147 int n;
148
149 for(n=0; n < (int) entries.count(); n++) {
150 CfgEntry* entry = entries.at(n);
151 QString l = entry->getLabel();
152 if (l.isEmpty()) {
153 l = entry->getFile();
154 }
155 fprintf(f, "\t<label name=\"%s\" state=\"km%d:*\"/>\n",
156 (const char*) l.utf8(), n);
157 }
158
159 for(n=0; n < (int) entries.count(); n++) {
160 CfgEntry* entry = entries.at(n);
161 fprintf(f, "\t<include file=\"%s\" prefix=\"km%d\"/>\n",
162 (const char*) entry->getFile().utf8(), n);
163 }
164
165 int k = n-1;
166 char* states[] = { "LShift", "LShift-Caps", "LShift-Num",
167 "LShift-Num-Caps", 0};
168
169 for(n=0; n < (int) entries.count(); n++) {
170 QString nstate = "km" + QString::number(n+1);
171 if (n == k) {
172 nstate = "km" + QString::number(0);
173 }
174
175 for(int i = 0; states[i] != 0; i++) {
176 fprintf(f, "\t<state name=\"km%d:%s\">\n",
177 n, states[i]);
178 fprintf(f, "\t\t<map keycode=\"Middle\" pressed=\"true\">\n");
179 fprintf(f, "\t\t\t<next-state name=\"%s:%s\"/>\n",
180 (const char*) nstate.utf8(), states[i]);
181 fprintf(f, "\t\t</map>\n\t</state>\n\n");
182 }
183 }
184
185 fprintf(f, "\t<state name=\"km0:Normal\" default=\"true\"/>\n");
186
187 fprintf(f, "</keymap>");
188 fclose(f);
189 return true;
190}
191
192CfgParser::~CfgParser() {
193}
194
195int CfgParser::getAutorepeatDelay() const {
196 return ardelay;
197}
198
199void CfgParser::setAutorepeatDelay(int n) {
200 ardelay = n;
201}
202
203int CfgParser::getAutorepeatPeriod() const {
204 return arperiod;
205}
206
207void CfgParser::setAutorepeatPeriod(int n) {
208 arperiod = n;
209}
210
211void CfgParser::addLabel(const QString& name, const QString& state) {
212 labels.insert(state, name);
213 labelList.append(&labels.find(state).data());
214}
215
216void CfgParser::addFile(const QString& file, const QString& prefix) {
217 includes.insert(file, prefix);
218 includeList.append(&includes.find(file).key());
219}
220
221QString CfgParser::errorString() {
222 return err;
223}
224
225bool CfgParser::warning(const QXmlParseException& e) {
226 QString tmp;
227
228 tmp.sprintf("%d: warning: %s\n", e.lineNumber(),
229 (const char*) e.message().utf8());
230
231 err += tmp;
232
233 return true;
234}
235
236bool CfgParser::error(const QXmlParseException& e) {
237 QString tmp;
238
239 tmp.sprintf("%d: error: %s\n", e.lineNumber(),
240 (const char*) e.message().utf8());
241
242 err += tmp;
243
244 return true;
245}
246
247bool CfgParser::fatalError(const QXmlParseException& e) {
248 QString tmp;
249
250 tmp.sprintf("%d: fatal error: %s\n", e.lineNumber(),
251 (const char*) e.message().utf8());
252
253 err += tmp;
254
255 return false;
256}
257
258// CfgHandler implementation
259CfgHandler::CfgHandler(CfgParser& c):cfg(c) {
260}
261
262CfgHandler::~CfgHandler() {
263}
264
265bool CfgHandler::startKeymapElement(int ard, int arp, const QString& author) {
266 if (author != "keyzcfg") {
267 bool ret;
268 ret = QMessageBox::warning(0, "keyz configurator",
269 "Your zkb.xml doesn't seem created by keyz configurator.\n"
270 "By using keyz configurator you may loose your current "
271 "configuration\n Do you want to continue\n\n",
272 "Yes", "No", 0, 0, 1);
273
274 if (ret != 0) {
275 err = "cancelled by user";
276 return false;
277 }
278 }
279
280 if (ard != -1) {
281 cfg.setAutorepeatDelay(ard);
282 }
283
284 if (arp != -1) {
285 cfg.setAutorepeatPeriod(arp);
286 }
287
288 return true;
289}
290
291bool CfgHandler::startIncludeElement(const QString& file,
292 const QString& pref) {
293
294 cfg.addFile(file, pref);
295 return true;
296}
297
298bool CfgHandler::startLabelElement(const QString& label,
299 const QString& state) {
300
301 cfg.addLabel(label, state);
302 return true;
303}
304
305bool CfgHandler::startStateElement(const QString&, const QString&, bool) {
306
307 return true;
308}
309
310bool CfgHandler::startMapElement(int, bool) {
311 return true;
312}
313
314bool CfgHandler::startEventElement(int, int, int, bool, bool) {
315 return true;
316}
317
318bool CfgHandler::startNextStateElement(const QString&) {
319 return true;
320}
321
322
323bool CfgHandler::endKeymapElement() {
324 return true;
325}
326
327bool CfgHandler::endIncludeElement() {
328 return true;
329}
330
331bool CfgHandler::endLabelElement() {
332 return true;
333}
334
335bool CfgHandler::endStateElement() {
336 return true;
337}
338
339bool CfgHandler::endMapElement() {
340 return true;
341}
342
343bool CfgHandler::endEventElement() {
344 return true;
345}
346
347bool CfgHandler::endNextStateElement() {
348 return true;
349}