~ubuntu-branches/ubuntu/karmic/kdepim/karmic-backports

« back to all changes in this revision

Viewing changes to akonadi/resources/kolabproxy/task.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Christian Mangold
  • Date: 2009-07-10 06:34:50 UTC
  • mfrom: (1.1.40 upstream)
  • Revision ID: james.westby@ubuntu.com-20090710063450-neojgew2fh0n3y0u
Tags: 4:4.2.96-0ubuntu1
* New upstream release
* Bump kde build-deps to 4.2.96

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
    This file is part of the kolab resource - the implementation of the
3
 
    Kolab storage format. See www.kolab.org for documentation on this.
4
 
 
5
 
    Copyright (c) 2004 Bo Thorsen <bo@sonofthor.dk>
6
 
 
7
 
    This library is free software; you can redistribute it and/or
8
 
    modify it under the terms of the GNU Library General Public
9
 
    License as published by the Free Software Foundation; either
10
 
    version 2 of the License, or (at your option) any later version.
11
 
 
12
 
    This library is distributed in the hope that it will be useful,
13
 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 
    Library General Public License for more details.
16
 
 
17
 
    You should have received a copy of the GNU Library General Public License
18
 
    along with this library; see the file COPYING.LIB.  If not, write to
19
 
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20
 
    Boston, MA 02110-1301, USA.
21
 
 
22
 
    In addition, as a special exception, the copyright holders give
23
 
    permission to link the code of this program with any edition of
24
 
    the Qt library by Trolltech AS, Norway (or with modified versions
25
 
    of Qt that use the same license as Qt), and distribute linked
26
 
    combinations including the two.  You must obey the GNU General
27
 
    Public License in all respects for all of the code used other than
28
 
    Qt.  If you modify this file, you may extend this exception to
29
 
    your version of the file, but you are not obligated to do so.  If
30
 
    you do not wish to do so, delete this exception statement from
31
 
    your version.
32
 
*/
33
 
 
34
 
#include "task.h"
35
 
 
36
 
#include <kcal/todo.h>
37
 
#include <kdebug.h>
38
 
 
39
 
using namespace Kolab;
40
 
 
41
 
 
42
 
KCal::Todo* Task::xmlToTask( const QString& xml, const QString& tz )
43
 
{
44
 
  Task task( tz );
45
 
  task.load( xml );
46
 
  KCal::Todo* todo = new KCal::Todo();
47
 
  task.saveTo( todo );
48
 
  return todo;
49
 
}
50
 
 
51
 
QString Task::taskToXML( KCal::Todo* todo, const QString& tz )
52
 
{
53
 
  Task task( tz, todo );
54
 
  return task.saveXML();
55
 
}
56
 
 
57
 
Task::Task( const QString& tz, KCal::Todo* task )
58
 
  : Incidence( tz, task ),
59
 
    mPriority( 5 ), mPercentCompleted( 0 ),
60
 
    mStatus( KCal::Incidence::StatusNone ),
61
 
    mHasStartDate( false ), mHasDueDate( false ),
62
 
    mHasCompletedDate( false )
63
 
{
64
 
  if ( task )
65
 
    setFields( task );
66
 
}
67
 
 
68
 
Task::~Task()
69
 
{
70
 
}
71
 
 
72
 
void Task::setPriority( int priority )
73
 
{
74
 
  mPriority = priority;
75
 
}
76
 
 
77
 
int Task::priority() const
78
 
{
79
 
  return mPriority;
80
 
}
81
 
 
82
 
void Task::setPercentCompleted( int percent )
83
 
{
84
 
  mPercentCompleted = percent;
85
 
}
86
 
 
87
 
int Task::percentCompleted() const
88
 
{
89
 
  return mPercentCompleted;
90
 
}
91
 
 
92
 
void Task::setStatus( KCal::Incidence::Status status )
93
 
{
94
 
  mStatus = status;
95
 
}
96
 
 
97
 
KCal::Incidence::Status Task::status() const
98
 
{
99
 
  return mStatus;
100
 
}
101
 
 
102
 
void Task::setParent( const QString& parentUid )
103
 
{
104
 
  mParent = parentUid;
105
 
}
106
 
 
107
 
QString Task::parent() const
108
 
{
109
 
  return mParent;
110
 
}
111
 
 
112
 
void Task::setDueDate( const KDateTime& date )
113
 
{
114
 
  mDueDate = date;
115
 
  mHasDueDate = true;
116
 
}
117
 
 
118
 
KDateTime Task::dueDate() const
119
 
{
120
 
  return mDueDate;
121
 
}
122
 
 
123
 
void Task::setHasStartDate( bool v )
124
 
{
125
 
  mHasStartDate = v;
126
 
}
127
 
 
128
 
bool Task::hasStartDate() const
129
 
{
130
 
  return mHasStartDate;
131
 
}
132
 
 
133
 
bool Task::hasDueDate() const
134
 
{
135
 
  return mHasDueDate;
136
 
}
137
 
 
138
 
void Task::setCompletedDate( const KDateTime& date )
139
 
{
140
 
  mCompletedDate = date;
141
 
  mHasCompletedDate = true;
142
 
}
143
 
 
144
 
KDateTime Task::completedDate() const
145
 
{
146
 
  return mCompletedDate;
147
 
}
148
 
 
149
 
bool Task::hasCompletedDate() const
150
 
{
151
 
  return mHasCompletedDate;
152
 
}
153
 
 
154
 
bool Task::loadAttribute( QDomElement& element )
155
 
{
156
 
  QString tagName = element.tagName();
157
 
 
158
 
  if ( tagName == "priority" ) {
159
 
    bool ok;
160
 
    int priority = element.text().toInt( &ok );
161
 
    if ( !ok || priority < 0 || priority > 5 )
162
 
      priority = 3;
163
 
    setPriority( priority );
164
 
  } else if ( tagName == "completed" ) {
165
 
    bool ok;
166
 
    int percent = element.text().toInt( &ok );
167
 
    if ( !ok || percent < 0 || percent > 100 )
168
 
      percent = 0;
169
 
    setPercentCompleted( percent );
170
 
  } else if ( tagName == "status" ) {
171
 
    if ( element.text() == "in-progress" )
172
 
      setStatus( KCal::Incidence::StatusInProcess );
173
 
    else if ( element.text() == "completed" )
174
 
      setStatus( KCal::Incidence::StatusCompleted );
175
 
    else if ( element.text() == "waiting-on-someone-else" )
176
 
      setStatus( KCal::Incidence::StatusNeedsAction );
177
 
    else if ( element.text() == "deferred" )
178
 
      // Guessing a status here
179
 
      setStatus( KCal::Incidence::StatusCanceled );
180
 
    else
181
 
      // Default
182
 
      setStatus( KCal::Incidence::StatusNone );
183
 
  } else if ( tagName == "due-date" )
184
 
    setDueDate( stringToDateTime( element.text() ) );
185
 
  else if ( tagName == "parent" )
186
 
    setParent( element.text() );
187
 
  else if ( tagName == "x-completed-date" )
188
 
    setCompletedDate( stringToDateTime( element.text() ) );
189
 
  else if ( tagName == "start-date" ) {
190
 
    setHasStartDate( true );
191
 
    setStartDate( element.text() );
192
 
  } else
193
 
    return Incidence::loadAttribute( element );
194
 
 
195
 
  // We handled this
196
 
  return true;
197
 
}
198
 
 
199
 
bool Task::saveAttributes( QDomElement& element ) const
200
 
{
201
 
  // Save the base class elements
202
 
  Incidence::saveAttributes( element );
203
 
 
204
 
  writeString( element, "priority", QString::number( priority() ) );
205
 
  writeString( element, "completed", QString::number( percentCompleted() ) );
206
 
 
207
 
  switch( status() ) {
208
 
  case KCal::Incidence::StatusInProcess:
209
 
    writeString( element, "status", "in-progress" );
210
 
    break;
211
 
  case KCal::Incidence::StatusCompleted:
212
 
    writeString( element, "status", "completed" );
213
 
    break;
214
 
  case KCal::Incidence::StatusNeedsAction:
215
 
    writeString( element, "status", "waiting-on-someone-else" );
216
 
    break;
217
 
  case KCal::Incidence::StatusCanceled:
218
 
    writeString( element, "status", "deferred" );
219
 
    break;
220
 
  case KCal::Incidence::StatusNone:
221
 
    writeString( element, "status", "not-started" );
222
 
    break;
223
 
  case KCal::Incidence::StatusTentative:
224
 
  case KCal::Incidence::StatusConfirmed:
225
 
  case KCal::Incidence::StatusDraft:
226
 
  case KCal::Incidence::StatusFinal:
227
 
  case KCal::Incidence::StatusX:
228
 
    // All of these are saved as StatusNone.
229
 
    writeString( element, "status", "not-started" );
230
 
    break;
231
 
  }
232
 
 
233
 
  if ( hasDueDate() )
234
 
    writeString( element, "due-date", dateTimeToString( dueDate() ) );
235
 
 
236
 
  if ( !parent().isNull() )
237
 
    writeString( element, "parent", parent() );
238
 
 
239
 
  if ( hasCompletedDate() && percentCompleted() == 100)
240
 
    writeString( element, "x-completed-date", dateTimeToString( completedDate() ) );
241
 
 
242
 
  return true;
243
 
}
244
 
 
245
 
 
246
 
bool Task::loadXML( const QDomDocument& document )
247
 
{
248
 
  QDomElement top = document.documentElement();
249
 
 
250
 
  if ( top.tagName() != "task" ) {
251
 
    qWarning( "XML error: Top tag was %s instead of the expected task",
252
 
              top.tagName().toAscii().data() );
253
 
    return false;
254
 
  }
255
 
  setHasStartDate( false ); // todo's don't necessarily have one
256
 
 
257
 
  for ( QDomNode n = top.firstChild(); !n.isNull(); n = n.nextSibling() ) {
258
 
    if ( n.isComment() )
259
 
      continue;
260
 
    if ( n.isElement() ) {
261
 
      QDomElement e = n.toElement();
262
 
      if ( !loadAttribute( e ) )
263
 
        // TODO: Unhandled tag - save for later storage
264
 
        kDebug() <<"Warning: Unhandled tag" << e.tagName();
265
 
    } else
266
 
      kDebug() <<"Node is not a comment or an element???";
267
 
  }
268
 
 
269
 
  return true;
270
 
}
271
 
 
272
 
QString Task::saveXML() const
273
 
{
274
 
  QDomDocument document = domTree();
275
 
  QDomElement element = document.createElement( "task" );
276
 
  element.setAttribute( "version", "1.0" );
277
 
  saveAttributes( element );
278
 
  if ( !hasStartDate() && startDate().isValid() ) {
279
 
    // events and journals always have a start date, but tasks don't.
280
 
    // Remove the entry done by the inherited save above, because we
281
 
    // don't have one.
282
 
    QDomNodeList l = element.elementsByTagName( "start-date" );
283
 
    Q_ASSERT( l.count() == 1 );
284
 
    element.removeChild( l.item( 0 ) );
285
 
  }
286
 
  document.appendChild( element );
287
 
  return document.toString();
288
 
}
289
 
 
290
 
void Task::setFields( const KCal::Todo* task )
291
 
{
292
 
  Incidence::setFields( task );
293
 
 
294
 
  setPriority( task->priority() );
295
 
  setPercentCompleted( task->percentComplete() );
296
 
  setStatus( task->status() );
297
 
  setHasStartDate( task->hasStartDate() );
298
 
 
299
 
  if ( task->hasDueDate() )
300
 
    setDueDate( localToUTC( task->dtDue() ) );
301
 
  else
302
 
    mHasDueDate = false;
303
 
  if ( task->relatedTo() )
304
 
    setParent( task->relatedTo()->uid() );
305
 
  else if ( !task->relatedToUid().isEmpty() )
306
 
    setParent( task->relatedToUid( ) );
307
 
  else
308
 
    setParent( QString() );
309
 
 
310
 
  if ( task->hasCompletedDate() && task->percentComplete() == 100 )
311
 
    setCompletedDate( localToUTC( task->completed() ) );
312
 
  else
313
 
    mHasCompletedDate = false;
314
 
}
315
 
 
316
 
void Task::saveTo( KCal::Todo* task )
317
 
{
318
 
  Incidence::saveTo( task );
319
 
 
320
 
  task->setPriority( priority() );
321
 
  task->setPercentComplete( percentCompleted() );
322
 
  task->setStatus( status() );
323
 
  task->setHasStartDate( hasStartDate() );
324
 
  task->setHasDueDate( hasDueDate() );
325
 
  if ( hasDueDate() )
326
 
    task->setDtDue( utcToLocal( dueDate() ) );
327
 
 
328
 
  if ( !parent().isEmpty() )
329
 
    task->setRelatedToUid( parent() );
330
 
 
331
 
  if ( hasCompletedDate() && task->percentComplete() == 100 )
332
 
    task->setCompleted( utcToLocal( mCompletedDate ) );
333
 
}