kitchensync Library API Documentation

addressbookpart.cpp

00001 
00002 
00003 #include <kdebug.h>
00004 #include <kiconloader.h>
00005 #include <kmessagebox.h>
00006 #include <ksimpleconfig.h>
00007 #include <kurlrequester.h>
00008 
00009 #include <kabc/stdaddressbook.h>
00010 
00011 #include <kparts/genericfactory.h>
00012 
00013 
00014 #include <mainwindow.h>
00015 
00016 #include <addressbooksyncee.h>
00017 #include <syncer.h>
00018 
00019 #include "addressbookpart.h"
00020 
00021 typedef KParts::GenericFactory< KSync::AddressBookPart> AddressBookPartFactory;
00022 K_EXPORT_COMPONENT_FACTORY( libaddressbookpart,  AddressBookPartFactory )
00023 
00024 using namespace KSync;
00025 
00026 
00027 
00028 AddressBookPart::AddressBookPart( QWidget* parent,  const char* name,
00029                                   QObject* obj,  const char* ,
00030                                   const QStringList & )
00031     : ManipulatorPart( parent ? parent : obj ,  name )
00032 {
00033     setInstance( AddressBookPartFactory::instance() );
00034     m_pixmap = KGlobal::iconLoader()->loadIcon("kaddressbook",  KIcon::Desktop,  48 );
00035 }
00036 AddressBookPart::~AddressBookPart(){
00037 }
00038 KAboutData *AddressBookPart::createAboutData(){
00039   return new KAboutData("KSyncAddressBookPart", I18N_NOOP("Sync AddressBook Part"), "0.0" );
00040 }
00041 QPixmap* AddressBookPart::pixmap(){
00042     return &m_pixmap;
00043 }
00044 QString AddressBookPart::type()const {
00045     return QString::fromLatin1("Addressbook");
00046 }
00047 QString AddressBookPart::name()const{
00048     return i18n("Addressbook");
00049 }
00050 QString AddressBookPart::iconName()const {
00051     return QString::fromLatin1("kaddressbook");
00052 }
00053 QString AddressBookPart::description()const {
00054     return i18n("The Addressbook Part");
00055 }
00056 bool AddressBookPart::configIsVisible()const {
00057     return false;
00058 }
00059 bool AddressBookPart::canSync()const{
00060     return true;
00061 }
00062 /*
00063  * SYnc it aye?
00064  * 1. get the currentProfile + Konnector
00065  * 2. get the paths + the path to the meta data
00066  * 3. search our AddressBookSyncee
00067  * 4. load the File
00068  * 5. do meta
00069  * 6. sync
00070  * 7. write Meta
00071  * 8. save
00072  * 9. write back
00073  * 10. party
00074  */
00075 void AddressBookPart::sync( const SynceeList& in,
00076                             SynceeList& out ){
00077     kdDebug(5228) << "processEntry in AddressBookPart aye" << endl;
00078     /* 1. */
00079     Profile prof = core()->currentProfile();
00080     KonnectorProfile kon = core()->currentKonnectorProfile();
00081 
00082     /* 2. */
00083     QString meta = kon.uid() + "/" + prof.uid() + "addressbook.rc";
00084     bool met = kon.kapabilities().isMetaSyncingEnabled();
00085     kdDebug(5228) << "Is meta syncing enabled? " << met << endl;
00086 
00087     /* 3. */
00088     AddressBookSyncee *aBook = in.addressBookSyncee();
00089     if (!aBook) {
00090       done();
00091       return;
00092     }
00093 
00094     progress( Progress(i18n("Going to load AddressBook") ) );
00095     /* 4. */
00096     AddressBookSyncee* ourbook;
00097     ourbook = load();
00098 
00099 
00100     if (!ourbook) {
00101         error( Error(i18n("Could not load the AddressBook") ) );
00102         done();
00103         return;
00104     }
00105     ourbook->setSource( i18n("KDE Addressbook") );
00106 
00107     /* 5. */
00108     if (met)
00109       doMeta( ourbook, meta );
00110 
00111     progress( Progress(i18n("Going to sync AddressBook") ) );
00112     /* 6. */
00113     Syncer sync( core()->syncUi(), core()->syncAlgorithm() );
00114     sync.addSyncee( aBook );
00115     sync.addSyncee( ourbook );
00116     sync.sync();
00117 
00118     if ( confirmBeforeWriting() ) {
00119         switch ( KMessageBox::questionYesNo(0, i18n("Do you want to write back addressbook?"), i18n("Save"),
00120                                             KStdGuiItem::save(), KStdGuiItem::dontSave() ) ) {
00121         case KMessageBox::No:{
00122             delete ourbook;
00123             done();
00124             return;
00125 
00126             break;
00127         }
00128         default:
00129             break;
00130         }
00131     }
00132 
00133     /* 7. KABC seems broken so we do meta from save*/
00134 /*    if (met)
00135       writeMeta( ourbook, meta );
00136 */
00137 
00138     progress( Progress(i18n("Going to save AddressBook") ) );
00139     /* 8. */
00140     save( ourbook, met ? meta : QString::null );
00141 
00142     /* writeback */
00143     out.append( ourbook );
00144     done();
00145 }
00146 void AddressBookPart::slotConfigOk(){
00147 }
00148 /*
00149  * let's load it
00150  * if path is empty or default Take KStdAddressBook
00151  * otherwise load the file
00152  */
00153 AddressBookSyncee* AddressBookPart::load() {
00154     KABC::AddressBook* book;
00155     AddressBookSyncee* sync;
00156     kdDebug(5228) << "use default one " << endl;
00157     book =  KABC::StdAddressBook::self();
00158     if (!book->load() )
00159         return 0l;
00160     sync = book2syncee( book );
00161     return sync;
00162 }
00163 void AddressBookPart::doMeta( Syncee* syncee, const QString& path ) {
00164     kdDebug(5228) << "Do Meta" << endl;
00165     QString str = QDir::homeDirPath();
00166     str += "/.kitchensync/meta/konnector-" + path;
00167     if (!QFile::exists( str ) ) {
00168         kdDebug(5228) << "Path does not exist ->First Sync" << endl;
00169     kdDebug(5228) << "Path was " << str << "  " << path << endl;
00170         syncee->setFirstSync( true );
00171         syncee->setSyncMode( Syncee::MetaMode );
00172         return;
00173     }
00174     syncee->setSyncMode( Syncee::MetaMode );
00175     KSimpleConfig conf( str );
00176 
00177     SyncEntry* entry;
00178     QString timestmp;
00179     QStringList ids;
00180 
00181     /* mod + added */
00182     for (entry= syncee->firstEntry(); entry; entry = syncee->nextEntry() ) {
00183         ids << entry->id();
00184     kdDebug(5228) << "Entry " << entry->name() << endl << "Entry id" << entry->id() << endl;
00185         if ( conf.hasGroup( entry->id() )  ) {
00186             conf.setGroup( entry->id() );
00187             timestmp = conf.readEntry("time");
00188         kdDebug(5228) << "Timestamp Old" << timestmp << endl;
00189         kdDebug(5228) << "Timestamp New" << entry->timestamp() << endl;
00190             if ( timestmp != entry->timestamp() )
00191                 entry->setState( SyncEntry::Modified );
00192         }
00193         /* added */
00194         else {
00195         kdDebug(5228) << "Entry added" << endl;
00196             entry->setState( SyncEntry::Added );
00197         }
00198     }
00199     /* find removed item... through reversed mapping */
00200     QStringList groups = conf.groupList();
00201     QStringList::Iterator it;
00202     for (it = groups.begin(); it != groups.end(); ++it ) {
00203         // removed items if ids is not present
00204         if (!ids.contains( (*it) ) ) {
00205         kdDebug(5228) << "Entry removed from abook" << (*it) << endl;
00206             KABC::Addressee adr;
00207             adr.setUid( (*it) );
00208             AddressBookSyncEntry* entry;
00209             entry = new AddressBookSyncEntry( adr );
00210             entry->setState( SyncEntry::Removed );
00211             syncee->addEntry( entry );
00212         }
00213     }
00214 }
00215 void AddressBookPart::writeMeta( KABC::AddressBook* book, const QString& path ) {
00216     /* no meta info to save */
00217     if (path.isEmpty() ) return;
00218 
00219     kdDebug(5228) << "WriteMeta AddressBookPart " << endl;
00220     QString str = QDir::homeDirPath();
00221     str += "/.kitchensync/meta/konnector-" + path;
00222     if (!QFile::exists( str ) ) {
00223         kdDebug(5228) << "Path does not exist " << endl;
00224     kdDebug(5228) << "Path = " << str << endl;
00225         KonnectorProfile kon = core()->currentKonnectorProfile();
00226         QDir dir;
00227         dir.mkdir( dir.homeDirPath() + "/.kitchensync");
00228         dir.mkdir( dir.homeDirPath() + "/.kitchensync/meta");
00229         dir.mkdir( dir.homeDirPath() + "/.kitchensync/meta/konnector-" + kon.uid() );
00230     kdDebug(5228) << "Kon UID " << kon.uid() << endl;
00231     }
00232     KSimpleConfig conf( str );
00233     QStringList grpList = conf.groupList();
00234     QStringList::Iterator it;
00235     for ( it = grpList.begin(); it != grpList.end(); ++it ) {
00236         conf.deleteGroup( (*it) );
00237     }
00238 
00239     KABC::AddressBook::Iterator aIt;
00240     for ( aIt = book->begin(); aIt != book->end(); ++aIt ) {
00241         kdDebug(5228) << "Name " << (*aIt).realName() << endl;
00242         kdDebug(5228) << "UID  " << (*aIt).uid() << endl;
00243         kdDebug(5228) << "Timestamp " << (*aIt).revision().toString() << endl;
00244 
00245         conf.setGroup( (*aIt).uid() );
00246         conf.writeEntry( "time", (*aIt).revision().toString() );
00247     }
00248 }
00249 void AddressBookPart::save( AddressBookSyncee* sync, const QString& meta) {
00250     AddressBookSyncEntry* entry;
00251     KABC::AddressBook* book;
00252 
00253 
00254     // save to the std. addressbook
00255     book = KABC::StdAddressBook::self();
00256     /* clear the old book first */
00257     book->clear();
00258 
00259     for ( entry = (AddressBookSyncEntry*)sync->firstEntry();
00260           entry;
00261           entry= (AddressBookSyncEntry*) sync->nextEntry() ) {
00262         if( entry->state() != SyncEntry::Removed ) {
00263             KABC::Addressee adr = entry->addressee();
00264             adr.setResource( resource(entry->resource() ) );
00265             book->insertAddressee( adr );
00266         }
00267     }
00268     KABC::StdAddressBook::save();
00269     kdDebug(5228) << "dumped abook " << endl;
00270     writeMeta( book, meta );
00271 
00272     KABC::StdAddressBook::close();
00273 }
00274 /*bool AddressBookPart::pathIsDefault( const QString& path ) {
00275     if ( path.isEmpty() ) return true;
00276     if ( path.stripWhiteSpace() == QString::fromLatin1("default") )
00277         return true;
00278 
00279     kdDebug(5228) << "Path is not default" << endl;
00280     return false;
00281     }*/
00282 AddressBookSyncee* AddressBookPart::book2syncee( KABC::AddressBook* book) {
00283     AddressBookSyncee* syncee = new AddressBookSyncee();
00284     AddressBookSyncEntry* entry=0l;
00285     KABC::AddressBook::Iterator it = book->begin();
00286     for ( ; it != book->end(); ++it ) {
00287         entry = new AddressBookSyncEntry( (*it) );
00288         QString res = (*it).resource() ? (*it).resource()->type() : QString::null;
00289         entry->setResource( res );
00290         syncee->addEntry( entry );
00291     }
00292     return syncee;
00293 }
00294 /*void AddressBookPart::saveAll( KABC::AddressBook* ab) {
00295     KABC::Resource *res = 0l;
00296     QPtrList<KABC::Resource> list = ab->resources();
00297     for (uint i = 0; i < list.count(); ++i ) {
00298         res = list.at( i );
00299         if (!res->readOnly() ) {
00300             KABC::Ticket* ticket = ab->requestSaveTicket( res );
00301             if (ticket)
00302                 ab->save( ticket );
00303         }
00304     }
00305     }*/
00306 
00307 KABC::Resource* AddressBookPart::resource( const QString& type ) {
00308     QPtrListIterator<KABC::Resource> it(KABC::StdAddressBook::self()->resources() );
00309     KABC::Resource* res = 0l;
00310     while ( (res = it.current()) ) {
00311         ++it;
00312         if ( res->type() == type )
00313             return res;
00314     }
00315     return 0;
00316 }
00317 
00318 #include "addressbookpart.moc"
KDE Logo
This file is part of the documentation for kitchensync Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Aug 23 18:20:08 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003