• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 API Reference
  • KDE Home
  • Contact Us
 

KCalCore Library

  • kcalcore
todo.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the kcalcore library.
3 
4  Copyright (c) 2001-2003 Cornelius Schumacher <schumacher@kde.org>
5  Copyright (C) 2009 Allen Winter <winter@kde.org>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  Boston, MA 02110-1301, USA.
21 */
34 #include "todo.h"
35 #include "visitor.h"
36 
37 #include <KDebug>
38 
39 using namespace KCalCore;
40 
45 //@cond PRIVATE
46 class KCalCore::Todo::Private
47 {
48  public:
49  Private()
50  : mPercentComplete( 0 ),
51  mHasDueDate( false ),
52  mHasStartDate( false ),
53  mHasCompletedDate( false )
54  {}
55  Private( const KCalCore::Todo::Private &other )
56  { init( other ); }
57 
58  void init( const KCalCore::Todo::Private &other );
59 
60  KDateTime mDtDue; // to-do due date (if there is one)
61  // ALSO the first occurrence of a recurring to-do
62  KDateTime mDtRecurrence; // next occurrence (for recurring to-dos)
63  KDateTime mCompleted; // to-do completion date (if it has been completed)
64  int mPercentComplete; // to-do percent complete [0,100]
65  bool mHasDueDate; // true if the to-do has a due date
66  bool mHasStartDate; // true if the to-do has a starting date
67  bool mHasCompletedDate; // true if the to-do has a completion date
68 
72  bool recurTodo( Todo *todo );
73 };
74 
75 void KCalCore::Todo::Private::init( const KCalCore::Todo::Private &other )
76 {
77  mDtDue = other.mDtDue;
78  mDtRecurrence = other.mDtRecurrence;
79  mCompleted = other.mCompleted;
80  mPercentComplete = other.mPercentComplete;
81  mHasDueDate = other.mHasDueDate;
82  mHasStartDate = other.mHasStartDate;
83  mHasCompletedDate = other.mHasCompletedDate;
84 }
85 
86 //@endcond
87 
88 Todo::Todo()
89  : d( new KCalCore::Todo::Private )
90 {
91 }
92 
93 Todo::Todo( const Todo &other )
94  : Incidence( other ),
95  d( new KCalCore::Todo::Private( *other.d ) )
96 {
97 }
98 
99 Todo::~Todo()
100 {
101  delete d;
102 }
103 
104 Todo *Todo::clone() const
105 {
106  return new Todo( *this );
107 }
108 
109 IncidenceBase &Todo::assign( const IncidenceBase &other )
110 {
111  if ( &other != this ) {
112  Incidence::assign( other );
113  const Todo *t = static_cast<const Todo*>( &other );
114  d->init( *( t->d ) );
115  }
116  return *this;
117 }
118 
119 bool Todo::equals( const IncidenceBase &todo ) const
120 {
121  if ( !Incidence::equals( todo ) ) {
122  return false;
123  } else {
124  // If they weren't the same type IncidenceBase::equals would had returned false already
125  const Todo *t = static_cast<const Todo*>( &todo );
126  return ( ( dtDue() == t->dtDue() ) ||
127  ( !dtDue().isValid() && !t->dtDue().isValid() ) ) &&
128  hasDueDate() == t->hasDueDate() &&
129  hasStartDate() == t->hasStartDate() &&
130  ( ( completed() == t->completed() ) ||
131  ( !completed().isValid() && !t->completed().isValid() ) ) &&
132  hasCompletedDate() == t->hasCompletedDate() &&
133  percentComplete() == t->percentComplete();
134  }
135 }
136 
137 Incidence::IncidenceType Todo::type() const
138 {
139  return TypeTodo;
140 }
141 
142 QByteArray Todo::typeStr() const
143 {
144  return "Todo";
145 }
146 void Todo::setDtDue( const KDateTime &dtDue, bool first )
147 {
148  startUpdates();
149 
150  //int diffsecs = d->mDtDue.secsTo(dtDue);
151 
152  /*if (mReadOnly) return;
153  const Alarm::List& alarms = alarms();
154  for (Alarm *alarm = alarms.first(); alarm; alarm = alarms.next()) {
155  if (alarm->enabled()) {
156  alarm->setTime(alarm->time().addSecs(diffsecs));
157  }
158  }*/
159  d->mHasDueDate = dtDue.isValid();
160 
161  if ( recurs() && !first ) {
162  d->mDtRecurrence = dtDue;
163  } else {
164  d->mDtDue = dtDue;
165  // TODO: This doesn't seem right...
166  recurrence()->setStartDateTime( dtDue );
167  recurrence()->setAllDay( allDay() );
168  }
169 
170  if ( recurs() && dtDue < recurrence()->startDateTime() ) {
171  setDtStart( dtDue );
172  }
173 
174  /*const Alarm::List& alarms = alarms();
175  for (Alarm *alarm = alarms.first(); alarm; alarm = alarms.next())
176  alarm->setAlarmStart(d->mDtDue);*/
177  setFieldDirty( FieldDtDue );
178  endUpdates();
179 }
180 
181 KDateTime Todo::dtDue( bool first ) const
182 {
183  if ( !hasDueDate() ) {
184  return KDateTime();
185  }
186  if ( recurs() && !first && d->mDtRecurrence.isValid() ) {
187  return d->mDtRecurrence;
188  }
189 
190  return d->mDtDue;
191 }
192 
193 bool Todo::hasDueDate() const
194 {
195  return d->mHasDueDate;
196 }
197 
198 void Todo::setHasDueDate( bool f )
199 {
200  if ( mReadOnly ) {
201  return;
202  }
203  update();
204  d->mHasDueDate = f;
205  setFieldDirty( FieldDtDue );
206  updated();
207 }
208 
209 bool Todo::hasStartDate() const
210 {
211  return d->mHasStartDate;
212 }
213 
214 void Todo::setHasStartDate( bool f )
215 {
216  if ( mReadOnly ) {
217  return;
218  }
219 
220  update();
221  if ( recurs() && !f ) {
222  if ( !comments().filter( "NoStartDate" ).count() ) {
223  addComment( "NoStartDate" ); //TODO: --> custom flag?
224  }
225  } else {
226  QString s( "NoStartDate" );
227  removeComment( s );
228  }
229  d->mHasStartDate = f;
230  setFieldDirty( FieldDtStart );
231  updated();
232 }
233 
234 KDateTime Todo::dtStart() const
235 {
236  return dtStart( false );
237 }
238 
239 KDateTime Todo::dtStart( bool first ) const
240 {
241  if ( !hasStartDate() ) {
242  return KDateTime();
243  }
244  if ( recurs() && !first ) {
245  KDateTime dt = d->mDtRecurrence.addDays( dtDue( true ).daysTo( IncidenceBase::dtStart() ) );
246  dt.setTime( IncidenceBase::dtStart().time() );
247  return dt;
248  } else {
249  return IncidenceBase::dtStart();
250  }
251 }
252 
253 void Todo::setDtStart( const KDateTime &dtStart )
254 {
255  // TODO: This doesn't seem right (rfc 2445/6 says, recurrence is calculated from the dtstart...)
256 
257  d->mHasStartDate = dtStart.isValid();
258 
259  if ( recurs() ) {
260  recurrence()->setStartDateTime( d->mDtDue );
261  recurrence()->setAllDay( allDay() );
262  }
263  IncidenceBase::setDtStart( dtStart );
264 }
265 
266 bool Todo::isCompleted() const
267 {
268  return d->mPercentComplete == 100;
269 }
270 
271 void Todo::setCompleted( bool completed )
272 {
273  update();
274  if ( completed ) {
275  d->mPercentComplete = 100;
276  } else {
277  d->mPercentComplete = 0;
278  d->mHasCompletedDate = false;
279  d->mCompleted = KDateTime();
280  }
281  setFieldDirty( FieldCompleted );
282  updated();
283 }
284 
285 KDateTime Todo::completed() const
286 {
287  if ( hasCompletedDate() ) {
288  return d->mCompleted;
289  } else {
290  return KDateTime();
291  }
292 }
293 
294 void Todo::setCompleted( const KDateTime &completed )
295 {
296  update();
297  if ( !d->recurTodo( this ) ) {
298  d->mHasCompletedDate = true;
299  d->mPercentComplete = 100;
300  d->mCompleted = completed.toUtc();
301  setFieldDirty( FieldCompleted );
302  }
303  updated();
304 }
305 
306 bool Todo::hasCompletedDate() const
307 {
308  return d->mHasCompletedDate;
309 }
310 
311 int Todo::percentComplete() const
312 {
313  return d->mPercentComplete;
314 }
315 
316 void Todo::setPercentComplete( int percent )
317 {
318  if ( percent > 100 ) {
319  percent = 100;
320  } else if ( percent < 0 ) {
321  percent = 0;
322  }
323 
324  update();
325  d->mPercentComplete = percent;
326  if ( percent != 100 ) {
327  d->mHasCompletedDate = false;
328  }
329  setFieldDirty( FieldPercentComplete );
330  updated();
331 }
332 
333 bool Todo::isInProgress( bool first ) const
334 {
335  if ( isOverdue() ) {
336  return false;
337  }
338 
339  if ( d->mPercentComplete > 0 ) {
340  return true;
341  }
342 
343  if ( d->mHasStartDate && d->mHasDueDate ) {
344  if ( allDay() ) {
345  QDate currDate = QDate::currentDate();
346  if ( dtStart( first ).date() <= currDate && currDate < dtDue( first ).date() ) {
347  return true;
348  }
349  } else {
350  KDateTime currDate = KDateTime::currentUtcDateTime();
351  if ( dtStart( first ) <= currDate && currDate < dtDue( first ) ) {
352  return true;
353  }
354  }
355  }
356 
357  return false;
358 }
359 
360 bool Todo::isOpenEnded() const
361 {
362  if ( !d->mHasDueDate && !isCompleted() ) {
363  return true;
364  }
365  return false;
366 
367 }
368 
369 bool Todo::isNotStarted( bool first ) const
370 {
371  if ( d->mPercentComplete > 0 ) {
372  return false;
373  }
374 
375  if ( !d->mHasStartDate ) {
376  return false;
377  }
378 
379  if ( allDay() ) {
380  if ( dtStart( first ).date() >= QDate::currentDate() ) {
381  return false;
382  }
383  } else {
384  if ( dtStart( first ) >= KDateTime::currentUtcDateTime() ) {
385  return false;
386  }
387  }
388  return true;
389 }
390 
391 void Todo::shiftTimes( const KDateTime::Spec &oldSpec,
392  const KDateTime::Spec &newSpec )
393 {
394  Incidence::shiftTimes( oldSpec, newSpec );
395  d->mDtDue = d->mDtDue.toTimeSpec( oldSpec );
396  d->mDtDue.setTimeSpec( newSpec );
397  if ( recurs() ) {
398  d->mDtRecurrence = d->mDtRecurrence.toTimeSpec( oldSpec );
399  d->mDtRecurrence.setTimeSpec( newSpec );
400  }
401  if ( d->mHasCompletedDate ) {
402  d->mCompleted = d->mCompleted.toTimeSpec( oldSpec );
403  d->mCompleted.setTimeSpec( newSpec );
404  }
405 }
406 
407 void Todo::setDtRecurrence( const KDateTime &dt )
408 {
409  d->mDtRecurrence = dt;
410  setFieldDirty( FieldRecurrence );
411 }
412 
413 KDateTime Todo::dtRecurrence() const
414 {
415  return d->mDtRecurrence.isValid() ? d->mDtRecurrence : d->mDtDue;
416 }
417 
418 bool Todo::recursOn( const QDate &date, const KDateTime::Spec &timeSpec ) const
419 {
420  QDate today = QDate::currentDate();
421  return
422  Incidence::recursOn( date, timeSpec ) &&
423  !( date < today && d->mDtRecurrence.date() < today &&
424  d->mDtRecurrence > recurrence()->startDateTime() );
425 }
426 
427 bool Todo::isOverdue() const
428 {
429  if ( !dtDue().isValid() ) {
430  return false; // if it's never due, it can't be overdue
431  }
432 
433  const bool inPast = allDay() ?
434  dtDue().date() < QDate::currentDate() :
435  dtDue() < KDateTime::currentUtcDateTime();
436  return inPast && !isCompleted();
437 }
438 
439 void Todo::setAllDay( bool allday )
440 {
441  if ( allday != allDay() && !mReadOnly ) {
442  if ( hasDueDate() && dtDue().isValid() ) {
443  setFieldDirty( FieldDtDue );
444  }
445  Incidence::setAllDay( allday );
446  }
447 }
448 
449 //@cond PRIVATE
450 bool Todo::Private::recurTodo( Todo *todo )
451 {
452  if ( todo && todo->recurs() ) {
453  Recurrence *r = todo->recurrence();
454  const KDateTime recurrenceEndDateTime = r->endDateTime();
455  KDateTime nextOccurrenceDateTime = r->getNextDateTime( todo->dtDue() );
456 
457  if ( ( r->duration() == -1 ||
458  ( nextOccurrenceDateTime.isValid() && recurrenceEndDateTime.isValid() &&
459  nextOccurrenceDateTime <= recurrenceEndDateTime ) ) ) {
460  // We convert to the same timeSpec so we get the correct .date()
461  const KDateTime rightNow =
462  KDateTime::currentUtcDateTime().toTimeSpec( nextOccurrenceDateTime.timeSpec() );
463  const bool isDateOnly = todo->allDay();
464 
465  /* Now we search for the occurrence that's _after_ the currentUtcDateTime, or
466  * if it's dateOnly, the occurrrence that's _during or after today_.
467  * The reason we use "<" for date only, but "<=" for ocurrences with time is that
468  * if it's date only, the user can still complete that ocurrence today, so that's
469  * the current ocurrence that needs completing.
470  */
471  while ( !todo->recursAt( nextOccurrenceDateTime ) ||
472  ( !isDateOnly && nextOccurrenceDateTime <= rightNow ) ||
473  ( isDateOnly && nextOccurrenceDateTime.date() < rightNow.date() ) ) {
474 
475  if ( !nextOccurrenceDateTime.isValid() ||
476  ( nextOccurrenceDateTime > recurrenceEndDateTime && r->duration() != -1 ) ) {
477  return false;
478  }
479  nextOccurrenceDateTime = r->getNextDateTime( nextOccurrenceDateTime );
480  }
481 
482  todo->setDtDue( nextOccurrenceDateTime );
483  todo->setCompleted( false );
484  todo->setRevision( todo->revision() + 1 );
485 
486  return true;
487  }
488  }
489 
490  return false;
491 }
492 //@endcond
493 
494 bool Todo::accept( Visitor &v, IncidenceBase::Ptr incidence )
495 {
496  return v.visit( incidence.staticCast<Todo>() );
497 }
498 
499 KDateTime Todo::dateTime( DateTimeRole role ) const
500 {
501  switch ( role ) {
502  case RoleAlarmStartOffset:
503  return dtStart();
504  case RoleAlarmEndOffset:
505  return dtDue();
506  case RoleSort:
507  // Sorting to-dos first compares dtDue, then dtStart if
508  // dtDue doesn't exist
509  return hasDueDate() ? dtDue() : dtStart();
510  case RoleCalendarHashing:
511  return dtDue();
512  case RoleStartTimeZone:
513  return dtStart();
514  case RoleEndTimeZone:
515  return dtDue();
516  case RoleEndRecurrenceBase:
517  return dtDue();
518  case RoleDisplayStart:
519  case RoleDisplayEnd:
520  return dtDue();
521  case RoleAlarm:
522  if ( alarms().isEmpty() ) {
523  return KDateTime();
524  } else {
525  Alarm::Ptr alarm = alarms().first();
526  if ( alarm->hasStartOffset() && hasStartDate() ) {
527  return dtStart();
528  } else if ( alarm->hasEndOffset() && hasDueDate() ) {
529  return dtDue();
530  } else {
531  // The application shouldn't add alarms on to-dos without dates.
532  return KDateTime();
533  }
534  }
535  case RoleRecurrenceStart:
536  return dtDue();
537  break;
538  case RoleEnd:
539  // todos don't have dtEnd
540  default:
541  return KDateTime();
542  }
543 }
544 
545 void Todo::setDateTime( const KDateTime &dateTime, DateTimeRole role )
546 {
547  switch ( role ) {
548  case RoleDnD:
549  setDtDue( dateTime );
550  break;
551  default:
552  kDebug() << "Unhandled role" << role;
553  }
554 }
555 
556 void Todo::virtual_hook( int id, void *data )
557 {
558  Q_UNUSED( id );
559  Q_UNUSED( data );
560  Q_ASSERT( false );
561 }
562 
563 QLatin1String Todo::mimeType() const
564 {
565  return Todo::todoMimeType();
566 }
567 
568 QLatin1String Todo::todoMimeType()
569 {
570  return QLatin1String( "application/x-vnd.akonadi.calendar.todo" );
571 }
572 
573 QLatin1String Todo::iconName( const KDateTime &recurrenceId ) const
574 {
575  KDateTime occurrenceDT = recurrenceId;
576 
577  if ( recurs() && occurrenceDT.isDateOnly() ) {
578  occurrenceDT.setTime( QTime( 0, 0 ) );
579  }
580 
581  const bool usesCompletedTaskPixmap = isCompleted() ||
582  ( recurs() && occurrenceDT.isValid() &&
583  occurrenceDT < dtDue( false ) );
584 
585  if ( usesCompletedTaskPixmap ) {
586  return QLatin1String( "task-complete" );
587  } else {
588  return QLatin1String( "view-calendar-tasks" );
589  }
590 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:24:52 by doxygen 1.8.3.1 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-4.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 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
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