kitchensync
synchistory.h00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00146 for ( Ent* entry = (Ent*)mSyncee->firstEntry();
00147 entry != 0; entry = (Ent*)mSyncee->nextEntry() ) {
00148
00149
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
00174
00175
00176
00177
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
00186
00187
00188
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
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
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
|