gnome-games r8423 - in trunk: aisleriot libgames-support



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

Log:
Add new function to get the themes list.

Modified:
   trunk/aisleriot/window.c
   trunk/libgames-support/games-card-selector.c
   trunk/libgames-support/games-card-selector.h
   trunk/libgames-support/games-card-theme-fixed.c
   trunk/libgames-support/games-card-theme-preimage.c
   trunk/libgames-support/games-card-theme-private.h
   trunk/libgames-support/games-card-theme.c
   trunk/libgames-support/games-card-theme.h
   trunk/libgames-support/games-files.c
   trunk/libgames-support/games-files.h

Modified: trunk/aisleriot/window.c
==============================================================================
--- trunk/aisleriot/window.c	(original)
+++ trunk/aisleriot/window.c	Tue Jan  6 18:18:13 2009
@@ -38,6 +38,7 @@
 #endif
 #endif /* HAVE_HILDON */
 
+#include <libgames-support/games-card-theme.h>
 #include <libgames-support/games-clock.h>
 #include <libgames-support/games-files.h>
 #include <libgames-support/games-stock.h>
@@ -1464,8 +1465,7 @@
 install_card_theme_menu (AisleriotWindow *window)
 {
   AisleriotWindowPrivate *priv = window->priv;
-  GamesFileList *themes;
-  GList *l;
+  GList *list, *l;
   GSList *radio_group = NULL;
   const char *card_theme;
   guint i = 0;
@@ -1483,11 +1483,12 @@
   /* See gtk bug #424448 */
   gtk_ui_manager_ensure_update (priv->ui_manager);
 
-  themes = games_file_list_card_themes (priv->scalable_cards);
+  list = games_card_theme_get_themes ();
 
   /* No need to install the menu when there's only one theme available anyway */
-  if (themes->list == NULL || themes->list->next == NULL) {
-    g_object_unref (themes);
+  if (list == NULL || list->next == NULL) {
+    g_list_foreach (list, (GFunc) g_free, NULL);
+    g_list_free (list);
     return;
   }
 
@@ -1499,7 +1500,7 @@
 
   card_theme = aisleriot_board_get_card_theme (priv->board);
 
-  for (l = themes->list; l != NULL; l = l->next) {
+  for (l = list; l != NULL; l = l->next) {
     const char *theme = (const char *) l->data;
     GtkRadioAction *action;
     char actionname[32];
@@ -1544,7 +1545,8 @@
     ++i;
   }
 
-  g_object_unref (themes);
+  g_list_foreach (list, (GFunc) g_free, NULL);
+  g_list_free (list);
 }
 
 /* Callbacks */

Modified: trunk/libgames-support/games-card-selector.c
==============================================================================
--- trunk/libgames-support/games-card-selector.c	(original)
+++ trunk/libgames-support/games-card-selector.c	Tue Jan  6 18:18:13 2009
@@ -27,6 +27,7 @@
 #include "games-card.h"
 #include "games-frame.h"
 #include "games-files.h"
+#include "games-card-theme.h"
 
 #include "games-card-selector.h"
 
@@ -43,16 +44,72 @@
 static void
 signal_propagator (GtkWidget * widget, GamesCardSelector * selector)
 {
-  gchar *name;
+  GList *l;
+  const char *name;
 
-  name = games_file_list_get_nth (selector->files,
-				  gtk_combo_box_get_active (GTK_COMBO_BOX
-							    (selector->
-							     combobox)));
+  l = g_list_nth (selector->files,
+                  gtk_combo_box_get_active (GTK_COMBO_BOX (selector->combobox)));
+  if (!l)
+    return;
+
+  name = l->data;
 
   g_signal_emit (selector, signals[CHANGED], 0, name);
 }
 
+static GtkWidget *
+create_combo_box (GList *filelist,
+                  const gchar * selection,
+                  guint flags)
+{
+  gint itemno;
+  GtkComboBox *widget;
+  gchar *visible, *string;
+  gboolean found = FALSE;
+
+  widget = GTK_COMBO_BOX (gtk_combo_box_new_text ());
+
+  itemno = 0;
+  while (filelist) {
+    gchar *s;
+
+    string = (gchar *) filelist->data;
+    visible = g_strdup (string);
+
+    /* These are a bit hackish, but we don't yet have a good regexp
+     * library in glib. There are probably some ways these could
+     * seriously mangle unicode strings. */
+    if (flags & GAMES_FILE_LIST_REMOVE_EXTENSION) {
+      s = g_strrstr (visible, ".");
+      if (s)
+        *s = '\0';
+    }
+    if (flags & GAMES_FILE_LIST_REPLACE_UNDERSCORES) {
+      s = visible;
+      while (*s) {
+        if (*s == '_')
+          *s = ' ';
+        s++;
+      }
+    }
+
+    gtk_combo_box_append_text (widget, visible);
+    if (selection && (!strcmp (string, selection))) {
+      gtk_combo_box_set_active (widget, itemno);
+      found = TRUE;
+    }
+
+    g_free (visible);
+
+    itemno++;
+    filelist = g_list_next (filelist);
+  }
+  if (!found)
+    gtk_combo_box_set_active (widget, 0);
+
+  return GTK_WIDGET (widget);
+}
+
 GtkWidget *
 games_card_selector_new (gboolean scalable, const gchar * current)
 {
@@ -62,11 +119,11 @@
 
   games_frame_set_label (GAMES_FRAME (selector), _("Card Style"));
 
-  selector->files = games_file_list_card_themes (scalable);
+  selector->files = games_card_theme_get_themes ();
 
-  selector->combobox = games_file_list_create_widget (selector->files,
-						      current,
-						      GAMES_FILE_LIST_REPLACE_UNDERSCORES);
+  selector->combobox = create_combo_box (selector->files,
+                                         current,
+                                         GAMES_FILE_LIST_REPLACE_UNDERSCORES);
 
   gtk_container_add (GTK_CONTAINER (selector), selector->combobox);
 
@@ -80,7 +137,8 @@
 {
   GamesCardSelector *selector = GAMES_CARD_SELECTOR (object);
 
-  g_object_unref (selector->files);
+  g_list_foreach (selector->files, (GFunc) g_free, NULL);
+  g_list_free (selector->files);
 
   G_OBJECT_CLASS (games_card_selector_parent_class)->finalize (object);
 }

Modified: trunk/libgames-support/games-card-selector.h
==============================================================================
--- trunk/libgames-support/games-card-selector.h	(original)
+++ trunk/libgames-support/games-card-selector.h	Tue Jan  6 18:18:13 2009
@@ -32,7 +32,7 @@
   GamesFrame parent;
 
   GtkWidget *combobox;
-  GamesFileList *files;
+  GList *files;
 } GamesCardSelector;
 
 typedef struct _GamesCardSelectorClass {

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:18:13 2009
@@ -376,13 +376,25 @@
   return pixbuf;
 }
 
-
 static const char *
 games_card_theme_fixed_get_default_theme_path (GamesCardThemeClass *klass)
 {
+  const char *env;
+
+  if ((env = g_getenv ("GAMES_CARD_THEME_PATH_FIXED")))
+    return env;
+  if ((env = g_getenv ("GAMES_CARD_THEME_PATH")))
+    return env;
+
   return games_runtime_get_directory (GAMES_RUNTIME_PRERENDERED_CARDS_DIRECTORY);
 }
 
+static const char *
+games_card_theme_fixed_get_theme_glob (GamesCardThemeClass *klass)
+{
+  return "*.card-theme";
+}
+
 static void
 games_card_theme_fixed_class_init (GamesCardThemeFixedClass * klass)
 {
@@ -392,6 +404,7 @@
   gobject_class->finalize = games_card_theme_fixed_finalize;
 
   theme_class->get_default_theme_path = games_card_theme_fixed_get_default_theme_path;
+  theme_class->get_theme_glob = games_card_theme_fixed_get_theme_glob;
   theme_class->load_theme = games_card_theme_fixed_load_theme;
   theme_class->get_theme_name = games_card_theme_fixed_get_theme_name;
   theme_class->set_card_size = games_card_theme_fixed_set_card_size;

Modified: trunk/libgames-support/games-card-theme-preimage.c
==============================================================================
--- trunk/libgames-support/games-card-theme-preimage.c	(original)
+++ trunk/libgames-support/games-card-theme-preimage.c	Tue Jan  6 18:18:13 2009
@@ -316,9 +316,22 @@
 static const char *
 games_card_theme_preimage_get_default_theme_path (GamesCardThemeClass *klass)
 {
+  const char *env;
+
+  if ((env = g_getenv ("GAMES_CARD_THEME_PATH_PREIMAGE")))
+    return env;
+  if ((env = g_getenv ("GAMES_CARD_THEME_PATH")))
+    return env;
+
   return games_runtime_get_directory (GAMES_RUNTIME_SCALABLE_CARDS_DIRECTORY);
 }
 
+static const char *
+games_card_theme_preimage_get_theme_glob (GamesCardThemeClass *klass)
+{
+  return "*.svg"; // FIXMEchpe: svgz ?
+}
+
 static void
 games_card_theme_preimage_class_init (GamesCardThemePreimageClass * klass)
 {
@@ -328,6 +341,7 @@
   gobject_class->finalize = games_card_theme_preimage_finalize;
 
   theme_class->get_default_theme_path = games_card_theme_preimage_get_default_theme_path;
+  theme_class->get_theme_glob = games_card_theme_preimage_get_theme_glob;
   theme_class->load_theme = games_card_theme_preimage_load_theme;
   theme_class->get_theme_name = games_card_theme_preimage_get_theme_name;
   theme_class->set_card_size = games_card_theme_preimage_set_card_size;

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:18:13 2009
@@ -27,6 +27,9 @@
 
   /* class vfuncs */
   const char * (* get_default_theme_path) (GamesCardThemeClass *klass);
+  const char * (* get_theme_glob)         (GamesCardThemeClass *klass);
+  GList *      (* get_themes_list)        (GamesCardThemeClass *klass,
+                                           const char *theme_dir);
 
   /* vfuncs */
   gboolean    (* load_theme)        (GamesCardTheme *theme,
@@ -57,7 +60,11 @@
   char *theme_name;
 };
 
-void _games_card_theme_emit_changed (GamesCardTheme * theme);
+const char *_games_card_theme_class_get_default_theme_path (GamesCardThemeClass *klass);
+const char *_games_card_theme_class_get_theme_glob (GamesCardThemeClass *klass);
+GList *_games_card_theme_class_get_themes_list (GamesCardThemeClass *klass);
+
+void _games_card_theme_emit_changed (GamesCardTheme *theme);
 
 /* GamesCardThemePreimage */
 

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:18:13 2009
@@ -43,10 +43,43 @@
 /* #defining this prints out the time it takes to render the theme */
 /* #define INSTRUMENT_LOADING */
 
+
 #ifdef INSTRUMENT_LOADING
 static long totaltime = 0;
 #endif
 
+static GType
+get_default_theme_type (void)
+{
+  GType type;
+  const char *env;
+
+#if defined(HAVE_RSVG) && !defined(HAVE_HILDON)
+  /* Default to scalable */
+  type = GAMES_TYPE_CARD_THEME_SVG;
+#else
+  /* Default to non-scalable */
+  type = GAMES_TYPE_CARD_THEME_FIXED;
+#endif
+
+#ifndef HAVE_HILDON
+  env = g_getenv ("GAMES_CARD_THEME_FORMAT");
+  if (env) {
+#ifdef HAVE_RSVG
+    if (strcmp (env, "svg") == 0)
+      type = GAMES_TYPE_CARD_THEME_SVG;
+    else
+#endif
+    if (strcmp (env, "sliced") == 0)
+      type = GAMES_TYPE_CARD_THEME_SLICED;
+    else if (strcmp (env, "fixed") == 0)
+      type = GAMES_TYPE_CARD_THEME_FIXED;
+  }
+#endif /* !HAVE_HILDON */
+
+  return type;
+}
+
 static gboolean
 games_card_theme_load_theme (GamesCardTheme *theme,
                              const char *theme_dir,
@@ -61,7 +94,7 @@
                                            const char *theme_name)
 {
   if (!theme_dir)
-    theme_dir = games_card_theme_get_default_theme_path (theme->klass);
+    theme_dir = _games_card_theme_class_get_default_theme_path (theme->klass);
 
   if (games_card_theme_load_theme (theme, theme_dir, theme_name))
     return TRUE;
@@ -118,6 +151,42 @@
   return object;
 }
 
+static GList *
+games_card_theme_get_themes_list (GamesCardThemeClass *klass,
+                                  const char *theme_dir)
+{
+  GamesFileList *files;
+  GList *l, *list;
+  const char *glob;
+
+  glob = _games_card_theme_class_get_theme_glob (klass);
+
+  if (!theme_dir)
+    theme_dir = _games_card_theme_class_get_default_theme_path (klass);
+
+  if (!theme_dir || !glob)
+    return NULL;
+
+  files = games_file_list_new (glob, theme_dir, NULL);
+  games_file_list_transform_basename (files);
+
+  for (l = files->list; l != NULL; l = l->next) {
+    const char *filename = (const char *) l->data;
+    char *dot;
+
+    dot = strrchr (filename, '.');
+    if (dot) {
+      *dot = '\0';
+    }
+  }
+
+  list = files->list;
+  files->list = NULL;
+  g_object_unref (files);
+
+  return list;
+}
+
 static void
 games_card_theme_class_init (GamesCardThemeClass * klass)
 {
@@ -125,6 +194,8 @@
 
   gobject_class->constructor = games_card_theme_constructor;
 
+  klass->get_themes_list = games_card_theme_get_themes_list;
+
   /**
    * GamesCardTheme:changed:
    * @theme: the object on which the signal is emitted
@@ -152,10 +223,15 @@
   g_signal_emit (theme, signals[CHANGED], 0);
 }
 
-/* public API */
-
+/**
+ * games_card_theme_class_get_default_theme_path:
+ * @klass:
+ *
+ * Returns: the default theme path for @klass. The string is owned by
+ * @klass and must not be modified or freed.
+ */
 const char *
-games_card_theme_get_default_theme_path (GamesCardThemeClass *klass)
+_games_card_theme_class_get_default_theme_path (GamesCardThemeClass *klass)
 {
   if (klass->get_default_theme_path)
     return klass->get_default_theme_path (klass);
@@ -163,6 +239,40 @@
   return NULL;
 }
 
+/**
+ * games_card_theme_class_get_theme_glob:
+ * @klass:
+ *
+ * Returns: the default theme path for @klass. The string is owned by
+ * @klass and must not be modified or freed.
+ */
+const char *
+_games_card_theme_class_get_theme_glob (GamesCardThemeClass *klass)
+{
+  if (klass->get_theme_glob)
+    return klass->get_theme_glob (klass);
+
+  return NULL;
+}
+
+/**
+ * games_card_theme_class_get_themes_list:
+ * @klass:
+ *
+ * Returns: a newly allocated list of newly allocated strings of
+ * containing the names of the themes found for @klass.
+ */
+GList *
+_games_card_theme_class_get_themes_list (GamesCardThemeClass *klass)
+{
+  if (klass->get_themes_list)
+    return klass->get_themes_list (klass, NULL);
+
+  return NULL;
+}
+
+/* public API */
+
 #if GTK_CHECK_VERSION (2, 10, 0)
 
 /**
@@ -316,38 +426,31 @@
 }
 
 /**
- * games_card_theme_sliced_new:
+ * games_card_theme_new:
  *
- * Returns: a new #GamesCardThemeSliced
+ * Returns: a new #GamesCardTheme
  */
 GamesCardTheme *
 games_card_theme_new (void)
 {
-  GType type = G_TYPE_INVALID;
-  const char *env;
 
-#if defined(HAVE_RSVG) && !defined(HAVE_HILDON)
-  /* Default to scalable */
-  type = GAMES_TYPE_CARD_THEME_SVG;
-#else
-  /* Default to non-scalable */
-  type = GAMES_TYPE_CARD_THEME_FIXED;
-#endif
+  return g_object_new (get_default_theme_type (), NULL);
+}
 
-#ifndef HAVE_HILDON
-  env = g_getenv ("GAMES_CARD_THEME_FORMAT");
-  if (env) {
-#ifdef HAVE_RSVG
-    if (strcmp (env, "svg") == 0)
-      type = GAMES_TYPE_CARD_THEME_SVG;
-    else
-#endif
-    if (strcmp (env, "sliced") == 0)
-      type = GAMES_TYPE_CARD_THEME_SLICED;
-    else if (strcmp (env, "fixed") == 0)
-      type = GAMES_TYPE_CARD_THEME_FIXED;
-  }
-#endif /* !HAVE_HILDON */
+/**
+ * games_card_theme_get_themes:
+ *
+ * Returns: 
+ */
+GList *
+games_card_theme_get_themes (void)
+{
+  GamesCardThemeClass *klass;
+  GList *list;
+
+  klass = g_type_class_ref (get_default_theme_type ());
+  list = games_card_theme_get_themes_list (klass, NULL);
+  g_type_class_unref (klass);
 
-  return g_object_new (type, NULL);
+  return list;
 }

Modified: trunk/libgames-support/games-card-theme.h
==============================================================================
--- trunk/libgames-support/games-card-theme.h	(original)
+++ trunk/libgames-support/games-card-theme.h	Tue Jan  6 18:18:13 2009
@@ -53,8 +53,6 @@
 
 GType games_card_theme_get_type (void);
 
-const char * games_card_theme_get_default_theme_path (GamesCardThemeClass *klass);
-
 #if GTK_CHECK_VERSION (2, 10, 0)
 void games_card_theme_set_font_options (GamesCardTheme *theme,
                                         const cairo_font_options_t *font_options);
@@ -146,4 +144,6 @@
 
 GamesCardTheme *games_card_theme_new (void);
 
+GList *games_card_theme_get_themes (void);
+
 #endif /* GAMES_CARD_THEME_H */

Modified: trunk/libgames-support/games-files.c
==============================================================================
--- trunk/libgames-support/games-files.c	(original)
+++ trunk/libgames-support/games-files.c	Tue Jan  6 18:18:13 2009
@@ -442,6 +442,8 @@
  * 
  * Returns: a new #GamesFileList containing the found themes
  */
+GamesFileList *games_file_list_card_themes (gboolean scalable);
+
 GamesFileList *
 games_file_list_card_themes (gboolean scalable)
 {

Modified: trunk/libgames-support/games-files.h
==============================================================================
--- trunk/libgames-support/games-files.h	(original)
+++ trunk/libgames-support/games-files.h	Tue Jan  6 18:18:13 2009
@@ -67,8 +67,6 @@
 					       const gchar * selection,
 					       guint flags);
 
-GamesFileList *games_file_list_card_themes (gboolean scalable);
-
 G_END_DECLS
 
 #endif /* GAMES_FILES_H */



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