General code cleanup
[projects/chimara/chimara.git] / player / app.c
1 /*
2  * Copyright (C) 2008, 2009, 2010, 2011 Philip Chimento and Marijn van Vliet.
3  * All rights reserved.
4  *
5  * Chimara is free software copyrighted by Philip Chimento and Marijn van Vliet.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice,
11  *    this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  *    this list of conditions and the following disclaimer in the documentation
14  *    and/or other materials provided with the distribution.
15  * 3. Neither of the names Philip Chimento or Marijn van Vliet, nor the name of
16  *    any other contributor may be used to endorse or promote products derived
17  *    from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <glib-object.h>
35 #include <glib/gi18n.h>
36 #include <glib/gstdio.h>
37
38 /* Use a custom GSettings backend for our preferences file */
39 #define G_SETTINGS_ENABLE_BACKEND
40 #include <gio/gsettingsbackend.h>
41
42 #include <config.h>
43 #include <libchimara/chimara-if.h>
44 #include "app.h"
45 #include "browser.h"
46 #include "error.h"
47 #include "player.h"
48 #include "preferences.h"
49 #include "util.h"
50
51 typedef struct _ChimaraAppPrivate {
52         GtkActionGroup *action_group;
53 } ChimaraAppPrivate;
54
55 #define CHIMARA_APP_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), CHIMARA_TYPE_APP, ChimaraAppPrivate))
56 #define CHIMARA_APP_USE_PRIVATE ChimaraAppPrivate *priv = CHIMARA_APP_PRIVATE(self)
57
58 G_DEFINE_TYPE(ChimaraApp, chimara_app, G_TYPE_OBJECT);
59
60 static void
61 chimara_app_finalize(GObject *self)
62 {
63         CHIMARA_APP_USE_PRIVATE;
64         g_object_unref(priv->action_group);
65         
66         /* Chain up */
67         G_OBJECT_CLASS(chimara_app_parent_class)->finalize(self);
68 }
69
70 static void
71 chimara_app_class_init(ChimaraAppClass *klass)
72 {
73         /* Override methods of parent classes */
74         GObjectClass *object_class = G_OBJECT_CLASS(klass);
75         object_class->finalize = chimara_app_finalize;
76
77         /* Private data */
78         g_type_class_add_private(klass, sizeof(ChimaraAppPrivate));
79 }
80
81 static void
82 chimara_app_init(ChimaraApp *self)
83 {
84         CHIMARA_APP_USE_PRIVATE;
85
86         /* Create configuration dir ~/.chimara */
87         gchar *configdir = g_build_filename(g_get_home_dir(), ".chimara", NULL);
88         if(!g_file_test(configdir, G_FILE_TEST_IS_DIR)
89                 && g_mkdir(configdir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
90                 g_error(_("Cannot create configuration directory ~/.chimara"));
91         g_free(configdir);
92
93         /* Initialize settings file; it can be overridden by a "chimara-config" file
94          in the current directory */
95         gchar *keyfile;
96         if(g_file_test("chimara-config", G_FILE_TEST_IS_REGULAR))
97                 keyfile = g_strdup("chimara-config");
98         else
99                 keyfile = g_build_filename(g_get_home_dir(), ".chimara", "config", NULL);
100         GSettingsBackend *backend = g_keyfile_settings_backend_new(keyfile, "/org/chimara-if/player/", NULL);
101         self->prefs_settings = g_settings_new_with_backend("org.chimara-if.player.preferences", backend);
102         self->state_settings = g_settings_new_with_backend("org.chimara-if.player.state", backend);
103         g_free(keyfile);
104
105         /* Build user interface */
106         char *object_ids[] = {
107                 "app_group",
108                 "aboutwindow",
109                 NULL
110         };
111         GtkBuilder *builder = new_builder_with_objects(object_ids);
112
113         self->aboutwindow = GTK_WIDGET(load_object(builder, "aboutwindow"));
114         priv->action_group = GTK_ACTION_GROUP(load_object(builder, "app_group"));
115         g_object_ref(priv->action_group);
116
117         const gchar **ptr;
118         GtkRecentFilter *filter = gtk_recent_filter_new();
119         /* TODO: Use mimetypes and construct the filter dynamically depending on 
120         what plugins are installed */
121         const gchar *patterns[] = {
122                 "*.z[1-8]", "*.[zg]lb", "*.[zg]blorb", "*.ulx", "*.blb", "*.blorb", NULL
123         };
124
125         for(ptr = patterns; *ptr; ptr++)
126                 gtk_recent_filter_add_pattern(filter, *ptr);
127         GtkRecentChooser *recent = GTK_RECENT_CHOOSER(load_object(builder, "recent"));
128         gtk_recent_chooser_add_filter(recent, filter);
129
130         gtk_builder_connect_signals(builder, self);
131
132         g_object_unref(builder);
133 }
134
135 /* PUBLIC FUNCTIONS */
136
137 ChimaraApp *
138 chimara_app_get(void)
139 {
140     static ChimaraApp *theapp = NULL;
141
142     if(G_UNLIKELY(theapp == NULL)) {
143                 theapp = CHIMARA_APP(g_object_new(CHIMARA_TYPE_APP, NULL));
144
145                 /* Create one-per-application windows */
146                 theapp->prefswindow = chimara_prefs_new();
147                 theapp->browser_window = chimara_browser_new();
148         }
149
150         return theapp;
151 }
152
153 GtkActionGroup *
154 chimara_app_get_action_group(ChimaraApp *self)
155 {
156         CHIMARA_APP_USE_PRIVATE;
157         return priv->action_group;
158 }
159
160 /* GLADE CALLBACKS */
161
162 /* Internal function: See if there is a corresponding graphics file. If so,
163 return its path. If not, return NULL. */
164 static char *
165 search_for_graphics_file(const char *filename)
166 {
167         ChimaraApp *theapp = chimara_app_get();
168
169         /* First get the name of the story file */
170         char *scratch = g_path_get_basename(filename);
171         *(strrchr(scratch, '.')) = '\0';
172
173         /* Check in the stored resource path, if set */
174         char *resource_path;
175         g_settings_get(theapp->prefs_settings, "resource-path", "ms", &resource_path);
176
177         /* Otherwise check in the current directory */
178         if(!resource_path)
179                 resource_path = g_path_get_dirname(filename);
180
181         char *blorbfile = g_strconcat(resource_path, "/", scratch, ".blb", NULL);
182         g_free(scratch);
183         g_free(resource_path);
184
185         if(g_file_test(blorbfile, G_FILE_TEST_EXISTS))
186                 return blorbfile;
187
188         g_free(blorbfile);
189         return NULL;
190 }
191
192 void
193 on_open_activate(GtkAction *action, ChimaraApp *theapp)
194 {
195         //if(!confirm_open_new_game(CHIMARA_GLK(player->glk)))
196         //      return;
197
198         GtkWidget *dialog = gtk_file_chooser_dialog_new(_("Open Game"),
199             NULL, // FIXME
200             GTK_FILE_CHOOSER_ACTION_OPEN,
201             GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
202             GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
203             NULL);
204
205         /* Get last opened path */
206         gchar *path;
207         g_settings_get(theapp->state_settings, "last-open-path", "ms", &path);
208         if(path) {
209                 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), path);
210                 g_free(path);
211         }
212
213         if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
214                 GError *error = NULL;
215                 char *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
216
217                 /* Open a new player window */
218                 ChimaraPlayer *player = CHIMARA_PLAYER(chimara_player_new());
219                 gtk_widget_show_all(GTK_WIDGET(player));
220                 gtk_window_present(GTK_WINDOW(player));
221
222                 gchar *blorbfile = search_for_graphics_file(filename);
223                 if(blorbfile) {
224                         g_object_set(player->glk, "graphics-file", blorbfile, NULL);
225                         g_free(blorbfile);
226                 }
227                 if(!chimara_if_run_game(CHIMARA_IF(player->glk), filename, &error)) {
228                         error_dialog(GTK_WINDOW(player), error, _("Could not open game file '%s': "), filename);
229                         g_free(filename);
230                         gtk_widget_destroy(dialog);
231                         return;
232                 }
233                 
234                 path = gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(dialog));
235                 if(path) {
236                         g_settings_set(theapp->state_settings, "last-open-path", "ms", path);
237                         g_free(path);
238                 }
239
240                 /* Add file to recent files list */
241                 GtkRecentManager *manager = gtk_recent_manager_get_default();
242                 gchar *uri;
243                 
244                 if(!(uri = g_filename_to_uri(filename, NULL, &error)))
245                         g_warning(_("Could not convert filename '%s' to URI: %s"), filename, error->message);
246                 else {
247                         if(!gtk_recent_manager_add_item(manager, uri))
248                                 g_warning(_("Could not add URI '%s' to recent files list."), uri);
249                         g_free(uri);
250                 }
251                 g_free(filename);
252         }
253         gtk_widget_destroy(dialog);
254 }
255
256 void
257 on_recent_item_activated(GtkRecentChooser *chooser, ChimaraApp *theapp)
258 {
259         GError *error = NULL;
260         gchar *uri = gtk_recent_chooser_get_current_uri(chooser);
261         gchar *filename;
262         if(!(filename = g_filename_from_uri(uri, NULL, &error))) {
263                 error_dialog(NULL /* FIXME */, error, _("Could not open game file '%s': "), uri);
264                 goto finally;
265         }
266         
267         //if(!confirm_open_new_game(CHIMARA_GLK(player->glk)))
268         //      goto finally2;
269
270         /* Open a new player window */
271         ChimaraPlayer *player = CHIMARA_PLAYER(chimara_player_new());
272         gtk_widget_show_all(GTK_WIDGET(player));
273         gtk_window_present(GTK_WINDOW(player));
274         
275         char *blorbfile = search_for_graphics_file(filename);
276         if(blorbfile) {
277                 g_object_set(player->glk, "graphics-file", blorbfile, NULL);
278                 g_free(blorbfile);
279         }
280         if(!chimara_if_run_game(CHIMARA_IF(player->glk), filename, &error)) {
281                 error_dialog(GTK_WINDOW(player), error, _("Could not open game file '%s': "), filename);
282                 goto finally2;
283         }
284         
285         /* Add file to recent files list again, this updates it to most recently used */
286         GtkRecentManager *manager = gtk_recent_manager_get_default();
287         if(!gtk_recent_manager_add_item(manager, uri))
288                 g_warning(_("Could not add URI '%s' to recent files list."), uri);
289
290 finally2:
291         g_free(filename);
292 finally:
293         g_free(uri);
294 }
295
296 void 
297 on_quit_chimara_activate(GtkAction *action, ChimaraApp *theapp)
298 {
299         gtk_main_quit();
300 }
301
302 void
303 on_preferences_activate(GtkAction *action, ChimaraApp *theapp)
304 {
305         gtk_window_present(GTK_WINDOW(theapp->prefswindow));
306 }
307
308 void
309 on_about_activate(GtkAction *action, ChimaraApp *theapp)
310 {
311         gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(theapp->aboutwindow), PACKAGE_VERSION);
312         gtk_window_present(GTK_WINDOW(theapp->aboutwindow));
313 }
314