summaryrefslogtreecommitdiff
path: root/libopie2/opiepim/backend/ocontactaccessbackend_xml.cpp
Unidiff
Diffstat (limited to 'libopie2/opiepim/backend/ocontactaccessbackend_xml.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opiepim/backend/ocontactaccessbackend_xml.cpp29
1 files changed, 4 insertions, 25 deletions
diff --git a/libopie2/opiepim/backend/ocontactaccessbackend_xml.cpp b/libopie2/opiepim/backend/ocontactaccessbackend_xml.cpp
index 18113c2..5df7253 100644
--- a/libopie2/opiepim/backend/ocontactaccessbackend_xml.cpp
+++ b/libopie2/opiepim/backend/ocontactaccessbackend_xml.cpp
@@ -99,25 +99,24 @@ bool OPimContactAccessBackend_XML::save()
99 out = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE Addressbook ><AddressBook>\n" 99 out = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE Addressbook ><AddressBook>\n"
100 " <Groups>\n" 100 " <Groups>\n"
101 " </Groups>\n" 101 " </Groups>\n"
102 " <Contacts>\n"; 102 " <Contacts>\n";
103 QCString cstr = out.utf8(); 103 QCString cstr = out.utf8();
104 f.writeBlock( cstr.data(), cstr.length() ); 104 f.writeBlock( cstr.data(), cstr.length() );
105 idx_offset += cstr.length(); 105 idx_offset += cstr.length();
106 out = ""; 106 out = "";
107 107
108 // Write all contacts 108 // Write all contacts
109 QListIterator<OPimContact> it( m_contactList ); 109 QListIterator<OPimContact> it( m_contactList );
110 for ( ; it.current(); ++it ) { 110 for ( ; it.current(); ++it ) {
111 // owarn << " Uid " << (*it)->uid() << " at Offset: " << idx_offset << "" << oendl;
112 out += "<Contact "; 111 out += "<Contact ";
113 (*it)->save( out ); 112 (*it)->save( out );
114 out += "/>\n"; 113 out += "/>\n";
115 cstr = out.utf8(); 114 cstr = out.utf8();
116 total_written = f.writeBlock( cstr.data(), cstr.length() ); 115 total_written = f.writeBlock( cstr.data(), cstr.length() );
117 idx_offset += cstr.length(); 116 idx_offset += cstr.length();
118 if ( total_written != int(cstr.length()) ) { 117 if ( total_written != int(cstr.length()) ) {
119 f.close(); 118 f.close();
120 QFile::remove( strNewFile ); 119 QFile::remove( strNewFile );
121 return false; 120 return false;
122 } 121 }
123 out = ""; 122 out = "";
@@ -128,26 +127,24 @@ bool OPimContactAccessBackend_XML::save()
128 cstr = out.utf8(); 127 cstr = out.utf8();
129 total_written = f.writeBlock( cstr.data(), cstr.length() ); 128 total_written = f.writeBlock( cstr.data(), cstr.length() );
130 if ( total_written != int( cstr.length() ) ) { 129 if ( total_written != int( cstr.length() ) ) {
131 f.close(); 130 f.close();
132 QFile::remove( strNewFile ); 131 QFile::remove( strNewFile );
133 return false; 132 return false;
134 } 133 }
135 f.close(); 134 f.close();
136 135
137 // move the file over, I'm just going to use the system call 136 // move the file over, I'm just going to use the system call
138 // because, I don't feel like using QDir. 137 // because, I don't feel like using QDir.
139 if ( ::rename( strNewFile.latin1(), m_fileName.latin1() ) < 0 ) { 138 if ( ::rename( strNewFile.latin1(), m_fileName.latin1() ) < 0 ) {
140 owarn << "problem renaming file " << strNewFile << " to " << m_journalName
141 << ", errno: " << errno << oendl;
142 // remove the tmp file... 139 // remove the tmp file...
143 QFile::remove( strNewFile ); 140 QFile::remove( strNewFile );
144 } 141 }
145 142
146 /* The journalfile should be removed now... */ 143 /* The journalfile should be removed now... */
147 removeJournal(); 144 removeJournal();
148 145
149 m_changed = false; 146 m_changed = false;
150 return true; 147 return true;
151} 148}
152 149
153bool OPimContactAccessBackend_XML::load () 150bool OPimContactAccessBackend_XML::load ()
@@ -204,27 +201,26 @@ OPimContact OPimContactAccessBackend_XML::find ( int uid ) const
204 OPimContact foundContact; //Create empty contact 201 OPimContact foundContact; //Create empty contact
205 202
206 OPimContact* found = m_uidToContact.find( QString().setNum( uid ) ); 203 OPimContact* found = m_uidToContact.find( QString().setNum( uid ) );
207 204
208 if ( found ){ 205 if ( found ){
209 foundContact = *found; 206 foundContact = *found;
210 } 207 }
211 208
212 return ( foundContact ); 209 return ( foundContact );
213} 210}
214 211
215QArray<int> OPimContactAccessBackend_XML::queryByExample ( const OPimContact &query, int settings, 212QArray<int> OPimContactAccessBackend_XML::queryByExample ( const OPimContact &query, int settings,
216 const QDateTime& d ) 213 const QDateTime& d )const
217{ 214{
218
219 QArray<int> m_currentQuery( m_contactList.count() ); 215 QArray<int> m_currentQuery( m_contactList.count() );
220 QListIterator<OPimContact> it( m_contactList ); 216 QListIterator<OPimContact> it( m_contactList );
221 uint arraycounter = 0; 217 uint arraycounter = 0;
222 218
223 for( ; it.current(); ++it ){ 219 for( ; it.current(); ++it ){
224 /* Search all fields and compare them with query object. Store them into list 220 /* Search all fields and compare them with query object. Store them into list
225 * if all fields matches. 221 * if all fields matches.
226 */ 222 */
227 QDate* queryDate = 0l; 223 QDate* queryDate = 0l;
228 QDate* checkDate = 0l; 224 QDate* checkDate = 0l;
229 bool allcorrect = true; 225 bool allcorrect = true;
230 for ( int i = 0; i < Qtopia::Groups; i++ ) { 226 for ( int i = 0; i < Qtopia::Groups; i++ ) {
@@ -269,31 +265,28 @@ QArray<int> OPimContactAccessBackend_XML::queryByExample ( const OPimContact &qu
269 // the search will fail.. 265 // the search will fail..
270 checkDate->setYMD( current.year(), 266 checkDate->setYMD( current.year(),
271 checkDate->month(), 267 checkDate->month(),
272 checkDate->day() ); 268 checkDate->day() );
273 if ( *checkDate < current ) 269 if ( *checkDate < current )
274 checkDate->setYMD( current.year()+1, 270 checkDate->setYMD( current.year()+1,
275 checkDate->month(), 271 checkDate->month(),
276 checkDate->day() ); 272 checkDate->day() );
277 273
278 // Check whether the birthday/anniversary date is between 274 // Check whether the birthday/anniversary date is between
279 // the current/given date and the maximum date 275 // the current/given date and the maximum date
280 // ( maximum time range ) ! 276 // ( maximum time range ) !
281 owarn << "Checking if " << checkDate->toString() << " is between " << current.toString()
282 << " and " << queryDate->toString() << " ! " << oendl;
283 if ( current.daysTo( *queryDate ) >= 0 ){ 277 if ( current.daysTo( *queryDate ) >= 0 ){
284 if ( !( ( *checkDate >= current ) && 278 if ( !( ( *checkDate >= current ) &&
285 ( *checkDate <= *queryDate ) ) ){ 279 ( *checkDate <= *queryDate ) ) ){
286 allcorrect = false; 280 allcorrect = false;
287 owarn << " Nope!.." << oendl;
288 } 281 }
289 } 282 }
290 } 283 }
291 } else{ 284 } else{
292 // checkDate is invalid. Therefore this entry is always rejected 285 // checkDate is invalid. Therefore this entry is always rejected
293 allcorrect = false; 286 allcorrect = false;
294 } 287 }
295 } 288 }
296 289
297 delete queryDate; 290 delete queryDate;
298 queryDate = 0l; 291 queryDate = 0l;
299 delete checkDate; 292 delete checkDate;
@@ -421,24 +414,25 @@ bool OPimContactAccessBackend_XML::hasQuerySettings (uint querySettings) const
421 return ( true ); 414 return ( true );
422 case OPimContactAccess::WildCards: 415 case OPimContactAccess::WildCards:
423 return ( true ); 416 return ( true );
424 case OPimContactAccess::ExactMatch: 417 case OPimContactAccess::ExactMatch:
425 return ( true ); 418 return ( true );
426 case 0: // one of the upper removed bits were set.. 419 case 0: // one of the upper removed bits were set..
427 return ( true ); 420 return ( true );
428 default: 421 default:
429 return ( false ); 422 return ( false );
430 } 423 }
431} 424}
432 425
426#if 0
433// Currently only asc implemented.. 427// Currently only asc implemented..
434QArray<int> OPimContactAccessBackend_XML::sorted( bool asc, int , int , int ) 428QArray<int> OPimContactAccessBackend_XML::sorted( bool asc, int , int , int )
435{ 429{
436 QMap<QString, int> nameToUid; 430 QMap<QString, int> nameToUid;
437 QStringList names; 431 QStringList names;
438 QArray<int> m_currentQuery( m_contactList.count() ); 432 QArray<int> m_currentQuery( m_contactList.count() );
439 433
440 // First fill map and StringList with all Names 434 // First fill map and StringList with all Names
441 // Afterwards sort namelist and use map to fill array to return.. 435 // Afterwards sort namelist and use map to fill array to return..
442 QListIterator<OPimContact> it( m_contactList ); 436 QListIterator<OPimContact> it( m_contactList );
443 for( ; it.current(); ++it ){ 437 for( ; it.current(); ++it ){
444 names.append( (*it)->fileAs() + QString::number( (*it)->uid() ) ); 438 names.append( (*it)->fileAs() + QString::number( (*it)->uid() ) );
@@ -449,53 +443,52 @@ QArray<int> OPimContactAccessBackend_XML::sorted( bool asc, int , int , int )
449 int i = 0; 443 int i = 0;
450 if ( asc ){ 444 if ( asc ){
451 for ( QStringList::Iterator it = names.begin(); it != names.end(); ++it ) 445 for ( QStringList::Iterator it = names.begin(); it != names.end(); ++it )
452 m_currentQuery[i++] = nameToUid[ (*it) ]; 446 m_currentQuery[i++] = nameToUid[ (*it) ];
453 }else{ 447 }else{
454 for ( QStringList::Iterator it = names.end(); it != names.begin(); --it ) 448 for ( QStringList::Iterator it = names.end(); it != names.begin(); --it )
455 m_currentQuery[i++] = nameToUid[ (*it) ]; 449 m_currentQuery[i++] = nameToUid[ (*it) ];
456 } 450 }
457 451
458 return m_currentQuery; 452 return m_currentQuery;
459 453
460} 454}
455#endif
456
461 457
462bool OPimContactAccessBackend_XML::add ( const OPimContact &newcontact ) 458bool OPimContactAccessBackend_XML::add ( const OPimContact &newcontact )
463{ 459{
464 //owarn << "odefaultbackend: ACTION::ADD" << oendl;
465 updateJournal (newcontact, ACTION_ADD); 460 updateJournal (newcontact, ACTION_ADD);
466 addContact_p( newcontact ); 461 addContact_p( newcontact );
467 462
468 m_changed = true; 463 m_changed = true;
469 464
470 return true; 465 return true;
471} 466}
472 467
473bool OPimContactAccessBackend_XML::replace ( const OPimContact &contact ) 468bool OPimContactAccessBackend_XML::replace ( const OPimContact &contact )
474{ 469{
475 m_changed = true; 470 m_changed = true;
476 471
477 OPimContact* found = m_uidToContact.find ( QString().setNum( contact.uid() ) ); 472 OPimContact* found = m_uidToContact.find ( QString().setNum( contact.uid() ) );
478 473
479 if ( found ) { 474 if ( found ) {
480 OPimContact* newCont = new OPimContact( contact ); 475 OPimContact* newCont = new OPimContact( contact );
481 476
482 updateJournal ( *newCont, ACTION_REPLACE); 477 updateJournal ( *newCont, ACTION_REPLACE);
483 m_contactList.removeRef ( found ); 478 m_contactList.removeRef ( found );
484 m_contactList.append ( newCont ); 479 m_contactList.append ( newCont );
485 m_uidToContact.remove( QString().setNum( contact.uid() ) ); 480 m_uidToContact.remove( QString().setNum( contact.uid() ) );
486 m_uidToContact.insert( QString().setNum( newCont->uid() ), newCont ); 481 m_uidToContact.insert( QString().setNum( newCont->uid() ), newCont );
487 482
488 owarn << "Nur zur Sicherheit: " << contact.uid() << " == " << newCont->uid() << " ?" << oendl;
489
490 return true; 483 return true;
491 } else 484 } else
492 return false; 485 return false;
493} 486}
494 487
495bool OPimContactAccessBackend_XML::remove ( int uid ) 488bool OPimContactAccessBackend_XML::remove ( int uid )
496{ 489{
497 m_changed = true; 490 m_changed = true;
498 491
499 OPimContact* found = m_uidToContact.find ( QString().setNum( uid ) ); 492 OPimContact* found = m_uidToContact.find ( QString().setNum( uid ) );
500 493
501 if ( found ) { 494 if ( found ) {
@@ -582,73 +575,61 @@ bool OPimContactAccessBackend_XML::load( const QString filename, bool isJournal
582 dict.insert( "Assistant", new int(Qtopia::Assistant) ); 575 dict.insert( "Assistant", new int(Qtopia::Assistant) );
583 dict.insert( "Manager", new int(Qtopia::Manager) ); 576 dict.insert( "Manager", new int(Qtopia::Manager) );
584 dict.insert( "Spouse", new int(Qtopia::Spouse) ); 577 dict.insert( "Spouse", new int(Qtopia::Spouse) );
585 dict.insert( "Children", new int(Qtopia::Children) ); 578 dict.insert( "Children", new int(Qtopia::Children) );
586 dict.insert( "Gender", new int(Qtopia::Gender) ); 579 dict.insert( "Gender", new int(Qtopia::Gender) );
587 dict.insert( "Birthday", new int(Qtopia::Birthday) ); 580 dict.insert( "Birthday", new int(Qtopia::Birthday) );
588 dict.insert( "Anniversary", new int(Qtopia::Anniversary) ); 581 dict.insert( "Anniversary", new int(Qtopia::Anniversary) );
589 dict.insert( "Nickname", new int(Qtopia::Nickname) ); 582 dict.insert( "Nickname", new int(Qtopia::Nickname) );
590 dict.insert( "Notes", new int(Qtopia::Notes) ); 583 dict.insert( "Notes", new int(Qtopia::Notes) );
591 dict.insert( "action", new int(JOURNALACTION) ); 584 dict.insert( "action", new int(JOURNALACTION) );
592 dict.insert( "actionrow", new int(JOURNALROW) ); 585 dict.insert( "actionrow", new int(JOURNALROW) );
593 586
594 //owarn << "OPimContactDefaultBackEnd::loading " << filename << "" << oendl;
595
596 XMLElement *root = XMLElement::load( filename ); 587 XMLElement *root = XMLElement::load( filename );
597 if(root != 0l ){ // start parsing 588 if(root != 0l ){ // start parsing
598 /* Parse all XML-Elements and put the data into the 589 /* Parse all XML-Elements and put the data into the
599 * Contact-Class 590 * Contact-Class
600 */ 591 */
601 XMLElement *element = root->firstChild(); 592 XMLElement *element = root->firstChild();
602 //owarn << "OPimContactAccess::load tagName(): " << root->tagName() << "" << oendl;
603 element = element ? element->firstChild() : 0; 593 element = element ? element->firstChild() : 0;
604 594
605 /* Search Tag "Contacts" which is the parent of all Contacts */ 595 /* Search Tag "Contacts" which is the parent of all Contacts */
606 while( element && !isJournal ){ 596 while( element && !isJournal ){
607 if( element->tagName() != QString::fromLatin1("Contacts") ){ 597 if( element->tagName() != QString::fromLatin1("Contacts") ){
608 //owarn << "OPimContactDefBack::Searching for Tag \"Contacts\"! Found: "
609 // << element->tagName() << oendl;
610 element = element->nextChild(); 598 element = element->nextChild();
611 } else { 599 } else {
612 element = element->firstChild(); 600 element = element->firstChild();
613 break; 601 break;
614 } 602 }
615 } 603 }
616 /* Parse all Contacts and ignore unknown tags */ 604 /* Parse all Contacts and ignore unknown tags */
617 while( element ){ 605 while( element ){
618 if( element->tagName() != QString::fromLatin1("Contact") ){ 606 if( element->tagName() != QString::fromLatin1("Contact") ){
619 //owarn << "OPimContactDefBack::Searching for Tag \"Contact\"! Found: "
620 // << element->tagName() << oendl;
621 element = element->nextChild(); 607 element = element->nextChild();
622 continue; 608 continue;
623 } 609 }
624 /* Found alement with tagname "contact", now parse and store all 610 /* Found alement with tagname "contact", now parse and store all
625 * attributes contained 611 * attributes contained
626 */ 612 */
627 //owarn << "OPimContactDefBack::load element tagName() : "
628 // << element->tagName() << oendl;
629 QString dummy; 613 QString dummy;
630 foundAction = false; 614 foundAction = false;
631 615
632 XMLElement::AttributeMap aMap = element->attributes(); 616 XMLElement::AttributeMap aMap = element->attributes();
633 XMLElement::AttributeMap::Iterator it; 617 XMLElement::AttributeMap::Iterator it;
634 contactMap.clear(); 618 contactMap.clear();
635 customMap.clear(); 619 customMap.clear();
636 for( it = aMap.begin(); it != aMap.end(); ++it ){ 620 for( it = aMap.begin(); it != aMap.end(); ++it ){
637 // owarn << "Read Attribute: " << it.key() << "=" << it.data() << oendl;
638
639 int *find = dict[ it.key() ]; 621 int *find = dict[ it.key() ];
640 /* Unknown attributes will be stored as "Custom" elements */ 622 /* Unknown attributes will be stored as "Custom" elements */
641 if ( !find ) { 623 if ( !find ) {
642 // owarn << "Attribute " << it.key() << " not known." << oendl;
643 //contact.setCustomField(it.key(), it.data()); 624 //contact.setCustomField(it.key(), it.data());
644 customMap.insert( it.key(), it.data() ); 625 customMap.insert( it.key(), it.data() );
645 continue; 626 continue;
646 } 627 }
647 628
648 /* Check if special conversion is needed and add attribute 629 /* Check if special conversion is needed and add attribute
649 * into Contact class 630 * into Contact class
650 */ 631 */
651 switch( *find ) { 632 switch( *find ) {
652 /* 633 /*
653 case Qtopia::AddressUid: 634 case Qtopia::AddressUid:
654 contact.setUid( it.data().toInt() ); 635 contact.setUid( it.data().toInt() );
@@ -695,28 +676,26 @@ bool OPimContactAccessBackend_XML::load( const QString filename, bool isJournal
695 owarn << "Unknown action: ignored !" << oendl; 676 owarn << "Unknown action: ignored !" << oendl;
696 break; 677 break;
697 } 678 }
698 }else{ 679 }else{
699 /* Add contact to list */ 680 /* Add contact to list */
700 addContact_p (contact); 681 addContact_p (contact);
701 } 682 }
702 683
703 /* Move to next element */ 684 /* Move to next element */
704 element = element->nextChild(); 685 element = element->nextChild();
705 } 686 }
706 }else { 687 }else {
707 owarn << "ODefBack::could not load" << oendl;
708 } 688 }
709 delete root; 689 delete root;
710 owarn << "returning from loading" << oendl;
711 return true; 690 return true;
712} 691}
713 692
714 693
715void OPimContactAccessBackend_XML::updateJournal( const OPimContact& cnt, 694void OPimContactAccessBackend_XML::updateJournal( const OPimContact& cnt,
716 journal_action action ) 695 journal_action action )
717{ 696{
718 QFile f( m_journalName ); 697 QFile f( m_journalName );
719 bool created = !f.exists(); 698 bool created = !f.exists();
720 if ( !f.open(IO_WriteOnly|IO_Append) ) 699 if ( !f.open(IO_WriteOnly|IO_Append) )
721 return; 700 return;
722 701