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