29 #include <KCalCore/Calendar>
30 #include <KCalCore/Event>
31 using namespace KCalCore;
33 #include <KCalUtils/IncidenceFormatter>
34 using namespace KCalUtils;
38 #include <KSystemTimeZones>
42 #include <QStringList>
44 bool SummaryEventInfo::mShowBirthdays =
true;
45 bool SummaryEventInfo::mShowAnniversaries =
true;
49 static bool eventLessThan(
const KCalCore::Event::Ptr &event1,
const KCalCore::Event::Ptr &event2)
51 KDateTime kdt1 =
sDateTimeByUid.value(event1->instanceIdentifier());
52 KDateTime kdt2 =
sDateTimeByUid.value(event2->instanceIdentifier());
53 if (kdt1.date() < kdt2.date()) {
55 }
else if (kdt1.date() > kdt2.date()) {
58 if (kdt1.isDateOnly() && !kdt2.isDateOnly()) {
60 }
else if (!kdt1.isDateOnly() && kdt2.isDateOnly()) {
65 }
else if (kdt1 < kdt2) {
68 return event1->summary() > event2->summary();
75 bool showAnniversaries )
77 mShowBirthdays = showBirthdays;
78 mShowAnniversaries = showAnniversaries;
81 bool SummaryEventInfo::skip(
const KCalCore::Event::Ptr &event )
85 QStringList c =
event->categories();
86 if ( !mShowBirthdays &&
87 c.contains( QLatin1String(
"BIRTHDAY"), Qt::CaseInsensitive ) ) {
90 if ( !mShowAnniversaries &&
91 c.contains( QLatin1String(
"ANNIVERSARY"), Qt::CaseInsensitive ) ) {
98 void SummaryEventInfo::dateDiff(
const QDate &
date,
int &days )
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 );
108 eventDate = QDate( date.year(), date.month(), date.day() );
111 currentDate = QDate( QDate::currentDate().year(),
112 QDate::currentDate().month(),
113 QDate::currentDate().day() );
114 eventDate = QDate( QDate::currentDate().year(), date.month(), date.day() );
117 int offset = currentDate.daysTo( eventDate );
120 if ( QDate::isLeapYear( QDate::currentDate().year() ) ) {
135 const KCalCore::Calendar::Ptr &calendar )
137 KCalCore::Event::List allEvents = calendar->events();
138 KCalCore::Event::List events;
139 KDateTime::Spec spec = KSystemTimeZones::local();
140 const KDateTime currentDateTime = KDateTime::currentDateTime( spec );
141 const QDate currentDate = currentDateTime.date();
145 for (
int i=0; i<allEvents.count(); ++i) {
146 KCalCore::Event::Ptr
event = allEvents.at(i);
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()) {
156 sDateTimeByUid.insert(event->instanceIdentifier(), occurrences.first());
159 if ( ( start >= eventStart.date() && start <= eventEnd.date() ) ||
160 ( end >= eventStart.date() && end <= eventEnd.date() ) ) {
162 if ( eventStart.date() < start ) {
163 sDateTimeByUid.insert(event->instanceIdentifier(), KDateTime(start,spec));
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 ) {
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();
185 QDate startOfMultiday = eventStart.date();
186 if ( startOfMultiday < currentDate ) {
187 startOfMultiday = currentDate;
189 bool firstDayOfMultiday = ( start == startOfMultiday );
192 eventInfoList.append( summaryEvent );
195 summaryEvent->
ev =
ev;
199 QDate sD = occurrenceStartDate;
200 if (currentDate >= sD) {
201 str = i18nc(
"the appointment is today",
"Today" );
203 }
else if ( sD == currentDate.addDays(1) ) {
204 str = i18nc(
"the appointment is tomorrow",
"Tomorrow" );
206 str = KGlobal::locale()->formatDate( sD, KLocale::FancyLongDate );
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;
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 );
226 const int daysTo = currentDate.daysTo(occurrenceStartDate);
228 str = i18np(
"in 1 day",
"in %1 days", daysTo );
230 if ( !ev->allDay() ) {
232 if ( !ev->recurs() ) {
233 secs = currentDateTime.secsTo( ev->dtStart() );
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 );
241 str = i18nc(
"eg. in 1 hour 2 minutes",
"in " );
242 int hours = secs / 3600;
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 );
249 int mins = secs / 60;
251 str += i18ncp(
"use abbreviation for minute to keep the text short",
252 "1 min",
"%1 mins", mins );
258 str = i18n(
"all day" );
264 str = ev->richSummary();
265 if ( ev->isMultiDay() && !ev->allDay() ) {
266 str.append( QString::fromLatin1(
" (%1/%2)" ).arg( dayof ).arg( span ) );
270 QString tipText( KCalUtils::IncidenceFormatter::toolTipStr(
271 KCalUtils::IncidenceFormatter::resourceString(
272 calendar, ev ), ev, start,
true, spec ) );
273 if ( !tipText.isEmpty() ) {
279 if ( !ev->allDay() ) {
280 QTime sST = eventStart.time();
281 QTime sET = eventEnd.time();
282 if ( ev->isMultiDay() ) {
283 if ( eventStart.date() < start ) {
286 if ( eventEnd.date() > end ) {
287 sET = QTime( 23, 59 );
290 str = i18nc(
"Time from - to",
"%1 - %2",
291 KGlobal::locale()->formatTime( sST ),
292 KGlobal::locale()->formatTime( sET ) );
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(),
304 if ( !summaryEvent->
timeRange.isEmpty() ) {
305 summaryEvent->
timeRange += QLatin1String(
"<br>");
307 summaryEvent->
timeRange += QLatin1String(
"<font size=\"small\"><i>") +
308 i18nc(
"next occurrence",
"Next: %1", tmp ) +
309 QLatin1String(
"</i></font>");
313 return eventInfoList;
317 const KCalCore::Calendar::Ptr &calendar )
static bool eventLessThan(const KCalCore::Event::Ptr &event1, const KCalCore::Event::Ptr &event2)
static QHash< QString, KDateTime > sDateTimeByUid
static void setShowSpecialEvents(bool skipBirthdays, bool skipAnniversaries)
static List eventsForRange(const QDate &start, const QDate &end, const KCalCore::Calendar::Ptr &calendar)
static
static List eventsForDate(const QDate &date, const KCalCore::Calendar::Ptr &calendar)