-
Notifications
You must be signed in to change notification settings - Fork 1
/
TaskList.cs
503 lines (435 loc) · 12.3 KB
/
TaskList.cs
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Authors:
// Jan Rüegg <[email protected]>
// Gabriel Walch <[email protected]>
// Gerd Zellweger <[email protected]>
//
using System;
using System.Collections.Generic;
using Gtk;
using Tomboy;
namespace Tomboy.TaskManager
{
/// <summary>
/// A task list is a collection of tasks grouped together.
/// It may have a title and a due date.
/// </summary>
public class TaskList : AttributedTask
{
/// <summary>
/// Name of this task list
/// </summary>
public string Name { get; set; }
/// <summary>
/// Describes what to do when tasklist is marked as done
/// </summary>
public override bool Done {
get {
if (Tasks != null) {
return Tasks.FindAll (c => c.Done == true).Count == Tasks.Count;
} else
return true;
}
set { Tasks.ForEach (c => c.Done = value); }
}
/// <summary>
/// Tasks for ITask interface
/// </summary>
public List<Task> Tasks { get; set; }
/// <summary>
/// Returns the last line where a Task is on
/// </summary>
public int LastTaskLine {
get {
int line = -1;
foreach (Task task in Tasks) {
if (task.Line > line)
line = task.Line;
}
return line;
}
}
/// <summary>
/// Shortcut for attached tag
/// </summary>
private TaskListTag TaskListTag {
get { return (TaskListTag)Tag; }
set { Tag = value; }
}
/// <summary>
/// Beginning of the description, for TaskLists the same as Start
/// </summary>
protected override TextIter DescriptionStart {
get { return Start; }
}
/// <summary>
/// End of the description, basically end of first line
/// </summary>
protected override TextIter DescriptionEnd {
get {
var start = Start;
if (!start.EndsLine())
start.ForwardToLineEnd ();
return start;
}
}
/// <summary>
/// End of the Tasklist, including all tasks
/// </summary>
protected override TextIter End {
get {
Gtk.TextIter iter = Buffer.GetIterAtLine (LastTaskLine);
iter.ForwardToLineEnd ();
return iter;
}
}
/// <summary>
/// The Buffer containing this TaskList
/// </summary>
protected override NoteBuffer Buffer {
get { return ContainingNote.Buffer; }
}
/// <summary>
/// Note containing the TaskList.
/// </summary>
internal Note ContainingNote { get; set; }
//Dropped for now
// /// <summary>
// /// LinkingTasks for ITask interface
// /// </summary>
// public List<Task> LinkinTasks {
// get; set;
// }
/// <summary>
/// Method that looks for all linking tasks
/// Use this method sparingly, as it loads all un-loaded notes and does an exhaustive search,
/// including the forward-parsing in the tasks themselfs
/// </summary>
public List<Task> SuperTasks {
get {
List<Task> result = new List<Task> ();
foreach (Note n in Tomboy.DefaultNoteManager.Notes) {
if (n == ContainingNote)
continue;
if (!n.IsLoaded)
Note.Load (n.FilePath, Tomboy.DefaultNoteManager);
foreach (TaskList tl in new TaskListParser (n).Parse ())
foreach (Task t in tl.Tasks)
if (t.Subtasks.Contains (this))
result.Add (t);
}
return result;
}
}
/// <summary>
/// Creates a new tasklists including all the given tasks.
/// </summary>
/// <param name="tasks">
/// A <see cref="List<Task>"/>
/// </param>
public TaskList (Note note, List<Task> tasks, String name, Gtk.TextIter start)
{
ContainingNote = note;
//TODO possible merge this with things below?
Name = name;
TaskListTag tag = (TaskListTag)ContainingNote.TagTable.CreateDynamicTag ("tasklist");
NoteBuffer buffer = note.Buffer;
Initialize (start, tag);
var end = Start;
if (tasks.Count == 0)
name = name + "\n";
buffer.Insert (ref end, name);
start = Start;
if (!end.EndsLine ())
end.ForwardToLineEnd ();
end.ForwardChar ();
Buffer.ApplyTag (TaskListTag, start, end);
Tasks = new List<Task> ();
if (tasks.Count > 0)
foreach (Task task in tasks) {
Tasks.Add (task);
task.RemoveTag (task.ContainingTaskList.Tag);
task.ContainingTaskList = this;
// This is required for intendation
this.Tag.Priority = 0;
task.ApplyTag (this.Tag);
}
else {
end.BackwardChar ();
AddTask (end);
}
}
/// <summary>
/// Creates a new task list with existing tags.
/// </summary>
public TaskList (Note note, Gtk.TextIter start, TaskListTag tag)
{
ContainingNote = note;
Name = ("New TaskList!");
//FIXME
Initialize (start, tag);
Logger.Debug ("TaskList created");
Tasks = new List<Task> ();
}
/// <summary>
/// Sets up the TaskList at cursor position.
/// </summary>
/// <param name="note">
/// <see cref="Note"/> where the TaskLists is located.
/// </param>
public TaskList (Note note)
{
//TODO: rewrite. looks a bit ugly everything...
ContainingNote = note;
Name = ("New TaskList!");
TaskListTag tag = (TaskListTag)ContainingNote.TagTable.CreateDynamicTag ("tasklist");
TextIter iter;
NoteBuffer buffer = note.Buffer;
int line = buffer.GetIterAtMark (buffer.InsertMark).Line;
var linestart = buffer.GetIterAtLine (line);
var lineend = linestart;
lineend.ForwardToLineEnd ();
if (buffer.GetText (linestart, lineend, false).Trim ().Length == 0)
iter = linestart;
else {
buffer.Insert (ref lineend, System.Environment.NewLine);
iter = lineend;
}
Initialize (iter, tag);
var end = Start;
buffer.Insert (ref end, "New Tasklist!\n\n");
var start = Start;
Buffer.ApplyTag (TaskListTag, start, end);
Logger.Debug ("TaskList created");
//Logger.Debug (iter.Char.ToString());
Tasks = new List<Task> ();
end.BackwardChar ();
AddTask (end);
LockEnd ();
}
/// <summary>
/// Remove deleted Tasks.
/// </summary>
/// <returns>
/// Returns the invalid tasks as <see cref="List<Task>"/>
/// </returns>
public List<Task> RemoveDeletedTasks ()
{
List<Task> remove_list = new List<Task> ();
List<Task> invalid_list = new List<Task> ();
foreach (Task task in Tasks) {
if (task.WasDeleted) {
Logger.Debug ("Found Deleted Task");
remove_list.Add (task);
} else {
if (task.IsValid) {
Logger.Debug (task.Description () + " is valid!");
} else {
Logger.Debug ("Found invalid Task");
invalid_list.Add (task);
}
}
}
foreach (Task task in remove_list) {
task.Delete ();
}
return invalid_list;
}
/// <summary>
/// Locks the end of a tasklist.
/// </summary>
/// <returns>
/// A <see cref="System.Boolean"/>, true if a newline was inserted
/// </returns>
public bool LockEnd ()
{
bool result = false;
Logger.Debug ("locking end");
var end = End;
if (Buffer.GetDynamicTag ("tasklist", end) != Tag) {
result = true;
Logger.Debug ("inserting \\n");
Buffer.Insert (ref end, System.Environment.NewLine);
if (Buffer.GetIterAtMark (Buffer.InsertMark).Equal (end)) {
end.BackwardChar ();
Buffer.PlaceCursor (end);
}
}
end = End;
var start = end;
end.ForwardChar ();
Buffer.ApplyTag ("locked", start, end);
return result;
}
/// <summary>
/// Returns true if the start of the TaskList was deleted.
/// </summary>
public bool StartDeleted ()
{
TaskListTag tag = (TaskListTag)Buffer.GetDynamicTag ("tasklist", Start);
if (tag != this.Tag)
return true;
return false;
}
/// <summary>
/// Returns true if the TaskList was deleted.
/// </summary>
public bool WasDeleted ()
{
TaskListTag tag = (TaskListTag)Buffer.GetDynamicTag ("tasklist", Start);
if (tag != this.Tag)
return true;
//FIXME what if only start deleted?
return false;
}
/// <summary>
/// Fix the TaskList, for example after heavy deletion operations.
/// </summary>
/// <param name="line">
/// A <see cref="System.Int32"/>, the line on which fixing is needed.
/// </param>
/// <returns>
/// Returns the new <see cref="TaskList"/> if splitting was required.
/// </returns>
public TaskList FixWithin (int line)
{
Logger.Debug ("FixWithin");
var invalid_list = RemoveDeletedTasks ();
if (invalid_list.Count >= 1) {
foreach (Task inv_task in invalid_list) {
if (inv_task.Line != line) {
Logger.Fatal ("Got wrong line!");
return null;
}
}
}
TaskTag tasktag = (TaskTag)Buffer.GetDynamicTag ("task", Buffer.GetIterAtLine (line));
// Not in title
if (tasktag != null) {
Task task = tasktag.Task;
return task.Fix ();
} else {
// In title
Logger.Debug ("Fixing title");
FixTitle ();
return null;
}
}
/// <summary>
/// Fix the title after merging or deleting operations.
/// </summary>
public void FixTitle ()
{
var iter = Start;
if (iter.LineOffset != 0) {
iter.LineOffset = 0;
this.Position = Buffer.CreateMark (null, iter, true);
}
utils.RemoveTaskTags (DescriptionStart, DescriptionEnd);
Buffer.ApplyTag (Tag, DescriptionStart, End);
}
/// <summary>
/// Delete all the metadata of the Tasklist.
/// </summary>
public void Delete ()
{
ContainingNote.TagTable.Remove (this.TaskListTag);
foreach (Task task in Tasks)
task.DeleteTag ();
}
/// <summary>
/// Print the structure of the TaskList to the console, only if debugging!
/// </summary>
public void DebugPrint ()
{
if (!Tomboy.Debugging)
return;
Console.WriteLine ("Tasklist '" + Description () + "':");
foreach (Task task in Tasks)
task.DebugPrint ();
Console.WriteLine ();
}
/// <summary>
/// Transfer all the tasks to another tasklist, used for merging
/// </summary>
/// <param name="tasklist">
/// The other <see cref="TaskList"/> tasklist to send the tasks to
/// </param>
public void TransferTasksTo (TaskList tasklist)
{
List<Task> to_transfer = new List<Task> ();
foreach (Task task in Tasks) {
if (!task.WasDeleted) {
Logger.Debug ("adding task " + task.Description ());
to_transfer.Add (task);
}
}
foreach (Task task in to_transfer) {
tasklist.AddFinishedTask (task);
task.RemoveTag (Tag);
Tasks.Remove (task);
}
Delete ();
tasklist.LockEnd ();
}
/// <summary>
/// Places the cursor at the end of the TaskList.
/// </summary>
public void PlaceCursorAtEnd ()
{
Buffer.PlaceCursor (End);
}
/// <summary>
/// Creates a new Task and add it to the `Tasks` list.
/// </summary>
/// <param name="at">
/// <see cref="Gtk.TextMark"/> Where to add the task in the Buffer.
/// </param>
public void AddTask (Gtk.TextIter position)
{
Tasks.Add (new Task (this, position));
}
/// <summary>
/// Add an existing task to this list.
/// </summary>
/// <param name="task">
/// A <see cref="Task"/> to add.
/// </param>
public void AddFinishedTask (Task task)
{
Tasks.Add (task);
task.ApplyTag (Tag);
}
/// <summary>
/// Add a new task to this tasklist
/// </summary>
/// <param name="position">
/// A <see cref="Gtk.TextIter"/>, where to insert the task
/// </param>
/// <param name="tag">
/// A <see cref="TaskTag"/>, what tag to give the new task
/// </param>
public void AddTask (Gtk.TextIter position, TaskTag tag)
{
Tasks.Add (new Task (this, position, tag));
}
}
}