libtdepim

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 "calendardiffalgo.h"
25 
26 using namespace KPIM;
27 
28 #ifndef KDE_USE_FINAL
29 static bool compareString( const TQString &left, const TQString &right )
30 {
31  if ( left.isEmpty() && right.isEmpty() )
32  return true;
33  else
34  return left == right;
35 }
36 #endif
37 
38 static TQString toString( KCal::Attendee *attendee )
39 {
40  return attendee->name() + "<" + attendee->email() + ">";
41 }
42 
43 static TQString toString( KCal::Alarm * )
44 {
45  return TQString();
46 }
47 
48 static TQString toString( KCal::Incidence * )
49 {
50  return TQString();
51 }
52 
53 static TQString toString( KCal::Attachment * )
54 {
55  return TQString();
56 }
57 
58 static TQString toString( const TQDate &date )
59 {
60  return date.toString();
61 }
62 
63 static TQString toString( const TQDateTime &dateTime )
64 {
65  return dateTime.toString();
66 }
67 
68 static TQString toString( const TQString str )
69 {
70  return str;
71 }
72 
73 static TQString toString( bool value )
74 {
75  if ( value )
76  return i18n( "Yes" );
77  else
78  return i18n( "No" );
79 }
80 
81 CalendarDiffAlgo::CalendarDiffAlgo( KCal::Incidence *leftIncidence,
82  KCal::Incidence *rightIncidence )
83  : mLeftIncidence( leftIncidence ), mRightIncidence( rightIncidence )
84 {
85 }
86 
87 void CalendarDiffAlgo::run()
88 {
89  begin();
90 
91  diffIncidenceBase( mLeftIncidence, mRightIncidence );
92  diffIncidence( mLeftIncidence, mRightIncidence );
93 
94  KCal::Event *leftEvent = dynamic_cast<KCal::Event*>( mLeftIncidence );
95  KCal::Event *rightEvent = dynamic_cast<KCal::Event*>( mRightIncidence );
96  if ( leftEvent && rightEvent ) {
97  diffEvent( leftEvent, rightEvent );
98  } else {
99  KCal::Todo *leftTodo = dynamic_cast<KCal::Todo*>( mLeftIncidence );
100  KCal::Todo *rightTodo = dynamic_cast<KCal::Todo*>( mRightIncidence );
101  if ( leftTodo && rightTodo ) {
102  diffTodo( leftTodo, rightTodo );
103  }
104  }
105 
106  end();
107 }
108 
109 void CalendarDiffAlgo::diffIncidenceBase( KCal::IncidenceBase *left, KCal::IncidenceBase *right )
110 {
111  diffList( i18n( "Attendees" ), left->attendees(), right->attendees() );
112 
113  if ( left->dtStart() != right->dtStart() )
114  conflictField( i18n( "Start time" ), left->dtStartStr(), right->dtStartStr() );
115 
116  if ( !compareString( left->organizer().fullName(), right->organizer().fullName() ) )
117  conflictField( i18n( "Organizer" ), left->organizer().fullName(), right->organizer().fullName() );
118 
119  if ( !compareString( left->uid(), right->uid() ) )
120  conflictField( i18n( "UID" ), left->uid(), right->uid() );
121 
122  if ( left->doesFloat() != right->doesFloat() )
123  conflictField( i18n( "Is floating" ), toString( left->doesFloat() ), toString( right->doesFloat() ) );
124 
125  if ( left->hasDuration() != right->hasDuration() )
126  conflictField( i18n( "Has duration" ), toString( left->hasDuration() ), toString( right->hasDuration() ) );
127 
128  if ( left->duration() != right->duration() )
129  conflictField( i18n( "Duration" ), TQString::number( left->duration() ), TQString::number( right->duration() ) );
130 }
131 
132 void CalendarDiffAlgo::diffIncidence( KCal::Incidence *left, KCal::Incidence *right )
133 {
134  if ( !compareString( left->description(), right->description() ) )
135  conflictField( i18n( "Description" ), left->description(), right->description() );
136 
137  if ( !compareString( left->summary(), right->summary() ) )
138  conflictField( i18n( "Summary" ), left->summary(), right->summary() );
139 
140  if ( left->status() != right->status() )
141  conflictField( i18n( "Status" ), left->statusStr(), right->statusStr() );
142 
143  if ( left->secrecy() != right->secrecy() )
144  conflictField( i18n( "Secrecy" ), toString( left->secrecy() ), toString( right->secrecy() ) );
145 
146  if ( left->priority() != right->priority() )
147  conflictField( i18n( "Priority" ), toString( left->priority() ), toString( right->priority() ) );
148 
149  if ( !compareString( left->location(), right->location() ) )
150  conflictField( i18n( "Location" ), left->location(), right->location() );
151 
152  diffList( i18n( "Categories" ), left->categories(), right->categories() );
153  diffList( i18n( "Alarms" ), left->alarms(), right->alarms() );
154  diffList( i18n( "Resources" ), left->resources(), right->resources() );
155  diffList( i18n( "Relations" ), left->relations(), right->relations() );
156  diffList( i18n( "Attachments" ), left->attachments(), right->attachments() );
157  diffList( i18n( "Exception Dates" ), left->recurrence()->exDates(), right->recurrence()->exDates() );
158  diffList( i18n( "Exception Times" ), left->recurrence()->exDateTimes(), right->recurrence()->exDateTimes() );
159  // TODO: recurrence dates and date/times, exrules, rrules
160 
161  if ( left->created() != right->created() )
162  conflictField( i18n( "Created" ), left->created().toString(), right->created().toString() );
163 
164  if ( !compareString( left->relatedToUid(), right->relatedToUid() ) )
165  conflictField( i18n( "Related Uid" ), left->relatedToUid(), right->relatedToUid() );
166 }
167 
168 void CalendarDiffAlgo::diffEvent( KCal::Event *left, KCal::Event *right )
169 {
170  if ( left->hasEndDate() != right->hasEndDate() )
171  conflictField( i18n( "Has End Date" ), toString( left->hasEndDate() ), toString( right->hasEndDate() ) );
172 
173  if ( left->dtEnd() != right->dtEnd() )
174  conflictField( i18n( "End Date" ), left->dtEndStr(), right->dtEndStr() );
175 
176  // TODO: check transparency
177 }
178 
179 void CalendarDiffAlgo::diffTodo( KCal::Todo *left, KCal::Todo *right )
180 {
181  if ( left->hasStartDate() != right->hasStartDate() )
182  conflictField( i18n( "Has Start Date" ), toString( left->hasStartDate() ), toString( right->hasStartDate() ) );
183 
184  if ( left->hasDueDate() != right->hasDueDate() )
185  conflictField( i18n( "Has Due Date" ), toString( left->hasDueDate() ), toString( right->hasDueDate() ) );
186 
187  if ( left->dtDue() != right->dtDue() )
188  conflictField( i18n( "Due Date" ), left->dtDue().toString(), right->dtDue().toString() );
189 
190  if ( left->hasCompletedDate() != right->hasCompletedDate() )
191  conflictField( i18n( "Has Complete Date" ), toString( left->hasCompletedDate() ), toString( right->hasCompletedDate() ) );
192 
193  if ( left->percentComplete() != right->percentComplete() )
194  conflictField( i18n( "Complete" ), TQString::number( left->percentComplete() ), TQString::number( right->percentComplete() ) );
195 
196  if ( left->completed() != right->completed() )
197  conflictField( i18n( "Completed" ), toString( left->completed() ), toString( right->completed() ) );
198 }
199 
200 template <class L>
201 void CalendarDiffAlgo::diffList( const TQString &id,
202  const TQValueList<L> &left, const TQValueList<L> &right )
203 {
204  for ( uint i = 0; i < left.count(); ++i ) {
205  if ( right.find( left[ i ] ) == right.end() )
206  additionalLeftField( id, toString( left[ i ] ) );
207  }
208 
209  for ( uint i = 0; i < right.count(); ++i ) {
210  if ( left.find( right[ i ] ) == left.end() )
211  additionalRightField( id, toString( right[ i ] ) );
212  }
213 }
TDEPIM classes for drag and drop of mails.