gnome-games r8497 - in trunk: . aisleriot blackjack/src libgames-support



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

Log:
Simplify the themes API, and adapt callers.

Modified:
   trunk/aisleriot/window.c
   trunk/blackjack/src/card.cpp
   trunk/configure.in
   trunk/libgames-support/games-card-themes.c
   trunk/libgames-support/games-card-themes.h

Modified: trunk/aisleriot/window.c
==============================================================================
--- trunk/aisleriot/window.c	(original)
+++ trunk/aisleriot/window.c	Tue Jan  6 18:20:30 2009
@@ -2402,8 +2402,7 @@
   GtkAccelGroup *accel_group;
   GtkAction *action;
   char *theme_name;
-  GamesCardThemeInfo *theme_info;
-  GamesCardTheme *theme = NULL;
+  GamesCardTheme *theme;
   guint i;
 #ifdef HAVE_HILDON
   GtkToolItem *tool_item;
@@ -2441,15 +2440,8 @@
   aisleriot_board_set_pixbuf_drawing (priv->board, priv->use_pixbuf_drawing);
 
   theme_name = games_conf_get_string (NULL, aisleriot_conf_get_key (CONF_THEME), NULL);
-  theme_info = games_card_themes_get_theme_info_by_name (priv->theme_manager, theme_name);
+  theme = games_card_themes_get_theme_by_name (priv->theme_manager, theme_name);
   g_free (theme_name);
-  if (!theme_info) {
-    games_card_themes_request_themes (priv->theme_manager);
-    games_card_themes_get_default_theme_info (priv->theme_manager);
-  }
-  if (theme_info) {
-    theme = games_card_themes_get_theme (priv->theme_manager, theme_info);
-  }
   if (!theme) {
     /* Last-ditch fallback: try getting *any* theme */
     theme = games_card_themes_get_theme_any (priv->theme_manager);
@@ -2458,6 +2450,8 @@
     aisleriot_window_take_card_theme (window, theme /* adopts */);
   } else {
     /* FIXMEchpe: FUCK, what now? Panic! */
+    /* Put up some UI, and exit! */
+    g_assert_not_reached ();
   }
 
   priv->action_group = gtk_action_group_new ("MenuActions");

Modified: trunk/blackjack/src/card.cpp
==============================================================================
--- trunk/blackjack/src/card.cpp	(original)
+++ trunk/blackjack/src/card.cpp	Tue Jan  6 18:20:30 2009
@@ -125,19 +125,11 @@
         }
         if (!theme) {
                 char *card_theme;
-                GamesCardThemeInfo *theme_info;
 
                 card_theme = bj_get_card_style ();
-                theme_info = games_card_themes_get_theme_info_by_name (theme_manager, card_theme);
+                theme = games_card_themes_get_theme_by_name (theme_manager, card_theme);
                 g_free (card_theme);
 
-                if (!theme_info) {
-                        games_card_themes_request_themes (theme_manager);
-                        theme_info = games_card_themes_get_default_theme_info (theme_manager);
-                }
-                if (theme_info) {
-                        theme = games_card_themes_get_theme (theme_manager, theme_info);
-                }
                 if (!theme) {
                         /* Last-ditch fallback: try getting *any* theme */
                         theme = games_card_themes_get_theme_any (theme_manager);
@@ -169,19 +161,12 @@
 void
 bj_card_set_theme (gchar *card_theme)
 {
-        GamesCardThemeInfo *new_theme_info;
         GamesCardTheme *new_theme;
 
         g_assert (theme_manager != NULL);
         g_assert (theme != NULL);
 
-        new_theme_info = games_card_themes_get_theme_info_by_name (theme_manager, card_theme);
-        if (!new_theme_info) {
-                g_warning ("Failed to find theme %s\n", card_theme);
-                return;
-        }
-
-        new_theme = games_card_themes_get_theme (theme_manager, new_theme_info);
+        new_theme = games_card_themes_get_theme_by_name (theme_manager, card_theme);
         if (!new_theme) {
                 g_warning ("Failed to load theme %s\n", card_theme);
                 return;

Modified: trunk/configure.in
==============================================================================
--- trunk/configure.in	(original)
+++ trunk/configure.in	Tue Jan  6 18:20:30 2009
@@ -380,8 +380,8 @@
 AC_ARG_WITH([default-card-theme],
   [AS_HELP_STRING([--with-default-card-theme=NAME],[Which card theme to use by default (default: gnomangelo_bitmap)])],
   [],[case "$with_platform" in
-        gnome|gtk-only) with_default_card_theme=gnomangelo_bitmap ;;
-        hildon) with_default_card_theme=paris ;;
+        gnome|gtk-only) with_default_card_theme=gnomangelo_bitmap.svg ;;
+        hildon) with_default_card_theme=paris.card-theme ;;
       esac])
 AC_MSG_RESULT([$with_default_card_theme])
 
@@ -397,14 +397,11 @@
 AC_MSG_RESULT([$with_default_card_theme_format])
 
 case "$with_default_card_theme_format" in
-  svg) GAMES_CARD_THEME_DEFAULT_FORMAT="GAMES_TYPE_CARD_THEME_SVG" ;;
-  kde) GAMES_CARD_THEME_DEFAULT_FORMAT="GAMES_TYPE_CARD_THEME_KDE" ;;
-  fixed) GAMES_CARD_THEME_DEFAULT_FORMAT="GAMES_TYPE_CARD_THEME_FIXED" ;;
-  sliced) GAMES_CARD_THEME_DEFAULT_FORMAT="GAMES_TYPE_CARD_THEME_SLICED" ;;
-  pysol) GAMES_CARD_THEME_DEFAULT_FORMAT="GAMES_TYPE_CARD_THEME_PYSOL" ;;
+  svg|kde|fixed|sliced|pysol) ;;
+  *) AC_MSG_ERROR([card theme format $with_default_card_theme_format unknown])
 esac
 
-AC_DEFINE_UNQUOTED([GAMES_CARD_THEME_DEFAULT_FORMAT],[$GAMES_CARD_THEME_DEFAULT_FORMAT],[The default card theme format type])
+AC_DEFINE_UNQUOTED([GAMES_CARD_THEME_DEFAULT_FORMAT_STRING],["$with_default_card_theme_format"],[The default card theme format type string])
 
 # Card theme installer
 
@@ -1149,6 +1146,8 @@
     Using RSVG:            ${have_rsvg}
     Using Clutter:         ${enable_clutter}
     Card theme formats:    ${with_card_theme_formats}
+    Default theme format:  ${with_default_card_theme_format}
+    Default theme:         ${with_default_card_theme}
     Sound support:         ${with_sound}
     Use setgid binaries:   ${setgid}
     Scores user:           ${scores_user}

Modified: trunk/libgames-support/games-card-themes.c
==============================================================================
--- trunk/libgames-support/games-card-themes.c	(original)
+++ trunk/libgames-support/games-card-themes.c	Tue Jan  6 18:20:30 2009
@@ -62,6 +62,13 @@
 
 static guint signals[LAST_SIGNAL];
 
+/**
+ * theme_type_from_string:
+ * @type_str:
+ * @type_str_len: the length of @type_str
+ *
+ * Returns: the #GType of the card theme type @type_str
+ */
 static GType
 theme_type_from_string (const char *type_str,
                         gssize type_str_len)
@@ -78,14 +85,12 @@
     { "kde", GAMES_TYPE_CARD_THEME_KDE },
 #endif
 #endif /* HAVE_RSVG */
-#ifndef HAVE_HILDON
 #ifdef ENABLE_CARD_THEME_FORMAT_SLICED
     { "sliced", GAMES_TYPE_CARD_THEME_SLICED },
 #endif
 #ifdef ENABLE_CARD_THEME_FORMAT_PYSOL
     { "pysol", GAMES_TYPE_CARD_THEME_PYSOL },
 #endif
-#endif /* !HAVE_HILDON */
 #ifdef ENABLE_CARD_THEME_FORMAT_FIXED
     { "fixed", GAMES_TYPE_CARD_THEME_FIXED }
 #endif
@@ -93,12 +98,10 @@
   GType type = G_TYPE_INVALID;
 
   if (type_str_len == 0) {
+    static const char default_type_string[] = GAMES_CARD_THEME_DEFAULT_FORMAT_STRING;
+
     /* Use the default type */
-#ifdef HAVE_HILDON
-    type = GAMES_TYPE_CARD_THEME_FIXED;
-#else
-    type = GAMES_TYPE_CARD_THEME_SVG;
-#endif
+    type = theme_type_from_string (default_type_string, strlen (default_type_string));
   } else {
     guint i;
 
@@ -113,6 +116,79 @@
   return type;
 }
 
+/**
+ * theme_filename_and_type_from_name:
+ * @theme_name: the theme name, or %NULL to get the default theme
+ * @type: return location for the card theme type
+ *
+ * Returns: the filename of the theme @theme_name, and puts the type
+ *   in @type, or %NULL if it was not possible to get the type or
+ *   filename from @theme_name, or if the requested theme type is not
+ *   supported
+ */
+static char *
+theme_filename_and_type_from_name (const char *theme_name,
+                                   GType *type)
+{
+  const char *colon;
+
+  g_return_val_if_fail (type != NULL, NULL);
+
+  _games_debug_print (GAMES_DEBUG_CARD_THEME,
+                      "theme_filename_and_type_from_name %s\n",
+                      theme_name ? theme_name : "(null)");
+
+  if (!theme_name || !theme_name[0])
+    theme_name = GAMES_CARD_THEME_DEFAULT;
+
+  colon = strchr (theme_name, ':');
+  *type = theme_type_from_string (theme_name, colon ? colon - theme_name : 0);
+  if (*type == G_TYPE_INVALID)
+    return NULL;
+
+  if (colon) {
+    const char *filename, *dot;
+
+    /* Get the filename from the theme name */
+    filename = colon + 1;
+    dot = strrchr (filename, '.');
+    if (filename == dot || !filename[0])
+      return NULL;
+
+    if (dot == NULL) {
+      /* No dot? Try appending the default, for compatibility with old settings */
+#if defined(ENABLE_CARD_THEME_FORMAT_FIXED)
+      if (*type == GAMES_TYPE_CARD_THEME_FIXED) {
+        return g_strconcat (filename, ".card-theme", NULL);
+      }
+#elif defined(ENABLE_CARD_THEME_FORMAT_SVG)
+      if (*type == GAMES_TYPE_CARD_THEME_SVG) {
+        return g_strconcat (filename, ".svg", NULL);
+      }
+#endif
+    } else {
+#if defined(HAVE_GNOME) && defined(ENABLE_CARD_THEME_FORMAT_SVG)
+      if (*type == GAMES_TYPE_CARD_THEME_SVG &&
+          g_str_has_suffix (filename, ".png")) {
+        char *base_name, *retval;
+
+        /* Very old version; replace .png with .svg */
+        base_name = g_strndup (filename, dot - filename);
+        retval = g_strconcat (base_name, ".svg", NULL);
+        g_free (base_name);
+
+        return retval;
+      } else
+#endif /* HAVE_GNOME && ENABLE_CARD_THEME_FORMAT_SVG */
+      {
+        return g_strdup (filename);
+      }
+    }
+  }
+
+  return g_strdup (theme_name);
+}
+
 static gboolean
 games_card_themes_foreach_theme_dir (GType type,
                                      GamesCardThemeForeachFunc callback,
@@ -148,14 +224,12 @@
   GAMES_TYPE_CARD_THEME_KDE,
 #endif
 #endif /* HAVE_RSVG */
-#ifndef HAVE_HILDON
 #ifdef ENABLE_CARD_THEME_FORMAT_SLICED
   GAMES_TYPE_CARD_THEME_SLICED,
 #endif
 #ifdef ENABLE_CARD_THEME_FORMAT_PYSOL
   GAMES_TYPE_CARD_THEME_PYSOL,
 #endif
-#endif /* !HAVE_HILDON */
 #ifdef ENABLE_CARD_THEME_FORMAT_FIXED
   GAMES_TYPE_CARD_THEME_FIXED
 #endif
@@ -226,6 +300,11 @@
 static void
 games_card_themes_load_theme_infos (GamesCardThemes *theme_manager)
 {
+  _games_debug_print (GAMES_DEBUG_CARD_THEME,
+                      "Scanning theme directories\n");
+
+  /* FIXMEchpe: clear the hash table here? */
+
   _games_profile_start ("looking for card themes");
   games_card_themes_foreach_theme_type_and_dir (theme_manager,
                                                 (GamesCardThemeForeachFunc) games_card_themes_get_theme_infos_in_dir,
@@ -239,39 +318,43 @@
   GType type;
   const char *filename;
   GamesCardThemeInfo *theme_info;
-} FindData;
+} ThemesByTypeAndFilenameData;
 
 static void
-find_by_type_and_name (gpointer key,
-                       GamesCardThemeInfo *info,
-                       FindData *data)
+themes_foreach_by_type_and_filename (gpointer key,
+                                     GamesCardThemeInfo *theme_info,
+                                     ThemesByTypeAndFilenameData *data)
 {
-  if (info->type != data->type ||
-      strcmp (info->filename, data->filename) != 0)
+  if (data->theme_info)
     return;
 
-  if (data->theme_info == NULL)
-    data->theme_info = info;
+  if (theme_info->type == data->type &&
+      strcmp (theme_info->filename, data->filename) == 0)
+    data->theme_info = theme_info;
 }
 
 static void
-foreach_add_to_list (gpointer key,
-                     gpointer data,
-                     GList **list)
+themes_foreach_add_to_list (gpointer key,
+                            gpointer data,
+                            GList **list)
 {
   *list = g_list_prepend (*list, data);
 }
 
-static GamesCardThemeInfo *
-games_card_themes_get_info_by_type_and_filename (GamesCardThemes *theme_manager,
-                                                 GType type,
-                                                 const char *filename)
-{
-  FindData data = { type, filename, NULL };
+typedef struct {
+  GamesCardThemes *theme_manager;
+  GamesCardTheme *theme;
+} ThemesAnyData;
 
-  g_hash_table_foreach (theme_manager->theme_infos, (GHFunc) find_by_type_and_name, &data);
+static void
+themes_foreach_any (gpointer key,
+                    GamesCardThemeInfo *theme_info,
+                    ThemesAnyData *data)
+{
+  if (data->theme)
+    return;
 
-  return data.theme_info;
+  data->theme = games_card_themes_get_theme (data->theme_manager, theme_info);
 }
 
 #ifdef ENABLE_CARD_THEMES_INSTALLER
@@ -438,122 +521,58 @@
 }
 
 /**
- * games_card_themes_get_theme_info_by_name:
- * @theme_name: a theme name
+ * games_card_themes_get_theme_by_name:
+ * @theme_manager:
+ * @theme_name: a theme name, or %NULL to get the default theme
  *
- * This function exists only for backward compatibility with
- * older aisleriot versions' preferences.
- * 
- * Returns: a new #GamesCardTheme for @name, or %NULL if there was an
- *  error while loading the theme.
+ * Gets a #GamesCardTheme by its persistent name. If @theme_name is %NULL,
+ * gets the defaul theme.
+ *
+ * Returns: a new #GamesCardTheme for @theme_name, or %NULL if there was an
+ *  error while loading the theme
  */
-GamesCardThemeInfo *
-games_card_themes_get_theme_info_by_name (GamesCardThemes *theme_manager,
-                                          const char *theme_name)
+GamesCardTheme *
+games_card_themes_get_theme_by_name (GamesCardThemes *theme_manager,
+                                     const char *theme_name)
 {
-  const char *colon, *filename, *dot;
-  char *free_me = NULL;
   GType type;
-  LookupData data;
+  char *filename;
   GamesCardThemeInfo *theme_info = NULL;
 
   g_return_val_if_fail (GAMES_IS_CARD_THEMES (theme_manager), NULL);
 
-  if (!theme_name || !theme_name[0])
-    return NULL;
+  filename = theme_filename_and_type_from_name (theme_name, &type);
+  _games_debug_print (GAMES_DEBUG_CARD_THEME,
+                      "Resolved card type=%s filename=%s\n",
+                      g_type_name (type),
+                      filename);
 
-  colon = strchr (theme_name, ':');
-  type = theme_type_from_string (theme_name, colon ? colon - theme_name : 0);
-  if (type == G_TYPE_INVALID)
+  if (filename == NULL || type == G_TYPE_INVALID)
     return NULL;
 
-  if (colon) {
-    /* Get the filename from the theme name */
-
-    filename = colon + 1;
-    dot = strrchr (filename, '.');
-
-    if (dot == NULL) {
-      /* No dot? Try appending the default, for compatibility with old settings */
-#if defined(HAVE_HILDON) && defined(ENABLE_CARD_THEME_FORMAT_FIXED)
-      if (type == GAMES_TYPE_CARD_THEME_FIXED) {
-        filename = free_me = g_strconcat (filename, ".card-theme", NULL);
-      }
-#elif defined(HAVE_RSVG) && defined(ENABLE_CARD_THEME_FORMAT_SVG)
-      if (type == GAMES_TYPE_CARD_THEME_SVG) {
-        filename = free_me = g_strconcat (filename, ".svg", NULL);
-      }
-#endif
-    } else {
-#if defined(HAVE_GNOME) && defined(ENABLE_CARD_THEME_FORMAT_SVG)
-      if (type == GAMES_TYPE_CARD_THEME_SVG &&
-          g_str_has_suffix (filename, ".png")) {
-        char *base_name;
-
-        /* Very old version; replace .png with .svg */
-        base_name = g_strndup (filename, dot - filename);
-        filename = free_me = g_strconcat (base_name, ".svg", NULL);
-        g_free (base_name);
-      }
-#endif /* HAVE_GNOME && ENABLE_CARD_THEME_FORMAT_SVG */
-    }
-  } else {
-    filename = theme_name;
-  }
-  if (!filename[0])
-    goto out;
-
   /* First try to find the theme in our hash table */
-  theme_info = games_card_themes_get_info_by_type_and_filename (theme_manager,
-                                                                type,
-                                                                filename);
-  if (theme_info)
-    goto out;
-
-  /* Not in our hash table, and the list is uptodate? No such theme! */
-  if (theme_manager->theme_infos_loaded)
-    goto out;
-
-  /* Then, try to find it in one of the theme dirs */
-  data.filename = filename;
-  data.theme_info = NULL;
-  games_card_themes_foreach_theme_dir (type, (GamesCardThemeForeachFunc) games_card_themes_try_theme_info_by_filename, &data);
-  theme_info = data.theme_info;
+  {
+    ThemesByTypeAndFilenameData data = { type, filename, NULL };
 
-out:
-  g_free (free_me);
+    g_hash_table_foreach (theme_manager->theme_infos, (GHFunc) themes_foreach_by_type_and_filename, &data);
 
-  return theme_info;
-}
+    theme_info = data.theme_info;
+  }
 
-/**
- * games_card_themes_get_default_theme_info:
- * @theme_manager:
- *
- * Note that you need to call games_card_themes_request_themes() first.
- *
- * Returns: the #GamesCardThemeInfo for the default theme, or %NULL if
- *   the default theme couldn't be found
- */
-GamesCardThemeInfo *
-games_card_themes_get_default_theme_info (GamesCardThemes *theme_manager)
-{
-  GType type;
-  char *filename;
-  GamesCardThemeInfo *theme_info;
+  if (theme_info == NULL &&
+      !theme_manager->theme_infos_loaded) {
+    LookupData data = { filename, NULL };
 
-#ifdef HAVE_HILDON
-  type = GAMES_TYPE_CARD_THEME_FIXED;
-  filename = g_strconcat (GAMES_CARD_THEME_DEFAULT, ".card-theme", NULL);
-#else
-  type = GAMES_TYPE_CARD_THEME_SVG;
-  filename = g_strconcat (GAMES_CARD_THEME_DEFAULT, ".svg", NULL);
-#endif
+    games_card_themes_foreach_theme_dir (type, (GamesCardThemeForeachFunc) games_card_themes_try_theme_info_by_filename, &data);
+    theme_info = data.theme_info;
+  }
 
-  theme_info = games_card_themes_get_info_by_type_and_filename (theme_manager, type, filename);
   g_free (filename);
 
-  return theme_info;
+  if (theme_info == NULL)
+    return NULL;
+
+  return games_card_themes_get_theme (theme_manager, theme_info);
 }
 
 /**
@@ -567,23 +586,18 @@
 GamesCardTheme *
 games_card_themes_get_theme_any (GamesCardThemes *theme_manager)
 {
-//   GList *l;
+  ThemesAnyData data = { theme_manager, NULL };
 
   g_return_val_if_fail (GAMES_IS_CARD_THEMES (theme_manager), NULL);
 
-/*  if (!theme_infos)
-    return NULL;
+  _games_debug_print (GAMES_DEBUG_CARD_THEME,
+                      "Fallback: trying to load any theme\n");
 
-  for (l = theme_infos; l != NULL; l = l->next) {
-    GamesCardThemeInfo *info = (GamesCardThemeInfo *) l->data;
-    GamesCardTheme *theme;
-
-    theme = games_card_theme_get (info);
-    if (theme)
-      return theme;
-  }
-*/
-  return NULL;
+  games_card_themes_request_themes (theme_manager);
+
+  g_hash_table_foreach (theme_manager->theme_infos, (GHFunc) themes_foreach_any, &data);
+
+  return data.theme;
 }
 
 /**
@@ -602,7 +616,7 @@
 
   g_return_val_if_fail (GAMES_IS_CARD_THEMES (theme_manager), NULL);
 
-  g_hash_table_foreach (theme_manager->theme_infos, (GHFunc) foreach_add_to_list, &list);
+  g_hash_table_foreach (theme_manager->theme_infos, (GHFunc) themes_foreach_add_to_list, &list);
 
   return g_list_sort (list, (GCompareFunc) _games_card_theme_info_collate);
 }

Modified: trunk/libgames-support/games-card-themes.h
==============================================================================
--- trunk/libgames-support/games-card-themes.h	(original)
+++ trunk/libgames-support/games-card-themes.h	Tue Jan  6 18:20:30 2009
@@ -45,17 +45,15 @@
 
 void games_card_themes_request_themes (GamesCardThemes *theme_manager);
 
+GList *games_card_themes_get_themes (GamesCardThemes *theme_manager);
+
 GamesCardTheme *games_card_themes_get_theme (GamesCardThemes *theme_manager,
                                              GamesCardThemeInfo *info);
 
-GamesCardTheme *games_card_themes_get_theme_any (GamesCardThemes *theme_manager);
-
-GamesCardThemeInfo *games_card_themes_get_theme_info_by_name (GamesCardThemes *theme_manager,
-                                                              const char *theme_name);
+GamesCardTheme *games_card_themes_get_theme_by_name (GamesCardThemes *theme_manager,
+                                                     const char *theme_name);
 
-GamesCardThemeInfo *games_card_themes_get_default_theme_info (GamesCardThemes *theme_manager);
-
-GList *games_card_themes_get_themes (GamesCardThemes *theme_manager);
+GamesCardTheme *games_card_themes_get_theme_any (GamesCardThemes *theme_manager);
 
 gboolean games_card_themes_can_install_themes (GamesCardThemes *theme_manager);
 



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