-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtask.cc
328 lines (276 loc) · 7.8 KB
/
task.cc
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
326
327
328
#include "task.h"
#include <assert.h>
#include <algorithm>
#include <string>
#include "file-versions.h"
#include "utils.h"
using std::string;
Task::Task(const string& title, const string& description)
: parent_(NULL),
status_(CREATED),
title_(title),
description_(description) {
creation_date_.SetToNow();
start_date_.SetToEmptyTime();
completion_date_.SetToEmptyTime();
}
Task::~Task() {
for (int i = 0; i < subtasks_.size(); ++i) {
delete subtasks_[i];
}
}
Task* Task::NewTaskFromSerializer(Serializer* s) {
string title = s->ReadString();
string description = s->ReadString();
Task* t = new Task(title, description);
t->UnSerializeFromSerializer(s);
return t;
}
void Task::AddNote(const string& note) { notes_.push_back(new Note(note)); }
bool Task::HasNotes() { return !notes_.empty(); }
void Task::DeleteNote(const string& note) {
std::vector<Note*>::iterator delete_it;
bool found = false;
for (vector<Note*>::iterator it = notes_.begin(); it != notes_.end(); it++) {
if ((*it)->GetText().compare(note) == 0) {
found = true;
delete_it = it;
}
}
if (found) {
notes_.erase(delete_it);
}
}
vector<string> Task::Notes() {
vector<string> notes;
for (int i = 0; i < notes_.size(); ++i) {
notes.push_back(notes_[i]->GetText());
}
return notes;
}
map<string, string> Task::MappedNotes() {
map<string, string> mappedNotes;
for (int i = 0; i < notes_.size(); ++i) {
mappedNotes[notes_[i]->Text()] = notes_[i]->GetText();
}
return mappedNotes;
}
void Task::ApplyFilter(FilterPredicate<Task>* filter) {
// It's important that we filter ourselves after our children because often
// filters have an OrPredicate of "Has any filtered children" which wouldn't
// if we filtered ourselves before our children.
for (int i = 0; i < NumChildren(); ++i) {
Child(i)->ApplyFilter(filter);
}
filtered_tasks_ = filter->FilterVector(subtasks_);
}
void Task::AddSubTask(Task* subtask) {
subtask->SetParent(this);
subtasks_.push_back(subtask);
}
void Task::RemoveSubtaskFromList(Task* t) {
for (int i = 0; i < subtasks_.size(); ++i) {
if (subtasks_[i] == t) {
subtasks_.erase(subtasks_.begin() + i);
return;
}
}
}
void Task::DeleteTask(Task* t) {
// Check if we're supposed to delete ourselves:
if (this == t) {
Delete();
} else {
for (int i = 0; i < NumChildren(); ++i) {
if (Child(i) == t) {
Child(i)->Delete();
return;
} else {
Child(i)->DeleteTask(t);
}
}
}
}
void Task::Delete() {
for (int i = 0; i < NumChildren(); ++i) {
Child(i)->Delete();
}
if (Parent() != NULL) {
Parent()->RemoveSubtaskFromList(this);
}
delete this;
}
void Task::SwapTasks(Task* a, Task* b) {
if (a == b) {
return;
}
// Find the indices of a and b.
vector<Task*>::iterator ait = find(subtasks_.begin(), subtasks_.end(), a);
vector<Task*>::iterator bit = find(subtasks_.begin(), subtasks_.end(), b);
assert(ait != subtasks_.end() || bit != subtasks_.end());
int ai = ait - subtasks_.begin();
int bi = bit - subtasks_.begin();
Task* tmp = a;
subtasks_[ai] = b;
subtasks_[bi] = tmp;
return;
}
void Task::MoveTaskUp(Task* t) {
// We can't move up the first task.
if (t == filtered_tasks_[0]) {
return;
}
// Find the task in our filtered list:
vector<Task*>::iterator it =
find(filtered_tasks_.begin(), filtered_tasks_.end(), t);
--it;
SwapTasks(*it, t);
}
void Task::MoveTaskDown(Task* t) {
// We can't move down the last task.
if (t == filtered_tasks_[filtered_tasks_.size() - 1]) {
return;
}
// Find the task in our filtered list:
vector<Task*>::iterator it =
find(filtered_tasks_.begin(), filtered_tasks_.end(), t);
++it;
SwapTasks(t, *it);
}
void Task::MoveUp() {
if (Parent()) {
Parent()->MoveTaskUp(this);
}
}
void Task::MoveDown() {
if (Parent()) {
Parent()->MoveTaskDown(this);
}
}
void Task::Serialize(Serializer* s) {
// Initially we store a unique identifier to ourselves that will help with
// reading in the tasks and assembling the tree.
s->WriteUint64((uint64)this);
// Data about this task.
s->WriteString(title_);
s->WriteString(description_);
s->WriteInt32(static_cast<int32>(status_));
// Various dates.
creation_date_.Serialize(s);
start_date_.Serialize(s);
completion_date_.Serialize(s);
// The notes associated with this task.
if (s->Version() >= NOTES_VERSION) {
s->WriteInt32(notes_.size());
for (int i = 0; i < notes_.size(); ++i) {
notes_[i]->Serialize(s);
}
}
// Task status changes.
if (s->Version() >= TASK_STATUS_VERSION) {
s->WriteInt32(status_changes_.size());
for (int i = 0; i < status_changes_.size(); ++i) {
status_changes_[i].date.Serialize(s);
s->WriteInt32(status_changes_[i].status);
}
}
// Finally our parent pointer and then we move onto the children.
s->WriteUint64((uint64)parent_);
for (int i = 0; i < subtasks_.size(); ++i) {
subtasks_[i]->Serialize(s);
}
}
void Task::UnSerializeFromSerializer(Serializer* s) {
status_ = static_cast<TaskStatus>(s->ReadInt32());
creation_date_.ReadFromSerializer(s);
start_date_.ReadFromSerializer(s);
completion_date_.ReadFromSerializer(s);
if (s->Version() >= NOTES_VERSION) {
int num_notes = s->ReadInt32();
for (int i = 0; i < num_notes; ++i) {
Note* n = new Note("");
n->ReadFromSerializer(s);
notes_.push_back(n);
}
}
if (s->Version() >= TASK_STATUS_VERSION) {
int num_status_changes = s->ReadInt32();
for (int i = 0; i < num_status_changes; ++i) {
Date d;
d.ReadFromSerializer(s);
int status = s->ReadInt32();
status_changes_.push_back(StatusChange(d, status));
}
}
}
void Task::SetStatus(TaskStatus t) {
if (status_ == CREATED && t == IN_PROGRESS) {
// We were set to in progress for the first time.
start_date_.SetToNow();
} else if (t == COMPLETED && status_ != COMPLETED) {
completion_date_.SetToNow();
} else if (t == PAUSED && status_ != PAUSED) {
completion_date_.SetToEmptyTime();
}
if (t == IN_PROGRESS) {
completion_date_.SetToEmptyTime();
}
status_ = t;
// Update the status record for this task.
status_changes_.push_back(StatusChange(Date(), status_));
}
int Task::NumOffspring() {
int sum_from_children = 0;
for (int i = 0; i < subtasks_.size(); ++i) {
sum_from_children += 1 + subtasks_[i]->NumOffspring();
}
return sum_from_children;
}
int Task::NumFilteredOffspring() {
int sum_from_children = 0;
for (int i = 0; i < filtered_tasks_.size(); ++i) {
sum_from_children += 1 + filtered_tasks_[i]->NumFilteredOffspring();
}
return sum_from_children;
}
int Task::ListColor() {
int c = 0;
if (status_ == CREATED) {
c |= COLOR_PAIR(0);
} else if (status_ == IN_PROGRESS) {
c |= COLOR_PAIR(2); // green
} else if (status_ == COMPLETED) {
c |= COLOR_PAIR(4); // blue
} else if (status_ == PAUSED) {
c |= COLOR_PAIR(1); // red
}
return c;
}
int Task::NumFilteredChildren() { return filtered_tasks_.size(); }
Task* Task::FilteredChild(int c) { return filtered_tasks_[c]; }
void Task::ToStream(ostream& out, int depth) {
const string marker = "- ";
for (int i = 0; i < depth; ++i) {
out << " ";
}
vector<string> words;
StrUtils::SplitStringUsing(" ", title_, &words);
out << marker;
int line_length = depth + marker.length();
for (int i = 0; i < words.size(); ++i) {
if (line_length + words[i].length() > 80) {
out << std::endl;
for (int s = 0; s < depth + marker.length(); ++s) {
out << " ";
}
line_length = depth + marker.length();
}
out << words[i];
out << " ";
line_length += words[i].length() + 1;
}
out << std::endl;
for (int i = 0; i < NumFilteredChildren(); ++i) {
FilteredChild(i)->ToStream(out, depth + 2);
}
}