load/save dialogs
[drnoksnes] / gui / plugin.c
1 /*
2 * This file is part of DrNokSnes
3 *
4 * Copyright (C) 2005 INdT - Instituto Nokia de Tecnologia
5 * http://www.indt.org/maemo
6 * Copyright (C) 2009 Javier S. Pedro <maemo@javispedro.com>
7 *
8 * This software is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
12 *
13 * This software is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this software; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 *
23 */
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <gtk/gtk.h>
28 #include <startup_plugin.h>
29 #include <gconf/gconf.h>
30 #include <gconf/gconf-client.h>
31 #include <hildon/hildon-file-chooser-dialog.h>
32 #include <hildon/hildon-banner.h>
33
34 #include "../platform/hgw.h"
35 #include "plugin.h"
36
37 static GtkWidget * load_plugin(void);
38 static void unload_plugin(void);
39 static void write_config(void);
40 static GtkWidget ** load_menu(guint *);
41 static void update_menu(void);
42 static void plugin_callback(GtkWidget * menu_item, gpointer data);
43
44 GConfClient *gcc = NULL;
45 static GameStartupInfo gs;
46 GtkWidget *menu_items[2];
47
48 static StartupPluginInfo plugin_info = {
49         load_plugin,
50         unload_plugin,
51         write_config,
52         load_menu,
53         update_menu,
54         plugin_callback
55 };
56
57 STARTUP_INIT_PLUGIN(plugin_info, gs, FALSE, TRUE)
58
59 char * current_rom_file = 0;
60 static GtkLabel * rom_label;
61
62 static void set_rom(const char * rom_file)
63 {
64         if (!rom_file) return;
65         if (current_rom_file) g_free(current_rom_file);
66
67         current_rom_file = g_strdup(rom_file);
68         gtk_label_set_text(GTK_LABEL(rom_label), rom_file);
69
70         game_state_update();
71         save_clear();
72 }
73
74 static inline GtkWindow* get_parent_window() {
75         return GTK_WINDOW(gs.ui->hildon_appview);
76 }
77
78 static void select_rom_callback(GtkWidget * button, gpointer data)
79 {
80         GtkWidget * dialog;
81         GtkFileFilter * filter;
82         const gchar * current_filename = gtk_label_get_text(rom_label);
83         gchar * filename = NULL;
84
85         filter = gtk_file_filter_new();
86         gtk_file_filter_add_pattern(filter, "*.smc");
87         gtk_file_filter_add_pattern(filter, "*.sfc");
88         gtk_file_filter_add_pattern(filter, "*.fig");
89
90         dialog = hildon_file_chooser_dialog_new_with_properties(
91                 get_parent_window(),
92                 "action", GTK_FILE_CHOOSER_ACTION_OPEN, "filter", filter, NULL);
93
94         if (current_filename && strlen(current_filename) > 1) {
95                 // By default open showing the last selected file
96                 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), 
97                         current_filename);
98         }
99
100         gtk_widget_show_all(GTK_WIDGET(dialog));
101         if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK) {
102                 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
103         }
104
105         gtk_widget_destroy(dialog);
106
107         if (filename) {
108                 set_rom(filename);
109                 g_free(filename);
110         }
111 }
112
113 static GtkWidget * load_plugin(void)
114 {
115         g_type_init();
116         gcc = gconf_client_get_default();
117
118         GtkWidget* parent = gtk_vbox_new(FALSE, HILDON_MARGIN_DEFAULT);
119         GtkWidget* parent_hbox = gtk_hbox_new(FALSE, HILDON_MARGIN_DEFAULT);
120         GtkWidget* selectRomBtn = gtk_button_new_with_label("Select ROM...");
121         rom_label = GTK_LABEL(gtk_label_new(NULL));
122         
123         gtk_widget_set_size_request(GTK_WIDGET(selectRomBtn),
124                                                                 180, 50);
125
126         g_signal_connect(G_OBJECT(selectRomBtn), "clicked",
127                                         G_CALLBACK (select_rom_callback), NULL);
128
129         gtk_box_pack_start(GTK_BOX(parent_hbox), selectRomBtn, FALSE, FALSE, 0);
130         gtk_box_pack_end(GTK_BOX(parent_hbox), GTK_WIDGET(rom_label), TRUE, TRUE, 0);
131         gtk_box_pack_start(GTK_BOX(parent), parent_hbox, FALSE, FALSE, 0);
132
133         // Load current configuration from gconf
134         set_rom(gconf_client_get_string(gcc, kGConfRomFile, NULL));
135
136         return parent;
137 }
138
139 static void unload_plugin(void)
140 {
141         if (current_rom_file) {
142                 g_free(current_rom_file);
143                 current_rom_file = 0;
144         }
145         game_state_clear();
146         save_clear();
147         g_object_unref(gcc);
148 }
149
150 static void write_config(void)
151 {
152         if (current_rom_file) {
153                 gconf_client_set_string(gcc, kGConfRomFile, current_rom_file, NULL);
154         } else {
155                 hildon_banner_show_information(GTK_WIDGET(get_parent_window()), NULL,
156                         "No ROM selected");
157         }
158 }
159
160 static GtkWidget **load_menu(guint *nitems)
161 {
162         *nitems = 0;
163         
164         return NULL;
165 }
166
167 static void update_menu(void)
168 {
169
170 }
171
172 static void plugin_callback(GtkWidget * menu_item, gpointer data)
173 {
174         switch ((gint) data) {
175                 case 20:        // ME_GAME_OPEN
176                         save_load(get_parent_window());
177                         break;
178                 case 21:        // ME_GAME_SAVE
179                         save_save(get_parent_window());
180                         break;
181                 case 22:        // ME_GAME_SAVE_AS
182                         save_save_as(get_parent_window());
183                         break;
184         }
185 }
186