You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
462 lines
13 KiB
462 lines
13 KiB
15 years ago
|
/*
|
||
|
This file is part of the kolab resource - the implementation of the
|
||
|
Kolab storage format. See www.kolab.org for documentation on this.
|
||
|
|
||
|
Copyright (c) 2004 Bo Thorsen <bo@sonofthor.dk>
|
||
|
|
||
|
This library is free software; you can redistribute it and/or
|
||
|
modify it under the terms of the GNU Library General Public
|
||
|
License as published by the Free Software Foundation; either
|
||
|
version 2 of the License, or (at your option) any later version.
|
||
|
|
||
|
This library is distributed in the hope that it will be useful,
|
||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
|
Library General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU Library General Public License
|
||
|
along with this library; see the file COPYING.LIB. If not, write to
|
||
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||
|
Boston, MA 02110-1301, USA.
|
||
|
|
||
|
In addition, as a special exception, the copyright holders give
|
||
|
permission to link the code of this program with any edition of
|
||
14 years ago
|
the TQt library by Trolltech AS, Norway (or with modified versions
|
||
|
of TQt that use the same license as TQt), and distribute linked
|
||
15 years ago
|
combinations including the two. You must obey the GNU General
|
||
|
Public License in all respects for all of the code used other than
|
||
14 years ago
|
TQt. If you modify this file, you may extend this exception to
|
||
15 years ago
|
your version of the file, but you are not obligated to do so. If
|
||
|
you do not wish to do so, delete this exception statement from
|
||
|
your version.
|
||
|
*/
|
||
|
|
||
|
#include "task.h"
|
||
|
|
||
|
#include <libkcal/todo.h>
|
||
|
#include <kdebug.h>
|
||
|
|
||
|
using namespace Kolab;
|
||
|
|
||
14 years ago
|
// Kolab Storage Specification:
|
||
|
// "The priority can be a number between 1 and 5, with 1 being the highest priority."
|
||
|
// iCalendar (RFC 2445):
|
||
|
// "The priority is specified as an integer in the range
|
||
|
// zero to nine. A value of zero specifies an
|
||
|
// undefined priority. A value of one is the
|
||
|
// highest priority. A value of nine is the lowest
|
||
|
// priority."
|
||
|
|
||
|
static int kcalPriorityToKolab( const int kcalPriority )
|
||
|
{
|
||
|
if ( kcalPriority >= 0 && kcalPriority <= 9 ) {
|
||
|
// We'll map undefined (0) to 3 (default)
|
||
|
// 0 1 2 3 4 5 6 7 8 9
|
||
|
static const int priorityMap[10] = { 3, 1, 1, 2, 2, 3, 3, 4, 4, 5 };
|
||
|
return priorityMap[kcalPriority];
|
||
|
}
|
||
|
else {
|
||
|
kdWarning() << "kcalPriorityToKolab(): Got invalid priority " << kcalPriority << endl;
|
||
|
return 3;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static int kolabPrioritytoKCal( const int kolabPriority )
|
||
|
{
|
||
|
if ( kolabPriority >= 1 && kolabPriority <= 5 ) {
|
||
|
// 1 2 3 4 5
|
||
|
static const int priorityMap[5] = { 1, 3, 5, 7, 9 };
|
||
|
return priorityMap[kolabPriority - 1];
|
||
|
}
|
||
|
else {
|
||
|
kdWarning() << "kolabPrioritytoKCal(): Got invalid priority " << kolabPriority << endl;
|
||
|
return 5;
|
||
|
}
|
||
|
}
|
||
15 years ago
|
|
||
15 years ago
|
KCal::Todo* Task::xmlToTask( const TQString& xml, const TQString& tz, KCal::ResourceKolab *res,
|
||
14 years ago
|
const TQString& subResource, TQ_UINT32 sernum )
|
||
15 years ago
|
{
|
||
|
Task task( res, subResource, sernum, tz );
|
||
|
task.load( xml );
|
||
|
KCal::Todo* todo = new KCal::Todo();
|
||
|
task.saveTo( todo );
|
||
|
return todo;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString Task::taskToXML( KCal::Todo* todo, const TQString& tz )
|
||
15 years ago
|
{
|
||
14 years ago
|
Task task( 0, TQString(), 0, tz, todo );
|
||
15 years ago
|
return task.saveXML();
|
||
|
}
|
||
|
|
||
14 years ago
|
Task::Task( KCal::ResourceKolab *res, const TQString &subResource, TQ_UINT32 sernum,
|
||
15 years ago
|
const TQString& tz, KCal::Todo* task )
|
||
15 years ago
|
: Incidence( res, subResource, sernum, tz ),
|
||
|
mPriority( 5 ), mPercentCompleted( 0 ),
|
||
13 years ago
|
mStatus( KCal::Incidence::StatusNone ),
|
||
15 years ago
|
mHasStartDate( false ), mHasDueDate( false ),
|
||
|
mHasCompletedDate( false )
|
||
|
{
|
||
|
if ( task )
|
||
|
setFields( task );
|
||
|
}
|
||
|
|
||
|
Task::~Task()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
void Task::setPriority( int priority )
|
||
|
{
|
||
|
mPriority = priority;
|
||
|
}
|
||
|
|
||
|
int Task::priority() const
|
||
|
{
|
||
|
return mPriority;
|
||
|
}
|
||
|
|
||
|
void Task::setPercentCompleted( int percent )
|
||
|
{
|
||
|
mPercentCompleted = percent;
|
||
|
}
|
||
|
|
||
|
int Task::percentCompleted() const
|
||
|
{
|
||
|
return mPercentCompleted;
|
||
|
}
|
||
|
|
||
13 years ago
|
void Task::setStatus( KCal::Incidence::Status status )
|
||
15 years ago
|
{
|
||
13 years ago
|
mStatus = status;
|
||
15 years ago
|
}
|
||
|
|
||
13 years ago
|
KCal::Incidence::Status Task::status() const
|
||
15 years ago
|
{
|
||
13 years ago
|
return mStatus;
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
void Task::setParent( const TQString& parentUid )
|
||
15 years ago
|
{
|
||
14 years ago
|
mParent = parentUid;
|
||
15 years ago
|
}
|
||
|
|
||
13 years ago
|
TQString Task::parent() const
|
||
15 years ago
|
{
|
||
|
return mParent;
|
||
|
}
|
||
|
|
||
15 years ago
|
void Task::setDueDate( const TQDateTime& date )
|
||
15 years ago
|
{
|
||
|
mDueDate = date;
|
||
|
mHasDueDate = true;
|
||
13 years ago
|
mFloatingStatus = HasTime;
|
||
14 years ago
|
}
|
||
|
|
||
|
void Task::setDueDate( const TQDate &date )
|
||
|
{
|
||
|
mDueDate = date;
|
||
|
mHasDueDate = true;
|
||
13 years ago
|
mFloatingStatus = AllDay;
|
||
14 years ago
|
}
|
||
|
|
||
|
|
||
|
void Task::setDueDate( const TQString &date )
|
||
|
{
|
||
|
if ( date.length() > 10 ) {
|
||
|
// This is a date + time
|
||
|
setDueDate( stringToDateTime( date ) );
|
||
|
} else {
|
||
|
// This is only a date
|
||
|
setDueDate( stringToDate( date ) );
|
||
|
}
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
TQDateTime Task::dueDate() const
|
||
15 years ago
|
{
|
||
|
return mDueDate;
|
||
|
}
|
||
|
|
||
|
void Task::setHasStartDate( bool v )
|
||
|
{
|
||
|
mHasStartDate = v;
|
||
|
}
|
||
|
|
||
|
bool Task::hasStartDate() const
|
||
|
{
|
||
|
return mHasStartDate;
|
||
|
}
|
||
|
|
||
|
bool Task::hasDueDate() const
|
||
|
{
|
||
|
return mHasDueDate;
|
||
|
}
|
||
|
|
||
15 years ago
|
void Task::setCompletedDate( const TQDateTime& date )
|
||
15 years ago
|
{
|
||
|
mCompletedDate = date;
|
||
|
mHasCompletedDate = true;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQDateTime Task::completedDate() const
|
||
15 years ago
|
{
|
||
|
return mCompletedDate;
|
||
|
}
|
||
|
|
||
|
bool Task::hasCompletedDate() const
|
||
|
{
|
||
|
return mHasCompletedDate;
|
||
|
}
|
||
|
|
||
15 years ago
|
bool Task::loadAttribute( TQDomElement& element )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQString tagName = element.tagName();
|
||
15 years ago
|
|
||
|
if ( tagName == "priority" ) {
|
||
|
bool ok;
|
||
14 years ago
|
mKolabPriorityFromDom = element.text().toInt( &ok );
|
||
|
if ( !ok || mKolabPriorityFromDom < 1 || mKolabPriorityFromDom > 5 ) {
|
||
|
kdWarning() << "loadAttribute(): Invalid \"priority\" value: " << element.text() << endl;
|
||
|
mKolabPriorityFromDom = -1;
|
||
|
}
|
||
|
} else if ( tagName == "x-kcal-priority" ) {
|
||
|
bool ok;
|
||
|
mKCalPriorityFromDom = element.text().toInt( &ok );
|
||
|
if ( !ok || mKCalPriorityFromDom < 0 || mKCalPriorityFromDom > 9 ) {
|
||
|
kdWarning() << "loadAttribute(): Invalid \"x-kcal-priority\" value: " << element.text() << endl;
|
||
|
mKCalPriorityFromDom = -1;
|
||
|
}
|
||
15 years ago
|
} else if ( tagName == "completed" ) {
|
||
|
bool ok;
|
||
|
int percent = element.text().toInt( &ok );
|
||
|
if ( !ok || percent < 0 || percent > 100 )
|
||
|
percent = 0;
|
||
|
setPercentCompleted( percent );
|
||
|
} else if ( tagName == "status" ) {
|
||
|
if ( element.text() == "in-progress" )
|
||
13 years ago
|
setStatus( KCal::Incidence::StatusInProcess );
|
||
15 years ago
|
else if ( element.text() == "completed" )
|
||
13 years ago
|
setStatus( KCal::Incidence::StatusCompleted );
|
||
15 years ago
|
else if ( element.text() == "waiting-on-someone-else" )
|
||
13 years ago
|
setStatus( KCal::Incidence::StatusNeedsAction );
|
||
15 years ago
|
else if ( element.text() == "deferred" )
|
||
|
// Guessing a status here
|
||
13 years ago
|
setStatus( KCal::Incidence::StatusCanceled );
|
||
15 years ago
|
else
|
||
|
// Default
|
||
13 years ago
|
setStatus( KCal::Incidence::StatusNone );
|
||
14 years ago
|
} else if ( tagName == "due-date" ) {
|
||
|
setDueDate( element.text() );
|
||
13 years ago
|
} else if ( tagName == "parent" ) {
|
||
15 years ago
|
setParent( element.text() );
|
||
14 years ago
|
} else if ( tagName == "x-completed-date" ) {
|
||
15 years ago
|
setCompletedDate( stringToDateTime( element.text() ) );
|
||
14 years ago
|
} else if ( tagName == "start-date" ) {
|
||
15 years ago
|
setHasStartDate( true );
|
||
|
setStartDate( element.text() );
|
||
|
} else
|
||
|
return Incidence::loadAttribute( element );
|
||
|
|
||
|
// We handled this
|
||
|
return true;
|
||
|
}
|
||
|
|
||
15 years ago
|
bool Task::saveAttributes( TQDomElement& element ) const
|
||
15 years ago
|
{
|
||
|
// Save the base class elements
|
||
|
Incidence::saveAttributes( element );
|
||
|
|
||
14 years ago
|
// We need to save x-kcal-priority as well, since the Kolab priority can only save values from
|
||
|
// 1 to 5, but we have values from 0 to 9, and do not want to loose them
|
||
|
writeString( element, "priority", TQString::number( kcalPriorityToKolab( priority() ) ) );
|
||
|
writeString( element, "x-kcal-priority", TQString::number( priority() ) );
|
||
|
|
||
15 years ago
|
writeString( element, "completed", TQString::number( percentCompleted() ) );
|
||
15 years ago
|
|
||
|
switch( status() ) {
|
||
|
case KCal::Incidence::StatusInProcess:
|
||
|
writeString( element, "status", "in-progress" );
|
||
|
break;
|
||
|
case KCal::Incidence::StatusCompleted:
|
||
|
writeString( element, "status", "completed" );
|
||
|
break;
|
||
|
case KCal::Incidence::StatusNeedsAction:
|
||
|
writeString( element, "status", "waiting-on-someone-else" );
|
||
|
break;
|
||
|
case KCal::Incidence::StatusCanceled:
|
||
|
writeString( element, "status", "deferred" );
|
||
|
break;
|
||
|
case KCal::Incidence::StatusNone:
|
||
|
writeString( element, "status", "not-started" );
|
||
|
break;
|
||
|
case KCal::Incidence::StatusTentative:
|
||
|
case KCal::Incidence::StatusConfirmed:
|
||
|
case KCal::Incidence::StatusDraft:
|
||
|
case KCal::Incidence::StatusFinal:
|
||
|
case KCal::Incidence::StatusX:
|
||
|
// All of these are saved as StatusNone.
|
||
|
writeString( element, "status", "not-started" );
|
||
|
break;
|
||
|
}
|
||
|
|
||
14 years ago
|
if ( hasDueDate() ) {
|
||
13 years ago
|
if ( mFloatingStatus == HasTime ) {
|
||
14 years ago
|
writeString( element, "due-date", dateTimeToString( dueDate() ) );
|
||
|
} else {
|
||
|
writeString( element, "due-date", dateToString( dueDate().date() ) );
|
||
|
}
|
||
|
}
|
||
15 years ago
|
|
||
13 years ago
|
if ( !parent().isNull() ) {
|
||
|
writeString( element, "parent", parent() );
|
||
14 years ago
|
}
|
||
15 years ago
|
|
||
14 years ago
|
if ( hasCompletedDate() && percentCompleted() == 100 ) {
|
||
15 years ago
|
writeString( element, "x-completed-date", dateTimeToString( completedDate() ) );
|
||
14 years ago
|
}
|
||
15 years ago
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
|
||
15 years ago
|
bool Task::loadXML( const TQDomDocument& document )
|
||
15 years ago
|
{
|
||
14 years ago
|
mKolabPriorityFromDom = -1;
|
||
|
mKCalPriorityFromDom = -1;
|
||
|
|
||
15 years ago
|
TQDomElement top = document.documentElement();
|
||
15 years ago
|
|
||
|
if ( top.tagName() != "task" ) {
|
||
13 years ago
|
tqWarning( "XML error: Top tag was %s instead of the expected task",
|
||
15 years ago
|
top.tagName().ascii() );
|
||
|
return false;
|
||
|
}
|
||
|
setHasStartDate( false ); // todo's don't necessarily have one
|
||
|
|
||
15 years ago
|
for ( TQDomNode n = top.firstChild(); !n.isNull(); n = n.nextSibling() ) {
|
||
15 years ago
|
if ( n.isComment() )
|
||
|
continue;
|
||
|
if ( n.isElement() ) {
|
||
15 years ago
|
TQDomElement e = n.toElement();
|
||
15 years ago
|
if ( !loadAttribute( e ) )
|
||
|
// TODO: Unhandled tag - save for later storage
|
||
|
kdDebug() << "Warning: Unhandled tag " << e.tagName() << endl;
|
||
|
} else
|
||
|
kdDebug() << "Node is not a comment or an element???" << endl;
|
||
|
}
|
||
|
|
||
|
loadAttachments();
|
||
14 years ago
|
decideAndSetPriority();
|
||
15 years ago
|
return true;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString Task::saveXML() const
|
||
15 years ago
|
{
|
||
15 years ago
|
TQDomDocument document = domTree();
|
||
|
TQDomElement element = document.createElement( "task" );
|
||
15 years ago
|
element.setAttribute( "version", "1.0" );
|
||
|
saveAttributes( element );
|
||
14 years ago
|
if ( !hasStartDate() && startDate().isValid() ) {
|
||
15 years ago
|
// events and journals always have a start date, but tasks don't.
|
||
|
// Remove the entry done by the inherited save above, because we
|
||
|
// don't have one.
|
||
15 years ago
|
TQDomNodeList l = element.elementsByTagName( "start-date" );
|
||
15 years ago
|
Q_ASSERT( l.count() == 1 );
|
||
|
element.removeChild( l.item( 0 ) );
|
||
|
}
|
||
|
document.appendChild( element );
|
||
|
return document.toString();
|
||
|
}
|
||
|
|
||
|
void Task::setFields( const KCal::Todo* task )
|
||
|
{
|
||
|
Incidence::setFields( task );
|
||
|
|
||
|
setPriority( task->priority() );
|
||
|
setPercentCompleted( task->percentComplete() );
|
||
13 years ago
|
setStatus( task->status() );
|
||
15 years ago
|
setHasStartDate( task->hasStartDate() );
|
||
|
|
||
14 years ago
|
if ( task->hasDueDate() ) {
|
||
15 years ago
|
setDueDate( localToUTC( task->dtDue() ) );
|
||
14 years ago
|
if ( task->doesFloat() ) {
|
||
|
// This is a floating task. Don't timezone move this one
|
||
13 years ago
|
mFloatingStatus = AllDay;
|
||
14 years ago
|
setDueDate( task->dtDue().date() );
|
||
|
} else {
|
||
13 years ago
|
mFloatingStatus = HasTime;
|
||
14 years ago
|
setDueDate( localToUTC( task->dtDue() ) );
|
||
|
}
|
||
|
} else {
|
||
15 years ago
|
mHasDueDate = false;
|
||
14 years ago
|
}
|
||
|
|
||
|
if ( task->relatedTo() ) {
|
||
15 years ago
|
setParent( task->relatedTo()->uid() );
|
||
14 years ago
|
} else if ( !task->relatedToUid().isEmpty() ) {
|
||
|
setParent( task->relatedToUid( ) );
|
||
|
} else {
|
||
14 years ago
|
setParent( TQString() );
|
||
14 years ago
|
}
|
||
15 years ago
|
|
||
14 years ago
|
if ( task->hasCompletedDate() && task->percentComplete() == 100 ) {
|
||
15 years ago
|
setCompletedDate( localToUTC( task->completed() ) );
|
||
14 years ago
|
} else {
|
||
15 years ago
|
mHasCompletedDate = false;
|
||
14 years ago
|
}
|
||
|
}
|
||
|
|
||
|
void Task::decideAndSetPriority()
|
||
|
{
|
||
|
// If we have both Kolab and KCal values in the XML, we prefer the KCal value, but only if the
|
||
|
// values are still in sync
|
||
|
if ( mKolabPriorityFromDom != -1 && mKCalPriorityFromDom != -1 ) {
|
||
|
const bool inSync = ( kcalPriorityToKolab( mKCalPriorityFromDom ) == mKolabPriorityFromDom );
|
||
|
if ( inSync ) {
|
||
|
setPriority( mKCalPriorityFromDom );
|
||
|
}
|
||
|
else {
|
||
|
// Out of sync, some other client changed the Kolab priority, so we have to ignore our
|
||
|
// KCal priority
|
||
|
setPriority( kolabPrioritytoKCal( mKolabPriorityFromDom ) );
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Only KCal priority set, use that.
|
||
|
else if ( mKolabPriorityFromDom == -1 && mKCalPriorityFromDom != -1 ) {
|
||
|
kdWarning() << "decideAndSetPriority(): No Kolab priority found, only the KCal priority!" << endl;
|
||
|
setPriority( mKCalPriorityFromDom );
|
||
|
}
|
||
|
|
||
|
// Only Kolab priority set, use that
|
||
|
else if ( mKolabPriorityFromDom != -1 && mKCalPriorityFromDom == -1 ) {
|
||
|
setPriority( kolabPrioritytoKCal( mKolabPriorityFromDom ) );
|
||
|
}
|
||
|
|
||
|
// No priority set, use the default
|
||
|
else {
|
||
|
// According the RFC 2445, we should use 0 here, for undefined priority, but AFAIK KOrganizer
|
||
|
// doesn't support that, so we'll use 5.
|
||
|
setPriority( 5 );
|
||
|
}
|
||
15 years ago
|
}
|
||
|
|
||
|
void Task::saveTo( KCal::Todo* task )
|
||
|
{
|
||
|
Incidence::saveTo( task );
|
||
|
|
||
|
task->setPriority( priority() );
|
||
|
task->setPercentComplete( percentCompleted() );
|
||
13 years ago
|
task->setStatus( status() );
|
||
15 years ago
|
task->setHasStartDate( hasStartDate() );
|
||
|
task->setHasDueDate( hasDueDate() );
|
||
|
if ( hasDueDate() )
|
||
|
task->setDtDue( utcToLocal( dueDate() ) );
|
||
|
|
||
13 years ago
|
if ( !parent().isNull() )
|
||
|
task->setRelatedToUid( parent() );
|
||
15 years ago
|
|
||
|
if ( hasCompletedDate() && task->percentComplete() == 100 )
|
||
|
task->setCompleted( utcToLocal( mCompletedDate ) );
|
||
|
}
|