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