File: task.cpp

package info (click to toggle)
kdepim-runtime 4%3A24.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,012 kB
  • sloc: cpp: 90,562; xml: 1,020; javascript: 60; sh: 58; makefile: 13
file content (325 lines) | stat: -rw-r--r-- 9,140 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
    This file is part of the kolab resource - the implementation of the
    Kolab storage format. See www.kolab.org for documentation on this.

    SPDX-FileCopyrightText: 2004 Bo Thorsen <bo@sonofthor.dk>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "task.h"
#include "pimkolab_debug.h"

using namespace KolabV2;

KCalendarCore::Todo::Ptr Task::fromXml(const QDomDocument &xmlDoc, const QString &tz)
{
    Task task(tz);
    task.loadXML(xmlDoc);
    KCalendarCore::Todo::Ptr todo(new KCalendarCore::Todo());
    task.saveTo(todo);
    return todo;
}

QString Task::taskToXML(const KCalendarCore::Todo::Ptr &todo, const QString &tz)
{
    Task task(tz, todo);
    return task.saveXML();
}

Task::Task(const QString &tz, const KCalendarCore::Todo::Ptr &task)
    : Incidence(tz, task)
    , mPercentCompleted(0)
    , mStatus(KCalendarCore::Incidence::StatusNone)
{
    if (task) {
        setFields(task);
    }
}

Task::~Task() = default;

void Task::setPercentCompleted(int percent)
{
    mPercentCompleted = percent;
}

int Task::percentCompleted() const
{
    return mPercentCompleted;
}

void Task::setStatus(KCalendarCore::Incidence::Status status)
{
    mStatus = status;
}

KCalendarCore::Incidence::Status Task::status() const
{
    return mStatus;
}

void Task::setParent(const QString &parentUid)
{
    mParent = parentUid;
}

QString Task::parent() const
{
    return mParent;
}

void Task::setDueDate(const QDateTime &date)
{
    mDueDate = date;
    mHasDueDate = true;
}

void Task::setDueDate(const QDate &date)
{
    mDueDate = QDateTime(date, QTime());
    mHasDueDate = true;
    mFloatingStatus = AllDay;
}

void Task::setDueDate(const QString &date)
{
    if (date.length() > 10) {
        // This is a date + time
        setDueDate(stringToDateTime(date));
    } else {
        // This is only a date
        setDueDate(stringToDate(date));
    }
}

QDateTime Task::dueDate() const
{
    return mDueDate;
}

void Task::setHasStartDate(bool v)
{
    mHasStartDate = v;
}

bool Task::hasStartDate() const
{
    return mHasStartDate;
}

bool Task::hasDueDate() const
{
    return mHasDueDate;
}

void Task::setCompletedDate(const QDateTime &date)
{
    mCompletedDate = date;
    mHasCompletedDate = true;
}

QDateTime Task::completedDate() const
{
    return mCompletedDate;
}

bool Task::hasCompletedDate() const
{
    return mHasCompletedDate;
}

bool Task::loadAttribute(QDomElement &element)
{
    QString tagName = element.tagName();

    if (tagName == QLatin1StringView("completed")) {
        bool ok;
        int percent = element.text().toInt(&ok);
        if (!ok || percent < 0 || percent > 100) {
            percent = 0;
        }
        setPercentCompleted(percent);
    } else if (tagName == QLatin1StringView("status")) {
        if (element.text() == QLatin1StringView("in-progress")) {
            setStatus(KCalendarCore::Incidence::StatusInProcess);
        } else if (element.text() == QLatin1StringView("completed")) {
            setStatus(KCalendarCore::Incidence::StatusCompleted);
        } else if (element.text() == QLatin1StringView("waiting-on-someone-else")) {
            setStatus(KCalendarCore::Incidence::StatusNeedsAction);
        } else if (element.text() == QLatin1StringView("deferred")) {
            // Guessing a status here
            setStatus(KCalendarCore::Incidence::StatusCanceled);
        } else {
            // Default
            setStatus(KCalendarCore::Incidence::StatusNone);
        }
    } else if (tagName == QLatin1StringView("due-date")) {
        setDueDate(element.text());
    } else if (tagName == QLatin1StringView("parent")) {
        setParent(element.text());
    } else if (tagName == QLatin1StringView("x-completed-date")) {
        setCompletedDate(stringToDateTime(element.text()));
    } else if (tagName == QLatin1StringView("start-date")) {
        setHasStartDate(true);
        setStartDate(element.text());
    } else {
        return Incidence::loadAttribute(element);
    }

    // We handled this
    return true;
}

bool Task::saveAttributes(QDomElement &element) const
{
    // Save the base class elements
    Incidence::saveAttributes(element);

    writeString(element, QStringLiteral("completed"), QString::number(percentCompleted()));

    switch (status()) {
    case KCalendarCore::Incidence::StatusInProcess:
        writeString(element, QStringLiteral("status"), QStringLiteral("in-progress"));
        break;
    case KCalendarCore::Incidence::StatusCompleted:
        writeString(element, QStringLiteral("status"), QStringLiteral("completed"));
        break;
    case KCalendarCore::Incidence::StatusNeedsAction:
        writeString(element, QStringLiteral("status"), QStringLiteral("waiting-on-someone-else"));
        break;
    case KCalendarCore::Incidence::StatusCanceled:
        writeString(element, QStringLiteral("status"), QStringLiteral("deferred"));
        break;
    case KCalendarCore::Incidence::StatusNone:
        writeString(element, QStringLiteral("status"), QStringLiteral("not-started"));
        break;
    case KCalendarCore::Incidence::StatusTentative:
    case KCalendarCore::Incidence::StatusConfirmed:
    case KCalendarCore::Incidence::StatusDraft:
    case KCalendarCore::Incidence::StatusFinal:
    case KCalendarCore::Incidence::StatusX:
        // All of these are saved as StatusNone.
        writeString(element, QStringLiteral("status"), QStringLiteral("not-started"));
        break;
    }

    if (hasDueDate()) {
        if (mFloatingStatus == HasTime) {
            writeString(element, QStringLiteral("due-date"), dateTimeToString(dueDate()));
        } else {
            writeString(element, QStringLiteral("due-date"), dateToString(dueDate().date()));
        }
    }

    if (!parent().isNull()) {
        writeString(element, QStringLiteral("parent"), parent());
    }

    if (hasCompletedDate() && percentCompleted() == 100) {
        writeString(element, QStringLiteral("x-completed-date"), dateTimeToString(completedDate()));
    }

    return true;
}

bool Task::loadXML(const QDomDocument &document)
{
    QDomElement top = document.documentElement();

    if (top.tagName() != QLatin1StringView("task")) {
        qCWarning(PIMKOLAB_LOG) << QStringLiteral("XML error: Top tag was %1 instead of the expected task").arg(top.tagName());
        return false;
    }
    setHasStartDate(false); // todo's don't necessarily have one

    for (QDomNode n = top.firstChild(); !n.isNull(); n = n.nextSibling()) {
        if (n.isComment()) {
            continue;
        }
        if (n.isElement()) {
            QDomElement e = n.toElement();
            if (!loadAttribute(e)) {
                // TODO: Unhandled tag - save for later storage
                qCDebug(PIMKOLAB_LOG) << "Warning: Unhandled tag" << e.tagName();
            }
        } else {
            qCDebug(PIMKOLAB_LOG) << "Node is not a comment or an element???";
        }
    }

    return true;
}

QString Task::saveXML() const
{
    QDomDocument document = domTree();
    QDomElement element = document.createElement(QStringLiteral("task"));
    element.setAttribute(QStringLiteral("version"), QStringLiteral("1.0"));
    saveAttributes(element);
    if (!hasStartDate() && startDate().isValid()) {
        // 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.
        QDomNodeList l = element.elementsByTagName(QStringLiteral("start-date"));
        Q_ASSERT(l.count() == 1);
        element.removeChild(l.item(0));
    }
    document.appendChild(element);
    return document.toString();
}

void Task::setFields(const KCalendarCore::Todo::Ptr &task)
{
    Incidence::setFields(task);

    setPercentCompleted(task->percentComplete());
    setStatus(task->status());
    setHasStartDate(task->hasStartDate());

    if (task->hasDueDate()) {
        if (task->allDay()) {
            // This is a floating task. Don't timezone move this one
            mFloatingStatus = AllDay;
            setDueDate(task->dtDue().date());
        } else {
            mFloatingStatus = HasTime;
            setDueDate(localToUTC(task->dtDue()));
        }
    } else {
        mHasDueDate = false;
    }

    if (!task->relatedTo().isEmpty()) {
        setParent(task->relatedTo());
    } else {
        setParent(QString());
    }

    if (task->hasCompletedDate() && task->percentComplete() == 100) {
        setCompletedDate(localToUTC(task->completed()));
    } else {
        mHasCompletedDate = false;
    }
}

void Task::saveTo(const KCalendarCore::Todo::Ptr &task)
{
    Incidence::saveTo(task);

    task->setPercentComplete(percentCompleted());
    task->setStatus(status());
    // PORT KF6 task->setHasStartDate( hasStartDate() );
    // PORT KF6 task->setHasDueDate( hasDueDate() );
    if (hasDueDate()) {
        task->setDtDue(utcToLocal(dueDate()));
    }

    if (!parent().isEmpty()) {
        task->setRelatedTo(parent());
    }

    if (hasCompletedDate() && task->percentComplete() == 100) {
        task->setCompleted(utcToLocal(mCompletedDate));
    }
}