2 * Copyright (C) 2008, 2009, 2010, 2011 Philip Chimento and Marijn van Vliet.
5 * Chimara is free software copyrighted by Philip Chimento and Marijn van Vliet.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
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.
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.
32 #include <sys/types.h>
34 #include <glib-object.h>
35 #include <glib/gi18n.h>
36 #include <glib/gstdio.h>
38 /* Use a custom GSettings backend for our preferences file */
39 #define G_SETTINGS_ENABLE_BACKEND
40 #include <gio/gsettingsbackend.h>
43 #include <libchimara/chimara-if.h>
48 #include "preferences.h"
51 typedef struct _ChimaraAppPrivate {
52 GtkActionGroup *action_group;
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)
58 G_DEFINE_TYPE(ChimaraApp, chimara_app, G_TYPE_OBJECT);
61 chimara_app_finalize(GObject *self)
63 CHIMARA_APP_USE_PRIVATE;
64 g_object_unref(priv->action_group);
67 G_OBJECT_CLASS(chimara_app_parent_class)->finalize(self);
71 chimara_app_class_init(ChimaraAppClass *klass)
73 /* Override methods of parent classes */
74 GObjectClass *object_class = G_OBJECT_CLASS(klass);
75 object_class->finalize = chimara_app_finalize;
78 g_type_class_add_private(klass, sizeof(ChimaraAppPrivate));
82 chimara_app_init(ChimaraApp *self)
84 CHIMARA_APP_USE_PRIVATE;
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"));
93 /* Initialize settings file; it can be overridden by a "chimara-config" file
94 in the current directory */
96 if(g_file_test("chimara-config", G_FILE_TEST_IS_REGULAR))
97 keyfile = g_strdup("chimara-config");
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);
105 /* Build user interface */
106 char *object_ids[] = {
111 GtkBuilder *builder = new_builder_with_objects(object_ids);
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);
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
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);
130 gtk_builder_connect_signals(builder, self);
132 g_object_unref(builder);
135 /* PUBLIC FUNCTIONS */
138 chimara_app_get(void)
140 static ChimaraApp *theapp = NULL;
142 if(G_UNLIKELY(theapp == NULL)) {
143 theapp = CHIMARA_APP(g_object_new(CHIMARA_TYPE_APP, NULL));
145 /* Create one-per-application windows */
146 theapp->prefswindow = chimara_prefs_new();
147 theapp->browser_window = chimara_browser_new();
154 chimara_app_get_action_group(ChimaraApp *self)
156 CHIMARA_APP_USE_PRIVATE;
157 return priv->action_group;
160 /* GLADE CALLBACKS */
162 /* Internal function: See if there is a corresponding graphics file. If so,
163 return its path. If not, return NULL. */
165 search_for_graphics_file(const char *filename)
167 ChimaraApp *theapp = chimara_app_get();
169 /* First get the name of the story file */
170 char *scratch = g_path_get_basename(filename);
171 *(strrchr(scratch, '.')) = '\0';
173 /* Check in the stored resource path, if set */
175 g_settings_get(theapp->prefs_settings, "resource-path", "ms", &resource_path);
177 /* Otherwise check in the current directory */
179 resource_path = g_path_get_dirname(filename);
181 char *blorbfile = g_strconcat(resource_path, "/", scratch, ".blb", NULL);
183 g_free(resource_path);
185 if(g_file_test(blorbfile, G_FILE_TEST_EXISTS))
193 on_open_activate(GtkAction *action, ChimaraApp *theapp)
195 //if(!confirm_open_new_game(CHIMARA_GLK(player->glk)))
198 GtkWidget *dialog = gtk_file_chooser_dialog_new(_("Open Game"),
200 GTK_FILE_CHOOSER_ACTION_OPEN,
201 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
202 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
205 /* Get last opened path */
207 g_settings_get(theapp->state_settings, "last-open-path", "ms", &path);
209 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), path);
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));
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));
222 gchar *blorbfile = search_for_graphics_file(filename);
224 g_object_set(player->glk, "graphics-file", blorbfile, NULL);
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);
230 gtk_widget_destroy(dialog);
234 path = gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(dialog));
236 g_settings_set(theapp->state_settings, "last-open-path", "ms", path);
240 /* Add file to recent files list */
241 GtkRecentManager *manager = gtk_recent_manager_get_default();
244 if(!(uri = g_filename_to_uri(filename, NULL, &error)))
245 g_warning(_("Could not convert filename '%s' to URI: %s"), filename, error->message);
247 if(!gtk_recent_manager_add_item(manager, uri))
248 g_warning(_("Could not add URI '%s' to recent files list."), uri);
253 gtk_widget_destroy(dialog);
257 on_recent_item_activated(GtkRecentChooser *chooser, ChimaraApp *theapp)
259 GError *error = NULL;
260 gchar *uri = gtk_recent_chooser_get_current_uri(chooser);
262 if(!(filename = g_filename_from_uri(uri, NULL, &error))) {
263 error_dialog(NULL /* FIXME */, error, _("Could not open game file '%s': "), uri);
267 //if(!confirm_open_new_game(CHIMARA_GLK(player->glk)))
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));
275 char *blorbfile = search_for_graphics_file(filename);
277 g_object_set(player->glk, "graphics-file", blorbfile, NULL);
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);
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);
297 on_quit_chimara_activate(GtkAction *action, ChimaraApp *theapp)
303 on_preferences_activate(GtkAction *action, ChimaraApp *theapp)
305 gtk_window_present(GTK_WINDOW(theapp->prefswindow));
309 on_about_activate(GtkAction *action, ChimaraApp *theapp)
311 gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(theapp->aboutwindow), PACKAGE_VERSION);
312 gtk_window_present(GTK_WINDOW(theapp->aboutwindow));