rom file chooser enhancements
[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
33 #include "../platform/hgw.h"
34
35 static GtkWidget * load_plugin(void);
36 static void unload_plugin(void);
37 static void write_config(void);
38 static GtkWidget ** load_menu(guint *);
39 static void update_menu(void);
40 static void plugin_callback(GtkWidget * menu_item, gpointer data);
41
42 GConfClient *gcc = NULL;
43 static GameStartupInfo gs;
44 GtkWidget *menu_items[2];
45
46 static StartupPluginInfo plugin_info = {
47         load_plugin,
48         unload_plugin,
49         write_config,
50         load_menu,
51         update_menu,
52         plugin_callback
53 };
54
55 static const gchar * rom_globs[] = {
56         "*.smc",
57         "*.fig",
58         "*.sfc",
59         NULL
60 };
61
62 STARTUP_INIT_PLUGIN(plugin_info, gs, FALSE, TRUE)
63
64 // Yes, I'm using the label not only to show but also save the current value.
65 static GtkLabel * rom_label;
66
67 static gchar *
68 interface_file_chooser
69 (GtkWindow * parent, GtkFileChooserAction action, const gchar ** extension)
70 {
71         GtkWidget * dialog;
72         GtkFileFilter * filter;
73         const gchar * current_filename;
74         gchar * filename = NULL;
75         int i;
76
77         filter = gtk_file_filter_new();
78         for (i = 0; extension[i]; i++) {
79                 gtk_file_filter_add_pattern(filter, extension[i]);
80         }
81
82         dialog = hildon_file_chooser_dialog_new_with_properties(parent, 
83                 "action", action, "local_only", TRUE, "filter", filter, NULL);
84
85         current_filename = gtk_label_get_text(rom_label);
86         if (current_filename && strlen(current_filename) > 1) {
87                 // By default open showing the last selected file
88                 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), 
89                         current_filename);
90         }
91
92         gtk_widget_show_all(GTK_WIDGET(dialog));
93         if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK) {
94                 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
95         }
96
97         gtk_widget_destroy(dialog);
98
99         return filename;
100 }
101
102 static void select_rom_callback(GtkWidget * button, gpointer data)
103 {
104         gchar * filename = interface_file_chooser(
105                 GTK_WINDOW(gtk_widget_get_parent_window(button)),
106                 GTK_FILE_CHOOSER_ACTION_OPEN,
107                 rom_globs);
108
109         if (!filename) return;
110         
111         gtk_label_set_text(rom_label, filename);
112         
113         g_free(filename);
114 }
115
116 static GtkWidget * load_plugin(void)
117 {
118         g_type_init();
119         gcc = gconf_client_get_default();
120
121         GtkWidget* parent = gtk_vbox_new(FALSE, HILDON_MARGIN_DEFAULT);
122         GtkWidget* parent_hbox = gtk_hbox_new(FALSE, HILDON_MARGIN_DEFAULT);
123         GtkWidget* selectRomBtn = gtk_button_new_with_label("Select ROM...");
124         rom_label = GTK_LABEL(gtk_label_new(NULL));
125         
126         gtk_widget_set_size_request(GTK_WIDGET(selectRomBtn),
127                                                                 180, 50);
128                                                                 
129         gtk_label_set_text(rom_label,
130                                                 gconf_client_get_string(gcc, kGConfRomFile, NULL));
131                                                                 
132
133         g_signal_connect(G_OBJECT(selectRomBtn), "clicked",
134                                         G_CALLBACK (select_rom_callback), NULL);
135
136         gtk_box_pack_start(GTK_BOX(parent_hbox), selectRomBtn, FALSE, FALSE, 0);
137         gtk_box_pack_end(GTK_BOX(parent_hbox), GTK_WIDGET(rom_label), TRUE, TRUE, 0);
138         gtk_box_pack_start(GTK_BOX(parent), parent_hbox, FALSE, FALSE, 0);
139
140         return parent;
141 }
142
143 static void unload_plugin(void)
144 {
145         g_object_unref(gcc);
146 }
147
148 static void write_config(void)
149 {
150         gconf_client_set_string(gcc, kGConfRomFile,
151         gtk_label_get_text(GTK_LABEL(rom_label)), NULL);
152 }
153
154 static GtkWidget **load_menu(guint *nitems)
155 {
156         *nitems = 0;
157         
158         return NULL;
159 }
160
161 static void update_menu(void)
162 {
163
164 }
165
166 static void plugin_callback(GtkWidget * menu_item, gpointer data)
167 {
168
169 }
170