summaryrefslogtreecommitdiff
path: root/libopie/pim/ocontactaccessbackend_xml.cpp
Unidiff
Diffstat (limited to 'libopie/pim/ocontactaccessbackend_xml.cpp') (more/less context) (show whitespace changes)
-rw-r--r--libopie/pim/ocontactaccessbackend_xml.cpp160
1 files changed, 99 insertions, 61 deletions
diff --git a/libopie/pim/ocontactaccessbackend_xml.cpp b/libopie/pim/ocontactaccessbackend_xml.cpp
index 2df6757..4abf4d9 100644
--- a/libopie/pim/ocontactaccessbackend_xml.cpp
+++ b/libopie/pim/ocontactaccessbackend_xml.cpp
@@ -17,6 +17,20 @@
17 * ===================================================================== 17 * =====================================================================
18 * History: 18 * History:
19 * $Log$ 19 * $Log$
20 * Revision 1.2 2003/03/21 10:33:09 eilers
21 * Merged speed optimized xml backend for contacts to main.
22 * Added QDateTime to querybyexample. For instance, it is now possible to get
23 * all Birthdays/Anniversaries between two dates. This should be used
24 * to show all birthdays in the datebook..
25 * This change is sourcecode backward compatible but you have to upgrade
26 * the binaries for today-addressbook.
27 *
28 * Revision 1.1.2.2 2003/02/11 12:17:28 eilers
29 * Speed optimization. Removed the sequential search loops.
30 *
31 * Revision 1.1.2.1 2003/02/10 15:31:38 eilers
32 * Writing offsets to debug output..
33 *
20 * Revision 1.1 2003/02/09 15:05:01 eilers 34 * Revision 1.1 2003/02/09 15:05:01 eilers
21 * Nothing happened.. Just some cleanup before I will start.. 35 * Nothing happened.. Just some cleanup before I will start..
22 * 36 *
@@ -89,6 +103,11 @@ using namespace Opie;
89OContactAccessBackend_XML::OContactAccessBackend_XML ( QString appname, QString filename = 0l ): 103OContactAccessBackend_XML::OContactAccessBackend_XML ( QString appname, QString filename = 0l ):
90 m_changed( false ) 104 m_changed( false )
91{ 105{
106 // Just m_contactlist should call delete if an entry
107 // is removed.
108 m_contactList.setAutoDelete( true );
109 m_uidToContact.setAutoDelete( false );
110
92 m_appName = appname; 111 m_appName = appname;
93 112
94 /* Set journalfile name ... */ 113 /* Set journalfile name ... */
@@ -117,19 +136,29 @@ bool OContactAccessBackend_XML::save()
117 return false; 136 return false;
118 137
119 int total_written; 138 int total_written;
139 int idx_offset = 0;
120 QString out; 140 QString out;
141
142 // Write Header
121 out = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE Addressbook ><AddressBook>\n" 143 out = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE Addressbook ><AddressBook>\n"
122 " <Groups>\n" 144 " <Groups>\n"
123 " </Groups>\n" 145 " </Groups>\n"
124 " <Contacts>\n"; 146 " <Contacts>\n";
125 //QValueList<Contact>::iterator it; 147 QCString cstr = out.utf8();
126 QValueListConstIterator<OContact> it; 148 f.writeBlock( cstr.data(), cstr.length() );
127 for ( it = m_contactList.begin(); it != m_contactList.end(); ++it ) { 149 idx_offset += cstr.length();
150 out = "";
151
152 // Write all contacts
153 QListIterator<OContact> it( m_contactList );
154 for ( ; it.current(); ++it ) {
155 qWarning(" Uid %d at Offset: %x", (*it)->uid(), idx_offset );
128 out += "<Contact "; 156 out += "<Contact ";
129 (*it).save( out ); 157 (*it)->save( out );
130 out += "/>\n"; 158 out += "/>\n";
131 QCString cstr = out.utf8(); 159 cstr = out.utf8();
132 total_written = f.writeBlock( cstr.data(), cstr.length() ); 160 total_written = f.writeBlock( cstr.data(), cstr.length() );
161 idx_offset += cstr.length();
133 if ( total_written != int(cstr.length()) ) { 162 if ( total_written != int(cstr.length()) ) {
134 f.close(); 163 f.close();
135 QFile::remove( strNewFile ); 164 QFile::remove( strNewFile );
@@ -139,7 +168,8 @@ bool OContactAccessBackend_XML::save()
139 } 168 }
140 out += " </Contacts>\n</AddressBook>\n"; 169 out += " </Contacts>\n</AddressBook>\n";
141 170
142 QCString cstr = out.utf8(); 171 // Write Footer
172 cstr = out.utf8();
143 total_written = f.writeBlock( cstr.data(), cstr.length() ); 173 total_written = f.writeBlock( cstr.data(), cstr.length() );
144 if ( total_written != int( cstr.length() ) ) { 174 if ( total_written != int( cstr.length() ) ) {
145 f.close(); 175 f.close();
@@ -167,6 +197,7 @@ bool OContactAccessBackend_XML::save()
167bool OContactAccessBackend_XML::load () 197bool OContactAccessBackend_XML::load ()
168{ 198{
169 m_contactList.clear(); 199 m_contactList.clear();
200 m_uidToContact.clear();
170 201
171 /* Load XML-File and journal if it exists */ 202 /* Load XML-File and journal if it exists */
172 if ( !load ( m_fileName, false ) ) 203 if ( !load ( m_fileName, false ) )
@@ -185,8 +216,9 @@ bool OContactAccessBackend_XML::load ()
185void OContactAccessBackend_XML::clear () 216void OContactAccessBackend_XML::clear ()
186{ 217{
187 m_contactList.clear(); 218 m_contactList.clear();
188 m_changed = false; 219 m_uidToContact.clear();
189 220
221 m_changed = false;
190} 222}
191 223
192bool OContactAccessBackend_XML::wasChangedExternally() 224bool OContactAccessBackend_XML::wasChangedExternally()
@@ -203,9 +235,9 @@ QArray<int> OContactAccessBackend_XML::allRecords() const
203 QArray<int> uid_list( m_contactList.count() ); 235 QArray<int> uid_list( m_contactList.count() );
204 236
205 uint counter = 0; 237 uint counter = 0;
206 QValueListConstIterator<OContact> it; 238 QListIterator<OContact> it( m_contactList );
207 for( it = m_contactList.begin(); it != m_contactList.end(); ++it ){ 239 for( ; it.current(); ++it ){
208 uid_list[counter++] = (*it).uid(); 240 uid_list[counter++] = (*it)->uid();
209 } 241 }
210 242
211 return ( uid_list ); 243 return ( uid_list );
@@ -213,31 +245,26 @@ QArray<int> OContactAccessBackend_XML::allRecords() const
213 245
214OContact OContactAccessBackend_XML::find ( int uid ) const 246OContact OContactAccessBackend_XML::find ( int uid ) const
215{ 247{
216 bool found = false;
217 OContact foundContact; //Create empty contact 248 OContact foundContact; //Create empty contact
218 249
219 QValueListConstIterator<OContact> it; 250 OContact* found = m_uidToContact.find( QString().setNum( uid ) );
220 for( it = m_contactList.begin(); it != m_contactList.end(); ++it ){ 251
221 if ((*it).uid() == uid){
222 found = true;
223 break;
224 }
225 }
226 if ( found ){ 252 if ( found ){
227 foundContact = *it; 253 foundContact = *found;
228 } 254 }
229 255
230 return ( foundContact ); 256 return ( foundContact );
231} 257}
232 258
233QArray<int> OContactAccessBackend_XML::queryByExample ( const OContact &query, int settings ) 259QArray<int> OContactAccessBackend_XML::queryByExample ( const OContact &query, int settings,
260 const QDateTime& d )
234{ 261{
235 262
236 QArray<int> m_currentQuery( m_contactList.count() ); 263 QArray<int> m_currentQuery( m_contactList.count() );
237 QValueListConstIterator<OContact> it; 264 QListIterator<OContact> it( m_contactList );
238 uint arraycounter = 0; 265 uint arraycounter = 0;
239 266
240 for( it = m_contactList.begin(); it != m_contactList.end(); ++it ){ 267 for( ; it.current(); ++it ){
241 /* Search all fields and compare them with query object. Store them into list 268 /* Search all fields and compare them with query object. Store them into list
242 * if all fields matches. 269 * if all fields matches.
243 */ 270 */
@@ -250,11 +277,11 @@ QArray<int> OContactAccessBackend_XML::queryByExample ( const OContact &query, i
250 switch ( i ){ 277 switch ( i ){
251 case Qtopia::Birthday: 278 case Qtopia::Birthday:
252 queryDate = new QDate( query.birthday() ); 279 queryDate = new QDate( query.birthday() );
253 checkDate = new QDate( (*it).birthday() ); 280 checkDate = new QDate( (*it)->birthday() );
254 case Qtopia::Anniversary: 281 case Qtopia::Anniversary:
255 if ( queryDate == 0l ){ 282 if ( queryDate == 0l ){
256 queryDate = new QDate( query.anniversary() ); 283 queryDate = new QDate( query.anniversary() );
257 checkDate = new QDate( (*it).anniversary() ); 284 checkDate = new QDate( (*it)->anniversary() );
258 } 285 }
259 286
260 if ( queryDate->isValid() ){ 287 if ( queryDate->isValid() ){
@@ -272,7 +299,15 @@ QArray<int> OContactAccessBackend_XML::queryByExample ( const OContact &query, i
272 allcorrect = false; 299 allcorrect = false;
273 } 300 }
274 if ( settings & OContactAccess::DateDiff ) { 301 if ( settings & OContactAccess::DateDiff ) {
275 QDate current = QDate::currentDate(); 302 QDate current;
303 // If we get an additional date, we
304 // will take this date instead of
305 // the current one..
306 if ( !d.date().isValid() )
307 current = QDate::currentDate();
308 else
309 current = d.date();
310
276 // We have to equalize the year, otherwise 311 // We have to equalize the year, otherwise
277 // the search will fail.. 312 // the search will fail..
278 checkDate->setYMD( current.year(), 313 checkDate->setYMD( current.year(),
@@ -282,6 +317,10 @@ QArray<int> OContactAccessBackend_XML::queryByExample ( const OContact &query, i
282 checkDate->setYMD( current.year()+1, 317 checkDate->setYMD( current.year()+1,
283 checkDate->month(), 318 checkDate->month(),
284 checkDate->day() ); 319 checkDate->day() );
320
321 // Check whether the birthday/anniversary date is between
322 // the current/given date and the maximum date
323 // ( maximum time range ) !
285 qWarning("Checking if %s is between %s and %s ! ", 324 qWarning("Checking if %s is between %s and %s ! ",
286 checkDate->toString().latin1(), 325 checkDate->toString().latin1(),
287 current.toString().latin1(), 326 current.toString().latin1(),
@@ -295,7 +334,7 @@ QArray<int> OContactAccessBackend_XML::queryByExample ( const OContact &query, i
295 } 334 }
296 } 335 }
297 } else{ 336 } else{
298 // checkDate is invalid. Therfore this entry is always rejected 337 // checkDate is invalid. Therefore this entry is always rejected
299 allcorrect = false; 338 allcorrect = false;
300 } 339 }
301 } 340 }
@@ -320,7 +359,7 @@ QArray<int> OContactAccessBackend_XML::queryByExample ( const OContact &query, i
320 QRegExp expr ( query.field(i), 359 QRegExp expr ( query.field(i),
321 !(settings & OContactAccess::IgnoreCase), 360 !(settings & OContactAccess::IgnoreCase),
322 false ); 361 false );
323 if ( expr.find ( (*it).field(i), 0 ) == -1 ) 362 if ( expr.find ( (*it)->field(i), 0 ) == -1 )
324 allcorrect = false; 363 allcorrect = false;
325 } 364 }
326 break; 365 break;
@@ -328,17 +367,17 @@ QArray<int> OContactAccessBackend_XML::queryByExample ( const OContact &query, i
328 QRegExp expr ( query.field(i), 367 QRegExp expr ( query.field(i),
329 !(settings & OContactAccess::IgnoreCase), 368 !(settings & OContactAccess::IgnoreCase),
330 true ); 369 true );
331 if ( expr.find ( (*it).field(i), 0 ) == -1 ) 370 if ( expr.find ( (*it)->field(i), 0 ) == -1 )
332 allcorrect = false; 371 allcorrect = false;
333 } 372 }
334 break; 373 break;
335 case OContactAccess::ExactMatch:{ 374 case OContactAccess::ExactMatch:{
336 if (settings & OContactAccess::IgnoreCase){ 375 if (settings & OContactAccess::IgnoreCase){
337 if ( query.field(i).upper() != 376 if ( query.field(i).upper() !=
338 (*it).field(i).upper() ) 377 (*it)->field(i).upper() )
339 allcorrect = false; 378 allcorrect = false;
340 }else{ 379 }else{
341 if ( query.field(i) != (*it).field(i) ) 380 if ( query.field(i) != (*it)->field(i) )
342 allcorrect = false; 381 allcorrect = false;
343 } 382 }
344 } 383 }
@@ -348,7 +387,7 @@ QArray<int> OContactAccessBackend_XML::queryByExample ( const OContact &query, i
348 } 387 }
349 } 388 }
350 if ( allcorrect ){ 389 if ( allcorrect ){
351 m_currentQuery[arraycounter++] = (*it).uid(); 390 m_currentQuery[arraycounter++] = (*it)->uid();
352 } 391 }
353 } 392 }
354 393
@@ -361,12 +400,12 @@ QArray<int> OContactAccessBackend_XML::queryByExample ( const OContact &query, i
361QArray<int> OContactAccessBackend_XML::matchRegexp( const QRegExp &r ) const 400QArray<int> OContactAccessBackend_XML::matchRegexp( const QRegExp &r ) const
362{ 401{
363 QArray<int> m_currentQuery( m_contactList.count() ); 402 QArray<int> m_currentQuery( m_contactList.count() );
364 QValueListConstIterator<OContact> it; 403 QListIterator<OContact> it( m_contactList );
365 uint arraycounter = 0; 404 uint arraycounter = 0;
366 405
367 for( it = m_contactList.begin(); it != m_contactList.end(); ++it ){ 406 for( ; it.current(); ++it ){
368 if ( (*it).match( r ) ){ 407 if ( (*it)->match( r ) ){
369 m_currentQuery[arraycounter++] = (*it).uid(); 408 m_currentQuery[arraycounter++] = (*it)->uid();
370 } 409 }
371 410
372 } 411 }
@@ -426,10 +465,10 @@ QArray<int> OContactAccessBackend_XML::sorted( bool asc, int , int , int )
426 465
427 // First fill map and StringList with all Names 466 // First fill map and StringList with all Names
428 // Afterwards sort namelist and use map to fill array to return.. 467 // Afterwards sort namelist and use map to fill array to return..
429 QValueListConstIterator<OContact> it; 468 QListIterator<OContact> it( m_contactList );
430 for( it = m_contactList.begin(); it != m_contactList.end(); ++it ){ 469 for( ; it.current(); ++it ){
431 names.append( (*it).fileAs() + QString::number( (*it).uid() ) ); 470 names.append( (*it)->fileAs() + QString::number( (*it)->uid() ) );
432 nameToUid.insert( (*it).fileAs() + QString::number( (*it).uid() ), (*it).uid() ); 471 nameToUid.insert( (*it)->fileAs() + QString::number( (*it)->uid() ), (*it)->uid() );
433 } 472 }
434 names.sort(); 473 names.sort();
435 474
@@ -461,19 +500,19 @@ bool OContactAccessBackend_XML::replace ( const OContact &contact )
461{ 500{
462 m_changed = true; 501 m_changed = true;
463 502
464 bool found = false; 503 OContact* found = m_uidToContact.find ( QString().setNum( contact.uid() ) );
465 504
466 QValueListIterator<OContact> it;
467 for( it = m_contactList.begin(); it != m_contactList.end(); ++it ){
468 if ( (*it).uid() == contact.uid() ){
469 found = true;
470 break;
471 }
472 }
473 if (found) { 505 if (found) {
474 updateJournal (contact, ACTION_REPLACE); 506 OContact* newCont = new OContact( contact );
475 m_contactList.remove (it); 507
476 m_contactList.append (contact); 508 updateJournal ( *newCont, ACTION_REPLACE);
509 m_contactList.removeRef ( found );
510 m_contactList.append ( newCont );
511 m_uidToContact.remove( QString().setNum( contact.uid() ) );
512 m_uidToContact.insert( QString().setNum( newCont->uid() ), newCont );
513
514 qWarning("Nur zur Sicherheit: %d == %d ?",contact.uid(), newCont->uid());
515
477 return true; 516 return true;
478 } else 517 } else
479 return false; 518 return false;
@@ -483,17 +522,13 @@ bool OContactAccessBackend_XML::remove ( int uid )
483{ 522{
484 m_changed = true; 523 m_changed = true;
485 524
486 bool found = false; 525 OContact* found = m_uidToContact.find ( QString().setNum( uid ) );
487 QValueListIterator<OContact> it; 526
488 for( it = m_contactList.begin(); it != m_contactList.end(); ++it ){
489 if ((*it).uid() == uid){
490 found = true;
491 break;
492 }
493 }
494 if (found) { 527 if (found) {
495 updateJournal ( *it, ACTION_REMOVE); 528 updateJournal ( *found, ACTION_REMOVE);
496 m_contactList.remove (it); 529 m_contactList.removeRef ( found );
530 m_uidToContact.remove( QString().setNum( uid ) );
531
497 return true; 532 return true;
498 } else 533 } else
499 return false; 534 return false;
@@ -506,7 +541,10 @@ bool OContactAccessBackend_XML::reload(){
506 541
507void OContactAccessBackend_XML::addContact_p( const OContact &newcontact ) 542void OContactAccessBackend_XML::addContact_p( const OContact &newcontact )
508{ 543{
509 m_contactList.append (newcontact); 544 OContact* contRef = new OContact( newcontact );
545
546 m_contactList.append ( contRef );
547 m_uidToContact.insert( QString().setNum( newcontact.uid() ), contRef );
510} 548}
511 549
512/* This function loads the xml-database and the journalfile */ 550/* This function loads the xml-database and the journalfile */