]> git.sven.stormbind.net Git - sven/vym.git/blob - src/task.cpp
New upstream version 2.9.22
[sven/vym.git] / src / task.cpp
1 #include "task.h"
2
3 #include <QDebug>
4
5 #include "branchitem.h"
6 #include "taskmodel.h"
7 #include "vymmodel.h"
8
9 Task::Task(TaskModel *tm)
10 {
11     //    qDebug()<<"Constr. Task";
12     status = NotStarted;
13     awake = Task::WideAwake;
14     branch = NULL;
15     prio = 0;
16     prio_delta = 0;
17     model = tm;
18     date_creation = QDateTime::currentDateTime();
19 }
20
21 Task::~Task()
22 {
23     //    qDebug()<<"Destr. Task";
24     if (branch)
25         branch->setTask(NULL);
26 }
27
28 void Task::setModel(TaskModel *tm) { model = tm; }
29
30 void Task::cycleStatus(bool reverse)
31 {
32     if (awake == Morning)
33         setAwake(WideAwake);
34     else {
35         int i = status;
36         reverse ? i-- : i++;
37
38         if (i < 0)
39             i = 2;
40         if (i > 2)
41             i = 0;
42
43         setStatus((Task::Status)i);
44     }
45     if (branch)
46         branch->updateTaskFlag();
47 }
48
49 void Task::setStatus(const QString &s)
50 {
51     if (s == "NotStarted")
52         setStatus(NotStarted);
53     else if (s == "WIP")
54         setStatus(WIP);
55     else if (s == "Finished")
56         setStatus(Finished);
57     else
58         qWarning() << "Task::setStatus Unknown value: " << s;
59 }
60
61 void Task::setStatus(Status s)
62 {
63     if (s == status)
64         return;
65     status = s;
66     if (branch)
67         branch->updateTaskFlag();
68 }
69
70 Task::Status Task::getStatus() { return status; }
71
72 QString Task::getStatusString()
73 {
74     switch (status) {
75     case NotStarted:
76         return "NotStarted";
77     case WIP:
78         return "WIP";
79     case Finished:
80         return "Finished";
81     }
82     return "Undefined";
83 }
84
85 QString Task::getIconString()
86 {
87     QString s;
88     switch (status) {
89     case NotStarted:
90         s = "task-new";
91         break;
92     case WIP:
93         s = "task-wip";
94         break;
95     case Finished:
96         s = "task-finished";
97         break;
98     default:
99         s = "status:undefined";
100     }
101     if (status != Finished)
102         switch (awake) {
103         case Sleeping:
104             s += "-sleeping";
105             break;
106         case Morning:
107             s += "-morning";
108             break;
109         default:
110             break;
111         }
112     return s;
113 }
114
115 void Task::setAwake(const QString &s)
116 {
117     if (s == "Sleeping")
118         setAwake(Sleeping);
119     else if (s == "Morning")
120         setAwake(Morning);
121     else if (s == "WideAwake")
122         setAwake(WideAwake);
123     else
124         qWarning() << "Task::setAwake Unknown value: " << s;
125 }
126
127 void Task::setAwake(Task::Awake a)
128 {
129     if (awake != a) {
130         awake = a;
131         if (branch)
132             branch->updateTaskFlag();
133     }
134 }
135
136 Task::Awake Task::getAwake() { return awake; }
137
138 QString Task::getAwakeString()
139 {
140     switch (getAwake()) {
141     case Sleeping:
142         return "Sleeping";
143     case Morning:
144         return "Morning";
145     case WideAwake:
146         return "WideAwake";
147     }
148     return "Undefined";
149 }
150
151 bool Task::updateAwake()
152 {
153     qint64 secs = getSecsSleep();
154
155     if (secs < 0) {
156         if (awake == Task::Sleeping) {
157             setAwake(Task::Morning);
158             return true;
159         }
160     }
161     else if (secs > 0) {
162         if (awake != Task::Sleeping) {
163             setAwake(Task::Sleeping);
164             return true;
165         }
166     }
167     return false;
168 }
169
170 void Task::setPriority(int p) { prio = p; }
171
172 int Task::getPriority() { return prio; }
173
174 int Task::getAgeCreation()
175 {
176     return date_creation.daysTo(QDateTime::currentDateTime());
177 }
178
179 int Task::getAgeModification()
180 {
181     if (date_modification.isValid())
182         return date_modification.daysTo(QDateTime::currentDateTime());
183     else
184         return getAgeCreation();
185 }
186
187 void Task::setDateCreation(const QString &s)
188 {
189     date_creation = QDateTime().fromString(s, Qt::ISODate);
190 }
191
192 QDateTime Task::getDateCreation() { return date_creation; }
193
194 void Task::setDateModification()
195 {
196     date_modification = QDateTime::currentDateTime();
197 }
198
199 void Task::setDateModification(const QString &s)
200 {
201     date_modification = QDateTime().fromString(s, Qt::ISODate);
202 }
203
204 QDateTime Task::getDateModification() { return date_modification; }
205
206 bool Task::setDaysSleep(qint64 n)
207 {
208     return setDateSleep(QDate::currentDate().addDays(n).toString(Qt::ISODate));
209 }
210
211 bool Task::setHoursSleep(qint64 n)
212 {
213     return setDateSleep(
214         QDateTime::currentDateTime().addSecs(n * 3600).toString(Qt::ISODate));
215 }
216
217 bool Task::setSecsSleep(qint64 n)
218 {
219     if (n == 0)
220         setAwake(Morning);
221     return setDateSleep(
222         QDateTime::currentDateTime().addSecs(n).toString(Qt::ISODate));
223 }
224
225 bool Task::setDateSleep(const QString &s)
226 {
227     if (setDateSleep(QDateTime().fromString(s, Qt::ISODate)))
228         return true;
229     else if (setDateSleep(QDateTime().fromString(s, Qt::TextDate)))
230         return true;
231     else if (setDateSleep(
232                  QDateTime().fromString(s, Qt::DefaultLocaleShortDate)))
233         return true;
234     else if (setDateSleep(QDateTime().fromString(s, Qt::DefaultLocaleLongDate)))
235         return true;
236     else
237         return false;
238 }
239
240 bool Task::setDateSleep(const QDateTime &d)
241 {
242     if (!d.isValid())
243         return false;
244
245     date_sleep = d;
246     updateAwake();
247     return true;
248 }
249
250 qint64 Task::getDaysSleep()
251 {
252     qint64 d = 1;
253     if (date_sleep.isValid())
254         d = QDateTime::currentDateTime().daysTo(date_sleep);
255     else {
256         // qWarning() << "Task::getDaysSleep date_sleep is invalid for branch "
257         // << branch->getHeadingPlain();
258         return -1;
259     }
260     return d;
261 }
262
263 qint64 Task::getSecsSleep()
264 {
265     qint64 d = 0; // Meaning: No sleep time set so far
266     if (date_sleep.isValid())
267         d = QDateTime::currentDateTime().secsTo(date_sleep);
268     return d;
269 }
270
271 QDateTime Task::getSleep() { return date_sleep; }
272
273 void Task::setPriorityDelta(const int &n) { prio_delta = n; }
274
275 int Task::getPriorityDelta() { return prio_delta; }
276
277 void Task::setBranch(BranchItem *bi)
278 {
279     branch = bi;
280     mapName = bi->getModel()->getMapName();
281 }
282
283 BranchItem *Task::getBranch() { return branch; }
284
285 QString Task::getName()
286 {
287     if (branch)
288         return branch->getHeadingPlain();
289     else {
290         qWarning() << "Task::getName  no branch!";
291         return "UNDEFINED";
292     }
293 }
294
295 QString Task::getMapName() { return mapName; }
296
297 QString Task::saveToDir()
298 {
299     QString sleepAttr;
300     if (date_sleep.isValid())
301         sleepAttr = attribut("date_sleep", date_sleep.toString(Qt::ISODate));
302     else
303         sleepAttr = attribut("date_sleep", "2018-01-01T00:00:00");
304
305     // Experimental: Also output priority based on arrow flags for external
306     // sorting
307     QString prioAttr;
308     if (branch) {
309         if (branch->hasActiveFlag("2arrow-up"))
310             prioAttr = attribut("prio", "2");
311         if (branch->hasActiveFlag("arrow-up"))
312             prioAttr = attribut("prio", "1");
313     }
314
315     QString prioDeltaAttr;
316     if (prio_delta != 0)
317         prioDeltaAttr = attribut("prio_delta", QString("%1").arg(prio_delta));
318     return singleElement(
319         "task",
320         attribut("status", getStatusString()) +
321             attribut("awake", getAwakeString()) +
322             attribut("date_creation", date_creation.toString(Qt::ISODate)) +
323             attribut("date_modification",
324                      date_modification.toString(Qt::ISODate)) +
325             prioDeltaAttr + sleepAttr + prioAttr);
326 }