calendardiffalgo.cpp
00001 /* 00002 This file is part of libtdepim. 00003 00004 Copyright (c) 2004 Tobias Koenig <tokoe@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 #include <tdelocale.h> 00023 00024 #include <libkcal/kcalversion.h> 00025 00026 #include "calendardiffalgo.h" 00027 00028 using namespace KSync; 00029 00030 #ifndef KDE_USE_FINAL 00031 static bool compareString( const TQString &left, const TQString &right ) 00032 { 00033 if ( left.isEmpty() && right.isEmpty() ) 00034 return true; 00035 else 00036 return left == right; 00037 } 00038 #endif 00039 00040 static TQString toString( KCal::Attendee *attendee ) 00041 { 00042 return attendee->name() + "<" + attendee->email() + ">"; 00043 } 00044 00045 static TQString toString( KCal::Alarm * ) 00046 { 00047 return TQString(); 00048 } 00049 00050 static TQString toString( KCal::Incidence * ) 00051 { 00052 return TQString(); 00053 } 00054 00055 static TQString toString( KCal::Attachment * ) 00056 { 00057 return TQString(); 00058 } 00059 00060 static TQString toString( const TQDate &date ) 00061 { 00062 return date.toString(); 00063 } 00064 00065 static TQString toString( const TQDateTime &dateTime ) 00066 { 00067 return dateTime.toString(); 00068 } 00069 00070 static TQString toString( const TQString str ) 00071 { 00072 return str; 00073 } 00074 00075 static TQString toString( bool value ) 00076 { 00077 if ( value ) 00078 return i18n( "Yes" ); 00079 else 00080 return i18n( "No" ); 00081 } 00082 00083 CalendarDiffAlgo::CalendarDiffAlgo( KCal::Incidence *leftIncidence, 00084 KCal::Incidence *rightIncidence ) 00085 : mLeftIncidence( leftIncidence ), mRightIncidence( rightIncidence ) 00086 { 00087 } 00088 00089 void CalendarDiffAlgo::run() 00090 { 00091 begin(); 00092 00093 diffIncidenceBase( mLeftIncidence, mRightIncidence ); 00094 diffIncidence( mLeftIncidence, mRightIncidence ); 00095 00096 KCal::Event *leftEvent = dynamic_cast<KCal::Event*>( mLeftIncidence ); 00097 KCal::Event *rightEvent = dynamic_cast<KCal::Event*>( mRightIncidence ); 00098 if ( leftEvent && rightEvent ) { 00099 diffEvent( leftEvent, rightEvent ); 00100 } else { 00101 KCal::Todo *leftTodo = dynamic_cast<KCal::Todo*>( mLeftIncidence ); 00102 KCal::Todo *rightTodo = dynamic_cast<KCal::Todo*>( mRightIncidence ); 00103 if ( leftTodo && rightTodo ) { 00104 diffTodo( leftTodo, rightTodo ); 00105 } 00106 } 00107 00108 end(); 00109 } 00110 00111 void CalendarDiffAlgo::diffIncidenceBase( KCal::IncidenceBase *left, KCal::IncidenceBase *right ) 00112 { 00113 diffList( i18n( "Attendees" ), left->attendees(), right->attendees() ); 00114 00115 if ( left->dtStart() != right->dtStart() ) 00116 conflictField( i18n( "Start time" ), left->dtStartStr(), right->dtStartStr() ); 00117 00118 if ( !compareString( left->organizer().fullName(), right->organizer().fullName() ) ) 00119 conflictField( i18n( "Organizer" ), left->organizer().fullName(), right->organizer().fullName() ); 00120 00121 if ( !compareString( left->uid(), right->uid() ) ) 00122 conflictField( i18n( "UID" ), left->uid(), right->uid() ); 00123 00124 if ( left->doesFloat() != right->doesFloat() ) 00125 conflictField( i18n( "Is floating" ), toString( left->doesFloat() ), toString( right->doesFloat() ) ); 00126 00127 if ( left->hasDuration() != right->hasDuration() ) 00128 conflictField( i18n( "Has duration" ), toString( left->hasDuration() ), toString( right->hasDuration() ) ); 00129 00130 if ( left->duration() != right->duration() ) 00131 conflictField( i18n( "Duration" ), TQString::number( left->duration() ), TQString::number( right->duration() ) ); 00132 } 00133 00134 void CalendarDiffAlgo::diffIncidence( KCal::Incidence *left, KCal::Incidence *right ) 00135 { 00136 if ( !compareString( left->description(), right->description() ) ) 00137 conflictField( i18n( "Description" ), left->description(), right->description() ); 00138 00139 if ( !compareString( left->summary(), right->summary() ) ) 00140 conflictField( i18n( "Summary" ), left->summary(), right->summary() ); 00141 00142 if ( left->status() != right->status() ) 00143 conflictField( i18n( "Status" ), left->statusStr(), right->statusStr() ); 00144 00145 if ( left->secrecy() != right->secrecy() ) 00146 conflictField( i18n( "Secrecy" ), toString( left->secrecy() ), toString( right->secrecy() ) ); 00147 00148 if ( left->priority() != right->priority() ) 00149 conflictField( i18n( "Priority" ), toString( left->priority() ), toString( right->priority() ) ); 00150 00151 if ( !compareString( left->location(), right->location() ) ) 00152 conflictField( i18n( "Location" ), left->location(), right->location() ); 00153 00154 diffList( i18n( "Categories" ), left->categories(), right->categories() ); 00155 diffList( i18n( "Alarms" ), left->alarms(), right->alarms() ); 00156 diffList( i18n( "Resources" ), left->resources(), right->resources() ); 00157 diffList( i18n( "Relations" ), left->relations(), right->relations() ); 00158 diffList( i18n( "Attachments" ), left->attachments(), right->attachments() ); 00159 #if LIBKCAL_IS_VERSION( 1, 3, 1 ) 00160 diffList( i18n( "Exception Dates" ), left->recurrence()->exDates(), right->recurrence()->exDates() ); 00161 diffList( i18n( "Exception Times" ), left->recurrence()->exDateTimes(), right->recurrence()->exDateTimes() ); 00162 #endif 00163 // TODO: recurrence dates and date/times, exrules, rrules 00164 00165 if ( left->created() != right->created() ) 00166 conflictField( i18n( "Created" ), left->created().toString(), right->created().toString() ); 00167 00168 if ( !compareString( left->relatedToUid(), right->relatedToUid() ) ) 00169 conflictField( i18n( "Related Uid" ), left->relatedToUid(), right->relatedToUid() ); 00170 } 00171 00172 void CalendarDiffAlgo::diffEvent( KCal::Event *left, KCal::Event *right ) 00173 { 00174 if ( left->hasEndDate() != right->hasEndDate() ) 00175 conflictField( i18n( "Has End Date" ), toString( left->hasEndDate() ), toString( right->hasEndDate() ) ); 00176 00177 if ( left->dtEnd() != right->dtEnd() ) 00178 conflictField( i18n( "End Date" ), left->dtEndStr(), right->dtEndStr() ); 00179 00180 // TODO: check transparency 00181 } 00182 00183 void CalendarDiffAlgo::diffTodo( KCal::Todo *left, KCal::Todo *right ) 00184 { 00185 if ( left->hasStartDate() != right->hasStartDate() ) 00186 conflictField( i18n( "Has Start Date" ), toString( left->hasStartDate() ), toString( right->hasStartDate() ) ); 00187 00188 if ( left->hasDueDate() != right->hasDueDate() ) 00189 conflictField( i18n( "Has Due Date" ), toString( left->hasDueDate() ), toString( right->hasDueDate() ) ); 00190 00191 if ( left->dtDue() != right->dtDue() ) 00192 conflictField( i18n( "Due Date" ), left->dtDue().toString(), right->dtDue().toString() ); 00193 00194 if ( left->hasCompletedDate() != right->hasCompletedDate() ) 00195 conflictField( i18n( "Has Complete Date" ), toString( left->hasCompletedDate() ), toString( right->hasCompletedDate() ) ); 00196 00197 if ( left->percentComplete() != right->percentComplete() ) 00198 conflictField( i18n( "Complete" ), TQString::number( left->percentComplete() ), TQString::number( right->percentComplete() ) ); 00199 00200 if ( left->completed() != right->completed() ) 00201 conflictField( i18n( "Completed" ), toString( left->completed() ), toString( right->completed() ) ); 00202 } 00203 00204 template <class L> 00205 void CalendarDiffAlgo::diffList( const TQString &id, 00206 const TQValueList<L> &left, const TQValueList<L> &right ) 00207 { 00208 for ( uint i = 0; i < left.count(); ++i ) { 00209 if ( right.find( left[ i ] ) == right.end() ) 00210 additionalLeftField( id, toString( left[ i ] ) ); 00211 } 00212 00213 for ( uint i = 0; i < right.count(); ++i ) { 00214 if ( left.find( right[ i ] ) == left.end() ) 00215 additionalRightField( id, toString( right[ i ] ) ); 00216 } 00217 }