gnome-games r8485 - trunk/libgames-support



Author: chpe
Date: Tue Jan  6 18:20:06 2009
New Revision: 8485
URL: http://svn.gnome.org/viewvc/gnome-games?rev=8485&view=rev

Log:
Change theme enumeration to be callback based.

Modified:
   trunk/libgames-support/games-card-theme-fixed.c
   trunk/libgames-support/games-card-theme-kde.c
   trunk/libgames-support/games-card-theme-private.h
   trunk/libgames-support/games-card-theme-pysol.c
   trunk/libgames-support/games-card-theme-sliced.c
   trunk/libgames-support/games-card-theme-svg.c
   trunk/libgames-support/games-card-theme.c

Modified: trunk/libgames-support/games-card-theme-fixed.c
==============================================================================
--- trunk/libgames-support/games-card-theme-fixed.c	(original)
+++ trunk/libgames-support/games-card-theme-fixed.c	Tue Jan  6 18:20:06 2009
@@ -325,15 +325,15 @@
   return info;
 }
 
-static void
-games_card_theme_fixed_class_get_theme_infos (GamesCardThemeClass *klass,
-                                              GList **list)
+static gboolean
+games_card_theme_fixed_class_foreach_theme_dir (GamesCardThemeClass *klass,
+                                                GamesCardThemeForeachFunc callback,
+                                                gpointer data)
 {
-  _games_card_theme_class_append_theme_info_foreach_env
-    (klass, "GAMES_CARD_THEME_PATH_FIXED", list);
+  if (!_games_card_theme_class_foreach_env (klass, "GAMES_CARD_THEME_PATH_FIXED", callback, data))
+    return FALSE;
 
-  _games_card_theme_class_append_theme_info_foreach
-    (klass, games_runtime_get_directory (GAMES_RUNTIME_PRERENDERED_CARDS_DIRECTORY), list);
+  return callback (klass, games_runtime_get_directory (GAMES_RUNTIME_PRERENDERED_CARDS_DIRECTORY), data);
 }
 
 static void
@@ -345,7 +345,7 @@
   gobject_class->finalize = games_card_theme_fixed_finalize;
 
   theme_class->get_theme_info = games_card_theme_fixed_class_get_theme_info;
-  theme_class->get_theme_infos = games_card_theme_fixed_class_get_theme_infos;
+  theme_class->foreach_theme_dir = games_card_theme_fixed_class_foreach_theme_dir;
 
   theme_class->load = games_card_theme_fixed_load;
   theme_class->set_card_size = games_card_theme_fixed_set_card_size;

Modified: trunk/libgames-support/games-card-theme-kde.c
==============================================================================
--- trunk/libgames-support/games-card-theme-kde.c	(original)
+++ trunk/libgames-support/games-card-theme-kde.c	Tue Jan  6 18:20:06 2009
@@ -295,16 +295,16 @@
 #endif /* HAVE_RSVG_BBOX */
 }
 
-static void
-games_card_theme_kde_class_get_theme_infos (GamesCardThemeClass *klass,
-                                            GList **list)
+static gboolean
+games_card_theme_kde_class_foreach_theme_dir (GamesCardThemeClass *klass,
+                                              GamesCardThemeForeachFunc callback,
+                                              gpointer data)
 {
-  _games_card_theme_class_append_theme_info_foreach_env
-    (klass, "GAMES_CARD_THEME_PATH_KDE", list);
+  if (!_games_card_theme_class_foreach_env (klass, "GAMES_CARD_THEME_PATH_KDE", callback, data))
+    return FALSE;
 
   /* FIXMEchpe: is this universal, or ubuntu specific? */
-  _games_card_theme_class_append_theme_info_foreach
-    (klass, "/usr/share/kde4/apps/carddecks", list);
+  return callback (klass, "/usr/share/kde4/apps/carddecks", data);
 }
 
 static void
@@ -317,7 +317,7 @@
   gobject_class->finalize = games_card_theme_kde_finalize;
 
   theme_class->get_theme_info = games_card_theme_kde_class_get_theme_info;
-  theme_class->get_theme_infos = games_card_theme_kde_class_get_theme_infos;
+  theme_class->foreach_theme_dir = games_card_theme_kde_class_foreach_theme_dir;
 
   theme_class->load = games_card_theme_kde_load;
   theme_class->get_card_aspect = games_card_theme_kde_get_card_aspect;

Modified: trunk/libgames-support/games-card-theme-private.h
==============================================================================
--- trunk/libgames-support/games-card-theme-private.h	(original)
+++ trunk/libgames-support/games-card-theme-private.h	Tue Jan  6 18:20:06 2009
@@ -44,6 +44,8 @@
                                                 gpointer data,
                                                 GDestroyNotify destroy_notify);
 
+guint _games_card_theme_info_hash  (GamesCardThemeInfo *a,
+                                    GamesCardThemeInfo *b);
 gboolean _games_card_theme_info_equal (GamesCardThemeInfo *a,
                                        GamesCardThemeInfo *b);
 
@@ -52,15 +54,21 @@
 
 /* GamesCardTheme */
 
+/* Return TRUE to continue, FALSE to abort */
+typedef gboolean (* GamesCardThemeForeachFunc) (GamesCardThemeClass *klass,
+                                                const char *path,
+                                                gpointer data);
+
 struct _GamesCardThemeClass {
   GObjectClass parent_class;
 
   /* class vfuncs */
-  void                 (* get_theme_infos)  (GamesCardThemeClass *klass,
-                                             GList **list /* inout */);
-  GamesCardThemeInfo * (* get_theme_info)   (GamesCardThemeClass *klass,
-                                             const char *dir,
-                                             const char *filename);
+  GamesCardThemeInfo * (* get_theme_info)     (GamesCardThemeClass *klass,
+                                               const char *dir,
+                                               const char *filename);
+  gboolean             (* foreach_theme_dir)  (GamesCardThemeClass *klass,
+                                               GamesCardThemeForeachFunc,
+                                               gpointer data);
 
   /* vfuncs */
   gboolean    (* load)              (GamesCardTheme *theme,
@@ -94,12 +102,13 @@
                                                             const char *dir,
                                                             const char *filename);
 
-void _games_card_theme_class_append_theme_info_foreach (GamesCardThemeClass *klass,
-                                                        const char *path,
-                                                        GList **list);
-void _games_card_theme_class_append_theme_info_foreach_env (GamesCardThemeClass *klass,
-                                                            const char *env,
-                                                            GList **list);
+gboolean _games_card_theme_class_foreach_theme_dir (GamesCardThemeClass *klass,
+                                                    GamesCardThemeForeachFunc callback,
+                                                    gpointer data);
+gboolean _games_card_theme_class_foreach_env (GamesCardThemeClass *klass,
+                                              const char *env,
+                                              GamesCardThemeForeachFunc callback,
+                                              gpointer data);
 
 void _games_card_theme_emit_changed (GamesCardTheme *theme);
 

Modified: trunk/libgames-support/games-card-theme-pysol.c
==============================================================================
--- trunk/libgames-support/games-card-theme-pysol.c	(original)
+++ trunk/libgames-support/games-card-theme-pysol.c	Tue Jan  6 18:20:06 2009
@@ -371,16 +371,16 @@
   return info;
 }
 
-static void
-games_card_theme_pysol_class_get_theme_infos (GamesCardThemeClass *klass,
-                                              GList **list)
+static gboolean
+games_card_theme_pysol_class_foreach_theme_dir (GamesCardThemeClass *klass,
+                                                GamesCardThemeForeachFunc callback,
+                                                gpointer data)
 {
-  _games_card_theme_class_append_theme_info_foreach_env
-    (klass, "GAMES_CARD_THEME_PATH_PYSOL", list);
+  if (!_games_card_theme_class_foreach_env (klass, "GAMES_CARD_THEME_PATH_PYSOL", callback, data))
+    return FALSE;
 
   /* FIXMEchpe: is this univeral or ubuntu specific? */
-  _games_card_theme_class_append_theme_info_foreach
-    (klass, "/usr/share/games/pysol", list);
+  return callback (klass, "/usr/share/games/pysol", data);
 }
 
 static void
@@ -389,7 +389,7 @@
   GamesCardThemeClass *theme_class = GAMES_CARD_THEME_CLASS (klass);
 
   theme_class->get_theme_info = games_card_theme_pysol_class_get_theme_info;
-  theme_class->get_theme_infos = games_card_theme_pysol_class_get_theme_infos;
+  theme_class->foreach_theme_dir = games_card_theme_pysol_class_foreach_theme_dir;
 
   theme_class->load = games_card_theme_pysol_load;
   theme_class->set_card_size = games_card_theme_pysol_set_card_size;

Modified: trunk/libgames-support/games-card-theme-sliced.c
==============================================================================
--- trunk/libgames-support/games-card-theme-sliced.c	(original)
+++ trunk/libgames-support/games-card-theme-sliced.c	Tue Jan  6 18:20:06 2009
@@ -209,20 +209,24 @@
   return info;
 }
 
-static void
-games_card_theme_sliced_class_get_theme_infos (GamesCardThemeClass *klass,
-                                               GList **list)
+static gboolean
+games_card_theme_sliced_class_foreach_theme_dir (GamesCardThemeClass *klass,
+                                                 GamesCardThemeForeachFunc callback,
+                                                 gpointer data)
 {
   char *dir;
+  gboolean retval;
 
-  _games_card_theme_class_append_theme_info_foreach_env
-    (klass, "GAMES_CARD_THEME_PATH_SLICED", list);
+  if (!_games_card_theme_class_foreach_env (klass, "GAMES_CARD_THEME_PATH_SLICED", callback, data))
+    return FALSE;
 
   /* Themes in the pre-2.19 theme format: $(datadir)/pixmaps/gnome-games-common/cards */
   dir = g_build_filename (games_runtime_get_directory (GAMES_RUNTIME_DATA_DIRECTORY),
                           "pixmaps", "gnome-games-common", "cards", NULL);
-  _games_card_theme_class_append_theme_info_foreach (klass, dir, list);
+  retval = callback (klass, dir, data);
   g_free (dir);
+
+  return retval;
 }
 
 static void
@@ -235,7 +239,7 @@
   gobject_class->finalize = games_card_theme_sliced_finalize;
 
   theme_class->get_theme_info = games_card_theme_sliced_class_get_theme_info;
-  theme_class->get_theme_infos = games_card_theme_sliced_class_get_theme_infos;
+  theme_class->foreach_theme_dir = games_card_theme_sliced_class_foreach_theme_dir;
 
   theme_class->load = games_card_theme_sliced_load;
   theme_class->get_card_pixbuf = games_card_theme_sliced_get_card_pixbuf;

Modified: trunk/libgames-support/games-card-theme-svg.c
==============================================================================
--- trunk/libgames-support/games-card-theme-svg.c	(original)
+++ trunk/libgames-support/games-card-theme-svg.c	Tue Jan  6 18:20:06 2009
@@ -126,15 +126,15 @@
   return NULL;
 }
 
-static void
-games_card_theme_svg_class_get_theme_infos (GamesCardThemeClass *klass,
-                                            GList **list)
+static gboolean
+games_card_theme_svg_class_foreach_theme_dir (GamesCardThemeClass *klass,
+                                              GamesCardThemeForeachFunc callback,
+                                              gpointer data)
 {
-  _games_card_theme_class_append_theme_info_foreach_env
-    (klass, "GAMES_CARD_THEME_PATH_SVG", list);
+  if (!_games_card_theme_class_foreach_env (klass, "GAMES_CARD_THEME_PATH_SVG", callback, data))
+    return FALSE;
 
-  _games_card_theme_class_append_theme_info_foreach
-    (klass, games_runtime_get_directory (GAMES_RUNTIME_SCALABLE_CARDS_DIRECTORY), list);
+  return callback (klass, games_runtime_get_directory (GAMES_RUNTIME_SCALABLE_CARDS_DIRECTORY), data);
 }
 
 static void
@@ -144,7 +144,7 @@
   GamesCardThemePreimageClass *preimage_theme_class = GAMES_CARD_THEME_PREIMAGE_CLASS (klass);
 
   theme_class->get_theme_info = games_card_theme_svg_class_get_theme_info;
-  theme_class->get_theme_infos = games_card_theme_svg_class_get_theme_infos;
+  theme_class->foreach_theme_dir = games_card_theme_svg_class_foreach_theme_dir;
 
   theme_class->get_card_pixbuf = games_card_theme_svg_get_card_pixbuf;
 

Modified: trunk/libgames-support/games-card-theme.c
==============================================================================
--- trunk/libgames-support/games-card-theme.c	(original)
+++ trunk/libgames-support/games-card-theme.c	Tue Jan  6 18:20:06 2009
@@ -44,14 +44,45 @@
 };
 
 static guint signals[LAST_SIGNAL];
-static GList *theme_infos;
+static GHashTable *theme_infos;
+
+static gboolean
+_games_card_theme_class_get_theme_info_foreach (GamesCardThemeClass *klass,
+                                                const char *path,
+                                                gpointer data)
+{
+  GHashTable *hash_table = (GHashTable *) data;
+  GDir *iter;
+  const char *filename;
+
+  _games_profile_start ("looking for %s card themes in %s", G_OBJECT_CLASS_NAME (klass), path);
+
+  iter = g_dir_open (path, 0, NULL);
+  if (!iter)
+    goto out;
+
+  while ((filename = g_dir_read_name (iter)) != NULL) {
+    GamesCardThemeInfo *info;
+
+    _games_profile_start ("checking for %ss card theme in file %s", G_OBJECT_CLASS_NAME (klass), filename);
+    info = _games_card_theme_class_get_theme_info (klass, path, filename);
+    _games_profile_end ("checking for %ss card theme in file %s", G_OBJECT_CLASS_NAME (klass), filename);
+
+    if (info)
+      g_hash_table_insert (hash_table, info->pref_name, info);
+  }
+      
+  g_dir_close (iter);
+
+out:
+  _games_profile_end ("looking for %s card themes in %s", G_OBJECT_CLASS_NAME (klass), path);
+
+  return TRUE;
+}
 
 static void
 _games_card_theme_ensure_theme_infos (void)
 {
-  if (theme_infos)
-    return;
-
   /* FIXME: take env vars and prefs into account on ordering here */
   GType types[] = {
 #ifdef HAVE_RSVG
@@ -66,7 +97,13 @@
   };
   guint i;
 
-  theme_infos = NULL;
+  if (theme_infos)
+    return;
+
+  /* Hash table: pref name => theme info */
+  theme_infos = g_hash_table_new_full (g_str_hash, g_str_equal,
+                                       NULL /* key is owned by data */,
+                                       (GDestroyNotify) games_card_theme_info_unref);
 
   _games_profile_start ("looking for card themes");
 
@@ -78,41 +115,53 @@
       continue;
 
     _games_profile_start ("looking for %s card themes", G_OBJECT_CLASS_NAME (klass));
-    klass->get_theme_infos (klass, &theme_infos);
+    _games_card_theme_class_foreach_theme_dir (klass,
+                                               _games_card_theme_class_get_theme_info_foreach,
+                                               theme_infos);
     _games_profile_end ("looking for %s card themes", G_OBJECT_CLASS_NAME (klass));
 
     g_type_class_unref (klass);
   }
 
   _games_profile_end ("looking for card themes");
+}
 
-  theme_infos = g_list_reverse (theme_infos);
+typedef struct {
+  GType type;
+  const char *filename;
+  GamesCardThemeInfo *theme_info;
+} FindData;
+
+static void
+find_by_type_and_name (gpointer key,
+                       GamesCardThemeInfo *info,
+                       FindData *data)
+{
+  if (info->type != data->type ||
+      strcmp (info->filename, data->filename) != 0)
+    return;
+
+  data->theme_info = info;
 }
 
 static GamesCardThemeInfo *
 _games_card_theme_get_info_by_type_and_name (GType type,
                                              const char *filename)
 {
-  GList *l;
-  GamesCardThemeInfo *theme_info = NULL;
+  FindData data = { type, filename, NULL };
 
-  g_print ("_games_card_theme_get_info_by_type_and_name type %s filename %s\n", g_type_name (type), filename);
-
-  for (l = theme_infos; l != NULL; l = l->next) {
-    GamesCardThemeInfo *info = (GamesCardThemeInfo *) l->data;
-
-    g_print ("==> comparing with type %s filename %s\n", g_type_name (info->type), info->filename);
-    
-    if (info->type != type ||
-        strcmp (info->filename, filename) != 0)
-      continue;
+  g_assert (theme_infos != NULL);
+  g_hash_table_foreach (theme_infos, (GHFunc) find_by_type_and_name, &data);
 
-    g_print ("==> !! MATCH!!\n");
-    theme_info = info;
-    break;
-  }
+  return data.theme_info;
+}
 
-  return theme_info;
+static void
+foreach_add_to_list (gpointer key,
+                     gpointer data,
+                     GList **list)
+{
+  *list = g_list_prepend (*list, data);
 }
 
 /* Class implementation */
@@ -236,52 +285,32 @@
   return klass->get_theme_info (klass, dir, filename);
 }
 
-void
-_games_card_theme_class_append_theme_info_foreach (GamesCardThemeClass *klass,
-                                                   const char *path,
-                                                   GList **list)
+gboolean
+_games_card_theme_class_foreach_theme_dir (GamesCardThemeClass *klass,
+                                           GamesCardThemeForeachFunc callback,
+                                           gpointer data)
 {
-  GDir *iter;
-  const char *filename;
-
-  _games_profile_start ("looking for %s card themes in %s", G_OBJECT_CLASS_NAME (klass), path);
-
-  iter = g_dir_open (path, 0, NULL);
-  if (!iter)
-    goto out;
-
-  while ((filename = g_dir_read_name (iter)) != NULL) {
-    GamesCardThemeInfo *info;
-
-    _games_profile_start ("checking for %ss card theme in file %s", G_OBJECT_CLASS_NAME (klass), filename);
-    info = _games_card_theme_class_get_theme_info (klass, path, filename);
-    _games_profile_end ("checking for %ss card theme in file %s", G_OBJECT_CLASS_NAME (klass), filename);
-    if (info)
-      *list = g_list_prepend (*list, info);
-  }
-      
-  g_dir_close (iter);
-
-out:
-  _games_profile_end ("looking for %s card themes in %s", G_OBJECT_CLASS_NAME (klass), path);
+  return klass->foreach_theme_dir (klass, callback, data);
 }
 
-void
-_games_card_theme_class_append_theme_info_foreach_env (GamesCardThemeClass *klass,
-                                                       const char *env,
-                                                       GList **list)
+gboolean
+_games_card_theme_class_foreach_env (GamesCardThemeClass *klass,
+                                     const char *env,
+                                     GamesCardThemeForeachFunc callback,
+                                     gpointer data)
 {
   const char *value;
   char **paths;
   guint i;
+  gboolean retval = TRUE;
 
   value = g_getenv (env);
   if (!value || !value[0])
-    return;
+    return TRUE;
 
   paths = g_strsplit (value, ":", -1);
   if (!paths)
-    return;
+    return TRUE;
 
   for (i = 0; paths[i]; ++i) {
     const char *path = paths[i];
@@ -289,10 +318,14 @@
     if (!paths[0])
       continue;
 
-    _games_card_theme_class_append_theme_info_foreach (klass, path, list);
+    retval = callback (klass, path, data);
+    if (!retval)
+      break;
   }
 
   g_strfreev (paths);
+
+  return retval;
 }
 
 /* public API */
@@ -563,10 +596,11 @@
 GamesCardTheme *
 games_card_theme_get_any (void)
 {
-  GList *l;
+//   GList *l;
+
   _games_card_theme_ensure_theme_infos ();
 
-  if (!theme_infos)
+/*  if (!theme_infos)
     return NULL;
 
   for (l = theme_infos; l != NULL; l = l->next) {
@@ -577,7 +611,7 @@
     if (theme)
       return theme;
   }
-
+*/
   return NULL;
 }
 
@@ -589,16 +623,13 @@
 GList *
 games_card_theme_get_all (void)
 {
-  GList *list;
+  GList *list = NULL;
 
   _games_card_theme_ensure_theme_infos ();
 
-  list = g_list_copy (theme_infos);
-  g_list_foreach (list, (GFunc) games_card_theme_info_ref, NULL);
+  g_hash_table_foreach (theme_infos, (GHFunc) foreach_add_to_list, &list);
 
-  return list;
-  // FIXMEchpe
-//  return g_list_sort (list, (GCompareFunc) _games_card_theme_info_collate, NULL);
+  return g_list_sort (list, (GCompareFunc) _games_card_theme_info_collate);
 }
 
 /* GamesCardThemeInfo impl */
@@ -650,15 +681,16 @@
   return info;
 }
 
-/**
- * _games_card_theme_info_equal:
- * @a:
- * @b:
- *
- * Compares @a and @b.
- *
- * Returns: %TRUE iff @a and @b refer to the same card theme
- */
+guint
+_games_card_theme_info_hash (GamesCardThemeInfo *a,
+                             GamesCardThemeInfo *b)
+{
+/*  return g_int_hash (a->type, b->type) ^
+         g_str_hash (a->path, b->path) ^
+         g_str_hash (a->filename, b->filename);*/
+  return 0;
+}
+
 gboolean
 _games_card_theme_info_equal (GamesCardThemeInfo *a,
                               GamesCardThemeInfo *b)



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