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