kitchensync

calendarsyncee.cpp

00001 /*
00002     This file is part of KitchenSync.
00003 
00004     Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org>
00005     Copyright (c) 2004 Holger Hans Peter Freyther <freyther@kde.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 "calendarsyncee.h"
00024 #include "calendarmerger.h"
00025 
00026 #include <libkcal/filestorage.h>
00027 #include <libkcal/calformat.h>
00028 #include <libkdepim/calendardiffalgo.h>
00029 
00030 #include <kdebug.h>
00031 
00032 
00033 using namespace KSync;
00034 using namespace KCal;
00035 
00036 CalendarSyncEntry::CalendarSyncEntry( Incidence *incidence, Syncee *parent )
00037   : SyncEntry( parent ), mIncidence( incidence )
00038 {
00039   setType( QString::fromLatin1( "CalendarSyncEntry" ) );
00040 }
00041 
00042 CalendarSyncEntry::CalendarSyncEntry( Syncee* parent )
00043   : SyncEntry( parent )
00044 {
00045   /* that is hard, use todo or calendar as the default? */
00046   mIncidence = new KCal::Todo;
00047   setType( QString::fromLatin1( "CalendarSyncEntry" ) );
00048 }
00049 
00050 
00051 CalendarSyncEntry::CalendarSyncEntry( const CalendarSyncEntry& ent )
00052   : SyncEntry( ent ), mIncidence( ent.mIncidence->clone() )
00053 {}
00054 
00055 QString CalendarSyncEntry::name()
00056 {
00057   return mIncidence->summary();
00058 }
00059 
00060 QString CalendarSyncEntry::id()
00061 {
00062   return mIncidence->uid();
00063 }
00064 
00065 void CalendarSyncEntry::setId(const QString& id)
00066 {
00067   mIncidence->setUid( id );
00068 }
00069 
00070 QString CalendarSyncEntry::timestamp()
00071 {
00072   // FIXME: last modified isn't sufficient to tell if an event has changed.
00073   return mIncidence->lastModified().toString();
00074 }
00075 
00076 KCal::Incidence *CalendarSyncEntry::incidence()const {
00077     return mIncidence;
00078 }
00079 
00080 bool CalendarSyncEntry::equals( SyncEntry *entry )
00081 {
00082   CalendarSyncEntry *calEntry = dynamic_cast<CalendarSyncEntry *>(entry);
00083   if (!calEntry) {
00084     kdDebug() << "CalendarSyncee::addEntry(): Wrong type." << endl;
00085     return false;
00086   }
00087 
00088   kdDebug() << "UID: " << mIncidence->uid() << " <-> "
00089             << calEntry->incidence()->uid() << endl;
00090   kdDebug() << "LAM: " << mIncidence->lastModified().toTime_t() << " <-> "
00091             << calEntry->incidence()->lastModified().toTime_t() << endl;
00092 
00093   if ( mIncidence->uid() != calEntry->incidence()->uid() ) {
00094     kdDebug() << "UID unequal" << endl;
00095     return false;
00096   }
00097   if ( mIncidence->lastModified() != calEntry->incidence()->lastModified() ) {
00098     kdDebug() << "LAM unequal" << endl;
00099     return false;
00100   }
00101 
00102   if ( *mIncidence == *( calEntry->incidence() ) ) return true;
00103 
00104   return false;
00105 }
00106 
00107 CalendarSyncEntry *CalendarSyncEntry::clone()
00108 {
00109   return new CalendarSyncEntry( *this );
00110 }
00111 
00112 KPIM::DiffAlgo* CalendarSyncEntry::diffAlgo( SyncEntry *syncEntry, SyncEntry *targetEntry )
00113 {
00114   CalendarSyncEntry *calSyncEntry = dynamic_cast<CalendarSyncEntry*>( syncEntry );
00115   CalendarSyncEntry *calTargetEntry = dynamic_cast<CalendarSyncEntry*>( targetEntry );
00116 
00117   if ( !calSyncEntry || !calTargetEntry )
00118     return 0;
00119 
00120   return new KPIM::CalendarDiffAlgo( calSyncEntry->incidence(), calTargetEntry->incidence() );
00121 }
00122 
00123 
00125 CalendarSyncee::CalendarSyncee( Calendar *calendar, CalendarMerger* merger )
00126     : Syncee( merger ), mIteratingEvents( true )
00127 {
00128   setType( QString::fromLatin1( "CalendarSyncee" ) );
00129   mCalendar = calendar;
00130 }
00131 
00132 CalendarSyncee::~CalendarSyncee()
00133 {
00134   clearEntries();
00135 }
00136 
00137 void CalendarSyncee::reset()
00138 {
00139   clearEntries();
00140 }
00141 
00142 void CalendarSyncee::clearEntries()
00143 {
00144   QMap<Incidence *, CalendarSyncEntry *>::Iterator it;
00145   for( it = mEntries.begin(); it != mEntries.end(); ++it ) {
00146     delete it.data();
00147   }
00148   mEntries.clear();
00149 }
00150 
00151 CalendarSyncEntry *CalendarSyncee::firstEntry()
00152 {
00153   mEvents = mCalendar->events();
00154   mCurrentEvent = mEvents.begin();
00155   mIteratingEvents = true;
00156   if( mCurrentEvent == mEvents.end() ) {
00157     mTodos = mCalendar->todos();
00158     mCurrentTodo = mTodos.begin();
00159     mIteratingEvents = false;
00160     if( mCurrentTodo == mTodos.end() ) {
00161       return 0;
00162     }
00163     return createEntry( *mCurrentTodo );
00164   }
00165 
00166   return createEntry( *mCurrentEvent );
00167 }
00168 
00169 CalendarSyncEntry *CalendarSyncee::nextEntry()
00170 {
00171   if( mIteratingEvents ) {
00172     ++mCurrentEvent;
00173     if ( mCurrentEvent == mEvents.end() ) {
00174       mTodos = mCalendar->todos();
00175       mCurrentTodo = mTodos.begin();
00176       mIteratingEvents = false;
00177       if( mCurrentTodo == mTodos.end() ) {
00178           return 0;
00179       }
00180       return createEntry( *mCurrentTodo );
00181     }
00182     return createEntry( *mCurrentEvent );
00183   } else {
00184     ++mCurrentTodo;
00185     if( mCurrentTodo == mTodos.end() ) {
00186       return 0;
00187     }
00188     return createEntry( *mCurrentTodo );
00189   }
00190 }
00191 
00192 
00193 void CalendarSyncee::addEntry( SyncEntry *entry )
00194 {
00195   CalendarSyncEntry *calEntry = dynamic_cast<CalendarSyncEntry *>(entry);
00196   if (calEntry) {
00197     Event *sourceEvent = dynamic_cast<Event *>(calEntry->incidence());
00198     if (!sourceEvent) {
00199         Todo *sourceTodo = dynamic_cast<Todo*>(calEntry->incidence());
00200         if(!sourceTodo) {
00201            kdDebug() << "CalendarSyncee::addEntry(): Incidence is not of type Event or Todo."
00202                 << endl;
00203         }
00204         Todo *todo = dynamic_cast<Todo *>(sourceTodo);
00205         mCalendar->addTodo(todo);
00206     } else {
00207       Event *event = dynamic_cast<Event *>(sourceEvent);
00208       mCalendar->addEvent(event);
00209     }
00210     /* do not lose the syncStatus and insert the Entry directly */
00211     calEntry->setSyncee( this );
00212     mEntries.insert(calEntry->incidence(), calEntry);
00213   }
00214 }
00215 
00216 void CalendarSyncee::removeEntry( SyncEntry *entry )
00217 {
00218   CalendarSyncEntry *calEntry = dynamic_cast<CalendarSyncEntry *>( entry );
00219   if ( calEntry ) {
00220     Event *ev = dynamic_cast<Event *>( calEntry->incidence() );
00221     if ( ev ) {
00222       mCalendar->deleteEvent( ev );
00223     } else {
00224       Todo *td = dynamic_cast<Todo*>( calEntry->incidence() );
00225       if( !td ) {
00226         kdDebug() << "CalendarSyncee::removeEntry(): Incidence has wrong type."
00227                   << endl;
00228       }
00229       mCalendar->deleteTodo( td );
00230     }
00231     calEntry->setSyncee( 0l );
00232     mEntries.remove( calEntry->incidence() );
00233   }
00234 }
00235 
00236 CalendarSyncEntry *CalendarSyncee::createEntry( Incidence *incidence )
00237 {
00238   if ( incidence ) {
00239     QMap<Incidence *,CalendarSyncEntry *>::ConstIterator it;
00240     it = mEntries.find( incidence );
00241     if ( it != mEntries.end() ) return it.data();
00242 
00243     CalendarSyncEntry *entry = new CalendarSyncEntry( incidence, this );
00244     mEntries.insert( incidence, entry );
00245     return entry;
00246   } else {
00247     return 0;
00248   }
00249 }
00250 
00251 bool CalendarSyncee::writeBackup( const QString &filename )
00252 {
00253   KCal::FileStorage storage( mCalendar, filename );
00254 
00255   bool ok = true;
00256   ok = ok && storage.open();
00257   ok = ok && storage.save();
00258   ok = ok && storage.close();
00259 
00260   return ok;
00261 }
00262 
00263 bool CalendarSyncee::restoreBackup( const QString &filename )
00264 {
00265   mCalendar->close();
00266 
00267   KCal::FileStorage storage( mCalendar, filename );
00268 
00269   bool ok = true;
00270   ok = ok && storage.open();
00271   ok = ok && storage.load();
00272   ok = ok && storage.close();
00273 
00274   clearEntries();
00275 
00276   return ok;
00277 }
00278 
00279 QString CalendarSyncee::generateNewId() const {
00280     return KCal::CalFormat::createUniqueId();
00281 }
KDE Home | KDE Accessibility Home | Description of Access Keys