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



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

Log:
Only look for enabled card theme formats. Some code cleanups. Adapt
callers to minor API changes.

Modified:
   trunk/aisleriot/window.c
   trunk/blackjack/src/card.cpp
   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:19 2009
@@ -1,7 +1,7 @@
 /*
  *  Copyright  1998, 2003 Jonathan Blandford <jrb alum mit edu>
  *  Copyright  2003 Callum McKenzie <callum physics otago ac nz>
- *  Copyright  2007, 2008 Christian Persch
+ *  Copyright  2007, 2008, 2009 Christian Persch
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -2384,7 +2384,7 @@
   GtkAction *action;
   char *theme_name;
   GamesCardThemeInfo *theme_info;
-  GamesCardTheme *theme;
+  GamesCardTheme *theme = NULL;
   guint i;
 #ifdef HAVE_HILDON
   GtkToolItem *tool_item;
@@ -2425,13 +2425,18 @@
   theme_info = games_card_themes_get_theme_info_by_name (priv->theme_manager, theme_name);
   g_free (theme_name);
   if (!theme_info) {
-    //XXXtheme = games_card_themes_get_theme_any (priv->theme_manager);
+    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) {
-      aisleriot_window_take_card_theme (window, theme /* adopts */);
-    }
+  }
+  if (!theme) {
+    /* Last-ditch fallback: try getting *any* theme */
+    theme = games_card_themes_get_theme_any (priv->theme_manager);
+  }
+  if (theme) {
+    aisleriot_window_take_card_theme (window, theme /* adopts */);
   } else {
     /* FIXMEchpe: FUCK, what now? Panic! */
   }

Modified: trunk/blackjack/src/card.cpp
==============================================================================
--- trunk/blackjack/src/card.cpp	(original)
+++ trunk/blackjack/src/card.cpp	Tue Jan  6 18:20:19 2009
@@ -125,18 +125,28 @@
         }
         if (!theme) {
                 char *card_theme;
+                GamesCardThemeInfo *theme_info;
 
                 card_theme = bj_get_card_style ();
-                theme = games_card_themes_get_theme_by_name (theme_manager, card_theme);
+                theme_info = games_card_themes_get_theme_info_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) {
-                        g_warning ("Failed to load theme %s!", card_theme);
+                        /* Last-ditch fallback: try getting *any* theme */
                         theme = games_card_themes_get_theme_any (theme_manager);
                 }
                 if (!theme) {
+                        /* No more options; quit. */
                         g_warning ("Failed to load any theme !");
                         exit (1);
                 }
-                g_free (card_theme);
 
                 images = games_card_images_new ();
                 games_card_images_set_theme (images, theme);
@@ -159,12 +169,19 @@
 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 = games_card_themes_get_theme_by_name (theme_manager, card_theme);
+        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);
         if (!new_theme) {
                 g_warning ("Failed to load theme %s\n", card_theme);
                 return;

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:19 2009
@@ -48,10 +48,7 @@
   GObject parent;
 
   GHashTable *theme_infos;
-  gboolean theme_infos_update_needed;
-
-  GType *theme_types;
-  guint n_theme_types;
+  gboolean theme_infos_loaded;
 };
 
 enum {
@@ -90,7 +87,7 @@
 #endif
 #endif /* !HAVE_HILDON */
 #ifdef ENABLE_CARD_THEME_FORMAT_FIXED
-    { "fixed", GAMES_TYPE_CARD_THEME_FIXED },
+    { "fixed", GAMES_TYPE_CARD_THEME_FIXED }
 #endif
   };
   GType type = G_TYPE_INVALID;
@@ -141,11 +138,33 @@
                                               GamesCardThemeForeachFunc callback,
                                               gpointer data)
 {
+  const GType types[] = {
+  /* List of supported theme types, in order of decreasing precedence */
+#ifdef HAVE_RSVG
+#ifdef ENABLE_CARD_THEME_FORMAT_SVG
+  GAMES_TYPE_CARD_THEME_SVG,
+#endif
+#ifdef ENABLE_CARD_THEME_FORMAT_KDE
+  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
+  };
   guint i;
   gboolean retval = TRUE;
 
-  for (i = 0; i < theme_manager->n_theme_types; ++i) {
-    retval = games_card_themes_foreach_theme_dir (theme_manager->theme_types[i], callback, data);
+  for (i = 0; i < G_N_ELEMENTS (types); ++i) {
+    retval = games_card_themes_foreach_theme_dir (types[i], callback, data);
     if (!retval)
       break;
   }
@@ -175,7 +194,8 @@
     _games_profile_end ("checking for %s card theme in file %s", G_OBJECT_CLASS_NAME (klass), filename);
 
     if (info)
-      g_hash_table_insert (theme_manager->theme_infos, info->pref_name, info);
+      /* Replace existing info with the new one */
+      g_hash_table_replace (theme_manager->theme_infos, info->pref_name, info);
   }
       
   g_dir_close (iter);
@@ -204,11 +224,8 @@
 }
 
 static void
-_games_card_theme_ensure_theme_infos (GamesCardThemes *theme_manager)
+games_card_themes_load_theme_infos (GamesCardThemes *theme_manager)
 {
-  if (!theme_manager->theme_infos_update_needed)
-    return;
-
   _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,
@@ -277,6 +294,7 @@
                         DBusGProxyCall *call,
                         ThemeInstallData *data)
 {
+  GamesCardThemes *theme_manager = data->theme_manager;
   GError *error = NULL;
 
   if (!dbus_g_proxy_end_call (proxy, call, &error, G_TYPE_INVALID)) {
@@ -287,7 +305,8 @@
     return;
   }
 
-  /* FIXME: re-scan theme directories, and emit "changed" signal */
+  /* Installation succeeded. Now re-scan the theme directories */
+  games_card_themes_load_theme_infos (theme_manager);
 }
 
 #endif /* ENABLE_CARD_THEMES_INSTALLER */
@@ -299,30 +318,12 @@
 static void
 games_card_themes_init (GamesCardThemes *theme_manager)
 {
-  GType *types;
-  guint n_types = 0;
-
   /* Hash table: pref name => theme info */
   theme_manager->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);
 
-  theme_manager->theme_infos_update_needed = TRUE;
-
-  types = theme_manager->theme_types = g_new (GType, N_THEME_TYPES);
-
-  /* List of supported theme types, in order of decreasing precedence */
-#ifdef HAVE_RSVG
-  types[n_types++] = GAMES_TYPE_CARD_THEME_SVG;
-  types[n_types++] = GAMES_TYPE_CARD_THEME_KDE;
-#endif
-#ifndef HAVE_HILDON
-  types[n_types++] = GAMES_TYPE_CARD_THEME_SLICED;
-  types[n_types++] = GAMES_TYPE_CARD_THEME_PYSOL;
-#endif
-  types[n_types++] = GAMES_TYPE_CARD_THEME_FIXED;
-
-  theme_manager->n_theme_types = n_types;
+  theme_manager->theme_infos_loaded = FALSE;
 }
 
 static void
@@ -373,6 +374,24 @@
 }
 
 /**
+ * games_card_themes_request_themes:
+ * @theme_manager:
+ *
+ * Scans all theme directories for themes, if necessary. If the
+ * themes list has changed, emits the "changed" signal synchronously.
+ */
+void
+games_card_themes_request_themes (GamesCardThemes *theme_manager)
+{
+  g_return_if_fail (GAMES_IS_CARD_THEMES (theme_manager));
+
+  if (theme_manager->theme_infos_loaded)
+    return;
+
+  games_card_themes_load_theme_infos (theme_manager);
+}
+
+/**
  * games_card_themes_get_theme:
  * @theme_manager:
  * @info: a #GamesCardThemeInfo
@@ -393,24 +412,27 @@
   if (info->type == G_TYPE_INVALID)
     return NULL;
 
-  _games_profile_start ("loading %s card theme %s", g_type_name (info->type), info->display_name);
+  _games_profile_start ("loading card theme %s/%s", g_type_name (info->type), info->display_name);
 
   theme = g_object_new (info->type, "theme-info", info, NULL);
   if (!theme->klass->load (theme, &error)) {
     _games_debug_print (GAMES_DEBUG_CARD_THEME,
-                        "Failed to load card theme %s: %s\n",
-                        info->display_name, error ? error->message : "(no error information)");
+                        "Failed to load card theme %s/%s: %s\n",
+                        g_type_name (info->type),
+                        info->display_name,
+                        error ? error->message : "(no error information)");
 
     g_clear_error (&error);
     g_object_unref (theme);
     theme = NULL;
   } else {
     _games_debug_print (GAMES_DEBUG_CARD_THEME,
-                        "Successfully loaded card theme %s\n",
+                        "Successfully loaded card theme %s/%s\n",
+                        g_type_name (info->type),
                         info->display_name);
   }
 
-  _games_profile_end ("loading %s card theme %s", g_type_name (info->type), info->display_name);
+  _games_profile_end ("loading card theme %s/%s", g_type_name (info->type), info->display_name);
 
   return theme;
 }
@@ -453,14 +475,19 @@
 
     if (dot == NULL) {
       /* No dot? Try appending the default, for compatibility with old settings */
-#if defined(HAVE_HILDON)
-      filename = free_me = g_strconcat (filename, ".card-theme", NULL);
-#elif defined(HAVE_RSVG)
-      filename = free_me = g_strconcat (filename, ".svg", NULL);
+#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 {
-#ifdef HAVE_GNOME
-      if (g_str_has_suffix (filename, ".png")) {
+#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 */
@@ -468,7 +495,7 @@
         filename = free_me = g_strconcat (base_name, ".svg", NULL);
         g_free (base_name);
       }
-#endif /* HAVE_GNOME */
+#endif /* HAVE_GNOME && ENABLE_CARD_THEME_FORMAT_SVG */
     }
   } else {
     filename = theme_name;
@@ -484,7 +511,7 @@
     goto out;
 
   /* Not in our hash table, and the list is uptodate? No such theme! */
-  if (!theme_manager->theme_infos_update_needed)
+  if (theme_manager->theme_infos_loaded)
     goto out;
 
   /* Then, try to find it in one of the theme dirs */
@@ -499,8 +526,16 @@
   return theme_info;
 }
 
-#if 0
-static GamesCardThemeInfo *
+/**
+ * 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;
@@ -520,7 +555,6 @@
 
   return theme_info;
 }
-#endif
 
 /**
  * games_card_themes_get_theme_any:
@@ -537,8 +571,6 @@
 
   g_return_val_if_fail (GAMES_IS_CARD_THEMES (theme_manager), NULL);
 
-  _games_card_theme_ensure_theme_infos (theme_manager);
-
 /*  if (!theme_infos)
     return NULL;
 
@@ -557,7 +589,11 @@
 /**
  * games_card_themes_get_themes:
  *
- * Returns:
+ * Gets the list of known themes. Note that you may need to call
+ * games_card_themes_request_themes() first ensure the themes
+ * information has been collected.
+ * 
+ * Returns: a newly allocated list of referenced #GamesCardThemeInfo objects
  */
 GList *
 games_card_themes_get_themes (GamesCardThemes *theme_manager)
@@ -566,8 +602,6 @@
 
   g_return_val_if_fail (GAMES_IS_CARD_THEMES (theme_manager), NULL);
 
-  _games_card_theme_ensure_theme_infos (theme_manager);
-
   g_hash_table_foreach (theme_manager->theme_infos, (GHFunc) 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:19 2009
@@ -43,6 +43,8 @@
 
 GamesCardThemes *games_card_themes_new (void);
 
+void games_card_themes_request_themes (GamesCardThemes *theme_manager);
+
 GamesCardTheme *games_card_themes_get_theme (GamesCardThemes *theme_manager,
                                              GamesCardThemeInfo *info);
 
@@ -51,6 +53,8 @@
 GamesCardThemeInfo *games_card_themes_get_theme_info_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);
 
 gboolean games_card_themes_can_install_themes (GamesCardThemes *theme_manager);



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