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"
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
00055
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
00076
00077
00078 _removing =
false;
00079 _name = taskName.stripWhiteSpace();
00080 _lastStart = QDateTime::currentDateTime();
00081 _totalTime = _time = minutes;
00082 _totalSessionTime = _sessionTime = sessionTime;
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
if ( ! _removing ) {
00114 storage->
stopTimer(
this);
00115 setPixmap(1, UserIcon(QString::fromLatin1(
"empty-watch.xpm")));
00116 }
00117 }
00118 }
00119 }
00120
00121 void Task::setUid(
QString uid) {
00122 _uid = uid;
00123 }
00124
00125 bool Task::isRunning()
const
00126
{
00127
return _timer->isActive();
00128 }
00129
00130 void Task::setName(
const QString& name,
KarmStorage* storage )
00131 {
00132 kdDebug(5970) <<
"Task:setName: " << name << endl;
00133
00134
QString oldname = _name;
00135
if ( oldname != name ) {
00136 _name = name;
00137 storage->
setName(
this, oldname);
00138
update();
00139 }
00140 }
00141
00142 void Task::setPercentComplete(
const int percent,
KarmStorage *storage)
00143 {
00144 kdDebug(5970) <<
"Task::setPercentComplete(" << percent <<
", storage): "
00145 << _uid << endl;
00146
00147
if (
isRunning())
setRunning(
false, storage);
00148
00149 setEnabled(
false);
00150 setOpen(
false);
00151
00152
if (!percent)
00153 _percentcomplete = 0;
00154
else if (percent > 100)
00155 _percentcomplete = 100;
00156
else if (percent < 0)
00157 _percentcomplete = 0;
00158
else
00159 _percentcomplete = percent;
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
if (_percentcomplete == 100)
00172 {
00173
for (
Task* child= this->
firstChild(); child; child = child->
nextSibling())
00174 child->
setPercentComplete(_percentcomplete, storage);
00175 }
00176 }
00177
00178 bool Task::isComplete() {
return _percentcomplete == 100; }
00179
00180 void Task::removeFromView()
00181 {
00182
for (
Task* child= this->
firstChild(); child; child= child->
nextSibling())
00183 child->
removeFromView();
00184
delete this;
00185 }
00186
00187 void Task::setDesktopList (
DesktopList desktopList )
00188 {
00189 _desktops = desktopList;
00190 }
00191
00192 void Task::changeTime(
long minutes,
KarmStorage* storage )
00193 {
00194
changeTimes( minutes, minutes, storage);
00195 }
00196
00197 void Task::changeTimes(
long minutesSession,
long minutes,
KarmStorage* storage)
00198 {
00199
if( minutesSession != 0 || minutes != 0)
00200 {
00201 _sessionTime += minutesSession;
00202 _time += minutes;
00203
if ( storage ) storage->
changeTime(
this, minutes * gSecondsPerMinute);
00204
changeTotalTimes( minutesSession, minutes );
00205 }
00206 }
00207
00208 void Task::changeTotalTimes(
long minutesSession,
long minutes )
00209 {
00210
00211
00212
00213
00214 _totalSessionTime += minutesSession;
00215 _totalTime += minutes;
00216
update();
00217 changeParentTotalTimes( minutesSession, minutes );
00218 }
00219
00220 void Task::resetTimes()
00221 {
00222 _totalSessionTime -= _sessionTime;
00223 _totalTime -= _time;
00224 changeParentTotalTimes( -_sessionTime, -_time);
00225 _sessionTime = 0;
00226 _time = 0;
00227
update();
00228 }
00229
00230
void Task::changeParentTotalTimes(
long minutesSession,
long minutes )
00231 {
00232
00233
00234
00235
00236
if (
isRoot() )
00237 emit totalTimesChanged( minutesSession, minutes );
00238
else
00239 parent()->
changeTotalTimes( minutesSession, minutes );
00240 }
00241
00242 bool Task::remove(
QPtrList<Task>& activeTasks,
KarmStorage* storage)
00243 {
00244 kdDebug(5970) <<
"Task::remove: " << _name << endl;
00245
00246
bool ok =
true;
00247
00248 _removing =
true;
00249 storage->
removeTask(
this);
00250
if(
isRunning() )
setRunning(
false, storage );
00251
00252
for (
Task* child = this->
firstChild(); child; child = child->
nextSibling())
00253 {
00254
if (child->
isRunning())
00255 child->
setRunning(
false, storage);
00256 child->
remove(activeTasks, storage);
00257 }
00258
00259 changeParentTotalTimes( -_sessionTime, -_time);
00260
00261 _removing =
false;
00262
00263
return ok;
00264 }
00265
00266 void Task::updateActiveIcon()
00267 {
00268 _currentPic = (_currentPic+1) % 8;
00269 setPixmap(1, *(*icons)[_currentPic]);
00270 }
00271
00272 QString Task::fullName()
const
00273
{
00274
if (
isRoot())
00275
return name();
00276
else
00277
return parent()->
fullName() + QString::fromLatin1(
"/") +
name();
00278 }
00279
00280 KCal::Todo*
Task::asTodo(KCal::Todo* todo)
const
00281
{
00282
00283 todo->setSummary(
name() );
00284
00285
00286
00287
00288
00289
00290 todo->setCustomProperty( kapp->instanceName(),
00291
QCString(
"totalTaskTime" ), QString::number( _time ) );
00292 todo->setCustomProperty( kapp->instanceName(),
00293
QCString(
"totalSessionTime" ), QString::number( _sessionTime) );
00294
00295
if (getDesktopStr().isEmpty())
00296 todo->removeCustomProperty(kapp->instanceName(),
QCString(
"desktopList"));
00297
else
00298 todo->setCustomProperty( kapp->instanceName(),
00299
QCString(
"desktopList" ), getDesktopStr() );
00300
00301 todo->setOrganizer( Preferences::instance()->userRealName() );
00302
00303 todo->setPercentComplete(_percentcomplete);
00304
00305
return todo;
00306 }
00307
00308
bool Task::parseIncidence( KCal::Incidence* incident,
long& minutes,
00309
long& sessionMinutes,
QString& name,
DesktopList& desktops,
00310
int& percent_complete )
00311 {
00312
bool ok;
00313
00314 name = incident->summary();
00315 _uid = incident->uid();
00316
00317 _comment = incident->description();
00318
00319 ok =
false;
00320 minutes = incident->customProperty( kapp->instanceName(),
00321
QCString(
"totalTaskTime" )).toInt( &ok );
00322
if ( !ok )
00323 minutes = 0;
00324
00325 ok =
false;
00326 sessionMinutes = incident->customProperty( kapp->instanceName(),
00327
QCString(
"totalSessionTime" )).toInt( &ok );
00328
if ( !ok )
00329 sessionMinutes = 0;
00330
00331
QString desktopList = incident->customProperty( kapp->instanceName(),
00332
QCString(
"desktopList" ) );
00333
QStringList desktopStrList = QStringList::split( QString::fromLatin1(
","),
00334 desktopList );
00335 desktops.clear();
00336
00337
for ( QStringList::iterator iter = desktopStrList.begin();
00338 iter != desktopStrList.end();
00339 ++iter ) {
00340
int desktopInt = (*iter).toInt( &ok );
00341
if ( ok ) {
00342 desktops.push_back( desktopInt );
00343 }
00344 }
00345
00346 percent_complete = static_cast<KCal::Todo*>(incident)->percentComplete();
00347
00348
00349
00350
00351
00352
return true;
00353 }
00354
00355
QString Task::getDesktopStr()
const
00356
{
00357
if ( _desktops.empty() )
00358
return QString();
00359
00360
QString desktopstr;
00361
for ( DesktopList::const_iterator iter = _desktops.begin();
00362 iter != _desktops.
end();
00363 ++iter ) {
00364 desktopstr += QString::number( *iter ) + QString::fromLatin1(
"," );
00365 }
00366 desktopstr.remove( desktopstr.length() - 1, 1 );
00367
return desktopstr;
00368 }
00369
00370 void Task::cut()
00371 {
00372
00373 changeParentTotalTimes( -_totalSessionTime, -_totalTime);
00374
if ( ! parent())
00375 listView()->takeItem(
this);
00376
else
00377 parent()->takeItem(
this);
00378 }
00379
00380 void Task::move(
Task* destination)
00381 {
00382
cut();
00383
paste(destination);
00384 }
00385
00386 void Task::paste(
Task* destination)
00387 {
00388 destination->insertItem(
this);
00389 changeParentTotalTimes( _totalSessionTime, _totalTime);
00390 }
00391
00392 void Task::update()
00393 {
00394 setText(0, _name);
00395 setText(1, formatTime(_sessionTime));
00396 setText(2, formatTime(_time));
00397 setText(3, formatTime(_totalSessionTime));
00398 setText(4, formatTime(_totalTime));
00399 }
00400
00401 void Task::addComment(
QString comment,
KarmStorage* storage )
00402 {
00403 _comment = _comment + QString::fromLatin1(
"\n") + comment;
00404 storage->
addComment(
this, comment);
00405 }
00406
00407 QString Task::comment()
const
00408
{
00409
return _comment;
00410 }
00411
00412 int Task::compare (
QListViewItem * i,
int col,
bool ascending )
const
00413
{
00414
long thistime = 0;
00415
long thattime = 0;
00416
Task *task = static_cast<Task*>(i);
00417
00418
switch ( col )
00419 {
00420
case 1:
00421 thistime = _sessionTime;
00422 thattime = task->
sessionTime();
00423
break;
00424
case 2:
00425 thistime = _time;
00426 thattime = task->
time();
00427
break;
00428
case 3:
00429 thistime = _totalSessionTime;
00430 thattime = task->
totalSessionTime();
00431
break;
00432
case 4:
00433 thistime = _totalTime;
00434 thattime = task->
totalTime();
00435
break;
00436
default:
00437
return key(col, ascending).localeAwareCompare( i->key(col, ascending) );
00438 }
00439
00440
if ( thistime < thattime )
return -1;
00441
if ( thistime > thattime )
return 1;
00442
return 0;
00443
00444 }
00445
00446
#include "task.moc"