-rw-r--r-- | noncore/apps/tableviewer/db/.cvsignore | 3 | ||||
-rw-r--r-- | noncore/apps/tableviewer/db/common.cpp | 1470 | ||||
-rw-r--r-- | noncore/apps/tableviewer/db/common.h | 285 | ||||
-rw-r--r-- | noncore/apps/tableviewer/db/csvsource.cpp | 207 | ||||
-rw-r--r-- | noncore/apps/tableviewer/db/csvsource.h | 53 | ||||
-rw-r--r-- | noncore/apps/tableviewer/db/datacache.cpp | 293 | ||||
-rw-r--r-- | noncore/apps/tableviewer/db/datacache.h | 130 | ||||
-rw-r--r-- | noncore/apps/tableviewer/db/xmlsource.cpp | 295 | ||||
-rw-r--r-- | noncore/apps/tableviewer/db/xmlsource.h | 119 |
9 files changed, 2855 insertions, 0 deletions
diff --git a/noncore/apps/tableviewer/db/.cvsignore b/noncore/apps/tableviewer/db/.cvsignore new file mode 100644 index 0000000..a433295 --- a/dev/null +++ b/noncore/apps/tableviewer/db/.cvsignore | |||
@@ -0,0 +1,3 @@ | |||
1 | moc_* | ||
2 | Makefile | ||
3 | |||
diff --git a/noncore/apps/tableviewer/db/common.cpp b/noncore/apps/tableviewer/db/common.cpp new file mode 100644 index 0000000..4c70e54 --- a/dev/null +++ b/noncore/apps/tableviewer/db/common.cpp | |||
@@ -0,0 +1,1470 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qtopia Environment. | ||
5 | ** | ||
6 | ** This file may be distributed and/or modified under the terms of the | ||
7 | ** GNU General Public License version 2 as published by the Free Software | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | #include <stdlib.h> | ||
21 | #include <qstring.h> | ||
22 | #include <qheader.h> | ||
23 | #include <qvector.h> | ||
24 | #include <qdatetime.h> | ||
25 | #include <timestring.h> | ||
26 | #include "common.h" | ||
27 | #include "datacache.h" | ||
28 | #include <assert.h> | ||
29 | |||
30 | static const int del_flag = 0x1; | ||
31 | static const int new_flag = 0x2; | ||
32 | |||
33 | /* Helper function */ | ||
34 | |||
35 | int parseNextNumber(QString *q) { | ||
36 | QChar c; | ||
37 | uint i; | ||
38 | int result = 0; | ||
39 | |||
40 | bool found_digits = FALSE; | ||
41 | for(i = 0; i < q->length(); i++) { | ||
42 | c = q->at(i); | ||
43 | if (c.isDigit()) { | ||
44 | if (found_digits) | ||
45 | result *= 10; | ||
46 | found_digits = TRUE; | ||
47 | result += c.digitValue(); | ||
48 | } else { | ||
49 | if (found_digits) | ||
50 | break; | ||
51 | /* just skip this char */ | ||
52 | } | ||
53 | } | ||
54 | /* now truncate q */ | ||
55 | if (found_digits) | ||
56 | q->remove(0, i); | ||
57 | return result; | ||
58 | } | ||
59 | |||
60 | /*! | ||
61 | \class QStringVector | ||
62 | \brief A Vector of QStrings that can be sorted and searched | ||
63 | |||
64 | Implmented in order to allow reverse lookup on the string name | ||
65 | |||
66 | */ | ||
67 | |||
68 | /*! | ||
69 | This function implements the compare function in order to allow the | ||
70 | searching and sorting of the QStringVector to occur | ||
71 | |||
72 | \returns an int which is either | ||
73 | <UL> | ||
74 | <LI> < 0 if the first string is smaller than the second,</LI> | ||
75 | <LI> > 0 if the first string is bigger then the second,</LI> | ||
76 | <LI> == 0 if the first string is equal to the second.</LI> | ||
77 | </UL> | ||
78 | */ | ||
79 | int QStringVector::compareItems(Item a, Item b) | ||
80 | { | ||
81 | QString *qa = (QString *)a; | ||
82 | QString *qb = (QString *)b; | ||
83 | |||
84 | return QString::compare(*qa, *qb); | ||
85 | } | ||
86 | |||
87 | /*! | ||
88 | \class TVVariant | ||
89 | A way of abstracting void * and keeping information on | ||
90 | the keytypes and behaviours in one place | ||
91 | */ | ||
92 | |||
93 | TVVariantPrivate::TVVariantPrivate() | ||
94 | { | ||
95 | typ = TVVariant::Invalid; | ||
96 | } | ||
97 | |||
98 | TVVariantPrivate::TVVariantPrivate( TVVariantPrivate *d) | ||
99 | { | ||
100 | switch(d->typ) | ||
101 | { | ||
102 | case TVVariant::Invalid: | ||
103 | break; | ||
104 | case TVVariant::String: | ||
105 | value.ptr = new QString(*((QString *)d->value.ptr)); | ||
106 | break; | ||
107 | case TVVariant::Date: | ||
108 | value.ptr = new QDate(*((QDate *)d->value.ptr)); | ||
109 | break; | ||
110 | case TVVariant::Time: | ||
111 | value.ptr = new QTime(*((QTime *)d->value.ptr)); | ||
112 | break; | ||
113 | case TVVariant::Int: | ||
114 | value.i = d->value.i; | ||
115 | break; | ||
116 | default: | ||
117 | ASSERT( 0 ); | ||
118 | } | ||
119 | |||
120 | typ = d->typ; | ||
121 | } | ||
122 | |||
123 | TVVariantPrivate::~TVVariantPrivate() | ||
124 | { | ||
125 | clear(); | ||
126 | } | ||
127 | |||
128 | void TVVariantPrivate::clear() | ||
129 | { | ||
130 | switch( typ ) | ||
131 | { | ||
132 | case TVVariant::String: | ||
133 | delete (QString *)value.ptr; | ||
134 | break; | ||
135 | case TVVariant::Date: | ||
136 | delete (QDate *)value.ptr; | ||
137 | break; | ||
138 | case TVVariant::Time: | ||
139 | delete (QTime *)value.ptr; | ||
140 | break; | ||
141 | case TVVariant::Invalid: | ||
142 | case TVVariant::Int: | ||
143 | break; | ||
144 | } | ||
145 | |||
146 | typ = TVVariant::Invalid; | ||
147 | } | ||
148 | |||
149 | /*! | ||
150 | \class TVVariant | ||
151 | blah | ||
152 | */ | ||
153 | |||
154 | TVVariant::TVVariant() | ||
155 | { | ||
156 | d = new TVVariantPrivate; | ||
157 | } | ||
158 | |||
159 | TVVariant::~TVVariant() | ||
160 | { | ||
161 | if (d->deref()) | ||
162 | delete d; | ||
163 | } | ||
164 | |||
165 | TVVariant::TVVariant(const TVVariant& p) | ||
166 | { | ||
167 | d = new TVVariantPrivate; | ||
168 | *this = p; | ||
169 | } | ||
170 | |||
171 | TVVariant::TVVariant(QDataStream& s) | ||
172 | { | ||
173 | d = new TVVariantPrivate; | ||
174 | s >> *this; | ||
175 | } | ||
176 | |||
177 | TVVariant::TVVariant(const QString &val) | ||
178 | { | ||
179 | d = new TVVariantPrivate; | ||
180 | d->typ = String; | ||
181 | d->value.ptr = new QString(val); | ||
182 | } | ||
183 | |||
184 | TVVariant::TVVariant(const QDate &val) | ||
185 | { | ||
186 | d = new TVVariantPrivate; | ||
187 | d->typ = Date; | ||
188 | d->value.ptr = new QDate(val); | ||
189 | } | ||
190 | |||
191 | TVVariant::TVVariant(const QTime &val) | ||
192 | { | ||
193 | d = new TVVariantPrivate; | ||
194 | d->typ = Time; | ||
195 | d->value.ptr = new QTime(val); | ||
196 | } | ||
197 | |||
198 | TVVariant::TVVariant( int val ) | ||
199 | { | ||
200 | d = new TVVariantPrivate; | ||
201 | d->typ = Int; | ||
202 | d->value.i = val; | ||
203 | } | ||
204 | |||
205 | TVVariant& TVVariant::operator=(const TVVariant& variant ) | ||
206 | { | ||
207 | TVVariant& other = (TVVariant&) variant; | ||
208 | |||
209 | other.d->ref(); | ||
210 | if ( d->deref() ) | ||
211 | delete d; | ||
212 | |||
213 | d = other.d; | ||
214 | |||
215 | return *this; | ||
216 | } | ||
217 | |||
218 | void TVVariant::detach() | ||
219 | { | ||
220 | if (d->count == 1) | ||
221 | return; | ||
222 | |||
223 | d->deref(); | ||
224 | d = new TVVariantPrivate(d); | ||
225 | } | ||
226 | |||
227 | const QString TVVariant::typeName() const | ||
228 | { | ||
229 | return typeToName(d->typ); | ||
230 | } | ||
231 | |||
232 | void TVVariant::clear() | ||
233 | { | ||
234 | if (d->count > 1) | ||
235 | { | ||
236 | d->deref(); | ||
237 | d = new TVVariantPrivate; | ||
238 | return; | ||
239 | } | ||
240 | |||
241 | d->clear(); | ||
242 | } | ||
243 | |||
244 | const QString TVVariant::typeToName(KeyType typ) | ||
245 | { | ||
246 | switch(typ) { | ||
247 | case String: | ||
248 | return QString("String"); | ||
249 | case Date: | ||
250 | return QString("Date"); | ||
251 | case Time: | ||
252 | return QString("Time"); | ||
253 | case Int: | ||
254 | return QString("Int"); | ||
255 | case Invalid: | ||
256 | default: | ||
257 | return QString("Invalid"); | ||
258 | } | ||
259 | return QString("Invalid"); | ||
260 | } | ||
261 | |||
262 | TVVariant::KeyType TVVariant::nameToType(const QString &name) | ||
263 | { | ||
264 | if(!qstrcmp("String", name)) | ||
265 | return String; | ||
266 | if(!qstrcmp("Date", name)) | ||
267 | return Date; | ||
268 | if(!qstrcmp("Time", name)) | ||
269 | return Time; | ||
270 | if(!qstrcmp("Int", name)) | ||
271 | return Int; | ||
272 | |||
273 | return Invalid; | ||
274 | } | ||
275 | |||
276 | void TVVariant::load(QDataStream &s ) | ||
277 | { | ||
278 | KeyType t; | ||
279 | s >> t; | ||
280 | |||
281 | d->typ = t; | ||
282 | switch(t) { | ||
283 | case Invalid: | ||
284 | d->typ = t; | ||
285 | break; | ||
286 | case String: | ||
287 | { | ||
288 | QString *x = new QString; | ||
289 | s >> *x; | ||
290 | d->value.ptr = x; | ||
291 | } | ||
292 | break; | ||
293 | case Time: | ||
294 | { | ||
295 | QTime *x = new QTime; | ||
296 | s >> *x; | ||
297 | d->value.ptr = x; | ||
298 | } | ||
299 | break; | ||
300 | case Date: | ||
301 | { | ||
302 | QDate *x = new QDate; | ||
303 | s >> *x; | ||
304 | d->value.ptr = x; | ||
305 | } | ||
306 | break; | ||
307 | case Int: | ||
308 | { | ||
309 | int x; | ||
310 | s >> x; | ||
311 | d->value.i = x; | ||
312 | } | ||
313 | break; | ||
314 | default: | ||
315 | qFatal("Unrecognized data type"); | ||
316 | } | ||
317 | } | ||
318 | |||
319 | void TVVariant::save( QDataStream &s ) const | ||
320 | { | ||
321 | s << type(); | ||
322 | |||
323 | switch( d->typ ) { | ||
324 | case String: | ||
325 | s << *((QString *)d->value.ptr); | ||
326 | break; | ||
327 | case Date: | ||
328 | s << *((QDate *)d->value.ptr); | ||
329 | break; | ||
330 | case Time: | ||
331 | s << *((QTime *)d->value.ptr); | ||
332 | break; | ||
333 | case Int: | ||
334 | s << d->value.i; | ||
335 | break; | ||
336 | case Invalid: | ||
337 | break; | ||
338 | } | ||
339 | } | ||
340 | |||
341 | QDataStream& operator>>(QDataStream& s, TVVariant& p) | ||
342 | { | ||
343 | p.load( s ); | ||
344 | return s; | ||
345 | } | ||
346 | |||
347 | QDataStream& operator<<(QDataStream &s, const TVVariant& p) | ||
348 | { | ||
349 | p.save( s ); | ||
350 | return s; | ||
351 | } | ||
352 | |||
353 | QDataStream& operator>> (QDataStream &s, TVVariant::KeyType& p) | ||
354 | { | ||
355 | Q_UINT8 u = 0; | ||
356 | s >> u; | ||
357 | p = (TVVariant::KeyType) u; | ||
358 | |||
359 | return s; | ||
360 | } | ||
361 | |||
362 | QDataStream& operator<< (QDataStream& s, const TVVariant::KeyType& p) | ||
363 | { | ||
364 | s << (Q_UINT8)p; | ||
365 | return s; | ||
366 | } | ||
367 | |||
368 | const QString TVVariant::toString() const | ||
369 | { | ||
370 | switch(d->typ) { | ||
371 | case String: | ||
372 | return *((QString*)d->value.ptr); | ||
373 | case Date: | ||
374 | return ((QDate*)d->value.ptr)->toString(); | ||
375 | case Time: | ||
376 | return ((QTime*)d->value.ptr)->toString(); | ||
377 | case Int: | ||
378 | return QString::number(d->value.i); | ||
379 | case Invalid: | ||
380 | default: | ||
381 | return QString::null; | ||
382 | } | ||
383 | return QString::null; | ||
384 | } | ||
385 | |||
386 | // TODO DO, this properly, */ | ||
387 | int TVVariant::toInt() const | ||
388 | { | ||
389 | if(d->typ == Int) | ||
390 | return d->value.i; | ||
391 | |||
392 | if(d->typ == String) { | ||
393 | QString tmpq(*(QString *)d->value.ptr); | ||
394 | return parseNextNumber(&tmpq); | ||
395 | } | ||
396 | |||
397 | return 0; | ||
398 | } | ||
399 | |||
400 | const QDate TVVariant::toDate() const | ||
401 | { | ||
402 | if(d->typ == Date) | ||
403 | return *((QDate *)d->value.ptr); | ||
404 | |||
405 | if(d->typ == String) { | ||
406 | QString q = toString(); | ||
407 | |||
408 | /* date format is day mon d yyyy */ | ||
409 | /* ignore the first three letters, read the next | ||
410 | three for month.. etc */ | ||
411 | |||
412 | int day = parseNextNumber(&q); | ||
413 | int month = parseNextNumber(&q); | ||
414 | int year = parseNextNumber(&q); | ||
415 | if (!QDate::isValid(year, month, day)) | ||
416 | return QDate(); | ||
417 | return QDate(year, month, day); | ||
418 | } | ||
419 | |||
420 | |||
421 | return QDate(); | ||
422 | } | ||
423 | |||
424 | const QTime TVVariant::toTime() const | ||
425 | { | ||
426 | if(d->typ == Time) | ||
427 | return *((QTime *)d->value.ptr); | ||
428 | |||
429 | if(d->typ == String) { | ||
430 | QString q = toString(); | ||
431 | int hour = parseNextNumber(&q); | ||
432 | int minute = parseNextNumber(&q); | ||
433 | int second = parseNextNumber(&q); | ||
434 | int msecond = parseNextNumber(&q); | ||
435 | if (!QTime::isValid(hour, minute, second, msecond)) | ||
436 | return QTime(); | ||
437 | return QTime(hour, minute, second, msecond); | ||
438 | } | ||
439 | |||
440 | return QTime(); | ||
441 | } | ||
442 | |||
443 | #define TV_VARIANT_AS( f ) Q##f& TVVariant::as##f() { \ | ||
444 | if ( d->typ != f ) \ | ||
445 | *this = TVVariant( to##f() ); \ | ||
446 | else \ | ||
447 | detach(); \ | ||
448 | return *((Q##f*)d->value.ptr); } | ||
449 | |||
450 | TV_VARIANT_AS(String) | ||
451 | TV_VARIANT_AS(Date) | ||
452 | TV_VARIANT_AS(Time) | ||
453 | |||
454 | #undef TV_VARIANT_AS | ||
455 | |||
456 | int& TVVariant::asInt() | ||
457 | { | ||
458 | detach(); | ||
459 | if (d->typ != Int) { | ||
460 | d->value.i = toInt(); | ||
461 | d->typ = Int; | ||
462 | } | ||
463 | return d->value.i; | ||
464 | } | ||
465 | |||
466 | /*! | ||
467 | valid cast is | ||
468 | anything to String | ||
469 | same to same | ||
470 | */ | ||
471 | bool TVVariant::canCast(KeyType t) const | ||
472 | { | ||
473 | if(d->typ == t) | ||
474 | return TRUE; | ||
475 | |||
476 | if(t == String) | ||
477 | return TRUE; | ||
478 | |||
479 | if(t == Int) { | ||
480 | if (d->typ == Date) | ||
481 | return TRUE; | ||
482 | if (d->typ == Time) | ||
483 | return TRUE; | ||
484 | if (d->typ == String) | ||
485 | return TRUE; | ||
486 | } | ||
487 | |||
488 | return FALSE; | ||
489 | } | ||
490 | |||
491 | bool TVVariant::operator==( const TVVariant &v ) const | ||
492 | { | ||
493 | switch(d->typ) { | ||
494 | case String: | ||
495 | return v.toString() == toString(); | ||
496 | case Date: | ||
497 | return v.toDate() == toDate(); | ||
498 | case Time: | ||
499 | return v.toTime() == toTime(); | ||
500 | case Int: | ||
501 | return v.toInt() == toInt(); | ||
502 | case Invalid: | ||
503 | break; | ||
504 | } | ||
505 | |||
506 | return FALSE; | ||
507 | } | ||
508 | |||
509 | bool TVVariant::operator!=( const TVVariant &v ) const | ||
510 | { | ||
511 | return !( v == *this); | ||
512 | } | ||
513 | |||
514 | bool TVVariant::operator<( const TVVariant &v ) const | ||
515 | { | ||
516 | switch(d->typ) { | ||
517 | case String: | ||
518 | return toString().lower() < v.toString().lower(); | ||
519 | case Date: | ||
520 | return toDate() < v.toDate(); | ||
521 | case Time: | ||
522 | return toTime() < v.toTime(); | ||
523 | case Int: | ||
524 | return toInt() < v.toInt(); | ||
525 | case Invalid: | ||
526 | default: | ||
527 | break; | ||
528 | } | ||
529 | return FALSE; | ||
530 | } | ||
531 | |||
532 | bool TVVariant::operator>( const TVVariant &v ) const | ||
533 | { | ||
534 | switch(d->typ) { | ||
535 | case String: | ||
536 | return toString().lower() > v.toString().lower(); | ||
537 | case Date: | ||
538 | return toDate() > v.toDate(); | ||
539 | case Time: | ||
540 | return toTime() > v.toTime(); | ||
541 | case Int: | ||
542 | return toInt() > v.toInt(); | ||
543 | case Invalid: | ||
544 | default: | ||
545 | break; | ||
546 | } | ||
547 | return FALSE; | ||
548 | } | ||
549 | |||
550 | /*! True if n is closer to this than o */ | ||
551 | bool TVVariant::closer(TVVariant n, TVVariant o) | ||
552 | { | ||
553 | /* Nothing is close to an invalid, so nothing can be closer */ | ||
554 | if(d->typ == Invalid) | ||
555 | return FALSE; | ||
556 | |||
557 | /* can't be closer if of different type */ | ||
558 | if(n.type() != type()) | ||
559 | return FALSE; | ||
560 | |||
561 | /* if new shares type, and old doesn't, then new is closer */ | ||
562 | if(o.type() != type()) | ||
563 | return TRUE; | ||
564 | |||
565 | switch(type()){ | ||
566 | case String: { | ||
567 | /* case for strings is close is a substring.. closer is | ||
568 | * earlier alphabetically */ | ||
569 | QString qs1 = n.toString().lower(); | ||
570 | QString qs2 = o.toString().lower(); | ||
571 | QString qsv = toString().lower(); | ||
572 | |||
573 | if (!qs1.startsWith(qsv)) | ||
574 | return FALSE; | ||
575 | |||
576 | /* contains sub-str, if later than is not closer */ | ||
577 | if(QString::compare(qs1, qs2) > 0) | ||
578 | return FALSE; | ||
579 | return TRUE; | ||
580 | } | ||
581 | case Int: { | ||
582 | /* case for int is smallest absolute difference */ | ||
583 | int i1 = n.toInt(); | ||
584 | int i2 = o.toInt(); | ||
585 | int iv = toInt(); | ||
586 | |||
587 | int diff1 = (i1 - iv); | ||
588 | if (diff1 < 0) | ||
589 | diff1 = -diff1; | ||
590 | int diff2 = (i2 - iv); | ||
591 | if (diff2 < 0) | ||
592 | diff2 = -diff2; | ||
593 | |||
594 | if (diff1 < diff2) | ||
595 | return TRUE; | ||
596 | return FALSE; | ||
597 | } | ||
598 | case Date: { | ||
599 | QDate i1 = n.toDate(); | ||
600 | QDate i2 = o.toDate(); | ||
601 | QDate iv = toDate(); | ||
602 | |||
603 | /* definition of closer is the least difference in days */ | ||
604 | int diff1 = i1.daysTo(iv); | ||
605 | if (diff1 < 0) | ||
606 | diff1 = -diff1; | ||
607 | int diff2 = i2.daysTo(iv); | ||
608 | if (diff2 < 0) | ||
609 | diff2 = -diff2; | ||
610 | |||
611 | if (diff1 < diff2) | ||
612 | return TRUE; | ||
613 | return FALSE; | ||
614 | } | ||
615 | case Time: { | ||
616 | QTime i1 = n.toTime(); | ||
617 | QTime i2 = o.toTime(); | ||
618 | QTime iv = toTime(); | ||
619 | |||
620 | /* definition of closer is the least difference in days */ | ||
621 | int diff1 = i1.msecsTo(iv); | ||
622 | if (diff1 < 0) | ||
623 | diff1 = -diff1; | ||
624 | int diff2 = i2.msecsTo(iv); | ||
625 | if (diff2 < 0) | ||
626 | diff2 = -diff2; | ||
627 | if (diff1 < diff2) | ||
628 | return TRUE; | ||
629 | return FALSE; | ||
630 | } | ||
631 | default: | ||
632 | /* don't know how to do 'closer' on this type, hence never closer | ||
633 | * or even close */ | ||
634 | break; | ||
635 | } | ||
636 | return FALSE; | ||
637 | } | ||
638 | |||
639 | /*! True if n is close to this */ | ||
640 | bool TVVariant::close(TVVariant n) | ||
641 | { | ||
642 | /* Nothing is close to an invalid, so nothing can be closer */ | ||
643 | if(type() == Invalid) | ||
644 | return FALSE; | ||
645 | |||
646 | /* can't be close if of different type */ | ||
647 | if(n.type() != type()) | ||
648 | return FALSE; | ||
649 | |||
650 | switch(type()){ | ||
651 | case String: { | ||
652 | /* case for strings is close is a substring.. closer is | ||
653 | * earlier alphabetically */ | ||
654 | QString qs1 = n.toString().lower(); | ||
655 | QString qsv = toString().lower(); | ||
656 | |||
657 | if (!qs1.startsWith(qsv)) | ||
658 | return FALSE; | ||
659 | return TRUE; | ||
660 | } | ||
661 | case Int: | ||
662 | case Date: | ||
663 | case Time: | ||
664 | return TRUE; | ||
665 | default: | ||
666 | /* don't know how to do 'closer' on this type, hence never closer | ||
667 | * or even close */ | ||
668 | break; | ||
669 | } | ||
670 | return FALSE; | ||
671 | } | ||
672 | |||
673 | /*! | ||
674 | \class Key | ||
675 | \brief document me! | ||
676 | |||
677 | document me! | ||
678 | */ | ||
679 | |||
680 | Key::Key() : kname(), kexample(), kflags(0) { } | ||
681 | |||
682 | Key::Key(QString name, TVVariant example, int flags = 0) : | ||
683 | kname(name), kexample(example), kflags(flags) { } | ||
684 | |||
685 | Key::Key(const Key &other) | ||
686 | { | ||
687 | kname = other.kname; | ||
688 | kexample = other.kexample; | ||
689 | kflags = other.kflags; | ||
690 | } | ||
691 | |||
692 | Key& Key::operator=(const Key& key) | ||
693 | { | ||
694 | kname = key.kname; | ||
695 | kexample = key.kexample; | ||
696 | kflags = key.kflags; | ||
697 | return *this; | ||
698 | } | ||
699 | |||
700 | QString Key::name() const | ||
701 | { | ||
702 | return QString(kname); | ||
703 | } | ||
704 | |||
705 | TVVariant Key::example() const | ||
706 | { | ||
707 | return TVVariant(kexample); | ||
708 | } | ||
709 | |||
710 | TVVariant::KeyType Key::type() const | ||
711 | { | ||
712 | return kexample.type(); | ||
713 | } | ||
714 | |||
715 | void Key::setName(const QString &name) | ||
716 | { | ||
717 | kname = QString(name); | ||
718 | } | ||
719 | |||
720 | void Key::setExample(const TVVariant &e) | ||
721 | { | ||
722 | kexample = TVVariant(e); | ||
723 | } | ||
724 | |||
725 | int Key::flags() const | ||
726 | { | ||
727 | return kflags; | ||
728 | } | ||
729 | |||
730 | void Key::setFlags(int fl) | ||
731 | { | ||
732 | kflags = fl; | ||
733 | } | ||
734 | |||
735 | bool Key::delFlag() const | ||
736 | { | ||
737 | if(kflags & del_flag) | ||
738 | return TRUE; | ||
739 | return FALSE; | ||
740 | } | ||
741 | |||
742 | bool Key::newFlag() const | ||
743 | { | ||
744 | if(kflags & new_flag) | ||
745 | return TRUE; | ||
746 | return FALSE; | ||
747 | } | ||
748 | |||
749 | void Key::setDelFlag(bool v) | ||
750 | { | ||
751 | if(delFlag() != v) | ||
752 | kflags = kflags ^ del_flag; | ||
753 | } | ||
754 | |||
755 | void Key::setNewFlag(bool v) | ||
756 | { | ||
757 | if(newFlag() != v) | ||
758 | kflags = kflags ^ new_flag; | ||
759 | } | ||
760 | |||
761 | /*! | ||
762 | \class KeyList | ||
763 | \brief A represntation of keys used for a table. | ||
764 | |||
765 | The KeyList class is used to store the representation of keys used in table | ||
766 | headings by DBStore. It stores the names and types of the keys | ||
767 | */ | ||
768 | |||
769 | /*! | ||
770 | Constructs a KeyList | ||
771 | */ | ||
772 | KeyList::KeyList() : QIntDict<Key>(20) | ||
773 | { | ||
774 | setAutoDelete(TRUE); | ||
775 | } | ||
776 | |||
777 | /* Should be deep copy, but isn't */ | ||
778 | KeyList::KeyList(const KeyList &k) : QIntDict<Key>(k) | ||
779 | { | ||
780 | KeyListIterator it(k); | ||
781 | while(it.current()) { | ||
782 | replace(it.currentKey(), new Key(*it.current())); | ||
783 | ++it; | ||
784 | } | ||
785 | |||
786 | setAutoDelete(TRUE); | ||
787 | } | ||
788 | |||
789 | /*! | ||
790 | Destroys a KeyList | ||
791 | */ | ||
792 | KeyList::~KeyList() { | ||
793 | } | ||
794 | |||
795 | /* Do a comparision base on Keys */ | ||
796 | bool KeyList::operator!=(const KeyList &other) | ||
797 | { | ||
798 | KeyListIterator it(*this); | ||
799 | |||
800 | if (other.getNumFields() != getNumFields()) | ||
801 | return TRUE; | ||
802 | |||
803 | while(it.current()) { | ||
804 | //it.currentKey(), it.current(); | ||
805 | if (other.getKeyName(it.currentKey()) != getKeyName(it.currentKey())) | ||
806 | return TRUE; | ||
807 | if (other.getKeyType(it.currentKey()) != getKeyType(it.currentKey())) | ||
808 | return TRUE; | ||
809 | ++it; | ||
810 | } | ||
811 | return FALSE; | ||
812 | } | ||
813 | |||
814 | /*! | ||
815 | Returns the number of keys stored in the KeyList | ||
816 | */ | ||
817 | int KeyList::getNumFields() const | ||
818 | { | ||
819 | return count(); | ||
820 | } | ||
821 | |||
822 | /*! | ||
823 | Adds a new key to the KeyList | ||
824 | |||
825 | \param name the name of the new key | ||
826 | \param type the type of the new key | ||
827 | */ | ||
828 | int KeyList::addKey(QString name, TVVariant example) | ||
829 | { | ||
830 | int i = count(); | ||
831 | while(find(i) && (i > -1)) | ||
832 | i--; | ||
833 | replace(i, new Key(name, example, 0)); | ||
834 | return i; | ||
835 | } | ||
836 | |||
837 | int KeyList::addKey(QString name, TVVariant::KeyType type) | ||
838 | { | ||
839 | /* generate a valid type for the example? */ | ||
840 | TVVariant e = TVVariant("0"); | ||
841 | switch(type) { | ||
842 | case TVVariant::String: | ||
843 | return addKey(name, TVVariant("<undefined>").asString()); | ||
844 | break; | ||
845 | case TVVariant::Date: | ||
846 | return addKey(name, TVVariant(QDate::currentDate()).asDate()); | ||
847 | break; | ||
848 | case TVVariant::Time: | ||
849 | return addKey(name, TVVariant(QTime(0,0,0)).toTime()); | ||
850 | break; | ||
851 | case TVVariant::Int: | ||
852 | return addKey(name, TVVariant(0).toInt()); | ||
853 | break; | ||
854 | default: | ||
855 | qWarning(QObject::tr("KeyList::addKey() Cannot make default " | ||
856 | "value for type %1, Key not added.").arg(type)); | ||
857 | break; | ||
858 | } | ||
859 | return -1; | ||
860 | } | ||
861 | |||
862 | void KeyList::setKeyFlags(int i, int flag) | ||
863 | { | ||
864 | if(find(i)) | ||
865 | find(i)->setFlags(flag); | ||
866 | } | ||
867 | |||
868 | int KeyList::getKeyFlags(int i) const | ||
869 | { | ||
870 | if(find(i)) | ||
871 | return find(i)->flags(); | ||
872 | return 0; | ||
873 | } | ||
874 | |||
875 | bool KeyList::checkNewFlag(int i) const | ||
876 | { | ||
877 | if (find(i)) | ||
878 | return find(i)->newFlag(); | ||
879 | return false; | ||
880 | } | ||
881 | |||
882 | void KeyList::setNewFlag(int i, bool f) | ||
883 | { | ||
884 | if(!find(i)) | ||
885 | return; | ||
886 | find(i)->setNewFlag(f); | ||
887 | } | ||
888 | |||
889 | bool KeyList::checkDeleteFlag(int i) const | ||
890 | { | ||
891 | if (find(i)) | ||
892 | return find(i)->delFlag(); | ||
893 | return false; | ||
894 | } | ||
895 | |||
896 | void KeyList::setDeleteFlag(int i, bool f) | ||
897 | { | ||
898 | if(!find(i)) | ||
899 | return; | ||
900 | find(i)->setDelFlag(f); | ||
901 | } | ||
902 | |||
903 | /*! | ||
904 | Returns the name of the key at index i | ||
905 | */ | ||
906 | QString KeyList::getKeyName(int i) const | ||
907 | { | ||
908 | if (find (i)) | ||
909 | return find(i)->name(); | ||
910 | return QString(); | ||
911 | } | ||
912 | |||
913 | void KeyList::setKeyName(int i, const QString &n) | ||
914 | { | ||
915 | if(find(i)) | ||
916 | find(i)->setName(n); | ||
917 | } | ||
918 | |||
919 | /*! | ||
920 | Returns the type of the key at index i | ||
921 | */ | ||
922 | TVVariant::KeyType KeyList::getKeyType(int i) const | ||
923 | { | ||
924 | if(find(i)) | ||
925 | return find(i)->type(); | ||
926 | return TVVariant::Invalid; | ||
927 | } | ||
928 | |||
929 | void KeyList::setKeyType(int i, TVVariant::KeyType t) | ||
930 | { | ||
931 | if(!find(i)) | ||
932 | return; | ||
933 | switch(t) { | ||
934 | case TVVariant::String: | ||
935 | find(i)->setExample(TVVariant(QString("default"))); | ||
936 | return; | ||
937 | case TVVariant::Int: | ||
938 | find(i)->setExample(TVVariant(int(0))); | ||
939 | return; | ||
940 | case TVVariant::Date: | ||
941 | find(i)->setExample(TVVariant(QDate::currentDate())); | ||
942 | return; | ||
943 | case TVVariant::Time: | ||
944 | find(i)->setExample(TVVariant(QTime(0,0,0,0))); | ||
945 | return; | ||
946 | default: | ||
947 | break; | ||
948 | } | ||
949 | return; | ||
950 | } | ||
951 | |||
952 | TVVariant KeyList::getKeyExample(int i) const | ||
953 | { | ||
954 | if(find(i)) | ||
955 | return find(i)->example(); | ||
956 | return TVVariant(); | ||
957 | } | ||
958 | |||
959 | void KeyList::setKeyExample(int i, TVVariant example) | ||
960 | { | ||
961 | if(find(i)) | ||
962 | find(i)->setExample(example); | ||
963 | } | ||
964 | |||
965 | /*! | ||
966 | Returns the index of the key with name q | ||
967 | */ | ||
968 | int KeyList::getKeyIndex(QString q) const | ||
969 | { | ||
970 | KeyListIterator it(*this); | ||
971 | |||
972 | while(it.current()) { | ||
973 | if(it.current()->name() == q) | ||
974 | return it.currentKey(); | ||
975 | ++it; | ||
976 | } | ||
977 | return -1; | ||
978 | } | ||
979 | |||
980 | bool KeyList::validIndex(int i) const | ||
981 | { | ||
982 | if(!find(i)) | ||
983 | return FALSE; | ||
984 | if(find(i)->delFlag()) | ||
985 | return FALSE; | ||
986 | return TRUE; | ||
987 | } | ||
988 | |||
989 | QDataStream &operator<<( QDataStream &s, const KeyList &k) | ||
990 | { | ||
991 | s << k.getNumFields(); | ||
992 | |||
993 | KeyListIterator it(k); | ||
994 | |||
995 | while(it.current()) { | ||
996 | s << (Q_UINT16)it.currentKey(); | ||
997 | s << it.current()->name(); | ||
998 | s << it.current()->example(); | ||
999 | s << (Q_UINT16)it.current()->flags(); | ||
1000 | ++it; | ||
1001 | } | ||
1002 | return s; | ||
1003 | } | ||
1004 | |||
1005 | QDataStream &operator>>( QDataStream &s, KeyList &k) | ||
1006 | { | ||
1007 | int i; | ||
1008 | int size; | ||
1009 | int index = 0; | ||
1010 | int flags = 0; | ||
1011 | TVVariant type = TVVariant(); | ||
1012 | QString name; | ||
1013 | |||
1014 | s >> size; | ||
1015 | |||
1016 | for (i=0; i < size; i++) { | ||
1017 | s >> (Q_UINT16 &)index; | ||
1018 | s >> name; | ||
1019 | s >> type; | ||
1020 | s >> (Q_UINT16 &)flags; | ||
1021 | k.replace(index, new Key(name, type, flags)); | ||
1022 | } | ||
1023 | return s; | ||
1024 | } | ||
1025 | |||
1026 | /*! | ||
1027 | \class DataElem | ||
1028 | \brief A class representing a single row or element of a table in a DBStore | ||
1029 | |||
1030 | This class holds the data of a row in a table. | ||
1031 | */ | ||
1032 | |||
1033 | |||
1034 | /*! | ||
1035 | Constructs a DataElem. This function needs a container because the | ||
1036 | size, types of keys and primary key are all defined by the containing | ||
1037 | database | ||
1038 | */ | ||
1039 | DataElem::DataElem(DBStore *c) : values(20) | ||
1040 | { | ||
1041 | int size; | ||
1042 | contained = c; | ||
1043 | size = c->getNumFields(); | ||
1044 | values.setAutoDelete(TRUE); | ||
1045 | } | ||
1046 | |||
1047 | /*! | ||
1048 | Destroys a DataElem and frees memory used by the DataElem | ||
1049 | */ | ||
1050 | DataElem::~DataElem() { | ||
1051 | } | ||
1052 | |||
1053 | |||
1054 | |||
1055 | QDataStream &operator<<( QDataStream &s, const DataElem &d) | ||
1056 | { | ||
1057 | int size = d.getNumFields(); | ||
1058 | |||
1059 | s << size; /* redundent data but makes streaming easier */ | ||
1060 | KeyList k = d.getKeys(); | ||
1061 | |||
1062 | KeyListIterator it(k); | ||
1063 | |||
1064 | while(it.current()) { | ||
1065 | s << (Q_UINT16)it.currentKey(); | ||
1066 | s << d.getField(it.currentKey()); | ||
1067 | ++it; | ||
1068 | } | ||
1069 | return s; | ||
1070 | } | ||
1071 | |||
1072 | QDataStream &operator>>( QDataStream &s, DataElem &d) | ||
1073 | { | ||
1074 | int i; | ||
1075 | int size; | ||
1076 | TVVariant t; | ||
1077 | int index = 0; | ||
1078 | |||
1079 | s >> size; /* redundent data but makes streaming easier */ | ||
1080 | if (size != d.getNumFields()) { | ||
1081 | qWarning("DataSize mis-match"); | ||
1082 | return s; /* sanity check failed.. don't load */ | ||
1083 | } | ||
1084 | |||
1085 | for(i = 0; i < size; i++) { | ||
1086 | s >> (Q_UINT16)index; | ||
1087 | s >> t; | ||
1088 | d.setField(index, t); | ||
1089 | } | ||
1090 | return s; | ||
1091 | } | ||
1092 | |||
1093 | /*! Returns the number of possible (not valid) fields in the data element */ | ||
1094 | int DataElem::getNumFields() const | ||
1095 | { | ||
1096 | return contained->getNumFields(); | ||
1097 | } | ||
1098 | |||
1099 | KeyList DataElem::getKeys() const | ||
1100 | { | ||
1101 | return *(contained->getKeys()); | ||
1102 | } | ||
1103 | |||
1104 | /*! | ||
1105 | This function determines whether field index i of the element has been | ||
1106 | set yet. | ||
1107 | |||
1108 | \return A boolean value that is TRUE if the specfied field of this | ||
1109 | element has been set and FALSE if the field has not yet been set | ||
1110 | */ | ||
1111 | bool DataElem::hasValidValue(int i) const | ||
1112 | { | ||
1113 | if(!values.find(i)) | ||
1114 | return FALSE; | ||
1115 | if(!contained->getKeys()->validIndex(i)) | ||
1116 | return FALSE; | ||
1117 | return values.find(i)->isValid(); | ||
1118 | } | ||
1119 | |||
1120 | /*! | ||
1121 | This function determines whether field name qs of the element has been | ||
1122 | set yet. | ||
1123 | |||
1124 | \return A boolean value that is TRUE if the specfied field of this | ||
1125 | element has been set and FALSE if the field has not yet been set | ||
1126 | */ | ||
1127 | bool DataElem::hasValidValue(QString qs) const | ||
1128 | { | ||
1129 | int i = contained->getKeyIndex(qs); | ||
1130 | return hasValidValue(i); | ||
1131 | } | ||
1132 | |||
1133 | /*! returns the type of the field specified by index i */ | ||
1134 | TVVariant::KeyType DataElem::getFieldType(int i) const | ||
1135 | { | ||
1136 | return contained->getKeyType(i); | ||
1137 | } | ||
1138 | |||
1139 | /*! returns the type of the field specified by name qs */ | ||
1140 | TVVariant::KeyType DataElem::getFieldType(QString qs) const | ||
1141 | { | ||
1142 | int i = contained->getKeyIndex(qs); | ||
1143 | return contained->getKeyType(i); | ||
1144 | } | ||
1145 | |||
1146 | /*! | ||
1147 | returns a pointer to the data stored in field index i for this | ||
1148 | data element, (value may not be valid) | ||
1149 | */ | ||
1150 | TVVariant DataElem::getField(int i) const | ||
1151 | { | ||
1152 | if(hasValidValue(i)) | ||
1153 | return TVVariant(*values.find(i)); | ||
1154 | return TVVariant(); | ||
1155 | } | ||
1156 | |||
1157 | /*! | ||
1158 | returns a pointer to the data stored in field name qs for this | ||
1159 | data element, (value may not be valid) | ||
1160 | */ | ||
1161 | TVVariant DataElem::getField(QString qs) const | ||
1162 | { | ||
1163 | int i = contained->getKeyIndex(qs); | ||
1164 | return getField(i); | ||
1165 | } | ||
1166 | |||
1167 | /*! | ||
1168 | Sets the value of the elements field index i to the value represented in | ||
1169 | the QString q. | ||
1170 | |||
1171 | \param i index of the field to set | ||
1172 | \param q a string that can be parsed to get the value to be set | ||
1173 | */ | ||
1174 | void DataElem::setField(int i, QString q) | ||
1175 | { | ||
1176 | /* from the type of the field, parse q and store */ | ||
1177 | TVVariant::KeyType kt = contained->getKeyType(i); | ||
1178 | |||
1179 | TVVariant t = TVVariant(q); | ||
1180 | |||
1181 | switch(kt) { | ||
1182 | case TVVariant::Int: { | ||
1183 | t.asInt(); | ||
1184 | setField(i, t); | ||
1185 | return; | ||
1186 | } | ||
1187 | case TVVariant::String: { | ||
1188 | t.asString(); | ||
1189 | setField(i, t); | ||
1190 | return; | ||
1191 | } | ||
1192 | case TVVariant::Date: { | ||
1193 | t.asDate(); | ||
1194 | setField(i, t); | ||
1195 | return; | ||
1196 | } | ||
1197 | case TVVariant::Time: { | ||
1198 | t.asTime(); | ||
1199 | setField(i, t); | ||
1200 | return; | ||
1201 | } | ||
1202 | default: | ||
1203 | qWarning( | ||
1204 | QObject::tr("DataElem::setField(%1, %2) No valid type found").arg(i).arg(q) | ||
1205 | ); | ||
1206 | } | ||
1207 | } | ||
1208 | |||
1209 | /*! | ||
1210 | Sets the value of the elements field index i to the value at the pointer | ||
1211 | value. | ||
1212 | |||
1213 | \param i index of the field to set | ||
1214 | \param value a pointer to the (already allocated) value to set | ||
1215 | */ | ||
1216 | void DataElem::setField(int i, TVVariant value) | ||
1217 | { | ||
1218 | if (value.isValid()) { | ||
1219 | values.remove(i); | ||
1220 | values.replace(i, new TVVariant(value)); | ||
1221 | } | ||
1222 | } | ||
1223 | |||
1224 | /*! | ||
1225 | Sets the value of the elements field name qs to the value represented in | ||
1226 | the QString q. | ||
1227 | |||
1228 | \param qs name of the field to set | ||
1229 | \param q a string that can be parsed to get the value to be set | ||
1230 | */ | ||
1231 | void DataElem::setField(QString qs, QString q) | ||
1232 | { | ||
1233 | /* from the type of the field, parse q and store */ | ||
1234 | int i = contained->getKeyIndex(qs); | ||
1235 | setField(i, qs); | ||
1236 | } | ||
1237 | |||
1238 | /*! | ||
1239 | Sets the value of the elements field name qs to the value at the pointer | ||
1240 | value. | ||
1241 | |||
1242 | \param qs name of the field to set | ||
1243 | \param value a pointer to the (already allocated) value to set | ||
1244 | */ | ||
1245 | void DataElem::setField(QString qs, TVVariant value) | ||
1246 | { | ||
1247 | int i = contained->getKeyIndex(qs); | ||
1248 | setField(i, value); | ||
1249 | } | ||
1250 | |||
1251 | void DataElem::unsetField(int i) { | ||
1252 | values.remove(i); | ||
1253 | } | ||
1254 | |||
1255 | void DataElem::unsetField(QString qs) | ||
1256 | { | ||
1257 | int i = contained->getKeyIndex(qs); | ||
1258 | unsetField(i); | ||
1259 | } | ||
1260 | |||
1261 | /*! | ||
1262 | Converts the data element to a Rich Text QString | ||
1263 | */ | ||
1264 | QString DataElem::toQString() const | ||
1265 | { | ||
1266 | /* lets make an attempt at this function */ | ||
1267 | int i; | ||
1268 | QString scratch = ""; | ||
1269 | |||
1270 | QIntDictIterator<TVVariant> it(values); | ||
1271 | |||
1272 | while (it.current()) { | ||
1273 | i = it.currentKey(); | ||
1274 | if(hasValidValue(i)) { | ||
1275 | scratch += "<B>" + contained->getKeyName(i) + ":</B> "; | ||
1276 | scratch += getField(i).toString(); | ||
1277 | scratch += "<br>"; | ||
1278 | } | ||
1279 | ++it; | ||
1280 | } | ||
1281 | return scratch; | ||
1282 | } | ||
1283 | |||
1284 | /*! formats individual fields to strings so can be displayed */ | ||
1285 | QString DataElem::toQString(int i) const | ||
1286 | { | ||
1287 | if(hasValidValue(i)) { | ||
1288 | return getField(i).toString(); | ||
1289 | } | ||
1290 | return ""; | ||
1291 | } | ||
1292 | /*! formats individual fields to strings so can be sorted by QListView */ | ||
1293 | QString DataElem::toSortableQString(int i) const | ||
1294 | { | ||
1295 | QString scratch = ""; | ||
1296 | if(hasValidValue(i)) { | ||
1297 | switch (contained->getKeyType(i)) { | ||
1298 | case TVVariant::String: { | ||
1299 | scratch += getField(i).toString(); | ||
1300 | break; | ||
1301 | } | ||
1302 | case TVVariant::Int: { | ||
1303 | scratch.sprintf("%08d", getField(i).toInt()); | ||
1304 | break; | ||
1305 | } | ||
1306 | case TVVariant::Date: { | ||
1307 | static QDate epochD(1800, 1, 1); | ||
1308 | scratch.sprintf("%08d", | ||
1309 | epochD.daysTo(getField(i).toDate())); | ||
1310 | break; | ||
1311 | } | ||
1312 | case TVVariant::Time: { | ||
1313 | static QTime epochT(0, 0, 0); | ||
1314 | scratch.sprintf("%08d", | ||
1315 | epochT.msecsTo(getField(i).toTime())); | ||
1316 | break; | ||
1317 | } | ||
1318 | default: | ||
1319 | scratch += "Unknown type"; | ||
1320 | break; | ||
1321 | } | ||
1322 | } | ||
1323 | return scratch; | ||
1324 | } | ||
1325 | |||
1326 | /* compare functions */ | ||
1327 | |||
1328 | bool DataElem::lessThan(int i, TVVariant v) const | ||
1329 | { | ||
1330 | if (!hasValidValue(i)) return FALSE; | ||
1331 | |||
1332 | if (getField(i).type() != v.type()) | ||
1333 | return FALSE; | ||
1334 | |||
1335 | return (getField(i) < v); | ||
1336 | } | ||
1337 | |||
1338 | bool DataElem::moreThan(int i, TVVariant v) const | ||
1339 | { | ||
1340 | if (!hasValidValue(i)) return FALSE; | ||
1341 | |||
1342 | if (getField(i).type() != v.type()) | ||
1343 | return FALSE; | ||
1344 | |||
1345 | return (getField(i) > v); | ||
1346 | } | ||
1347 | |||
1348 | bool DataElem::equalTo(int i, TVVariant v) const | ||
1349 | { | ||
1350 | if (!hasValidValue(i)) return FALSE; | ||
1351 | |||
1352 | if (getField(i).type() != v.type()) | ||
1353 | return FALSE; | ||
1354 | |||
1355 | return (getField(i) == v); | ||
1356 | } | ||
1357 | |||
1358 | bool DataElem::contains(int i, TVVariant v) const | ||
1359 | { | ||
1360 | if (!hasValidValue(i)) return FALSE; | ||
1361 | |||
1362 | if (getField(i).type() != v.type()) | ||
1363 | return FALSE; | ||
1364 | |||
1365 | switch(getField(i).type()) { | ||
1366 | case TVVariant::String: { | ||
1367 | QString qs1 = getField(i).toString().lower(); | ||
1368 | QString qs2 = v.toString().lower(); | ||
1369 | if (qs1.contains(qs2) > 0) return TRUE; | ||
1370 | break; | ||
1371 | } | ||
1372 | /* meaningless for ints */ | ||
1373 | /* meaningless for time */ | ||
1374 | /* meaningless for dates */ | ||
1375 | case TVVariant::Int: | ||
1376 | case TVVariant::Time: | ||
1377 | case TVVariant::Date: | ||
1378 | break; | ||
1379 | default: | ||
1380 | qWarning("Tried to compare unknown data type"); | ||
1381 | } | ||
1382 | return FALSE; | ||
1383 | } | ||
1384 | |||
1385 | bool DataElem::startsWith(int i, TVVariant v) const | ||
1386 | { | ||
1387 | if (!hasValidValue(i)) return FALSE; | ||
1388 | |||
1389 | if (getField(i).type() != v.type()) | ||
1390 | return FALSE; | ||
1391 | |||
1392 | switch(getField(i).type()) { | ||
1393 | case TVVariant::String: { | ||
1394 | QString qs1 = getField(i).toString().lower(); | ||
1395 | QString qs2 = v.toString().lower(); | ||
1396 | return qs1.startsWith(qs2); | ||
1397 | } | ||
1398 | /* meaningless for ints */ | ||
1399 | /* meaningless for time */ | ||
1400 | /* meaningless for dates */ | ||
1401 | case TVVariant::Int: | ||
1402 | case TVVariant::Time: | ||
1403 | case TVVariant::Date: | ||
1404 | return FALSE; | ||
1405 | default: | ||
1406 | qWarning("Tried to compare unknown data type"); | ||
1407 | } | ||
1408 | return FALSE; | ||
1409 | } | ||
1410 | |||
1411 | bool DataElem::endsWith(int i, TVVariant v) const | ||
1412 | { | ||
1413 | if (!hasValidValue(i)) return FALSE; | ||
1414 | |||
1415 | if (getField(i).type() != v.type()) | ||
1416 | return FALSE; | ||
1417 | |||
1418 | switch(getField(i).type()) { | ||
1419 | case TVVariant::String: { | ||
1420 | QString qs1 = getField(i).toString().lower(); | ||
1421 | QString qs2 = v.toString().lower(); | ||
1422 | return qs1.startsWith(qs2); | ||
1423 | } | ||
1424 | /* meaningless for ints */ | ||
1425 | /* meaningless for time */ | ||
1426 | /* meaningless for dates */ | ||
1427 | case TVVariant::Int: | ||
1428 | case TVVariant::Time: | ||
1429 | case TVVariant::Date: | ||
1430 | return FALSE; | ||
1431 | default: | ||
1432 | qWarning("Tried to compare unknown data type"); | ||
1433 | } | ||
1434 | return FALSE; | ||
1435 | } | ||
1436 | |||
1437 | /*! | ||
1438 | Determins which of the first to parameters are closer to the third, target | ||
1439 | parameter. | ||
1440 | |||
1441 | \return | ||
1442 | <UL> | ||
1443 | <LI>TRUE if the first element is a closer match to the target than the | ||
1444 | second element</LI> | ||
1445 | <LI>FALSE if the first element is not a closer match to the target than | ||
1446 | the second element</LI> | ||
1447 | </UL> | ||
1448 | */ | ||
1449 | bool DataElem::closer(DataElem*d1, DataElem *d2, TVVariant target, int column) | ||
1450 | { | ||
1451 | int type; | ||
1452 | |||
1453 | if(!d1) return FALSE; | ||
1454 | |||
1455 | if (!d1->hasValidValue(column)) return FALSE; | ||
1456 | |||
1457 | if(!target.isValid()) return FALSE; | ||
1458 | |||
1459 | type = d1->getField(column).type(); | ||
1460 | |||
1461 | if(d2) { | ||
1462 | if (type != d2->getField(column).type()) { | ||
1463 | /* can't do compare */ | ||
1464 | qWarning("Tried to compare two incompatable types"); | ||
1465 | return FALSE; | ||
1466 | } | ||
1467 | return target.closer(d1->getField(column), d2->getField(column)); | ||
1468 | } | ||
1469 | return target.close(d1->getField(column)); | ||
1470 | } | ||
diff --git a/noncore/apps/tableviewer/db/common.h b/noncore/apps/tableviewer/db/common.h new file mode 100644 index 0000000..bb0a953 --- a/dev/null +++ b/noncore/apps/tableviewer/db/common.h | |||
@@ -0,0 +1,285 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qtopia Environment. | ||
5 | ** | ||
6 | ** This file may be distributed and/or modified under the terms of the | ||
7 | ** GNU General Public License version 2 as published by the Free Software | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | |||
21 | |||
22 | /* This file represents shared data structures that will be passed | ||
23 | * around often. | ||
24 | */ | ||
25 | #ifndef __SHAREDDATA_H__ | ||
26 | #define __SHAREDDATA_H__ | ||
27 | |||
28 | // TODO rename this to a sensable class name | ||
29 | |||
30 | #include <qvector.h> | ||
31 | #include <qstring.h> | ||
32 | #include <qdatetime.h> | ||
33 | #include <qcstring.h> | ||
34 | #include <qdatastream.h> | ||
35 | #include <qintdict.h> | ||
36 | |||
37 | class DBStore; | ||
38 | |||
39 | /* helper classes to common classes */ | ||
40 | class QStringVector : public QVector<QString> | ||
41 | { | ||
42 | public: | ||
43 | int compareItems(Item a, Item b); | ||
44 | }; | ||
45 | |||
46 | /* in QT 2.3, dates and times not supported int QVariant. So..... | ||
47 | * for now use my special Variant type which is basically identical | ||
48 | * except that does it for my types. TODO replace with QVariant when | ||
49 | * qvariant supports all the types we require */ | ||
50 | |||
51 | class TVVariantPrivate; | ||
52 | |||
53 | class TVVariant | ||
54 | { | ||
55 | public: | ||
56 | enum KeyType { | ||
57 | Invalid = 0, | ||
58 | Int, | ||
59 | String, | ||
60 | Date, | ||
61 | Time, | ||
62 | }; | ||
63 | |||
64 | TVVariant(); | ||
65 | ~TVVariant(); | ||
66 | |||
67 | TVVariant(const TVVariant&); | ||
68 | TVVariant(QDataStream&); | ||
69 | |||
70 | TVVariant(const QString &); | ||
71 | TVVariant(const int); | ||
72 | TVVariant(const QDate &); | ||
73 | TVVariant(const QTime &); | ||
74 | |||
75 | TVVariant& operator=(const TVVariant& ); | ||
76 | bool operator==(const TVVariant&) const; | ||
77 | bool operator!=(const TVVariant&) const; | ||
78 | bool operator<(const TVVariant&) const; | ||
79 | bool operator>(const TVVariant&) const; | ||
80 | |||
81 | bool closer(TVVariant, TVVariant); | ||
82 | bool close(TVVariant); | ||
83 | |||
84 | KeyType type() const; | ||
85 | const QString typeName() const; | ||
86 | int numTypes() const; | ||
87 | |||
88 | const QString typeName(KeyType) const; | ||
89 | bool canCast(KeyType) const; | ||
90 | bool isValid() const; | ||
91 | void clear(); | ||
92 | |||
93 | const QString toString() const; | ||
94 | const QDate toDate() const; | ||
95 | const QTime toTime() const; | ||
96 | int toInt() const; | ||
97 | |||
98 | QString& asString(); | ||
99 | QDate& asDate(); | ||
100 | QTime& asTime(); | ||
101 | int& asInt(); | ||
102 | |||
103 | void load(QDataStream&); | ||
104 | void save(QDataStream&) const; | ||
105 | |||
106 | static const QString typeToName(KeyType typ); | ||
107 | static KeyType nameToType(const QString &); | ||
108 | private: | ||
109 | void detach(); | ||
110 | |||
111 | TVVariantPrivate *d; | ||
112 | }; | ||
113 | |||
114 | class TVVariantPrivate : public QShared | ||
115 | { | ||
116 | public: | ||
117 | TVVariantPrivate(); | ||
118 | TVVariantPrivate(TVVariantPrivate *); | ||
119 | |||
120 | ~TVVariantPrivate(); | ||
121 | |||
122 | void clear(); | ||
123 | |||
124 | TVVariant::KeyType typ; | ||
125 | |||
126 | union { | ||
127 | int i; | ||
128 | void *ptr; | ||
129 | } value; | ||
130 | }; | ||
131 | |||
132 | inline TVVariant::KeyType TVVariant::type() const | ||
133 | { | ||
134 | return d->typ; | ||
135 | } | ||
136 | |||
137 | inline bool TVVariant::isValid() const | ||
138 | { | ||
139 | return (d->typ != Invalid); | ||
140 | } | ||
141 | |||
142 | inline int TVVariant::numTypes() const | ||
143 | { | ||
144 | return 4; | ||
145 | } | ||
146 | |||
147 | class Key { | ||
148 | public: | ||
149 | Key(); | ||
150 | Key(QString name, TVVariant example, int flags = 0); | ||
151 | Key(const Key &); | ||
152 | Key& operator=(const Key& ); | ||
153 | |||
154 | QString name() const; | ||
155 | TVVariant example() const; | ||
156 | TVVariant::KeyType type() const; | ||
157 | int flags() const; | ||
158 | |||
159 | void setName(const QString &); | ||
160 | void setExample(const TVVariant &); | ||
161 | void setFlags(int); | ||
162 | |||
163 | bool delFlag() const; | ||
164 | bool newFlag() const; | ||
165 | |||
166 | void setDelFlag(bool); | ||
167 | void setNewFlag(bool); | ||
168 | |||
169 | private: | ||
170 | QString kname; | ||
171 | TVVariant kexample; | ||
172 | int kflags; | ||
173 | }; | ||
174 | |||
175 | class KeyList : public QIntDict<Key> { | ||
176 | public: | ||
177 | KeyList(); | ||
178 | KeyList(const KeyList&); | ||
179 | |||
180 | ~KeyList(); | ||
181 | |||
182 | bool operator!=(const KeyList &); | ||
183 | |||
184 | int getNumFields() const; | ||
185 | |||
186 | int addKey(QString KeyName, TVVariant example); | ||
187 | int addKey(QString KeyName, TVVariant::KeyType type); | ||
188 | |||
189 | TVVariant getKeyExample(int ) const; | ||
190 | void setKeyExample(int, TVVariant e); | ||
191 | |||
192 | QString getKeyName(int i) const; | ||
193 | void setKeyName(int i, const QString &n); | ||
194 | |||
195 | TVVariant::KeyType getKeyType(int i) const; | ||
196 | void setKeyType(int i, TVVariant::KeyType); | ||
197 | |||
198 | int getKeyIndex(QString q) const; | ||
199 | |||
200 | int getKeyFlags(int i) const; | ||
201 | void setKeyFlags(int i, int flag); | ||
202 | |||
203 | /* Below should be abstracted a bit more */ | ||
204 | bool checkNewFlag(int i) const; | ||
205 | void setNewFlag(int i, bool f); | ||
206 | bool checkDeleteFlag(int i) const; | ||
207 | void setDeleteFlag(int i, bool f); | ||
208 | |||
209 | bool validIndex(int) const; | ||
210 | }; | ||
211 | |||
212 | class KeyListIterator : public QIntDictIterator<Key> | ||
213 | { | ||
214 | public: | ||
215 | KeyListIterator(const KeyList &k) : QIntDictIterator<Key>(k) {}; | ||
216 | }; | ||
217 | |||
218 | /* TODO start using this */ | ||
219 | class DataElem { | ||
220 | public: | ||
221 | DataElem(DBStore *container); | ||
222 | ~DataElem(); | ||
223 | |||
224 | int getNumFields() const; | ||
225 | KeyList getKeys() const; | ||
226 | |||
227 | bool hasValidValue(int) const; | ||
228 | bool hasValidValue(QString) const; | ||
229 | TVVariant::KeyType getFieldType(int) const; | ||
230 | TVVariant::KeyType getFieldType(QString) const; | ||
231 | TVVariant getField(int) const; | ||
232 | TVVariant getField(QString) const; | ||
233 | |||
234 | void setField(int, QString); | ||
235 | void setField(int, TVVariant); | ||
236 | void setField(QString, QString); | ||
237 | void setField(QString, TVVariant); | ||
238 | void unsetField(int); | ||
239 | void unsetField(QString); | ||
240 | |||
241 | QString toQString() const; | ||
242 | QString toQString(int i) const; | ||
243 | QString toSortableQString(int i) const; | ||
244 | |||
245 | /* compare functions */ | ||
246 | bool lessThan(int i, TVVariant) const; | ||
247 | bool moreThan(int i, TVVariant) const; | ||
248 | bool equalTo(int i, TVVariant) const; | ||
249 | bool contains(int i, TVVariant) const; | ||
250 | bool startsWith(int i, TVVariant) const; | ||
251 | bool endsWith(int i, TVVariant) const; | ||
252 | |||
253 | /* class functions... Compare is based of the primary key, which | ||
254 | is determined by the containing DBStores of each element. */ | ||
255 | static int compare(const TVVariant, const TVVariant, int i); | ||
256 | |||
257 | /* False, second element's primary key is closer to target. | ||
258 | * True, first element's primary key is a closer match to target */ | ||
259 | static bool closer(DataElem*, DataElem *, TVVariant, int column); | ||
260 | private: | ||
261 | QIntDict<TVVariant> values; | ||
262 | DBStore *contained; | ||
263 | }; | ||
264 | |||
265 | typedef struct _TableState { | ||
266 | int current_column; | ||
267 | KeyList *kRep; | ||
268 | DataElem *current_elem; | ||
269 | } TableState; | ||
270 | |||
271 | /* Stream functions */ | ||
272 | #ifndef QT_NO_DATASTREAM | ||
273 | Q_EXPORT QDataStream &operator<<( QDataStream &, const KeyList & ); | ||
274 | Q_EXPORT QDataStream &operator<<( QDataStream &, const DataElem & ); | ||
275 | Q_EXPORT QDataStream &operator>>( QDataStream &, KeyList & ); | ||
276 | Q_EXPORT QDataStream &operator>>( QDataStream &, DataElem & ); | ||
277 | |||
278 | |||
279 | Q_EXPORT QDataStream &operator>>( QDataStream &, TVVariant & ); | ||
280 | Q_EXPORT QDataStream &operator<<( QDataStream &, const TVVariant & ); | ||
281 | Q_EXPORT QDataStream &operator>>( QDataStream &, TVVariant::KeyType& ); | ||
282 | Q_EXPORT QDataStream &operator<<( QDataStream &, const TVVariant::KeyType& ); | ||
283 | #endif | ||
284 | |||
285 | #endif | ||
diff --git a/noncore/apps/tableviewer/db/csvsource.cpp b/noncore/apps/tableviewer/db/csvsource.cpp new file mode 100644 index 0000000..2561b4b --- a/dev/null +++ b/noncore/apps/tableviewer/db/csvsource.cpp | |||
@@ -0,0 +1,207 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qtopia Environment. | ||
5 | ** | ||
6 | ** This file may be distributed and/or modified under the terms of the | ||
7 | ** GNU General Public License version 2 as published by the Free Software | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | #include "csvsource.h" | ||
21 | #include "common.h" | ||
22 | #include "datacache.h" | ||
23 | #include <qtextstream.h> | ||
24 | #include <qstringlist.h> | ||
25 | #include <qmap.h> | ||
26 | #include <qregexp.h> | ||
27 | |||
28 | DBCsv::DBCsv(DBStore *d) | ||
29 | { | ||
30 | dstore = d; | ||
31 | } | ||
32 | |||
33 | DBCsv::~DBCsv() | ||
34 | { | ||
35 | } | ||
36 | |||
37 | QString DBCsv::type() | ||
38 | { | ||
39 | return "csv"; | ||
40 | } | ||
41 | |||
42 | QStringList readElem(QString in) | ||
43 | { | ||
44 | QStringList out; | ||
45 | |||
46 | if (in.isEmpty()) | ||
47 | return out; | ||
48 | |||
49 | bool firstChar = TRUE; | ||
50 | bool quotedElem = FALSE; | ||
51 | uint index = 0; | ||
52 | while(index < in.length()) { | ||
53 | if(firstChar) { | ||
54 | /* skip whitespace */ | ||
55 | while(index < in.length() && in[index] == ' ') | ||
56 | index++; | ||
57 | if(in[index] == '"') { | ||
58 | quotedElem = TRUE; | ||
59 | index++; | ||
60 | } | ||
61 | } | ||
62 | /* real first char */ | ||
63 | QString elem; | ||
64 | if(quotedElem) { | ||
65 | while(index < in.length() && in[index] != '"') { | ||
66 | /* check for escape character */ | ||
67 | if (in[index] == '\\') { | ||
68 | if (index++ < in.length()) { | ||
69 | elem.append(in[index]); | ||
70 | index++; | ||
71 | } | ||
72 | } else { | ||
73 | elem.append(in[index]); | ||
74 | index++; | ||
75 | } | ||
76 | } | ||
77 | } else { | ||
78 | while(index < in.length() && in[index] != ',') { | ||
79 | if (in[index] == '\\') { | ||
80 | if (index++ < in.length()) { | ||
81 | elem.append(in[index]); | ||
82 | index++; | ||
83 | } | ||
84 | } else { | ||
85 | elem.append(in[index]); | ||
86 | index++; | ||
87 | } | ||
88 | } | ||
89 | } | ||
90 | /* we have our current elem */ | ||
91 | out << elem.stripWhiteSpace(); | ||
92 | firstChar = TRUE; | ||
93 | quotedElem = FALSE; | ||
94 | /* skip till a , or end of line */ | ||
95 | while (index < in.length() && in[index] != ',') index++; | ||
96 | if(index == in.length()) | ||
97 | return out; | ||
98 | else | ||
99 | index++; | ||
100 | } | ||
101 | } | ||
102 | |||
103 | bool DBCsv::openSource(QIODevice *inDev) | ||
104 | { | ||
105 | QTextStream tsIn(inDev); | ||
106 | QString in = tsIn.readLine().stripWhiteSpace(); | ||
107 | QStringList keys; | ||
108 | |||
109 | keys = readElem(in); | ||
110 | |||
111 | QMap<int,int> keyIndexes; | ||
112 | |||
113 | KeyList *keyR = new KeyList(); | ||
114 | QStringList::Iterator i = keys.begin(); | ||
115 | |||
116 | uint fileIndex = 0; | ||
117 | while(i != keys.end()) { | ||
118 | if ((*i).isEmpty()) | ||
119 | keyIndexes.insert(fileIndex, keyR->addKey("Unamed", TVVariant::String)); | ||
120 | else | ||
121 | keyIndexes.insert(fileIndex, keyR->addKey(*i, TVVariant::String)); | ||
122 | i++; | ||
123 | fileIndex++; | ||
124 | } | ||
125 | dstore->setKeys(keyR); | ||
126 | |||
127 | in = tsIn.readLine().stripWhiteSpace(); | ||
128 | while(!in.isNull()) { | ||
129 | QStringList elems = readElem(in); | ||
130 | |||
131 | i = elems.begin(); | ||
132 | fileIndex = 0; | ||
133 | DataElem *current_data = new DataElem(dstore); | ||
134 | while(i != elems.end()) { | ||
135 | if(!(*i).isEmpty()) { | ||
136 | current_data->setField(keyIndexes[fileIndex], *i); | ||
137 | } | ||
138 | fileIndex++; | ||
139 | i++; | ||
140 | } | ||
141 | dstore->addItem(current_data); | ||
142 | in = tsIn.readLine().stripWhiteSpace(); | ||
143 | } | ||
144 | |||
145 | return TRUE; | ||
146 | } | ||
147 | |||
148 | bool DBCsv::saveSource(QIODevice *outDev) | ||
149 | { | ||
150 | /* try not to use the escape character when possible. */ | ||
151 | int i; | ||
152 | DataElem *elem; | ||
153 | KeyList *k; | ||
154 | QTextStream outstream(outDev); | ||
155 | |||
156 | k = dstore->getKeys(); | ||
157 | KeyListIterator it(*k); | ||
158 | while(it.current()) { | ||
159 | if(!it.current()->delFlag()) { | ||
160 | QString name = it.current()->name(); | ||
161 | |||
162 | name.replace(QRegExp("\\"), "\\\\"); | ||
163 | name.replace(QRegExp("\""), "\\\""); | ||
164 | if(name.find(',') != -1) { | ||
165 | name.prepend('\"'); | ||
166 | name.append('\"'); | ||
167 | } | ||
168 | |||
169 | outstream << name; | ||
170 | } | ||
171 | ++it; | ||
172 | if(it.current()) | ||
173 | outstream << ", "; | ||
174 | } | ||
175 | outstream << "\n"; | ||
176 | |||
177 | dstore->first(); | ||
178 | |||
179 | do { | ||
180 | elem = dstore->getCurrentData(); | ||
181 | if(!elem) | ||
182 | break; | ||
183 | it.toFirst(); | ||
184 | while(it.current()) { | ||
185 | i = it.currentKey(); | ||
186 | if (elem->hasValidValue(i)) { | ||
187 | QString name = elem->toQString(i); | ||
188 | |||
189 | name.replace(QRegExp("\\"), "\\\\"); | ||
190 | name.replace(QRegExp("\""), "\\\""); | ||
191 | if(name.find(',') != -1) { | ||
192 | name.prepend('\"'); | ||
193 | name.append('\"'); | ||
194 | } | ||
195 | |||
196 | outstream << name; | ||
197 | } | ||
198 | ++it; | ||
199 | if(it.current()) | ||
200 | outstream << ", "; | ||
201 | } | ||
202 | outstream << "\n"; | ||
203 | } while (dstore->next()); | ||
204 | |||
205 | return TRUE; | ||
206 | } | ||
207 | |||
diff --git a/noncore/apps/tableviewer/db/csvsource.h b/noncore/apps/tableviewer/db/csvsource.h new file mode 100644 index 0000000..691234c --- a/dev/null +++ b/noncore/apps/tableviewer/db/csvsource.h | |||
@@ -0,0 +1,53 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qtopia Environment. | ||
5 | ** | ||
6 | ** This file may be distributed and/or modified under the terms of the | ||
7 | ** GNU General Public License version 2 as published by the Free Software | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | |||
21 | /* A Class to parse an comma seperated values docment of the form | ||
22 | |||
23 | "Displayed Name", key2name, key 3 name, key name 4 | ||
24 | value1, , value 3, "value 4" | ||
25 | value1, , value 3, "value 4" | ||
26 | |||
27 | * '\' is the escape character. Basically \", \' and \\ escape the ", ' or \ | ||
28 | * into the actual value of the field. | ||
29 | * Before any other character will have the effect of dropping the '\'. | ||
30 | * | ||
31 | * Currently there is no support for comments. */ | ||
32 | |||
33 | #ifndef __CSVSOURCE_H__ | ||
34 | #define __CSVSOURCE_H__ | ||
35 | |||
36 | #include <qstring.h> | ||
37 | #include "datacache.h" | ||
38 | #include "common.h" | ||
39 | |||
40 | |||
41 | class DBCsv : public DBAccess | ||
42 | { | ||
43 | public: | ||
44 | /* create connection and either open or initialize */ | ||
45 | DBCsv(DBStore *d); | ||
46 | QString type(); | ||
47 | bool openSource(QIODevice *); | ||
48 | bool saveSource(QIODevice *); | ||
49 | /* does a db write */ | ||
50 | ~DBCsv(); | ||
51 | }; | ||
52 | |||
53 | #endif | ||
diff --git a/noncore/apps/tableviewer/db/datacache.cpp b/noncore/apps/tableviewer/db/datacache.cpp new file mode 100644 index 0000000..7c14eef --- a/dev/null +++ b/noncore/apps/tableviewer/db/datacache.cpp | |||
@@ -0,0 +1,293 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qtopia Environment. | ||
5 | ** | ||
6 | ** This file may be distributed and/or modified under the terms of the | ||
7 | ** GNU General Public License version 2 as published by the Free Software | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | /* | ||
21 | * This file is used to load the xml files that represent the database. | ||
22 | * The main requirment for said file is each data entry must contain a key, | ||
23 | * otherwise any other data headings are allowed. | ||
24 | */ | ||
25 | |||
26 | #include "datacache.h" | ||
27 | #include "xmlsource.h" | ||
28 | #include "csvsource.h" | ||
29 | #include <stdlib.h> | ||
30 | #include <qheader.h> | ||
31 | |||
32 | #define INIT_TABLE_SIZE 128 | ||
33 | |||
34 | /*! | ||
35 | \class DBStore datastore.h | ||
36 | |||
37 | \brief The DBStore class is the class responsible for storing, sorting and | ||
38 | searching the data loaded by the application | ||
39 | |||
40 | */ | ||
41 | |||
42 | /*! | ||
43 | Constructs a DBStore item | ||
44 | */ | ||
45 | DBStore::DBStore() | ||
46 | { | ||
47 | name = ""; | ||
48 | number_elems = 0; | ||
49 | full = false; | ||
50 | kRep = new KeyList(); | ||
51 | master_table.resize(INIT_TABLE_SIZE); | ||
52 | table_size = INIT_TABLE_SIZE; | ||
53 | |||
54 | current_elem = 0; | ||
55 | archive = 0; | ||
56 | } | ||
57 | |||
58 | //TODO | ||
59 | /*! | ||
60 | Reinitializes the table to empty (include a resize of the master table, | ||
61 | which should free some memory) | ||
62 | */ | ||
63 | void DBStore::freeTable() | ||
64 | { | ||
65 | name = ""; | ||
66 | if(archive) { | ||
67 | delete archive; | ||
68 | archive = 0; | ||
69 | } | ||
70 | kRep->clear(); /* clear the current key list */ | ||
71 | |||
72 | number_elems = 0; | ||
73 | table_size = INIT_TABLE_SIZE; | ||
74 | master_table.resize(table_size); | ||
75 | full = false; | ||
76 | current_elem = 0; | ||
77 | } | ||
78 | |||
79 | /*! | ||
80 | Removes all items from the DBStore and destroys the DBStore | ||
81 | */ | ||
82 | DBStore::~DBStore() | ||
83 | { | ||
84 | freeTable(); | ||
85 | } | ||
86 | |||
87 | /*! | ||
88 | This function opens the given xml file, loads it and sets up the | ||
89 | appropriate data structures. | ||
90 | |||
91 | \param file_name A string representing the name of the file to be opened | ||
92 | \return true if successful, false otherwise. | ||
93 | */ | ||
94 | bool DBStore::openSource(QIODevice *inDev, const QString &source) { | ||
95 | |||
96 | /* first check if db is already open, if contains data.. then clear */ | ||
97 | if(number_elems > 0) { | ||
98 | freeTable(); | ||
99 | } | ||
100 | |||
101 | if (source == "text/x-xml-tableviewer") { | ||
102 | archive = new DBXml(this); | ||
103 | } else if (source == "text/csv") { | ||
104 | archive = new DBCsv(this); | ||
105 | } else | ||
106 | return false; | ||
107 | |||
108 | return (archive->openSource(inDev)); | ||
109 | } | ||
110 | |||
111 | bool DBStore::saveSource(QIODevice *outDev, const QString &source) | ||
112 | { | ||
113 | /* saving a new file */ | ||
114 | if(!archive) { | ||
115 | if (source == "text/x-xml-tableviewer") { | ||
116 | archive = new DBXml(this); | ||
117 | } else if (source == "text/x-xml-tableviewer") { | ||
118 | archive = new DBCsv(this); | ||
119 | } else | ||
120 | return false; | ||
121 | } | ||
122 | |||
123 | /* changing file type */ | ||
124 | if(archive->type() != source) { | ||
125 | delete archive; | ||
126 | if (source == "text/x-xml-tableviewer") { | ||
127 | archive = new DBXml(this); | ||
128 | } else if (source == "text/x-xml-tableviewer") { | ||
129 | archive = new DBCsv(this); | ||
130 | } else | ||
131 | return false; | ||
132 | } | ||
133 | |||
134 | return (archive->saveSource(outDev)); | ||
135 | } | ||
136 | |||
137 | /*! | ||
138 | This function is used to add new elements to the database. If the database | ||
139 | has already reached the maximum allowable size this function does not alter | ||
140 | the database. | ||
141 | |||
142 | \param delm An already allocated and initialized data element to be added | ||
143 | */ | ||
144 | void DBStore::addItem(DataElem *delem) | ||
145 | { | ||
146 | addItemInternal(delem); | ||
147 | } | ||
148 | |||
149 | void DBStore::addItemInternal(DataElem *delem) | ||
150 | { | ||
151 | /* if already full, don't over fill, do a qWarning though */ | ||
152 | if (full) { | ||
153 | qWarning("Attempted to add items to already full table"); | ||
154 | return; | ||
155 | } | ||
156 | |||
157 | master_table.insert(number_elems, delem); | ||
158 | |||
159 | current_elem = number_elems; | ||
160 | number_elems++; | ||
161 | |||
162 | if(number_elems >= table_size) { | ||
163 | /* filled current table, double if we can */ | ||
164 | table_size = table_size << 1; | ||
165 | |||
166 | /* check that the new table size is still valid, i.e. that we didn't | ||
167 | just shift the 1 bit of the end of the int. */ | ||
168 | if (!table_size) { | ||
169 | full = true; | ||
170 | /* no point in doing antying else. */ | ||
171 | return; | ||
172 | } | ||
173 | master_table.resize(table_size); | ||
174 | } | ||
175 | } | ||
176 | |||
177 | void DBStore::removeItem(DataElem *r) | ||
178 | { | ||
179 | int position = master_table.findRef(r); | ||
180 | if(position != -1) { | ||
181 | /* there is at least one item, this is it */ | ||
182 | /* replace this with the last element, decrease the element count */ | ||
183 | master_table.insert(position, master_table.at(--number_elems)); | ||
184 | master_table.remove(number_elems); | ||
185 | delete r; | ||
186 | } | ||
187 | } | ||
188 | |||
189 | /*! | ||
190 | Sets the name of the database | ||
191 | |||
192 | \param n A string representing the new name of the database. | ||
193 | */ | ||
194 | void DBStore::setName(const QString &n) | ||
195 | { | ||
196 | name = n; | ||
197 | } | ||
198 | |||
199 | /*! | ||
200 | Gets the name of the database | ||
201 | |||
202 | \return A string representing the name of the database. | ||
203 | */ | ||
204 | QString DBStore::getName() | ||
205 | { | ||
206 | return name; | ||
207 | } | ||
208 | |||
209 | /*! | ||
210 | Retrieves a pointer to the key representation of the database for | ||
211 | other classes to use as reference. | ||
212 | |||
213 | \return a pointer to the databases key representaion | ||
214 | */ | ||
215 | KeyList *DBStore::getKeys() | ||
216 | { | ||
217 | return kRep; | ||
218 | } | ||
219 | |||
220 | /*! | ||
221 | sets the database's key representation the passed pointer | ||
222 | \param a pointer to a key representaton | ||
223 | */ | ||
224 | void DBStore::setKeys(KeyList *k) | ||
225 | { | ||
226 | kRep = k; | ||
227 | } | ||
228 | |||
229 | /*! | ||
230 | Sets the current element to the first element of the database | ||
231 | */ | ||
232 | void DBStore::first() | ||
233 | { | ||
234 | current_elem = 0; | ||
235 | } | ||
236 | |||
237 | /*! | ||
238 | Sets the current element to the last element of the database | ||
239 | */ | ||
240 | void DBStore::last() | ||
241 | { | ||
242 | current_elem = number_elems - 1; | ||
243 | } | ||
244 | |||
245 | /*! | ||
246 | Sets the current element to the next element of the database if | ||
247 | there exists an element after the current one. | ||
248 | */ | ||
249 | bool DBStore::next() | ||
250 | { | ||
251 | unsigned int new_current_elem = current_elem + 1; | ||
252 | if (current_elem < number_elems) | ||
253 | /* was valid before inc (it is possible but unlikely that inc current | ||
254 | elem will change it from invalid to valid) */ | ||
255 | if (new_current_elem < number_elems) { | ||
256 | /* is valid after inc */ | ||
257 | current_elem = new_current_elem; | ||
258 | return true; | ||
259 | } | ||
260 | return false; | ||
261 | } | ||
262 | |||
263 | /*! | ||
264 | Sets the current element to the previous element of the database if | ||
265 | there exists an element before the current one. | ||
266 | */ | ||
267 | bool DBStore::previous() | ||
268 | { | ||
269 | unsigned int new_current_elem = current_elem -1; | ||
270 | if (current_elem < number_elems) | ||
271 | /* was valid */ | ||
272 | if (new_current_elem < number_elems) { | ||
273 | /* still is (if was 0, then now -1, but as is unsigned will wrap | ||
274 | and hence be invalid */ | ||
275 | current_elem = new_current_elem; | ||
276 | return true; | ||
277 | } | ||
278 | return false; | ||
279 | } | ||
280 | |||
281 | /*! | ||
282 | Returns the current data element in the database. Which element is current | ||
283 | is affected by newly added items, findItem, next, previous, first and | ||
284 | last functions | ||
285 | |||
286 | \return a pointer to the current data element | ||
287 | */ | ||
288 | DataElem *DBStore::getCurrentData() | ||
289 | { | ||
290 | if (current_elem >= number_elems) | ||
291 | return NULL; | ||
292 | return master_table[current_elem]; | ||
293 | } | ||
diff --git a/noncore/apps/tableviewer/db/datacache.h b/noncore/apps/tableviewer/db/datacache.h new file mode 100644 index 0000000..c5dc637 --- a/dev/null +++ b/noncore/apps/tableviewer/db/datacache.h | |||
@@ -0,0 +1,130 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qtopia Environment. | ||
5 | ** | ||
6 | ** This file may be distributed and/or modified under the terms of the | ||
7 | ** GNU General Public License version 2 as published by the Free Software | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | |||
21 | /* | ||
22 | * This file is used to load the xml files that represent the database. | ||
23 | * The main requirment for said file is each data entry must contain a key, | ||
24 | * otherwise any other data headings are allowed. | ||
25 | */ | ||
26 | |||
27 | #ifndef __DATACACHE_H__ | ||
28 | #define __DATACACHE_H__ | ||
29 | |||
30 | #include <qstring.h> | ||
31 | #include <qvector.h> | ||
32 | #include "common.h" | ||
33 | |||
34 | class DBStore; | ||
35 | |||
36 | /*! Abstract class that defines how database stuff can be accessed */ | ||
37 | class DBAccess { | ||
38 | public: | ||
39 | |||
40 | // DBAccess(DBStore *d) { dstore = d; } | ||
41 | virtual ~DBAccess() {} | ||
42 | |||
43 | virtual QString type() { | ||
44 | return QString(); | ||
45 | } | ||
46 | |||
47 | virtual bool openSource(QIODevice *) { | ||
48 | qWarning("DBAccess::openSource not yet implemented"); | ||
49 | return false; | ||
50 | } | ||
51 | |||
52 | virtual bool saveSource(QIODevice *) { | ||
53 | qWarning("DBAccess::saveSource(QString) not yet implemented"); | ||
54 | return false; | ||
55 | } | ||
56 | |||
57 | protected: | ||
58 | DBStore *dstore; | ||
59 | QString source_name; | ||
60 | }; | ||
61 | |||
62 | class DBStore { | ||
63 | public: | ||
64 | DBStore(); | ||
65 | ~DBStore(); | ||
66 | |||
67 | bool openSource(QIODevice *, const QString &source); | ||
68 | bool saveSource(QIODevice *, const QString &source); | ||
69 | |||
70 | // Add an item | ||
71 | void addItem(DataElem *); | ||
72 | void addItemInternal(DataElem *); | ||
73 | void removeItem(DataElem *); | ||
74 | |||
75 | // Set the name of the database | ||
76 | void setName(const QString &name); | ||
77 | |||
78 | // Get the name of the database | ||
79 | QString getName(); | ||
80 | |||
81 | KeyList *getKeys(); | ||
82 | void setKeys(KeyList *); | ||
83 | |||
84 | /*! gets the number of fields defined in the database */ | ||
85 | inline int getNumFields() { | ||
86 | return kRep->getNumFields(); | ||
87 | } | ||
88 | |||
89 | /*! gets the index of a key given its name */ | ||
90 | inline int getKeyIndex(QString qs) { | ||
91 | return kRep->getKeyIndex(qs); | ||
92 | } | ||
93 | |||
94 | /*! gets the type of a key given its index */ | ||
95 | inline TVVariant::KeyType getKeyType(int i) { | ||
96 | return kRep->getKeyType(i); | ||
97 | } | ||
98 | |||
99 | /*! gets the name of a key given its index */ | ||
100 | inline QString getKeyName(int i) { | ||
101 | return kRep->getKeyName(i); | ||
102 | } | ||
103 | |||
104 | // Access functions.. iterator type stuff | ||
105 | |||
106 | void first(); | ||
107 | void last(); | ||
108 | |||
109 | bool next(); | ||
110 | bool previous(); | ||
111 | |||
112 | DataElem* getCurrentData(); | ||
113 | |||
114 | private: | ||
115 | /* does the work of freeing used memory */ | ||
116 | void freeTable(); | ||
117 | QString name; | ||
118 | |||
119 | QVector<DataElem> master_table; | ||
120 | DBAccess *archive; | ||
121 | |||
122 | KeyList *kRep; | ||
123 | |||
124 | unsigned int number_elems; | ||
125 | unsigned int table_size; /* should always be a power of 2 */ | ||
126 | bool full; /* since because we are using an int for indexing there is | ||
127 | an upper limit on the number of items we can store. */ | ||
128 | unsigned int current_elem; | ||
129 | }; | ||
130 | #endif | ||
diff --git a/noncore/apps/tableviewer/db/xmlsource.cpp b/noncore/apps/tableviewer/db/xmlsource.cpp new file mode 100644 index 0000000..7418a85 --- a/dev/null +++ b/noncore/apps/tableviewer/db/xmlsource.cpp | |||
@@ -0,0 +1,295 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qtopia Environment. | ||
5 | ** | ||
6 | ** This file may be distributed and/or modified under the terms of the | ||
7 | ** GNU General Public License version 2 as published by the Free Software | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | #include "xmlsource.h" | ||
21 | #include <qdict.h> | ||
22 | #include <stdlib.h> | ||
23 | #include <qtextstream.h> | ||
24 | |||
25 | |||
26 | DBXml::DBXml(DBStore *d) | ||
27 | { | ||
28 | dstore = d; | ||
29 | } | ||
30 | |||
31 | QString DBXml::type() | ||
32 | { | ||
33 | return "xml"; | ||
34 | } | ||
35 | |||
36 | bool DBXml::openSource(QIODevice *inDev) | ||
37 | { | ||
38 | bool ok; | ||
39 | |||
40 | DBXmlHandler h(dstore); | ||
41 | |||
42 | QTextStream tsIn(inDev); | ||
43 | QXmlInputSource source(tsIn); | ||
44 | QXmlSimpleReader reader; | ||
45 | reader.setContentHandler(&h); | ||
46 | reader.setErrorHandler(&h); | ||
47 | ok = reader.parse(source); | ||
48 | |||
49 | return ok; | ||
50 | } | ||
51 | |||
52 | bool DBXml::saveSource(QIODevice *outDev) | ||
53 | { | ||
54 | int i; | ||
55 | DataElem *elem; | ||
56 | KeyList *k; | ||
57 | |||
58 | QTextStream outstream(outDev); | ||
59 | |||
60 | outstream << "<database name=\"" << dstore->getName() << "\">" << endl; | ||
61 | outstream << "<header>" << endl; | ||
62 | |||
63 | k = dstore->getKeys(); | ||
64 | KeyListIterator it(*k); | ||
65 | while(it.current()) { | ||
66 | if (!it.current()->delFlag()) { | ||
67 | outstream << "<key name=\"KEYID" << it.currentKey() << "\" "; | ||
68 | outstream << "type=\"" | ||
69 | << TVVariant::typeToName(it.current()->type()) | ||
70 | << "\">"; | ||
71 | outstream << it.current()->name() << "</key>" << endl; | ||
72 | } | ||
73 | ++it; | ||
74 | } | ||
75 | |||
76 | outstream << "</header>" << endl; | ||
77 | |||
78 | dstore->first(); | ||
79 | |||
80 | do { | ||
81 | elem = dstore->getCurrentData(); | ||
82 | if (!elem) | ||
83 | break; | ||
84 | outstream << "<record>" << endl; | ||
85 | it.toFirst(); | ||
86 | while (it.current()) { | ||
87 | i = it.currentKey(); | ||
88 | if (elem->hasValidValue(i)) { | ||
89 | outstream << "<KEYID" << i << ">"; | ||
90 | if (dstore->getKeyType(i) == TVVariant::Date) { | ||
91 | // dates in files are different from displayed dates | ||
92 | QDate date = elem->getField(i).toDate(); | ||
93 | outstream << date.day() << "/" | ||
94 | << date.month() << "/" | ||
95 | << date.year(); | ||
96 | } else { | ||
97 | outstream << elem->toQString(i); | ||
98 | } | ||
99 | outstream << "</KEYID" << i << ">" << endl; | ||
100 | } | ||
101 | ++it; | ||
102 | } | ||
103 | outstream << "</record>" << endl; | ||
104 | } while(dstore->next()); | ||
105 | |||
106 | outstream << "</database>" << endl; | ||
107 | return TRUE; | ||
108 | } | ||
109 | |||
110 | DBXml::~DBXml() {} | ||
111 | |||
112 | /*! | ||
113 | \class DBXmlHandler | ||
114 | \brief An Xml parser for flat tables. | ||
115 | |||
116 | An xml parser for parsing the files used by the table viewer application. | ||
117 | |||
118 | The format of the xml files can be found at the front of the file | ||
119 | dataparser.h | ||
120 | */ | ||
121 | |||
122 | /*! | ||
123 | Constructs a new DBXmlHandler, and sets that the table should be | ||
124 | constructed in the DBStore pointed to by ds. | ||
125 | */ | ||
126 | DBXmlHandler::DBXmlHandler(DBStore *ds) | ||
127 | { | ||
128 | data_store = ds; | ||
129 | current_keyrep = 0; | ||
130 | } | ||
131 | |||
132 | /*! | ||
133 | Destroys the DBXmlHandler | ||
134 | */ | ||
135 | DBXmlHandler::~DBXmlHandler() | ||
136 | { | ||
137 | } | ||
138 | |||
139 | QString DBXmlHandler::errorProtocol() | ||
140 | { | ||
141 | qWarning("Error reading file"); | ||
142 | return errorProt; | ||
143 | } | ||
144 | |||
145 | bool DBXmlHandler::startDocument() | ||
146 | { | ||
147 | errorProt = ""; | ||
148 | state = StateInit; | ||
149 | return TRUE; | ||
150 | } | ||
151 | |||
152 | bool DBXmlHandler::startElement(const QString&, const QString&, | ||
153 | const QString& qName, const QXmlAttributes& atts) | ||
154 | { | ||
155 | if (state == StateInit && qName == "database") { | ||
156 | // First thing it expects is a <document name="..."> tag | ||
157 | state = StateDocument; | ||
158 | data_store->setName(atts.value("name")); | ||
159 | return TRUE; | ||
160 | } | ||
161 | if (state == StateDocument && qName == "header") { | ||
162 | state = StateHeader; | ||
163 | if (current_keyrep) delete current_keyrep; | ||
164 | current_keyrep = new KeyList(); | ||
165 | return TRUE; | ||
166 | } | ||
167 | if (state == StateHeader && qName == "key") { | ||
168 | /* Ok, adding a new key to our KeyList TODO */ | ||
169 | state = StateKey; | ||
170 | last_key_type = TVVariant::String; | ||
171 | key = atts.value("name"); | ||
172 | if (key.isEmpty()) { | ||
173 | qWarning("empty key name"); | ||
174 | return FALSE; | ||
175 | } | ||
176 | if(!atts.value("type").isEmpty()) | ||
177 | last_key_type = TVVariant::nameToType(atts.value("type")); | ||
178 | return TRUE; | ||
179 | } | ||
180 | if (state == StateDocument && qName == "record") { | ||
181 | state = StateRecord; | ||
182 | current_data = new DataElem(data_store); | ||
183 | // Now expecting a <record> tag | ||
184 | return TRUE; | ||
185 | } | ||
186 | if (state == StateRecord) { | ||
187 | state = StateField; | ||
188 | /* the qName is the name of a key */ | ||
189 | if (!keyIndexList[qName]) { | ||
190 | /* invalid key, we failed */ | ||
191 | qWarning("Invalid key in record"); | ||
192 | return FALSE; | ||
193 | } | ||
194 | keyIndex = *keyIndexList[qName]; | ||
195 | return TRUE; | ||
196 | } | ||
197 | qWarning("Unable to determine tag type"); | ||
198 | return FALSE; | ||
199 | } | ||
200 | |||
201 | bool DBXmlHandler::endElement(const QString&, const QString&, | ||
202 | const QString& qName) | ||
203 | { | ||
204 | switch(state) { | ||
205 | case StateField: | ||
206 | // TODO checks 'could' be done of the popped value | ||
207 | state = StateRecord; | ||
208 | break; | ||
209 | case StateKey: | ||
210 | // TODO checks 'could' be done of the popped value | ||
211 | state = StateHeader; | ||
212 | break; | ||
213 | case StateHeader: | ||
214 | data_store->setKeys(current_keyrep); | ||
215 | state = StateDocument; | ||
216 | break; | ||
217 | case StateRecord: | ||
218 | data_store->addItem(current_data); | ||
219 | state = StateDocument; | ||
220 | break; | ||
221 | case StateDocument: | ||
222 | // we are done... | ||
223 | break; | ||
224 | default: | ||
225 | // should only get a 'endElement' from one of the above states. | ||
226 | qWarning("Invalid end tag"); | ||
227 | return FALSE; | ||
228 | break; | ||
229 | } | ||
230 | return TRUE; | ||
231 | } | ||
232 | |||
233 | bool DBXmlHandler::characters(const QString& ch) | ||
234 | { | ||
235 | // this is where the 'between tag' stuff happens. | ||
236 | // e.g. the stuff between tags. | ||
237 | QString ch_simplified = ch.simplifyWhiteSpace(); | ||
238 | |||
239 | if (ch_simplified.isEmpty()) | ||
240 | return TRUE; | ||
241 | |||
242 | if (state == StateKey) { | ||
243 | int *tmp_val = new int; | ||
244 | /* We just grabbed the display name of a key */ | ||
245 | *tmp_val = current_keyrep->addKey(ch_simplified, last_key_type); | ||
246 | keyIndexList.insert(key, tmp_val); | ||
247 | return TRUE; | ||
248 | } | ||
249 | if (state == StateField) { | ||
250 | /* Ok, need to add data here */ | ||
251 | current_data->setField(keyIndex, ch_simplified); | ||
252 | return TRUE; | ||
253 | } | ||
254 | |||
255 | qWarning("Junk characters found... ignored"); | ||
256 | return TRUE; | ||
257 | } | ||
258 | |||
259 | QString DBXmlHandler::errorString() | ||
260 | { | ||
261 | return "the document is not in the expected file format"; | ||
262 | } | ||
263 | |||
264 | bool DBXmlHandler::warning(const QXmlParseException& exception) | ||
265 | { | ||
266 | errorProt += QString("warning parsing error: %1 in line %2, column %3\n" ) | ||
267 | .arg(exception.message()) | ||
268 | .arg(exception.lineNumber()) | ||
269 | .arg(exception.columnNumber()); | ||
270 | |||
271 | qWarning(errorProt); | ||
272 | return QXmlDefaultHandler::fatalError(exception); | ||
273 | } | ||
274 | |||
275 | bool DBXmlHandler::error(const QXmlParseException& exception) | ||
276 | { | ||
277 | errorProt += QString("error parsing error: %1 in line %2, column %3\n" ) | ||
278 | .arg(exception.message()) | ||
279 | .arg(exception.lineNumber()) | ||
280 | .arg(exception.columnNumber()); | ||
281 | |||
282 | qWarning(errorProt); | ||
283 | return QXmlDefaultHandler::fatalError(exception); | ||
284 | } | ||
285 | |||
286 | bool DBXmlHandler::fatalError(const QXmlParseException& exception) | ||
287 | { | ||
288 | errorProt += QString("fatal parsing error: %1 in line %2, column %3\n" ) | ||
289 | .arg(exception.message()) | ||
290 | .arg(exception.lineNumber()) | ||
291 | .arg(exception.columnNumber()); | ||
292 | |||
293 | qWarning(errorProt); | ||
294 | return QXmlDefaultHandler::fatalError(exception); | ||
295 | } | ||
diff --git a/noncore/apps/tableviewer/db/xmlsource.h b/noncore/apps/tableviewer/db/xmlsource.h new file mode 100644 index 0000000..ec267a6 --- a/dev/null +++ b/noncore/apps/tableviewer/db/xmlsource.h | |||
@@ -0,0 +1,119 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qtopia Environment. | ||
5 | ** | ||
6 | ** This file may be distributed and/or modified under the terms of the | ||
7 | ** GNU General Public License version 2 as published by the Free Software | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | |||
21 | /* A Class to parse an xml docment of the form | ||
22 | * <database name="..."> | ||
23 | * <header> | ||
24 | * <key name=key_name type=String>Displayed Name</key> | ||
25 | * <key name=key2name>key2name</key> | ||
26 | * <key name=key3name type=Date>Key 3</key> | ||
27 | * <key name=key4name type=Int>key 4</key> | ||
28 | * </header> | ||
29 | * <record> | ||
30 | * <key_name>string</key_name> | ||
31 | * <key4name>int</key4name> | ||
32 | * <key2name>string</key2name> | ||
33 | * </record> | ||
34 | * <record> | ||
35 | * .... | ||
36 | * </record> | ||
37 | * .... | ||
38 | * </database> | ||
39 | * | ||
40 | * There is some room for improvment mostly around using better checking | ||
41 | * and the use of more advanced xml features. | ||
42 | */ | ||
43 | |||
44 | #ifndef __XMLSOURCE_H__ | ||
45 | #define __XMLSOURCE_H__ | ||
46 | |||
47 | #include <qxml.h> | ||
48 | #include <qstring.h> | ||
49 | #include <qstringlist.h> | ||
50 | #include <qstack.h> | ||
51 | #include <qdict.h> | ||
52 | #include "datacache.h" | ||
53 | #include "common.h" | ||
54 | |||
55 | |||
56 | class DBXml : public DBAccess | ||
57 | { | ||
58 | public: | ||
59 | /* create connection and either open or initialize */ | ||
60 | DBXml(DBStore *d); | ||
61 | QString type(); | ||
62 | bool openSource(QIODevice *); | ||
63 | bool saveSource(QIODevice *); | ||
64 | /* does a db write */ | ||
65 | ~DBXml(); | ||
66 | }; | ||
67 | |||
68 | |||
69 | class DBXmlHandler : public QXmlDefaultHandler | ||
70 | { | ||
71 | |||
72 | public: | ||
73 | DBXmlHandler(DBStore *ds); | ||
74 | virtual ~DBXmlHandler(); | ||
75 | |||
76 | // return the error protocol if parsing failed | ||
77 | QString errorProtocol(); | ||
78 | |||
79 | // overloaded handler functions | ||
80 | bool startDocument(); | ||
81 | bool startElement(const QString& namespaceURI, const QString& localName, | ||
82 | const QString& qName, const QXmlAttributes& atts); | ||
83 | bool endElement(const QString& namespaceURI, const QString& localName, | ||
84 | const QString& qName); | ||
85 | bool characters(const QString& ch); | ||
86 | |||
87 | QString errorString(); | ||
88 | |||
89 | bool warning(const QXmlParseException& exception); | ||
90 | bool error(const QXmlParseException& exception); | ||
91 | bool fatalError(const QXmlParseException& exception); | ||
92 | |||
93 | private: | ||
94 | |||
95 | QStack<QString> stack; | ||
96 | KeyList *current_keyrep; | ||
97 | DataElem *current_data; | ||
98 | TVVariant::KeyType last_key_type; | ||
99 | |||
100 | QString errorProt; | ||
101 | DBStore *data_store; | ||
102 | |||
103 | enum State { | ||
104 | StateInit, | ||
105 | StateHeader, | ||
106 | StateKey, | ||
107 | StateDocument, | ||
108 | StateRecord, | ||
109 | StateField | ||
110 | }; | ||
111 | |||
112 | State state; | ||
113 | |||
114 | QDict<int> keyIndexList; | ||
115 | int keyIndex; | ||
116 | QString key; | ||
117 | }; | ||
118 | |||
119 | #endif | ||