• 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.14
  • 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  : d( 0 )
105 {
106 }
107 
108 Compat::~Compat()
109 {
110 }
111 
112 void Compat::fixEmptySummary(const Incidence::Ptr &incidence)
113 {
114  // some stupid vCal exporters ignore the standard and use Description
115  // instead of Summary for the default field. Correct for this: Copy the
116  // first line of the description to the summary (if summary is just one
117  // line, move it)
118  if (incidence->summary().isEmpty() && !(incidence->description().isEmpty())) {
119  QString oldDescription = incidence->description().trimmed();
120  QString newSummary(oldDescription);
121  newSummary.remove(QRegExp(QLatin1String("\n.*")));
122  incidence->setSummary(newSummary);
123  if (oldDescription == newSummary) {
124  incidence->setDescription(QLatin1String(""));
125  }
126  }
127 }
128 
129 void Compat::fixAlarms(const Incidence::Ptr &incidence)
130 {
131  Q_UNUSED(incidence);
132 }
133 
134 void Compat::fixFloatingEnd(QDate &date)
135 {
136  Q_UNUSED(date);
137 }
138 
139 void Compat::fixRecurrence(const Incidence::Ptr &incidence)
140 {
141  Q_UNUSED(incidence);
142  // Prevent use of compatibility mode during subsequent changes by the application
143  // incidence->recurrence()->setCompatVersion();
144 }
145 
146 int Compat::fixPriority(int priority)
147 {
148  return priority;
149 }
150 
151 bool Compat::useTimeZoneShift()
152 {
153  return true;
154 }
155 
156 void Compat::setCreatedToDtStamp(const Incidence::Ptr &incidence, const KDateTime &dtstamp)
157 {
158  Q_UNUSED(incidence);
159  Q_UNUSED(dtstamp);
160 }
161 
162 class CompatDecorator::Private {
163 public:
164  Compat *compat;
165 };
166 
167 CompatDecorator::CompatDecorator(Compat *compat)
168  : d(new CompatDecorator::Private)
169 {
170  d->compat = compat;
171 }
172 
173 CompatDecorator::~CompatDecorator()
174 {
175  delete d->compat;
176  delete d;
177 }
178 
179 void CompatDecorator::fixEmptySummary(const Incidence::Ptr &incidence)
180 {
181  d->compat->fixEmptySummary(incidence);
182 }
183 
184 void CompatDecorator::fixAlarms(const Incidence::Ptr &incidence)
185 {
186  d->compat->fixAlarms(incidence);
187 }
188 
189 void CompatDecorator::fixFloatingEnd(QDate &date)
190 {
191  d->compat->fixFloatingEnd(date);
192 }
193 
194 void CompatDecorator::fixRecurrence(const Incidence::Ptr &incidence)
195 {
196  d->compat->fixRecurrence(incidence);
197 }
198 
199 int CompatDecorator::fixPriority(int priority)
200 {
201  return d->compat->fixPriority(priority);
202 }
203 
204 bool CompatDecorator::useTimeZoneShift()
205 {
206  return d->compat->useTimeZoneShift();
207 }
208 
209 void CompatDecorator::setCreatedToDtStamp(const Incidence::Ptr &incidence,
210  const KDateTime &dtstamp)
211 {
212  d->compat->setCreatedToDtStamp(incidence, dtstamp);
213 }
214 
215 void CompatPre35::fixRecurrence(const Incidence::Ptr &incidence)
216 {
217  Recurrence *recurrence = incidence->recurrence();
218  if (recurrence) {
219  KDateTime start(incidence->dtStart());
220  // kde < 3.5 only had one rrule, so no need to loop over all RRULEs.
221  RecurrenceRule *r = recurrence->defaultRRule();
222  if (r && !r->dateMatchesRules(start)) {
223  recurrence->addExDateTime(start);
224  }
225  }
226 
227  // Call base class method now that everything else is done
228  Compat::fixRecurrence(incidence);
229 }
230 
231 int CompatPre34::fixPriority(int priority)
232 {
233  if (0 < priority && priority < 6) {
234  // adjust 1->1, 2->3, 3->5, 4->7, 5->9
235  return 2 * priority - 1;
236  } else {
237  return priority;
238  }
239 }
240 
241 void CompatPre32::fixRecurrence(const Incidence::Ptr &incidence)
242 {
243  Recurrence *recurrence = incidence->recurrence();
244  if (recurrence->recurs() && recurrence->duration() > 0) {
245  recurrence->setDuration(recurrence->duration() + incidence->recurrence()->exDates().count());
246  }
247  // Call base class method now that everything else is done
248  CompatPre35::fixRecurrence(incidence);
249 }
250 
251 void CompatPre31::fixFloatingEnd(QDate &endDate)
252 {
253  endDate = endDate.addDays(1);
254 }
255 
256 void CompatPre31::fixRecurrence(const Incidence::Ptr &incidence)
257 {
258  CompatPre32::fixRecurrence(incidence);
259 
260  Recurrence *recur = incidence->recurrence();
261  RecurrenceRule *r = 0;
262  if (recur) {
263  r = recur->defaultRRule();
264  }
265  if (recur && r) {
266  int duration = r->duration();
267  if (duration > 0) {
268  // Backwards compatibility for KDE < 3.1.
269  // rDuration was set to the number of time periods to recur,
270  // with week start always on a Monday.
271  // Convert this to the number of occurrences.
272  r->setDuration(-1);
273  QDate end(r->startDt().date());
274  bool doNothing = false;
275  // # of periods:
276  int tmp = (duration - 1) * r->frequency();
277  switch (r->recurrenceType()) {
278  case RecurrenceRule::rWeekly:
279  {
280  end = end.addDays(tmp * 7 + 7 - end.dayOfWeek());
281  break;
282  }
283  case RecurrenceRule::rMonthly:
284  {
285  int month = end.month() - 1 + tmp;
286  end.setYMD(end.year() + month / 12, month % 12 + 1, 31);
287  break;
288  }
289  case RecurrenceRule::rYearly:
290  {
291  end.setYMD(end.year() + tmp, 12, 31);
292  break;
293  }
294  default:
295  doNothing = true;
296  break;
297  }
298  if (!doNothing) {
299  duration = r->durationTo(
300  KDateTime(end, QTime(0, 0, 0), incidence->dtStart().timeSpec()));
301  r->setDuration(duration);
302  }
303  }
304 
305  /* addYearlyNum */
306  // Dates were stored as day numbers, with a fiddle to take account of
307  // leap years. Convert the day number to a month.
308  QList<int> days = r->byYearDays();
309  if (!days.isEmpty()) {
310  QList<int> months = r->byMonths();
311  for (int i = 0; i < months.size(); ++i) {
312  int newmonth =
313  QDate(r->startDt().date().year(), 1, 1).addDays(months.at(i) - 1).month();
314  if (!months.contains(newmonth)) {
315  months.append(newmonth);
316  }
317  }
318 
319  r->setByMonths(months);
320  days.clear();
321  r->setByYearDays(days);
322  }
323  }
324 }
325 
326 void CompatOutlook9::fixAlarms(const Incidence::Ptr &incidence)
327 {
328  if (!incidence) {
329  return;
330  }
331  Alarm::List alarms = incidence->alarms();
332  Alarm::List::Iterator it;
333  for (it = alarms.begin(); it != alarms.end(); ++it) {
334  Alarm::Ptr al = *it;
335  if (al && al->hasStartOffset()) {
336  Duration offsetDuration = al->startOffset();
337  int offs = offsetDuration.asSeconds();
338  if (offs > 0) {
339  offsetDuration = Duration(-offs);
340  }
341  al->setStartOffset(offsetDuration);
342  }
343  }
344 }
345 
346 bool Compat32PrereleaseVersions::useTimeZoneShift()
347 {
348  return false;
349 }
350 
351 CompatPre410::CompatPre410(Compat *decoratedCompat)
352  : CompatDecorator(decoratedCompat)
353 {
354 }
355 
356 void CompatPre410::setCreatedToDtStamp(const Incidence::Ptr &incidence, const KDateTime &dtstamp)
357 {
358  if (dtstamp.isValid()) {
359  incidence->setCreated(dtstamp);
360  }
361 }
QList::clear
void clear()
KCalCore::Compat::setCreatedToDtStamp
virtual void setCreatedToDtStamp(const Incidence::Ptr &incidence, const KDateTime &dtstamp)
Sets the created and dtstamp.
Definition: compat.cpp:156
QString::indexOf
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
KCalCore::Compat::useTimeZoneShift
virtual bool useTimeZoneShift()
Returns true if a timezone shift should be used; false otherwise.
Definition: compat.cpp:151
QVector::Iterator
typedef Iterator
KCalCore::CompatDecorator::fixEmptySummary
virtual void fixEmptySummary(const Incidence::Ptr &incidence)
Definition: compat.cpp:179
KCalCore::CompatDecorator::setCreatedToDtStamp
virtual void setCreatedToDtStamp(const Incidence::Ptr &incidence, const KDateTime &dtstamp)
Definition: compat.cpp:209
QVector::begin
iterator begin()
KCalCore::Compat::fixFloatingEnd
virtual void fixFloatingEnd(QDate &date)
Fixes the end date for floating events.
Definition: compat.cpp:134
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:108
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:356
QList::at
const T & at(int i) const
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:204
KCalCore::CompatPre31::fixRecurrence
virtual void fixRecurrence(const Incidence::Ptr &incidence)
Definition: compat.cpp:256
KCalCore::CompatDecorator::fixAlarms
virtual void fixAlarms(const Incidence::Ptr &incidence)
Definition: compat.cpp:184
QString::remove
QString & remove(int position, int n)
KCalCore::CompatDecorator::fixFloatingEnd
virtual void fixFloatingEnd(QDate &date)
Definition: compat.cpp:189
QTime
QList::size
int size() const
KCalCore::Compat
This class provides compatibility to older or broken calendar files.
Definition: compat.h:71
QRegExp
KCalCore::CompatPre32
Compatibility class for KOrganizer pre-3.2 calendar files.
Definition: compat.h:251
QList::append
void append(const T &value)
QSharedPointer
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:146
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:112
QString::toInt
int toInt(bool *ok, int base) const
QList::isEmpty
bool isEmpty() const
KCalCore::CompatPre32::fixRecurrence
virtual void fixRecurrence(const Incidence::Ptr &incidence)
Definition: compat.cpp:241
QString::isEmpty
bool isEmpty() const
KCalCore::CompatPre34::fixPriority
virtual int fixPriority(int priority)
Definition: compat.cpp:231
QDate
QString
QList
KCalCore::CompatDecorator::fixRecurrence
virtual void fixRecurrence(const Incidence::Ptr &incidence)
Definition: compat.cpp:194
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:346
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
QString::contains
bool contains(QChar ch, Qt::CaseSensitivity cs) const
QLatin1Char
compat.h
This file is part of the API for handling calendar data and defines classes for managing compatibilit...
QList::contains
bool contains(const T &value) const
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:129
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
QString::mid
QString mid(int position, int n) const
QVector
QLatin1String
KCalCore::CompatPre35::fixRecurrence
virtual void fixRecurrence(const Incidence::Ptr &incidence)
Definition: compat.cpp:215
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:199
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:326
QString::section
QString section(QChar sep, int start, int end, QFlags< QString::SectionFlag > flags) const
QDate::addDays
QDate addDays(int ndays) const
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:139
QVector::end
iterator end()
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:251
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-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:36:53 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
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2

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