kitchensync

synchistory.h

00001 /*
00002     This file is part of KitchenSync.
00003 
00004     Copyright (c) 2002,2003,2004 Holger Freyther <freyther@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019     Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #ifndef KSYNC_SYNC_HISTORY_H
00023 #define KSYNC_SYNC_HISTORY_H
00024 
00025 #include <qmap.h>
00026 
00027 #include <addressbooksyncee.h>
00028 #include <bookmarksyncee.h>
00029 #include <calendarsyncee.h>
00030 #include <kdepimmacros.h>
00031 
00032 class KConfig;
00033 
00034 namespace KSync {
00035 
00040 class KDE_EXPORT SyncHistoryMap 
00041 {
00042 public:
00043   typedef QMap<QString, QString> Map;
00044   typedef QMap<QString, QString>::Iterator Iterator;
00045   SyncHistoryMap( const QString& fileName = QString::null );
00046   virtual ~SyncHistoryMap();
00047   
00048   void setFileName( const QString& fileName );
00049   QString fileName()const;
00050   
00051   void load();
00052   void save();
00053   
00054   QString  text( const QString& id_key )const;
00055   bool contains( const QString& id_key )const;
00056   void insert(   const QString& id_key, const QString& text_data );
00057   void set( const SyncHistoryMap::Map& );
00058   
00059   SyncHistoryMap::Map map()const;
00060   
00061   void clear();
00062   
00063 protected:
00064   KConfig* config();
00065 private:
00066   Map mMap;
00067   QString mFile;
00068   KConfig* mConf;
00069 };
00070 
00083 template <class Syn, class Ent>
00084 class SyncHistory 
00085 {
00086 public:
00087   SyncHistory( Syn*, const QString& file );
00088   virtual ~SyncHistory();
00089 
00090   void save ( );
00091   void load ( );
00092 protected:
00093   virtual void      save( SyncHistoryMap* );
00094   virtual SyncHistoryMap*  load( const QString& );
00095   virtual QString string( Ent * );
00096   
00097 private:
00098   SyncHistoryMap* loadAndClear();
00099   SyncHistoryMap* loadInternal();
00100   SyncHistoryMap *mMap;
00101   QString mFile;
00102   Syn *mSyncee;
00103 };
00104 
00105 
00106 typedef SyncHistory<KSync::CalendarSyncee,    KSync::CalendarSyncEntry   > CalendarSyncHistory;
00107 typedef SyncHistory<KSync::AddressBookSyncee, KSync::AddressBookSyncEntry> AddressBookSyncHistory;
00108 typedef SyncHistory<KSync::BookmarkSyncee,    KSync::BookmarkSyncEntry   > BookmarkSyncHistory;
00109 
00110 
00120 template <class Sync, class Ent>
00121 SyncHistory<Sync, Ent>::SyncHistory( Sync* sy, const QString& file )
00122   : mMap( 0l ), mFile( file ), mSyncee( sy )
00123 {}
00124 
00125 
00129 template <class Sync, class Ent>
00130 SyncHistory<Sync, Ent>::~SyncHistory() {
00131   delete mMap;
00132 }
00133 
00134 
00140 template <class Sync, class Ent>
00141 void SyncHistory<Sync, Ent>::save() 
00142 {
00143   mMap = loadAndClear();
00144   
00145   /* update the state */
00146   for ( Ent* entry = (Ent*)mSyncee->firstEntry();
00147         entry != 0; entry = (Ent*)mSyncee->nextEntry() ) {
00148     
00149     /* only save meta for not deleted SyncEntries! */
00150     if ( entry->state() != SyncEntry::Removed )
00151       mMap->insert( entry->id(), string( entry ) );
00152     
00153   }
00154   
00155   save( mMap );
00156 }
00157 
00164 template <class Sync, class Ent>
00165 void SyncHistory<Sync, Ent>::load() 
00166 {
00167   mMap = loadInternal();
00168   
00169   bool found;
00170   Ent* entryNew;
00171   
00172   /*
00173    * Now we'll search for some meta info
00174    * go through all entries
00175    * check if they exist
00176    * if exist check if modified
00177    * else it was added
00178    */
00179   for ( entryNew = static_cast<Ent*>(mSyncee->firstEntry());
00180         entryNew != 0;
00181         entryNew = static_cast<Ent*>(mSyncee->nextEntry()) ) {
00182     found = false;
00183     
00184     /*
00185      * check if the Map contains the UID
00186      * if the string sums are not equal
00187      * set the modified state
00188      * ADDED set Added state
00189      */
00190     if ( mMap->contains( entryNew->id() ) ) {
00191       found = true;
00192       QString str = mMap->text( entryNew->id() );
00193       QString newStr = string( entryNew );
00194       
00195       if ( str != newStr)
00196         entryNew->setState( SyncEntry::Modified );
00197       
00198     }
00199     if (!found )
00200       entryNew->setState( SyncEntry::Added );
00201     
00202   }
00203   
00204   /*
00205    * Now find the deleted records
00206    */
00207   SyncHistoryMap::Map::Iterator it;
00208   SyncHistoryMap::Map ma = mMap->map();
00209   for ( it = ma.begin(); it != ma.end(); ++it ) {
00210     entryNew = static_cast<Ent*>( mSyncee->findEntry( it.key() ) );
00211     
00218     if (!entryNew) {
00219       entryNew = new Ent(mSyncee);
00220       entryNew->setId( it.key() );
00221       
00222       kdDebug() << "FOUND deleted record of type " << entryNew->type() << " and ids are " << it.key() << " and " << entryNew->id() << endl;
00223             /* add entry first and then to setState */
00224       entryNew->setState( KSync::SyncEntry::Removed );
00225       mSyncee->addEntry( entryNew );
00226     }
00227   }
00228 }
00229 
00234 template <class Sync, class Ent>
00235 void SyncHistory<Sync, Ent>::save( SyncHistoryMap* m) 
00236 {
00237   m->save();
00238 }
00239 
00245 template <class Sync, class Ent>
00246 SyncHistoryMap* SyncHistory<Sync, Ent>::load( const QString& f) 
00247 {
00248   SyncHistoryMap *map  = new SyncHistoryMap( f );
00249   
00250   map->load();
00251   return map;
00252 }
00253 
00254 
00260 template <class Sync, class Ent>
00261 QString SyncHistory<Sync, Ent>::string( Ent* ent) 
00262 {
00263   return ent->timestamp();
00264 }
00265 
00266 
00270 template <class Sync, class Ent>
00271 SyncHistoryMap* SyncHistory<Sync, Ent>::loadAndClear() 
00272 {
00273   if(!mMap )
00274     mMap = load( mFile );
00275   mMap->clear();
00276   
00277   return mMap;
00278 }
00279 
00283 template <class Sync, class Ent>
00284 SyncHistoryMap* SyncHistory<Sync, Ent>::loadInternal() 
00285 {
00286   if(!mMap )
00287     mMap = load( mFile );
00288   
00289   return mMap;
00290 }
00291 
00292 }
00293 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys