karm Library API Documentation

task.cpp

00001 #include <qcstring.h>
00002 #include <qdatetime.h>
00003 #include <qstring.h>
00004 #include <qtimer.h>
00005 
00006 #include <kiconloader.h>
00007 
00008 #include "kapplication.h"       // kapp
00009 #include "kdebug.h"
00010 
00011 #include "event.h"
00012 
00013 #include "karmutility.h"
00014 #include "task.h"
00015 #include "taskview.h"
00016 #include "preferences.h"
00017 
00018 
00019 const int gSecondsPerMinute = 60;
00020 
00021 
00022 QPtrVector<QPixmap> *Task::icons = 0;
00023 
00024 Task::Task( const QString& taskName, long minutes, long sessionTime,
00025             DesktopList desktops, TaskView *parent)
00026   : QObject(), QListViewItem(parent)
00027 {
00028   init(taskName, minutes, sessionTime, desktops, 0);
00029 }
00030 
00031 Task::Task( const QString& taskName, long minutes, long sessionTime,
00032             DesktopList desktops, Task *parent)
00033   : QObject(), QListViewItem(parent)
00034 {
00035   init(taskName, minutes, sessionTime, desktops, 0);
00036 }
00037 
00038 Task::Task( KCal::Todo* todo, TaskView* parent )
00039   : QObject(), QListViewItem( parent )
00040 {
00041   long minutes = 0;
00042   QString name;
00043   long sessionTime = 0;
00044   int percent_complete = 0;
00045   DesktopList desktops;
00046 
00047   parseIncidence(todo, minutes, sessionTime, name, desktops, percent_complete);
00048   init(name, minutes, sessionTime, desktops, percent_complete);
00049 }
00050 
00051 void Task::init( const QString& taskName, long minutes, long sessionTime,
00052                  DesktopList desktops, int percent_complete)
00053 {
00054   // If our parent is the taskview then connect our totalTimesChanged
00055   // signal to its receiver
00056   if ( ! parent() )
00057     connect( this, SIGNAL( totalTimesChanged ( long, long ) ),
00058              listView(), SLOT( taskTotalTimesChanged( long, long) ));
00059 
00060   connect( this, SIGNAL( deletingTask( Task* ) ),
00061            listView(), SLOT( deletingTask( Task* ) ));
00062 
00063   if (icons == 0) {
00064     icons = new QPtrVector<QPixmap>(8);
00065     for (int i=0; i<8; i++)
00066     {
00067       QPixmap *icon = new QPixmap();
00068       QString name;
00069       name.sprintf("watch-%d.xpm",i);
00070       *icon = UserIcon(name);
00071       icons->insert(i,icon);
00072     }
00073   }
00074 
00075   //kdDebug() << "Task::init(" << taskName << ", " << minutes << ", "
00076   //  << sessionTime << ", desktops)" << endl;
00077 
00078   _name = taskName.stripWhiteSpace();
00079   _lastStart = QDateTime::currentDateTime();
00080   _totalTime = _time = minutes;
00081   _totalSessionTime = _sessionTime = sessionTime;
00082   noNegativeTimes();
00083   _timer = new QTimer(this);
00084   _desktops = desktops;
00085   connect(_timer, SIGNAL(timeout()), this, SLOT(updateActiveIcon()));
00086   setPixmap(1, UserIcon(QString::fromLatin1("empty-watch.xpm")));
00087   _currentPic = 0;
00088   _percentcomplete = percent_complete;
00089 
00090   update();
00091   changeParentTotalTimes( _sessionTime, _time);
00092 }
00093 
00094 Task::~Task() {
00095   emit deletingTask(this);
00096   delete _timer;
00097 }
00098 
00099 void Task::setRunning( bool on, KarmStorage* storage )
00100 {
00101   if (on) {
00102     if (!_timer->isActive()) {
00103       _timer->start(1000);
00104       storage->startTimer(this);
00105       _currentPic=7;
00106       _lastStart = QDateTime::currentDateTime();
00107       updateActiveIcon();
00108     }
00109   }
00110   else {
00111     if (_timer->isActive()) {
00112       _timer->stop();
00113       storage->stopTimer(this);
00114       setPixmap(1, UserIcon(QString::fromLatin1("empty-watch.xpm")));
00115     }
00116   }
00117 }
00118 
00119 void Task::setUid(QString uid) {
00120   _uid = uid;
00121 }
00122 
00123 bool Task::isRunning() const
00124 {
00125   return _timer->isActive();
00126 }
00127 
00128 void Task::setName( const QString& name, KarmStorage* storage )
00129 {
00130   kdDebug() << "Task:setName: " << name << endl;
00131 
00132   QString oldname = _name;
00133   if ( oldname != name ) {
00134     _name = name;
00135     storage->setName(this, oldname);
00136     update();
00137   }
00138 }
00139 
00140 void Task::setPercentComplete(const int percent, KarmStorage *storage)
00141 {
00142   kdDebug() << "Task::setPercentComplete(" << percent << ", storage): "
00143     << _uid << endl;
00144 
00145   if (isRunning()) setRunning(false, storage);
00146 
00147   setEnabled(false);
00148   setOpen(false);
00149 
00150   if (!percent)
00151     _percentcomplete = 0;
00152   else if (percent > 100)
00153     _percentcomplete = 100;
00154   else if (percent < 0)
00155     _percentcomplete = 0;
00156   else
00157     _percentcomplete = percent;
00158 
00159   // When parent marked as complete, mark all children as complete as well.
00160   // Complete tasks are not displayed in the task view, so if a parent is
00161   // marked as complete and some of the children are not, then we get an error
00162   // message.  KArm actually keep chugging along in this case and displays the
00163   // child tasks just fine, so an alternative solution is to remove that error
00164   // message (from KarmStorage::load).  But I think it makes more sense that
00165   // if you mark a parent task as complete, then all children should be
00166   // complete as well.
00167   //
00168   // This behavior is consistent with KOrganizer (as of 2003-09-24).
00169   if (_percentcomplete == 100)
00170   {
00171     for (Task* child= this->firstChild(); child; child = child->nextSibling())
00172       child->setPercentComplete(_percentcomplete, storage);
00173   }
00174 }
00175 
00176 bool Task::isComplete() { return _percentcomplete == 100; }
00177 
00178 void Task::removeFromView()
00179 {
00180   for (Task* child= this->firstChild(); child; child= child->nextSibling())
00181     child->removeFromView();
00182   delete this;
00183 }
00184 
00185 void Task::setDesktopList ( DesktopList desktopList )
00186 {
00187   _desktops = desktopList;
00188 }
00189 
00190 void Task::changeTimes( long minutesSession, long minutes, bool do_logging,
00191     KarmStorage* storage)
00192 {
00193   if( minutesSession != 0 || minutes != 0) {
00194     _sessionTime += minutesSession;
00195 
00196     //kdDebug()
00197     //  << "Task::changeTimes: " << name()
00198     //  << ", _sessionTime = " << minutesSession << endl;
00199 
00200     _time += minutes;
00201     if ( do_logging )
00202       storage->changeTime(this, minutes * gSecondsPerMinute);
00203       //_logging->changeTimes( this, minutesSession, minutes);
00204 
00205     noNegativeTimes();
00206     changeTotalTimes( minutesSession, minutes );
00207   }
00208 }
00209 
00210 void Task::changeTotalTimes( long minutesSession, long minutes )
00211 {
00212   //kdDebug()
00213   //  << "Task::changeTotalTimes(" << minutesSession << ", "
00214   //  << minutes << ") for " << name() << endl;
00215 
00216   _totalSessionTime += minutesSession;
00217   _totalTime += minutes;
00218   noNegativeTimes();
00219   update();
00220   changeParentTotalTimes( minutesSession, minutes );
00221 }
00222 
00223 void Task::resetTimes()
00224 {
00225   _totalSessionTime -= _sessionTime;
00226   _totalTime -= _time;
00227   changeParentTotalTimes( -_sessionTime, -_time);
00228   _sessionTime = 0;
00229   _time = 0;
00230   update();
00231 }
00232 
00233 void Task::changeParentTotalTimes( long minutesSession, long minutes )
00234 {
00235   //kdDebug()
00236   //  << "Task::changeParentTotalTimes(" << minutesSession << ", "
00237   //  << minutes << ") for " << name() << endl;
00238 
00239   if ( isRoot() )
00240     emit totalTimesChanged( minutesSession, minutes );
00241   else
00242     parent()->changeTotalTimes( minutesSession, minutes );
00243 }
00244 
00245 bool Task::remove( QPtrList<Task>& activeTasks, KarmStorage* storage)
00246 {
00247   kdDebug() << "Task::remove: " << _name << endl;
00248 
00249   bool ok = true;
00250 
00251   storage->removeTask(this);
00252 
00253   if( isRunning() ) setRunning( false, storage );
00254 
00255   for (Task* child = this->firstChild(); child; child = child->nextSibling())
00256   {
00257     if (child->isRunning())
00258       child->setRunning(false, storage);
00259     child->remove(activeTasks, storage);
00260   }
00261 
00262   changeParentTotalTimes( -_sessionTime, -_time);
00263 
00264   return ok;
00265 }
00266 
00267 void Task::updateActiveIcon()
00268 {
00269   _currentPic = (_currentPic+1) % 8;
00270   setPixmap(1, *(*icons)[_currentPic]);
00271 }
00272 
00273 void Task::noNegativeTimes()
00274 {
00275   if ( _time < 0 )
00276       _time = 0;
00277   if ( _sessionTime < 0 )
00278       _sessionTime = 0;
00279 }
00280 
00281 QString Task::fullName() const
00282 {
00283   if (isRoot())
00284     return name();
00285   else
00286     return parent()->fullName() + QString::fromLatin1("/") + name();
00287 }
00288 
00289 KCal::Todo* Task::asTodo(KCal::Todo* todo) const
00290 {
00291 
00292   todo->setSummary( name() );
00293 
00294   // Note: if the date start is empty, the KOrganizer GUI will have the
00295   // checkbox blank, but will prefill the todo's starting datetime to the
00296   // time the file is opened.
00297   // todo->setDtStart( current );
00298 
00299   todo->setCustomProperty( kapp->instanceName(),
00300       QCString( "totalTaskTime" ), QString::number( _time ) );
00301   todo->setCustomProperty( kapp->instanceName(),
00302       QCString( "totalSessionTime" ), QString::number( _sessionTime) );
00303 
00304   if (getDesktopStr().isEmpty())
00305     todo->removeCustomProperty(kapp->instanceName(), QCString("desktopList"));
00306   else
00307     todo->setCustomProperty( kapp->instanceName(),
00308         QCString( "desktopList" ), getDesktopStr() );
00309 
00310   todo->setOrganizer( Preferences::instance()->userRealName() );
00311 
00312   todo->setPercentComplete(_percentcomplete);
00313 
00314   return todo;
00315 }
00316 
00317 bool Task::parseIncidence( KCal::Incidence* incident, long& minutes,
00318     long& sessionMinutes, QString& name, DesktopList& desktops,
00319     int& percent_complete )
00320 {
00321   bool ok;
00322 
00323   name = incident->summary();
00324   _uid = incident->uid();
00325 
00326   _comment = incident->description();
00327 
00328   ok = false;
00329   minutes = incident->customProperty( kapp->instanceName(),
00330       QCString( "totalTaskTime" )).toInt( &ok );
00331   if ( !ok )
00332     minutes = 0;
00333 
00334   ok = false;
00335   sessionMinutes = incident->customProperty( kapp->instanceName(),
00336       QCString( "totalSessionTime" )).toInt( &ok );
00337   if ( !ok )
00338     sessionMinutes = 0;
00339 
00340   QString desktopList = incident->customProperty( kapp->instanceName(),
00341       QCString( "desktopList" ) );
00342   QStringList desktopStrList = QStringList::split(QString::fromLatin1("\\,"),
00343       desktopList );
00344   desktops.clear();
00345 
00346   for ( QStringList::iterator iter = desktopStrList.begin();
00347         iter != desktopStrList.end();
00348         ++iter ) {
00349     int desktopInt = (*iter).toInt( &ok );
00350     if ( ok ) {
00351       desktops.push_back( desktopInt );
00352     }
00353   }
00354 
00355   percent_complete = static_cast<KCal::Todo*>(incident)->percentComplete();
00356 
00357   //kdDebug() << "Task::parseIncidence: "
00358   //  << name << ", Minutes: " << minutes
00359   //  <<  ", desktop: " << desktopList << endl;
00360 
00361   return true;
00362 }
00363 
00364 QString Task::getDesktopStr() const
00365 {
00366   if ( _desktops.empty() )
00367     return QString();
00368 
00369   QString desktopstr;
00370   for ( DesktopList::const_iterator iter = _desktops.begin();
00371         iter != _desktops.end();
00372         ++iter ) {
00373     desktopstr += QString::number( *iter ) + QString::fromLatin1( "," );
00374   }
00375   desktopstr.remove( desktopstr.length() - 1, 1 );
00376   return desktopstr;
00377 }
00378 
00379 void Task::cut()
00380 {
00381   //kdDebug() << "Task::cut - " << name() << endl;
00382   changeParentTotalTimes( -_totalSessionTime, -_totalTime);
00383   if ( ! parent())
00384     listView()->takeItem(this);
00385   else
00386     parent()->takeItem(this);
00387 }
00388 
00389 void Task::move(Task* destination)
00390 {
00391   cut();
00392   paste(destination);
00393 }
00394 
00395 void Task::paste(Task* destination)
00396 {
00397   destination->insertItem(this);
00398   changeParentTotalTimes( _totalSessionTime, _totalTime);
00399 }
00400 
00401 void Task::update()
00402 {
00403   setText(0, _name);
00404   setText(1, formatTime(_sessionTime));
00405   setText(2, formatTime(_time));
00406   setText(3, formatTime(_totalSessionTime));
00407   setText(4, formatTime(_totalTime));
00408 }
00409 
00410 void Task::addComment( QString comment, KarmStorage* storage )
00411 {
00412   _comment = _comment + QString::fromLatin1("\n") + comment;
00413   storage->addComment(this, comment);
00414 }
00415 
00416 QString Task::comment() const
00417 {
00418   return _comment;
00419 }
00420 
00421 #include "task.moc"
KDE Logo
This file is part of the documentation for karm Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:37:53 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003