[tasque/transition: 208/213] [RTM] Adjustments to new model API



commit b85da4b61fe92271445d7f9441d91f641a4da787
Author: Antonius Riha <antoniusriha gmail com>
Date:   Sun Aug 19 22:48:54 2012 +0200

    [RTM] Adjustments to new model API

 src/Addins/RtmBackend/Gtk/RtmPreferencesWidget.cs |   10 +-
 src/Addins/RtmBackend/RtmBackend.cs               |  415 +++++++--------------
 src/Addins/RtmBackend/RtmCategory.cs              |   23 +-
 src/Addins/RtmBackend/RtmNote.cs                  |   34 +-
 src/Addins/RtmBackend/RtmTask.cs                  |  302 ++++-----------
 5 files changed, 251 insertions(+), 533 deletions(-)
---
diff --git a/src/Addins/RtmBackend/Gtk/RtmPreferencesWidget.cs b/src/Addins/RtmBackend/Gtk/RtmPreferencesWidget.cs
index d3fab58..dac5216 100644
--- a/src/Addins/RtmBackend/Gtk/RtmPreferencesWidget.cs
+++ b/src/Addins/RtmBackend/Gtk/RtmPreferencesWidget.cs
@@ -7,7 +7,7 @@ using Gtk;
 
 namespace Tasque.Backends.RtmBackend
 {
-	public class RtmPreferencesWidget : EventBox
+	public class RtmPreferencesWidget : EventBox, IBackendPreferences
 	{
 		private LinkButton		authButton;
 		private Label			statusLabel;
@@ -67,7 +67,7 @@ namespace Tasque.Backends.RtmBackend
 			if (isAuthorized) {
 				statusLabel.Text = "\n\n" +
 					Catalog.GetString ("You are currently connected");
-				string userName = Application.Preferences.Get (Preferences.UserNameKey);
+				var userName = Application.Instance.Preferences.Get (Preferences.UserNameKey);
 				if (userName != null && userName.Trim () != string.Empty)
 					statusLabel.Text = "\n\n" +
 						Catalog.GetString ("You are currently connected as") +
@@ -87,7 +87,7 @@ namespace Tasque.Backends.RtmBackend
 		
 		private void LoadPreferences ()
 		{
-			string authToken = Tasque.Application.Preferences.Get (Preferences.AuthTokenKey);
+			var authToken = Application.Instance.Preferences.Get (Preferences.AuthTokenKey);
 			if (authToken == null || authToken.Trim () == "") {
 				Debug.WriteLine ("Rtm: Not authorized");
 				isAuthorized = false;
@@ -99,7 +99,7 @@ namespace Tasque.Backends.RtmBackend
 		
 		private void OnAuthButtonClicked (object sender, EventArgs args)
 		{
-			RtmBackend rtmBackend = Application.Backend as RtmBackend;
+			var rtmBackend = Application.Instance.Backend as RtmBackend;
 			if (rtmBackend != null) {
 				if (!isAuthorized && !authRequested) {
 					string url = string.Empty;
@@ -139,7 +139,7 @@ namespace Tasque.Backends.RtmBackend
 					statusLabel.Text = "\n\n" +
 						Catalog.GetString ("You are currently connected");
 					string userName =
-						Application.Preferences.Get (Preferences.UserNameKey);
+						Application.Instance.Preferences.Get (Preferences.UserNameKey);
 					if (userName != null && userName.Trim () != string.Empty)
 						statusLabel.Text = "\n\n" +
 							Catalog.GetString ("You are currently connected as") +
diff --git a/src/Addins/RtmBackend/RtmBackend.cs b/src/Addins/RtmBackend/RtmBackend.cs
index 8574299..61c5c09 100644
--- a/src/Addins/RtmBackend/RtmBackend.cs
+++ b/src/Addins/RtmBackend/RtmBackend.cs
@@ -17,82 +17,72 @@ namespace Tasque.Backends.RtmBackend
 {
 	public class RtmBackend : Backend
 	{
-		private const string apiKey = "b29f7517b6584035d07df3170b80c430";
-		private const string sharedSecret = "93eb5f83628b2066";
-		private Thread refreshThread;
-		private bool runningRefreshThread;
-		private AutoResetEvent runRefreshEvent;
-		private Rtm rtm;
-		private string frob;
-		private Auth rtmAuth;
-		private string timeline;
-		private object taskLock;
-		private Dictionary<string, RtmCategory> categories;
-		private object catLock;
-		
 		public RtmBackend () : base ("Remember the Milk")
 		{
 			taskLock = new Object ();
-			
-			categories = new Dictionary<string, RtmCategory> ();
 			catLock = new Object ();
 
-			// *************************************
-			// Data Model Set up
-			// *************************************
-
 			runRefreshEvent = new AutoResetEvent (false);
-			
 			runningRefreshThread = false;
 			refreshThread = new Thread (RefreshThreadLoop);
 		}
 		
+		public override Category DefaultCategory {
+			get {
+				if (!Initialized)
+					throw new InvalidOperationException ("Backend not initialized");
+				return Categories.ElementAt (0);
+			}
+			set { throw new NotSupportedException (); }
+		}
+		
 		public string RtmUserName {
 			get {
-				if ((rtmAuth != null) && (rtmAuth.User != null)) {
+				if (rtmAuth != null && rtmAuth.User != null)
 					return rtmAuth.User.Username;
-				} else
+				else
 					return null;
 			}
 		}
 
 		#region Public Methods
-		protected override Task CreateTaskCore (string taskName, IEnumerable<Category> categories)
+		public override Task CreateTask (string taskName)
 		{
-			var category = categories.ElementAt (0);
-			var categoryID = (category as RtmCategory).ID;
-			RtmTask rtmTask = null;
-
-			if (rtm != null) {
-				try {
-					var list = rtm.TasksAdd (timeline, taskName, categoryID);
-					var ts = list.TaskSeriesCollection [0];
-					if (ts != null)
-						rtmTask = new RtmTask (ts, this, list.ID);
-				} catch (Exception e) {
-					Debug.WriteLine ("Unable to set create task: " + taskName);
-					Debug.WriteLine (e.ToString ());
-				}
-			} else
-				throw new Exception ("Unable to communicate with Remember The Milk");
-				
-			return rtmTask;
+			throw new NotImplementedException ();
+//			var category = categories.ElementAt (0);
+//			var categoryID = (category as RtmCategory).ID;
+//			RtmTask rtmTask = null;
+//
+//			if (rtm != null) {
+//				try {
+//					var list = rtm.TasksAdd (timeline, taskName, categoryID);
+//					var ts = list.TaskSeriesCollection [0];
+//					if (ts != null)
+//						rtmTask = new RtmTask (ts, this, list.ID);
+//				} catch (Exception e) {
+//					Debug.WriteLine ("Unable to set create task: " + taskName);
+//					Debug.WriteLine (e.ToString ());
+//				}
+//			} else
+//				throw new Exception ("Unable to communicate with Remember The Milk");
+//				
+//			return rtmTask;
 		}
 		
-		protected override void OnDeleteTask (Task task)
-		{
-			var rtmTask = task as RtmTask;
-			if (rtm != null) {
-				try {
-					rtm.TasksDelete (timeline, rtmTask.ListID, rtmTask.SeriesTaskID, rtmTask.TaskTaskID);
-				} catch (Exception e) {
-					Debug.WriteLine ("Unable to delete task: " + task.Name);
-					Debug.WriteLine (e.ToString ());
-				}
-			} else
-				throw new Exception ("Unable to communicate with Remember The Milk");
-			base.OnDeleteTask (task);
-		}
+//		protected override void OnDeleteTask (Task task)
+//		{
+//			var rtmTask = task as RtmTask;
+//			if (rtm != null) {
+//				try {
+//					rtm.TasksDelete (timeline, rtmTask.ListID, rtmTask.SeriesTaskID, rtmTask.TaskTaskID);
+//				} catch (Exception e) {
+//					Debug.WriteLine ("Unable to delete task: " + task.Name);
+//					Debug.WriteLine (e.ToString ());
+//				}
+//			} else
+//				throw new Exception ("Unable to communicate with Remember The Milk");
+//			base.OnDeleteTask (task);
+//		}
 		
 		public override void Refresh ()
 		{
@@ -105,26 +95,26 @@ namespace Tasque.Backends.RtmBackend
 			
 			Debug.WriteLine ("Done refreshing data!");
 		}
-
-		public void Initialize ()
+		
+		public override void Initialize ()
 		{
 			// *************************************
 			// AUTHENTICATION to Remember The Milk
 			// *************************************
-			string authToken = Application.Preferences.Get (Tasque.Preferences.AuthTokenKey);
+			var authToken = Application.Instance.Preferences.Get (Tasque.Preferences.AuthTokenKey);
 			if (authToken != null) {
 				Debug.WriteLine ("Found AuthToken, checking credentials...");
 				try {
-					rtm = new Rtm (apiKey, sharedSecret, authToken);
+					rtm = new Rtm (ApiKey, SharedSecret, authToken);
 					rtmAuth = rtm.AuthCheckToken (authToken);
 					timeline = rtm.TimelineCreate ();
 					Debug.WriteLine ("RTM Auth Token is valid!");
 					Debug.WriteLine ("Setting configured status to true");
 					Configured = true;
 				} catch (RtmApiException e) {
-					Application.Preferences.Set (Tasque.Preferences.AuthTokenKey, null);
-					Application.Preferences.Set (Tasque.Preferences.UserIdKey, null);
-					Application.Preferences.Set (Tasque.Preferences.UserNameKey, null);
+					Application.Instance.Preferences.Set (Tasque.Preferences.AuthTokenKey, null);
+					Application.Instance.Preferences.Set (Tasque.Preferences.UserIdKey, null);
+					Application.Instance.Preferences.Set (Tasque.Preferences.UserNameKey, null);
 					rtm = null;
 					rtmAuth = null;
 					Trace.TraceError ("Exception authenticating, reverting" + e.Message);
@@ -140,7 +130,7 @@ namespace Tasque.Backends.RtmBackend
 			}
 
 			if (rtm == null)
-				rtm = new Rtm (apiKey, sharedSecret);
+				rtm = new Rtm (ApiKey, SharedSecret);
 	
 			StartThread ();	
 		}
@@ -163,30 +153,29 @@ namespace Tasque.Backends.RtmBackend
 			runRefreshEvent.Set ();
 		}
 
-		public void Cleanup ()
+		protected override void Dispose (bool disposing)
 		{
-			runningRefreshThread = false;
-			runRefreshEvent.Set ();
-			refreshThread.Abort ();
+			if (disposing) {
+				runningRefreshThread = false;
+				runRefreshEvent.Set ();
+				refreshThread.Abort ();
+			}
+			base.Dispose (disposing);
 		}
 
-		public Gtk.Widget GetPreferencesWidget ()
-		{
-			return new RtmPreferencesWidget ();
-		}
+		public override IBackendPreferences Preferences { get { return new RtmPreferencesWidget (); } }
 
 		public string GetAuthUrl ()
 		{
 			frob = rtm.AuthGetFrob ();
-			string url = rtm.AuthCalcUrl (frob, AuthLevel.Delete);
-			return url;
+			return rtm.AuthCalcUrl (frob, AuthLevel.Delete);
 		}
 
 		public void FinishedAuth ()
 		{
 			rtmAuth = rtm.AuthGetToken (frob);
 			if (rtmAuth != null) {
-				Preferences prefs = Application.Preferences;
+				var prefs = Application.Instance.Preferences;
 				prefs.Set (Tasque.Preferences.AuthTokenKey, rtmAuth.Token);
 				if (rtmAuth.User != null) {
 					prefs.Set (Tasque.Preferences.UserNameKey, rtmAuth.User.Username);
@@ -194,12 +183,11 @@ namespace Tasque.Backends.RtmBackend
 				}
 			}
 			
-			string authToken =
-				Application.Preferences.Get (Tasque.Preferences.AuthTokenKey);
+			var authToken = Application.Instance.Preferences.Get (Tasque.Preferences.AuthTokenKey);
 			if (authToken != null) {
 				Debug.WriteLine ("Found AuthToken, checking credentials...");
 				try {
-					rtm = new Rtm (apiKey, sharedSecret, authToken);
+					rtm = new Rtm (ApiKey, SharedSecret, authToken);
 					rtmAuth = rtm.AuthCheckToken (authToken);
 					timeline = rtm.TimelineCreate ();
 					Debug.WriteLine ("RTM Auth Token is valid!");
@@ -213,19 +201,13 @@ namespace Tasque.Backends.RtmBackend
 				}	
 			}
 		}
-
+		
 		public void UpdateTaskName (RtmTask task)
 		{
 			if (rtm != null) {
 				try {
-					List list = rtm.TasksSetName (
-						timeline,
-						task.ListID,
-						task.SeriesTaskID,
-						task.TaskTaskID,
-						task.Name
-					);		
-					UpdateTaskFromResult (list);
+					rtm.TasksSetName (timeline, task.ListID, task.SeriesTaskID,
+					                  task.TaskTaskID, task.Name);
 				} catch (Exception e) {
 					Debug.WriteLine ("Unable to set name on task: " + task.Name);
 					Debug.WriteLine (e.ToString ());
@@ -237,23 +219,11 @@ namespace Tasque.Backends.RtmBackend
 		{
 			if (rtm != null) {
 				try {
-					List list;
 					if (task.DueDate == DateTime.MinValue)
-						list = rtm.TasksSetDueDate (
-							timeline,
-							task.ListID,
-							task.SeriesTaskID,
-							task.TaskTaskID
-						);
+						rtm.TasksSetDueDate (timeline, task.ListID, task.SeriesTaskID, task.TaskTaskID);
 					else	
-						list = rtm.TasksSetDueDate (
-							timeline,
-							task.ListID,
-							task.SeriesTaskID,
-							task.TaskTaskID,
-							task.DueDateString
-						);
-					UpdateTaskFromResult (list);
+						rtm.TasksSetDueDate (timeline, task.ListID, task.SeriesTaskID,
+						                     task.TaskTaskID, task.DueDateString);
 				} catch (Exception e) {
 					Debug.WriteLine ("Unable to set due date on task: " + task.Name);
 					Debug.WriteLine (e.ToString ());
@@ -261,23 +231,12 @@ namespace Tasque.Backends.RtmBackend
 			}
 		}
 		
-		public void UpdateTaskCompleteDate (RtmTask task)
-		{
-			UpdateTask (task);
-		}
-		
 		public void UpdateTaskPriority (RtmTask task)
 		{
 			if (rtm != null) {
 				try {
-					List list = rtm.TasksSetPriority (
-						timeline,
-						task.ListID,
-						task.SeriesTaskID,
-						task.TaskTaskID,
-						task.PriorityString
-					);
-					UpdateTaskFromResult (list);
+					rtm.TasksSetPriority (timeline, task.ListID, task.SeriesTaskID,
+					                      task.TaskTaskID, task.PriorityString);
 				} catch (Exception e) {
 					Debug.WriteLine ("Unable to set priority on task: " + task.Name);
 					Debug.WriteLine (e.ToString ());
@@ -287,41 +246,21 @@ namespace Tasque.Backends.RtmBackend
 		
 		public void UpdateTaskActive (RtmTask task)
 		{
-			if (task.State == TaskState.Completed) {
-				if (rtm != null) {
-					try {
-						List list = rtm.TasksUncomplete (
-							timeline,
-							task.ListID,
-							task.SeriesTaskID,
-							task.TaskTaskID
-						);
-						UpdateTaskFromResult (list);
-					} catch (Exception e) {
-						Debug.WriteLine ("Unable to set Task as completed: " + task.Name);
-						Debug.WriteLine (e.ToString ());
-					}
+			if (rtm != null) {
+				try {
+					rtm.TasksUncomplete (timeline, task.ListID, task.SeriesTaskID, task.TaskTaskID);
+				} catch (Exception e) {
+					Debug.WriteLine ("Unable to set Task as completed: " + task.Name);
+					Debug.WriteLine (e.ToString ());
 				}
-			} else
-				UpdateTask (task);
-		}
-		
-		public void UpdateTaskInactive (RtmTask task)
-		{
-			UpdateTask (task);
+			}
 		}
 
 		public void UpdateTaskCompleted (RtmTask task)
 		{
 			if (rtm != null) {
 				try {
-					List list = rtm.TasksComplete (
-						timeline,
-						task.ListID,
-						task.SeriesTaskID,
-						task.TaskTaskID
-					);
-					UpdateTaskFromResult (list);
+					rtm.TasksComplete (timeline, task.ListID, task.SeriesTaskID, task.TaskTaskID);
 				} catch (Exception e) {
 					Debug.WriteLine ("Unable to set Task as completed: " + task.Name);
 					Debug.WriteLine (e.ToString ());
@@ -331,21 +270,21 @@ namespace Tasque.Backends.RtmBackend
 
 		public void UpdateTaskDeleted (RtmTask task)
 		{
-			UpdateTask (task);
+			if (rtm != null) {
+				try {
+					rtm.TasksDelete (timeline, task.ListID, task.SeriesTaskID, task.TaskTaskID);
+				} catch (Exception e) {
+					Debug.WriteLine ("Unable to set Task as deleted: " + task.Name);
+					Debug.WriteLine (e.ToString ());
+				}
+			}
 		}
 
 		public void MoveTaskCategory (RtmTask task, string id)
 		{
 			if (rtm != null) {
 				try {
-					List list = rtm.TasksMoveTo (
-						timeline,
-						task.ListID,
-						id,
-						task.SeriesTaskID,
-						task.TaskTaskID
-					);
-					UpdateTaskFromResult (list);
+					rtm.TasksMoveTo (timeline, task.ListID, id, task.SeriesTaskID, task.TaskTaskID);
 				} catch (Exception e) {
 					Debug.WriteLine ("Unable to set Task as completed: " + task.Name);
 					Debug.WriteLine (e.ToString ());
@@ -353,68 +292,17 @@ namespace Tasque.Backends.RtmBackend
 			}					
 		}
 		
-		public void UpdateTask (RtmTask task)
-		{
-			lock (taskLock) {
-				Gtk.TreeIter iter;
-				
-				Gtk.Application.Invoke (delegate {
-					if (taskIters.ContainsKey (task.ID)) {
-						iter = taskIters [task.ID];
-						Tasks.SetValue (iter, 0, task);
-					}
-				}
-				);
-			}		
-		}
-		
-		public RtmTask UpdateTaskFromResult (List list)
-		{
-			TaskSeries ts = list.TaskSeriesCollection [0];
-			if (ts != null) {
-				RtmTask rtmTask = new RtmTask (ts, this, list.ID);
-				lock (taskLock) {
-					Gtk.Application.Invoke (delegate {
-						if (taskIters.ContainsKey (rtmTask.ID)) {
-							Gtk.TreeIter iter = taskIters [rtmTask.ID];
-							Tasks.SetValue (iter, 0, rtmTask);
-						} else {
-							Gtk.TreeIter iter = Tasks.AppendNode ();
-							taskIters.Add (rtmTask.ID, iter);
-							Tasks.SetValue (iter, 0, rtmTask);
-						}
-					}
-					);
-				}
-				return rtmTask;				
-			}
-			return null;
-		}
-		
-		public RtmCategory GetCategory (string id)
-		{
-			if (categories.ContainsKey (id))
-				return categories [id];
-			else
-				return null;
-		}
-		
 		public RtmNote CreateNote (RtmTask rtmTask, string text)
 		{
-			RtmNet.Note note = null;
+			Note note = null;
 			RtmNote rtmNote = null;
 			
 			if (rtm != null) {
 				try {
-					note = rtm.NotesAdd (
-						timeline,
-						rtmTask.ListID,
-						rtmTask.SeriesTaskID,
-						rtmTask.TaskTaskID,
-						String.Empty,
-						text
-					);
+					note = rtm.NotesAdd (timeline, rtmTask.ListID, rtmTask.SeriesTaskID,
+					                     rtmTask.TaskTaskID, string.Empty, text);
 					rtmNote = new RtmNote (note);
+					rtmNote.OnTextChangedAction = delegate { SaveNote (rtmTask, rtmNote); };
 				} catch (Exception e) {
 					Debug.WriteLine ("RtmBackend.CreateNote: Unable to create a new note");
 					Debug.WriteLine (e.ToString ());
@@ -438,11 +326,11 @@ namespace Tasque.Backends.RtmBackend
 				throw new Exception ("Unable to communicate with Remember The Milk");
 		}
 
-		public void SaveNote (RtmTask rtmTask, RtmNote note)
+		void SaveNote (RtmTask rtmTask, RtmNote note)
 		{
 			if (rtm != null) {
 				try {
-					rtm.NotesEdit (timeline, note.ID, String.Empty, note.Text);
+					rtm.NotesEdit (timeline, note.ID, string.Empty, note.Text);
 				} catch (Exception e) {
 					Debug.WriteLine ("RtmBackend.SaveNote: Unable to save note");
 					Debug.WriteLine (e.ToString ());
@@ -457,48 +345,36 @@ namespace Tasque.Backends.RtmBackend
 		/// Update the model to match what is in RTM
 		/// FIXME: This is a lame implementation and needs to be optimized
 		/// </summary>		
-		private void UpdateCategories (Lists lists)
+		void UpdateCategories (Lists lists)
 		{
 			Debug.WriteLine ("RtmBackend.UpdateCategories was called");
-			
 			try {
-				foreach (List list in lists.listCollection) {
-					RtmCategory rtmCategory = new RtmCategory (list);
-
+				foreach (var list in lists.listCollection) {
 					lock (catLock) {
-						Gtk.TreeIter iter;
-						
-						Gtk.Application.Invoke (delegate {
-
-							if (categories.ContainsKey (rtmCategory.ID)) {
-								iter = categories [rtmCategory.ID].Iter;
-								categoryListStore.SetValue (iter, 0, rtmCategory);
-							} else {
-								iter = categoryListStore.Append ();
-								categoryListStore.SetValue (iter, 0, rtmCategory);
-								rtmCategory.Iter = iter;
-								categories.Add (rtmCategory.ID, rtmCategory);
+						Application.Instance.Dispatcher.Invoke (delegate {
+							RtmCategory rtmCategory;
+							if (!Categories.Any (c => ((RtmCategory)c).ID == list.ID)) {
+								rtmCategory = new RtmCategory (list);
+								Categories.Add (rtmCategory);
 							}
-						}
-						);
+						});
 					}
 				}
 			} catch (Exception e) {
 				Debug.WriteLine ("Exception in fetch " + e.Message);
 			}
-			Debug.WriteLine ("RtmBackend.UpdateCategories is done");			
+			Debug.WriteLine ("RtmBackend.UpdateCategories is done");
 		}
 
 		/// <summary>
 		/// Update the model to match what is in RTM
 		/// FIXME: This is a lame implementation and needs to be optimized
 		/// </summary>		
-		private void UpdateTasks (Lists lists)
+		void UpdateTasks (Lists lists)
 		{
 			Debug.WriteLine ("RtmBackend.UpdateTasks was called");
-			
 			try {
-				foreach (List list in lists.listCollection) {
+				foreach (var list in lists.listCollection) {
 					Tasks tasks = null;
 					try {
 						tasks = rtm.TasksGetList (list.ID);
@@ -507,27 +383,22 @@ namespace Tasque.Backends.RtmBackend
 					}
 
 					if (tasks != null) {
-						foreach (List tList in tasks.ListCollection) {
+						foreach (var tList in tasks.ListCollection) {
 							if (tList.TaskSeriesCollection == null)
 								continue;
-							foreach (TaskSeries ts in tList.TaskSeriesCollection) {
-								RtmTask rtmTask = new RtmTask (ts, this, list.ID);
-								
+							
+							foreach (var ts in tList.TaskSeriesCollection) {
 								lock (taskLock) {
-									Gtk.TreeIter iter;
-									
-									Gtk.Application.Invoke (delegate {
-
-										if (taskIters.ContainsKey (rtmTask.ID)) {
-											iter = taskIters [rtmTask.ID];
-										} else {
-											iter = Tasks.AppendNode ();
-											taskIters.Add (rtmTask.ID, iter);
+									Application.Instance.Dispatcher.Invoke (delegate {
+										if (!Tasks.Any (t => ((RtmTask)t).ID == ts.TaskID)) {
+											
+											var rtmTask = new RtmTask (ts, this, list.ID);
+											var cat = Categories.SingleOrDefault (
+												c => ((RtmCategory)c).ID == list.ID);
+											if (cat != null)
+												cat.Add (rtmTask);
 										}
-
-										Tasks.SetValue (iter, 0, rtmTask);
-									}
-									);
+									});
 								}
 							}
 						}
@@ -540,48 +411,46 @@ namespace Tasque.Backends.RtmBackend
 			Debug.WriteLine ("RtmBackend.UpdateTasks is done");			
 		}
 		
-		private void RefreshThreadLoop ()
+		void RefreshThreadLoop ()
 		{
 			while (runningRefreshThread) {
 				runRefreshEvent.WaitOne ();
 
 				if (!runningRefreshThread)
 					return;
-
+				
 				// Fire the event on the main thread
-				Gtk.Application.Invoke (delegate {
-					if (BackendSyncStarted != null)
-						BackendSyncStarted ();
-				}
-				);
-
-				runRefreshEvent.Reset ();
+				Application.Instance.Dispatcher.Invoke (delegate { OnBackendSyncStarted (); });
 
 				if (rtmAuth != null) {
-					Lists lists = rtm.ListsGetList ();
+					var lists = rtm.ListsGetList ();
 					UpdateCategories (lists);
 					UpdateTasks (lists);
 				}
-				if (!initialized) {
-					initialized = true;
 
-					// Fire the event on the main thread
-					Gtk.Application.Invoke (delegate {
-						if (BackendInitialized != null)
-							BackendInitialized ();
-					}
-					);
-				}
 
 				// Fire the event on the main thread
-				Gtk.Application.Invoke (delegate {
-					if (BackendSyncFinished != null)
-						BackendSyncFinished ();
-				}
-				);
+				Application.Instance.Dispatcher.Invoke (delegate { 
+					if (!Initialized)
+						Initialized = true;
+				});
 			}
+
+			// Fire the event on the main thread
+			Application.Instance.Dispatcher.Invoke (delegate { OnBackendSyncFinished (); });
 		}
+		#endregion // Private Methods
 		
-#endregion // Private Methods
+		const string ApiKey = "b29f7517b6584035d07df3170b80c430";
+		const string SharedSecret = "93eb5f83628b2066";
+		Thread refreshThread;
+		bool runningRefreshThread;
+		AutoResetEvent runRefreshEvent;
+		Rtm rtm;
+		string frob;
+		Auth rtmAuth;
+		string timeline;
+		object taskLock, catLock;
 	}
 }
+
diff --git a/src/Addins/RtmBackend/RtmCategory.cs b/src/Addins/RtmBackend/RtmCategory.cs
index eef5f67..6f2d1f2 100644
--- a/src/Addins/RtmBackend/RtmCategory.cs
+++ b/src/Addins/RtmBackend/RtmCategory.cs
@@ -11,17 +11,12 @@ namespace Tasque.Backends.RtmBackend
 {
 	public class RtmCategory : Category
 	{
-		private List list;
-		private Gtk.TreeIter iter;
+		List list;
 
-		public RtmCategory (List list)
+		public RtmCategory (List list) : base (list.Name)
 		{
 			this.list = list;
 		}
-		
-		public string Name {
-			get { return list.Name; }
-		}
 
 		public string ID {
 			get { return list.ID; }
@@ -46,19 +41,5 @@ namespace Tasque.Backends.RtmBackend
 		public int Smart {
 			get { return list.Smart; }
 		}
-		
-		public Gtk.TreeIter Iter {
-			get { return iter; }
-			set { iter = value; }
-		}
-
-		public bool ContainsTask (Task task)
-		{
-			if (task.Category is RtmCategory)
-				return ((task.Category as RtmCategory).ID.CompareTo (ID) == 0);
-			else
-				return false;
-		}
-
 	}
 }
diff --git a/src/Addins/RtmBackend/RtmNote.cs b/src/Addins/RtmBackend/RtmNote.cs
index 30af63d..45efe96 100644
--- a/src/Addins/RtmBackend/RtmNote.cs
+++ b/src/Addins/RtmBackend/RtmNote.cs
@@ -4,31 +4,37 @@
 // To change standard headers go to Edit->Preferences->Coding->Standard Headers
 //
 using System;
-using Tasque;
 using RtmNet;
+using Tasque;
 
 namespace Tasque.Backends.RtmBackend
 {
 	public class RtmNote : TaskNote
 	{
-		Note note;
-		
-		public RtmNote (Note note)
+		static string GetText (Note note)
 		{
-			this.note = note;
-			if ((note.Title != null) && (note.Title.Length > 0)) {
+			if (note.Title != null && note.Title.Length > 0)
 				note.Text = note.Title + note.Text;
-			}
-			note.Title = String.Empty;
+			note.Title = string.Empty;
+			return note.Text;
 		}
 		
-		public string ID {
-			get { return note.ID; }
+		public RtmNote (Note note) : base (note.Text)
+		{
+			this.note = note;
 		}
-    
-		public string Text {
-			get { return note.Text; }
-			set { note.Text = value; }
+		
+		public string ID { get { return note.ID; } }
+		
+		protected override void OnTextChanged ()
+		{
+			if (OnTextChangedAction != null)
+				OnTextChangedAction ();
+			base.OnTextChanged ();
 		}
+		
+		internal Action OnTextChangedAction { get; set; }
+		
+		Note note;
 	}
 }
diff --git a/src/Addins/RtmBackend/RtmTask.cs b/src/Addins/RtmBackend/RtmTask.cs
index b6b5748..c819834 100644
--- a/src/Addins/RtmBackend/RtmTask.cs
+++ b/src/Addins/RtmBackend/RtmTask.cs
@@ -1,17 +1,15 @@
 // Task.cs created with MonoDevelop
 // User: boyd at 8:50 PM 2/10/2008
 using System;
-using RtmNet;
 using System.Collections.Generic;
+using System.Diagnostics;
+using RtmNet;
 
 namespace Tasque.Backends.RtmBackend
 {
-	public class RtmTask : AbstractTask
+	public class RtmTask : Task
 	{
-		private RtmBackend rtmBackend;
-		private TaskState state;
-		private RtmCategory category;
-		private List<TaskNote> notes;
+		RtmBackend rtmBackend;
 		TaskSeries taskSeries;
 		
 		/// <summary>
@@ -20,58 +18,68 @@ namespace Tasque.Backends.RtmBackend
 		/// <param name="taskSeries">
 		/// A <see cref="TaskSeries"/>
 		/// </param>
-		public RtmTask (TaskSeries taskSeries, RtmBackend be, string listID)
+		public RtmTask (TaskSeries taskSeries, RtmBackend be, string listId)
+			: base (taskSeries.Name, TaskNoteSupport.Multiple)
 		{
+			ListID = listId;
 			this.taskSeries = taskSeries;
-			this.rtmBackend = be;
-			this.category = be.GetCategory (listID);
+			rtmBackend = be;
+			DueDate = taskSeries.Task.Due;
+			CompletionDate = taskSeries.Task.Completed;
+			
+			var priority = TaskPriority.None;
+			switch (taskSeries.Task.Priority) {
+			case "N":
+				priority = TaskPriority.None;
+				break;
+			case "1":
+				priority = TaskPriority.High;
+				break;
+			case "2":
+				priority = TaskPriority.Medium;
+				break;
+			case "3":
+				priority = TaskPriority.Low;
+				break;
+			}
+			Priority = priority;
 			
-			if (CompletionDate == DateTime.MinValue)
-				state = TaskState.Active;
+			if (taskSeries.Task.Completed == DateTime.MinValue)
+				State = TaskState.Active;
 			else
-				state = TaskState.Completed;
-			notes = new List<TaskNote> ();
-
+				State = TaskState.Completed;
+			
 			if (taskSeries.Notes.NoteCollection != null) {
-				foreach (Note note in taskSeries.Notes.NoteCollection) {
-					RtmNote rtmNote = new RtmNote (note);
-					notes.Add (rtmNote);
+				foreach (var note in taskSeries.Notes.NoteCollection) {
+					var rtmNote = new RtmNote (note);
+					AddNote (rtmNote);
 				}
 			}
 		}
 		
-		#region Public Properties
-		/// <value>
-		/// Gets the id of the task
-		/// </value>
-		public override string Id {
-			get { return taskSeries.Task.TaskID; } 
-		}
-
-		/// <value>
-		/// Holds the name of the task
-		/// </value>		
-		public override string Name {
-			get { return taskSeries.Name; }
-			set {
-				if (value != null) {
-					taskSeries.Name = value.Trim ();
-					rtmBackend.UpdateTaskName (this);
-				}
-			}
-		}
+		public string ListID { get; private set; }
 		
-		/// <value>
-		/// Due Date for the task
-		/// </value>
-		public override DateTime DueDate {
-			get { return taskSeries.Task.Due; }
-			set { 
-				taskSeries.Task.Due = value;
-				rtmBackend.UpdateTaskDueDate (this);			
+		protected override void OnNameChanged ()
+		{
+			var name = Name.Trim ();
+			if (name != Name) {
+				Name = name;
+				return;
+			}
+			
+			if (taskSeries != null) {
+				taskSeries.Name = name;
+				rtmBackend.UpdateTaskName (this);
 			}
+			base.OnNameChanged ();
 		}
 		
+		protected override void OnDueDateChanged ()
+		{
+			taskSeries.Task.Due = DueDate;
+			rtmBackend.UpdateTaskDueDate (this);		
+			base.OnDueDateChanged ();
+		}
 
 		/// <value>
 		/// Due Date for the task
@@ -84,106 +92,28 @@ namespace Tasque.Backends.RtmBackend
 				return dateString;
 			}
 		}
-
-		
-		/// <value>
-		/// Completion Date for the task
-		/// </value>
-		public override DateTime CompletionDate {
-			get { return taskSeries.Task.Completed; }
-			set { 
-				taskSeries.Task.Completed = value;
-				rtmBackend.UpdateTaskCompleteDate (this);
-			}
-		}
-		
-		/// <value>
-		/// Returns if the task is complete
-		/// </value>
-		public override bool IsComplete {
-			get { return state == TaskState.Completed; }
-		}
-		
-		/// <value>
-		/// Holds the priority of the task
-		/// </value>
-		public override TaskPriority Priority {
-			get { 
-				switch (taskSeries.Task.Priority) {
-				default:
-				case "N":
-					return TaskPriority.None;
-				case "1":
-					return TaskPriority.High;
-				case "2":
-					return TaskPriority.Medium;
-				case "3":
-					return TaskPriority.Low;
-				}
-			}
-			set {
-				switch (value) {
-				default:
-				case TaskPriority.None:
-					taskSeries.Task.Priority = "N";
-					break;
-				case TaskPriority.High:
-					taskSeries.Task.Priority = "1";
-					break;
-				case TaskPriority.Medium:
-					taskSeries.Task.Priority = "2";
-					break;
-				case TaskPriority.Low:
-					taskSeries.Task.Priority = "3";
-					break;
-				}
-				rtmBackend.UpdateTaskPriority (this);				
-			}
-		}
-		
-		public string PriorityString {
-			get { return taskSeries.Task.Priority; }
-		}		
-		
-		
-		/// <value>
-		/// Returns if the task has any notes
-		/// </value>
-		public override bool HasNotes {
-			get { return (notes.Count > 0); }
-		}
-		
-		/// <value>
-		/// Returns if the task supports multiple notes
-		/// </value>
-		public override bool SupportsMultipleNotes {
-			get { return true; }
-		}
-		
-		/// <value>
-		/// Holds the current state of the task
-		/// </value>
-		public override TaskState State {
-			get { return state; }
-		}
 		
-		/// <value>
-		/// Returns the category object for this task
-		/// </value>
-		public override Category Category {
-			get { return category; } 
-			set {
-				RtmCategory rtmCategory = value as RtmCategory;
-				rtmBackend.MoveTaskCategory (this, rtmCategory.ID);				
+		protected override void OnPriorityChanged ()
+		{
+			switch (Priority) {
+			case TaskPriority.None:
+				taskSeries.Task.Priority = "N";
+				break;
+			case TaskPriority.High:
+				taskSeries.Task.Priority = "1";
+				break;
+			case TaskPriority.Medium:
+				taskSeries.Task.Priority = "2";
+				break;
+			case TaskPriority.Low:
+				taskSeries.Task.Priority = "3";
+				break;
 			}
+			rtmBackend.UpdateTaskPriority (this);	
+			base.OnPriorityChanged ();
 		}
 		
-		/// <value>
-		/// Returns the notes associates with this task
-		/// </value>
-		public override List<TaskNote> Notes {
-			get { return notes; }
-		}
+		public string PriorityString { get { return taskSeries.Task.Priority; } }
 		
 		/// <value>
 		/// Holds the current RtmBackend for this task
@@ -204,101 +134,33 @@ namespace Tasque.Backends.RtmBackend
 			get { return taskSeries.Task.TaskID; }
 		}
 		
-		public string ListID {
-			get { return category.ID; }
-		}
-		#endregion // Public Properties
-		
-		#region Public Methods
-		/// <summary>
-		/// Activates the task
-		/// </summary>
-		public override void Activate ()
+		protected override void OnActivate ()
 		{
-			Debug.WriteLine ("Activating Task: " + Name);
-			state = TaskState.Active;
-			taskSeries.Task.Completed = DateTime.MinValue;
 			rtmBackend.UpdateTaskActive (this);
+			base.OnActivate ();
 		}
 		
-		/// <summary>
-		/// Sets the task to be inactive
-		/// </summary>
-		public override void Inactivate ()
-		{
-			Debug.WriteLine ("Inactivating Task: " + Name);		
-			state = TaskState.Inactive;
-			taskSeries.Task.Completed = DateTime.Now;
-			rtmBackend.UpdateTaskInactive (this);
-		}
-		
-		/// <summary>
-		/// Completes the task
-		/// </summary>
-		public override void Complete ()
+		protected override void OnComplete ()
 		{
-			Debug.WriteLine ("Completing Task: " + Name);			
-			state = TaskState.Completed;
-			if (taskSeries.Task.Completed == DateTime.MinValue)
-				taskSeries.Task.Completed = DateTime.Now;
 			rtmBackend.UpdateTaskCompleted (this);
+			base.OnComplete ();
 		}
 		
-		/// <summary>
-		/// Deletes the task
-		/// </summary>
-		public override void Delete ()
+		protected override void OnDelete ()
 		{
-			state = TaskState.Deleted;
 			rtmBackend.UpdateTaskDeleted (this);
+			base.OnDelete ();
 		}
 		
-		/// <summary>
-		/// Adds a note to a task
-		/// </summary>
-		/// <param name="note">
-		/// A <see cref="INote"/>
-		/// </param>
 		public override TaskNote CreateNote (string text)
 		{
-			RtmNote rtmNote;
-			
-			rtmNote = rtmBackend.CreateNote (this, text);
-			notes.Add (rtmNote);
-			
-			return rtmNote;
+			return rtmBackend.CreateNote (this, text);
 		}
 		
-		/// <summary>
-		/// Deletes a note from a task
-		/// </summary>
-		/// <param name="note">
-		/// A <see cref="INote"/>
-		/// </param>
-		public override void DeleteNote (TaskNote note)
+		protected override void OnRemoveNote (TaskNote note)
 		{
-			RtmNote rtmNote = (note as RtmNote);
-			
-			foreach (RtmNote lRtmNote in notes) {
-				if (lRtmNote.ID == rtmNote.ID) {
-					notes.Remove (lRtmNote);
-					break;
-				}
-			}
-			rtmBackend.DeleteNote (this, rtmNote);
-		}		
-
-		/// <summary>
-		/// Deletes a note from a task
-		/// </summary>
-		/// <param name="note">
-		/// A <see cref="INote"/>
-		/// </param>
-		public override void SaveNote (TaskNote note)
-		{		
-			rtmBackend.SaveNote (this, (note as RtmNote));
-		}		
-
-		#endregion // Public Methods
+			rtmBackend.DeleteNote (this, note as RtmNote);
+			base.OnRemoveNote (note);
+		}
 	}
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]