~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): Sebastian Dröge
  • Date: 2007-07-16 10:26:35 UTC
  • mfrom: (1.1.21 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20070716102635-0wzk26jo50csob7b
Tags: 0.7.2-0ubuntu1
New upstream release

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
        #endregion // Public Methods
 
172
 
 
173
        #region Events
 
174
                public static event TasksChangedHandler TaskDeleted;
 
175
                public static event TasksChangedHandler TaskAdded;
 
176
                public static event TaskRenamedHandler TaskRenamed;
 
177
                public static event TaskSavedHandler TaskSaved;
 
178
                public static event TaskStatusChangedHandler TaskStatusChanged;
 
179
        #endregion // Events
 
180
 
 
181
        #region Private Methods
 
182
                /// <summary>
 
183
                /// Create the notes directory if it doesn't exist yet.
 
184
                /// </summary>
 
185
                void CreateTasksDir ()
 
186
                {
 
187
                        if (!DirectoryExists (tasks_dir)) {
 
188
                                // First run. Create storage directory.
 
189
                                CreateDirectory (tasks_dir);
 
190
                        }
 
191
                }
 
192
 
 
193
                // For overriding in test methods.
 
194
                protected virtual bool DirectoryExists (string directory)
 
195
                {
 
196
                        return Directory.Exists (directory);
 
197
                }
 
198
 
 
199
                // For overriding in test methods.
 
200
                protected virtual DirectoryInfo CreateDirectory (string directory)
 
201
                {
 
202
                        return Directory.CreateDirectory (directory);
 
203
                }
 
204
 
 
205
                protected virtual bool FirstRun ()
 
206
                {
 
207
                        return !DirectoryExists (tasks_dir);
 
208
                }
 
209
                
 
210
                /// <summary>
 
211
                /// Create a "Learn About Tomboy" task
 
212
                /// </summary>
 
213
                protected virtual void CreateStartTasks () 
 
214
                {
 
215
                        try {
 
216
                                Task first_task = Create (Catalog.GetString ("Learn About Tomboy"));
 
217
                                first_task.Details =
 
218
                                        Catalog.GetString (
 
219
                                                "Click on the Tomboy icon in your panel and select " +
 
220
                                                "\"Start Here\".  You'll see more instructions on how " +
 
221
                                                "to use Tomboy inside the \"Start Here\" note.\n\n" +
 
222
                                                "When you've opened the \"Start Here\" note, mark this " +
 
223
                                                "task as being complete.");
 
224
                                first_task.QueueSave (true);
 
225
                        } catch (Exception e) {
 
226
                                Logger.Warn ("Error creating the \"Learn About Tomboy\" task: {0}\n{1}",
 
227
                                                e.Message, e.StackTrace);
 
228
                        }
 
229
                }
 
230
 
 
231
                protected virtual void LoadTasks ()
 
232
                {
 
233
                        string [] files = Directory.GetFiles (tasks_dir, "*.task");
 
234
 
 
235
                        foreach (string file_path in files) {
 
236
                                try {
 
237
                                        Task task = Task.Load (file_path, this);
 
238
                                        if (task != null) {
 
239
                                                task.Renamed += OnTaskRenamed;
 
240
                                                task.Saved += OnTaskSaved;
 
241
                                                task.StatusChanged += OnTaskStatusChanged;
 
242
                                                
 
243
                                                Gtk.TreeIter iter = tasks.Append ();
 
244
                                                tasks.SetValue (iter, 0, task);
 
245
                                                task_iters [task.Uri] = iter;
 
246
                                        }
 
247
                                } catch (System.Xml.XmlException e) {
 
248
                                        Logger.Log ("Error parsing task XML, skipping \"{0}\": {1}",
 
249
                                                    file_path,
 
250
                                                    e.Message);
 
251
                                }
 
252
                        }
 
253
                        
 
254
                        Logger.Debug ("{0} tasks loaded", task_iters.Count);
 
255
                }
 
256
 
 
257
                string MakeNewFileName ()
 
258
                {
 
259
                        Guid guid = Guid.NewGuid ();
 
260
                        return Path.Combine (tasks_dir, guid.ToString () + ".task");
 
261
                }
 
262
 
 
263
                void EmitRowChangedForTask (Task task)
 
264
                {
 
265
                        if (task_iters.ContainsKey (task.Uri)) {
 
266
                                Gtk.TreeIter iter = task_iters [task.Uri];
 
267
                                
 
268
                                tasks.EmitRowChanged (tasks.GetPath (iter), iter);
 
269
                        }
 
270
                }
 
271
 
 
272
        #endregion // Private Methods
 
273
 
 
274
        #region Event Handlers
 
275
                void OnTaskRenamed (Task task, string old_summary)
 
276
                {
 
277
                        EmitRowChangedForTask (task);
 
278
 
 
279
                        if (TaskRenamed != null)
 
280
                                TaskRenamed (task, old_summary);
 
281
                }
 
282
                
 
283
                void OnTaskSaved (Task task)
 
284
                {
 
285
                        EmitRowChangedForTask (task);
 
286
 
 
287
                        if (TaskSaved != null)
 
288
                                TaskSaved (task);
 
289
                }
 
290
                
 
291
                void OnTaskStatusChanged (Task task)
 
292
                {
 
293
                        EmitRowChangedForTask (task);
 
294
                        
 
295
                        if (TaskStatusChanged != null)
 
296
                                TaskStatusChanged (task);
 
297
                }
 
298
 
 
299
        #endregion // Event Handlers
 
300
        }
 
301
}