kitchensync
addressbooksyncee.cpp
00001 /* 00002 This file is part of KitchenSync. 00003 00004 Copyright (c) 2002,2004 Cornelius Schumacher <schumacher@kde.org> 00005 Copyright (c) 2002 Holger Freyther <zecke@handhelds.org> 00006 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Library General Public 00009 License as published by the Free Software Foundation; either 00010 version 2 of the License, or (at your option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Library General Public License for more details. 00016 00017 You should have received a copy of the GNU Library General Public License 00018 along with this library; see the file COPYING.LIB. If not, write to 00019 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00020 Boston, MA 02110-1301, USA. 00021 */ 00022 00023 #include "addressbooksyncee.h" 00024 00025 #include "addressbookmerger.h" 00026 #include "syncee.h" 00027 00028 #include <libkdepim/addresseediffalgo.h> 00029 #include <libkdepim/kabcresourcenull.h> 00030 00031 #include <kapplication.h> 00032 #include <kdebug.h> 00033 #include <kstaticdeleter.h> 00034 00035 using namespace KSync; 00036 00037 AddressBookSyncEntry::AddressBookSyncEntry( Syncee* parent ) 00038 : SyncEntry( parent ) 00039 { 00040 setType( QString::fromLatin1("AddressBookSyncEntry") ); 00041 } 00042 00043 AddressBookSyncEntry::AddressBookSyncEntry( const KABC::Addressee &a, 00044 Syncee *parent ) 00045 : SyncEntry( parent ) 00046 { 00047 mAddressee = a; 00048 setType( QString::fromLatin1("AddressBookSyncEntry") ); 00049 } 00050 00051 AddressBookSyncEntry::AddressBookSyncEntry( const AddressBookSyncEntry& entry ) 00052 : SyncEntry( entry ) 00053 { 00054 mAddressee = entry.mAddressee; 00055 m_res = entry.m_res; 00056 // type is copied by the SyncEntry c'tor 00057 } 00058 00059 QString AddressBookSyncEntry::name() 00060 { 00061 return mAddressee.realName(); 00062 } 00063 00064 QString AddressBookSyncEntry::id() 00065 { 00066 return mAddressee.uid(); 00067 } 00068 00069 void AddressBookSyncEntry::setId(const QString& id) 00070 { 00071 mAddressee.setUid( id ); 00072 } 00073 00074 AddressBookSyncEntry* AddressBookSyncEntry::clone() { 00075 return new AddressBookSyncEntry( *this ); 00076 } 00077 00078 QString AddressBookSyncEntry::timestamp() 00079 { 00080 QDateTime r = mAddressee.revision(); 00081 if ( r.isValid() ) return r.toString(); 00082 else return "norevision"; 00083 } 00084 00085 bool AddressBookSyncEntry::equals( SyncEntry *entry ) 00086 { 00087 AddressBookSyncEntry *abEntry = dynamic_cast<AddressBookSyncEntry *>(entry); 00088 if ( !abEntry ) { 00089 kdDebug(5228) << "AddressBookSyncee::equals(): Wrong type." << endl; 00090 return false; 00091 } 00092 00093 if ( mAddressee == abEntry->addressee() ) { 00094 kdDebug(5228) << "AddressBookSyncEntry::equals(): '" << entry->name() << "':" 00095 << "equal" << endl; 00096 return true; 00097 } else { 00098 kdDebug(5228) << "AddressBookSyncEntry::equals(): '" << entry->name() << "':" 00099 << "not equal" << endl; 00100 return false; 00101 } 00102 } 00103 00104 QString AddressBookSyncEntry::resource() const 00105 { 00106 return m_res; 00107 } 00108 00109 void AddressBookSyncEntry::setResource( const QString &str ) 00110 { 00111 m_res = str; 00112 } 00113 00114 KPIM::DiffAlgo* AddressBookSyncEntry::diffAlgo( SyncEntry *syncEntry, SyncEntry *targetEntry ) 00115 { 00116 AddressBookSyncEntry *abSyncEntry = dynamic_cast<AddressBookSyncEntry*>( syncEntry ); 00117 AddressBookSyncEntry *abTargetEntry = dynamic_cast<AddressBookSyncEntry*>( targetEntry ); 00118 00119 if ( !abSyncEntry || !abTargetEntry ) 00120 return 0; 00121 00122 return new KPIM::AddresseeDiffAlgo( abSyncEntry->addressee(), abTargetEntry->addressee() ); 00123 } 00124 00125 void AddressBookSyncEntry::setAddressee( const KABC::Addressee& addr ) { 00126 mAddressee = addr; 00127 } 00128 00129 00133 AddressBookSyncee::AddressBookSyncee( AddressBookMerger* merger) 00134 : Syncee( merger ) 00135 { 00136 setType( QString::fromLatin1("AddressBookSyncee") ); 00137 mAddressBook = new KABC::AddressBook; 00138 mAddressBook->addResource( new KABC::ResourceNull() ); 00139 mOwnAddressBook = true; 00140 00141 mEntries.setAutoDelete( false ); 00142 } 00143 00144 AddressBookSyncee::AddressBookSyncee( KABC::AddressBook *ab, AddressBookMerger* merger ) 00145 : Syncee( merger ) // set the support size 00146 { 00147 setType( QString::fromLatin1("AddressBookSyncee") ); 00148 mAddressBook = ab; 00149 mOwnAddressBook = false; 00150 00151 mEntries.setAutoDelete( false ); 00152 00153 KABC::AddressBook::Iterator it; 00154 for ( it = ab->begin(); it != ab->end(); ++it ) 00155 createEntry( *it ); 00156 00157 } 00158 00159 AddressBookSyncee::~AddressBookSyncee() 00160 { 00161 if ( mOwnAddressBook ) delete mAddressBook; 00162 mEntries.setAutoDelete( true ); 00163 } 00164 00165 void AddressBookSyncee::reset() 00166 { 00167 mEntries.clear(); 00168 } 00169 00170 AddressBookSyncEntry *AddressBookSyncee::firstEntry() 00171 { 00172 return mEntries.first(); 00173 } 00174 00175 AddressBookSyncEntry *AddressBookSyncee::nextEntry() 00176 { 00177 return mEntries.next(); 00178 } 00179 00180 00181 void AddressBookSyncee::addEntry( SyncEntry *entry ) 00182 { 00183 // kdDebug() << "AddressBookSyncee::addEntry()" << endl; 00184 00185 AddressBookSyncEntry *abEntry = dynamic_cast<AddressBookSyncEntry *>( entry ); 00186 00187 if ( !abEntry ) { 00188 kdDebug(5228) << "AddressBookSyncee::addEntry(): SyncEntry has wrong type." 00189 << endl; 00190 } else { 00191 abEntry->setSyncee( this ); // set the parent as we're now responsible 00192 00193 mEntries.append( abEntry ); 00194 00195 KABC::Addressee a = abEntry->addressee(); 00196 a.setResource( 0 ); 00197 if (!a.revision().isValid() ) 00198 a.setRevision( QDateTime::currentDateTime() ); 00199 mAddressBook->insertAddressee( a ); 00200 00201 /* 00202 * now we need to update the Addressee to contain the 00203 * right resource so that removal works as well 00204 */ 00205 abEntry->setAddressee( mAddressBook->findByUid( a.uid() ) ); 00206 } 00207 } 00208 00209 void AddressBookSyncee::removeEntry( SyncEntry *entry ) 00210 { 00211 AddressBookSyncEntry *abEntry = dynamic_cast<AddressBookSyncEntry *>(entry); 00212 if ( !abEntry ) { 00213 kdDebug(5228) << "AddressBookSyncee::removeEntry(): SyncEntry has wrong type." 00214 << endl; 00215 } else { 00216 mAddressBook->removeAddressee( abEntry->addressee() ); 00217 abEntry->setSyncee( 0 ); 00218 mEntries.remove( abEntry ); 00219 } 00220 } 00221 00222 AddressBookSyncEntry *AddressBookSyncee::createEntry( const KABC::Addressee &a ) 00223 { 00224 if ( !a.isEmpty() ) { 00225 AddressBookSyncEntry *entry = new AddressBookSyncEntry( a, this ); 00226 entry->setSyncee( this ); 00227 mEntries.append( entry ); 00228 return entry; 00229 } else { 00230 return 0; 00231 } 00232 } 00233 00234 00235 QString AddressBookSyncee::generateNewId() const 00236 { 00237 return KApplication::randomString( 10 ); 00238 } 00239