libkcal

todo.cpp

Go to the documentation of this file.
00001 /*
00002     This file is part of libkcal.
00003 
00004     Copyright (c) 2001-2003 Cornelius Schumacher <schumacher@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019     Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include <kglobal.h>
00023 #include <klocale.h>
00024 #include <kdebug.h>
00025 
00026 #include "todo.h"
00027 
00028 using namespace KCal;
00029 
00030 Todo::Todo()
00031 {
00032   mHasDueDate = false;
00033   mHasStartDate = false;
00034 
00035   mHasCompletedDate = false;
00036   mPercentComplete = 0;
00037 }
00038 
00039 Todo::Todo(const Todo &t) : Incidence(t)
00040 {
00041   mDtDue = t.mDtDue;
00042   mHasDueDate = t.mHasDueDate;
00043   mHasStartDate = t.mHasStartDate;
00044   mCompleted = t.mCompleted;
00045   mHasCompletedDate = t.mHasCompletedDate;
00046   mPercentComplete = t.mPercentComplete;
00047   mDtRecurrence = t.mDtRecurrence;
00048 }
00049 
00050 Todo::~Todo()
00051 {
00052 }
00053 
00054 Todo *Todo::clone()
00055 {
00056   return new Todo( *this );
00057 }
00058 
00059 
00060 Todo& Todo::operator=( const Todo &t )
00061 {
00062   Incidence::operator=( t );
00063   mDtDue = t.mDtDue;
00064   mHasDueDate = t.mHasDueDate;
00065   mHasStartDate = t.mHasStartDate;
00066   mCompleted = t.mCompleted;
00067   mHasCompletedDate = t.mHasCompletedDate;
00068   mPercentComplete = t.mPercentComplete;
00069   mDtRecurrence = t.mDtRecurrence;
00070   return *this;
00071 }
00072 
00073 bool Todo::operator==( const Todo& t2 ) const
00074 {
00075     return
00076         static_cast<const Incidence&>(*this) == static_cast<const Incidence&>(t2) &&
00077         dtDue() == t2.dtDue() &&
00078         hasDueDate() == t2.hasDueDate() &&
00079         hasStartDate() == t2.hasStartDate() &&
00080         completed() == t2.completed() &&
00081         hasCompletedDate() == t2.hasCompletedDate() &&
00082         percentComplete() == t2.percentComplete();
00083 }
00084 
00085 void Todo::setDtDue(const QDateTime &dtDue, bool first )
00086 {
00087   //int diffsecs = mDtDue.secsTo(dtDue);
00088 
00089   /*if (mReadOnly) return;
00090   const Alarm::List& alarms = alarms();
00091   for (Alarm* alarm = alarms.first(); alarm; alarm = alarms.next()) {
00092     if (alarm->enabled()) {
00093       alarm->setTime(alarm->time().addSecs(diffsecs));
00094     }
00095   }*/
00096   if( doesRecur() && !first ) {
00097     mDtRecurrence = dtDue;
00098   } else {
00099     mDtDue = dtDue;
00100     // TODO: This doesn't seem right...
00101     recurrence()->setStartDateTime( dtDue );
00102     recurrence()->setFloats( doesFloat() );
00103   }
00104 
00105   if ( doesRecur() && dtDue < recurrence()->startDateTime() )
00106     setDtStart( dtDue );
00107 
00108   //kdDebug(5800) << "setDtDue says date is " << mDtDue.toString() << endl;
00109 
00110   /*const Alarm::List& alarms = alarms();
00111   for (Alarm* alarm = alarms.first(); alarm; alarm = alarms.next())
00112     alarm->setAlarmStart(mDtDue);*/
00113 
00114   updated();
00115 }
00116 
00117 QDateTime Todo::dtDue( bool first ) const
00118 {
00119   if ( doesRecur() && !first && mDtRecurrence.isValid() )
00120     return mDtRecurrence;
00121 
00122   return mDtDue;
00123 }
00124 
00125 QString Todo::dtDueTimeStr() const
00126 {
00127   return KGlobal::locale()->formatTime( dtDue(!doesRecur()).time() );
00128 }
00129 
00130 QString Todo::dtDueDateStr(bool shortfmt) const
00131 {
00132   return KGlobal::locale()->formatDate(dtDue( !doesRecur() ).date(),shortfmt);
00133 }
00134 
00135 // TODO: Add shortfmt param!!!
00136 QString Todo::dtDueStr() const
00137 {
00138   return KGlobal::locale()->formatDateTime( dtDue( !doesRecur() ) );
00139 }
00140 
00141 bool Todo::hasDueDate() const
00142 {
00143   return mHasDueDate;
00144 }
00145 
00146 void Todo::setHasDueDate(bool f)
00147 {
00148   if (mReadOnly) return;
00149   mHasDueDate = f;
00150   updated();
00151 }
00152 
00153 
00154 bool Todo::hasStartDate() const
00155 {
00156   return mHasStartDate;
00157 }
00158 
00159 void Todo::setHasStartDate(bool f)
00160 {
00161   if (mReadOnly) return;
00162 
00163   if ( doesRecur() && !f ) {
00164     if ( !comments().grep("NoStartDate").count() )
00165       addComment("NoStartDate"); //TODO: --> custom flag?
00166   } else {
00167     QString s("NoStartDate");
00168     removeComment(s);
00169   }
00170   mHasStartDate = f;
00171   updated();
00172 }
00173 
00174 QDateTime Todo::dtStart( bool first ) const
00175 {
00176   if ( doesRecur() && !first )
00177     return mDtRecurrence.addDays( dtDue( first ).daysTo( IncidenceBase::dtStart() ) );
00178   else
00179     return IncidenceBase::dtStart();
00180 }
00181 
00182 void Todo::setDtStart( const QDateTime &dtStart )
00183 {
00184   // TODO: This doesn't seem right (rfc 2445/6 says, recurrence is calculated from the dtstart...)
00185   if ( doesRecur() ) {
00186     recurrence()->setStartDateTime( mDtDue );
00187     recurrence()->setFloats( doesFloat() );
00188   }
00189   IncidenceBase::setDtStart( dtStart );
00190 }
00191 
00192 QString Todo::dtStartTimeStr( bool first ) const
00193 {
00194   return KGlobal::locale()->formatTime(dtStart(first).time());
00195 }
00196 
00197 QString Todo::dtStartDateStr(bool shortfmt, bool first) const
00198 {
00199   return KGlobal::locale()->formatDate(dtStart(first).date(),shortfmt);
00200 }
00201 
00202 QString Todo::dtStartStr(bool first) const
00203 {
00204   return KGlobal::locale()->formatDateTime(dtStart(first));
00205 }
00206 
00207 bool Todo::isCompleted() const
00208 {
00209   if (mPercentComplete == 100) return true;
00210   else return false;
00211 }
00212 
00213 void Todo::setCompleted(bool completed)
00214 {
00215   if (completed)
00216     mPercentComplete = 100;
00217   else {
00218     mPercentComplete = 0;
00219     mHasCompletedDate = false;
00220     mCompleted = QDateTime();
00221   }
00222   updated();
00223 }
00224 
00225 QDateTime Todo::completed() const
00226 {
00227   if ( hasCompletedDate() )
00228     return mCompleted;
00229   else
00230     return QDateTime();
00231 }
00232 
00233 QString Todo::completedStr() const
00234 {
00235   return KGlobal::locale()->formatDateTime(mCompleted);
00236 }
00237 
00238 void Todo::setCompleted(const QDateTime &completed)
00239 {
00240   if( !recurTodo() ) {
00241     mHasCompletedDate = true;
00242     mPercentComplete = 100;
00243     mCompleted = completed;
00244   }
00245   updated();
00246 }
00247 
00248 bool Todo::hasCompletedDate() const
00249 {
00250   return mHasCompletedDate;
00251 }
00252 
00253 int Todo::percentComplete() const
00254 {
00255   return mPercentComplete;
00256 }
00257 
00258 void Todo::setPercentComplete(int v)
00259 {
00260   mPercentComplete = v;
00261   if ( v != 100 ) mHasCompletedDate = false;
00262   updated();
00263 }
00264 
00265 void Todo::setDtRecurrence( const QDateTime &dt )
00266 {
00267   mDtRecurrence = dt;
00268 }
00269 
00270 QDateTime Todo::dtRecurrence() const
00271 {
00272   return mDtRecurrence.isValid() ? mDtRecurrence : mDtDue;
00273 }
00274 
00275 bool Todo::recursOn( const QDate &date ) const
00276 {
00277   QDate today = QDate::currentDate();
00278   return ( Incidence::recursOn(date) &&
00279            !( date < today && mDtRecurrence.date() < today &&
00280               mDtRecurrence > recurrence()->startDateTime() ) );
00281 }
00282 
00283 bool Todo::recurTodo()
00284 {
00285   if ( doesRecur() ) {
00286     Recurrence *r = recurrence();
00287     QDateTime endDateTime = r->endDateTime();
00288     QDateTime nextDate = r->getNextDateTime( dtDue() );
00289 
00290     if ( ( r->duration() == -1 || ( nextDate.isValid() && endDateTime.isValid()
00291            && nextDate <= endDateTime ) ) ) {
00292 
00293       while ( !recursAt( nextDate ) || nextDate <= QDateTime::currentDateTime() ) {
00294 
00295         if ( !nextDate.isValid() || nextDate > endDateTime ) {
00296           return false;
00297         }
00298 
00299         nextDate = r->getNextDateTime( nextDate );
00300       }
00301 
00302       setDtDue( nextDate );
00303       setCompleted( false );
00304       setRevision( revision() + 1 );
00305 
00306       return true;
00307     }
00308   }
00309 
00310   return false;
00311 }
00312 
00313 bool Todo::isOverdue() const
00314 {
00315   bool inPast = doesFloat() ? dtDue().date() < QDate::currentDate()
00316                             : dtDue() < QDateTime::currentDateTime();
00317   return ( inPast && !isCompleted() );
00318 }