KCalendarCore

event.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the kcalcore library.
3 
4  SPDX-FileCopyrightText: 2001 Cornelius Schumacher <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-or-later
7 */
19 #include "event.h"
20 #include "visitor.h"
21 #include "utils_p.h"
22 #include "kcalendarcore_debug.h"
23 
24 #include <QDate>
25 
26 using namespace KCalendarCore;
27 
32 //@cond PRIVATE
33 class Q_DECL_HIDDEN KCalendarCore::Event::Private
34 {
35 public:
36  QDateTime mDtEnd;
37  Transparency mTransparency = Opaque;
38  bool mMultiDayValid = false;
39  bool mMultiDay = false;
40 };
41 //@endcond
42 
44  : d(new KCalendarCore::Event::Private)
45 {
46 }
47 
48 Event::Event(const Event &other)
49  : Incidence(other), d(new KCalendarCore::Event::Private(*other.d))
50 {
51 }
52 
53 Event::Event(const Incidence &other)
54  : Incidence(other)
55  , d(new KCalendarCore::Event::Private)
56 {
57 }
58 
60 {
61  delete d;
62 }
63 
65 {
66  return new Event(*this);
67 }
68 
70 {
71  if (&other != this) {
72  Incidence::assign(other);
73  const Event *e = static_cast<const Event *>(&other);
74  *d = *(e->d);
75  }
76  return *this;
77 }
78 
79 bool Event::equals(const IncidenceBase &event) const
80 {
81  if (!Incidence::equals(event)) {
82  return false;
83  } else {
84  // If they weren't the same type IncidenceBase::equals would had returned false already
85  const Event *e = static_cast<const Event *>(&event);
86  return
87  ((dtEnd() == e->dtEnd()) ||
88  (!dtEnd().isValid() && !e->dtEnd().isValid())) &&
89  transparency() == e->transparency();
90  }
91 }
92 
94 {
95  return TypeEvent;
96 }
97 
99 {
100  return QByteArrayLiteral("Event");
101 }
102 
104 {
105  d->mMultiDayValid = false;
107 }
108 
110 {
111  if (mReadOnly) {
112  return;
113  }
114 
115  if (d->mDtEnd != dtEnd || hasDuration() == dtEnd.isValid()) {
116  update();
117  d->mDtEnd = dtEnd;
118  d->mMultiDayValid = false;
119  setHasDuration(!dtEnd.isValid());
121  updated();
122  }
123 }
124 
125 QDateTime Event::dtEnd() const
126 {
127  if (d->mDtEnd.isValid()) {
128  return d->mDtEnd;
129  }
130 
131  if (hasDuration()) {
132  if (allDay()) {
133  // For all day events, dtEnd is always inclusive
134  QDateTime end = duration().end(dtStart().addDays(-1));
135  return end >= dtStart() ? end : dtStart();
136  } else {
137  return duration().end(dtStart());
138  }
139  }
140 
141  // It is valid for a VEVENT to be without a DTEND. See RFC2445, Sect4.6.1.
142  // Be careful to use Event::dateEnd() as appropriate due to this possibility.
143  return dtStart();
144 }
145 
147 {
148  QDateTime end = dtEnd().toTimeZone(dtStart().timeZone());
149  if (allDay()) {
150  return end.date();
151  } else {
152  return end.addSecs(-1).date();
153  }
154 }
155 
156 bool Event::hasEndDate() const
157 {
158  return d->mDtEnd.isValid();
159 }
160 
161 bool Event::isMultiDay(const QTimeZone &zone) const
162 {
163  // First off, if spec's not valid, we can check for cache
164  if (!zone.isValid() && d->mMultiDayValid) {
165  return d->mMultiDay;
166  }
167 
168  // Not in cache -> do it the hard way
169  QDateTime start, end;
170 
171  if (!zone.isValid()) {
172  start = dtStart();
173  end = dtEnd();
174  } else {
175  start = dtStart().toTimeZone(zone);
176  end = dtEnd().toTimeZone(zone);
177  }
178 
179  bool multi = (start < end && start.date() != end.date());
180 
181  // End date is non inclusive
182  // If we have an incidence that duration is one day and ends with a start of a new day
183  // than it is not a multiday event
184  if (multi && end.time() == QTime(0, 0, 0)) {
185  multi = start.daysTo(end) > 1;
186  }
187 
188  // Update the cache
189  // Also update Cache if spec is invalid
190  d->mMultiDayValid = true;
191  d->mMultiDay = multi;
192  return multi;
193 }
194 
195 void Event::shiftTimes(const QTimeZone &oldZone, const QTimeZone &newZone)
196 {
197  Incidence::shiftTimes(oldZone, newZone);
198  if (d->mDtEnd.isValid()) {
199  d->mDtEnd = d->mDtEnd.toTimeZone(oldZone);
200  d->mDtEnd.setTimeZone(newZone);
201  }
202 }
203 
205 {
206  if (mReadOnly) {
207  return;
208  }
209  update();
210  d->mTransparency = transparency;
212  updated();
213 }
214 
216 {
217  return d->mTransparency;
218 }
219 
221 {
222  setDtEnd(QDateTime());
223  Incidence::setDuration(duration);
224 }
225 
226 void Event::setAllDay(bool allday)
227 {
228  if (allday != allDay() && !mReadOnly) {
230  Incidence::setAllDay(allday);
231  }
232 }
233 
234 bool Event::accept(Visitor &v, const IncidenceBase::Ptr &incidence)
235 {
236  return v.visit(incidence.staticCast<Event>());
237 }
238 
240 {
241  switch (role) {
242  case RoleRecurrenceStart:
244  case RoleStartTimeZone:
245  case RoleSort:
246  return dtStart();
247  case RoleCalendarHashing:
248  return !recurs() && !isMultiDay() ? dtStart() : QDateTime();
249  case RoleAlarmEndOffset:
250  case RoleEndTimeZone:
251  case RoleEndRecurrenceBase:
252  case RoleEnd:
253  case RoleDisplayEnd:
254  return dtEnd();
255  case RoleDisplayStart:
256  return dtStart();
257  case RoleAlarm:
258  if (alarms().isEmpty()) {
259  return QDateTime();
260  } else {
261  Alarm::Ptr alarm = alarms().at(0);
262  return alarm->hasStartOffset() ? dtStart() : dtEnd();
263  }
264  default:
265  return QDateTime();
266  }
267 }
268 
270 {
271  switch (role) {
272  case RoleDnD: {
273  const qint64 duration = dtStart().secsTo(dtEnd());
274 
275  setDtStart(dateTime);
276  setDtEnd(dateTime.addSecs(duration <= 0 ? 3600 : duration));
277  break;
278  }
279  case RoleEnd:
280  setDtEnd(dateTime);
281  break;
282  default:
283  qCDebug(KCALCORE_LOG) << "Unhandled role" << role;
284  }
285 }
286 
287 void Event::virtual_hook(VirtualHook id, void *data)
288 {
289  Q_UNUSED(id);
290  Q_UNUSED(data);
291 }
292 
294 {
295  return Event::eventMimeType();
296 }
297 
299 {
300  return QLatin1String("application/x-vnd.akonadi.calendar.event");
301 }
302 
304 {
305  return QLatin1String("view-calendar-day");
306 }
307 
308 void Event::serialize(QDataStream &out) const
309 {
311  serializeQDateTimeAsKDateTime(out, d->mDtEnd);
312  out << hasEndDate() << static_cast<quint32>(d->mTransparency) << d->mMultiDayValid << d->mMultiDay;
313 }
314 
315 void Event::deserialize(QDataStream &in)
316 {
318  bool hasEndDateDummy = true;
319  deserializeKDateTimeAsQDateTime(in, d->mDtEnd);
320  in >> hasEndDateDummy;
321  quint32 transp;
322  in >> transp;
323  d->mTransparency = static_cast<Transparency>(transp);
324  in >> d->mMultiDayValid >> d->mMultiDay;
325 }
326 
328 {
329  return true;
330 }
Transparency transparency() const
Returns the event&#39;s time transparency level.
void shiftTimes(const QTimeZone &oldZone, const QTimeZone &newZone) override
Definition: event.cpp:195
Alarm::List alarms() const
Returns a list of all incidence alarms.
Definition: incidence.cpp:827
QLatin1String mimeType() const override
Definition: event.cpp:293
Role for determining new start and end dates after a DnD.
void update()
Call this to notify the observers after the IncidenceBase object will be changed. ...
void setDateTime(const QDateTime &dateTime, DateTimeRole role) override
Definition: event.cpp:269
This class provides the interface for a visitor of calendar components.
Definition: visitor.h:31
bool hasDuration() const
Returns true if the incidence has a duration; false otherwise.
void serialize(QDataStream &out) const override
Sub-type specific serialization.
Definition: incidence.cpp:1073
bool supportsGroupwareCommunication() const override
Definition: event.cpp:327
qint64 daysTo(const QDateTime &other) const const
virtual QDateTime dtEnd() const
Returns the event end date and time.
QDateTime end(const QDateTime &start) const
Computes a duration end time by adding the number of seconds or days in the duration to the specified...
Definition: duration.cpp:172
bool recurs() const
Returns whether the event recurs at all.
Definition: incidence.cpp:575
void setDtStart(const QDateTime &dt) override
Sets the incidence starting date/time.
Definition: event.cpp:103
static QLatin1String eventMimeType()
Returns the Akonadi specific sub MIME type of a KCalendarCore::Event.
Definition: event.cpp:298
QTime time() const const
QLatin1String iconName(const QDateTime &recurrenceId={}) const override
Definition: event.cpp:303
QDateTime toTimeZone(const QTimeZone &timeZone) const const
QDate dateEnd() const
Returns the date when the event ends.
Definition: event.cpp:146
Role for determining the start of the recurrence.
DateTimeRole
The different types of incidence date/times roles.
Field representing the STATUS component.
virtual void setDuration(const Duration &duration)
Sets the incidence duration.
An abstract class that provides a common base for all calendar incidence classes. ...
Definition: incidencebase.h:96
Role for an incidence&#39;s date/time used when sorting.
void setAllDay(bool allDay) override
Definition: incidence.cpp:338
bool equals(const IncidenceBase &event) const override
Compares two events for equality.
Definition: event.cpp:79
Represents a span of time measured in seconds or days.
Definition: duration.h:44
Role for determining an incidence&#39;s starting timezone.
Duration duration() const
Returns the length of the incidence duration.
Role for determining an incidence&#39;s ending timezone.
This class provides an Event in the sense of RFC2445.
Definition: event.h:30
void setHasDuration(bool hasDuration)
Sets if the incidence has a duration.
Role for looking up an incidence in a Calendar.
QDateTime dateTime(DateTimeRole role) const override
Definition: event.cpp:239
void shiftTimes(const QTimeZone &oldZone, const QTimeZone &newZone) override
Shift the times of the incidence so that they appear at the same clock time as before but in a new ti...
Definition: incidence.cpp:396
Role for determining the date/time of the first alarm.
Role for an incidence alarm&#39;s starting offset date/time.
IncidenceType
The different types of incidences, per RFC2445.
void setDtStart(const QDateTime &dt) override
Sets the incidence starting date/time.
Definition: incidence.cpp:388
void setDtEnd(const QDateTime &dtEnd)
Sets the event end date and time.
Definition: event.cpp:109
Field representing the DTSTART component.
void updated()
Call this to notify the observers after the IncidenceBase object has changed.
IncidenceType type() const override
Definition: event.cpp:93
bool isValid() const const
Role for display purposes, represents the start boundary of an incidence.
Transparency
The different Event transparency types.
Definition: event.h:39
Event * clone() const override
Returns an exact copy of this Event.
Definition: event.cpp:64
const T & at(int i) const const
~Event() override
Destroys the event.
Definition: event.cpp:59
QDate date() const const
void setFieldDirty(IncidenceBase::Field field)
Marks Field field as dirty.
This file is part of the API for handling calendar data and defines the Event class.
qint64 secsTo(const QDateTime &other) const const
Event()
Constructs an event.
Definition: event.cpp:43
IncidenceBase & assign(const IncidenceBase &other) override
Definition: event.cpp:69
void deserialize(QDataStream &in) override
Sub-type specific deserialization.
Definition: incidence.cpp:1098
bool isMultiDay(const QTimeZone &zone={}) const
Returns true if the event spans multiple days, otherwise return false.
Definition: event.cpp:161
bool allDay() const
Returns true or false depending on whether the incidence is all-day.
Role for determining an incidence&#39;s dtEnd, will return an invalid QDateTime if the incidence does not...
Role used for display purposes, represents the end boundary if an incidence supports dtEnd...
bool isValid() const const
virtual bool visit(const Event::Ptr &event)
Reimplement this function in your concrete subclass of IncidenceBase::Visitor to perform actions on a...
Definition: visitor.cpp:29
void setDuration(const Duration &duration) override
Sets the duration of this event.
Definition: event.cpp:220
void virtual_hook(VirtualHook id, void *data) override
Definition: event.cpp:287
virtual QDateTime dtStart() const
Returns an incidence&#39;s starting date/time as a QDateTime.
void setAllDay(bool allDay) override
Definition: event.cpp:226
QDateTime addSecs(qint64 s) const const
Provides the abstract base class common to non-FreeBusy (Events, To-dos, Journals) calendar component...
Definition: incidence.h:56
QSharedPointer< X > staticCast() const const
bool hasEndDate() const
Returns whether the event has an end date/time.
Definition: event.cpp:156
IncidenceBase & assign(const IncidenceBase &other) override
Provides polymorfic assignment.
Definition: incidence.cpp:198
void setTransparency(Transparency transparency)
Sets the event&#39;s time transparency level.
Definition: event.cpp:204
bool equals(const IncidenceBase &incidence) const override
Compares this with Incidence incidence for equality.
Definition: incidence.cpp:211
Namespace for all KCalendarCore types.
Definition: alarm.h:36
Role for an incidence alarm&#39;s ending offset date/time.
QByteArray typeStr() const override
Definition: event.cpp:98
bool mReadOnly
Identifies a read-only incidence.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed Aug 5 2020 22:48:58 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.