tasque r205 - in branches/new_cache: . src



Author: bgmerrell
Date: Sun Jan  4 07:55:31 2009
New Revision: 205
URL: http://svn.gnome.org/viewvc/tasque?rev=205&view=rev

Log:
* LocalCache.cs: Add property (needs renamed) to get a dictionary of
the tasks in the local database (much like the dictionary received
from the backend). Fix some indentation.
* SyncManager.cs: Add logic to (1) send local tasks to the remote
backend if they don't exist at the remote backend and (2) update the
local task that was sent to the remote backend with an external ID (so
it won't be sent to the remote backend again.  Currently crashing when
calling the backend's CreateTask method.  Needs work.
* Task.cs: Add ExternalId property (with a getter and setter) that can
get the external ID in the local database or set it.


Modified:
   branches/new_cache/ChangeLog
   branches/new_cache/src/LocalCache.cs
   branches/new_cache/src/SyncManager.cs
   branches/new_cache/src/Task.cs

Modified: branches/new_cache/src/LocalCache.cs
==============================================================================
--- branches/new_cache/src/LocalCache.cs	(original)
+++ branches/new_cache/src/LocalCache.cs	Sun Jan  4 07:55:31 2009
@@ -13,6 +13,7 @@
 	public class LocalCache
 	{
 		private Dictionary<int, Gtk.TreeIter> taskIters;
+		private Dictionary<string, Task> tasks;
 		private Gtk.TreeStore taskStore;
 		private Gtk.TreeModelSort sortedTasksModel;
 		private bool initialized;
@@ -72,6 +73,24 @@
 		{
 			get { return sortedTasksModel; }
 		}
+
+		public Dictionary<string, Task> TasksNew
+		{
+			get {
+				tasks = new Dictionary<string, Task>();
+				Task newTask;
+				string command = "SELECT id FROM Tasks";
+				SqliteCommand cmd = db.Connection.CreateCommand();
+				cmd.CommandText = command;
+				SqliteDataReader dataReader = cmd.ExecuteReader();
+				while(dataReader.Read()) {
+					int id = dataReader.GetInt32(0);
+					newTask = new Task(this, id);
+					tasks.Add(newTask.Id.ToString(), newTask);	
+				}
+				return tasks;
+			}
+		}
 		
 		/// <value>
 		/// This returns all the task lists (categories) that exist.
@@ -252,11 +271,11 @@
 			bool hasValues = false;
 			
 			string command = "SELECT id FROM Categories";
-        	SqliteCommand cmd = db.Connection.CreateCommand();
-        	cmd.CommandText = command;
-        	SqliteDataReader dataReader = cmd.ExecuteReader();
-        	while(dataReader.Read()) {
-			    int id = dataReader.GetInt32(0);
+			SqliteCommand cmd = db.Connection.CreateCommand();
+			cmd.CommandText = command;
+			SqliteDataReader dataReader = cmd.ExecuteReader();
+			while(dataReader.Read()) {
+				int id = dataReader.GetInt32(0);
 				hasValues = true;
 				
 				newCategory = new Category (this, id);
@@ -264,10 +283,10 @@
 					defaultCategory = newCategory;
 				iter = categoryListStore.Append ();
 				categoryListStore.SetValue (iter, 0, newCategory);				
-        	}
+			}
 
-        	dataReader.Close();
-        	cmd.Dispose();
+			dataReader.Close();
+			cmd.Dispose();
 
 			if(!hasValues)
 			{
@@ -294,8 +313,8 @@
 		public void RefreshTasks()
 		{
 			Gtk.TreeIter iter;
-        	Gtk.TreeIter parentIter;
-        	Task newTask;
+			Gtk.TreeIter parentIter;
+			Task newTask;
 			bool hasValues = false;
 
 			overdueIter = taskStore.AppendNode();
@@ -317,11 +336,11 @@
 			taskStore.SetValue(completedTaskIter, 0, new TaskModelNode(Catalog.GetString("Completed")));
 
 			string command = "SELECT id FROM Tasks";
-        	SqliteCommand cmd = db.Connection.CreateCommand();
-        	cmd.CommandText = command;
-        	SqliteDataReader dataReader = cmd.ExecuteReader();
-        	while(dataReader.Read()) {
-			    int id = dataReader.GetInt32(0);
+			SqliteCommand cmd = db.Connection.CreateCommand();
+			cmd.CommandText = command;
+			SqliteDataReader dataReader = cmd.ExecuteReader();
+			while(dataReader.Read()) {
+				int id = dataReader.GetInt32(0);
 				hasValues = true;
 				
 				newTask = new Task(this, id);
@@ -329,10 +348,10 @@
 				iter = taskStore.AppendNode(parentIter);
 				taskStore.SetValue (iter, 0, new TaskModelNode(newTask));
 				taskIters [newTask.Id] = iter;				
-        	}
+        		}
 
-        	dataReader.Close();
-        	cmd.Dispose();
+			dataReader.Close();
+			cmd.Dispose();
 
 			if(!hasValues)
 			{

Modified: branches/new_cache/src/SyncManager.cs
==============================================================================
--- branches/new_cache/src/SyncManager.cs	(original)
+++ branches/new_cache/src/SyncManager.cs	Sun Jan  4 07:55:31 2009
@@ -45,6 +45,10 @@
 			syncThread.Abort();
 		}		
 
+		private void Pause(int timeout)
+		{
+			Thread.Sleep(timeout);
+		}
 		
 		private void SyncThreadLoop()
 		{
@@ -62,7 +66,8 @@
 					(Application.Backend.Configured) &&
 					(Application.Backend.Initialized) )
 				{
-					Dictionary<string, ITask> tasks;
+					Dictionary<string, ITask> remoteTasks;
+					Dictionary<string, Task> localTasks;
 					Dictionary<string, ICategory> categories;
 					
 					// Refresh the tasks
@@ -77,18 +82,58 @@
 					}
 					
 					// Read Tasks and populate them into the localCache
-					tasks = Application.Backend.Tasks;
-					Logger.Debug("Populating local cache with tasks.");
-					foreach(ITask task in tasks.Values)
+					remoteTasks = Application.Backend.Tasks;
+					Logger.Debug("Populating local cache with remote tasks.");
+					foreach(ITask task in remoteTasks.Values)
 					{
 						CreateLocalTask(task);
 					}
+
+					// "Upload" any local tasks that haven't been
+					// uploaded
+					// localTasks = Application.LocalCache.Tasks;
+					Logger.Debug("Sending local tasks to remote backend");
+					foreach(Task task in Application.LocalCache.TasksNew.Values)
+					{
+						CreateRemoteTask(task);
+					}
+
 				}
 			
 				Logger.Debug("SyncThreadLoop done!");
 			}
 		}
 
+		// Check each local task for an external ID, if the external ID does not exist
+		// send that task to the remote backend
+		private void CreateRemoteTask(Task task)
+		{
+			Logger.Debug("Creating Remote Task:");
+			Logger.Debug("Name: {0}", task.Name);
+			Logger.Debug("DueDate: {0}", task.DueDate);
+			Logger.Debug("CompletionDate: {0}", task.CompletionDate);
+			Logger.Debug("Priority: {0}", task.Priority);
+			Logger.Debug("State: {0}", task.State);
+			Logger.Debug("Category: {0}", task.Category);
+			Logger.Debug("ID: {0}", task.Id);
+			Logger.Debug("External ID: {0}", task.ExternalId);
+			if (task.ExternalId != "" && Application.LocalCache.Database.ExternalTaskExists(task.ExternalId)) {
+				Logger.Debug("Remote task not created");
+				Logger.Debug("Task already has an external ID ({0})", task.ExternalId);
+				return;
+			} 
+			// XXX: use a more intelligent timeout.  also, i think a good system would be
+			// to add a timeout time to the backend interface so each backend has a 
+			// unique timeout time.  maybe allow this to be overridden by the user?
+			this.Pause(1000);
+			Logger.Debug("Creating remote task...");
+			ITask remoteTask = Application.Backend.CreateTask(task.Name, task.Category as ICategory);
+			task.ExternalId = remoteTask.Id;
+		}
+
+		// Get the external ID from each task (from the backend).  If there is already a task
+		// in the local database with the retrieved external ID, ignore the external task.  
+		// Otherwise, add the external task to the local database.
 		private void CreateLocalTask(ITask task)
 		{
 			Logger.Debug("Creating Local Task:");
@@ -100,7 +145,7 @@
 			Logger.Debug("Category: {0}", task.Category);
 			Logger.Debug("External ID: {0}", task.Id);
 			if (Application.LocalCache.Database.ExternalTaskExists(task.Id)) {
-				Logger.Debug("Task not added.");
+				Logger.Debug("Local task not created.");
 				Logger.Debug("External ID {0} already exists", task.Id);
 				return;
 			}

Modified: branches/new_cache/src/Task.cs
==============================================================================
--- branches/new_cache/src/Task.cs	(original)
+++ branches/new_cache/src/Task.cs	Sun Jan  4 07:55:31 2009
@@ -47,6 +47,20 @@
 			get { return id; }
 			set { id = value; }
 		}
+
+		public string ExternalId
+		{
+			get {
+				string command = String.Format("SELECT ExternalID FROM Tasks where ID='{0}'", id);
+				return cache.Database.GetSingleString(command);
+			}
+
+			set {
+				string command = String.Format("UPDATE Tasks set ExternalID='{0}' where ID='{1}'", value, id);
+				cache.Database.ExecuteScalar(command);
+				cache.UpdateTask(this);
+			}
+		}
 		
 		public string Name
 		{



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