-rw-r--r-- | libopie/pim/ocontactaccessbackend_xml.h | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/libopie/pim/ocontactaccessbackend_xml.h b/libopie/pim/ocontactaccessbackend_xml.h index 8b95102..adc8dc3 100644 --- a/libopie/pim/ocontactaccessbackend_xml.h +++ b/libopie/pim/ocontactaccessbackend_xml.h | |||
@@ -1,115 +1,118 @@ | |||
1 | /* | 1 | /* |
2 | * XML Backend for the OPIE-Contact Database. | 2 | * XML Backend for the OPIE-Contact Database. |
3 | * | 3 | * |
4 | * Copyright (c) 2002 by Stefan Eilers (Eilers.Stefan@epost.de) | 4 | * Copyright (c) 2002 by Stefan Eilers (Eilers.Stefan@epost.de) |
5 | * | 5 | * |
6 | * ===================================================================== | 6 | * ===================================================================== |
7 | *This program is free software; you can redistribute it and/or | 7 | *This program is free software; you can redistribute it and/or |
8 | *modify it under the terms of the GNU Library General Public | 8 | *modify it under the terms of the GNU Library General Public |
9 | * License as published by the Free Software Foundation; either | 9 | * License as published by the Free Software Foundation; either |
10 | * version 2 of the License, or (at your option) any later version. | 10 | * version 2 of the License, or (at your option) any later version. |
11 | * ===================================================================== | 11 | * ===================================================================== |
12 | * ToDo: XML-Backend: Automatic reload if something was changed... | 12 | * ToDo: XML-Backend: Automatic reload if something was changed... |
13 | * | 13 | * |
14 | * | 14 | * |
15 | * ===================================================================== | 15 | * ===================================================================== |
16 | * Version: $Id$ | 16 | * Version: $Id$ |
17 | * ===================================================================== | 17 | * ===================================================================== |
18 | * History: | 18 | * History: |
19 | * $Log$ | 19 | * $Log$ |
20 | * Revision 1.7 2002/11/13 15:02:46 eilers | ||
21 | * Small Bug in sorted fixed | ||
22 | * | ||
20 | * Revision 1.6 2002/11/13 14:14:51 eilers | 23 | * Revision 1.6 2002/11/13 14:14:51 eilers |
21 | * Added sorted for Contacts.. | 24 | * Added sorted for Contacts.. |
22 | * | 25 | * |
23 | * Revision 1.5 2002/11/01 15:10:42 eilers | 26 | * Revision 1.5 2002/11/01 15:10:42 eilers |
24 | * Added regExp-search in database for all fields in a contact. | 27 | * Added regExp-search in database for all fields in a contact. |
25 | * | 28 | * |
26 | * Revision 1.4 2002/10/16 10:52:40 eilers | 29 | * Revision 1.4 2002/10/16 10:52:40 eilers |
27 | * Added some docu to the interface and now using the cache infrastucture by zecke.. :) | 30 | * Added some docu to the interface and now using the cache infrastucture by zecke.. :) |
28 | * | 31 | * |
29 | * Revision 1.3 2002/10/14 16:21:54 eilers | 32 | * Revision 1.3 2002/10/14 16:21:54 eilers |
30 | * Some minor interface updates | 33 | * Some minor interface updates |
31 | * | 34 | * |
32 | * Revision 1.2 2002/10/07 17:34:24 eilers | 35 | * Revision 1.2 2002/10/07 17:34:24 eilers |
33 | * added OBackendFactory for advanced backend access | 36 | * added OBackendFactory for advanced backend access |
34 | * | 37 | * |
35 | * Revision 1.1 2002/09/27 17:11:44 eilers | 38 | * Revision 1.1 2002/09/27 17:11:44 eilers |
36 | * Added API for accessing the Contact-Database ! It is compiling, but | 39 | * Added API for accessing the Contact-Database ! It is compiling, but |
37 | * please do not expect that anything is working ! | 40 | * please do not expect that anything is working ! |
38 | * I will debug that stuff in the next time .. | 41 | * I will debug that stuff in the next time .. |
39 | * Please read README_COMPILE for compiling ! | 42 | * Please read README_COMPILE for compiling ! |
40 | * | 43 | * |
41 | * | 44 | * |
42 | */ | 45 | */ |
43 | 46 | ||
44 | #ifndef _OContactAccessBackend_XML_ | 47 | #ifndef _OContactAccessBackend_XML_ |
45 | #define _OContactAccessBackend_XML_ | 48 | #define _OContactAccessBackend_XML_ |
46 | 49 | ||
47 | #include <qasciidict.h> | 50 | #include <qasciidict.h> |
48 | #include <qdatetime.h> | 51 | #include <qdatetime.h> |
49 | #include <qfile.h> | 52 | #include <qfile.h> |
50 | #include <qfileinfo.h> | 53 | #include <qfileinfo.h> |
51 | #include <qregexp.h> | 54 | #include <qregexp.h> |
52 | #include <qarray.h> | 55 | #include <qarray.h> |
53 | #include <qmap.h> | 56 | #include <qmap.h> |
54 | 57 | ||
55 | #include <qpe/global.h> | 58 | #include <qpe/global.h> |
56 | 59 | ||
57 | #include <opie/xmltree.h> | 60 | #include <opie/xmltree.h> |
58 | #include "ocontactaccessbackend.h" | 61 | #include "ocontactaccessbackend.h" |
59 | #include "ocontactaccess.h" | 62 | #include "ocontactaccess.h" |
60 | 63 | ||
61 | #include <stdlib.h> | 64 | #include <stdlib.h> |
62 | #include <errno.h> | 65 | #include <errno.h> |
63 | 66 | ||
64 | using namespace Opie; | 67 | using namespace Opie; |
65 | 68 | ||
66 | /* the default xml implementation */ | 69 | /* the default xml implementation */ |
67 | class OContactAccessBackend_XML : public OContactAccessBackend { | 70 | class OContactAccessBackend_XML : public OContactAccessBackend { |
68 | public: | 71 | public: |
69 | OContactAccessBackend_XML ( QString appname, QString filename = 0l ): | 72 | OContactAccessBackend_XML ( QString appname, QString filename = 0l ): |
70 | m_changed( false ) | 73 | m_changed( false ) |
71 | { | 74 | { |
72 | m_appName = appname; | 75 | m_appName = appname; |
73 | 76 | ||
74 | /* Set journalfile name ... */ | 77 | /* Set journalfile name ... */ |
75 | m_journalName = getenv("HOME"); | 78 | m_journalName = getenv("HOME"); |
76 | m_journalName +="/.abjournal" + appname; | 79 | m_journalName +="/.abjournal" + appname; |
77 | 80 | ||
78 | /* Expecting to access the default filename if nothing else is set */ | 81 | /* Expecting to access the default filename if nothing else is set */ |
79 | if ( filename.isEmpty() ){ | 82 | if ( filename.isEmpty() ){ |
80 | m_fileName = Global::applicationFileName( "addressbook","addressbook.xml" ); | 83 | m_fileName = Global::applicationFileName( "addressbook","addressbook.xml" ); |
81 | } else | 84 | } else |
82 | m_fileName = filename; | 85 | m_fileName = filename; |
83 | 86 | ||
84 | /* Load Database now */ | 87 | /* Load Database now */ |
85 | load (); | 88 | load (); |
86 | } | 89 | } |
87 | 90 | ||
88 | bool save() { | 91 | bool save() { |
89 | 92 | ||
90 | if ( !m_changed ) | 93 | if ( !m_changed ) |
91 | return true; | 94 | return true; |
92 | 95 | ||
93 | QString strNewFile = m_fileName + ".new"; | 96 | QString strNewFile = m_fileName + ".new"; |
94 | QFile f( strNewFile ); | 97 | QFile f( strNewFile ); |
95 | if ( !f.open( IO_WriteOnly|IO_Raw ) ) | 98 | if ( !f.open( IO_WriteOnly|IO_Raw ) ) |
96 | return false; | 99 | return false; |
97 | 100 | ||
98 | int total_written; | 101 | int total_written; |
99 | QString out; | 102 | QString out; |
100 | out = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE Addressbook ><AddressBook>\n" | 103 | out = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE Addressbook ><AddressBook>\n" |
101 | " <Groups>\n" | 104 | " <Groups>\n" |
102 | " </Groups>\n" | 105 | " </Groups>\n" |
103 | " <Contacts>\n"; | 106 | " <Contacts>\n"; |
104 | //QValueList<Contact>::iterator it; | 107 | //QValueList<Contact>::iterator it; |
105 | QValueListConstIterator<OContact> it; | 108 | QValueListConstIterator<OContact> it; |
106 | for ( it = m_contactList.begin(); it != m_contactList.end(); ++it ) { | 109 | for ( it = m_contactList.begin(); it != m_contactList.end(); ++it ) { |
107 | out += "<Contact "; | 110 | out += "<Contact "; |
108 | (*it).save( out ); | 111 | (*it).save( out ); |
109 | out += "/>\n"; | 112 | out += "/>\n"; |
110 | QCString cstr = out.utf8(); | 113 | QCString cstr = out.utf8(); |
111 | total_written = f.writeBlock( cstr.data(), cstr.length() ); | 114 | total_written = f.writeBlock( cstr.data(), cstr.length() ); |
112 | if ( total_written != int(cstr.length()) ) { | 115 | if ( total_written != int(cstr.length()) ) { |
113 | f.close(); | 116 | f.close(); |
114 | QFile::remove( strNewFile ); | 117 | QFile::remove( strNewFile ); |
115 | return false; | 118 | return false; |
@@ -219,198 +222,198 @@ class OContactAccessBackend_XML : public OContactAccessBackend { | |||
219 | bool allcorrect = true; | 222 | bool allcorrect = true; |
220 | for ( int i = 0; i < Qtopia::rid; i++ ) { | 223 | for ( int i = 0; i < Qtopia::rid; i++ ) { |
221 | /* Just compare fields which are not empty in the query object */ | 224 | /* Just compare fields which are not empty in the query object */ |
222 | if ( !query.field(i).isEmpty() ){ | 225 | if ( !query.field(i).isEmpty() ){ |
223 | switch ( settings & ~OContactAccess::IgnoreCase ){ | 226 | switch ( settings & ~OContactAccess::IgnoreCase ){ |
224 | case OContactAccess::RegExp:{ | 227 | case OContactAccess::RegExp:{ |
225 | QRegExp expr ( query.field(i), | 228 | QRegExp expr ( query.field(i), |
226 | !(settings & OContactAccess::IgnoreCase), | 229 | !(settings & OContactAccess::IgnoreCase), |
227 | false ); | 230 | false ); |
228 | if ( expr.find ( (*it).field(i), 0 ) == -1 ) | 231 | if ( expr.find ( (*it).field(i), 0 ) == -1 ) |
229 | allcorrect = false; | 232 | allcorrect = false; |
230 | } | 233 | } |
231 | break; | 234 | break; |
232 | case OContactAccess::WildCards:{ | 235 | case OContactAccess::WildCards:{ |
233 | QRegExp expr ( query.field(i), | 236 | QRegExp expr ( query.field(i), |
234 | !(settings & OContactAccess::IgnoreCase), | 237 | !(settings & OContactAccess::IgnoreCase), |
235 | true ); | 238 | true ); |
236 | if ( expr.find ( (*it).field(i), 0 ) == -1 ) | 239 | if ( expr.find ( (*it).field(i), 0 ) == -1 ) |
237 | allcorrect = false; | 240 | allcorrect = false; |
238 | } | 241 | } |
239 | break; | 242 | break; |
240 | case OContactAccess::ExactMatch:{ | 243 | case OContactAccess::ExactMatch:{ |
241 | if (settings & OContactAccess::IgnoreCase){ | 244 | if (settings & OContactAccess::IgnoreCase){ |
242 | if ( query.field(i).upper() != | 245 | if ( query.field(i).upper() != |
243 | (*it).field(i).upper() ) | 246 | (*it).field(i).upper() ) |
244 | allcorrect = false; | 247 | allcorrect = false; |
245 | }else{ | 248 | }else{ |
246 | if ( query.field(i) != (*it).field(i) ) | 249 | if ( query.field(i) != (*it).field(i) ) |
247 | allcorrect = false; | 250 | allcorrect = false; |
248 | } | 251 | } |
249 | } | 252 | } |
250 | break; | 253 | break; |
251 | } | 254 | } |
252 | } | 255 | } |
253 | } | 256 | } |
254 | if ( allcorrect ){ | 257 | if ( allcorrect ){ |
255 | m_currentQuery[arraycounter++] = (*it).uid(); | 258 | m_currentQuery[arraycounter++] = (*it).uid(); |
256 | } | 259 | } |
257 | } | 260 | } |
258 | 261 | ||
259 | // Shrink to fit.. | 262 | // Shrink to fit.. |
260 | m_currentQuery.resize(arraycounter); | 263 | m_currentQuery.resize(arraycounter); |
261 | 264 | ||
262 | return m_currentQuery; | 265 | return m_currentQuery; |
263 | } | 266 | } |
264 | 267 | ||
265 | QArray<int> matchRegexp( const QRegExp &r ) const{ | 268 | QArray<int> matchRegexp( const QRegExp &r ) const{ |
266 | QArray<int> m_currentQuery( m_contactList.count() ); | 269 | QArray<int> m_currentQuery( m_contactList.count() ); |
267 | QValueListConstIterator<OContact> it; | 270 | QValueListConstIterator<OContact> it; |
268 | uint arraycounter = 0; | 271 | uint arraycounter = 0; |
269 | 272 | ||
270 | for( it = m_contactList.begin(); it != m_contactList.end(); ++it ){ | 273 | for( it = m_contactList.begin(); it != m_contactList.end(); ++it ){ |
271 | if ( (*it).match( r ) ){ | 274 | if ( (*it).match( r ) ){ |
272 | m_currentQuery[arraycounter++] = (*it).uid(); | 275 | m_currentQuery[arraycounter++] = (*it).uid(); |
273 | } | 276 | } |
274 | 277 | ||
275 | } | 278 | } |
276 | // Shrink to fit.. | 279 | // Shrink to fit.. |
277 | m_currentQuery.resize(arraycounter); | 280 | m_currentQuery.resize(arraycounter); |
278 | 281 | ||
279 | return m_currentQuery; | 282 | return m_currentQuery; |
280 | } | 283 | } |
281 | 284 | ||
282 | const uint querySettings() | 285 | const uint querySettings() |
283 | { | 286 | { |
284 | return ( OContactAccess::WildCards | 287 | return ( OContactAccess::WildCards |
285 | & OContactAccess::IgnoreCase | 288 | & OContactAccess::IgnoreCase |
286 | & OContactAccess::RegExp | 289 | & OContactAccess::RegExp |
287 | & OContactAccess::ExactMatch ); | 290 | & OContactAccess::ExactMatch ); |
288 | } | 291 | } |
289 | 292 | ||
290 | bool hasQuerySettings (uint querySettings) const | 293 | bool hasQuerySettings (uint querySettings) const |
291 | { | 294 | { |
292 | /* OContactAccess::IgnoreCase may be added with one | 295 | /* OContactAccess::IgnoreCase may be added with one |
293 | * of the other settings, but never used alone. | 296 | * of the other settings, but never used alone. |
294 | * The other settings are just valid alone... | 297 | * The other settings are just valid alone... |
295 | */ | 298 | */ |
296 | switch ( querySettings & ~OContactAccess::IgnoreCase ){ | 299 | switch ( querySettings & ~OContactAccess::IgnoreCase ){ |
297 | case OContactAccess::RegExp: | 300 | case OContactAccess::RegExp: |
298 | return ( true ); | 301 | return ( true ); |
299 | case OContactAccess::WildCards: | 302 | case OContactAccess::WildCards: |
300 | return ( true ); | 303 | return ( true ); |
301 | case OContactAccess::ExactMatch: | 304 | case OContactAccess::ExactMatch: |
302 | return ( true ); | 305 | return ( true ); |
303 | default: | 306 | default: |
304 | return ( false ); | 307 | return ( false ); |
305 | } | 308 | } |
306 | } | 309 | } |
307 | 310 | ||
308 | // Currently only asc implemented.. | 311 | // Currently only asc implemented.. |
309 | QArray<int> sorted( bool asc, int , int , int ) | 312 | QArray<int> sorted( bool asc, int , int , int ) |
310 | { | 313 | { |
311 | QMap<QString, int> nameToUid; | 314 | QMap<QString, int> nameToUid; |
312 | QStringList names; | 315 | QStringList names; |
313 | QArray<int> m_currentQuery( m_contactList.count() ); | 316 | QArray<int> m_currentQuery( m_contactList.count() ); |
314 | 317 | ||
315 | // First fill map and StringList with all Names ( better LastNames ? ) | 318 | // First fill map and StringList with all Names |
316 | // Afterwards sort namelist and use map to fill array to return.. | 319 | // Afterwards sort namelist and use map to fill array to return.. |
317 | QValueListConstIterator<OContact> it; | 320 | QValueListConstIterator<OContact> it; |
318 | for( it = m_contactList.begin(); it != m_contactList.end(); ++it ){ | 321 | for( it = m_contactList.begin(); it != m_contactList.end(); ++it ){ |
319 | names.append( (*it).lastName() ); | 322 | names.append( (*it).fileAs() ); |
320 | nameToUid.insert( (*it).lastName(), (*it).uid() ); | 323 | nameToUid.insert( (*it).fileAs(), (*it).uid() ); |
321 | } | 324 | } |
322 | names.sort(); | 325 | names.sort(); |
323 | 326 | ||
324 | int i = 0; | 327 | int i = 0; |
325 | if ( asc ){ | 328 | if ( asc ){ |
326 | for ( QStringList::Iterator it = names.begin(); it != names.end(); ++it ) | 329 | for ( QStringList::Iterator it = names.begin(); it != names.end(); ++it ) |
327 | m_currentQuery[i++] = nameToUid[ (*it) ]; | 330 | m_currentQuery[i++] = nameToUid[ (*it) ]; |
328 | }else{ | 331 | }else{ |
329 | for ( QStringList::Iterator it = names.end(); it != names.begin(); --it ) | 332 | for ( QStringList::Iterator it = names.end(); it != names.begin(); --it ) |
330 | m_currentQuery[i++] = nameToUid[ (*it) ]; | 333 | m_currentQuery[i++] = nameToUid[ (*it) ]; |
331 | } | 334 | } |
332 | 335 | ||
333 | return m_currentQuery; | 336 | return m_currentQuery; |
334 | 337 | ||
335 | } | 338 | } |
336 | bool add ( const OContact &newcontact ) | 339 | bool add ( const OContact &newcontact ) |
337 | { | 340 | { |
338 | //qWarning("odefaultbackend: ACTION::ADD"); | 341 | //qWarning("odefaultbackend: ACTION::ADD"); |
339 | updateJournal (newcontact, OContact::ACTION_ADD); | 342 | updateJournal (newcontact, OContact::ACTION_ADD); |
340 | addContact_p( newcontact ); | 343 | addContact_p( newcontact ); |
341 | 344 | ||
342 | m_changed = true; | 345 | m_changed = true; |
343 | 346 | ||
344 | return true; | 347 | return true; |
345 | } | 348 | } |
346 | 349 | ||
347 | bool replace ( const OContact &contact ) | 350 | bool replace ( const OContact &contact ) |
348 | { | 351 | { |
349 | m_changed = true; | 352 | m_changed = true; |
350 | 353 | ||
351 | bool found = false; | 354 | bool found = false; |
352 | 355 | ||
353 | QValueListIterator<OContact> it; | 356 | QValueListIterator<OContact> it; |
354 | for( it = m_contactList.begin(); it != m_contactList.end(); ++it ){ | 357 | for( it = m_contactList.begin(); it != m_contactList.end(); ++it ){ |
355 | if ( (*it).uid() == contact.uid() ){ | 358 | if ( (*it).uid() == contact.uid() ){ |
356 | found = true; | 359 | found = true; |
357 | break; | 360 | break; |
358 | } | 361 | } |
359 | } | 362 | } |
360 | if (found) { | 363 | if (found) { |
361 | updateJournal (contact, OContact::ACTION_REPLACE); | 364 | updateJournal (contact, OContact::ACTION_REPLACE); |
362 | m_contactList.remove (it); | 365 | m_contactList.remove (it); |
363 | m_contactList.append (contact); | 366 | m_contactList.append (contact); |
364 | return true; | 367 | return true; |
365 | } else | 368 | } else |
366 | return false; | 369 | return false; |
367 | } | 370 | } |
368 | 371 | ||
369 | bool remove ( int uid ) | 372 | bool remove ( int uid ) |
370 | { | 373 | { |
371 | m_changed = true; | 374 | m_changed = true; |
372 | 375 | ||
373 | bool found = false; | 376 | bool found = false; |
374 | QValueListIterator<OContact> it; | 377 | QValueListIterator<OContact> it; |
375 | for( it = m_contactList.begin(); it != m_contactList.end(); ++it ){ | 378 | for( it = m_contactList.begin(); it != m_contactList.end(); ++it ){ |
376 | if ((*it).uid() == uid){ | 379 | if ((*it).uid() == uid){ |
377 | found = true; | 380 | found = true; |
378 | break; | 381 | break; |
379 | } | 382 | } |
380 | } | 383 | } |
381 | if (found) { | 384 | if (found) { |
382 | updateJournal ( *it, OContact::ACTION_REMOVE); | 385 | updateJournal ( *it, OContact::ACTION_REMOVE); |
383 | m_contactList.remove (it); | 386 | m_contactList.remove (it); |
384 | return true; | 387 | return true; |
385 | } else | 388 | } else |
386 | return false; | 389 | return false; |
387 | } | 390 | } |
388 | 391 | ||
389 | bool reload(){ | 392 | bool reload(){ |
390 | /* Reload is the same as load in this implementation */ | 393 | /* Reload is the same as load in this implementation */ |
391 | return ( load() ); | 394 | return ( load() ); |
392 | } | 395 | } |
393 | 396 | ||
394 | private: | 397 | private: |
395 | void addContact_p( const OContact &newcontact ){ | 398 | void addContact_p( const OContact &newcontact ){ |
396 | m_contactList.append (newcontact); | 399 | m_contactList.append (newcontact); |
397 | } | 400 | } |
398 | 401 | ||
399 | /* This function loads the xml-database and the journalfile */ | 402 | /* This function loads the xml-database and the journalfile */ |
400 | bool load( const QString filename, bool isJournal ) { | 403 | bool load( const QString filename, bool isJournal ) { |
401 | 404 | ||
402 | /* We use the time of the last read to check if the file was | 405 | /* We use the time of the last read to check if the file was |
403 | * changed externally. | 406 | * changed externally. |
404 | */ | 407 | */ |
405 | if ( !isJournal ){ | 408 | if ( !isJournal ){ |
406 | QFileInfo fi( filename ); | 409 | QFileInfo fi( filename ); |
407 | m_readtime = fi.lastModified (); | 410 | m_readtime = fi.lastModified (); |
408 | } | 411 | } |
409 | 412 | ||
410 | const int JOURNALACTION = Qtopia::Notes + 1; | 413 | const int JOURNALACTION = Qtopia::Notes + 1; |
411 | const int JOURNALROW = JOURNALACTION + 1; | 414 | const int JOURNALROW = JOURNALACTION + 1; |
412 | 415 | ||
413 | bool foundAction = false; | 416 | bool foundAction = false; |
414 | OContact::journal_action action = OContact::ACTION_ADD; | 417 | OContact::journal_action action = OContact::ACTION_ADD; |
415 | int journalKey = 0; | 418 | int journalKey = 0; |
416 | QMap<int, QString> contactMap; | 419 | QMap<int, QString> contactMap; |