libkcal

compat.cpp
1 /*
2  This file is part of libkcal.
3 
4  Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org>
5  Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  Boston, MA 02110-1301, USA.
21 */
22 
23 #include "compat.h"
24 
25 #include <kdebug.h>
26 
27 #include <tqregexp.h>
28 
29 #include "incidence.h"
30 
31 using namespace KCal;
32 
33 Compat *CompatFactory::createCompat( const TQString &productId )
34 {
35 // kdDebug(5800) << "CompatFactory::createCompat(): '" << productId << "'"
36 // << endl;
37 
38  Compat *compat = 0;
39 
40  int korg = productId.find( "KOrganizer" );
41  int outl9 = productId.find( "Outlook 9.0" );
42 // int kcal = productId.find( "LibKCal" );
43 
44  // TODO: Use the version of LibKCal to determine the compat class...
45  if ( korg >= 0 ) {
46  int versionStart = productId.find( " ", korg );
47  if ( versionStart >= 0 ) {
48  int versionStop = productId.find( TQRegExp( "[ /]" ), versionStart + 1 );
49  if ( versionStop >= 0 ) {
50  TQString version = productId.mid( versionStart + 1,
51  versionStop - versionStart - 1 );
52 // kdDebug(5800) << "Found KOrganizer version: " << version << endl;
53 
54  int versionNum = version.section( ".", 0, 0 ).toInt() * 10000 +
55  version.section( ".", 1, 1 ).toInt() * 100 +
56  version.section( ".", 2, 2 ).toInt();
57  int releaseStop = productId.find( "/", versionStop );
58  TQString release;
59  if ( releaseStop > versionStop ) {
60  release = productId.mid( versionStop+1, releaseStop-versionStop-1 );
61  }
62 // kdDebug(5800) << "KOrganizer release: \"" << release << "\"" << endl;
63 
64 // kdDebug(5800) << "Numerical version: " << versionNum << endl;
65 
66  if ( versionNum < 30100 ) {
67  compat = new CompatPre31;
68  } else if ( versionNum < 30200 ) {
69  compat = new CompatPre32;
70  } else if ( versionNum == 30200 && release == "pre" ) {
71  kdDebug(5800) << "Generating compat for KOrganizer 3.2 pre " << endl;
72  compat = new Compat32PrereleaseVersions;
73  } else if ( versionNum < 30400 ) {
74  compat = new CompatPre34;
75  } else if ( versionNum < 30500 ) {
76  compat = new CompatPre35;
77  }
78  }
79  }
80  } else if ( outl9 >= 0 ) {
81  kdDebug(5800) << "Generating compat for Outlook < 2000 (Outlook 9.0)" << endl;
82  compat = new CompatOutlook9;
83  }
84 
85  if ( !compat ) compat = new Compat;
86 
87  return compat;
88 }
89 
90 void Compat::fixEmptySummary( Incidence *incidence )
91 {
92  // some stupid vCal exporters ignore the standard and use Description
93  // instead of Summary for the default field. Correct for this: Copy the
94  // first line of the description to the summary (if summary is just one
95  // line, move it)
96  if (incidence->summary().isEmpty() &&
97  !(incidence->description().isEmpty())) {
98  TQString oldDescription = incidence->description().stripWhiteSpace();
99  TQString newSummary( oldDescription );
100  newSummary.remove( TQRegExp("\n.*") );
101  incidence->setSummary( newSummary );
102  if ( oldDescription == newSummary )
103  incidence->setDescription("");
104  }
105 }
106 
107 void Compat::fixRecurrence( Incidence */*incidence*/ )
108 {
109  // Prevent use of compatibility mode during subsequent changes by the application
110 // incidence->recurrence()->setCompatVersion();
111 }
112 
118 void CompatPre35::fixRecurrence( Incidence *incidence )
119 {
120  Recurrence* recurrence = incidence->recurrence();
121  if (recurrence ) {
122  TQDateTime start( incidence->dtStart() );
123  // kde < 3.5 only had one rrule, so no need to loop over all RRULEs.
124  RecurrenceRule *r = recurrence->defaultRRule();
125  if ( r && !r->dateMatchesRules( start ) ) {
126  recurrence->addExDateTime( start );
127  }
128  }
129 
130  // Call base class method now that everything else is done
131  Compat::fixRecurrence( incidence );
132 }
133 
134 int CompatPre34::fixPriority( int prio )
135 {
136  if ( 0<prio && prio<6 ) {
137  // adjust 1->1, 2->3, 3->5, 4->7, 5->9
138  return 2*prio - 1;
139  } else return prio;
140 }
141 
146 void CompatPre32::fixRecurrence( Incidence *incidence )
147 {
148  Recurrence* recurrence = incidence->recurrence();
149  if ( recurrence->doesRecur() && recurrence->duration() > 0 ) {
150  recurrence->setDuration( recurrence->duration() + incidence->recurrence()->exDates().count() );
151  }
152  // Call base class method now that everything else is done
153  CompatPre35::fixRecurrence( incidence );
154 }
155 
167 void CompatPre31::fixFloatingEnd( TQDate &endDate )
168 {
169  endDate = endDate.addDays( 1 );
170 }
171 
172 void CompatPre31::fixRecurrence( Incidence *incidence )
173 {
174  CompatPre32::fixRecurrence( incidence );
175 
176  Recurrence *recur = incidence->recurrence();
177  RecurrenceRule *r = 0;
178  if ( recur ) r = recur->defaultRRule();
179  if ( recur && r ) {
180  int duration = r->duration();
181  if ( duration > 0 ) {
182  // Backwards compatibility for KDE < 3.1.
183  // rDuration was set to the number of time periods to recur,
184  // with week start always on a Monday.
185  // Convert this to the number of occurrences.
186  r->setDuration( -1 );
187  TQDate end( r->startDt().date() );
188  bool doNothing = false;
189  // # of periods:
190  int tmp = ( duration - 1 ) * r->frequency();
191  switch ( r->recurrenceType() ) {
192  case RecurrenceRule::rWeekly: {
193  end = end.addDays( tmp * 7 + 7 - end.dayOfWeek() );
194  break; }
195  case RecurrenceRule::rMonthly: {
196  int month = end.month() - 1 + tmp;
197  end.setYMD( end.year() + month / 12, month % 12 + 1, 31 );
198  break; }
199  case RecurrenceRule::rYearly: {
200  end.setYMD( end.year() + tmp, 12, 31);
201  break; }
202  default:
203  doNothing = true;
204  break;
205  }
206  if ( !doNothing ) {
207  duration = r->durationTo( TQDateTime( end, TQTime( 0, 0, 0 ) ) );
208  r->setDuration( duration );
209  }
210  }
211 
212  /* addYearlyNum */
213  // Dates were stored as day numbers, with a fiddle to take account of leap years.
214  // Convert the day number to a month.
215  TQValueList<int> days = r->byYearDays();
216  if ( !days.isEmpty() ) {
217  TQValueList<int> months = r->byMonths();
218  for ( TQValueListConstIterator<int> it = days.begin(); it != days.end(); ++it ) {
219  int newmonth = TQDate( r->startDt().date().year(), 1, 1).addDays( (*it) - 1 ).month();
220  if ( !months.contains( newmonth ) )
221  months.append( newmonth );
222  }
223  r->setByMonths( months );
224  days.clear();
225  r->setByYearDays( days );
226  }
227  }
228 
229 
230 }
231 
235 void CompatOutlook9::fixAlarms( Incidence *incidence )
236 {
237  if ( !incidence ) return;
238  Alarm::List alarms = incidence->alarms();
239  Alarm::List::Iterator it;
240  for ( it = alarms.begin(); it != alarms.end(); ++it ) {
241  Alarm *al = *it;
242  if ( al && al->hasStartOffset() ) {
243  Duration offsetDuration = al->startOffset();
244  int offs = offsetDuration.asSeconds();
245  if ( offs>0 )
246  offsetDuration = Duration( -offs );
247  al->setStartOffset( offsetDuration );
248  }
249  }
250 }
This class represents an alarm notification.
Definition: alarm.h:46
bool hasStartOffset() const
Return whether the alarm is defined in terms of an offset relative to the start of the event.
Definition: alarm.cpp:456
Duration startOffset() const
Return offset of alarm in time relative to the start of the event.
Definition: alarm.cpp:451
void setStartOffset(const Duration &)
Set offset of alarm in time relative to the start of the event.
Definition: alarm.cpp:443
This class provides compatibility to older (broken) versions of KOrganizer.
Definition: compat.h:46
This class represents a duration.
Definition: duration.h:34
int asSeconds() const
Returns the length of the duration in seconds.
Definition: duration.cpp:165
virtual TQDateTime dtStart() const
returns an event's starting date/time as a TQDateTime.
This class provides the base class common to all calendar components.
Definition: incidence.h:48
const Alarm::List & alarms() const
All alarms that are associated with this incidence.
Definition: incidence.cpp:828
void setSummary(const TQString &summary)
Set short summary.
Definition: incidence.cpp:286
TQString description() const
Return long description.
Definition: incidence.cpp:280
void setDescription(const TQString &description)
Set the long description.
Definition: incidence.cpp:273
TQString summary() const
Return short summary.
Definition: incidence.cpp:293
Recurrence * recurrence() const
Return the recurrence rule associated with this incidence.
Definition: incidence.cpp:390
This class represents a recurrence rule for a calendar incidence.
void setDuration(int duration)
Sets the total number of times the event is to occur, including both the first and last.
int durationTo(const TQDateTime &) const
Returns the number of recurrences up to and including the date/time specified.
TQDateTime startDt() const
Return the start of the recurrence.
uint frequency() const
Returns frequency of recurrence, in terms of the recurrence time period type.
int duration() const
Returns -1 if the event recurs infinitely, 0 if the end date is set, otherwise the total number of re...
bool dateMatchesRules(const TQDateTime &qdt) const
Returns true if the date matches the rules.
This class represents a recurrence rule for a calendar incidence.
Definition: recurrence.h:90
bool doesRecur() const
Returns whether the event recurs at all.
Definition: recurrence.cpp:184
int duration() const
Returns -1 if the event recurs infinitely, 0 if the end date is set, otherwise the total number of re...
Definition: recurrence.cpp:395
void setDuration(int duration)
Sets the total number of times the event is to occur, including both the first and last.
Definition: recurrence.cpp:415
Namespace KCal is for global classes, objects and/or functions in libkcal.
Definition: alarm.h:38