[aisleriot] Canonicalise game names



commit 1a08a4bf16dc05bc1d00d3eea4ffdcfc987cd1df
Author: Christian Persch <chpe gnome org>
Date:   Fri Dec 2 21:21:57 2011 +0100

    Canonicalise game names
    
    Always store the game name internally without the .scm extension, and only
    append it when storing prefs (for backwards compatibility).

 src/ar-game-chooser.c     |   26 ++++++-----
 src/board-noclutter.c     |    2 +-
 src/board.c               |    2 +-
 src/conf.c                |   63 +++++++++++++++++++--------
 src/conf.h                |    8 ++--
 src/game.c                |   52 ++++++++++++-----------
 src/game.h                |    6 +-
 src/lib/ar-string-utils.c |   26 +++++++++++
 src/lib/ar-string-utils.h |    2 +
 src/sol.c                 |   24 ++++++-----
 src/util.c                |   50 ++++-----------------
 src/util.h                |    4 +-
 src/window.c              |  105 +++++++++++++++++++++++---------------------
 src/window.h              |    8 ++--
 14 files changed, 205 insertions(+), 173 deletions(-)
---
diff --git a/src/ar-game-chooser.c b/src/ar-game-chooser.c
index 0db3f11..ded04e8 100644
--- a/src/ar-game-chooser.c
+++ b/src/ar-game-chooser.c
@@ -54,7 +54,7 @@ enum {
 
 enum {
   COL_NAME,
-  COL_GAME_FILE
+  COL_GAME_MODULE
 };
 
 #define SELECT_GAME_DIALOG_MIN_HEIGHT (256)
@@ -83,16 +83,16 @@ response_cb (GtkWidget *dialog,
 
   if (response == GTK_RESPONSE_OK &&
       gtk_tree_selection_get_selected (priv->selection, &model, &iter)) {
-    char *game_file = NULL;
+    char *game_module = NULL;
 
     gtk_tree_model_get (model, &iter,
-                        COL_GAME_FILE, &game_file,
+                        COL_GAME_MODULE, &game_module,
                         -1);
-    g_assert (game_file != NULL);
+    g_assert (game_module != NULL);
 
-    aisleriot_window_set_game (priv->window, game_file, 0);
+    aisleriot_window_set_game_module (priv->window, game_module, NULL);
 
-    g_free (game_file);
+    g_free (game_module);
   }
 
   gtk_widget_destroy (dialog);
@@ -128,7 +128,7 @@ ar_game_chooser_constructor (GType type,
   GtkWidget *hbox;
   GtkTreeIter current_iter;
   gboolean current_iter_set = FALSE;
-  const char *current_game_file;
+  const char *current_game_module;
   const char *games_dir;
   GDir *dir;
   GtkWidget *content_area;
@@ -145,7 +145,7 @@ ar_game_chooser_constructor (GType type,
 
   priv->store = list = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
 
-  current_game_file = aisleriot_game_get_game_file (aisleriot_window_get_game (priv->window));
+  current_game_module = aisleriot_window_get_game_module (priv->window);
 
   games_dir = ar_runtime_get_directory (AR_RUNTIME_GAMES_DIRECTORY);
 
@@ -154,7 +154,7 @@ ar_game_chooser_constructor (GType type,
     const char *game_file;
 
     while ((game_file = g_dir_read_name (dir)) != NULL) {
-      char *game_name;
+      char *game_name, *game_module;
       GtkTreeIter iter;
 
       if (!g_str_has_suffix (game_file, ".scm") ||
@@ -162,20 +162,22 @@ ar_game_chooser_constructor (GType type,
         continue;
 
       game_name = ar_filename_to_display_name (game_file);
+      game_module = ar_filename_to_game_module (game_file);
 
       gtk_list_store_insert_with_values (GTK_LIST_STORE (list), &iter,
                                          -1,
                                          COL_NAME, game_name,
-                                         COL_GAME_FILE, game_file,
+                                         COL_GAME_MODULE, game_module,
                                          -1);
 
-      if (current_game_file &&
-          strcmp (current_game_file, game_file) == 0) {
+      if (current_game_module &&
+          strcmp (current_game_module, game_module) == 0) {
         current_iter = iter;
         current_iter_set = TRUE;
       }
 
       g_free (game_name);
+      g_free (game_module);
     }
 
     g_dir_close (dir);
diff --git a/src/board-noclutter.c b/src/board-noclutter.c
index 8adbc48..4b3b843 100644
--- a/src/board-noclutter.c
+++ b/src/board-noclutter.c
@@ -1929,7 +1929,7 @@ game_new_cb (AisleriotGame *game,
 #if 0
   g_print ("{ %.3f , %.3f /* %s */ },\n",
            priv->width, priv->height,
-           aisleriot_game_get_game_file (priv->game));
+           aisleriot_game_get_game_module (priv->game));
 #endif
 
   gtk_widget_queue_draw (GTK_WIDGET (board));
diff --git a/src/board.c b/src/board.c
index 066e7b7..deb49be 100644
--- a/src/board.c
+++ b/src/board.c
@@ -2074,7 +2074,7 @@ game_new_cb (AisleriotGame *game,
 #if 0
   g_print ("{ %.3f , %.3f /* %s */ },\n",
            priv->width, priv->height,
-           aisleriot_game_get_game_file (priv->game));
+           aisleriot_game_get_game_module (priv->game));
 #endif
 
   /* Check for animations so that the differences will be reset */
diff --git a/src/conf.c b/src/conf.c
index 6524d87..733856e 100644
--- a/src/conf.c
+++ b/src/conf.c
@@ -71,11 +71,17 @@ static GConfClient *gconf_client;
 static GHashTable *stats;
 
 static char *
-options_gconf_key (const char *game_file)
+options_gconf_key (const char *game_module)
 {
   static const char basekey[] = "/apps/aisleriot/rules/";
 
-  return g_strconcat (basekey, game_file, NULL);
+  return g_strdelimit (g_strconcat (basekey, game_module, ".scm", NULL), "-", '_');
+}
+
+static char *
+game_module_to_game_name (const char *game_module)
+{
+  return g_strdelimit (g_strconcat (game_module, ".scm", NULL), "-", '_');
 }
 
 static void
@@ -183,7 +189,7 @@ aisleriot_conf_init (void)
   }
 
 #ifdef HAVE_GNOME
-  
+
   gconf_client = gconf_client_get_default ();
 
   stats = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
@@ -217,7 +223,7 @@ aisleriot_conf_get_key (AisleriotConfKey key)
 }
 
 gboolean
-aisleriot_conf_get_options (const char *game_file,
+aisleriot_conf_get_options (const char *game_module,
                             int *options)
 {
 #ifdef HAVE_GNOME
@@ -225,7 +231,7 @@ aisleriot_conf_get_options (const char *game_file,
   GConfValue *value;
   char *gconf_key;
 
-  gconf_key = options_gconf_key (game_file);
+  gconf_key = options_gconf_key (game_module);
   entry = gconf_client_get_entry (gconf_client, gconf_key, NULL, TRUE, NULL);
   g_free (gconf_key);
   if (!entry)
@@ -245,7 +251,7 @@ aisleriot_conf_get_options (const char *game_file,
 #else
   GError *error = NULL;
 
-  *options = ar_conf_get_integer (game_file, "Options", &error);
+  *options = ar_conf_get_integer (game_module, "Options", &error);
   if (error) {
     g_error_free (error);
     return FALSE;
@@ -256,14 +262,14 @@ aisleriot_conf_get_options (const char *game_file,
 }
 
 void
-aisleriot_conf_set_options (const char *game_file,
+aisleriot_conf_set_options (const char *game_module,
                             int value)
 {
 #ifdef HAVE_GNOME
   GConfSchema *schema;
   char *gconf_key, *schemas_key;
 
-  gconf_key = options_gconf_key (game_file);
+  gconf_key = options_gconf_key (game_module);
 
   schemas_key = g_strconcat ("/schemas", gconf_key, NULL);
 
@@ -296,25 +302,28 @@ aisleriot_conf_set_options (const char *game_file,
   gconf_client_set_int (gconf_client, gconf_key, value, NULL);
   g_free (gconf_key);
 #else
-  ar_conf_set_integer (game_file, "Options", value);
+  ar_conf_set_integer (game_module, "Options", value);
 #endif /* HAVE_GNOME */
 }
 
 void
-aisleriot_conf_get_statistic (const char *game_file,
+aisleriot_conf_get_statistic (const char *game_module,
                               AisleriotStatistic *statistic)
 {
 #ifdef HAVE_GNOME
   AisleriotStatistic *game_stat;
+  char *game_name;
+
+  game_name = game_module_to_game_name (game_module);
 
-  game_stat = g_hash_table_lookup (stats, game_file);
+  game_stat = g_hash_table_lookup (stats, game_name);
   if (!game_stat) {
     char *display_name;
 
     /* Previous versions used the localised name as key, so try it as fall-back.
      * See bug #406267 and bug #525177.
      */
-    display_name = ar_filename_to_display_name (game_file);
+    display_name = ar_filename_to_display_name (game_module);
     game_stat = g_hash_table_lookup (stats, display_name);
     g_free (display_name);
   }
@@ -325,21 +334,28 @@ aisleriot_conf_get_statistic (const char *game_file,
     memset (statistic, 0, sizeof (AisleriotStatistic));
   }
 
+  g_free (game_name);
+
 #else
 
   int *values;
   gsize len = 0;
   GError *error = NULL;
+  char *game_name;
+
+  game_name = game_module_to_game_name (game_module);
 
   memset (statistic, 0, sizeof (AisleriotStatistic));
 
-  values = ar_conf_get_integer_list (game_file, "Statistic", &len, &error);
+  values = ar_conf_get_integer_list (game_name, "Statistic", &len, &error);
   if (error) {
     g_error_free (error);
+    g_free (game_name);
     return;
   }
   if (len != 4) {
     g_free (values);
+    g_free (game_name);
     return;
   }
 
@@ -358,46 +374,57 @@ aisleriot_conf_get_statistic (const char *game_file,
   }
 
   g_free (values);
+  g_free (game_name);
 #endif /* HAVE_GNOME */
 }
 
 void
-aisleriot_conf_set_statistic (const char *game_file,
+aisleriot_conf_set_statistic (const char *game_module,
                               AisleriotStatistic *statistic)
 {
 #ifdef HAVE_GNOME
   AisleriotStatistic *game_stat;
+  char *game_name;
+
+  game_name = game_module_to_game_name (game_module);
 
-  game_stat = g_hash_table_lookup (stats, game_file);
+  game_stat = g_hash_table_lookup (stats, game_name);
   /* Backward compatibility with buggy old aisleriot versions
    * which stored the localised game name.
    */
   if (!game_stat) {
     char *localised_name;
 
-    localised_name = ar_filename_to_display_name (game_file);
+    localised_name = ar_filename_to_display_name (game_module);
     game_stat = g_hash_table_lookup (stats, localised_name);
     g_free (localised_name);
   }
 
   if (!game_stat) {
     game_stat = g_new0 (AisleriotStatistic, 1);
-    g_hash_table_insert (stats, g_strdup (game_file), game_stat);
+    g_hash_table_insert (stats, g_strdup (game_name), game_stat);
   }
 
   *game_stat = *statistic;
 
   save_statistics ();
 
+  g_free (game_name);
+
 #else
 
+  char *game_name;
   int values[4];
 
+  game_name = game_module_to_game_name (game_module);
+
   values[0] = statistic->wins;
   values[1] = statistic->total;
   values[2] = statistic->best;
   values[3] = statistic->worst;
 
-  ar_conf_set_integer_list (game_file, "Statistic", values, G_N_ELEMENTS (values));
+  ar_conf_set_integer_list (game_name, "Statistic", values, G_N_ELEMENTS (values));
+
+  g_free (game_name);
 #endif /* HAVE_GNOME */
 }
diff --git a/src/conf.h b/src/conf.h
index 8a28866..c1d911e 100644
--- a/src/conf.h
+++ b/src/conf.h
@@ -48,14 +48,14 @@ void aisleriot_conf_shutdown (void);
 
 const char *aisleriot_conf_get_key (AisleriotConfKey key);
 
-gboolean aisleriot_conf_get_options (const char *game_file, int *options);
+gboolean aisleriot_conf_get_options (const char *game_module, int *options);
 
-void aisleriot_conf_set_options (const char *game_file, int options);
+void aisleriot_conf_set_options (const char *game_module, int options);
 
-void aisleriot_conf_get_statistic (const char *game_file,
+void aisleriot_conf_get_statistic (const char *game_module,
                                    AisleriotStatistic * statistic);
 
-void aisleriot_conf_set_statistic (const char *game_file,
+void aisleriot_conf_set_statistic (const char *game_module,
                                    AisleriotStatistic * statistic);
 
 G_END_DECLS
diff --git a/src/game.c b/src/game.c
index 3e995f4..30b0e75 100644
--- a/src/game.c
+++ b/src/game.c
@@ -77,7 +77,7 @@ struct _AisleriotGame
 
   GPtrArray *slots;
 
-  char *game_file;
+  char *game_module;
 
   GRand *rand;
   GRand *saved_rand;
@@ -219,7 +219,7 @@ update_statistics (AisleriotGame *game)
   AisleriotStatistic current_stats;
   time_t t;
 
-  aisleriot_conf_get_statistic (game->game_file, &current_stats);
+  aisleriot_conf_get_statistic (game->game_module, &current_stats);
 
   current_stats.total++;
 
@@ -237,7 +237,7 @@ update_statistics (AisleriotGame *game)
     }
   }
 
-  aisleriot_conf_set_statistic (game->game_file, &current_stats);
+  aisleriot_conf_set_statistic (game->game_module, &current_stats);
 }
 
 static void
@@ -313,7 +313,7 @@ cscmi_exception_get_backtrace (SCM tag, SCM throw_args)
 
   message = g_string_sized_new (1024);
 
-  g_string_append_printf (message, "Variation: %s\n", aisleriot_game_get_game_file (game));
+  g_string_append_printf (message, "Variation: %s\n", aisleriot_game_get_game_module (game));
 #if 0
   g_string_append_printf (message, "Seed: %u\n", game->seed);
 #endif
@@ -1196,7 +1196,7 @@ aisleriot_game_finalize (GObject *object)
   clear_slots (game, FALSE);
   g_ptr_array_free (game->slots, TRUE);
 
-  g_free (game->game_file);
+  g_free (game->game_module);
 
   g_timer_destroy (game->timer);
 
@@ -1229,7 +1229,7 @@ aisleriot_game_get_property (GObject *object,
       g_value_set_boolean (value, game->can_deal);
       break;
     case PROP_GAME_FILE:
-      g_value_set_string (value, game->game_file);
+      g_value_set_string (value, game->game_module);
       break;
     case PROP_SCORE:
       g_value_set_int (value, game->score);
@@ -1656,12 +1656,14 @@ static SCM
 game_scm_load_game (void *user_data)
 {
   AisleriotGame *game = app_game;
-  const char *game_file = user_data;
-  char *path;
+  const char *game_module = user_data;
+  char *game_file, *path;
   int i;
 
   scm_dynwind_begin (0);
 
+  game_file = g_strconcat (game_module, ".scm", NULL);
+  scm_dynwind_unwind_handler (g_free, game_file, SCM_F_WIND_EXPLICITLY);
   path = ar_runtime_get_file (AR_RUNTIME_GAMES_DIRECTORY, game_file);
   scm_dynwind_unwind_handler (g_free, path, SCM_F_WIND_EXPLICITLY);
   scm_c_primitive_load (path);
@@ -1695,33 +1697,33 @@ game_scm_load_game (void *user_data)
 /**
  * aisleriot_game_load_game:
  * @game:
- * @game_file: the game file to load
+ * @game_module: the game module to load
  * @error: a location for a #GError
  *
- * Loads the game @game_file into @game. If there is an error,
+ * Loads the game @game_module into @game. If there is an error,
  * @error is filled in and %FALSE returned.
  *
  * Returns: %TRUE iff loading the game succeeded
  */
 gboolean
 aisleriot_game_load_game (AisleriotGame *game,
-                          const char *game_file,
+                          const char *game_module,
                           GError **error)
 {
   GObject *object = G_OBJECT (game);
   GError *err = NULL;
   int i;
 
-  g_return_val_if_fail (game_file != NULL && game_file[0] != '\0', FALSE);
+  g_return_val_if_fail (game_module != NULL && game_module[0] != '\0', FALSE);
 
   /* FIXMEchpe: allow re-loading */
-  if (g_strcmp0 (game_file, game->game_file) == 0)
+  if (g_strcmp0 (game_module, game->game_module) == 0)
     return TRUE;
 
-  /* We use @game_file as a filename, but since it'll be used in configuration
+  /* We use @game_module as a filename, but since it'll be used in configuration
    * as a key, we also require it to be valid UTF-8 !
    */
-  if (!g_utf8_validate (game_file, -1, NULL)) {
+  if (!g_utf8_validate (game_module, -1, NULL)) {
     g_set_error (error, AISLERIOT_GAME_ERROR, GAME_ERROR_GENERIC,
                  "Invalid UTF-8");
     return FALSE;
@@ -1748,7 +1750,7 @@ aisleriot_game_load_game (AisleriotGame *game,
     game->lambdas[i] = SCM_UNDEFINED;
 
   scm_c_catch (SCM_BOOL_T,
-               (scm_t_catch_body) game_scm_load_game, (void *) game_file,
+               (scm_t_catch_body) game_scm_load_game, (void *) game_module,
                game_scm_catch_handler, NULL,
                game_scm_pre_unwind_handler, &err);
 
@@ -1758,8 +1760,8 @@ aisleriot_game_load_game (AisleriotGame *game,
     return FALSE;
   }
 
-  g_free (game->game_file);
-  game->game_file = g_strdup (game_file);
+  g_free (game->game_module);
+  game->game_module = g_strdup (game_module);
 
   set_game_state (game, GAME_LOADED);
 
@@ -1941,7 +1943,7 @@ aisleriot_game_restart_game (AisleriotGame *game)
 }
 
 /**
- * aisleriot_game_get_game_file:
+ * aisleriot_game_get_game_module:
  * @game:
  *
  * Returns the game filename for the currently loaded game type.
@@ -1949,9 +1951,9 @@ aisleriot_game_restart_game (AisleriotGame *game)
  * Returns: a string owned by @game; you must not modify or free it
  */
 const char *
-aisleriot_game_get_game_file (AisleriotGame *game)
+aisleriot_game_get_game_module (AisleriotGame *game)
 {
-  return game->game_file;
+  return game->game_module;
 }
 
 /**
@@ -1966,11 +1968,11 @@ aisleriot_game_get_game_file (AisleriotGame *game)
 char *
 aisleriot_game_get_name (AisleriotGame *game)
 {
-  return ar_filename_to_display_name (game->game_file);
+  return ar_filename_to_display_name (game->game_module);
 }
 
 /**
- * aisleriot_game_get_game_file:
+ * aisleriot_game_get_game_module:
  * @game:
  * @width: a location to store the width in
  * @height: a location to store the height in
@@ -2219,7 +2221,7 @@ aisleriot_game_get_hint (AisleriotGame *game)
                  "Please file a bug at http://bugzilla.gnome.org "
                  "including this message and the name of the game "
                  "you were playing, which is %s.\n",
-                 aisleriot_game_get_game_file (game));
+                 aisleriot_game_get_game_module (game));
       break;
 
     case 4: /* This is deprecated (due to i18n issues) do not use. */
@@ -2227,7 +2229,7 @@ aisleriot_game_get_hint (AisleriotGame *game)
                  "Please file a bug at http://bugzilla.gnome.org "
                  "including this message and the name of the game "
                  "you were playing, which is %s.\n",
-                 aisleriot_game_get_game_file (game));
+                 aisleriot_game_get_game_module (game));
       break;
 
     default:
diff --git a/src/game.h b/src/game.h
index 8b9b2d7..8143f9f 100644
--- a/src/game.h
+++ b/src/game.h
@@ -103,8 +103,8 @@ char *ar_slot_get_hint_string (ArSlot *slot,
 #define AISLERIOT_IS_GAME_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), AISLERIOT_TYPE_GAME))
 #define AISLERIOT_GAME_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), AISLERIOT_TYPE_GAME, AisleriotGameClass))
 
-#define DEFAULT_VARIATION  "klondike.scm"
-#define FREECELL_VARIATION "freecell.scm"
+#define DEFAULT_VARIATION  "klondike"
+#define FREECELL_VARIATION "freecell"
 
 #define AISLERIOT_GAME_ERROR  (aisleriot_game_error_quark ())
 
@@ -186,7 +186,7 @@ void aisleriot_game_new_game_with_rand (AisleriotGame *game,
 
 void aisleriot_game_restart_game (AisleriotGame * game);
 
-const char *aisleriot_game_get_game_file (AisleriotGame * game);
+const char *aisleriot_game_get_game_module (AisleriotGame * game);
 
 char *aisleriot_game_get_name (AisleriotGame * game);
 
diff --git a/src/lib/ar-string-utils.c b/src/lib/ar-string-utils.c
index b5d8bef..a5d82a9 100644
--- a/src/lib/ar-string-utils.c
+++ b/src/lib/ar-string-utils.c
@@ -96,3 +96,29 @@ ar_filename_to_display_name (const char *filename)
 
   return display_name;
 }
+
+/**
+ * ar_filename_to_game_module:
+ * @game_file: name of a game from command line
+ *
+ * Creates a game module name from a command line argument.
+ *
+ * Returns: a newly allocated string containing the game file name for @game_file
+ */
+char *
+ar_filename_to_game_module (const char *game_file)
+{
+  char *game_module;
+
+  game_module = g_ascii_strdown (game_file, -1);
+
+  /* Replace dangerous characters: '.' (as in ".."), '/' and '\' */
+  g_strdelimit (game_module, "./\\" , '\0');
+  if (game_module[0] == '\0') {
+    g_free (game_module);
+    return NULL;
+  }
+
+  g_strdelimit (game_module, NULL, '-');
+  return game_module;
+}
diff --git a/src/lib/ar-string-utils.h b/src/lib/ar-string-utils.h
index 272ae81..b2da2a9 100644
--- a/src/lib/ar-string-utils.h
+++ b/src/lib/ar-string-utils.h
@@ -25,6 +25,8 @@ G_BEGIN_DECLS
 
 char *ar_filename_to_display_name (const char *filename);
 
+char *ar_filename_to_game_module (const char *game_file);
+
 G_END_DECLS
 
 #endif /* !GAMES_STRING_UTILS_H */
diff --git a/src/sol.c b/src/sol.c
index aad67c0..82aca9d 100644
--- a/src/sol.c
+++ b/src/sol.c
@@ -43,9 +43,10 @@
 #include "eggsmclient.h"
 #endif /* WITH_SMCLIENT */
 
+#include "ar-string-utils.h"
 #include "conf.h"
-#include "game.h"
 #include "window.h"
+#include "game.h"
 #include "util.h"
 
 #if 0
@@ -69,14 +70,11 @@ save_state_cb (EggSMClient *client,
                GKeyFile *key_file,
                AppData *data)
 {
-  AisleriotGame *game;
   char *argv[5];
   const char *game_name;
   int argc = 0;
 
-  game = aisleriot_window_get_game (data->window);
-
-  game_name = aisleriot_game_get_game_file (game);
+  game_name = aisleriot_window_get_game_module (data->window);
 
   argv[argc++] = g_get_prgname ();
 
@@ -170,18 +168,22 @@ main_prog (void *closure, int argc, char *argv[])
   /* If we are asked for a specific game, check that it is valid. */
   if (!data.freecell &&
       data.variation != NULL) {
-    char *game_file = NULL;
+    char *game_module = NULL;
 
     if (data.variation[0] != '\0') {
-      game_file = aisleriot_variation_to_game_file (data.variation);
+      game_module = ar_filename_to_game_module (data.variation);
     }
 
     g_free (data.variation);
-    data.variation = game_file;
+    data.variation = game_module;
   }
 
   if (!data.freecell && !data.variation) {
-    data.variation = ar_conf_get_string_with_default (NULL, aisleriot_conf_get_key (CONF_VARIATION), DEFAULT_VARIATION);
+    char *pref;
+
+    pref = ar_conf_get_string_with_default (NULL, aisleriot_conf_get_key (CONF_VARIATION), DEFAULT_VARIATION);
+    data.variation = ar_filename_to_game_module (pref);
+    g_free (pref);
   }
 
   g_assert (data.variation != NULL || data.freecell);
@@ -203,9 +205,9 @@ main_prog (void *closure, int argc, char *argv[])
 #endif /* WITH_SMCLIENT */
 
   if (data.freecell) {
-    aisleriot_window_set_game (data.window, FREECELL_VARIATION, NULL);
+    aisleriot_window_set_game_module (data.window, FREECELL_VARIATION, NULL);
   } else {
-    aisleriot_window_set_game (data.window, data.variation, NULL);
+    aisleriot_window_set_game_module (data.window, data.variation, NULL);
   }
 
   gtk_window_present (GTK_WINDOW (data.window));
diff --git a/src/util.c b/src/util.c
index 4b9e9a4..70e703f 100644
--- a/src/util.c
+++ b/src/util.c
@@ -31,11 +31,11 @@
 #include "util.h"
 
 static char *
-game_file_to_help_section (const char *game_file)
+game_module_to_help_section (const char *game_module)
 {
   char *p, *buf;
 
-  buf = g_path_get_basename (game_file);
+  buf = g_path_get_basename (game_module);
 
   if ((p = strrchr (buf, '.')))
     *p = '\0';
@@ -60,27 +60,27 @@ game_file_to_help_section (const char *game_file)
 /**
  * aisleriot_show_help:
  * @window: a parent window to use for error messages
- * @game_file: the game to show help for, or %NULL to show
+ * @game_module: the game to show help for, or %NULL to show
  *   general help
  *
- * Shows help for @game_file, or the main help if @game_file is %NULL.
+ * Shows help for @game_module, or the main help if @game_module is %NULL.
  */
 void
 aisleriot_show_help (GtkWidget *window,
-                        const char *game_file)
+                        const char *game_module)
 {
   char *help_section = NULL;
   GError *error = NULL;
 
-  if (game_file != NULL) {
-    help_section = game_file_to_help_section (game_file);
+  if (game_module != NULL) {
+    help_section = game_module_to_help_section (game_module);
   }
 
   if (!ar_help_display_full (GTK_WIDGET (window), DOC_MODULE, help_section, &error)) {
-    if (game_file != NULL) {
+    if (game_module != NULL) {
       char *help_section_display;
 
-      help_section_display = ar_filename_to_display_name (game_file);
+      help_section_display = ar_filename_to_display_name (game_module);
 
       ar_show_error (window, error,
                         _("Could not show help for â%sâ"),
@@ -98,38 +98,6 @@ aisleriot_show_help (GtkWidget *window,
 }
 
 /**
- * aisleriot_variation_to_game_file:
- * @variation: name of a game from command line
- *
- * Creates a game file name from a command line --variation argument.
- * This strips dangerous characters like .. and /.
- *
- * Returns: a newly allocated string containing the game file name for @variation
- */
-char *
-aisleriot_variation_to_game_file (const char *variation)
-{
-  char *game_file, *s;
-
-  game_file = g_ascii_strdown (variation, -1);
-
-  /* Replace dangerous characters: '.' (as in ".."), '/' and '\' */
-  g_strdelimit (game_file, "./\\" , '\0');
-  g_strdelimit (game_file, NULL, '_');
-
-  if (game_file[0] == '\0') {
-    g_free (game_file);
-    return NULL;
-  }
-
-  /* Add the suffix */
-  s = g_strconcat (game_file, ".scm", NULL);
-  g_free (game_file);
-
-  return s;
-}
-
-/**
  * ar_atk_util_add_atk_relation:
  * @widget:
  * @other:
diff --git a/src/util.h b/src/util.h
index b9ca39f..3367741 100644
--- a/src/util.h
+++ b/src/util.h
@@ -25,9 +25,7 @@
 G_BEGIN_DECLS
 
 void aisleriot_show_help (GtkWidget *window,
-                          const char *game_file);
-
-char *aisleriot_variation_to_game_file (const char *variation);
+                          const char *game_module);
 
 void ar_atk_util_add_atk_relation (GtkWidget *widget,
                                    GtkWidget *other,
diff --git a/src/window.c b/src/window.c
index 6539a87..0948848 100644
--- a/src/window.c
+++ b/src/window.c
@@ -286,7 +286,7 @@ update_statistics_display (AisleriotWindow *window)
   aisleriot_stats_dialog_set_name (priv->stats_dialog, game_name);
   g_free (game_name);
 
-  aisleriot_conf_get_statistic (aisleriot_game_get_game_file (priv->game),
+  aisleriot_conf_get_statistic (aisleriot_game_get_game_module (priv->game),
                                 &current_stats);
 
   aisleriot_stats_dialog_update (priv->stats_dialog, &current_stats);
@@ -302,7 +302,7 @@ stats_dialog_response_cb (GtkWidget *widget,
   if (response == GTK_RESPONSE_REJECT) {
     AisleriotStatistic current_stats = { 0, 0, 0, 0 };
 
-    aisleriot_conf_set_statistic (aisleriot_game_get_game_file (priv->game),
+    aisleriot_conf_set_statistic (aisleriot_game_get_game_module (priv->game),
                                   &current_stats);
     aisleriot_stats_dialog_update (priv->stats_dialog, &current_stats);
 
@@ -476,10 +476,10 @@ help_on_game_cb (GtkAction *action,
                  AisleriotWindow *window)
 {
   AisleriotWindowPrivate *priv = window->priv;
-  const char *game_file;
+  const char *game_module;
   
-  game_file = aisleriot_game_get_game_file (priv->game);
-  aisleriot_show_help (GTK_WIDGET (window), game_file);
+  game_module = aisleriot_game_get_game_module (priv->game);
+  aisleriot_show_help (GTK_WIDGET (window), game_module);
 }
 
 static void
@@ -518,8 +518,8 @@ install_themes_cb (GtkAction *action,
   AisleriotWindowPrivate *priv = window->priv;
 
   ar_card_themes_install_themes (priv->theme_manager,
-                                    GTK_WINDOW (window),
-                                    gtk_get_current_event_time ());
+                                 GTK_WIDGET (window),
+                                 gtk_get_current_event_time ());
 }
 
 #ifdef ENABLE_DEBUG_UI
@@ -608,7 +608,7 @@ debug_ensure_game_list (AisleriotWindow *window)
           strcmp (game_file, "api.scm") == 0)
         continue;
 
-      list = g_list_prepend (list, g_strdup (game_file));
+      list = g_list_prepend (list, ar_filename_to_game_module (game_file));
     }
 
     list = g_list_sort (list, (GCompareFunc) strcmp);
@@ -619,7 +619,7 @@ debug_ensure_game_list (AisleriotWindow *window)
   data->window = window;
   data->games_list = list;
   data->current_game = g_list_find_custom (data->games_list,
-                                           aisleriot_game_get_game_file (priv->game),
+                                           aisleriot_game_get_game_module (priv->game),
                                            (GCompareFunc) strcmp);
 
   g_object_set_data_full (G_OBJECT (window), DEBUG_WINDOW_DATA_KEY,
@@ -632,7 +632,7 @@ static gboolean
 debug_cycle_timeout_cb (AisleriotWindow *window)
 {
   DebugWindowData *data;
-  char *game_file;
+  char *game_module;
 
   data = debug_ensure_game_list (window);
   if (data->current_game != NULL) {
@@ -647,8 +647,8 @@ debug_cycle_timeout_cb (AisleriotWindow *window)
   if (!data->current_game)
     return FALSE;
 
-  game_file = data->current_game->data;  
-  aisleriot_window_set_game (data->window, game_file, NULL);
+  game_module = data->current_game->data;  
+  aisleriot_window_set_game_module (data->window, game_module, NULL);
 
   return TRUE;
 }
@@ -671,7 +671,7 @@ debug_game_first (GtkAction *action,
   if (!data->current_game)
     return;
 
-  aisleriot_window_set_game (data->window, (const char *) data->current_game->data, NULL);
+  aisleriot_window_set_game_module (data->window, (const char *) data->current_game->data, NULL);
 }
 
 static void
@@ -685,7 +685,7 @@ debug_game_last (GtkAction *action,
   if (!data->current_game)
     return;
 
-  aisleriot_window_set_game (data->window, (const char *) data->current_game->data, NULL);
+  aisleriot_window_set_game_module (data->window, (const char *) data->current_game->data, NULL);
 }
 
 static void
@@ -704,7 +704,7 @@ debug_game_next (GtkAction *action,
   if (!data->current_game)
     return;
 
-  aisleriot_window_set_game (data->window, (const char *) data->current_game->data, NULL);
+  aisleriot_window_set_game_module (data->window, (const char *) data->current_game->data, NULL);
 }
 
 static void
@@ -723,7 +723,7 @@ debug_game_prev (GtkAction *action,
   if (!data->current_game)
     return;
 
-  aisleriot_window_set_game (data->window, (const char *) data->current_game->data, NULL);
+  aisleriot_window_set_game_module (data->window, (const char *) data->current_game->data, NULL);
 }
 
 static void
@@ -1101,7 +1101,7 @@ option_cb (GtkToggleAction *action,
 
   value = aisleriot_game_change_options (priv->game, changed_mask, changed_value);
 
-  aisleriot_conf_set_options (aisleriot_game_get_game_file (priv->game), (int) value);
+  aisleriot_conf_set_options (aisleriot_game_get_game_module (priv->game), (int) value);
 
   /* Now re-deal, so the option is applied */
   aisleriot_game_new_game (priv->game);
@@ -1132,7 +1132,7 @@ install_options_menu (AisleriotWindow *window)
   /* Only apply the options if they exist. Otherwise the options in the menu
    * and the real game options are out of sync until first changed by the user.
    */
-  if (aisleriot_conf_get_options (aisleriot_game_get_game_file (priv->game), &options_value)) {
+  if (aisleriot_conf_get_options (aisleriot_game_get_game_module (priv->game), &options_value)) {
     aisleriot_game_change_options (priv->game, AISLERIOT_GAME_OPTIONS_MAX, options_value);
   }
 
@@ -1205,13 +1205,13 @@ install_options_menu (AisleriotWindow *window)
  */
 static void
 add_recently_played_game (AisleriotWindow *window,
-                          const char *game_file)
+                          const char *game_module)
 {
   AisleriotWindowPrivate *priv = window->priv;
   char **recent_games, **new_recent;
   gsize i, n_recent = 0, n_new_recent = 0;
 
-  if (!game_file)
+  if (!game_module)
     return;
 
   /* Don't store the game type in freecell mode */
@@ -1222,17 +1222,17 @@ add_recently_played_game (AisleriotWindow *window,
 
   if (recent_games == NULL) {
     new_recent = g_new (char *, 2);
-    new_recent[0] = g_strdup (game_file);
+    new_recent[0] = g_strdup (game_module);
     new_recent[1] = NULL;
     n_new_recent = 1;
   } else {
     new_recent = g_new (char *, MIN (n_recent + 1, MAX_RECENT) + 1);
     n_new_recent = 0;
 
-    new_recent[n_new_recent++] = g_strdup (game_file);
+    new_recent[n_new_recent++] = g_strdup (game_module);
 
     for (i = 0; i < n_recent && n_new_recent < MAX_RECENT; ++i) {
-      if (g_ascii_strcasecmp (game_file, recent_games[i]) != 0) {
+      if (g_ascii_strcasecmp (game_module, recent_games[i]) != 0) {
         new_recent[n_new_recent++] = g_strdup (recent_games[i]);
       }
     }
@@ -1252,14 +1252,14 @@ static void
 recent_game_cb (GtkAction *action,
                 AisleriotWindow *window)
 {
-  const char *game_file;
+  const char *game_module;
 
-  game_file = g_object_get_data (G_OBJECT (action), "game");
-  g_return_if_fail (game_file != NULL);
+  game_module = g_object_get_data (G_OBJECT (action), "game");
+  g_return_if_fail (game_module != NULL);
 
-  aisleriot_window_set_game (window, game_file, NULL);
-  
-  ar_conf_set_string (NULL, aisleriot_conf_get_key (CONF_VARIATION), game_file);
+  aisleriot_window_set_game_module (window, game_module, NULL);
+
+  ar_conf_set_string (NULL, aisleriot_conf_get_key (CONF_VARIATION), game_module);
 }
 
 static void
@@ -1301,7 +1301,8 @@ install_recently_played_menu (AisleriotWindow *window)
     g_free (tooltip);
  
     g_object_set_data_full (G_OBJECT (action), "game",
-                            recent_games[i], (GDestroyNotify) g_free);
+                            ar_filename_to_game_module (recent_games[i]),
+                            (GDestroyNotify) g_free);
     g_signal_connect (action, "activate",
                       G_CALLBACK (recent_game_cb), window);
     gtk_action_group_add_action (priv->recent_games_group, action);
@@ -1627,7 +1628,7 @@ game_type_changed_cb (AisleriotGame *game,
 
   g_free (game_name);
 
-  add_recently_played_game (window, aisleriot_game_get_game_file (game));
+  add_recently_played_game (window, aisleriot_game_get_game_module (game));
 
   install_recently_played_menu (window);
 
@@ -2611,7 +2612,7 @@ aisleriot_window_new (gboolean freecell_mode)
 
 typedef struct {
   AisleriotWindow *window;
-  char *game_file;
+  char *game_module;
   GRand *rand;
 } LoadIdleData;
 
@@ -2621,7 +2622,7 @@ load_error_response_cb (GtkWidget *dialog,
                         AisleriotWindow *window)
 {
   /* Load the default game */
-  aisleriot_window_set_game (window, DEFAULT_VARIATION, NULL);
+  aisleriot_window_set_game_module (window, DEFAULT_VARIATION, NULL);
 
   gtk_widget_destroy (dialog);
 }
@@ -2633,11 +2634,11 @@ load_idle_cb (LoadIdleData *data)
   GError *error = NULL;
   GRand *rand;
 
-  if (!aisleriot_game_load_game (priv->game, data->game_file, &error)) {
+  if (!aisleriot_game_load_game (priv->game, data->game_module, &error)) {
     GtkWidget *dialog;
     char *name;
 
-    name = ar_filename_to_display_name (data->game_file);
+    name = ar_filename_to_display_name (data->game_module);
 
     dialog = gtk_message_dialog_new (GTK_WINDOW (data->window),
                                      GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
@@ -2648,7 +2649,7 @@ load_idle_cb (LoadIdleData *data)
     g_free (name);
 
     if (priv->freecell_mode ||
-        strcmp (data->game_file, DEFAULT_VARIATION) == 0) {
+        strcmp (data->game_module, DEFAULT_VARIATION) == 0) {
       /* Loading freecell/the fallback game failed; all we can do is exit */
       g_signal_connect_swapped (dialog, "response",
                                 G_CALLBACK (gtk_widget_destroy), data->window);
@@ -2679,7 +2680,11 @@ load_idle_cb (LoadIdleData *data)
    * store it in conf, except when we're running in freecell mode.
    */
   if (!priv->freecell_mode) {
-    ar_conf_set_string (NULL, aisleriot_conf_get_key (CONF_VARIATION), data->game_file);
+    char *pref;
+
+    pref = g_strconcat (data->game_module, ".scm", NULL);
+    ar_conf_set_string (NULL, aisleriot_conf_get_key (CONF_VARIATION), pref);
+    g_free (pref);
   }
 
   rand = data->rand;
@@ -2700,24 +2705,24 @@ free_load_idle_data (LoadIdleData *data)
   if (data->rand)
     g_rand_free (data->rand);
 
-  g_free (data->game_file);
+  g_free (data->game_module);
   g_slice_free (LoadIdleData, data);
 }
 
 /**
  * aisleriot_window_set_game:
  * @window:
- * @game_file: a UTF-8 string
+ * @game_module: a UTF-8 string
  * @rand: (allow-none) (transfer full): a #GRand, or %NULL
  *
- * Loads the game variation defined in the @game_file file.
- * Note that even though @game_file is used as a filename,
+ * Loads the game variation defined in the @game_module file.
+ * Note that even though @game_module is used as a filename,
  * it must be in UTF-8!
  */
 void
-aisleriot_window_set_game (AisleriotWindow *window,
-                           const char *game_file,
-                           GRand *rand)
+aisleriot_window_set_game_module (AisleriotWindow *window,
+                                  const char *game_module,
+                                  GRand *rand)
 {
   AisleriotWindowPrivate *priv = window->priv;
   LoadIdleData *data;
@@ -2729,7 +2734,7 @@ aisleriot_window_set_game (AisleriotWindow *window,
 
   data = g_slice_new (LoadIdleData);
   data->window = window;
-  data->game_file = g_strdup (game_file);
+  data->game_module = g_strdup (game_module);
   data->rand = rand; /* adopted */
 
   priv->load_idle_id = g_idle_add_full (G_PRIORITY_LOW,
@@ -2739,15 +2744,15 @@ aisleriot_window_set_game (AisleriotWindow *window,
 }
 
 /**
- * aisleriot_window_get_game:
+ * aisleriot_window_get_game_module:
  * @window:
  *
- * Returns: the #AisleriotGame running in @window
+ * Returns: the name of the game running in @window
  */
-AisleriotGame *
-aisleriot_window_get_game (AisleriotWindow *window)
+const char *
+aisleriot_window_get_game_module (AisleriotWindow *window)
 {
   AisleriotWindowPrivate *priv = window->priv;
 
-  return priv->game;
+  return aisleriot_game_get_game_module (priv->game);
 }
diff --git a/src/window.h b/src/window.h
index b3456cd..e549cee 100644
--- a/src/window.h
+++ b/src/window.h
@@ -47,11 +47,11 @@ GType aisleriot_window_get_type (void);
 
 GtkWidget *aisleriot_window_new (gboolean freecell_mode);
 
-void aisleriot_window_set_game (AisleriotWindow * window,
-                                const char *game_file,
-                                GRand *rand);
+void aisleriot_window_set_game_module (AisleriotWindow * window,
+                                       const char *game_module,
+                                       GRand *rand);
 
-AisleriotGame *aisleriot_window_get_game (AisleriotWindow * window);
+const char *aisleriot_window_get_game_module (AisleriotWindow *window);
 
 G_END_DECLS
 



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