[glabels/vala] Some cleanup.



commit 80d6974695080cc0b52fe17878bc7f829bb5d3bc
Author: Jim Evins <evins snaught com>
Date:   Tue Sep 25 22:32:58 2012 -0400

    Some cleanup.
    
    - Don't mix Gee and GLib HashMaps.  Moved to Gee implementations only.
    - Do proper sorting on names in BarcodeMenu
    - Removed unneeded code.

 configure.ac                  |    1 +
 glabels/barcode_backends.vala |   93 +++++++++++++++++++----------------------
 glabels/barcode_menu.vala     |   42 ++++++++----------
 libglbarcode/Makefile.am      |    1 +
 libglbarcode/factory.vala     |   30 +++----------
 5 files changed, 71 insertions(+), 96 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 6d3e1b1..f8a56c4 100644
--- a/configure.ac
+++ b/configure.ac
@@ -105,6 +105,7 @@ AC_SUBST(LIBGLABELS_LIBS)
 PKG_CHECK_MODULES(LIBGLBARCODE, [\
                                 glib-2.0 >= $GLIB_REQUIRED \
                                 gobject-2.0 >= $GLIB_REQUIRED \
+                                gee-1.0 >= $GEE_REQUIRED \
                                 cairo >= $CAIRO_REQUIRED \
                                 pangocairo >= $PANGOCAIRO_REQUIRED \
 ])
diff --git a/glabels/barcode_backends.vala b/glabels/barcode_backends.vala
index 28f49dd..d0d5367 100644
--- a/glabels/barcode_backends.vala
+++ b/glabels/barcode_backends.vala
@@ -31,20 +31,22 @@ namespace glabels
 		private const string DEFAULT_NAME = _("Code 39");
 
 
-		private static HashTable<string,string> backend_map;
+		private static Gee.HashMap<string,string> backend_id_map;
+		private static Gee.HashMap<string,string> backend_name_map;
 
-		private static HashTable<string,BarcodeStyle> id_map;
-		private static HashTable<string,BarcodeStyle> name_map;
+		private static Gee.HashMap<string,BarcodeStyle> id_map;
+		private static Gee.HashMap<string,BarcodeStyle> name_map;
 
 		private static bool initialized = false;
 
 
 		static construct
 		{
-			backend_map = new HashTable<string,string>( str_hash, str_equal );
+			backend_id_map   = new Gee.HashMap<string,string>();
+			backend_name_map = new Gee.HashMap<string,string>();
 
-			id_map   = new HashTable<string,BarcodeStyle>( str_hash, str_equal );
-			name_map = new HashTable<string,BarcodeStyle>( str_hash, str_equal );
+			id_map   = new Gee.HashMap<string,BarcodeStyle>();
+			name_map = new Gee.HashMap<string,BarcodeStyle>();
 
 			register_style( "POSTNET", "", _("POSTNET (any)"),
 			                false, false, true, false, "12345-6789-12", false, 11 );
@@ -428,7 +430,8 @@ namespace glabels
 
 		private static void register_backend( string id, string name )
 		{
-			backend_map.insert( id, name );
+			backend_id_map.set( id, name );
+			backend_name_map.set( name, id );
 		}
 
 
@@ -447,16 +450,16 @@ namespace glabels
 			                                       can_text, text_optional, can_checksum, checksum_optional,
 			                                       default_digits, can_freeform, prefered_n );
 
-			id_map.insert( id, style );
-			name_map.insert( name, style );
+			id_map.set( id, style );
+			name_map.set( "%s.%s".printf(backend_id, name), style ); /* Name may not be unique w/o backend */
 		}
 
 
 		public static string backend_id_to_name( string id )
 		{
-			if ( backend_map.contains( id ) )
+			if ( backend_id_map.has_key( id ) )
 			{
-				return backend_map.lookup( id );
+				return backend_id_map.get( id );
 			}
 			else
 			{
@@ -465,82 +468,72 @@ namespace glabels
 		}
 
 
-		public static BarcodeStyle? lookup_style_from_name( string name )
+		public static string backend_name_to_id( string name )
 		{
-			if ( name_map.contains( name ) )
+			if ( backend_name_map.has_key( name ) )
 			{
-				return name_map.lookup( name );
+				return backend_name_map.get( name );
 			}
 			else
 			{
-				return id_map.lookup( DEFAULT_ID );
+				return ""; /* Built-in backend. */
 			}
 		}
 
 
-		public static BarcodeStyle? lookup_style_from_id( string id )
+		public static List<string> get_backend_name_list()
 		{
-			if ( id_map.contains( id ) )
-			{
-				return id_map.lookup( id );
-			}
-			else
+			List<string> list = null;
+
+			foreach ( string backend_name in backend_id_map.values )
 			{
-				return id_map.lookup( DEFAULT_ID );
+				list.insert_sorted( backend_name, strcmp );
 			}
+
+			return list;
 		}
 
 
-		public static string id_to_name( string id )
+		public static BarcodeStyle? lookup_style_from_id( string id )
 		{
-			BarcodeStyle? style = lookup_style_from_id( id );
-
-			if ( style != null )
+			if ( id_map.has_key( id ) )
 			{
-				return style.name;
+				return id_map.get( id );
 			}
 			else
 			{
-				return DEFAULT_NAME;
+				return id_map.get( DEFAULT_ID );
 			}
 		}
 
 
-		public static string name_to_id( string name )
+		public static BarcodeStyle? lookup_style_from_name( string backend_id, string name )
 		{
-			BarcodeStyle? style = lookup_style_from_name( name );
+			string query_string = "%s.%s".printf(backend_id, name);
 
-			if ( style != null )
+			if ( name_map.has_key( query_string ) )
 			{
-				return style.id;
+				return name_map.get( query_string );
 			}
 			else
 			{
-				return DEFAULT_ID;
+				return id_map.get( DEFAULT_ID );
 			}
 		}
 
 
-		public static List<weak string> get_name_list()
+		public static List<string> get_name_list( string backend_id )
 		{
-			List<weak string> list = name_map.get_keys();
-			list.sort( strcmp );
-			return list;
-		}
-
-
-		public static List<weak string> get_id_list()
-		{
-			List<weak string> list = id_map.get_keys();
-			list.sort( strcmp );
-			return list;
-		}
+			List<string> list = null;
 
+			foreach ( BarcodeStyle bc_style in id_map.values )
+			{
+				if ( bc_style.backend_id == backend_id )
+				{
+					list.insert_sorted( bc_style.name, strcmp );
+				}
+			}
 
-		public static List<weak string> get_backend_id_list()
-		{
-			List<weak string> list = backend_map.get_keys();
-			list.sort( strcmp );
 			return list;
 		}
 
diff --git a/glabels/barcode_menu.vala b/glabels/barcode_menu.vala
index fe04251..80a673f 100644
--- a/glabels/barcode_menu.vala
+++ b/glabels/barcode_menu.vala
@@ -35,28 +35,26 @@ namespace glabels
 
 		public BarcodeMenu()
 		{
-			List<weak string> id_list = BarcodeBackends.get_id_list();
-
-			foreach ( string id in id_list )
+			/* Built-in styles */
+			foreach ( string name in BarcodeBackends.get_name_list( "" ) )
 			{
-				BarcodeStyle bc_style = BarcodeBackends.lookup_style_from_id( id );
-				if ( bc_style.backend_id == "" )
-				{
-					BarcodeMenuItem bc_menu_item = new BarcodeMenuItem( bc_style );
-					bc_menu_item.show();
-					bc_menu_item.activate.connect( on_menu_item_activated );
+				BarcodeStyle bc_style = BarcodeBackends.lookup_style_from_name( "", name );
 
-					append( bc_menu_item );
-				}
+				BarcodeMenuItem bc_menu_item = new BarcodeMenuItem( bc_style );
+				bc_menu_item.show();
+				bc_menu_item.activate.connect( on_menu_item_activated );
+
+				append( bc_menu_item );
 			}
 
 			Gtk.MenuItem separator_item = new Gtk.SeparatorMenuItem();
 			separator_item.show();
 			append( separator_item );
 
-			foreach ( string backend_id in BarcodeBackends.get_backend_id_list() )
+			/* Create a submenu for each non-built-in backend. */
+			foreach ( string backend_name in BarcodeBackends.get_backend_name_list() )
 			{
-				string backend_name = BarcodeBackends.backend_id_to_name( backend_id );
+				string backend_id = BarcodeBackends.backend_name_to_id( backend_name );
 				Gtk.MenuItem menu_item = new Gtk.MenuItem.with_label( backend_name );
 				menu_item.show();
 				append( menu_item );
@@ -64,17 +62,15 @@ namespace glabels
 				Gtk.Menu sub_menu = new Gtk.Menu();
 				menu_item.set_submenu( sub_menu );
 
-				foreach ( string id in id_list )
+				foreach ( string name in BarcodeBackends.get_name_list( backend_id ) )
 				{
-					BarcodeStyle bc_style = BarcodeBackends.lookup_style_from_id( id );
-					if ( bc_style.backend_id == backend_id )
-					{
-						BarcodeMenuItem bc_menu_item = new BarcodeMenuItem( bc_style );
-						bc_menu_item.show();
-						bc_menu_item.activate.connect( on_menu_item_activated );
-
-						sub_menu.append( bc_menu_item );
-					}
+					BarcodeStyle bc_style = BarcodeBackends.lookup_style_from_name( backend_id, name );
+
+					BarcodeMenuItem bc_menu_item = new BarcodeMenuItem( bc_style );
+					bc_menu_item.show();
+					bc_menu_item.activate.connect( on_menu_item_activated );
+
+					sub_menu.append( bc_menu_item );
 				}
 
 			}
diff --git a/libglbarcode/Makefile.am b/libglbarcode/Makefile.am
index 13e7362..b76f8fe 100644
--- a/libglbarcode/Makefile.am
+++ b/libglbarcode/Makefile.am
@@ -29,6 +29,7 @@ VALAFLAGS = \
 	--pkg config \
 	--pkg posix \
 	--pkg gobject-2.0 \
+	--pkg gee-1.0 \
 	--pkg cairo \
 	--pkg pangocairo \
 	--header=libglbarcode-4.h \
diff --git a/libglbarcode/factory.vala b/libglbarcode/factory.vala
index 9c7307b..6780055 100644
--- a/libglbarcode/factory.vala
+++ b/libglbarcode/factory.vala
@@ -24,36 +24,18 @@ using GLib;
 namespace glbarcode
 {
 
-	private uint str_case_hash( string s )
-	{
-		uint h = 5381;
-
-		for ( int i = 0; i < s.length; i++ )
-		{
-			h = (h << 5) + h + s[i].tolower();
-		}
-		return h;
-	}
-
-
-	private bool str_case_equal( string a, string b )
-	{
-		return a.ascii_casecmp( b ) == 0;
-	}
-
-
 	/**
 	 * Barcode factory.
 	 */
 	public class Factory
 	{
 		private static bool initialized = false;
-		private static HashTable<string,Type> mappings;
+		private static Gee.HashMap<string,Type> mappings;
 
 
 		static construct
 		{
-			mappings = new HashTable<string, Type>( str_case_hash, str_case_equal );
+			mappings = new Gee.HashMap<string, Type>();
 
 			/* Register built-in types. */
 			register_type( "Code39",          typeof(BarcodeCode39) );
@@ -100,7 +82,7 @@ namespace glbarcode
 		{
 			if ( type.is_a( typeof(Barcode) ) )
 			{
-				mappings.insert( name, type );
+				mappings.set( name.casefold(), type );
 			}
 			else
 			{
@@ -128,10 +110,12 @@ namespace glbarcode
 		{
 			Barcode? barcode = null;
 
-			Type type = mappings.lookup( name );
+			string name_casefold = name.casefold();
 
-			if ( mappings.contains( name ) )
+			if ( mappings.has_key( name_casefold ) )
 			{
+				Type type = mappings.get( name_casefold );
+
 				barcode = Object.new( type,
 				                      text_flag     : text_flag,
 				                      checksum_flag : checksum_flag,



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