Diffstat (limited to 'libopie/pim/ocontactaccessbackend_xml.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | libopie/pim/ocontactaccessbackend_xml.cpp | 166 |
1 files changed, 102 insertions, 64 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 @@ -18,4 +18,18 @@ * History: * $Log$ + * Revision 1.2 2003/03/21 10:33:09 eilers + * Merged speed optimized xml backend for contacts to main. + * Added QDateTime to querybyexample. For instance, it is now possible to get + * all Birthdays/Anniversaries between two dates. This should be used + * to show all birthdays in the datebook.. + * This change is sourcecode backward compatible but you have to upgrade + * the binaries for today-addressbook. + * + * Revision 1.1.2.2 2003/02/11 12:17:28 eilers + * Speed optimization. Removed the sequential search loops. + * + * Revision 1.1.2.1 2003/02/10 15:31:38 eilers + * Writing offsets to debug output.. + * * Revision 1.1 2003/02/09 15:05:01 eilers * Nothing happened.. Just some cleanup before I will start.. @@ -90,4 +104,9 @@ OContactAccessBackend_XML::OContactAccessBackend_XML ( QString appname, QString m_changed( false ) { + // Just m_contactlist should call delete if an entry + // is removed. + m_contactList.setAutoDelete( true ); + m_uidToContact.setAutoDelete( false ); + m_appName = appname; @@ -118,17 +137,27 @@ bool OContactAccessBackend_XML::save() int total_written; + int idx_offset = 0; QString out; + + // Write Header out = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE Addressbook ><AddressBook>\n" " <Groups>\n" " </Groups>\n" " <Contacts>\n"; - //QValueList<Contact>::iterator it; - QValueListConstIterator<OContact> it; - for ( it = m_contactList.begin(); it != m_contactList.end(); ++it ) { + QCString cstr = out.utf8(); + f.writeBlock( cstr.data(), cstr.length() ); + idx_offset += cstr.length(); + out = ""; + + // Write all contacts + QListIterator<OContact> it( m_contactList ); + for ( ; it.current(); ++it ) { + qWarning(" Uid %d at Offset: %x", (*it)->uid(), idx_offset ); out += "<Contact "; - (*it).save( out ); + (*it)->save( out ); out += "/>\n"; - QCString cstr = out.utf8(); + cstr = out.utf8(); total_written = f.writeBlock( cstr.data(), cstr.length() ); + idx_offset += cstr.length(); if ( total_written != int(cstr.length()) ) { f.close(); @@ -140,5 +169,6 @@ bool OContactAccessBackend_XML::save() out += " </Contacts>\n</AddressBook>\n"; - QCString cstr = out.utf8(); + // Write Footer + cstr = out.utf8(); total_written = f.writeBlock( cstr.data(), cstr.length() ); if ( total_written != int( cstr.length() ) ) { @@ -168,4 +198,5 @@ bool OContactAccessBackend_XML::load () { m_contactList.clear(); + m_uidToContact.clear(); /* Load XML-File and journal if it exists */ @@ -186,6 +217,7 @@ void OContactAccessBackend_XML::clear () { m_contactList.clear(); + m_uidToContact.clear(); + m_changed = false; - } @@ -204,7 +236,7 @@ QArray<int> OContactAccessBackend_XML::allRecords() const uint counter = 0; - QValueListConstIterator<OContact> it; - for( it = m_contactList.begin(); it != m_contactList.end(); ++it ){ - uid_list[counter++] = (*it).uid(); + QListIterator<OContact> it( m_contactList ); + for( ; it.current(); ++it ){ + uid_list[counter++] = (*it)->uid(); } @@ -214,16 +246,10 @@ QArray<int> OContactAccessBackend_XML::allRecords() const OContact OContactAccessBackend_XML::find ( int uid ) const { - bool found = false; OContact foundContact; //Create empty contact - QValueListConstIterator<OContact> it; - for( it = m_contactList.begin(); it != m_contactList.end(); ++it ){ - if ((*it).uid() == uid){ - found = true; - break; - } - } + OContact* found = m_uidToContact.find( QString().setNum( uid ) ); + if ( found ){ - foundContact = *it; + foundContact = *found; } @@ -231,12 +257,13 @@ OContact OContactAccessBackend_XML::find ( int uid ) const } -QArray<int> OContactAccessBackend_XML::queryByExample ( const OContact &query, int settings ) +QArray<int> OContactAccessBackend_XML::queryByExample ( const OContact &query, int settings, + const QDateTime& d ) { QArray<int> m_currentQuery( m_contactList.count() ); - QValueListConstIterator<OContact> it; + QListIterator<OContact> it( m_contactList ); uint arraycounter = 0; - for( it = m_contactList.begin(); it != m_contactList.end(); ++it ){ + for( ; it.current(); ++it ){ /* Search all fields and compare them with query object. Store them into list * if all fields matches. @@ -251,9 +278,9 @@ QArray<int> OContactAccessBackend_XML::queryByExample ( const OContact &query, i case Qtopia::Birthday: queryDate = new QDate( query.birthday() ); - checkDate = new QDate( (*it).birthday() ); + checkDate = new QDate( (*it)->birthday() ); case Qtopia::Anniversary: if ( queryDate == 0l ){ queryDate = new QDate( query.anniversary() ); - checkDate = new QDate( (*it).anniversary() ); + checkDate = new QDate( (*it)->anniversary() ); } @@ -273,5 +300,13 @@ QArray<int> OContactAccessBackend_XML::queryByExample ( const OContact &query, i } if ( settings & OContactAccess::DateDiff ) { - QDate current = QDate::currentDate(); + QDate current; + // If we get an additional date, we + // will take this date instead of + // the current one.. + if ( !d.date().isValid() ) + current = QDate::currentDate(); + else + current = d.date(); + // We have to equalize the year, otherwise // the search will fail.. @@ -283,4 +318,8 @@ QArray<int> OContactAccessBackend_XML::queryByExample ( const OContact &query, i checkDate->month(), checkDate->day() ); + + // Check whether the birthday/anniversary date is between + // the current/given date and the maximum date + // ( maximum time range ) ! qWarning("Checking if %s is between %s and %s ! ", checkDate->toString().latin1(), @@ -296,5 +335,5 @@ QArray<int> OContactAccessBackend_XML::queryByExample ( const OContact &query, i } } else{ - // checkDate is invalid. Therfore this entry is always rejected + // checkDate is invalid. Therefore this entry is always rejected allcorrect = false; } @@ -321,5 +360,5 @@ QArray<int> OContactAccessBackend_XML::queryByExample ( const OContact &query, i !(settings & OContactAccess::IgnoreCase), false ); - if ( expr.find ( (*it).field(i), 0 ) == -1 ) + if ( expr.find ( (*it)->field(i), 0 ) == -1 ) allcorrect = false; } @@ -329,5 +368,5 @@ QArray<int> OContactAccessBackend_XML::queryByExample ( const OContact &query, i !(settings & OContactAccess::IgnoreCase), true ); - if ( expr.find ( (*it).field(i), 0 ) == -1 ) + if ( expr.find ( (*it)->field(i), 0 ) == -1 ) allcorrect = false; } @@ -336,8 +375,8 @@ QArray<int> OContactAccessBackend_XML::queryByExample ( const OContact &query, i if (settings & OContactAccess::IgnoreCase){ if ( query.field(i).upper() != - (*it).field(i).upper() ) + (*it)->field(i).upper() ) allcorrect = false; }else{ - if ( query.field(i) != (*it).field(i) ) + if ( query.field(i) != (*it)->field(i) ) allcorrect = false; } @@ -349,5 +388,5 @@ QArray<int> OContactAccessBackend_XML::queryByExample ( const OContact &query, i } if ( allcorrect ){ - m_currentQuery[arraycounter++] = (*it).uid(); + m_currentQuery[arraycounter++] = (*it)->uid(); } } @@ -362,10 +401,10 @@ QArray<int> OContactAccessBackend_XML::matchRegexp( const QRegExp &r ) const { QArray<int> m_currentQuery( m_contactList.count() ); - QValueListConstIterator<OContact> it; + QListIterator<OContact> it( m_contactList ); uint arraycounter = 0; - for( it = m_contactList.begin(); it != m_contactList.end(); ++it ){ - if ( (*it).match( r ) ){ - m_currentQuery[arraycounter++] = (*it).uid(); + for( ; it.current(); ++it ){ + if ( (*it)->match( r ) ){ + m_currentQuery[arraycounter++] = (*it)->uid(); } @@ -427,8 +466,8 @@ QArray<int> OContactAccessBackend_XML::sorted( bool asc, int , int , int ) // First fill map and StringList with all Names // Afterwards sort namelist and use map to fill array to return.. - QValueListConstIterator<OContact> it; - for( it = m_contactList.begin(); it != m_contactList.end(); ++it ){ - names.append( (*it).fileAs() + QString::number( (*it).uid() ) ); - nameToUid.insert( (*it).fileAs() + QString::number( (*it).uid() ), (*it).uid() ); + QListIterator<OContact> it( m_contactList ); + for( ; it.current(); ++it ){ + names.append( (*it)->fileAs() + QString::number( (*it)->uid() ) ); + nameToUid.insert( (*it)->fileAs() + QString::number( (*it)->uid() ), (*it)->uid() ); } names.sort(); @@ -462,17 +501,17 @@ bool OContactAccessBackend_XML::replace ( const OContact &contact ) m_changed = true; - bool found = false; - - QValueListIterator<OContact> it; - for( it = m_contactList.begin(); it != m_contactList.end(); ++it ){ - if ( (*it).uid() == contact.uid() ){ - found = true; - break; - } - } - if (found) { - updateJournal (contact, ACTION_REPLACE); - m_contactList.remove (it); - m_contactList.append (contact); + OContact* found = m_uidToContact.find ( QString().setNum( contact.uid() ) ); + + if ( found ) { + OContact* newCont = new OContact( contact ); + + updateJournal ( *newCont, ACTION_REPLACE); + m_contactList.removeRef ( found ); + m_contactList.append ( newCont ); + m_uidToContact.remove( QString().setNum( contact.uid() ) ); + m_uidToContact.insert( QString().setNum( newCont->uid() ), newCont ); + + qWarning("Nur zur Sicherheit: %d == %d ?",contact.uid(), newCont->uid()); + return true; } else @@ -484,15 +523,11 @@ bool OContactAccessBackend_XML::remove ( int uid ) m_changed = true; - bool found = false; - QValueListIterator<OContact> it; - for( it = m_contactList.begin(); it != m_contactList.end(); ++it ){ - if ((*it).uid() == uid){ - found = true; - break; - } - } - if (found) { - updateJournal ( *it, ACTION_REMOVE); - m_contactList.remove (it); + OContact* found = m_uidToContact.find ( QString().setNum( uid ) ); + + if ( found ) { + updateJournal ( *found, ACTION_REMOVE); + m_contactList.removeRef ( found ); + m_uidToContact.remove( QString().setNum( uid ) ); + return true; } else @@ -507,5 +542,8 @@ bool OContactAccessBackend_XML::reload(){ void OContactAccessBackend_XML::addContact_p( const OContact &newcontact ) { - m_contactList.append (newcontact); + OContact* contRef = new OContact( newcontact ); + + m_contactList.append ( contRef ); + m_uidToContact.insert( QString().setNum( newcontact.uid() ), contRef ); } |