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