Open game as a method of ChimaraApp
[projects/chimara/chimara.git] / player / app.c
index a9cf6746e8e9944300de51eed44ee144143d14b6..14c457ef91c873d76780c063d8fcf8e326c2e918 100644 (file)
@@ -1,3 +1,34 @@
+/*
+ * Copyright (C) 2008, 2009, 2010, 2011 Philip Chimento and Marijn van Vliet.
+ * All rights reserved.
+ *
+ * Chimara is free software copyrighted by Philip Chimento and Marijn van Vliet.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ *    this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ *    this list of conditions and the following disclaimer in the documentation
+ *    and/or other materials provided with the distribution.
+ * 3. Neither of the names Philip Chimento or Marijn van Vliet, nor the name of
+ *    any other contributor may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <glib-object.h>
 #include "app.h"
 #include "browser.h"
 #include "error.h"
-#include "preferences.h"
 #include "player.h"
+#include "preferences.h"
+#include "util.h"
 
 typedef struct _ChimaraAppPrivate {
+       /* Action group containing "application actions" */
        GtkActionGroup *action_group;
+       /* List of currently opened player windows */
+       GSList *window_list;
 } ChimaraAppPrivate;
 
 #define CHIMARA_APP_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), CHIMARA_TYPE_APP, ChimaraAppPrivate))
@@ -30,6 +65,7 @@ chimara_app_finalize(GObject *self)
 {
        CHIMARA_APP_USE_PRIVATE;
        g_object_unref(priv->action_group);
+       g_slist_free(priv->window_list);
        
        /* Chain up */
        G_OBJECT_CLASS(chimara_app_parent_class)->finalize(self);
@@ -40,34 +76,16 @@ chimara_app_class_init(ChimaraAppClass *klass)
 {
        /* Override methods of parent classes */
        GObjectClass *object_class = G_OBJECT_CLASS(klass);
-       //object_class->set_property = chimara_if_set_property;
-       //object_class->get_property = chimara_if_get_property;
        object_class->finalize = chimara_app_finalize;
-       
-       /* Signals */
-
-       /* Properties */
 
        /* Private data */
        g_type_class_add_private(klass, sizeof(ChimaraAppPrivate));
 }
 
-static GObject *
-load_object(GtkBuilder *builder, const gchar *name)
-{
-       GObject *retval;
-       if( (retval = gtk_builder_get_object(builder, name)) == NULL) {
-               error_dialog(NULL, NULL, "Error while getting object '%s'", name);
-               g_error("Error while getting object '%s'", name);
-       }
-       return retval;
-}
-
 static void
 chimara_app_init(ChimaraApp *self)
 {
        CHIMARA_APP_USE_PRIVATE;
-       GError *error = NULL;
 
        /* Create configuration dir ~/.chimara */
        gchar *configdir = g_build_filename(g_get_home_dir(), ".chimara", NULL);
@@ -89,25 +107,12 @@ chimara_app_init(ChimaraApp *self)
        g_free(keyfile);
 
        /* Build user interface */
-       GtkBuilder *builder = gtk_builder_new();
        char *object_ids[] = {
                "app_group",
                "aboutwindow",
                NULL
        };
-       
-       if( !gtk_builder_add_objects_from_file(builder, PACKAGE_DATA_DIR "/chimara.ui", object_ids, &error) ) {
-#ifdef DEBUG
-               g_error_free(error);
-               error = NULL;
-               if( !gtk_builder_add_objects_from_file(builder, PACKAGE_SRC_DIR "/chimara.ui", object_ids, &error) ) {
-#endif /* DEBUG */
-                       error_dialog(NULL, error, "Error while building interface: ");  
-                       return;
-#ifdef DEBUG
-               }
-#endif /* DEBUG */
-       }
+       GtkBuilder *builder = new_builder_with_objects(object_ids);
 
        self->aboutwindow = GTK_WIDGET(load_object(builder, "aboutwindow"));
        priv->action_group = GTK_ACTION_GROUP(load_object(builder, "app_group"));
@@ -156,17 +161,15 @@ 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)
+search_for_graphics_file(const char *path)
 {
        ChimaraApp *theapp = chimara_app_get();
 
        /* First get the name of the story file */
-       char *scratch = g_path_get_basename(filename);
+       char *scratch = g_path_get_basename(path);
        *(strrchr(scratch, '.')) = '\0';
 
        /* Check in the stored resource path, if set */
@@ -175,7 +178,7 @@ search_for_graphics_file(const char *filename)
 
        /* Otherwise check in the current directory */
        if(!resource_path)
-               resource_path = g_path_get_dirname(filename);
+               resource_path = g_path_get_dirname(path);
 
        char *blorbfile = g_strconcat(resource_path, "/", scratch, ".blb", NULL);
        g_free(scratch);
@@ -188,6 +191,46 @@ search_for_graphics_file(const char *filename)
        return NULL;
 }
 
+/* Remove a deleted player window from the list of currently opened windows */
+static gboolean
+on_player_delete_event(GtkWidget *player, GdkEvent *event, ChimaraApp *self)
+{
+       CHIMARA_APP_USE_PRIVATE;
+       priv->window_list = g_slist_remove(priv->window_list, player);
+       return FALSE; /* don't block event */
+}
+
+ChimaraPlayer *
+chimara_app_open_game(ChimaraApp *self, const char *path)
+{
+       CHIMARA_APP_USE_PRIVATE;
+       GError *error = NULL;
+
+       /* 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(path);
+       if(blorbfile) {
+               g_object_set(player->glk, "graphics-file", blorbfile, NULL);
+               g_free(blorbfile);
+       }
+       if(!chimara_if_run_game(CHIMARA_IF(player->glk), path, &error)) {
+               error_dialog(GTK_WINDOW(player), error, _("Could not open game file '%s': "), path);
+               gtk_widget_destroy(GTK_WIDGET(player));
+               return NULL;
+       }
+
+       /* Add the opened game to the list of currently opened windows */
+       priv->window_list = g_slist_prepend(priv->window_list, player);
+       g_signal_connect_after(player, "delete-event", G_CALLBACK(on_player_delete_event), self);
+
+       return player;
+}
+
+/* GLADE CALLBACKS */
+
 void
 on_open_activate(GtkAction *action, ChimaraApp *theapp)
 {
@@ -195,7 +238,7 @@ on_open_activate(GtkAction *action, ChimaraApp *theapp)
        //      return;
 
        GtkWidget *dialog = gtk_file_chooser_dialog_new(_("Open Game"),
-           NULL, // FIXME
+           GTK_WINDOW(theapp->browser_window),
            GTK_FILE_CHOOSER_ACTION_OPEN,
            GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
            GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
@@ -209,46 +252,36 @@ on_open_activate(GtkAction *action, ChimaraApp *theapp)
                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);
+       if(gtk_dialog_run(GTK_DIALOG(dialog)) != GTK_RESPONSE_ACCEPT)
+               goto finally;
+
+       GError *error = NULL;
+       char *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
+
+       if(!chimara_app_open_game(theapp, filename))
+               goto finally2;
+
+       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);
+       }
+
+finally2:
+       g_free(filename);
+finally:
        gtk_widget_destroy(dialog);
 }
 
@@ -259,27 +292,15 @@ on_recent_item_activated(GtkRecentChooser *chooser, ChimaraApp *theapp)
        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);
+               error_dialog(GTK_WINDOW(theapp->browser_window), 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);
+       if(!chimara_app_open_game(theapp, filename))
                goto finally2;
-       }
        
        /* Add file to recent files list again, this updates it to most recently used */
        GtkRecentManager *manager = gtk_recent_manager_get_default();