Add preference for graphics file directory
authorPhilip Chimento <philip.chimento@gmail.com>
Sun, 6 Mar 2011 21:43:51 +0000 (22:43 +0100)
committerPhilip Chimento <philip.chimento@gmail.com>
Sun, 6 Mar 2011 21:43:51 +0000 (22:43 +0100)
You can now set a preference for the default directory in which to look
for graphics resource files for older Infocom games. Unfortnately, these
resource files still mysteriously don't work, but it's a start.

player/callbacks.c
player/chimara.ui
player/preferences.c

index 229ff0cbcfe9b92f651aa4421220e108954f7fea..a4cc173593c8a1185ecf272519fad2d4e71c390f 100644 (file)
@@ -70,7 +70,35 @@ confirm_open_new_game(ChimaraGlk *glk)
        return TRUE;
 }
 
-void 
+/* Internal function: See if there is a corresponding graphics file */
+static void
+search_for_graphics_file(const char *filename, ChimaraIF *glk)
+{
+
+       extern GSettings *prefs_settings;
+
+       /* First get the name of the story file */
+       char *scratch = g_path_get_basename(filename);
+       *(strrchr(scratch, '.')) = '\0';
+
+       /* Check in the stored resource path, if set */
+       char *resource_path;
+       g_settings_get(prefs_settings, "resource-path", "ms", &resource_path);
+
+       /* Otherwise check in the current directory */
+       if(!resource_path)
+               resource_path = g_path_get_dirname(filename);
+
+       char *blorbfile = g_strconcat(resource_path, "/", scratch, ".blb", NULL);
+       if(g_file_test(blorbfile, G_FILE_TEST_EXISTS))
+               g_object_set(glk, "graphics-file", blorbfile, NULL);
+
+       g_free(blorbfile);
+       g_free(scratch);
+       g_free(resource_path);
+}
+
+void
 on_open_activate(GtkAction *action, ChimaraGlk *glk) 
 {
        GtkWindow *window = GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(glk)));
@@ -96,27 +124,10 @@ on_open_activate(GtkAction *action, ChimaraGlk *glk)
 
        if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
                GError *error = NULL;
-               gchar *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
-
-               /* See if there is a corresponding graphics file */
-               /* FIXME: hardcoded path */
-               gchar *path = g_path_get_dirname(filename);
-               gchar *scratch = g_path_get_basename(filename);
-               *(strrchr(scratch, '.')) = '\0';
-               gchar *blorbfile = g_strconcat(path, "/../Resources/", scratch, ".blb", NULL);
-               if(g_file_test(blorbfile, G_FILE_TEST_EXISTS)) {
-                       g_object_set(glk, "graphics-file", blorbfile, NULL);
-                       g_printerr("Setting graphics file to %s\n", blorbfile);
-               }
-               g_free(blorbfile);
-               g_free(path);
-               g_free(scratch);
+               extern GSettings *prefs_settings;
+               char *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
 
-               path = gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(dialog));
-               if(path) {
-                       g_settings_set(state_settings, "last-open-path", "ms", path);
-                       g_free(path);
-               }
+               search_for_graphics_file(filename, CHIMARA_IF(glk));
                if(!chimara_if_run_game(CHIMARA_IF(glk), filename, &error)) {
                        error_dialog(window, error, _("Could not open game file '%s': "), filename);
                        g_free(filename);
@@ -124,6 +135,12 @@ on_open_activate(GtkAction *action, ChimaraGlk *glk)
                        return;
                }
                
+               path = gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(dialog));
+               if(path) {
+                       g_settings_set(state_settings, "last-open-path", "ms", path);
+                       g_free(path);
+               }
+
                /* Add file to recent files list */
                GtkRecentManager *manager = gtk_recent_manager_get_default();
                gchar *uri;
@@ -149,29 +166,26 @@ on_recent_item_activated(GtkRecentChooser *chooser, ChimaraGlk *glk)
        gchar *filename;
        if(!(filename = g_filename_from_uri(uri, NULL, &error))) {
                error_dialog(window, error, _("Could not open game file '%s': "), uri);
-               g_free(uri);
-               return;
+               goto finally;
        }
        
-       if(!confirm_open_new_game(glk)) {
-               g_free(filename);
-               g_free(uri);
-               return;
-       }
+       if(!confirm_open_new_game(glk))
+               goto finally2;
        
+       search_for_graphics_file(filename, CHIMARA_IF(glk));
        if(!chimara_if_run_game(CHIMARA_IF(glk), filename, &error)) {
                error_dialog(window, error, _("Could not open game file '%s': "), filename);
-               g_free(filename);
-               g_free(uri);
-               return;
+               goto finally2;
        }
-       g_free(filename);
        
        /* Add file to recent files list again, this updates it to most recently used */
        GtkRecentManager *manager = gtk_recent_manager_get_default();
        if(!gtk_recent_manager_add_item(manager, uri))
                g_warning(_("Could not add URI '%s' to recent files list."), uri);
-       
+
+finally2:
+       g_free(filename);
+finally:
        g_free(uri);
 }
 
index 7a60d05cee78fef08e289a69a554ba574907db6d..44fca73f1eb2ed1173309fa663311d166ff83a5d 100644 (file)
@@ -842,6 +842,7 @@ Philip Chimento</property>
                         <property name="can_focus">False</property>
                         <property name="tooltip_text" translatable="yes">Where to look for Blorb resource files containing graphics and sound; normally, these are part of the story file itself, but they are provided as separate files in some older games.</property>
                         <property name="action">select-folder</property>
+                        <signal name="file-set" handler="on_resource_file_set" swapped="no"/>
                       </object>
                       <packing>
                         <property name="expand">True</property>
index 157a784c3d3cacce1065361364b654da8f2e5785..f2cbaf085973c09c1e24bfed7b1a33bc8c10656f 100644 (file)
@@ -91,6 +91,13 @@ preferences_create(ChimaraGlk *glk)
        extern GSettings *prefs_settings;
        GObject *flep = G_OBJECT( load_object("flep") );
        g_settings_bind(prefs_settings, "flep", flep, "active", G_SETTINGS_BIND_DEFAULT);
+       GtkFileChooser *blorb_chooser = GTK_FILE_CHOOSER( load_object("blorb_file_chooser") );
+       char *filename;
+       g_settings_get(prefs_settings, "resource-path", "ms", &filename);
+       if(filename) {
+               gtk_file_chooser_set_filename(blorb_chooser, filename);
+               g_free(filename);
+       }
 }
 
 static void
@@ -240,3 +247,12 @@ on_font_set(GtkFontButton *button, ChimaraGlk *glk)
        g_object_set(current_tag, "font-desc", font_description, NULL);
        chimara_glk_update_style(glk);
 }
+
+void
+on_resource_file_set(GtkFileChooserButton *button, ChimaraGlk *glk)
+{
+       extern GSettings *prefs_settings;
+       char *filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(button) );
+       g_settings_set(prefs_settings, "resource-path", "ms", filename);
+       g_free(filename);
+}