Diffstat (limited to 'pwmanager/pwmanager/serializer.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | pwmanager/pwmanager/serializer.cpp | 664 |
1 files changed, 664 insertions, 0 deletions
diff --git a/pwmanager/pwmanager/serializer.cpp b/pwmanager/pwmanager/serializer.cpp new file mode 100644 index 0000000..65e442d --- a/dev/null +++ b/pwmanager/pwmanager/serializer.cpp | |||
@@ -0,0 +1,664 @@ | |||
1 | /*************************************************************************** | ||
2 | * * | ||
3 | * copyright (C) 2004 by Michael Buesch * | ||
4 | * email: mbuesch@freenet.de * | ||
5 | * * | ||
6 | * This program is free software; you can redistribute it and/or modify * | ||
7 | * it under the terms of the GNU General Public License version 2 * | ||
8 | * as published by the Free Software Foundation. * | ||
9 | * * | ||
10 | ***************************************************************************/ | ||
11 | |||
12 | |||
13 | /*************************************************************************** | ||
14 | * copyright (C) 2004 by Ulf Schenk | ||
15 | * This file is originaly based on version 2.0 of pwmanager | ||
16 | * and was modified to run on embedded devices that run microkde | ||
17 | * | ||
18 | * $Id$ | ||
19 | **************************************************************************/ | ||
20 | |||
21 | #include "serializer.h" | ||
22 | #include "pwmexception.h" | ||
23 | |||
24 | #ifdef PWM_EMBEDDED | ||
25 | #include <klocale.h> | ||
26 | #endif | ||
27 | |||
28 | /* enable/disable serializer debugging (0/1) */ | ||
29 | #define SERIALIZER_DEBUG0 | ||
30 | /* use the old xml tags for writing (0/1) */ | ||
31 | #define USE_OLD_TAGS 0 | ||
32 | /* write a CDATA section (0/1) */ | ||
33 | #define WRITE_CDATA_SEC 0 | ||
34 | |||
35 | |||
36 | #define META_CREATE_DATE"c" | ||
37 | #define META_VALID_DATE "v" | ||
38 | #define META_EXPIRE_DATE"e" | ||
39 | #define META_UPDATE_DATE"u" | ||
40 | #define META_UPDATE_INT "i" | ||
41 | //US ENH : uniqueid | ||
42 | #define META_UNIQUEID "n" | ||
43 | |||
44 | /* This is compatibility stuff. | ||
45 | * The names of the entries have changed and here are the | ||
46 | * new and old ones | ||
47 | */ | ||
48 | #define ROOT_MAGIC_OLD "PwM-xml-dat" | ||
49 | #define VER_STR_OLD "ver" | ||
50 | #define COMPAT_VER_OLD "0x02" | ||
51 | #define CAT_ROOT_OLD "categories" | ||
52 | #define CAT_PREFIX_OLD "cat_" | ||
53 | #define CAT_NAME_OLD "name" | ||
54 | #define ENTRY_PREFIX_OLD"entry_" | ||
55 | #define ENTRY_DESC_OLD "desc" | ||
56 | #define ENTRY_NAME_OLD "name" | ||
57 | #define ENTRY_PW_OLD "pw" | ||
58 | #define ENTRY_COMMENT_OLD"comment" | ||
59 | #define ENTRY_URL_OLD "url" | ||
60 | #define ENTRY_LAUNCHER_OLD"launcher" | ||
61 | #define ENTRY_LVP_OLD "listViewPos" | ||
62 | #define ENTRY_BIN_OLD "b" | ||
63 | #define ENTRY_META_OLD "m" | ||
64 | |||
65 | #define ROOT_MAGIC_NEW "P" | ||
66 | #define VER_STR_NEW "v" | ||
67 | #define COMPAT_VER_NEW "2" | ||
68 | #define CAT_ROOT_NEW "c" | ||
69 | #define CAT_PREFIX_NEW "c" | ||
70 | #define CAT_NAME_NEW "n" | ||
71 | #define ENTRY_PREFIX_NEW"e" | ||
72 | #define ENTRY_DESC_NEW "d" | ||
73 | #define ENTRY_NAME_NEW "n" | ||
74 | #define ENTRY_PW_NEW "p" | ||
75 | #define ENTRY_COMMENT_NEW"c" | ||
76 | #define ENTRY_URL_NEW "u" | ||
77 | #define ENTRY_LAUNCHER_NEW"l" | ||
78 | #define ENTRY_LVP_NEW "v" | ||
79 | #define ENTRY_BIN_NEW ENTRY_BIN_OLD | ||
80 | #define ENTRY_META_NEW ENTRY_META_OLD | ||
81 | |||
82 | #if USE_OLD_TAGS != 0 | ||
83 | # define ROOT_MAGIC_WR ROOT_MAGIC_OLD | ||
84 | # define VER_STR_WR VER_STR_OLD | ||
85 | # define COMPAT_VER_WR COMPAT_VER_OLD | ||
86 | # define CAT_ROOT_WR CAT_ROOT_OLD | ||
87 | # define CAT_PREFIX_WR CAT_PREFIX_OLD | ||
88 | # define CAT_NAME_WR CAT_NAME_OLD | ||
89 | # define ENTRY_PREFIX_WRENTRY_PREFIX_OLD | ||
90 | # define ENTRY_DESC_WR ENTRY_DESC_OLD | ||
91 | # define ENTRY_NAME_WR ENTRY_NAME_OLD | ||
92 | # define ENTRY_PW_WR ENTRY_PW_OLD | ||
93 | # define ENTRY_COMMENT_WRENTRY_COMMENT_OLD | ||
94 | # define ENTRY_URL_WR ENTRY_URL_OLD | ||
95 | # define ENTRY_LAUNCHER_WRENTRY_LAUNCHER_OLD | ||
96 | # define ENTRY_LVP_WR ENTRY_LVP_OLD | ||
97 | # define ENTRY_BIN_WR ENTRY_BIN_OLD | ||
98 | # define ENTRY_META_WR ENTRY_META_OLD | ||
99 | #else | ||
100 | # define ROOT_MAGIC_WR ROOT_MAGIC_NEW | ||
101 | # define VER_STR_WR VER_STR_NEW | ||
102 | # define COMPAT_VER_WR COMPAT_VER_NEW | ||
103 | # define CAT_ROOT_WR CAT_ROOT_NEW | ||
104 | # define CAT_PREFIX_WR CAT_PREFIX_NEW | ||
105 | # define CAT_NAME_WR CAT_NAME_NEW | ||
106 | # define ENTRY_PREFIX_WRENTRY_PREFIX_NEW | ||
107 | # define ENTRY_DESC_WR ENTRY_DESC_NEW | ||
108 | # define ENTRY_NAME_WR ENTRY_NAME_NEW | ||
109 | # define ENTRY_PW_WR ENTRY_PW_NEW | ||
110 | # define ENTRY_COMMENT_WRENTRY_COMMENT_NEW | ||
111 | # define ENTRY_URL_WR ENTRY_URL_NEW | ||
112 | # define ENTRY_LAUNCHER_WRENTRY_LAUNCHER_NEW | ||
113 | # define ENTRY_LVP_WR ENTRY_LVP_NEW | ||
114 | # define ENTRY_BIN_WR ENTRY_BIN_NEW | ||
115 | # define ENTRY_META_WR ENTRY_META_NEW | ||
116 | #endif | ||
117 | |||
118 | |||
119 | Serializer::Serializer() | ||
120 | { | ||
121 | defaultLockStat = true; | ||
122 | domDoc = new QDomDocument; | ||
123 | } | ||
124 | |||
125 | Serializer::Serializer(const QCString &buffer) | ||
126 | { | ||
127 | defaultLockStat = true; | ||
128 | domDoc = new QDomDocument; | ||
129 | if (!parseXml(buffer)) { | ||
130 | delete domDoc; | ||
131 | #ifndef PWM_EMBEDDED | ||
132 | throw PwMException(PwMException::EX_PARSE); | ||
133 | #else | ||
134 | qDebug("Serializer::Serializer : Parse Exception "); | ||
135 | #endif | ||
136 | } | ||
137 | } | ||
138 | |||
139 | Serializer::~Serializer() | ||
140 | { | ||
141 | delete_ifnot_null(domDoc); | ||
142 | } | ||
143 | |||
144 | void Serializer::clear() | ||
145 | { | ||
146 | delete_ifnot_null(domDoc); | ||
147 | domDoc = new QDomDocument; | ||
148 | } | ||
149 | |||
150 | bool Serializer::parseXml(const QCString &buffer) | ||
151 | { | ||
152 | PWM_ASSERT(domDoc); | ||
153 | #ifndef PWM_EMBEDDED | ||
154 | if (!domDoc->setContent(buffer, true)) | ||
155 | return false; | ||
156 | #else | ||
157 | if (!domDoc->setContent(buffer)) | ||
158 | return false; | ||
159 | #endif | ||
160 | if (!checkValid()) | ||
161 | return false; | ||
162 | return true; | ||
163 | } | ||
164 | |||
165 | QCString Serializer::getXml() | ||
166 | { | ||
167 | PWM_ASSERT(domDoc); | ||
168 | |||
169 | #ifndef PWM_EMBEDDED | ||
170 | #if defined(PWM_DEBUG) && SERIALIZER_DEBUG != 0 | ||
171 | QCString tmp(domDoc->toCString(8)); | ||
172 | printDebug("<BEGIN Serializer::getXml() dump>\n"); | ||
173 | cout << tmp << endl; | ||
174 | printDebug("<END Serializer::getXml() dump>"); | ||
175 | #endif // DEBUG | ||
176 | |||
177 | QCString ret(domDoc->toCString(0)); | ||
178 | ret.replace('\n', ""); | ||
179 | return ret; | ||
180 | #else | ||
181 | |||
182 | #if defined(PWM_DEBUG) && SERIALIZER_DEBUG != 0 | ||
183 | QCString tmp(" " + domDoc->toCString()); | ||
184 | printDebug("<BEGIN Serializer::getXml() dump>\n"); | ||
185 | cout << tmp << endl; | ||
186 | printDebug("<END Serializer::getXml() dump>"); | ||
187 | #endif // DEBUG | ||
188 | |||
189 | QCString ret(domDoc->toCString()); | ||
190 | ret.replace(QRegExp("\n"), ""); | ||
191 | return ret; | ||
192 | |||
193 | #endif | ||
194 | } | ||
195 | |||
196 | bool Serializer::serialize(const vector<PwMCategoryItem> &dta) | ||
197 | { | ||
198 | PWM_ASSERT(domDoc); | ||
199 | QDomElement root(genNewRoot()); | ||
200 | QDomElement catNode(domDoc->createElement(CAT_ROOT_WR)); | ||
201 | root.appendChild(catNode); | ||
202 | if (!addCategories(&catNode, dta)) | ||
203 | return false; | ||
204 | return true; | ||
205 | } | ||
206 | |||
207 | bool Serializer::deSerialize(vector<PwMCategoryItem> *dta) | ||
208 | { | ||
209 | PWM_ASSERT(domDoc); | ||
210 | PWM_ASSERT(dta); | ||
211 | QDomElement root(domDoc->documentElement()); | ||
212 | QDomNode n; | ||
213 | |||
214 | dta->clear(); | ||
215 | for (n = root.firstChild(); !n.isNull(); n = n.nextSibling()) { | ||
216 | // find <categories> ... </categories> | ||
217 | // <c> ... </c> | ||
218 | if (n.nodeName() == CAT_ROOT_NEW || | ||
219 | n.nodeName() == CAT_ROOT_OLD) { | ||
220 | if (!readCategories(n, dta)) { | ||
221 | return false; | ||
222 | } | ||
223 | |||
224 | /* NOTE: We can stop processing here, as we | ||
225 | * don't have more nodes in root, yet. | ||
226 | */ | ||
227 | return true; | ||
228 | } | ||
229 | } | ||
230 | return false; | ||
231 | } | ||
232 | |||
233 | bool Serializer::readCategories(const QDomNode &n, | ||
234 | vector<PwMCategoryItem> *dta) | ||
235 | { | ||
236 | QDomNodeList nl(n.childNodes()); | ||
237 | QDomNode cur; | ||
238 | QString name; | ||
239 | unsigned int numCat = nl.count(), i; | ||
240 | PwMCategoryItem curCat; | ||
241 | vector<PwMDataItem> curEntr; | ||
242 | |||
243 | if (!numCat) { | ||
244 | printDebug("Serializer::readCategories(): empty"); | ||
245 | return false; | ||
246 | } | ||
247 | for (i = 0; i < numCat; ++i) { | ||
248 | cur = nl.item(i); | ||
249 | if (cur.nodeName().left(1) == CAT_PREFIX_NEW || | ||
250 | cur.nodeName().left(4) == CAT_PREFIX_OLD) { | ||
251 | name = cur.toElement().attribute(CAT_NAME_NEW); | ||
252 | if (name == QString::null) | ||
253 | name = cur.toElement().attribute(CAT_NAME_OLD); | ||
254 | PWM_ASSERT(name != QString::null); | ||
255 | PWM_ASSERT(name != ""); | ||
256 | curCat.clear(); | ||
257 | curCat.name = name.latin1(); | ||
258 | if (!readEntries(cur, &curEntr)) { | ||
259 | dta->clear(); | ||
260 | return false; | ||
261 | } | ||
262 | curCat.d = curEntr; | ||
263 | dta->push_back(curCat); | ||
264 | } else { | ||
265 | printDebug("Serializer::readCategories(): uh? not a category?"); | ||
266 | } | ||
267 | } | ||
268 | return true; | ||
269 | } | ||
270 | |||
271 | bool Serializer::readEntries(const QDomNode &n, | ||
272 | vector<PwMDataItem> *dta) | ||
273 | { | ||
274 | QDomNodeList nl(n.childNodes()); | ||
275 | QDomNode cur; | ||
276 | unsigned int numEntr = nl.count(), i; | ||
277 | PwMDataItem curEntr; | ||
278 | |||
279 | dta->clear(); | ||
280 | for (i = 0; i < numEntr; ++i) { | ||
281 | cur = nl.item(i); | ||
282 | if (cur.nodeName().left(1) == ENTRY_PREFIX_NEW || | ||
283 | cur.nodeName().left(6) == ENTRY_PREFIX_OLD) { | ||
284 | if (!extractEntry(cur, &curEntr)) { | ||
285 | return false; | ||
286 | } | ||
287 | dta->push_back(curEntr); | ||
288 | } else { | ||
289 | printDebug("Serializer::readEntries(): hm? not an entry?"); | ||
290 | } | ||
291 | } | ||
292 | return true; | ||
293 | } | ||
294 | |||
295 | bool Serializer::extractEntry(const QDomNode &n, | ||
296 | PwMDataItem *dta) | ||
297 | { | ||
298 | QDomNodeList nl(n.childNodes()); | ||
299 | QDomNode cur, cdata; | ||
300 | unsigned int cnt = nl.count(), i; | ||
301 | QString name, text; | ||
302 | |||
303 | if (!cnt) { | ||
304 | printDebug("Serializer::extractEntry(): empty"); | ||
305 | return false; | ||
306 | } | ||
307 | dta->clear(); | ||
308 | for (i = 0; i < cnt; ++i) { | ||
309 | cur = nl.item(i); | ||
310 | name = cur.nodeName(); | ||
311 | cdata = cur.firstChild(); | ||
312 | if (unlikely(cdata.isCDATASection())) { | ||
313 | text = cdata.toCDATASection().data(); | ||
314 | } else if (likely(cur.isElement())) { | ||
315 | text = cur.toElement().text(); | ||
316 | } else { | ||
317 | printDebug("Serializer::extractEntry(): neither CDATA nor element."); | ||
318 | return false; | ||
319 | } | ||
320 | if (text == " ") | ||
321 | text = ""; // for backward compatibility. | ||
322 | if (name == ENTRY_DESC_NEW || | ||
323 | name == ENTRY_DESC_OLD) { | ||
324 | dta->desc = unescapeEntryData(text).latin1(); | ||
325 | } else if (name == ENTRY_NAME_NEW || | ||
326 | name == ENTRY_NAME_OLD) { | ||
327 | dta->name = unescapeEntryData(text).latin1(); | ||
328 | } else if (name == ENTRY_PW_NEW || | ||
329 | name == ENTRY_PW_OLD) { | ||
330 | dta->pw = unescapeEntryData(text).latin1(); | ||
331 | } else if (name == ENTRY_COMMENT_NEW || | ||
332 | name == ENTRY_COMMENT_OLD) { | ||
333 | dta->comment = unescapeEntryData(text).latin1(); | ||
334 | } else if (name == ENTRY_URL_NEW || | ||
335 | name == ENTRY_URL_OLD) { | ||
336 | dta->url = unescapeEntryData(text).latin1(); | ||
337 | } else if (name == ENTRY_LAUNCHER_NEW || | ||
338 | name == ENTRY_LAUNCHER_OLD) { | ||
339 | dta->launcher = unescapeEntryData(text).latin1(); | ||
340 | } else if (name == ENTRY_LVP_NEW || | ||
341 | name == ENTRY_LVP_OLD) { | ||
342 | dta->listViewPos = strtol(text.latin1(), 0, 10); | ||
343 | } else if (name == ENTRY_BIN_NEW) { | ||
344 | // ENTRY_BIN_NEW == ENTRY_BIN_OLD | ||
345 | if (text == "0") { | ||
346 | dta->binary = false; | ||
347 | } else { | ||
348 | dta->binary = true; | ||
349 | } | ||
350 | } else if (name == ENTRY_META_NEW) { | ||
351 | // ENTRY_META_NEW == ENTRY_META_OLD | ||
352 | if (!extractMeta(cur, &dta->meta)) | ||
353 | return false; | ||
354 | } else { | ||
355 | printDebug(string("Serializer::extractEntry(): invalid: ") | ||
356 | + name.latin1()); | ||
357 | } | ||
358 | } | ||
359 | dta->lockStat = defaultLockStat; | ||
360 | return true; | ||
361 | } | ||
362 | |||
363 | bool Serializer::extractMeta(const QDomNode &n, | ||
364 | PwMMetaData *dta) | ||
365 | { | ||
366 | QDomNode cur(n.firstChild()); | ||
367 | QString name, val; | ||
368 | while (!cur.isNull()) { | ||
369 | name = cur.nodeName(); | ||
370 | val = cur.toElement().text(); | ||
371 | if (val == "") { | ||
372 | cur = cur.nextSibling(); | ||
373 | continue; | ||
374 | } | ||
375 | #ifndef PWM_EMBEDDED | ||
376 | if (name == META_CREATE_DATE) { | ||
377 | dta->create = QDateTime::fromString(val, Qt::ISODate); | ||
378 | } else if (name == META_VALID_DATE) { | ||
379 | dta->valid = QDateTime::fromString(val, Qt::ISODate); | ||
380 | } else if (name == META_EXPIRE_DATE) { | ||
381 | dta->expire = QDateTime::fromString(val, Qt::ISODate); | ||
382 | } else if (name == META_UPDATE_DATE) { | ||
383 | dta->update = QDateTime::fromString(val, Qt::ISODate); | ||
384 | } else if (name == META_UPDATE_INT) { | ||
385 | dta->updateInt = strtoul(val.latin1(), 0, 10); | ||
386 | } else if (name == META_UNIQUEID) { | ||
387 | dta->uniqueid = unescapeEntryData(val).latin1(); | ||
388 | } else { | ||
389 | printDebug(string("extractMeta(): invalid: ") | ||
390 | + name.latin1()); | ||
391 | } | ||
392 | #else | ||
393 | |||
394 | QDateTime m_dt; | ||
395 | |||
396 | if ((name == META_CREATE_DATE) || | ||
397 | (name == META_VALID_DATE) || | ||
398 | (name == META_EXPIRE_DATE) || | ||
399 | (name == META_UPDATE_DATE)) | ||
400 | { | ||
401 | int pos = val.find("T"); | ||
402 | QString date = val.left(pos); | ||
403 | QString time = val.mid(pos+1); | ||
404 | qDebug("Serializer::extractMeta : date=%s ,time=%s",date.latin1(), time.latin1() ); | ||
405 | bool ok1, ok2; | ||
406 | |||
407 | QDate m_date = KGlobal::locale()->readDate(date, &ok1); | ||
408 | QTime m_time = KGlobal::locale()->readTime(time, &ok2); | ||
409 | if ((ok1 == false) || (ok2 == false)) | ||
410 | qDebug("Serializer::extractMeta invalid date or time !!!!!!!!!!!!!"); | ||
411 | m_dt.setDate(m_date); | ||
412 | m_dt.setTime(m_time); | ||
413 | } | ||
414 | |||
415 | |||
416 | if (name == META_CREATE_DATE) { | ||
417 | dta->create = m_dt; | ||
418 | } else if (name == META_VALID_DATE) { | ||
419 | dta->valid = m_dt; | ||
420 | } else if (name == META_EXPIRE_DATE) { | ||
421 | dta->expire = m_dt; | ||
422 | } else if (name == META_UPDATE_DATE) { | ||
423 | dta->update = m_dt; | ||
424 | } else if (name == META_UPDATE_INT) { | ||
425 | dta->updateInt = strtoul(val.latin1(), 0, 10); | ||
426 | } else if (name == META_UNIQUEID) { | ||
427 | dta->uniqueid = unescapeEntryData(val).latin1(); | ||
428 | } else { | ||
429 | printDebug(string("extractMeta(): invalid: ") | ||
430 | + name.latin1()); | ||
431 | } | ||
432 | #endif | ||
433 | cur = cur.nextSibling(); | ||
434 | } | ||
435 | return true; | ||
436 | } | ||
437 | |||
438 | bool Serializer::checkValid() | ||
439 | { | ||
440 | PWM_ASSERT(domDoc); | ||
441 | QDomElement root(domDoc->documentElement()); | ||
442 | if (root.nodeName() != ROOT_MAGIC_NEW && | ||
443 | root.nodeName() != ROOT_MAGIC_OLD) { | ||
444 | printDebug("Serializer: wrong magic"); | ||
445 | return false; | ||
446 | } | ||
447 | if (root.attribute(VER_STR_NEW) != COMPAT_VER_NEW && | ||
448 | root.attribute(VER_STR_OLD) != COMPAT_VER_OLD) { | ||
449 | printDebug("Serializer: wrong version"); | ||
450 | return false; | ||
451 | } | ||
452 | return true; | ||
453 | } | ||
454 | |||
455 | QDomElement Serializer::genNewRoot() | ||
456 | { | ||
457 | PWM_ASSERT(domDoc); | ||
458 | QDomElement root(domDoc->createElement(ROOT_MAGIC_WR)); | ||
459 | root.setAttribute(VER_STR_WR, COMPAT_VER_WR); | ||
460 | domDoc->appendChild(root); | ||
461 | return root; | ||
462 | } | ||
463 | |||
464 | bool Serializer::addCategories(QDomElement *e, | ||
465 | const vector<PwMCategoryItem> &dta) | ||
466 | { | ||
467 | unsigned int numCat = dta.size(), i; | ||
468 | QString curId, curName; | ||
469 | QDomElement curCat; | ||
470 | |||
471 | for (i = 0; i < numCat; ++i) { | ||
472 | curId = CAT_PREFIX_WR; | ||
473 | curId += tostr(i).c_str(); | ||
474 | curName = dta[i].name.c_str(); | ||
475 | curCat = domDoc->createElement(curId); | ||
476 | curCat.setAttribute(CAT_NAME_WR, curName); | ||
477 | if (!addEntries(&curCat, dta[i].d)) { | ||
478 | return false; | ||
479 | } | ||
480 | e->appendChild(curCat); | ||
481 | } | ||
482 | return true; | ||
483 | } | ||
484 | |||
485 | bool Serializer::addEntries(QDomElement *e, | ||
486 | const vector<PwMDataItem> &dta) | ||
487 | { | ||
488 | unsigned int numEntr = dta.size(), i; | ||
489 | QString curId; | ||
490 | QDomElement curEntr; | ||
491 | |||
492 | for (i = 0; i < numEntr; ++i) { | ||
493 | curId = ENTRY_PREFIX_WR; | ||
494 | curId += tostr(i).c_str(); | ||
495 | curEntr = domDoc->createElement(curId); | ||
496 | if (!writeEntry(&curEntr, dta[i])) { | ||
497 | return false; | ||
498 | } | ||
499 | e->appendChild(curEntr); | ||
500 | } | ||
501 | return true; | ||
502 | } | ||
503 | |||
504 | bool Serializer::writeEntry(QDomElement *e, | ||
505 | const PwMDataItem &_dta) | ||
506 | { | ||
507 | #if WRITE_CDATA_SEC != 0 | ||
508 | # define new_text(x)domDoc->createCDATASection(x) | ||
509 | QDomCDATASection curText; | ||
510 | #else | ||
511 | # define new_text(x)domDoc->createTextNode(x) | ||
512 | QDomText curText; | ||
513 | #endif | ||
514 | |||
515 | QDomText plainText; | ||
516 | QDomElement tag; | ||
517 | |||
518 | // begin -- This is for compatibility with the old serializer | ||
519 | PwMDataItem dta = _dta; | ||
520 | if (!dta.desc.size()) | ||
521 | dta.desc = " "; | ||
522 | if (!dta.name.size()) | ||
523 | dta.name = " "; | ||
524 | if (!dta.pw.size()) | ||
525 | dta.pw = " "; | ||
526 | if (!dta.comment.size()) | ||
527 | dta.comment = " "; | ||
528 | if (!dta.url.size()) | ||
529 | dta.url = " "; | ||
530 | if (!dta.launcher.size()) | ||
531 | dta.launcher = " "; | ||
532 | // end -- This is for compatibility with the old serializer | ||
533 | |||
534 | tag = domDoc->createElement(ENTRY_DESC_WR); | ||
535 | curText = new_text(escapeEntryData(dta.desc.c_str())); | ||
536 | tag.appendChild(curText); | ||
537 | e->appendChild(tag); | ||
538 | |||
539 | tag = domDoc->createElement(ENTRY_NAME_WR); | ||
540 | curText = new_text(escapeEntryData(dta.name.c_str())); | ||
541 | tag.appendChild(curText); | ||
542 | e->appendChild(tag); | ||
543 | |||
544 | tag = domDoc->createElement(ENTRY_PW_WR); | ||
545 | curText = new_text(escapeEntryData(dta.pw.c_str())); | ||
546 | tag.appendChild(curText); | ||
547 | e->appendChild(tag); | ||
548 | |||
549 | tag = domDoc->createElement(ENTRY_COMMENT_WR); | ||
550 | curText = new_text(escapeEntryData(dta.comment.c_str())); | ||
551 | tag.appendChild(curText); | ||
552 | e->appendChild(tag); | ||
553 | |||
554 | tag = domDoc->createElement(ENTRY_URL_WR); | ||
555 | curText = new_text(escapeEntryData(dta.url.c_str())); | ||
556 | tag.appendChild(curText); | ||
557 | e->appendChild(tag); | ||
558 | |||
559 | tag = domDoc->createElement(ENTRY_LAUNCHER_WR); | ||
560 | curText = new_text(escapeEntryData(dta.launcher.c_str())); | ||
561 | tag.appendChild(curText); | ||
562 | e->appendChild(tag); | ||
563 | |||
564 | tag = domDoc->createElement(ENTRY_LVP_WR); | ||
565 | plainText = domDoc->createTextNode(tostr(dta.listViewPos).c_str()); | ||
566 | tag.appendChild(plainText); | ||
567 | e->appendChild(tag); | ||
568 | |||
569 | tag = domDoc->createElement(ENTRY_BIN_WR); | ||
570 | if (dta.binary) | ||
571 | plainText = domDoc->createTextNode("1"); | ||
572 | else | ||
573 | plainText = domDoc->createTextNode("0"); | ||
574 | tag.appendChild(plainText); | ||
575 | e->appendChild(tag); | ||
576 | |||
577 | tag = domDoc->createElement(ENTRY_META_WR); | ||
578 | if (!writeMeta(&tag, dta.meta)) | ||
579 | return false; | ||
580 | e->appendChild(tag); | ||
581 | |||
582 | #undef new_text | ||
583 | return true; | ||
584 | } | ||
585 | |||
586 | bool Serializer::writeMeta(QDomElement *e, | ||
587 | const PwMMetaData &dta) | ||
588 | { | ||
589 | QDomText text; | ||
590 | QDomElement tag; | ||
591 | |||
592 | tag = domDoc->createElement(META_CREATE_DATE); | ||
593 | #ifndef PWM_EMBEDDED | ||
594 | text = domDoc->createTextNode(dta.create.toString(Qt::ISODate)); | ||
595 | #else | ||
596 | text = domDoc->createTextNode(KGlobal::locale()->formatDateTime(dta.create, KLocale::ISODate)); | ||
597 | #endif | ||
598 | tag.appendChild(text); | ||
599 | e->appendChild(tag); | ||
600 | |||
601 | tag = domDoc->createElement(META_VALID_DATE); | ||
602 | #ifndef PWM_EMBEDDED | ||
603 | text = domDoc->createTextNode(dta.valid.toString(Qt::ISODate)); | ||
604 | #else | ||
605 | text = domDoc->createTextNode(KGlobal::locale()->formatDateTime(dta.valid, KLocale::ISODate)); | ||
606 | #endif | ||
607 | tag.appendChild(text); | ||
608 | e->appendChild(tag); | ||
609 | |||
610 | tag = domDoc->createElement(META_EXPIRE_DATE); | ||
611 | #ifndef PWM_EMBEDDED | ||
612 | text = domDoc->createTextNode(dta.expire.toString(Qt::ISODate)); | ||
613 | #else | ||
614 | text = domDoc->createTextNode(KGlobal::locale()->formatDateTime(dta.expire, KLocale::ISODate)); | ||
615 | #endif | ||
616 | tag.appendChild(text); | ||
617 | e->appendChild(tag); | ||
618 | |||
619 | tag = domDoc->createElement(META_UPDATE_DATE); | ||
620 | #ifndef PWM_EMBEDDED | ||
621 | text = domDoc->createTextNode(dta.update.toString(Qt::ISODate)); | ||
622 | #else | ||
623 | text = domDoc->createTextNode(KGlobal::locale()->formatDateTime(dta.update, KLocale::ISODate)); | ||
624 | #endif | ||
625 | tag.appendChild(text); | ||
626 | e->appendChild(tag); | ||
627 | |||
628 | tag = domDoc->createElement(META_UPDATE_INT); | ||
629 | text = domDoc->createTextNode(tostr(dta.updateInt).c_str()); | ||
630 | tag.appendChild(text); | ||
631 | e->appendChild(tag); | ||
632 | |||
633 | tag = domDoc->createElement(META_UNIQUEID); | ||
634 | text = domDoc->createTextNode(escapeEntryData(dta.uniqueid)); | ||
635 | tag.appendChild(text); | ||
636 | e->appendChild(tag); | ||
637 | |||
638 | #undef new_text | ||
639 | return true; | ||
640 | } | ||
641 | |||
642 | QString Serializer::escapeEntryData(QString dta) | ||
643 | { | ||
644 | #ifndef PWM_EMBEDDED | ||
645 | dta.replace('\n', "$>--endl--<$"); | ||
646 | dta.replace("]]>", "||>"); | ||
647 | #else | ||
648 | dta.replace(QRegExp("\n"), "$>--endl--<$"); | ||
649 | dta.replace(QRegExp("]]>"), "||>"); | ||
650 | #endif | ||
651 | return dta; | ||
652 | } | ||
653 | |||
654 | QString Serializer::unescapeEntryData(QString dta) | ||
655 | { | ||
656 | #ifndef PWM_EMBEDDED | ||
657 | dta.replace("$>--endl--<$", "\n"); | ||
658 | dta.replace("||>", "]]>"); | ||
659 | #else | ||
660 | dta.replace(QRegExp("$>--endl--<$"), "\n"); | ||
661 | dta.replace(QRegExp("||>"), "]]>"); | ||
662 | #endif | ||
663 | return dta; | ||
664 | } | ||