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

KCalCore Library

  • sources
  • kde-4.12
  • kdepimlibs
  • 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(QLatin1String("KOrganizer"));
52  int outl9 = productId.indexOf(QLatin1String("Outlook 9.0"));
53 
54  if (korg >= 0) {
55  int versionStart = productId.indexOf(QLatin1String(" "), korg);
56  if (versionStart >= 0) {
57  int versionStop = productId.indexOf(QRegExp(QLatin1String("[ /]")), versionStart + 1);
58  if (versionStop >= 0) {
59  QString version = productId.mid(versionStart + 1,
60  versionStop - versionStart - 1);
61 
62  int versionNum = version.section(QLatin1Char('.'), 0, 0).toInt() * 10000 +
63  version.section(QLatin1Char('.'), 1, 1).toInt() * 100 +
64  version.section(QLatin1Char('.'), 2, 2).toInt();
65  int releaseStop = productId.indexOf(QLatin1String("/"), 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 == QLatin1String("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(QLatin1String("libkcal")) ||
95  productId.contains(QLatin1String("KOrganizer")) ||
96  productId.contains(QLatin1String("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(QLatin1String("\n.*")));
121  incidence->setSummary(newSummary);
122  if (oldDescription == newSummary) {
123  incidence->setDescription(QLatin1String(""));
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 class CompatDecorator::Private {
162 public:
163  Compat *compat;
164 };
165 
166 CompatDecorator::CompatDecorator(Compat *compat)
167  : d(new CompatDecorator::Private)
168 {
169  d->compat = compat;
170 }
171 
172 CompatDecorator::~CompatDecorator()
173 {
174  delete d->compat;
175  delete d;
176 }
177 
178 void CompatDecorator::fixEmptySummary(const Incidence::Ptr &incidence)
179 {
180  d->compat->fixEmptySummary(incidence);
181 }
182 
183 void CompatDecorator::fixAlarms(const Incidence::Ptr &incidence)
184 {
185  d->compat->fixAlarms(incidence);
186 }
187 
188 void CompatDecorator::fixFloatingEnd(QDate &date)
189 {
190  d->compat->fixFloatingEnd(date);
191 }
192 
193 void CompatDecorator::fixRecurrence(const Incidence::Ptr &incidence)
194 {
195  d->compat->fixRecurrence(incidence);
196 }
197 
198 int CompatDecorator::fixPriority(int priority)
199 {
200  return d->compat->fixPriority(priority);
201 }
202 
203 bool CompatDecorator::useTimeZoneShift()
204 {
205  return d->compat->useTimeZoneShift();
206 }
207 
208 void CompatDecorator::setCreatedToDtStamp(const Incidence::Ptr &incidence,
209  const KDateTime &dtstamp)
210 {
211  d->compat->setCreatedToDtStamp(incidence, dtstamp);
212 }
213 
214 void CompatPre35::fixRecurrence(const Incidence::Ptr &incidence)
215 {
216  Recurrence *recurrence = incidence->recurrence();
217  if (recurrence) {
218  KDateTime start(incidence->dtStart());
219  // kde < 3.5 only had one rrule, so no need to loop over all RRULEs.
220  RecurrenceRule *r = recurrence->defaultRRule();
221  if (r && !r->dateMatchesRules(start)) {
222  recurrence->addExDateTime(start);
223  }
224  }
225 
226  // Call base class method now that everything else is done
227  Compat::fixRecurrence(incidence);
228 }
229 
230 int CompatPre34::fixPriority(int priority)
231 {
232  if (0 < priority && priority < 6) {
233  // adjust 1->1, 2->3, 3->5, 4->7, 5->9
234  return 2 * priority - 1;
235  } else {
236  return priority;
237  }
238 }
239 
240 void CompatPre32::fixRecurrence(const Incidence::Ptr &incidence)
241 {
242  Recurrence *recurrence = incidence->recurrence();
243  if (recurrence->recurs() && recurrence->duration() > 0) {
244  recurrence->setDuration(recurrence->duration() + incidence->recurrence()->exDates().count());
245  }
246  // Call base class method now that everything else is done
247  CompatPre35::fixRecurrence(incidence);
248 }
249 
250 void CompatPre31::fixFloatingEnd(QDate &endDate)
251 {
252  endDate = endDate.addDays(1);
253 }
254 
255 void CompatPre31::fixRecurrence(const Incidence::Ptr &incidence)
256 {
257  CompatPre32::fixRecurrence(incidence);
258 
259  Recurrence *recur = incidence->recurrence();
260  RecurrenceRule *r = 0;
261  if (recur) {
262  r = recur->defaultRRule();
263  }
264  if (recur && r) {
265  int duration = r->duration();
266  if (duration > 0) {
267  // Backwards compatibility for KDE < 3.1.
268  // rDuration was set to the number of time periods to recur,
269  // with week start always on a Monday.
270  // Convert this to the number of occurrences.
271  r->setDuration(-1);
272  QDate end(r->startDt().date());
273  bool doNothing = false;
274  // # of periods:
275  int tmp = (duration - 1) * r->frequency();
276  switch (r->recurrenceType()) {
277  case RecurrenceRule::rWeekly:
278  {
279  end = end.addDays(tmp * 7 + 7 - end.dayOfWeek());
280  break;
281  }
282  case RecurrenceRule::rMonthly:
283  {
284  int month = end.month() - 1 + tmp;
285  end.setYMD(end.year() + month / 12, month % 12 + 1, 31);
286  break;
287  }
288  case RecurrenceRule::rYearly:
289  {
290  end.setYMD(end.year() + tmp, 12, 31);
291  break;
292  }
293  default:
294  doNothing = true;
295  break;
296  }
297  if (!doNothing) {
298  duration = r->durationTo(
299  KDateTime(end, QTime(0, 0, 0), incidence->dtStart().timeSpec()));
300  r->setDuration(duration);
301  }
302  }
303 
304  /* addYearlyNum */
305  // Dates were stored as day numbers, with a fiddle to take account of
306  // leap years. Convert the day number to a month.
307  QList<int> days = r->byYearDays();
308  if (!days.isEmpty()) {
309  QList<int> months = r->byMonths();
310  for (int i = 0; i < months.size(); ++i) {
311  int newmonth =
312  QDate(r->startDt().date().year(), 1, 1).addDays(months.at(i) - 1).month();
313  if (!months.contains(newmonth)) {
314  months.append(newmonth);
315  }
316  }
317 
318  r->setByMonths(months);
319  days.clear();
320  r->setByYearDays(days);
321  }
322  }
323 }
324 
325 void CompatOutlook9::fixAlarms(const Incidence::Ptr &incidence)
326 {
327  if (!incidence) {
328  return;
329  }
330  Alarm::List alarms = incidence->alarms();
331  Alarm::List::Iterator it;
332  for (it = alarms.begin(); it != alarms.end(); ++it) {
333  Alarm::Ptr al = *it;
334  if (al && al->hasStartOffset()) {
335  Duration offsetDuration = al->startOffset();
336  int offs = offsetDuration.asSeconds();
337  if (offs > 0) {
338  offsetDuration = Duration(-offs);
339  }
340  al->setStartOffset(offsetDuration);
341  }
342  }
343 }
344 
345 bool Compat32PrereleaseVersions::useTimeZoneShift()
346 {
347  return false;
348 }
349 
350 CompatPre410::CompatPre410(Compat *decoratedCompat)
351  : CompatDecorator(decoratedCompat)
352 {
353 }
354 
355 void CompatPre410::setCreatedToDtStamp(const Incidence::Ptr &incidence, const KDateTime &dtstamp)
356 {
357  if (dtstamp.isValid()) {
358  incidence->setCreated(dtstamp);
359  }
360 }
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:178
KCalCore::CompatDecorator::setCreatedToDtStamp
virtual void setCreatedToDtStamp(const Incidence::Ptr &incidence, const KDateTime &dtstamp)
Definition: compat.cpp:208
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:55
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:78
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:499
KCalCore::CompatPre410::setCreatedToDtStamp
virtual void setCreatedToDtStamp(const Incidence::Ptr &incidence, const KDateTime &dtstamp)
Definition: compat.cpp:355
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:996
KCalCore::CompatDecorator::useTimeZoneShift
virtual bool useTimeZoneShift()
Definition: compat.cpp:203
KCalCore::CompatPre31::fixRecurrence
virtual void fixRecurrence(const Incidence::Ptr &incidence)
Definition: compat.cpp:255
KCalCore::CompatDecorator::fixAlarms
virtual void fixAlarms(const Incidence::Ptr &incidence)
Definition: compat.cpp:183
KCalCore::Alarm::List
QVector< Ptr > List
List of alarms.
Definition: alarm.h:83
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:188
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:240
KCalCore::CompatPre34::fixPriority
virtual int fixPriority(int priority)
Definition: compat.cpp:230
KCalCore::CompatDecorator::fixRecurrence
virtual void fixRecurrence(const Incidence::Ptr &incidence)
Definition: compat.cpp:193
KCalCore::RecurrenceRule::frequency
uint frequency() const
Returns the recurrence frequency, in terms of the recurrence time period type.
Definition: recurrencerule.cpp:2152
KCalCore::Compat32PrereleaseVersions::useTimeZoneShift
virtual bool useTimeZoneShift()
Definition: compat.cpp:345
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:2142
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:481
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:214
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:1582
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:2157
KCalCore::CompatDecorator::fixPriority
virtual int fixPriority(int priority)
Definition: compat.cpp:198
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:325
KCalCore::Duration::asSeconds
int asSeconds() const
Returns the length of the duration in seconds.
Definition: duration.cpp:200
KCalCore::CompatPre410
Compatibility class for Kontact < 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:228
KCalCore::CompatPre31::fixFloatingEnd
virtual void fixFloatingEnd(QDate &date)
Definition: compat.cpp:250
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-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:59:57 by doxygen 1.8.7 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 API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



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