-
Notifications
You must be signed in to change notification settings - Fork 123
/
TaskList.cs
313 lines (271 loc) · 8.4 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using CommonExtensions;
namespace ToDoLib
{
/// <summary>
/// A thin data access abstraction over the actual todo.txt file
/// </summary>
public class TaskList
{
// It may look like an overly simple approach has been taken here, but it's well considered. This class
// represents *the file itself* - when you call a method it should be as though you directly edited the file.
// This reduces the likelihood of concurrent update conflicts by making each action as autonomous as possible.
// Although this does lead to some extra IO, it's a small price for maintaining the integrity of the file.
// NB, this is not the place for higher-level functions like searching, task manipulation etc. It's simply
// for CRUDing the todo.txt file.
#region Properties
string _filePath = null;
string _preferredLineEnding = null;
public List<Task> Tasks { get; private set; }
// Task List MetaData
public List<string> Projects { get; private set; }
public List<string> Contexts { get; private set; }
public List<string> Priorities { get; private set; }
public bool PreserveWhiteSpace { get; set; }
#endregion
#region Constructor
public TaskList(string filePath, bool preserveWhitespace = false)
{
_filePath = filePath;
_preferredLineEnding = Environment.NewLine;
PreserveWhiteSpace = preserveWhitespace;
ReloadTasks();
}
#endregion
#region Events
public event EventHandler Modified;
#endregion
#region Task List Metadata Methods
public void UpdateTaskListMetaData()
{
var UniqueProjects = new SortedSet<string>();
var UniqueContexts = new SortedSet<string>();
var UniquePriorities = new SortedSet<string>();
foreach (Task t in Tasks)
{
foreach (string p in t.Projects)
{
UniqueProjects.Add(p);
}
foreach (string c in t.Contexts)
{
UniqueContexts.Add(c);
}
UniquePriorities.Add(t.Priority);
}
this.Projects = UniqueProjects.ToList<string>();
this.Contexts = UniqueContexts.ToList<string>();
this.Priorities = UniquePriorities.ToList<string>();
}
#endregion
public void ReloadTasks()
{
Log.Debug("Loading tasks from {0}.", _filePath);
try
{
Tasks = new List<Task>();
var file = File.OpenRead(_filePath);
using (var reader = new StreamReader(file))
{
string raw;
while ((raw = reader.ReadLine()) != null)
{
if (!raw.IsNullOrEmpty() || PreserveWhiteSpace)
{
Tasks.Add(new Task(raw));
}
}
}
Log.Debug("Finished loading tasks from {0}.", _filePath);
_preferredLineEnding = GetPreferredFileLineEndingFromFile();
}
catch (IOException ex)
{
var msg = "There was a problem trying to read from your todo.txt file.";
Log.Error(msg, ex);
throw new TaskException(msg, ex);
}
catch (Exception ex)
{
Log.Error(ex.ToString());
throw;
}
finally
{
UpdateTaskListMetaData();
RaiseModifiedEvent();
}
}
public void Add(Task task)
{
try
{
var output = task.ToString();
Log.Debug("Adding task '{0}'", output);
var text = File.ReadAllText(_filePath);
if (text.Length > 0 && !text.EndsWith(_preferredLineEnding))
{
output = _preferredLineEnding + output;
}
File.AppendAllLines(_filePath, new string[] { output });
Tasks.Add(task);
Log.Debug("Task '{0}' added", output);
}
catch (IOException ex)
{
var msg = "An error occurred while trying to add your task to the task list file";
Log.Error(msg, ex);
throw new TaskException(msg, ex);
}
catch (Exception ex)
{
Log.Error(ex.ToString());
throw;
}
finally
{
UpdateTaskListMetaData();
RaiseModifiedEvent();
}
}
private void RaiseModifiedEvent()
{
if(Modified != null)
{
Modified(this, new EventArgs());
}
}
public void Delete(Task task)
{
try
{
Log.Debug("Deleting task '{0}'", task.ToString());
if (Tasks.Remove(Tasks.First(t => t.Raw == task.Raw)))
{
WriteAllTasksToFile();
}
Log.Debug("Task '{0}' deleted", task.ToString());
}
catch (IOException ex)
{
var msg = "An error occurred while trying to remove your task from the task list file";
Log.Error(msg, ex);
throw new TaskException(msg, ex);
}
catch (Exception ex)
{
Log.Error(ex.ToString());
throw;
}
finally
{
UpdateTaskListMetaData();
RaiseModifiedEvent();
}
}
/// <summary>
/// This method updates one task in the file. It works by replacing the "current task" with the "new task".
/// </summary>
/// <param name="currentTask">The task to replace.</param>
/// <param name="newTask">The replacement task.</param>
/// <param name="reloadTasksPriorToUpdate">Optionally reload task file prior to the update. Default is TRUE.</param>
/// <param name="writeTasks">Optionally write task file after the update. Default is TRUE.</param>
/// <param name="reloadTasksAfterUpdate">Optionally reload task file after the update. Default is TRUE.</param>
public void Update(Task currentTask, Task newTask, bool writeTasks = true)
{
Log.Debug("Updating task '{0}' to '{1}'", currentTask.ToString(), newTask.ToString());
try
{
// ensure that the task list still contains the current task...
if (!Tasks.Any(t => t.Raw == currentTask.Raw))
{
throw new Exception("That task no longer exists in to todo.txt file.");
}
var currentIndex = Tasks.IndexOf(Tasks.First(t => t.Raw == currentTask.Raw));
Tasks[currentIndex] = newTask;
Log.Debug("Task '{0}' updated", currentTask.ToString());
if (writeTasks)
{
WriteAllTasksToFile();
}
}
catch (IOException ex)
{
var msg = "An error occurred while trying to update your task in the task list file.";
Log.Error(msg, ex);
throw new TaskException(msg, ex);
}
catch (Exception ex)
{
Log.Error(ex.ToString());
throw;
}
finally
{
UpdateTaskListMetaData();
RaiseModifiedEvent();
}
}
protected string GetPreferredFileLineEndingFromFile()
{
try
{
using (StreamReader fileStream = new StreamReader(_filePath))
{
char previousChar = '\0';
// Read the first 4000 characters to try and find a newline
for (int i = 0; i < 4000; i++)
{
int b = fileStream.Read();
if (b == -1) break;
char currentChar = (char)b;
if (currentChar == '\n')
{
return (previousChar == '\r') ? "\r\n" : "\n";
}
previousChar = currentChar;
}
// if no newline found, use the default newline character for the environment
return Environment.NewLine;
}
}
catch (IOException ex)
{
var msg = "An error occurred while trying to read the task list file";
Log.Error(msg, ex);
throw new TaskException(msg, ex);
}
catch (Exception ex)
{
Log.Error(ex.ToString());
throw;
}
}
protected void WriteAllTasksToFile()
{
try
{
using (StreamWriter writer = new StreamWriter(_filePath))
{
writer.NewLine = _preferredLineEnding;
Tasks.ForEach((Task t) => { writer.WriteLine(t.ToString()); });
writer.Close();
}
}
catch (IOException ex)
{
var msg = "An error occurred while trying to write to the task list file.";
Log.Error(msg, ex);
throw new TaskException(msg, ex);
}
catch (Exception ex)
{
Log.Error(ex.ToString());
throw;
}
}
}
}