~ted/ubuntu/lucid/tomboy/with-patch

« back to all changes in this revision

Viewing changes to Tomboy/Addins/Tasks/TaskManager.cs

  • Committer: Bazaar Package Importer
  • Author(s): Pedro Fragoso
  • Date: 2008-01-15 11:32:52 UTC
  • mfrom: (1.1.31 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20080115113252-p59wg7nqkl6vcg7a
Tags: 0.9.4-0ubuntu1
* New upstream release (LP: #181798)
  - Fix crash during note deletion 
  - Fix null reference exception 
  - Fix mnemonics in sync preferences dialog 
  - Fix fuse mount timeout for sync 
  - New port option for SSH sync 
  - New multi-select notes support in Search All Notes window
  - New config dialog for Insert Timestamp Add-in 
  - New gconf preference, middle-click paste on Tomboy icon
  - New gconf preference, disable ESC closing notes 
  - New paragraph within a bullet with SHIFT + ENTER
  - New bug numbers as links in Export to HTML 
  - New notebook notes can be created off notebook's context menu.
  - New sketching add-in (still incomplete, --enable-sketching)
  - New "Unfiled Notes" item to notebook list 
  - New drag note to "Unfiled Notes" to remove from notebook 
 * debian/tomboy.menu: updated

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
using System;
3
 
using System.Collections.Generic;
4
 
using System.IO;
5
 
using Mono.Unix;
6
 
using Tomboy;
7
 
 
8
 
namespace Tomboy.Tasks
9
 
{
10
 
        public delegate void TasksChangedHandler (TaskManager manager, Task task);
11
 
 
12
 
        public class TaskManager 
13
 
        {
14
 
        #region Private Members
15
 
                string tasks_dir;
16
 
                string archive_dir;
17
 
                
18
 
                Gtk.ListStore tasks;
19
 
                Dictionary<string, Gtk.TreeIter> task_iters;
20
 
        #endregion // Private Members
21
 
 
22
 
        #region Constructors
23
 
                public TaskManager (string directory) : 
24
 
                        this (directory, Path.Combine (directory, "Archive")) 
25
 
                {
26
 
                }
27
 
 
28
 
                public TaskManager (string directory, string archive_directory) 
29
 
                {
30
 
                        Logger.Log ("TaskManager created with path \"{0}\".", directory);
31
 
 
32
 
                        tasks_dir = directory;
33
 
                        archive_dir = archive_directory;
34
 
                        
35
 
                        tasks = new Gtk.ListStore (typeof (Task));
36
 
                        task_iters = new Dictionary<string,Gtk.TreeIter> ();
37
 
 
38
 
                        bool first_run = FirstRun ();
39
 
                        CreateTasksDir ();
40
 
 
41
 
                        if (first_run) {
42
 
                                // First run. Create "Learn About Tomboy" task.
43
 
                                CreateStartTasks ();
44
 
                        } else {
45
 
                                LoadTasks ();
46
 
                        }
47
 
                }
48
 
        #endregion // Constructors
49
 
 
50
 
        #region Public Properties
51
 
                public Gtk.ListStore Tasks
52
 
                {
53
 
                        get { return tasks; }
54
 
                }
55
 
        #endregion // Public Properties
56
 
 
57
 
        #region Public Methods
58
 
                /// <summary>
59
 
                /// Delete the specified task from the system.
60
 
                /// </summary>
61
 
                public void Delete (Task task) 
62
 
                {
63
 
                        if (File.Exists (task.FilePath)) {
64
 
                                if (archive_dir != null) {
65
 
                                        // FIXME: Instead of moving deleted tasks into the archive dir, move them into a backup dir 
66
 
                                        if (!Directory.Exists (archive_dir))
67
 
                                                Directory.CreateDirectory (archive_dir);
68
 
 
69
 
                                        string archive_path = 
70
 
                                                Path.Combine (archive_dir, 
71
 
                                                              Path.GetFileName (task.FilePath));
72
 
                                        if (File.Exists (archive_path))
73
 
                                                File.Delete (archive_path);
74
 
 
75
 
                                        File.Move (task.FilePath, archive_path);
76
 
                                } else 
77
 
                                        File.Delete (task.FilePath);
78
 
                        }
79
 
 
80
 
                        string uri = task.Uri;
81
 
                        if (task_iters.ContainsKey (uri)) {
82
 
                                Gtk.TreeIter iter = task_iters [uri];
83
 
                                tasks.Remove (ref iter);
84
 
                                task_iters.Remove (uri);
85
 
                                task.Delete ();
86
 
                        }
87
 
 
88
 
                        Logger.Log ("Deleting task '{0}'.", task.Summary);
89
 
 
90
 
                        if (TaskDeleted != null)
91
 
                                TaskDeleted (this, task);
92
 
                }
93
 
 
94
 
                /// <summary>
95
 
                /// Create a new Task with the specified summary.
96
 
                /// <param name="summary">The summary of the new task.  This may be an
97
 
                /// empty string but should not be null.</param>
98
 
                /// </summary>
99
 
                public Task Create (string summary) 
100
 
                {
101
 
                        if (summary == null)
102
 
                                throw new ArgumentNullException ("summary", "You cannot create the a new task with a null summary.  Use String.Empty instead.");
103
 
 
104
 
//                      if (summary.Length > 0 && Find (summary) != null)
105
 
//                              throw new Exception ("A task with this summary already exists");
106
 
 
107
 
                        string filename = MakeNewFileName ();
108
 
                        
109
 
                        Task new_task = Task.CreateNewTask (summary, filename, this);
110
 
                        new_task.Renamed += OnTaskRenamed;
111
 
                        new_task.Saved += OnTaskSaved;
112
 
                        new_task.StatusChanged += OnTaskStatusChanged;
113
 
 
114
 
                        Gtk.TreeIter iter = tasks.Append ();
115
 
                        tasks.SetValue (iter, 0, new_task);
116
 
                        task_iters [new_task.Uri] = iter;
117
 
                        
118
 
                        if (TaskAdded != null)
119
 
                                TaskAdded (this, new_task);
120
 
                        
121
 
                        new_task.QueueSave (true);
122
 
 
123
 
                        return new_task;
124
 
                }
125
 
 
126
 
                /// <summary>
127
 
                /// Find an existing task with the specified summary.
128
 
                /// <param name="summary">The summary string to search for.</param>
129
 
                /// </summary>
130
 
                public Task Find (string summary) 
131
 
                {
132
 
                        Gtk.TreeIter iter;
133
 
                        if (tasks.GetIterFirst (out iter)) {
134
 
                                do {
135
 
                                        Task task = tasks.GetValue (iter, 0) as Task;
136
 
                                        if (task.Summary.ToLower () == summary.ToLower ())
137
 
                                                return task;
138
 
                                } while (tasks.IterNext (ref iter));
139
 
                        }
140
 
                        
141
 
                        return null;
142
 
                }
143
 
 
144
 
                public Task FindByUri (string uri)
145
 
                {
146
 
                        if (task_iters.ContainsKey (uri)) {
147
 
                                Gtk.TreeIter iter = task_iters [uri];
148
 
                                Task task = tasks.GetValue (iter, 0) as Task;
149
 
                                return task;
150
 
                        }
151
 
                        
152
 
                        return null;
153
 
                }
154
 
                
155
 
                /// <summary>
156
 
                /// Save any task that hasn't been saved already.
157
 
                /// This should only be called by TasksApplicationAddin.Shutdown ().
158
 
                /// </summary>
159
 
                public void Shutdown ()
160
 
                {
161
 
                        Logger.Log ("Saving unsaved tasks...");
162
 
                        
163
 
                        Gtk.TreeIter iter;
164
 
                        if (tasks.GetIterFirst (out iter)) {
165
 
                                do {
166
 
                                        Task task = tasks.GetValue (iter, 0) as Task;
167
 
                                        task.Save ();
168
 
                                } while (tasks.IterNext (ref iter));
169
 
                        }
170
 
                }
171
 
                
172
 
                /// <summary>
173
 
                /// Return a list of tasks whose origin note is the note specified.
174
 
                /// </summary>
175
 
                public List<Task> GetTasksForNote (Note note)
176
 
                {
177
 
                        List<Task> list = new List<Task> ();
178
 
                        
179
 
                        Gtk.TreeIter iter;
180
 
                        if (tasks.GetIterFirst (out iter)) {
181
 
                                do {
182
 
                                        Task task = tasks.GetValue (iter, 0) as Task;
183
 
                                        if (task.OriginNoteUri != null &&
184
 
                                                        task.OriginNoteUri.CompareTo (note.Uri) == 0)
185
 
                                                list.Add (task);
186
 
                                } while (tasks.IterNext (ref iter));
187
 
                        }
188
 
                        
189
 
                        return list;
190
 
                }
191
 
        #endregion // Public Methods
192
 
 
193
 
        #region Events
194
 
                public static event TasksChangedHandler TaskDeleted;
195
 
                public static event TasksChangedHandler TaskAdded;
196
 
                public static event TaskRenamedHandler TaskRenamed;
197
 
                public static event TaskSavedHandler TaskSaved;
198
 
                public static event TaskStatusChangedHandler TaskStatusChanged;
199
 
        #endregion // Events
200
 
 
201
 
        #region Private Methods
202
 
                /// <summary>
203
 
                /// Create the notes directory if it doesn't exist yet.
204
 
                /// </summary>
205
 
                void CreateTasksDir ()
206
 
                {
207
 
                        if (!DirectoryExists (tasks_dir)) {
208
 
                                // First run. Create storage directory.
209
 
                                CreateDirectory (tasks_dir);
210
 
                        }
211
 
                }
212
 
 
213
 
                // For overriding in test methods.
214
 
                protected virtual bool DirectoryExists (string directory)
215
 
                {
216
 
                        return Directory.Exists (directory);
217
 
                }
218
 
 
219
 
                // For overriding in test methods.
220
 
                protected virtual DirectoryInfo CreateDirectory (string directory)
221
 
                {
222
 
                        return Directory.CreateDirectory (directory);
223
 
                }
224
 
 
225
 
                protected virtual bool FirstRun ()
226
 
                {
227
 
                        return !DirectoryExists (tasks_dir);
228
 
                }
229
 
                
230
 
                /// <summary>
231
 
                /// Create a "Learn About Tomboy" task
232
 
                /// </summary>
233
 
                protected virtual void CreateStartTasks () 
234
 
                {
235
 
                        try {
236
 
                                Task first_task = Create (Catalog.GetString ("Learn About Tomboy"));
237
 
                                first_task.Details =
238
 
                                        Catalog.GetString (
239
 
                                                "Click on the Tomboy icon in your panel and select " +
240
 
                                                "\"Start Here\".  You'll see more instructions on how " +
241
 
                                                "to use Tomboy inside the \"Start Here\" note.\n\n" +
242
 
                                                "When you've opened the \"Start Here\" note, mark this " +
243
 
                                                "task as being complete.");
244
 
                                first_task.QueueSave (true);
245
 
                        } catch (Exception e) {
246
 
                                Logger.Warn ("Error creating the \"Learn About Tomboy\" task: {0}\n{1}",
247
 
                                                e.Message, e.StackTrace);
248
 
                        }
249
 
                }
250
 
 
251
 
                protected virtual void LoadTasks ()
252
 
                {
253
 
                        string [] files = Directory.GetFiles (tasks_dir, "*.task");
254
 
 
255
 
                        foreach (string file_path in files) {
256
 
                                try {
257
 
                                        Task task = Task.Load (file_path, this);
258
 
                                        if (task != null) {
259
 
                                                task.Renamed += OnTaskRenamed;
260
 
                                                task.Saved += OnTaskSaved;
261
 
                                                task.StatusChanged += OnTaskStatusChanged;
262
 
                                                
263
 
                                                Gtk.TreeIter iter = tasks.Append ();
264
 
                                                tasks.SetValue (iter, 0, task);
265
 
                                                task_iters [task.Uri] = iter;
266
 
                                        }
267
 
                                } catch (System.Xml.XmlException e) {
268
 
                                        Logger.Log ("Error parsing task XML, skipping \"{0}\": {1}",
269
 
                                                    file_path,
270
 
                                                    e.Message);
271
 
                                }
272
 
                        }
273
 
                        
274
 
                        Logger.Debug ("{0} tasks loaded", task_iters.Count);
275
 
                }
276
 
 
277
 
                string MakeNewFileName ()
278
 
                {
279
 
                        Guid guid = Guid.NewGuid ();
280
 
                        return Path.Combine (tasks_dir, guid.ToString () + ".task");
281
 
                }
282
 
 
283
 
                void EmitRowChangedForTask (Task task)
284
 
                {
285
 
                        if (task_iters.ContainsKey (task.Uri)) {
286
 
                                Gtk.TreeIter iter = task_iters [task.Uri];
287
 
                                
288
 
                                tasks.EmitRowChanged (tasks.GetPath (iter), iter);
289
 
                        }
290
 
                }
291
 
 
292
 
        #endregion // Private Methods
293
 
 
294
 
        #region Event Handlers
295
 
                void OnTaskRenamed (Task task, string old_summary)
296
 
                {
297
 
                        EmitRowChangedForTask (task);
298
 
 
299
 
                        if (TaskRenamed != null)
300
 
                                TaskRenamed (task, old_summary);
301
 
                }
302
 
                
303
 
                void OnTaskSaved (Task task)
304
 
                {
305
 
                        EmitRowChangedForTask (task);
306
 
 
307
 
                        if (TaskSaved != null)
308
 
                                TaskSaved (task);
309
 
                }
310
 
                
311
 
                void OnTaskStatusChanged (Task task)
312
 
                {
313
 
                        EmitRowChangedForTask (task);
314
 
                        
315
 
                        if (TaskStatusChanged != null)
316
 
                                TaskStatusChanged (task);
317
 
                }
318
 
 
319
 
        #endregion // Event Handlers
320
 
        }
321
 
}