kitchensync Library API Documentation

calendarsyncee.cpp

00001 /* 00002 This file is part of KitchenSync. 00003 00004 Copyright (c) 2002 Cornelius Schumacher <schumacher@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., 59 Temple Place - Suite 330, 00019 Boston, MA 02111-1307, USA. 00020 */ 00021 00022 #include <kdebug.h> 00023 #include <libkcal/filestorage.h> 00024 #include <libkdepim/calendardiffalgo.h> 00025 00026 #include "calendarsyncee.h" 00027 00028 using namespace KSync; 00029 using namespace KCal; 00030 00031 CalendarSyncEntry::CalendarSyncEntry( Incidence *incidence, Syncee *parent ) 00032 : SyncEntry( parent ), mIncidence( incidence ) 00033 { 00034 } 00035 00036 QString CalendarSyncEntry::name() 00037 { 00038 return mIncidence->summary(); 00039 } 00040 00041 QString CalendarSyncEntry::id() 00042 { 00043 return mIncidence->uid(); 00044 } 00045 00046 QString CalendarSyncEntry::timestamp() 00047 { 00048 // FIXME: last modified isn't sufficient to tell if an event has changed. 00049 return mIncidence->lastModified().toString(); 00050 } 00051 00052 bool CalendarSyncEntry::equals( SyncEntry *entry ) 00053 { 00054 CalendarSyncEntry *calEntry = dynamic_cast<CalendarSyncEntry *>(entry); 00055 if (!calEntry) { 00056 kdDebug() << "CalendarSyncee::addEntry(): Wrong type." << endl; 00057 return false; 00058 } 00059 00060 kdDebug() << "UID: " << mIncidence->uid() << " <-> " 00061 << calEntry->incidence()->uid() << endl; 00062 kdDebug() << "LAM: " << mIncidence->lastModified().toTime_t() << " <-> " 00063 << calEntry->incidence()->lastModified().toTime_t() << endl; 00064 00065 if ( mIncidence->uid() != calEntry->incidence()->uid() ) { 00066 kdDebug() << "UID unequal" << endl; 00067 return false; 00068 } 00069 if ( mIncidence->lastModified() != calEntry->incidence()->lastModified() ) { 00070 kdDebug() << "LAM unequal" << endl; 00071 return false; 00072 } 00073 00074 if ( *mIncidence == *( calEntry->incidence() ) ) return true; 00075 00076 return false; 00077 } 00078 00079 CalendarSyncEntry *CalendarSyncEntry::clone() 00080 { 00081 return new CalendarSyncEntry( *this ); 00082 } 00083 00084 KPIM::DiffAlgo* CalendarSyncEntry::diffAlgo( SyncEntry *syncEntry, SyncEntry *targetEntry ) 00085 { 00086 CalendarSyncEntry *calSyncEntry = dynamic_cast<CalendarSyncEntry*>( syncEntry ); 00087 CalendarSyncEntry *calTargetEntry = dynamic_cast<CalendarSyncEntry*>( targetEntry ); 00088 00089 if ( !calSyncEntry || !calTargetEntry ) 00090 return 0; 00091 00092 return new KPIM::CalendarDiffAlgo( calSyncEntry->incidence(), calTargetEntry->incidence() ); 00093 } 00094 00095 CalendarSyncee::CalendarSyncee( Calendar *calendar ) 00096 : mIteratingEvents( true ) 00097 { 00098 mCalendar = calendar; 00099 } 00100 00101 CalendarSyncee::~CalendarSyncee() 00102 { 00103 clearEntries(); 00104 } 00105 00106 void CalendarSyncee::reset() 00107 { 00108 clearEntries(); 00109 } 00110 00111 void CalendarSyncee::clearEntries() 00112 { 00113 QMap<Incidence *, CalendarSyncEntry *>::Iterator it; 00114 for( it = mEntries.begin(); it != mEntries.end(); ++it ) { 00115 delete it.data(); 00116 } 00117 mEntries.clear(); 00118 } 00119 00120 CalendarSyncEntry *CalendarSyncee::firstEntry() 00121 { 00122 mEvents = mCalendar->events(); 00123 mCurrentEvent = mEvents.begin(); 00124 mIteratingEvents = true; 00125 if( mCurrentEvent == mEvents.end() ) { 00126 mTodos = mCalendar->todos(); 00127 mCurrentTodo = mTodos.begin(); 00128 mIteratingEvents = false; 00129 if( mCurrentTodo == mTodos.end() ) { 00130 return 0; 00131 } 00132 return createEntry( *mCurrentTodo ); 00133 } 00134 return createEntry( *mCurrentEvent ); 00135 } 00136 00137 CalendarSyncEntry *CalendarSyncee::nextEntry() 00138 { 00139 if( mIteratingEvents ) { 00140 ++mCurrentEvent; 00141 if ( mCurrentEvent == mEvents.end() ) { 00142 mTodos = mCalendar->todos(); 00143 mCurrentTodo = mTodos.begin(); 00144 mIteratingEvents = false; 00145 if( mCurrentTodo == mTodos.end() ) { 00146 return 0; 00147 } 00148 return createEntry( *mCurrentTodo ); 00149 } 00150 return createEntry( *mCurrentEvent ); 00151 } else { 00152 ++mCurrentTodo; 00153 if( mCurrentTodo == mTodos.end() ) { 00154 return 0; 00155 } 00156 return createEntry( *mCurrentTodo ); 00157 } 00158 } 00159 00160 #if 0 00161 CalendarSyncEntry *CalendarSyncee::findEntry(const QString &id) 00162 { 00163 Event *event = mCalendar->getEvent(id); 00164 return createEntry(event); 00165 } 00166 #endif 00167 00168 void CalendarSyncee::addEntry( SyncEntry *entry ) 00169 { 00170 CalendarSyncEntry *calEntry = dynamic_cast<CalendarSyncEntry *>(entry); 00171 if (!calEntry) { 00172 kdDebug() << "CalendarSyncee::addEntry(): SyncEntry has wrong type." 00173 << endl; 00174 } else { 00175 Event *sourceEvent = dynamic_cast<Event *>(calEntry->incidence()); 00176 if (!sourceEvent) { 00177 Todo *sourceTodo = dynamic_cast<Todo*>(calEntry->incidence()); 00178 if(!sourceTodo) { 00179 kdDebug() << "CalendarSyncee::addEntry(): Incidence is not of type Event or Todo." 00180 << endl; 00181 } 00182 Todo *todo = dynamic_cast<Todo *>(sourceTodo->clone()); 00183 mCalendar->addTodo(todo); 00184 } else { 00185 Event *event = dynamic_cast<Event *>(sourceEvent->clone()); 00186 mCalendar->addEvent(event); 00187 } 00188 } 00189 } 00190 00191 void CalendarSyncee::removeEntry( SyncEntry *entry ) 00192 { 00193 CalendarSyncEntry *calEntry = dynamic_cast<CalendarSyncEntry *>( entry ); 00194 if ( !calEntry ) { 00195 kdDebug() << "CalendarSyncee::removeEntry(): SyncEntry has wrong type." 00196 << endl; 00197 } else { 00198 Event *ev = dynamic_cast<Event *>( calEntry->incidence() ); 00199 if ( ev ) { 00200 mCalendar->deleteEvent( ev ); 00201 } else { 00202 Todo *td = dynamic_cast<Todo*>( calEntry->incidence() ); 00203 if( !td ) { 00204 kdDebug() << "CalendarSyncee::removeEntry(): Incidence has wrong type." 00205 << endl; 00206 } 00207 mCalendar->deleteTodo( td ); 00208 } 00209 } 00210 } 00211 00212 CalendarSyncEntry *CalendarSyncee::createEntry( Incidence *incidence ) 00213 { 00214 if ( incidence ) { 00215 QMap<Incidence *,CalendarSyncEntry *>::ConstIterator it; 00216 it = mEntries.find( incidence ); 00217 if ( it != mEntries.end() ) return it.data(); 00218 00219 CalendarSyncEntry *entry = new CalendarSyncEntry( incidence, this ); 00220 mEntries.insert( incidence, entry ); 00221 return entry; 00222 } else { 00223 return 0; 00224 } 00225 } 00226 00227 bool CalendarSyncee::writeBackup( const QString &filename ) 00228 { 00229 KCal::FileStorage storage( mCalendar, filename ); 00230 00231 bool ok = true; 00232 ok = ok && storage.open(); 00233 ok = ok && storage.save(); 00234 ok = ok && storage.close(); 00235 00236 return ok; 00237 } 00238 00239 bool CalendarSyncee::restoreBackup( const QString &filename ) 00240 { 00241 mCalendar->close(); 00242 00243 KCal::FileStorage storage( mCalendar, filename ); 00244 00245 bool ok = true; 00246 ok = ok && storage.open(); 00247 ok = ok && storage.load(); 00248 ok = ok && storage.close(); 00249 00250 clearEntries(); 00251 00252 return ok; 00253 }
KDE Logo
This file is part of the documentation for kitchensync Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Aug 27 12:50:47 2004 by doxygen 1.3.8 written by Dimitri van Heesch, © 1997-2003