libkcal

todo.cpp
1 /*
2  This file is part of libkcal.
3 
4  Copyright (c) 2001-2003 Cornelius Schumacher <schumacher@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 #include <tdeglobal.h>
23 #include <tdelocale.h>
24 #include <kdebug.h>
25 
26 #include "todo.h"
27 
28 using namespace KCal;
29 
30 Todo::Todo()
31 {
32  mHasDueDate = false;
33  mHasStartDate = false;
34 
35  mHasCompletedDate = false;
36  mPercentComplete = 0;
37 }
38 
39 Todo::Todo(const Todo &t) : Incidence(t)
40 {
41  mDtDue = t.mDtDue;
42  mHasDueDate = t.mHasDueDate;
43  mHasStartDate = t.mHasStartDate;
44  mCompleted = t.mCompleted;
45  mHasCompletedDate = t.mHasCompletedDate;
46  mPercentComplete = t.mPercentComplete;
47  mDtRecurrence = t.mDtRecurrence;
48 }
49 
50 Todo::~Todo()
51 {
52 }
53 
55 {
56  return new Todo( *this );
57 }
58 
59 
60 Todo& Todo::operator=( const Todo &t )
61 {
62  Incidence::operator=( t );
63  mDtDue = t.mDtDue;
64  mHasDueDate = t.mHasDueDate;
65  mHasStartDate = t.mHasStartDate;
66  mCompleted = t.mCompleted;
67  mHasCompletedDate = t.mHasCompletedDate;
68  mPercentComplete = t.mPercentComplete;
69  mDtRecurrence = t.mDtRecurrence;
70  return *this;
71 }
72 
73 bool Todo::operator==( const Todo& t2 ) const
74 {
75  return
76  static_cast<const Incidence&>(*this) == static_cast<const Incidence&>(t2) &&
77  dtDue() == t2.dtDue() &&
78  hasDueDate() == t2.hasDueDate() &&
79  hasStartDate() == t2.hasStartDate() &&
80  completed() == t2.completed() &&
83 }
84 
85 void Todo::setDtDue(const TQDateTime &dtDue, bool first )
86 {
87  //int diffsecs = mDtDue.secsTo(dtDue);
88 
89  /*if (mReadOnly) return;
90  const Alarm::List& alarms = alarms();
91  for (Alarm* alarm = alarms.first(); alarm; alarm = alarms.next()) {
92  if (alarm->enabled()) {
93  alarm->setTime(alarm->time().addSecs(diffsecs));
94  }
95  }*/
96  if( doesRecur() && !first ) {
97  mDtRecurrence = dtDue;
98  } else {
99  mDtDue = dtDue;
100  // TODO: This doesn't seem right...
103  }
104 
105  if ( doesRecur() && dtDue < recurrence()->startDateTime() )
106  setDtStart( dtDue );
107 
108  //kdDebug(5800) << "setDtDue says date is " << mDtDue.toString() << endl;
109 
110  /*const Alarm::List& alarms = alarms();
111  for (Alarm* alarm = alarms.first(); alarm; alarm = alarms.next())
112  alarm->setAlarmStart(mDtDue);*/
113 
114  updated();
115 }
116 
117 TQDateTime Todo::dtDue( bool first ) const
118 {
119  if ( doesRecur() && !first && mDtRecurrence.isValid() ) {
120  return mDtRecurrence;
121  } else if ( hasDueDate() ) {
122  return mDtDue;
123  } else {
124  return TQDateTime();
125  }
126 }
127 
128 TQString Todo::dtDueTimeStr() const
129 {
130  return TDEGlobal::locale()->formatTime( dtDue(!doesRecur()).time() );
131 }
132 
133 TQString Todo::dtDueDateStr(bool shortfmt) const
134 {
135  return TDEGlobal::locale()->formatDate(dtDue( !doesRecur() ).date(),shortfmt);
136 }
137 
138 // TODO: Add shortfmt param!!!
139 TQString Todo::dtDueStr() const
140 {
141  return TDEGlobal::locale()->formatDateTime( dtDue( !doesRecur() ) );
142 }
143 
144 bool Todo::hasDueDate() const
145 {
146  return mHasDueDate;
147 }
148 
150 {
151  if (mReadOnly) return;
152  mHasDueDate = f;
153  updated();
154 }
155 
156 
157 bool Todo::hasStartDate() const
158 {
159  return mHasStartDate;
160 }
161 
163 {
164  if (mReadOnly) return;
165 
166  if ( doesRecur() && !f ) {
167  if ( !comments().grep("NoStartDate").count() )
168  addComment("NoStartDate"); //TODO: --> custom flag?
169  } else {
170  TQString s("NoStartDate");
171  removeComment(s);
172  }
173  mHasStartDate = f;
174  updated();
175 }
176 
177 TQDateTime Todo::dtStart( bool first ) const
178 {
179  if ( doesRecur() && !first ) {
180  TQDateTime dt = mDtRecurrence.addDays( dtDue( true ).daysTo( IncidenceBase::dtStart() ) );
181 
182  // We want the dtStart's time, not dtDue's
183  dt.setTime( IncidenceBase::dtStart().time() );
184  return dt;
185  } else if ( hasStartDate() ) {
186  return IncidenceBase::dtStart();
187  } else {
188  return TQDateTime();
189  }
190 }
191 
192 void Todo::setDtStart( const TQDateTime &dtStart )
193 {
194  // TODO: This doesn't seem right (rfc 2445/6 says, recurrence is calculated from the dtstart...)
195  if ( doesRecur() ) {
196  recurrence()->setStartDateTime( mDtDue );
198  }
200 }
201 
202 TQString Todo::dtStartTimeStr( bool first ) const
203 {
204  return TDEGlobal::locale()->formatTime(dtStart(first).time());
205 }
206 
207 TQString Todo::dtStartDateStr(bool shortfmt, bool first) const
208 {
209  return TDEGlobal::locale()->formatDate(dtStart(first).date(),shortfmt);
210 }
211 
212 TQString Todo::dtStartStr(bool first) const
213 {
214  return TDEGlobal::locale()->formatDateTime(dtStart(first));
215 }
216 
217 bool Todo::isCompleted() const
218 {
219  if (mPercentComplete == 100) return true;
220  else return false;
221 }
222 
223 void Todo::setCompleted(bool completed)
224 {
225  if (completed)
226  mPercentComplete = 100;
227  else {
228  mPercentComplete = 0;
229  mHasCompletedDate = false;
230  mCompleted = TQDateTime();
231  }
232  updated();
233 }
234 
235 TQDateTime Todo::completed() const
236 {
237  if ( hasCompletedDate() )
238  return mCompleted;
239  else
240  return TQDateTime();
241 }
242 
243 TQString Todo::completedStr() const
244 {
245  return TDEGlobal::locale()->formatDateTime(mCompleted);
246 }
247 
248 void Todo::setCompleted(const TQDateTime &completed)
249 {
250  if( !recurTodo() ) {
251  mHasCompletedDate = true;
252  mPercentComplete = 100;
253  mCompleted = completed;
254  }
255  updated();
256 }
257 
259 {
260  return mHasCompletedDate;
261 }
262 
264 {
265  return mPercentComplete;
266 }
267 
269 {
270  mPercentComplete = v;
271  if ( v != 100 ) {
272  mHasCompletedDate = false;
273  mCompleted = TQDateTime();
274  }
275 
276  updated();
277 }
278 
279 void Todo::setDtRecurrence( const TQDateTime &dt )
280 {
281  mDtRecurrence = dt;
282 }
283 
284 TQDateTime Todo::dtRecurrence() const
285 {
286  return mDtRecurrence.isValid() ? mDtRecurrence : mDtDue;
287 }
288 
289 bool Todo::recursOn( const TQDate &date ) const
290 {
291  TQDate today = TQDate::currentDate();
292  return ( Incidence::recursOn(date) &&
293  !( date < today && mDtRecurrence.date() < today &&
294  mDtRecurrence > recurrence()->startDateTime() ) );
295 }
296 
297 bool Todo::recurTodo()
298 {
299  if ( doesRecur() ) {
300  Recurrence *r = recurrence();
301  TQDateTime endDateTime = r->endDateTime();
302  TQDateTime nextDate = r->getNextDateTime( dtDue() );
303 
304  if ( ( r->duration() == -1 || ( nextDate.isValid() && endDateTime.isValid()
305  && nextDate <= endDateTime ) ) ) {
306 
307  while ( !recursAt( nextDate ) || nextDate <= TQDateTime::currentDateTime() ) {
308 
309  if ( !nextDate.isValid() ||
310  ( nextDate > endDateTime && r->duration() != -1 ) ) {
311  return false;
312  }
313 
314  nextDate = r->getNextDateTime( nextDate );
315  }
316 
317  setDtDue( nextDate );
318  setCompleted( false );
319  setRevision( revision() + 1 );
320 
321  return true;
322  }
323  }
324 
325  return false;
326 }
327 
328 bool Todo::isOverdue() const
329 {
330  bool inPast = doesFloat() ? dtDue().date() < TQDate::currentDate()
331  : dtDue() < TQDateTime::currentDateTime();
332  return ( inPast && !isCompleted() );
333 }
Todo * clone()
Returns an exact copy of this todo.
Definition: todo.cpp:54
bool hasCompletedDate() const
Returns true, if todo has a date associated with completion, otherwise return false.
Definition: todo.cpp:258
bool isOverdue() const
Returns true if this todo is overdue (e.g.
Definition: todo.cpp:328
TQString dtStartDateStr(bool shortfmt=true, bool first=false) const
Returns an todo's starting date as a string formatted according to the users locale settings.
Definition: todo.cpp:207
void setDtDue(const TQDateTime &dtDue, bool first=false)
Sets due date and time.
Definition: todo.cpp:85
void setFloats(bool floats)
Sets whether the dtstart is a floating time (i.e.
Definition: recurrence.cpp:133
virtual TDE_DEPRECATED TQString dtStartStr() const
returns an event's starting date and time as a string formatted according to the users locale setting...
virtual bool recursOn(const TQDate &date) const
Returns true if the date specified is one on which the todo will recur.
Definition: todo.cpp:289
Recurrence * recurrence() const
Return the recurrence rule associated with this incidence.
Definition: incidence.cpp:390
This class represents a recurrence rule for a calendar incidence.
Definition: recurrence.h:89
void addComment(const TQString &comment)
Add a comment to this incidence.
void setPercentComplete(int)
Set how many percent of the task are completed.
Definition: todo.cpp:268
TDE_DEPRECATED TQString dtDueDateStr(bool shortfmt=true) const
Returns due date as string formatted according to the users locale settings.
Definition: todo.cpp:133
int duration() const
Returns -1 if the event recurs infinitely, 0 if the end date is set, otherwise the total number of re...
Definition: recurrence.cpp:395
bool doesRecur() const
Forward to Recurrence::doesRecur().
Definition: incidence.cpp:416
Definition: alarm.h:38
virtual bool recursOn(const TQDate &qd) const
Returns true if the date specified is one on which the incidence will recur.
Definition: incidence.cpp:422
bool isCompleted() const
Returns true if the todo is 100% completed, otherwise return false.
Definition: todo.cpp:217
bool recursAt(const TQDateTime &qdt) const
Returns true if the date/time specified is one on which the incidence will recur.
Definition: incidence.cpp:430
TQString completedStr() const
Returns string contaiting date and time when the todo was completed formatted according to the users ...
Definition: todo.cpp:243
TQDateTime endDateTime() const
Returns the date/time of the last recurrence.
Definition: recurrence.cpp:351
TQStringList comments() const
Return all comments associated with this incidence.
This class provides a Todo in the sense of RFC2445.
Definition: todo.h:31
virtual TDE_DEPRECATED TQString dtStartTimeStr() const
returns an event's starting time as a string formatted according to the users locale settings.
TQDateTime dtRecurrence() const
Returns the due date/time of the current occurrence if recurrent.
Definition: todo.cpp:284
TQDateTime completed() const
Returns date and time when todo was completed.
Definition: todo.cpp:235
virtual void setDtStart(const TQDateTime &dtStart)
for setting the event's starting date/time with a TQDateTime.
void setRevision(int rev)
Set the number of revisions this event has seen.
Definition: incidence.cpp:251
This class provides the base class common to all calendar components.
Definition: incidence.h:47
bool doesFloat() const
Return true or false depending on whether the incidence "floats," i.e.
void setDtStart(const TQDateTime &dtStart)
Sets the startdate of the todo.
Definition: todo.cpp:192
bool removeComment(const TQString &comment)
Remove a comment from the event.
int percentComplete() const
Returns how many percent of the task are completed.
Definition: todo.cpp:263
void setStartDateTime(const TQDateTime &start)
Set start of recurrence, as a date and time.
Definition: recurrence.cpp:444
TDE_DEPRECATED TQString dtDueStr() const
Returns due date and time as string formatted according to the users locale settings.
Definition: todo.cpp:139
bool hasStartDate() const
Returns true if the todo has a start date, otherwise return false.
Definition: todo.cpp:157
void setDtRecurrence(const TQDateTime &dt)
Sets the due date/time of the current occurrence if recurrent.
Definition: todo.cpp:279
int revision() const
Return the number of revisions this event has seen.
Definition: incidence.cpp:259
virtual TQDateTime dtStart() const
returns an event's starting date/time as a TQDateTime.
bool hasDueDate() const
Returns true if the todo has a due date, otherwise return false.
Definition: todo.cpp:144
TQDateTime dtDue(bool first=false) const
Returns due date and time.
Definition: todo.cpp:117
void setCompleted(bool completed)
Set completed state.
Definition: todo.cpp:223
void updated()
Call this to notify the observers after the IncidenceBas object has changed.
TDE_DEPRECATED TQString dtDueTimeStr() const
Returns due time as string formatted according to the users locale settings.
Definition: todo.cpp:128
TQDateTime getNextDateTime(const TQDateTime &preDateTime) const
Returns the date and time of the next recurrence, after the specified date/time.
Definition: recurrence.cpp:837
void setHasStartDate(bool hasStartDate)
Set if the todo has a start date.
Definition: todo.cpp:162
void setHasDueDate(bool hasDueDate)
Set if the todo has a due date.
Definition: todo.cpp:149