workaround a problem with the harmattan gcc
[drnoksnes] / gui / save.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 <string.h>
26 #include <glib.h>
27 #include <libgnomevfs/gnome-vfs-uri.h>
28 #include <libgnomevfs/gnome-vfs-utils.h>
29 #include <libgnomevfs/gnome-vfs-ops.h>
30 #include <libgnomevfs/gnome-vfs-xfer.h>
31 #include <hildon/hildon-file-chooser-dialog.h>
32 #include <hildon/hildon-banner.h>
33
34 #include "plugin.h"
35
36 static gchar * cur_save_uri = NULL;
37
38 // Pulling nearly all of gnomevfs just to copy a file. *sigh*.
39 static GnomeVFSResult copy_file(const char * source_uri, const char * dest_uri)
40 {
41         GnomeVFSURI* src = gnome_vfs_uri_new(source_uri);
42         GnomeVFSURI* dst = gnome_vfs_uri_new(dest_uri);
43         GnomeVFSResult res;
44
45         res = gnome_vfs_xfer_uri(src, dst,
46                         GNOME_VFS_XFER_TARGET_DEFAULT_PERMS,
47                         GNOME_VFS_XFER_ERROR_MODE_ABORT,
48                         GNOME_VFS_XFER_OVERWRITE_MODE_REPLACE,
49                         NULL, NULL);
50
51         gnome_vfs_uri_unref(src);
52         gnome_vfs_uri_unref(dst);
53
54         return res;
55 }
56
57 static gboolean show_result(GnomeVFSResult res, GtkWindow* parent, const char* msg)
58 {
59         if (res == GNOME_VFS_OK) {
60                 hildon_banner_show_information(GTK_WIDGET(parent), NULL, msg);
61                 return TRUE;
62         } else {
63                 hildon_banner_show_information(GTK_WIDGET(parent), NULL, "Failed");
64                 return FALSE;
65         }
66 }
67
68 void save_clear()
69 {
70         if (cur_save_uri) {
71                 g_free(cur_save_uri);
72                 cur_save_uri = NULL;
73         }
74 }
75
76 static gchar * show_dialog(GtkWindow* parent, GtkFileChooserAction action)
77 {
78         GtkWidget * dialog;
79         GtkFileFilter * filter;
80         gchar * uri = NULL;
81
82         filter = gtk_file_filter_new();
83         gtk_file_filter_add_pattern(filter, "*.snsg");
84
85         dialog = hildon_file_chooser_dialog_new_with_properties(GTK_WINDOW(parent), 
86                 "action", action, "local-only", FALSE, "filter", filter, NULL);
87
88         hildon_file_chooser_dialog_set_extension(HILDON_FILE_CHOOSER_DIALOG(dialog),
89                 "snsg");
90 #if defined(MAEMO)
91         {
92                 gchar * games_dir = g_strdup_printf("%s/.games", g_getenv("MYDOCSDIR"));
93                 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), games_dir);
94                 g_free(games_dir);
95         }
96 #endif
97
98         gtk_widget_show_all(GTK_WIDGET(dialog));
99         if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK) {
100                 uri = gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(dialog));
101         }
102
103         gtk_widget_destroy(dialog);
104
105         return uri;
106 }
107
108 void save_load(GtkWindow* parent)
109 {
110         gchar * uri = show_dialog(parent, GTK_FILE_CHOOSER_ACTION_OPEN);
111
112         if (uri) {
113                 const gchar * frz_file = game_state_get_frz_file();
114                 gchar * frz_uri = gnome_vfs_get_uri_from_local_path(frz_file);
115                 show_result(copy_file(uri, frz_uri), parent, "Game loaded");
116                 g_free(frz_uri);
117         }
118
119         if (cur_save_uri) {
120                 g_free(cur_save_uri);
121         }
122         cur_save_uri = uri;
123 }
124
125 void save_save(GtkWindow* parent)
126 {
127         if (cur_save_uri) {
128                 const gchar * frz_file = game_state_get_frz_file();
129                 gchar * frz_uri = gnome_vfs_get_uri_from_local_path(frz_file);
130                 show_result(copy_file(frz_uri, cur_save_uri), parent, "Game saved");
131                 g_free(frz_uri);
132         } else {
133                 save_save_as(parent);
134         }
135 }
136
137 void save_save_as(GtkWindow* parent)
138 {
139         gchar * uri = show_dialog(parent, GTK_FILE_CHOOSER_ACTION_SAVE);
140
141         if (uri) {
142                 const gchar * frz_file = game_state_get_frz_file();
143                 gchar * frz_uri = gnome_vfs_get_uri_from_local_path(frz_file);
144                 gboolean res = show_result(copy_file(frz_uri, uri), parent, "Game saved");
145                 g_free(frz_uri);
146
147                 if (!res) return;
148
149                 if (cur_save_uri) {
150                         g_free(cur_save_uri);
151                 }
152                 cur_save_uri = uri;
153         }
154 }
155