ChimaraPrefs class, split up callbacks.c
[projects/chimara/chimara.git] / player / app.c
index 2efe755bbe54974dc405561e5c76ebabf8c4794c..988c08302e9527a3a23c75e1a2a07569fe4626fb 100644 (file)
@@ -8,9 +8,12 @@
 #define G_SETTINGS_ENABLE_BACKEND
 #include <gio/gsettingsbackend.h>
 
+#include <config.h>
+#include <libchimara/chimara-if.h>
 #include "app.h"
 #include "error.h"
 #include "preferences.h"
+#include "player.h"
 
 typedef struct _ChimaraAppPrivate {
        GtkActionGroup *action_group;
@@ -89,10 +92,6 @@ chimara_app_init(ChimaraApp *self)
        char *object_ids[] = {
                "app_group",
                "aboutwindow",
-               "prefswindow",
-               "available_interpreters",
-               "interpreters",
-               "style-list",
                NULL
        };
        
@@ -110,7 +109,6 @@ chimara_app_init(ChimaraApp *self)
        }
 
        self->aboutwindow = GTK_WIDGET(load_object(builder, "aboutwindow"));
-       self->prefswindow = GTK_WIDGET(load_object(builder, "prefswindow"));
        priv->action_group = GTK_ACTION_GROUP(load_object(builder, "app_group"));
        g_object_ref(priv->action_group);
 
@@ -127,9 +125,6 @@ chimara_app_init(ChimaraApp *self)
        GtkRecentChooser *recent = GTK_RECENT_CHOOSER(load_object(builder, "recent"));
        gtk_recent_chooser_add_filter(recent, filter);
 
-       /* Create preferences window */
-       preferences_create(self, builder);
-
        gtk_builder_connect_signals(builder, self);
 
        g_object_unref(builder);
@@ -142,9 +137,13 @@ chimara_app_get(void)
 {
     static ChimaraApp *theapp = NULL;
 
-    if(G_UNLIKELY(theapp == NULL))
+    if(G_UNLIKELY(theapp == NULL)) {
                theapp = CHIMARA_APP(g_object_new(CHIMARA_TYPE_APP, NULL));
 
+               /* Create preferences window */
+               theapp->prefswindow = chimara_prefs_new();
+       }
+
        return theapp;
 }
 
@@ -155,3 +154,158 @@ chimara_app_get_action_group(ChimaraApp *self)
        return priv->action_group;
 }
 
+/* GLADE CALLBACKS */
+
+/* Internal function: See if there is a corresponding graphics file. If so,
+return its path. If not, return NULL. */
+static char *
+search_for_graphics_file(const char *filename)
+{
+       ChimaraApp *theapp = chimara_app_get();
+
+       /* 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(theapp->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);
+       g_free(scratch);
+       g_free(resource_path);
+
+       if(g_file_test(blorbfile, G_FILE_TEST_EXISTS))
+               return blorbfile;
+
+       g_free(blorbfile);
+       return NULL;
+}
+
+void
+on_open_activate(GtkAction *action, ChimaraApp *theapp)
+{
+       //if(!confirm_open_new_game(CHIMARA_GLK(player->glk)))
+       //      return;
+
+       GtkWidget *dialog = gtk_file_chooser_dialog_new(_("Open Game"),
+           NULL, // FIXME
+           GTK_FILE_CHOOSER_ACTION_OPEN,
+           GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+           GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
+           NULL);
+
+       /* Get last opened path */
+       gchar *path;
+       g_settings_get(theapp->state_settings, "last-open-path", "ms", &path);
+       if(path) {
+               gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), path);
+               g_free(path);
+       }
+
+       if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
+               GError *error = NULL;
+               char *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
+
+               /* Open a new player window */
+               ChimaraPlayer *player = CHIMARA_PLAYER(chimara_player_new());
+               gtk_widget_show_all(GTK_WIDGET(player));
+               gtk_window_present(GTK_WINDOW(player));
+
+               gchar *blorbfile = search_for_graphics_file(filename);
+               if(blorbfile) {
+                       g_object_set(player->glk, "graphics-file", blorbfile, NULL);
+                       g_free(blorbfile);
+               }
+               if(!chimara_if_run_game(CHIMARA_IF(player->glk), filename, &error)) {
+                       error_dialog(GTK_WINDOW(player), error, _("Could not open game file '%s': "), filename);
+                       g_free(filename);
+                       gtk_widget_destroy(dialog);
+                       return;
+               }
+               
+               path = gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(dialog));
+               if(path) {
+                       g_settings_set(theapp->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;
+               
+               if(!(uri = g_filename_to_uri(filename, NULL, &error)))
+                       g_warning(_("Could not convert filename '%s' to URI: %s"), filename, error->message);
+               else {
+                       if(!gtk_recent_manager_add_item(manager, uri))
+                               g_warning(_("Could not add URI '%s' to recent files list."), uri);
+                       g_free(uri);
+               }
+               g_free(filename);
+       }
+       gtk_widget_destroy(dialog);
+}
+
+void
+on_recent_item_activated(GtkRecentChooser *chooser, ChimaraApp *theapp)
+{
+       GError *error = NULL;
+       gchar *uri = gtk_recent_chooser_get_current_uri(chooser);
+       gchar *filename;
+       if(!(filename = g_filename_from_uri(uri, NULL, &error))) {
+               error_dialog(NULL /* FIXME */, error, _("Could not open game file '%s': "), uri);
+               goto finally;
+       }
+       
+       //if(!confirm_open_new_game(CHIMARA_GLK(player->glk)))
+       //      goto finally2;
+
+       /* Open a new player window */
+       ChimaraPlayer *player = CHIMARA_PLAYER(chimara_player_new());
+       gtk_widget_show_all(GTK_WIDGET(player));
+       gtk_window_present(GTK_WINDOW(player));
+       
+       char *blorbfile = search_for_graphics_file(filename);
+       if(blorbfile) {
+               g_object_set(player->glk, "graphics-file", blorbfile, NULL);
+               g_free(blorbfile);
+       }
+       if(!chimara_if_run_game(CHIMARA_IF(player->glk), filename, &error)) {
+               error_dialog(GTK_WINDOW(player), error, _("Could not open game file '%s': "), filename);
+               goto finally2;
+       }
+       
+       /* 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);
+}
+
+void 
+on_quit_chimara_activate(GtkAction *action, ChimaraApp *theapp)
+{
+       gtk_main_quit();
+}
+
+void
+on_preferences_activate(GtkAction *action, ChimaraApp *theapp)
+{
+       gtk_window_present(GTK_WINDOW(theapp->prefswindow));
+}
+
+void
+on_about_activate(GtkAction *action, ChimaraApp *theapp)
+{
+       gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(theapp->aboutwindow), PACKAGE_VERSION);
+       gtk_window_present(GTK_WINDOW(theapp->aboutwindow));
+}
+