-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFixAction.cs
227 lines (191 loc) · 5.68 KB
/
FixAction.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
// 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]>
//
namespace Tomboy.TaskManager
{
/// <summary>
/// This class can be used to create Actions that should be carried out on the Buffer, but have
/// to be deferred because it would render still used Gtk.TextIters useless, crashing Tomboy...
/// </summary>
public abstract class FixAction
{
protected TaskManagerNoteAddin addin;
/// <summary>
/// If set to true, this will be handled before other FixActions
/// </summary>
public bool Priority;
public FixAction (TaskManagerNoteAddin addin)
{
this.addin = addin;
}
/// <summary>
/// The actual fix to perform
/// </summary>
public abstract void fix ();
}
/// <summary>
/// Does a simple undo.
/// </summary>
public class FixUndoAction : FixAction
{
public FixUndoAction (TaskManagerNoteAddin addin) : base(addin)
{
Priority = false;
}
public override void fix ()
{
addin.Buffer.Undoer.Undo ();
}
}
/// <summary>
/// Action that Deletes (cleanly) a Task
/// </summary>
public class FixDeleteAction : FixAction
{
TaskList tasklist1;
TaskList tasklist2;
int line;
public FixDeleteAction (TaskManagerNoteAddin addin, TaskList tasklist1, TaskList tasklist2, int line) : base(addin)
{
Priority = false;
this.tasklist1 = tasklist1;
this.tasklist2 = tasklist2;
this.line = line;
}
public override void fix ()
{
if (tasklist2 == null && tasklist1 == null) {
Logger.Debug ("Checking for Deleted Tasks");
addin.ValidateTaskLists ();
} else if (tasklist1 != null && tasklist2 != null) {
if (tasklist1 == tasklist2) {
Logger.Debug ("Have to repair within TaskList");
TaskList new_list = tasklist1.FixWithin (line);
if (new_list != null)
addin.TaskLists.Add (new_list);
} else {
Logger.Debug ("Oh No, have to merge two TaskLists!");
tasklist2.FixWithin (line);
tasklist1.TransferTasksTo (tasklist2);
addin.TaskLists.Remove (tasklist1);
}
} else if (tasklist1 != null) {
Logger.Debug ("Fixing Start");
tasklist1.FixTitle ();
tasklist1.RemoveDeletedTasks ();
} else {
Logger.Debug ("Fixing End");
tasklist2.FixWithin (line);
tasklist2.LockEnd ();
}
addin.Utils.ResetCursor ();
addin.Buffer.Undoer.ClearUndoHistory ();
}
}
/// <summary>
/// Does a simple undo.
/// </summary>
public class FixDeleteEmptyCheckBoxAction : FixAction
{
Task task;
public FixDeleteEmptyCheckBoxAction (TaskManagerNoteAddin addin, Task task) : base(addin)
{
this.task = task;
Priority = false;
}
public override void fix ()
{
var iter = addin.Buffer.GetIterAtLine(task.Start.Line+1);
var start = iter;
var end = iter;
if (!end.EndsLine())
end.ForwardToLineEnd();
string text = addin.Buffer.GetText(start, end, false);
TaskList list;
if (text.Trim().Length == 0)
list = task.DeleteEmptyCheckBox (null);
else
list = task.DeleteEmptyCheckBox (string.Empty);
if (list != null)
addin.TaskLists.Add (list);
addin.Utils.ResetCursor ();
addin.Buffer.Undoer.ClearUndoHistory ();
}
}
/// <summary>
/// Action that Fixes the title of a TaskList
/// </summary>
public class FixTitleAction : FixAction
{
TaskList tasklist;
public FixTitleAction (TaskManagerNoteAddin addin, TaskList tasklist) : base(addin)
{
Priority = false;
this.tasklist = tasklist;
}
public override void fix ()
{
tasklist.FixTitle();
}
}
/// <summary>
/// Action that adds a Task
/// </summary>
public class NewTaskAction : FixAction
{
TaskList tasklist;
public NewTaskAction (TaskManagerNoteAddin addin, TaskList tasklist) : base(addin)
{
Priority = false;
this.tasklist = tasklist;
}
public NewTaskAction (TaskManagerNoteAddin addin) : base(addin)
{
Priority = false;
}
public override void fix ()
{
Logger.Debug ("Adding a new Task");
if (tasklist == null) {
Gtk.TextIter start = addin.Buffer.GetIterAtMark (addin.Buffer.InsertMark);
Gtk.TextIter end = start;
start.BackwardLine ();
addin.Buffer.Delete (ref start, ref end);
addin.TaskLists.Add (new TaskList (addin.Note));
} else {
var iter = addin.Buffer.GetIterAtMark (addin.Buffer.InsertMark);
var end = iter;
end.ForwardToLineEnd ();
TaskTag tt = addin.Utils.GetTaskTag (iter);
if (tt != null) {
//Remove Old TaskTag
addin.Buffer.RemoveTag (tt, iter, end);
}
tasklist.AddTask (iter);
}
addin.Utils.ResetCursor();
addin.Buffer.Undoer.ClearUndoHistory();
}
}
}