kitchensync

calendardiffalgo.cpp
1 /*
2  This file is part of libtdepim.
3 
4  Copyright (c) 2004 Tobias Koenig <tokoe@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 #include <tdelocale.h>
23 
24 #include <libkcal/kcalversion.h>
25 
26 #include "calendardiffalgo.h"
27 
28 using namespace KSync;
29 
30 #ifndef KDE_USE_FINAL
31 static bool compareString( const TQString &left, const TQString &right )
32 {
33  if ( left.isEmpty() && right.isEmpty() )
34  return true;
35  else
36  return left == right;
37 }
38 #endif
39 
40 static TQString toString( KCal::Attendee *attendee )
41 {
42  return attendee->name() + "<" + attendee->email() + ">";
43 }
44 
45 static TQString toString( KCal::Alarm * )
46 {
47  return TQString();
48 }
49 
50 static TQString toString( KCal::Incidence * )
51 {
52  return TQString();
53 }
54 
55 static TQString toString( KCal::Attachment * )
56 {
57  return TQString();
58 }
59 
60 static TQString toString( const TQDate &date )
61 {
62  return date.toString();
63 }
64 
65 static TQString toString( const TQDateTime &dateTime )
66 {
67  return dateTime.toString();
68 }
69 
70 static TQString toString( const TQString str )
71 {
72  return str;
73 }
74 
75 static TQString toString( bool value )
76 {
77  if ( value )
78  return i18n( "Yes" );
79  else
80  return i18n( "No" );
81 }
82 
83 CalendarDiffAlgo::CalendarDiffAlgo( KCal::Incidence *leftIncidence,
84  KCal::Incidence *rightIncidence )
85  : mLeftIncidence( leftIncidence ), mRightIncidence( rightIncidence )
86 {
87 }
88 
89 void CalendarDiffAlgo::run()
90 {
91  begin();
92 
93  diffIncidenceBase( mLeftIncidence, mRightIncidence );
94  diffIncidence( mLeftIncidence, mRightIncidence );
95 
96  KCal::Event *leftEvent = dynamic_cast<KCal::Event*>( mLeftIncidence );
97  KCal::Event *rightEvent = dynamic_cast<KCal::Event*>( mRightIncidence );
98  if ( leftEvent && rightEvent ) {
99  diffEvent( leftEvent, rightEvent );
100  } else {
101  KCal::Todo *leftTodo = dynamic_cast<KCal::Todo*>( mLeftIncidence );
102  KCal::Todo *rightTodo = dynamic_cast<KCal::Todo*>( mRightIncidence );
103  if ( leftTodo && rightTodo ) {
104  diffTodo( leftTodo, rightTodo );
105  }
106  }
107 
108  end();
109 }
110 
111 void CalendarDiffAlgo::diffIncidenceBase( KCal::IncidenceBase *left, KCal::IncidenceBase *right )
112 {
113  diffList( i18n( "Attendees" ), left->attendees(), right->attendees() );
114 
115  if ( left->dtStart() != right->dtStart() )
116  conflictField( i18n( "Start time" ), left->dtStartStr(), right->dtStartStr() );
117 
118  if ( !compareString( left->organizer().fullName(), right->organizer().fullName() ) )
119  conflictField( i18n( "Organizer" ), left->organizer().fullName(), right->organizer().fullName() );
120 
121  if ( !compareString( left->uid(), right->uid() ) )
122  conflictField( i18n( "UID" ), left->uid(), right->uid() );
123 
124  if ( left->doesFloat() != right->doesFloat() )
125  conflictField( i18n( "Is floating" ), toString( left->doesFloat() ), toString( right->doesFloat() ) );
126 
127  if ( left->hasDuration() != right->hasDuration() )
128  conflictField( i18n( "Has duration" ), toString( left->hasDuration() ), toString( right->hasDuration() ) );
129 
130  if ( left->duration() != right->duration() )
131  conflictField( i18n( "Duration" ), TQString::number( left->duration() ), TQString::number( right->duration() ) );
132 }
133 
134 void CalendarDiffAlgo::diffIncidence( KCal::Incidence *left, KCal::Incidence *right )
135 {
136  if ( !compareString( left->description(), right->description() ) )
137  conflictField( i18n( "Description" ), left->description(), right->description() );
138 
139  if ( !compareString( left->summary(), right->summary() ) )
140  conflictField( i18n( "Summary" ), left->summary(), right->summary() );
141 
142  if ( left->status() != right->status() )
143  conflictField( i18n( "Status" ), left->statusStr(), right->statusStr() );
144 
145  if ( left->secrecy() != right->secrecy() )
146  conflictField( i18n( "Secrecy" ), toString( left->secrecy() ), toString( right->secrecy() ) );
147 
148  if ( left->priority() != right->priority() )
149  conflictField( i18n( "Priority" ), toString( left->priority() ), toString( right->priority() ) );
150 
151  if ( !compareString( left->location(), right->location() ) )
152  conflictField( i18n( "Location" ), left->location(), right->location() );
153 
154  diffList( i18n( "Categories" ), left->categories(), right->categories() );
155  diffList( i18n( "Alarms" ), left->alarms(), right->alarms() );
156  diffList( i18n( "Resources" ), left->resources(), right->resources() );
157  diffList( i18n( "Relations" ), left->relations(), right->relations() );
158  diffList( i18n( "Attachments" ), left->attachments(), right->attachments() );
159 #if LIBKCAL_IS_VERSION( 1, 3, 1 )
160  diffList( i18n( "Exception Dates" ), left->recurrence()->exDates(), right->recurrence()->exDates() );
161  diffList( i18n( "Exception Times" ), left->recurrence()->exDateTimes(), right->recurrence()->exDateTimes() );
162 #endif
163  // TODO: recurrence dates and date/times, exrules, rrules
164 
165  if ( left->created() != right->created() )
166  conflictField( i18n( "Created" ), left->created().toString(), right->created().toString() );
167 
168  if ( !compareString( left->relatedToUid(), right->relatedToUid() ) )
169  conflictField( i18n( "Related Uid" ), left->relatedToUid(), right->relatedToUid() );
170 }
171 
172 void CalendarDiffAlgo::diffEvent( KCal::Event *left, KCal::Event *right )
173 {
174  if ( left->hasEndDate() != right->hasEndDate() )
175  conflictField( i18n( "Has End Date" ), toString( left->hasEndDate() ), toString( right->hasEndDate() ) );
176 
177  if ( left->dtEnd() != right->dtEnd() )
178  conflictField( i18n( "End Date" ), left->dtEndStr(), right->dtEndStr() );
179 
180  // TODO: check transparency
181 }
182 
183 void CalendarDiffAlgo::diffTodo( KCal::Todo *left, KCal::Todo *right )
184 {
185  if ( left->hasStartDate() != right->hasStartDate() )
186  conflictField( i18n( "Has Start Date" ), toString( left->hasStartDate() ), toString( right->hasStartDate() ) );
187 
188  if ( left->hasDueDate() != right->hasDueDate() )
189  conflictField( i18n( "Has Due Date" ), toString( left->hasDueDate() ), toString( right->hasDueDate() ) );
190 
191  if ( left->dtDue() != right->dtDue() )
192  conflictField( i18n( "Due Date" ), left->dtDue().toString(), right->dtDue().toString() );
193 
194  if ( left->hasCompletedDate() != right->hasCompletedDate() )
195  conflictField( i18n( "Has Complete Date" ), toString( left->hasCompletedDate() ), toString( right->hasCompletedDate() ) );
196 
197  if ( left->percentComplete() != right->percentComplete() )
198  conflictField( i18n( "Complete" ), TQString::number( left->percentComplete() ), TQString::number( right->percentComplete() ) );
199 
200  if ( left->completed() != right->completed() )
201  conflictField( i18n( "Completed" ), toString( left->completed() ), toString( right->completed() ) );
202 }
203 
204 template <class L>
205 void CalendarDiffAlgo::diffList( const TQString &id,
206  const TQValueList<L> &left, const TQValueList<L> &right )
207 {
208  for ( uint i = 0; i < left.count(); ++i ) {
209  if ( right.find( left[ i ] ) == right.end() )
210  additionalLeftField( id, toString( left[ i ] ) );
211  }
212 
213  for ( uint i = 0; i < right.count(); ++i ) {
214  if ( left.find( right[ i ] ) == left.end() )
215  additionalRightField( id, toString( right[ i ] ) );
216  }
217 }