author | alwin <alwin> | 2004-08-01 21:26:02 (UTC) |
---|---|---|
committer | alwin <alwin> | 2004-08-01 21:26:02 (UTC) |
commit | cd26bd549ac20b73039bab11662d627895f37c1a (patch) (unidiff) | |
tree | a1840bf1e13ca4ff1b43572db8194eacdcbe091d /noncore/apps/keyz-cfg/cfgfile.cpp | |
parent | 9084969d2d4fa39d4d2c185695f0c88739a0f95c (diff) | |
download | opie-cd26bd549ac20b73039bab11662d627895f37c1a.zip opie-cd26bd549ac20b73039bab11662d627895f37c1a.tar.gz opie-cd26bd549ac20b73039bab11662d627895f37c1a.tar.bz2 |
reorganized zkbapplet for easier packaging within OE
Diffstat (limited to 'noncore/apps/keyz-cfg/cfgfile.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/apps/keyz-cfg/cfgfile.cpp | 349 |
1 files changed, 0 insertions, 349 deletions
diff --git a/noncore/apps/keyz-cfg/cfgfile.cpp b/noncore/apps/keyz-cfg/cfgfile.cpp deleted file mode 100644 index be7150e..0000000 --- a/noncore/apps/keyz-cfg/cfgfile.cpp +++ b/dev/null | |||
@@ -1,349 +0,0 @@ | |||
1 | #include "cfgfile.h" | ||
2 | |||
3 | /* OPIE */ | ||
4 | #include <opie2/odebug.h> | ||
5 | using namespace Opie::Core; | ||
6 | |||
7 | /* QT */ | ||
8 | #include <qmessagebox.h> | ||
9 | |||
10 | // CfgEntry implementation | ||
11 | CfgEntry::CfgEntry() { | ||
12 | } | ||
13 | |||
14 | CfgEntry::CfgEntry(const QString& f, const QString& l): | ||
15 | file(f), label(l) { | ||
16 | } | ||
17 | |||
18 | const QString& CfgEntry::getFile() const { | ||
19 | return file; | ||
20 | } | ||
21 | |||
22 | void CfgEntry::setFile(const QString& f) { | ||
23 | file = f; | ||
24 | } | ||
25 | |||
26 | const QString& CfgEntry::getLabel() const { | ||
27 | return label; | ||
28 | } | ||
29 | |||
30 | void CfgEntry::setLabel(const QString& f) { | ||
31 | label = f; | ||
32 | } | ||
33 | |||
34 | // CfgFile implementation | ||
35 | CfgFile::CfgFile():ardelay(400), arperiod(80) { | ||
36 | } | ||
37 | |||
38 | CfgFile::~CfgFile() { | ||
39 | } | ||
40 | |||
41 | QList<CfgEntry>& CfgFile::getEntries() { | ||
42 | return entries; | ||
43 | } | ||
44 | |||
45 | bool 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 | |||
58 | bool 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 | |||
69 | int CfgFile::getAutorepeatDelay() const { | ||
70 | return ardelay; | ||
71 | } | ||
72 | |||
73 | void CfgFile::setAutorepeatDelay(int n) { | ||
74 | ardelay = n; | ||
75 | } | ||
76 | |||
77 | int CfgFile::getAutorepeatPeriod() const { | ||
78 | return arperiod; | ||
79 | } | ||
80 | |||
81 | void CfgFile::setAutorepeatPeriod(int n) { | ||
82 | arperiod = n; | ||
83 | } | ||
84 | |||
85 | // CfgParser implementation | ||
86 | CfgParser::CfgParser() { | ||
87 | } | ||
88 | |||
89 | bool 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 | |||
135 | bool 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 | |||
192 | CfgParser::~CfgParser() { | ||
193 | } | ||
194 | |||
195 | int CfgParser::getAutorepeatDelay() const { | ||
196 | return ardelay; | ||
197 | } | ||
198 | |||
199 | void CfgParser::setAutorepeatDelay(int n) { | ||
200 | ardelay = n; | ||
201 | } | ||
202 | |||
203 | int CfgParser::getAutorepeatPeriod() const { | ||
204 | return arperiod; | ||
205 | } | ||
206 | |||
207 | void CfgParser::setAutorepeatPeriod(int n) { | ||
208 | arperiod = n; | ||
209 | } | ||
210 | |||
211 | void CfgParser::addLabel(const QString& name, const QString& state) { | ||
212 | labels.insert(state, name); | ||
213 | labelList.append(&labels.find(state).data()); | ||
214 | } | ||
215 | |||
216 | void CfgParser::addFile(const QString& file, const QString& prefix) { | ||
217 | includes.insert(file, prefix); | ||
218 | includeList.append(&includes.find(file).key()); | ||
219 | } | ||
220 | |||
221 | QString CfgParser::errorString() { | ||
222 | return err; | ||
223 | } | ||
224 | |||
225 | bool 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 | |||
236 | bool 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 | |||
247 | bool 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 | ||
259 | CfgHandler::CfgHandler(CfgParser& c):cfg(c) { | ||
260 | } | ||
261 | |||
262 | CfgHandler::~CfgHandler() { | ||
263 | } | ||
264 | |||
265 | bool 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 | |||
291 | bool CfgHandler::startIncludeElement(const QString& file, | ||
292 | const QString& pref) { | ||
293 | |||
294 | cfg.addFile(file, pref); | ||
295 | return true; | ||
296 | } | ||
297 | |||
298 | bool CfgHandler::startLabelElement(const QString& label, | ||
299 | const QString& state) { | ||
300 | |||
301 | cfg.addLabel(label, state); | ||
302 | return true; | ||
303 | } | ||
304 | |||
305 | bool CfgHandler::startStateElement(const QString&, const QString&, bool) { | ||
306 | |||
307 | return true; | ||
308 | } | ||
309 | |||
310 | bool CfgHandler::startMapElement(int, bool) { | ||
311 | return true; | ||
312 | } | ||
313 | |||
314 | bool CfgHandler::startEventElement(int, int, int, bool, bool) { | ||
315 | return true; | ||
316 | } | ||
317 | |||
318 | bool CfgHandler::startNextStateElement(const QString&) { | ||
319 | return true; | ||
320 | } | ||
321 | |||
322 | |||
323 | bool CfgHandler::endKeymapElement() { | ||
324 | return true; | ||
325 | } | ||
326 | |||
327 | bool CfgHandler::endIncludeElement() { | ||
328 | return true; | ||
329 | } | ||
330 | |||
331 | bool CfgHandler::endLabelElement() { | ||
332 | return true; | ||
333 | } | ||
334 | |||
335 | bool CfgHandler::endStateElement() { | ||
336 | return true; | ||
337 | } | ||
338 | |||
339 | bool CfgHandler::endMapElement() { | ||
340 | return true; | ||
341 | } | ||
342 | |||
343 | bool CfgHandler::endEventElement() { | ||
344 | return true; | ||
345 | } | ||
346 | |||
347 | bool CfgHandler::endNextStateElement() { | ||
348 | return true; | ||
349 | } | ||