]> err.no Git - mapper/blob - src/file.c
More map widget integration changes
[mapper] / src / file.c
1 /*
2  * This file is part of mapper
3  *
4  * Copyright (C) 2007 Kaj-Michael Lang
5  * Copyright (C) 2006-2007 John Costigan.
6  *
7  * POI and GPS-Info code originally written by Cezary Jackiewicz.
8  *
9  * Default map data provided by http://www.openstreetmap.org/
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License along
22  * with this program; if not, write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #include <config.h>
27 #include <gtk/gtk.h>
28 #include <libgnomevfs/gnome-vfs.h>
29
30 #include "hildon-mapper.h"
31
32 #include "path.h"
33 #include "utils.h"
34 #include "mapper-types.h"
35 #include "settings.h"
36 #include "ui-common.h"
37 #include "hildon-wrappers.h"
38 #include "file.h"
39
40 /**
41  * Open a file for reading.
42  */
43 gboolean
44 file_open(gchar **dir, gchar **new_file)
45 {
46 GtkWidget *dialog;
47 gboolean success=FALSE;
48 GtkFileFilter *filter;
49
50 #ifdef WITH_HILDON
51 dialog = hildon_file_chooser_dialog_new(GTK_WINDOW(_window), GTK_FILE_CHOOSER_ACTION_OPEN);
52 #else
53 dialog = gtk_file_chooser_dialog_new("Open...", GTK_WINDOW(_window), GTK_FILE_CHOOSER_ACTION_OPEN,
54                                                 GTK_STOCK_CANCEL,
55                                                 GTK_RESPONSE_CANCEL,
56                                                 GTK_STOCK_OPEN,
57                                                 GTK_RESPONSE_OK, NULL);
58 #endif
59
60 /* Show .gpx files only */
61 filter=gtk_file_filter_new();
62 gtk_file_filter_add_pattern(filter, "*.gpx");
63 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
64
65 if (dir && *dir) {
66         gtk_file_chooser_set_current_folder_uri(GTK_FILE_CHOOSER(dialog), *dir);
67 } else {
68         const gchar *home;
69
70         home=g_get_home_dir();
71         if (home)
72                 gtk_file_chooser_set_current_folder_uri(GTK_FILE_CHOOSER(dialog), home);
73 }
74
75 *new_file=NULL;
76
77 if (gtk_dialog_run(GTK_DIALOG(dialog))==GTK_RESPONSE_OK) {
78                 /* Get the selected filename. */
79                 *new_file = gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(dialog));
80                 success=TRUE;
81 }
82
83 gtk_widget_destroy(dialog);
84 return success;
85 }
86
87 /**
88  * Open a file for writing. Return a GnomeVFSHandle to the file for writing.
89  */
90 gboolean
91 file_save(gchar **dir, gchar **file, GnomeVFSHandle **vfs_handle)
92 {
93 GtkWidget *dialog;
94 gboolean success=FALSE;
95
96 #ifdef WITH_HILDON
97 dialog = hildon_file_chooser_dialog_new(GTK_WINDOW(_window), GTK_FILE_CHOOSER_ACTION_SAVE);
98 #else
99 dialog = gtk_file_chooser_dialog_new("Save...", GTK_WINDOW(_window), GTK_FILE_CHOOSER_ACTION_SAVE,
100                                                      GTK_STOCK_CANCEL,
101                                                      GTK_RESPONSE_CANCEL,
102                                                      GTK_STOCK_SAVE,
103                                                      GTK_RESPONSE_OK, NULL);
104 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
105 #endif
106
107 if (dir && *dir) {
108         gtk_file_chooser_set_current_folder_uri(GTK_FILE_CHOOSER(dialog), *dir);
109 } else {
110         const gchar *home;
111
112         home=g_get_home_dir();
113         if (home)
114                 gtk_file_chooser_set_current_folder_uri(GTK_FILE_CHOOSER(dialog), home);
115 }
116
117 if (file && *file) {
118         gtk_file_chooser_set_uri(GTK_FILE_CHOOSER(dialog), *file);
119         if (strlen(*file)==0)
120                 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "track.gpx");
121 } else
122         gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "track.gpx");
123
124 if (gtk_dialog_run(GTK_DIALOG(dialog))==GTK_RESPONSE_OK) {
125         gchar *file_uri_str;
126         GnomeVFSResult vfs_result;
127
128         file_uri_str = gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(dialog));
129         vfs_result = gnome_vfs_create(vfs_handle, file_uri_str, GNOME_VFS_OPEN_WRITE, FALSE, 0664);
130         g_free(file_uri_str);
131
132         if (vfs_result!=GNOME_VFS_OK) {
133                 success=FALSE;
134         } else {
135                 if (dir) {
136                         g_free(*dir);
137                         *dir = gtk_file_chooser_get_current_folder_uri(GTK_FILE_CHOOSER(dialog));
138                 }
139                 if (file) {
140                         g_free(*file);
141                         *file = gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(dialog));
142                 }
143                 success=TRUE;
144         }
145 }
146
147 gtk_widget_destroy(dialog);
148 return success;
149 }
150
151 gboolean
152 file_open_get_contents(gchar **dir, gchar **buffer, guint *bytes)
153 {
154 gchar *file_uri_str;
155 GnomeVFSResult vfs_result;
156
157 if (!file_open(dir, &file_uri_str)) {
158         buffer=NULL;
159         return FALSE;
160 }
161
162 vfs_result=gnome_vfs_read_entire_file(file_uri_str, bytes, buffer);
163 if (vfs_result!=GNOME_VFS_OK)
164         return FALSE;
165
166 return TRUE;
167 }