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

kontact

  • sources
  • kde-4.12
  • kdepim
  • kontact
  • plugins
  • korganizer
summaryeventinfo.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Kontact.
3 
4  Copyright (c) 2003 Tobias Koenig <tokoe@kde.org>
5  Copyright (c) 2005-2006,2008-2009 Allen Winter <winter@kde.org>
6  Copyright (c) 2008 Thomas McGuire <mcguire@kde.org>
7 
8  This program is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12 
13  This program 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
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License along
19  with this program; if not, write to the Free Software Foundation, Inc.,
20  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 
22  As a special exception, permission is given to link this program
23  with any edition of Qt, and distribute the resulting executable,
24  without including the source code for Qt in the source distribution.
25 */
26 
27 #include "summaryeventinfo.h"
28 
29 #include <KCalCore/Calendar>
30 #include <KCalCore/Event>
31 using namespace KCalCore;
32 
33 #include <KCalUtils/IncidenceFormatter>
34 using namespace KCalUtils;
35 
36 #include <KGlobal>
37 #include <KLocale>
38 #include <KSystemTimeZones>
39 
40 #include <QDate>
41 #include <QDebug>
42 #include <QStringList>
43 
44 bool SummaryEventInfo::mShowBirthdays = true;
45 bool SummaryEventInfo::mShowAnniversaries = true;
46 
47 static QHash<QString, KDateTime> sDateTimeByUid;
48 
49 static bool eventLessThan(const KCalCore::Event::Ptr &event1, const KCalCore::Event::Ptr &event2)
50 {
51  KDateTime kdt1 = sDateTimeByUid.value(event1->instanceIdentifier());
52  KDateTime kdt2 = sDateTimeByUid.value(event2->instanceIdentifier());
53  if (kdt1.date() < kdt2.date()) { // Compare dates first since comparing all day with non-all-day doesn't work
54  return true;
55  } else if (kdt1.date() > kdt2.date()) {
56  return false;
57  } else {
58  if (kdt1.isDateOnly() && !kdt2.isDateOnly()) {
59  return false;
60  } else if (!kdt1.isDateOnly() && kdt2.isDateOnly()) {
61  return true;
62  } else {
63  if (kdt1 > kdt2) {
64  return true;
65  } else if (kdt1 < kdt2) {
66  return false;
67  } else {
68  return event1->summary() > event2->summary();
69  }
70  }
71  }
72 }
73 
74 void SummaryEventInfo::setShowSpecialEvents( bool showBirthdays,
75  bool showAnniversaries )
76 {
77  mShowBirthdays = showBirthdays;
78  mShowAnniversaries = showAnniversaries;
79 }
80 
81 bool SummaryEventInfo::skip( const KCalCore::Event::Ptr &event )
82 {
83  //simply check categories because the birthdays resource always adds
84  //the appropriate category to the event.
85  QStringList c = event->categories();
86  if ( !mShowBirthdays &&
87  c.contains( QLatin1String("BIRTHDAY"), Qt::CaseInsensitive ) ) {
88  return true;
89  }
90  if ( !mShowAnniversaries &&
91  c.contains( QLatin1String("ANNIVERSARY"), Qt::CaseInsensitive ) ) {
92  return true;
93  }
94 
95  return false;
96 }
97 
98 void SummaryEventInfo::dateDiff( const QDate &date, int &days )
99 {
100  QDate currentDate;
101  QDate eventDate;
102 
103  if ( QDate::isLeapYear( date.year() ) && date.month() == 2 && date.day() == 29 ) {
104  currentDate = QDate( date.year(), QDate::currentDate().month(), QDate::currentDate().day() );
105  if ( !QDate::isLeapYear( QDate::currentDate().year() ) ) {
106  eventDate = QDate( date.year(), date.month(), 28 ); // celebrate one day earlier ;)
107  } else {
108  eventDate = QDate( date.year(), date.month(), date.day() );
109  }
110  } else {
111  currentDate = QDate( QDate::currentDate().year(),
112  QDate::currentDate().month(),
113  QDate::currentDate().day() );
114  eventDate = QDate( QDate::currentDate().year(), date.month(), date.day() );
115  }
116 
117  int offset = currentDate.daysTo( eventDate );
118  if ( offset < 0 ) {
119  days = 365 + offset;
120  if ( QDate::isLeapYear( QDate::currentDate().year() ) ) {
121  days++;
122  }
123  } else {
124  days = offset;
125  }
126 }
127 
128 SummaryEventInfo::SummaryEventInfo()
129  : makeBold( false )
130 {
131 }
132 
134 SummaryEventInfo::List SummaryEventInfo::eventsForRange( const QDate &start, const QDate &end,
135  const KCalCore::Calendar::Ptr &calendar )
136 {
137  KCalCore::Event::List allEvents = calendar->events(); // calendar->rawEvents() isn't exactly what we want, doesn't handle recurrence right
138  KCalCore::Event::List events;
139  KDateTime::Spec spec = KSystemTimeZones::local();
140  const KDateTime currentDateTime = KDateTime::currentDateTime( spec );
141  const QDate currentDate = currentDateTime.date();
142 
143  sDateTimeByUid.clear();
144 
145  for (int i=0; i<allEvents.count(); ++i) {
146  KCalCore::Event::Ptr event = allEvents.at(i);
147  if (skip(event))
148  continue;
149 
150  KDateTime eventStart = event->dtStart().toTimeSpec(spec);
151  KDateTime eventEnd = event->dtEnd().toTimeSpec(spec);
152  if (event->recurs()) {
153  KCalCore::DateTimeList occurrences = event->recurrence()->timesInInterval(KDateTime(start, spec), KDateTime(end, spec));
154  if (!occurrences.isEmpty()) {
155  events << event;
156  sDateTimeByUid.insert(event->instanceIdentifier(), occurrences.first());
157  }
158  } else {
159  if ( ( start >= eventStart.date() && start <= eventEnd.date() ) ||
160  ( end >= eventStart.date() && end <= eventEnd.date() ) ) {
161  events << event;
162  if ( eventStart.date() < start ) {
163  sDateTimeByUid.insert(event->instanceIdentifier(), KDateTime(start,spec));
164  } else {
165  sDateTimeByUid.insert(event->instanceIdentifier(), eventStart);
166  }
167  }
168  }
169  }
170 
171  qSort(events.begin(), events.end(), eventLessThan);
172 
173  SummaryEventInfo::List eventInfoList;
174  KCalCore::Event::Ptr ev;
175  KCalCore::Event::List::ConstIterator itEnd = events.constEnd();
176  for ( KCalCore::Event::List::ConstIterator it=events.constBegin(); it != itEnd; ++it ) {
177  ev = *it;
178  // Count number of days remaining in multiday event
179  int span = 1;
180  int dayof = 1;
181  const KDateTime eventStart = ev->dtStart().toTimeSpec(spec);
182  const KDateTime eventEnd = ev->dtEnd().toTimeSpec(spec);
183  const QDate occurrenceStartDate = sDateTimeByUid.value(ev->instanceIdentifier()).date();
184 
185  QDate startOfMultiday = eventStart.date();
186  if ( startOfMultiday < currentDate ) {
187  startOfMultiday = currentDate;
188  }
189  bool firstDayOfMultiday = ( start == startOfMultiday );
190 
191  SummaryEventInfo *summaryEvent = new SummaryEventInfo();
192  eventInfoList.append( summaryEvent );
193 
194  // Event
195  summaryEvent->ev = ev;
196 
197  // Start date label
198  QString str;
199  QDate sD = occurrenceStartDate;
200  if (currentDate >= sD) {
201  str = i18nc( "the appointment is today", "Today" );
202  summaryEvent->makeBold = true;
203  } else if ( sD == currentDate.addDays(1) ) {
204  str = i18nc( "the appointment is tomorrow", "Tomorrow" );
205  } else {
206  str = KGlobal::locale()->formatDate( sD, KLocale::FancyLongDate );
207  }
208  summaryEvent->startDate = str;
209 
210  if ( ev->isMultiDay() ) {
211  dayof = eventStart.date().daysTo(start)+1;
212  dayof = dayof <= 0 ? 1 : dayof;
213  span = eventStart.date().daysTo(eventEnd.date()) + 1;
214  }
215  // Print the date span for multiday, floating events, for the
216  // first day of the event only.
217  if ( ev->isMultiDay() && ev->allDay() && firstDayOfMultiday && span > 1 ) {
218  str = IncidenceFormatter::dateToString( ev->dtStart(), false, spec ) +
219  QLatin1String(" -\n ") +
220  IncidenceFormatter::dateToString( ev->dtEnd(), false, spec );
221  }
222  summaryEvent->dateSpan = str;
223 
224  // Days to go label
225  str.clear();
226  const int daysTo = currentDate.daysTo(occurrenceStartDate);
227  if ( daysTo > 0 ) {
228  str = i18np( "in 1 day", "in %1 days", daysTo );
229  } else {
230  if ( !ev->allDay() ) {
231  int secs;
232  if ( !ev->recurs() ) {
233  secs = currentDateTime.secsTo( ev->dtStart() );
234  } else {
235  KDateTime kdt( start, QTime( 0, 0, 0 ), spec );
236  kdt = kdt.addSecs( -1 );
237  KDateTime next = ev->recurrence()->getNextDateTime( kdt );
238  secs = currentDateTime.secsTo( next );
239  }
240  if ( secs > 0 ) {
241  str = i18nc( "eg. in 1 hour 2 minutes", "in " );
242  int hours = secs / 3600;
243  if ( hours > 0 ) {
244  str += i18ncp( "use abbreviation for hour to keep the text short",
245  "1 hr", "%1 hrs", hours );
246  str += QLatin1Char(' ');
247  secs -= ( hours * 3600 );
248  }
249  int mins = secs / 60;
250  if ( mins > 0 ) {
251  str += i18ncp( "use abbreviation for minute to keep the text short",
252  "1 min", "%1 mins", mins );
253  }
254  } else {
255  str = i18n( "now" );
256  }
257  } else {
258  str = i18n( "all day" );
259  }
260  }
261  summaryEvent->daysToGo = str;
262 
263  // Summary label
264  str = ev->richSummary();
265  if ( ev->isMultiDay() && !ev->allDay() ) {
266  str.append( QString::fromLatin1( " (%1/%2)" ).arg( dayof ).arg( span ) );
267  }
268  summaryEvent->summaryText = str;
269  summaryEvent->summaryUrl = ev->uid();
270  QString tipText( KCalUtils::IncidenceFormatter::toolTipStr(
271  KCalUtils::IncidenceFormatter::resourceString(
272  calendar, ev ), ev, start, true, spec ) );
273  if ( !tipText.isEmpty() ) {
274  summaryEvent->summaryTooltip = tipText;
275  }
276 
277  // Time range label (only for non-floating events)
278  str.clear();
279  if ( !ev->allDay() ) {
280  QTime sST = eventStart.time();
281  QTime sET = eventEnd.time();
282  if ( ev->isMultiDay() ) {
283  if ( eventStart.date() < start ) {
284  sST = QTime( 0, 0 );
285  }
286  if ( eventEnd.date() > end ) {
287  sET = QTime( 23, 59 );
288  }
289  }
290  str = i18nc( "Time from - to", "%1 - %2",
291  KGlobal::locale()->formatTime( sST ),
292  KGlobal::locale()->formatTime( sET ) );
293  summaryEvent->timeRange = str;
294  }
295 
296  // For recurring events, append the next occurrence to the time range label
297  if ( ev->recurs() ) {
298  KDateTime kdt( start, QTime( 0, 0, 0 ), spec );
299  kdt = kdt.addSecs( -1 );
300  KDateTime next = ev->recurrence()->getNextDateTime( kdt );
301  QString tmp = IncidenceFormatter::dateTimeToString(
302  ev->recurrence()->getNextDateTime( next ), ev->allDay(),
303  true, spec );
304  if ( !summaryEvent->timeRange.isEmpty() ) {
305  summaryEvent->timeRange += QLatin1String("<br>");
306  }
307  summaryEvent->timeRange += QLatin1String("<font size=\"small\"><i>") +
308  i18nc( "next occurrence", "Next: %1", tmp ) +
309  QLatin1String("</i></font>");
310  }
311  }
312 
313  return eventInfoList;
314 }
315 
316 SummaryEventInfo::List SummaryEventInfo::eventsForDate( const QDate &date,
317  const KCalCore::Calendar::Ptr &calendar )
318 {
319  return eventsForRange(date, date, calendar);
320 }
eventLessThan
static bool eventLessThan(const KCalCore::Event::Ptr &event1, const KCalCore::Event::Ptr &event2)
Definition: summaryeventinfo.cpp:49
SummaryEventInfo::summaryUrl
QString summaryUrl
Definition: summaryeventinfo.h:57
SummaryEventInfo
Definition: summaryeventinfo.h:37
sDateTimeByUid
static QHash< QString, KDateTime > sDateTimeByUid
Definition: summaryeventinfo.cpp:47
SummaryEventInfo::setShowSpecialEvents
static void setShowSpecialEvents(bool skipBirthdays, bool skipAnniversaries)
Definition: summaryeventinfo.cpp:74
SummaryEventInfo::summaryText
QString summaryText
Definition: summaryeventinfo.h:56
date
time_t date() const
SummaryEventInfo::summaryTooltip
QString summaryTooltip
Definition: summaryeventinfo.h:58
SummaryEventInfo::startDate
QString startDate
Definition: summaryeventinfo.h:52
SummaryEventInfo::eventsForRange
static List eventsForRange(const QDate &start, const QDate &end, const KCalCore::Calendar::Ptr &calendar)
static
Definition: summaryeventinfo.cpp:134
SummaryEventInfo::ev
KCalCore::Event::Ptr ev
Definition: summaryeventinfo.h:51
SummaryEventInfo::dateSpan
QString dateSpan
Definition: summaryeventinfo.h:53
SummaryEventInfo::eventsForDate
static List eventsForDate(const QDate &date, const KCalCore::Calendar::Ptr &calendar)
Definition: summaryeventinfo.cpp:316
SummaryEventInfo::daysToGo
QString daysToGo
Definition: summaryeventinfo.h:54
summaryeventinfo.h
SummaryEventInfo::SummaryEventInfo
SummaryEventInfo()
Definition: summaryeventinfo.cpp:128
SummaryEventInfo::makeBold
bool makeBold
Definition: summaryeventinfo.h:59
SummaryEventInfo::timeRange
QString timeRange
Definition: summaryeventinfo.h:55
QList
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:58:30 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kontact

Skip menu "kontact"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

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