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

kontact

  • sources
  • kde-4.14
  • 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 <KLocalizedString>
38 #include <KSystemTimeZones>
39 #include <KLocale>
40 
41 #include <QDate>
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  KCalCore::Calendar *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 ( ( end >= eventStart.date() && start <= eventEnd.date() ) ||
160  ( start >= 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  /*
271  Commented out because a ETMCalendar doesn't have any name, it's a group of selected
272  calendars, not an individual one.
273 
274  QString tipText( KCalUtils::IncidenceFormatter::toolTipStr(
275  KCalUtils::IncidenceFormatter::resourceString(
276  calendar, ev ), ev, start, true, spec ) );
277  if ( !tipText.isEmpty() ) {
278  summaryEvent->summaryTooltip = tipText;
279  }*/
280 
281  // Time range label (only for non-floating events)
282  str.clear();
283  if ( !ev->allDay() ) {
284  QTime sST = eventStart.time();
285  QTime sET = eventEnd.time();
286  if ( ev->isMultiDay() ) {
287  if ( eventStart.date() < start ) {
288  sST = QTime( 0, 0 );
289  }
290  if ( eventEnd.date() > end ) {
291  sET = QTime( 23, 59 );
292  }
293  }
294  str = i18nc( "Time from - to", "%1 - %2",
295  KGlobal::locale()->formatTime( sST ),
296  KGlobal::locale()->formatTime( sET ) );
297  summaryEvent->timeRange = str;
298  }
299 
300  // For recurring events, append the next occurrence to the time range label
301  if ( ev->recurs() ) {
302  KDateTime kdt( start, QTime( 0, 0, 0 ), spec );
303  kdt = kdt.addSecs( -1 );
304  KDateTime next = ev->recurrence()->getNextDateTime( kdt );
305  QString tmp = IncidenceFormatter::dateTimeToString(
306  ev->recurrence()->getNextDateTime( next ), ev->allDay(),
307  true, spec );
308  if ( !summaryEvent->timeRange.isEmpty() ) {
309  summaryEvent->timeRange += QLatin1String("<br>");
310  }
311  summaryEvent->timeRange += QLatin1String("<font size=\"small\"><i>") +
312  i18nc( "next occurrence", "Next: %1", tmp ) +
313  QLatin1String("</i></font>");
314  }
315  }
316 
317  return eventInfoList;
318 }
319 
320 SummaryEventInfo::List SummaryEventInfo::eventsForDate( const QDate &date,
321  KCalCore::Calendar *calendar )
322 {
323  return eventsForRange(date, date, calendar);
324 }
QDate::daysTo
int daysTo(const QDate &d) const
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
QString::append
QString & append(QChar ch)
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
QStringList::contains
bool contains(const QString &str, Qt::CaseSensitivity cs) const
QDate::month
int month() const
SummaryEventInfo::startDate
QString startDate
Definition: summaryeventinfo.h:52
QTime
QString::clear
void clear()
QHash
QString::isEmpty
bool isEmpty() const
QDate::day
int day() const
SummaryEventInfo::ev
KCalCore::Event::Ptr ev
Definition: summaryeventinfo.h:51
QDate
QDate::year
int year() const
QString
QList
QStringList
QLatin1Char
SummaryEventInfo::dateSpan
QString dateSpan
Definition: summaryeventinfo.h:53
QDate::isLeapYear
bool isLeapYear(int year)
SummaryEventInfo::eventsForRange
static List eventsForRange(const QDate &start, const QDate &end, KCalCore::Calendar *calendar)
static
Definition: summaryeventinfo.cpp:134
SummaryEventInfo::daysToGo
QString daysToGo
Definition: summaryeventinfo.h:54
QLatin1String
summaryeventinfo.h
QDate::currentDate
QDate currentDate()
QString::fromLatin1
QString fromLatin1(const char *str, int size)
SummaryEventInfo::SummaryEventInfo
SummaryEventInfo()
Definition: summaryeventinfo.cpp:128
SummaryEventInfo::makeBold
bool makeBold
Definition: summaryeventinfo.h:59
QDate::addDays
QDate addDays(int ndays) const
SummaryEventInfo::timeRange
QString timeRange
Definition: summaryeventinfo.h:55
SummaryEventInfo::eventsForDate
static List eventsForDate(const QDate &date, KCalCore::Calendar *calendar)
Definition: summaryeventinfo.cpp:320
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:34:11 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
  • pimprint

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