[tasque/transition: 186/213] [libtasque] Category: Make class abstract; Resolve DefaultCategory mess



commit a2e9aae08d22466381cd843939d4b798ad751c56
Author: Antonius Riha <antoniusriha gmail com>
Date:   Sun Aug 19 10:35:39 2012 +0200

    [libtasque] Category: Make class abstract; Resolve DefaultCategory mess
    
    libtasque:
    * Category.cs: Child classes rely on getting only their flavor of Category.
    Hence make category abstract and ensure that only instances of child
    categories are around. This also means the ctor needs not be public.
    * Backend.cs: Does not create a default category in ctor anymore. Declares
    that member as abstract and lets the child class handle it. Provides
    guidelines for implementation.
    ---------------------
    DummyBackend:
    The backend allows interaction with DefaultCategory member only after
    initialization. WorkCategory is declared as DefaultCategory;
    ---------------------
    SQLiteBackend:
    Same as DummyBackend
    Additionally: Changed namespace of pref widget class.

 src/Addins/DummyBackend/DummyBackend.cs           |   26 ++++++++++++++--
 src/Addins/DummyBackend/DummyBackend.csproj       |    1 +
 src/Addins/DummyBackend/DummyCategory.cs          |   34 +++++++++++++++++++++
 src/Addins/SqliteBackend/Gtk/SqlitePreferences.cs |    2 +-
 src/Addins/SqliteBackend/SqliteBackend.cs         |   30 ++++++++++++++----
 src/libtasque/Backend.cs                          |   21 ++-----------
 src/libtasque/Category.cs                         |    5 ++-
 7 files changed, 88 insertions(+), 31 deletions(-)
---
diff --git a/src/Addins/DummyBackend/DummyBackend.cs b/src/Addins/DummyBackend/DummyBackend.cs
index 4708e25..7def4f3 100644
--- a/src/Addins/DummyBackend/DummyBackend.cs
+++ b/src/Addins/DummyBackend/DummyBackend.cs
@@ -38,6 +38,23 @@ namespace Tasque.Backends.Dummy
 			Configured = true;
 		}
 		
+		public override Category DefaultCategory {
+			get {
+				if (!Initialized)
+					throw new InvalidOperationException ("Backend not initialized");
+				return defaultCategory;
+			}
+			set {
+				if (!Initialized)
+					throw new InvalidOperationException ("Backend not initialized");
+				if (value == null)
+					throw new ArgumentNullException ("value");
+				if (!Categories.Contains (value))
+					throw new ArgumentException ("Default category must be one of Categories.");
+				defaultCategory = value;
+			}
+		}
+		
 		#region Public Methods		
 		public override void Refresh () {}
 		
@@ -46,13 +63,14 @@ namespace Tasque.Backends.Dummy
 			//
 			// Add in some fake categories
 			//
-			var homeCategory = new Category ("Home");
+			var homeCategory = new DummyCategory ("Home");
 			Categories.Add (homeCategory);
 			
-			var workCategory = new Category ("Work");
+			var workCategory = new DummyCategory ("Work");
 			Categories.Add (workCategory);
+			defaultCategory = workCategory;
 			
-			var projectsCategory = new Category ("Projects");
+			var projectsCategory = new DummyCategory ("Projects");
 			Categories.Add (projectsCategory);
 			
 			//
@@ -122,5 +140,7 @@ namespace Tasque.Backends.Dummy
 		{
 			return new DummyTask (taskName);
 		}
+		
+		Category defaultCategory;
 	}
 }
diff --git a/src/Addins/DummyBackend/DummyBackend.csproj b/src/Addins/DummyBackend/DummyBackend.csproj
index 6249812..639f5b3 100644
--- a/src/Addins/DummyBackend/DummyBackend.csproj
+++ b/src/Addins/DummyBackend/DummyBackend.csproj
@@ -50,6 +50,7 @@
       <Link>Properties\GlobalDefines.cs</Link>
     </Compile>
     <Compile Include="DummyNote.cs" />
+    <Compile Include="DummyCategory.cs" />
   </ItemGroup>
   <ItemGroup>
     <Folder Include="Properties\" />
diff --git a/src/Addins/DummyBackend/DummyCategory.cs b/src/Addins/DummyBackend/DummyCategory.cs
new file mode 100644
index 0000000..8160cb4
--- /dev/null
+++ b/src/Addins/DummyBackend/DummyCategory.cs
@@ -0,0 +1,34 @@
+// 
+// DummyCategory.cs
+//  
+// Author:
+//       Antonius Riha <antoniusriha gmail com>
+// 
+// Copyright (c) 2012 Antonius Riha
+// 
+// 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.
+using System;
+
+namespace Tasque.Backends.Dummy
+{
+	public class DummyCategory : Category
+	{
+		public DummyCategory (string name) : base (name) {}
+	}
+}
diff --git a/src/Addins/SqliteBackend/Gtk/SqlitePreferences.cs b/src/Addins/SqliteBackend/Gtk/SqlitePreferences.cs
index 0e3bcb8..bb0162f 100644
--- a/src/Addins/SqliteBackend/Gtk/SqlitePreferences.cs
+++ b/src/Addins/SqliteBackend/Gtk/SqlitePreferences.cs
@@ -27,7 +27,7 @@ using Mono.Unix;
 using Gtk;
 using Tasque;
 
-namespace Tasque.Backends.SqliteBackend.Gtk
+namespace Tasque.Backends.SqliteBackend
 {
 	public class SqlitePreferences : Box, IBackendPreferences
 	{
diff --git a/src/Addins/SqliteBackend/SqliteBackend.cs b/src/Addins/SqliteBackend/SqliteBackend.cs
index 480e738..e1d1e68 100644
--- a/src/Addins/SqliteBackend/SqliteBackend.cs
+++ b/src/Addins/SqliteBackend/SqliteBackend.cs
@@ -5,6 +5,7 @@ using System.Collections.Generic;
 using System.Linq;
 using Mono.Data.Sqlite;
 using Mono.Unix;
+using Tasque.Backends.SqliteBackend;
 
 namespace Tasque.Backends.Sqlite
 {
@@ -17,6 +18,23 @@ namespace Tasque.Backends.Sqlite
 		
 		public Database Database { get; private set; }
 		
+		public override Category DefaultCategory {
+			get {
+				if (!Initialized)
+					throw new InvalidOperationException ("Backend not initialized");
+				return defaultCategory;
+			}
+			set {
+				if (!Initialized)
+					throw new InvalidOperationException ("Backend not initialized");
+				if (value == null)
+					throw new ArgumentNullException ("value");
+				if (!Categories.Contains (value))
+					throw new ArgumentException ("Default category must be one of Categories.");
+				defaultCategory = value;
+			}
+		}
+		
 		protected override Task CreateTaskCore (string taskName, IEnumerable<Category> categories)
 		{
 			var index = 0;
@@ -72,7 +90,6 @@ namespace Tasque.Backends.Sqlite
 		
 		public void RefreshCategories ()
 		{
-			SqliteCategory defaultCategory = null;
 			SqliteCategory newCategory;
 			var hasValues = false;
 			
@@ -97,8 +114,9 @@ namespace Tasque.Backends.Sqlite
 			cmd.Dispose ();
 
 			if (!hasValues) {
-				defaultCategory = new SqliteCategory (this, Catalog.GetString ("Work"));
-				Categories.Add (defaultCategory);
+				newCategory = new SqliteCategory (this, Catalog.GetString ("Work"));
+				Categories.Add (newCategory);
+				defaultCategory = newCategory;
 
 				newCategory = new SqliteCategory (this, Catalog.GetString ("Personal"));
 				Categories.Add (newCategory);
@@ -109,10 +127,6 @@ namespace Tasque.Backends.Sqlite
 				newCategory = new SqliteCategory (this, Catalog.GetString ("Project"));
 				Categories.Add (newCategory);		
 			}
-			
-			// remove fake default category
-			Categories.Remove (DefaultCategory);
-			DefaultCategory = defaultCategory;
 		}
 
 		public void RefreshTasks ()
@@ -146,5 +160,7 @@ namespace Tasque.Backends.Sqlite
 			if (!hasValues)
 				CreateTask (Catalog.GetString ("Create some tasks"), DefaultCategory);
 		}
+		
+		Category defaultCategory;
 	}
 }
diff --git a/src/libtasque/Backend.cs b/src/libtasque/Backend.cs
index ab77068..f4acebd 100644
--- a/src/libtasque/Backend.cs
+++ b/src/libtasque/Backend.cs
@@ -50,12 +50,6 @@ namespace Tasque
 			categoriesChangedSources = new List<INotifyCollectionChanged> ();
 			Categories = new SortedNotifyCollection<Category> ();
 			
-			// create default category here, because it is required for the model to be there. Overwrite
-			// default category preferably in child class constructor with more appropriate value
-			var defaultCategory = new Category ("Default");
-			Categories.Add (defaultCategory);
-			DefaultCategory = defaultCategory;
-			
 			Categories.CollectionChanged += HandleCategoriesChanged;
 		}
 
@@ -72,16 +66,8 @@ namespace Tasque
 		/// </value>
 		public bool Configured { get; protected set; }
 		
-		public Category DefaultCategory {
-			get { return defaultCategory; }
-			set {
-				if (value == null)
-					throw new ArgumentNullException ("value");
-				if (!Categories.Contains (value))
-					throw new ArgumentException ("Value must be an element of Backend.Categories.", "value");
-				defaultCategory = value;
-			}
-		}
+		//DOCS: must not be null; must be entailed in Categories
+		public abstract Category DefaultCategory { get; set; }
 
 		/// <value>
 		/// Inidication that the backend is initialized
@@ -157,7 +143,7 @@ namespace Tasque
 			foreach (var item in categories) {
 				Category cat = item;
 				if (item == null)
-					cat = defaultCategory;
+					cat = DefaultCategory;
 
 				cat.Add (task);
 				isEmpty = false;
@@ -287,7 +273,6 @@ namespace Tasque
 		}
 
 		List<INotifyCollectionChanged> categoriesChangedSources;
-		Category defaultCategory;
 		SortedNotifyCollection<Task> tasks;
 		bool initialized;
 	}
diff --git a/src/libtasque/Category.cs b/src/libtasque/Category.cs
index b4a92f3..781fad7 100644
--- a/src/libtasque/Category.cs
+++ b/src/libtasque/Category.cs
@@ -28,9 +28,10 @@ using System.ComponentModel;
 
 namespace Tasque
 {
-	public class Category : SortedNotifyCollection<Task>, IComparable<Category>, INotifyPropertyChanged
+	public abstract class Category
+		: SortedNotifyCollection<Task>, IComparable<Category>, INotifyPropertyChanged
 	{
-		public Category (string name)
+		protected Category (string name)
 		{
 			Name = name;
 		}



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