• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.11.3 API Reference
  • KDE Home
  • Contact Us
 

KCalCore Library

  • kcalcore
compat.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the kcalcore library.
3 
4  Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org>
5  Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
6  Copyright (C) 2012 Christian Mollekopf <mollekopf@kolabsys.com>
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Library General Public
10  License as published by the Free Software Foundation; either
11  version 2 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Library General Public License for more details.
17 
18  You should have received a copy of the GNU Library General Public License
19  along with this library; see the file COPYING.LIB. If not, write to
20  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  Boston, MA 02110-1301, USA.
22 */
35 #include "compat.h"
36 #include "incidence.h"
37 
38 #include <KDebug>
39 
40 #include <QtCore/QRegExp>
41 #include <QtCore/QString>
42 #include <QtCore/QDate>
43 
44 using namespace KCalCore;
45 
46 Compat *CompatFactory::createCompat( const QString &productId,
47  const QString &implementationVersion )
48 {
49  Compat *compat = 0;
50 
51  int korg = productId.indexOf( "KOrganizer" );
52  int outl9 = productId.indexOf( "Outlook 9.0" );
53 
54  if ( korg >= 0 ) {
55  int versionStart = productId.indexOf( " ", korg );
56  if ( versionStart >= 0 ) {
57  int versionStop = productId.indexOf( QRegExp( "[ /]" ), versionStart + 1 );
58  if ( versionStop >= 0 ) {
59  QString version = productId.mid( versionStart + 1,
60  versionStop - versionStart - 1 );
61 
62  int versionNum = version.section( '.', 0, 0 ).toInt() * 10000 +
63  version.section( '.', 1, 1 ).toInt() * 100 +
64  version.section( '.', 2, 2 ).toInt();
65  int releaseStop = productId.indexOf( "/", versionStop );
66  QString release;
67  if ( releaseStop > versionStop ) {
68  release = productId.mid( versionStop+1, releaseStop-versionStop-1 );
69  }
70  if ( versionNum < 30100 ) {
71  compat = new CompatPre31;
72  } else if ( versionNum < 30200 ) {
73  compat = new CompatPre32;
74  } else if ( versionNum == 30200 && release == "pre" ) {
75  kDebug() << "Generating compat for KOrganizer 3.2 pre";
76  compat = new Compat32PrereleaseVersions;
77  } else if ( versionNum < 30400 ) {
78  compat = new CompatPre34;
79  } else if ( versionNum < 30500 ) {
80  compat = new CompatPre35;
81  }
82  }
83  }
84  } else if ( outl9 >= 0 ) {
85  kDebug() << "Generating compat for Outlook < 2000 (Outlook 9.0)";
86  compat = new CompatOutlook9;
87  }
88  if ( !compat ) {
89  compat = new Compat;
90  }
91  // Older implementations lacked the implementation version,
92  // so apply this fix if it is a file from kontact and the version is missing.
93  if ( implementationVersion.isEmpty() &&
94  ( productId.contains( "libkcal" ) ||
95  productId.contains( "KOrganizer" ) ||
96  productId.contains( "KAlarm" ) ) ) {
97  compat = new CompatPre410( compat );
98  }
99 
100  return compat;
101 }
102 
103 Compat::Compat()
104 {
105 }
106 
107 Compat::~Compat()
108 {
109 }
110 
111 void Compat::fixEmptySummary( const Incidence::Ptr &incidence )
112 {
113  // some stupid vCal exporters ignore the standard and use Description
114  // instead of Summary for the default field. Correct for this: Copy the
115  // first line of the description to the summary (if summary is just one
116  // line, move it)
117  if ( incidence->summary().isEmpty() && !( incidence->description().isEmpty() ) ) {
118  QString oldDescription = incidence->description().trimmed();
119  QString newSummary( oldDescription );
120  newSummary.remove( QRegExp( "\n.*" ) );
121  incidence->setSummary( newSummary );
122  if ( oldDescription == newSummary ) {
123  incidence->setDescription( "" );
124  }
125  }
126 }
127 
128 void Compat::fixAlarms( const Incidence::Ptr &incidence )
129 {
130  Q_UNUSED( incidence );
131 }
132 
133 void Compat::fixFloatingEnd( QDate &date )
134 {
135  Q_UNUSED( date );
136 }
137 
138 void Compat::fixRecurrence( const Incidence::Ptr &incidence )
139 {
140  Q_UNUSED( incidence );
141  // Prevent use of compatibility mode during subsequent changes by the application
142  // incidence->recurrence()->setCompatVersion();
143 }
144 
145 int Compat::fixPriority( int priority )
146 {
147  return priority;
148 }
149 
150 bool Compat::useTimeZoneShift()
151 {
152  return true;
153 }
154 
155 void Compat::setCreatedToDtStamp( const Incidence::Ptr &incidence, const KDateTime &dtstamp )
156 {
157  Q_UNUSED( incidence );
158  Q_UNUSED( dtstamp );
159 }
160 
161 struct CompatDecorator::Private {
162  Compat *compat;
163 };
164 
165 CompatDecorator::CompatDecorator( Compat *compat )
166 : d( new CompatDecorator::Private )
167 {
168  d->compat = compat;
169 }
170 
171 CompatDecorator::~CompatDecorator()
172 {
173  delete d->compat;
174  delete d;
175 }
176 
177 void CompatDecorator::fixEmptySummary( const Incidence::Ptr &incidence )
178 {
179  d->compat->fixEmptySummary( incidence );
180 }
181 
182 void CompatDecorator::fixAlarms( const Incidence::Ptr &incidence )
183 {
184  d->compat->fixAlarms( incidence );
185 }
186 
187 void CompatDecorator::fixFloatingEnd( QDate &date )
188 {
189  d->compat->fixFloatingEnd( date );
190 }
191 
192 void CompatDecorator::fixRecurrence( const Incidence::Ptr &incidence )
193 {
194  d->compat->fixRecurrence( incidence );
195 }
196 
197 int CompatDecorator::fixPriority( int priority )
198 {
199  return d->compat->fixPriority( priority );
200 }
201 
202 bool CompatDecorator::useTimeZoneShift()
203 {
204  return d->compat->useTimeZoneShift();
205 }
206 
207 void CompatDecorator::setCreatedToDtStamp( const Incidence::Ptr &incidence,
208  const KDateTime &dtstamp )
209 {
210  d->compat->setCreatedToDtStamp( incidence, dtstamp );
211 }
212 
213 void CompatPre35::fixRecurrence( const Incidence::Ptr &incidence )
214 {
215  Recurrence *recurrence = incidence->recurrence();
216  if ( recurrence ) {
217  KDateTime start( incidence->dtStart() );
218  // kde < 3.5 only had one rrule, so no need to loop over all RRULEs.
219  RecurrenceRule *r = recurrence->defaultRRule();
220  if ( r && !r->dateMatchesRules( start ) ) {
221  recurrence->addExDateTime( start );
222  }
223  }
224 
225  // Call base class method now that everything else is done
226  Compat::fixRecurrence( incidence );
227 }
228 
229 int CompatPre34::fixPriority( int priority )
230 {
231  if ( 0 < priority && priority < 6 ) {
232  // adjust 1->1, 2->3, 3->5, 4->7, 5->9
233  return 2 * priority - 1;
234  } else {
235  return priority;
236  }
237 }
238 
239 void CompatPre32::fixRecurrence( const Incidence::Ptr &incidence )
240 {
241  Recurrence *recurrence = incidence->recurrence();
242  if ( recurrence->recurs() && recurrence->duration() > 0 ) {
243  recurrence->setDuration( recurrence->duration() + incidence->recurrence()->exDates().count() );
244  }
245  // Call base class method now that everything else is done
246  CompatPre35::fixRecurrence( incidence );
247 }
248 
249 void CompatPre31::fixFloatingEnd( QDate &endDate )
250 {
251  endDate = endDate.addDays( 1 );
252 }
253 
254 void CompatPre31::fixRecurrence( const Incidence::Ptr &incidence )
255 {
256  CompatPre32::fixRecurrence( incidence );
257 
258  Recurrence *recur = incidence->recurrence();
259  RecurrenceRule *r = 0;
260  if ( recur ) {
261  r = recur->defaultRRule();
262  }
263  if ( recur && r ) {
264  int duration = r->duration();
265  if ( duration > 0 ) {
266  // Backwards compatibility for KDE < 3.1.
267  // rDuration was set to the number of time periods to recur,
268  // with week start always on a Monday.
269  // Convert this to the number of occurrences.
270  r->setDuration( -1 );
271  QDate end( r->startDt().date() );
272  bool doNothing = false;
273  // # of periods:
274  int tmp = ( duration - 1 ) * r->frequency();
275  switch ( r->recurrenceType() ) {
276  case RecurrenceRule::rWeekly:
277  {
278  end = end.addDays( tmp * 7 + 7 - end.dayOfWeek() );
279  break;
280  }
281  case RecurrenceRule::rMonthly:
282  {
283  int month = end.month() - 1 + tmp;
284  end.setYMD( end.year() + month / 12, month % 12 + 1, 31 );
285  break;
286  }
287  case RecurrenceRule::rYearly:
288  {
289  end.setYMD( end.year() + tmp, 12, 31 );
290  break;
291  }
292  default:
293  doNothing = true;
294  break;
295  }
296  if ( !doNothing ) {
297  duration = r->durationTo(
298  KDateTime( end, QTime( 0, 0, 0 ), incidence->dtStart().timeSpec() ) );
299  r->setDuration( duration );
300  }
301  }
302 
303  /* addYearlyNum */
304  // Dates were stored as day numbers, with a fiddle to take account of
305  // leap years. Convert the day number to a month.
306  QList<int> days = r->byYearDays();
307  if ( !days.isEmpty() ) {
308  QList<int> months = r->byMonths();
309  for ( int i = 0; i < months.size(); ++i ) {
310  int newmonth =
311  QDate( r->startDt().date().year(), 1, 1 ).addDays( months.at( i ) - 1 ).month();
312  if ( !months.contains( newmonth ) ) {
313  months.append( newmonth );
314  }
315  }
316 
317  r->setByMonths( months );
318  days.clear();
319  r->setByYearDays( days );
320  }
321  }
322 }
323 
324 void CompatOutlook9::fixAlarms( const Incidence::Ptr &incidence )
325 {
326  if ( !incidence ) {
327  return;
328  }
329  Alarm::List alarms = incidence->alarms();
330  Alarm::List::Iterator it;
331  for ( it = alarms.begin(); it != alarms.end(); ++it ) {
332  Alarm::Ptr al = *it;
333  if ( al && al->hasStartOffset() ) {
334  Duration offsetDuration = al->startOffset();
335  int offs = offsetDuration.asSeconds();
336  if ( offs > 0 ) {
337  offsetDuration = Duration( -offs );
338  }
339  al->setStartOffset( offsetDuration );
340  }
341  }
342 }
343 
344 bool Compat32PrereleaseVersions::useTimeZoneShift()
345 {
346  return false;
347 }
348 
349 CompatPre410::CompatPre410( Compat *decoratedCompat )
350 : CompatDecorator( decoratedCompat )
351 {
352 }
353 
354 void CompatPre410::setCreatedToDtStamp( const Incidence::Ptr &incidence, const KDateTime &dtstamp )
355 {
356  if ( dtstamp.isValid() ) {
357  incidence->setCreated( dtstamp );
358  }
359 }
KCalCore::Compat::setCreatedToDtStamp
virtual void setCreatedToDtStamp(const Incidence::Ptr &incidence, const KDateTime &dtstamp)
Sets the created and dtstamp.
Definition: compat.cpp:155
KCalCore::Compat::useTimeZoneShift
virtual bool useTimeZoneShift()
Returns true if a timezone shift should be used; false otherwise.
Definition: compat.cpp:150
KCalCore::CompatDecorator::fixEmptySummary
virtual void fixEmptySummary(const Incidence::Ptr &incidence)
Definition: compat.cpp:177
KCalCore::CompatDecorator::setCreatedToDtStamp
virtual void setCreatedToDtStamp(const Incidence::Ptr &incidence, const KDateTime &dtstamp)
Definition: compat.cpp:207
KCalCore::Compat::fixFloatingEnd
virtual void fixFloatingEnd(QDate &date)
Fixes the end date for floating events.
Definition: compat.cpp:133
KCalCore::Duration
Represents a span of time measured in seconds or days.
Definition: duration.h:52
KCalCore::Compat::~Compat
virtual ~Compat()
Destructor.
Definition: compat.cpp:107
KCalCore::Alarm::Ptr
QSharedPointer< Alarm > Ptr
A shared pointer to an Alarm object.
Definition: alarm.h:76
KCalCore::Recurrence::setDuration
void setDuration(int duration)
Sets the total number of times the event is to occur, including both the first and last...
Definition: recurrence.cpp:500
KCalCore::CompatPre410::setCreatedToDtStamp
virtual void setCreatedToDtStamp(const Incidence::Ptr &incidence, const KDateTime &dtstamp)
Definition: compat.cpp:354
KCalCore::RecurrenceRule::setDuration
void setDuration(int duration)
Sets the total number of times the event is to occur, including both the first and last...
Definition: recurrencerule.cpp:995
KCalCore::CompatDecorator::useTimeZoneShift
virtual bool useTimeZoneShift()
Definition: compat.cpp:202
KCalCore::CompatPre31::fixRecurrence
virtual void fixRecurrence(const Incidence::Ptr &incidence)
Definition: compat.cpp:254
KCalCore::CompatDecorator::fixAlarms
virtual void fixAlarms(const Incidence::Ptr &incidence)
Definition: compat.cpp:182
KCalCore::Alarm::List
QVector< Ptr > List
List of alarms.
Definition: alarm.h:81
KCalCore::Incidence::Ptr
QSharedPointer< Incidence > Ptr
A shared pointer to an Incidence.
Definition: incidence.h:112
KCalCore::CompatDecorator::fixFloatingEnd
virtual void fixFloatingEnd(QDate &date)
Definition: compat.cpp:187
KCalCore::Compat
This class provides compatibility to older or broken calendar files.
Definition: compat.h:71
KCalCore::CompatPre32
Compatibility class for KOrganizer pre-3.2 calendar files.
Definition: compat.h:251
KCalCore::Recurrence
This class represents a recurrence rule for a calendar incidence.
Definition: recurrence.h:87
KCalCore::Compat::fixPriority
virtual int fixPriority(int priority)
Fixes the priority.
Definition: compat.cpp:145
KCalCore::CompatPre35
Compatibility class for KOrganizer pre-3.5 calendar files.
Definition: compat.h:206
KCalCore::Compat::fixEmptySummary
virtual void fixEmptySummary(const Incidence::Ptr &incidence)
Fixes an empty summary for an incidence.
Definition: compat.cpp:111
KCalCore::CompatPre32::fixRecurrence
virtual void fixRecurrence(const Incidence::Ptr &incidence)
Definition: compat.cpp:239
KCalCore::CompatPre34::fixPriority
virtual int fixPriority(int priority)
Definition: compat.cpp:229
KCalCore::CompatDecorator::fixRecurrence
virtual void fixRecurrence(const Incidence::Ptr &incidence)
Definition: compat.cpp:192
KCalCore::RecurrenceRule::frequency
uint frequency() const
Returns the recurrence frequency, in terms of the recurrence time period type.
Definition: recurrencerule.cpp:2149
KCalCore::Compat32PrereleaseVersions::useTimeZoneShift
virtual bool useTimeZoneShift()
Definition: compat.cpp:344
KCalCore::CompatOutlook9
Compatibility class for Outlook 9 calendar files.
Definition: compat.h:333
KCalCore::RecurrenceRule::startDt
KDateTime startDt() const
Returns the recurrence start date/time.
Definition: recurrencerule.cpp:2139
KCalCore::CompatPre31
Compatibility class for KOrganizer pre-3.1 calendar files.
Definition: compat.h:283
KCalCore::Recurrence::duration
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:482
compat.h
This file is part of the API for handling calendar data and defines classes for managing compatibilit...
KCalCore::CompatFactory::createCompat
static Compat * createCompat(const QString &productId, const QString &implementationVersion)
Creates the appropriate Compat class as determined by the Product ID.
Definition: compat.cpp:46
KCalCore::Compat::fixAlarms
virtual void fixAlarms(const Incidence::Ptr &incidence)
Fixes the alarms list an incidence.
Definition: compat.cpp:128
KCalCore::CompatDecorator
Decorator so multiple compatibility classes can be stacked.
Definition: compat.h:140
KCalCore::Compat32PrereleaseVersions
Compatibility class for KOrganizer prerelease 3.2 calendar files.
Definition: compat.h:309
KCalCore::Compat::Compat
Compat()
Constructor.
Definition: compat.cpp:103
KCalCore::CompatPre35::fixRecurrence
virtual void fixRecurrence(const Incidence::Ptr &incidence)
Definition: compat.cpp:213
incidence.h
This file is part of the API for handling calendar data and defines the Incidence class...
KCalCore::RecurrenceRule::durationTo
int durationTo(const KDateTime &dt) const
Returns the number of recurrences up to and including the date/time specified.
Definition: recurrencerule.cpp:1581
KCalCore::RecurrenceRule::duration
int duration() const
Returns -1 if the event recurs infinitely, 0 if the end date is set, otherwise the total number of re...
Definition: recurrencerule.cpp:2154
KCalCore::CompatDecorator::fixPriority
virtual int fixPriority(int priority)
Definition: compat.cpp:197
KCalCore::CompatPre34
Compatibility class for KOrganizer pre-3.4 calendar files.
Definition: compat.h:226
KCalCore::CompatOutlook9::fixAlarms
virtual void fixAlarms(const Incidence::Ptr &incidence)
Definition: compat.cpp:324
KCalCore::Duration::asSeconds
int asSeconds() const
Returns the length of the duration in seconds.
Definition: duration.cpp:200
KCalCore::CompatPre410
Compatibility class for Kontact &lt; 4.10 calendar files.
Definition: compat.h:353
KCalCore::Compat::fixRecurrence
virtual void fixRecurrence(const Incidence::Ptr &incidence)
Fixes the recurrence rule for an incidence.
Definition: compat.cpp:138
KCalCore::Recurrence::recurs
bool recurs() const
Returns whether the event recurs at all.
Definition: recurrence.cpp:230
KCalCore::CompatPre31::fixFloatingEnd
virtual void fixFloatingEnd(QDate &date)
Definition: compat.cpp:249
KCalCore::RecurrenceRule
This class represents a recurrence rule for a calendar incidence.
Definition: recurrencerule.h:43
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Tue Nov 26 2013 09:02:04 by doxygen 1.8.5 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KCalCore Library

Skip menu "KCalCore Library"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdepimlibs-4.11.3 API Reference

Skip menu "kdepimlibs-4.11.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal