]> err.no Git - mapper/blob - src/file.c
Move some variables and types around
[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 #define _GNU_SOURCE
27
28 #include <config.h>
29 #include <gtk/gtk.h>
30 #include <libgnomevfs/gnome-vfs.h>
31
32 #include "hildon-mapper.h"
33
34 #include "path.h"
35 #include "utils.h"
36 #include "mapper-types.h"
37 #include "settings.h"
38 #include "ui-common.h"
39 #include "hildon-wrappers.h"
40 #include "file.h"
41
42 /**
43  * Open a file for reading.
44  */
45 gboolean
46 file_open(gchar **dir, gchar **new_file)
47 {
48 GtkWidget *dialog;
49 gboolean success=FALSE;
50 GtkFileFilter *filter;
51
52 #ifdef WITH_HILDON
53 dialog = hildon_file_chooser_dialog_new(GTK_WINDOW(_window), GTK_FILE_CHOOSER_ACTION_OPEN);
54 #else
55 dialog = gtk_file_chooser_dialog_new("Open...", GTK_WINDOW(_window), GTK_FILE_CHOOSER_ACTION_OPEN,
56                                                 GTK_STOCK_CANCEL,
57                                                 GTK_RESPONSE_CANCEL,
58                                                 GTK_STOCK_OPEN,
59                                                 GTK_RESPONSE_OK, NULL);
60 #endif
61
62 /* Show .gpx files only */
63 filter=gtk_file_filter_new();
64 gtk_file_filter_add_pattern(filter, "*.gpx");
65 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
66
67 if (dir && *dir) {
68         gtk_file_chooser_set_current_folder_uri(GTK_FILE_CHOOSER(dialog), *dir);
69 } else {
70         gtk_file_chooser_set_current_folder_uri(GTK_FILE_CHOOSER(dialog), g_get_home_dir());
71 }
72
73 *new_file=NULL;
74
75 if (gtk_dialog_run(GTK_DIALOG(dialog))==GTK_RESPONSE_OK) {
76                 /* Get the selected filename. */
77                 *new_file = gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(dialog));
78                 g_printf("file: %s\n", *new_file);
79                 success=TRUE;
80 }
81
82 gtk_widget_destroy(dialog);
83 return success;
84 }
85
86 /**
87  * Open a file for writing. Return a GnomeVFSHandle to the file for writing.
88  */
89 gboolean
90 file_save(gchar **dir, gchar **file, GnomeVFSHandle **vfs_handle)
91 {
92 GtkWidget *dialog;
93 gboolean success=FALSE;
94
95 #ifdef WITH_HILDON
96 dialog = hildon_file_chooser_dialog_new(GTK_WINDOW(_window), GTK_FILE_CHOOSER_ACTION_SAVE);
97 #else
98 dialog = gtk_file_chooser_dialog_new("Save...", GTK_WINDOW(_window), GTK_FILE_CHOOSER_ACTION_SAVE,
99                                                      GTK_STOCK_CANCEL,
100                                                      GTK_RESPONSE_CANCEL,
101                                                      GTK_STOCK_SAVE,
102                                                      GTK_RESPONSE_OK, NULL);
103 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
104 #endif
105
106 if (dir && *dir) {
107         gtk_file_chooser_set_current_folder_uri(GTK_FILE_CHOOSER(dialog), *dir);
108 } else {
109         gtk_file_chooser_set_current_folder_uri(GTK_FILE_CHOOSER(dialog), g_get_home_dir());
110 }
111
112 if (file && *file)
113         gtk_file_chooser_set_uri(GTK_FILE_CHOOSER(dialog), *file);
114 else
115         gtk_file_chooser_set_uri(GTK_FILE_CHOOSER(dialog), "track.gpx");
116
117 if (gtk_dialog_run(GTK_DIALOG(dialog))==GTK_RESPONSE_OK) {
118         gchar *file_uri_str;
119         GnomeVFSResult vfs_result;
120
121         file_uri_str = gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(dialog));
122         vfs_result = gnome_vfs_create(vfs_handle, file_uri_str, GNOME_VFS_OPEN_WRITE, FALSE, 0664);
123         g_free(file_uri_str);
124
125         if (vfs_result!=GNOME_VFS_OK) {
126                 success=FALSE;
127         } else {
128                 if (dir) {
129                         g_free(*dir);
130                         *dir = gtk_file_chooser_get_current_folder_uri(GTK_FILE_CHOOSER(dialog));
131                 }
132                 if (file) {
133                         g_free(*file);
134                         *file = gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(dialog));
135                 }
136                 success=TRUE;
137         }
138 }
139
140 gtk_widget_destroy(dialog);
141 return success;
142 }
143
144 gboolean
145 file_open_get_contents(gchar **dir, gchar **buffer, guint *bytes)
146 {
147 gchar *file_uri_str;
148 GnomeVFSResult vfs_result;
149
150 if (!file_open(dir, &file_uri_str)) {
151         buffer=NULL;
152         return TRUE;
153 }
154
155 vfs_result=gnome_vfs_read_entire_file(file_uri_str, bytes, buffer);
156 if (vfs_result!=GNOME_VFS_OK)
157         return FALSE;
158
159 return TRUE;
160 }