-#define _GNU_SOURCE
+/*
+ * This file is part of mapper
+ *
+ * Copyright (C) 2007 Kaj-Michael Lang
+ * Copyright (C) 2006-2007 John Costigan.
+ *
+ * POI and GPS-Info code originally written by Cezary Jackiewicz.
+ *
+ * Default map data provided by http://www.openstreetmap.org/
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
#include <config.h>
#include <gtk/gtk.h>
#include <libgnomevfs/gnome-vfs.h>
+#include "hildon-mapper.h"
+
+#include "path.h"
#include "utils.h"
#include "mapper-types.h"
-
#include "settings.h"
#include "ui-common.h"
#include "hildon-wrappers.h"
+#include "file.h"
/**
- * This is a multi-purpose function for allowing the user to select a file
- * for either reading or writing. If chooser_action is
- * GTK_FILE_CHOOSER_ACTION_OPEN, then bytes_out and size_out must be
- * non-NULL. If chooser_action is GTK_FILE_CHOOSER_ACTION_SAVE, then
- * handle_out must be non-NULL. Either dir or file (or both) can be NULL.
- * This function returns TRUE if a file was successfully opened.
+ * Open a file for reading.
*/
gboolean
-open_file(gchar ** bytes_out, GnomeVFSHandle ** handle_out, gint * size_out,
- gchar ** dir, gchar ** file, GtkFileChooserAction chooser_action)
+file_open(gchar **dir, gchar **new_file)
{
- GtkWidget *dialog;
- gboolean success = FALSE;
- printf("%s()\n", __PRETTY_FUNCTION__);
-
- dialog =
- hildon_file_chooser_dialog_new(GTK_WINDOW(_window), chooser_action);
-
- if (dir && *dir)
- gtk_file_chooser_set_current_folder_uri(GTK_FILE_CHOOSER
- (dialog), *dir);
-
- if (file && *file) {
- GValue val;
- gtk_file_chooser_set_uri(GTK_FILE_CHOOSER(dialog), *file);
- if (chooser_action == GTK_FILE_CHOOSER_ACTION_SAVE) {
- /* Work around a bug in HildonFileChooserDialog. */
- memset(&val, 0, sizeof(val));
- g_value_init(&val, G_TYPE_BOOLEAN);
- g_value_set_boolean(&val, FALSE);
- g_object_set_property(G_OBJECT(dialog), "autonaming",
- &val);
- gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER
- (dialog),
- strrchr(*file,
- '/') + 1);
- }
- }
+GtkWidget *dialog;
+gboolean success=FALSE;
+GtkFileFilter *filter;
+
+#ifdef WITH_HILDON
+dialog = hildon_file_chooser_dialog_new(GTK_WINDOW(_window), GTK_FILE_CHOOSER_ACTION_OPEN);
+#else
+dialog = gtk_file_chooser_dialog_new("Open...", GTK_WINDOW(_window), GTK_FILE_CHOOSER_ACTION_OPEN,
+ GTK_STOCK_CANCEL,
+ GTK_RESPONSE_CANCEL,
+ GTK_STOCK_OPEN,
+ GTK_RESPONSE_OK, NULL);
+#endif
+
+/* Show .gpx files only */
+filter=gtk_file_filter_new();
+gtk_file_filter_add_pattern(filter, "*.gpx");
+gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
+
+if (dir && *dir) {
+ gtk_file_chooser_set_current_folder_uri(GTK_FILE_CHOOSER(dialog), *dir);
+} else {
+ gtk_file_chooser_set_current_folder_uri(GTK_FILE_CHOOSER(dialog), g_get_home_dir());
+}
- gtk_widget_show_all(dialog);
+*new_file=NULL;
- printf("A");
+if (gtk_dialog_run(GTK_DIALOG(dialog))==GTK_RESPONSE_OK) {
+ /* Get the selected filename. */
+ *new_file = gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(dialog));
+ g_printf("file: %s\n", *new_file);
+ success=TRUE;
+}
- while (!success
- && gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK) {
- gchar *file_uri_str;
- GnomeVFSResult vfs_result;
+gtk_widget_destroy(dialog);
+return success;
+}
- /* Get the selected filename. */
- file_uri_str =
- gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(dialog));
-
- printf("file: %s\n", file_uri_str);
-
- if ((chooser_action == GTK_FILE_CHOOSER_ACTION_OPEN
- && (GNOME_VFS_OK !=
- (vfs_result =
- gnome_vfs_read_entire_file(file_uri_str, size_out,
- bytes_out))))
- || (chooser_action == GTK_FILE_CHOOSER_ACTION_SAVE
- && GNOME_VFS_OK != (vfs_result =
- gnome_vfs_create(handle_out,
- file_uri_str,
- GNOME_VFS_OPEN_WRITE,
- FALSE, 0664)))) {
- gchar buffer[BUFFER_SIZE];
- snprintf(buffer, sizeof(buffer),
- "%s:\n%s",
- chooser_action ==
- GTK_FILE_CHOOSER_ACTION_OPEN ?
- _("Failed to open file for reading")
- : _("Failed to open file for writing"),
- gnome_vfs_result_to_string(vfs_result));
- popup_error(dialog, buffer);
- } else
- success = TRUE;
-
- g_free(file_uri_str);
- }
+/**
+ * Open a file for writing. Return a GnomeVFSHandle to the file for writing.
+ */
+gboolean
+file_save(gchar **dir, gchar **file, GnomeVFSHandle **vfs_handle)
+{
+GtkWidget *dialog;
+gboolean success=FALSE;
+
+#ifdef WITH_HILDON
+dialog = hildon_file_chooser_dialog_new(GTK_WINDOW(_window), GTK_FILE_CHOOSER_ACTION_SAVE);
+#else
+dialog = gtk_file_chooser_dialog_new("Save...", GTK_WINDOW(_window), GTK_FILE_CHOOSER_ACTION_SAVE,
+ GTK_STOCK_CANCEL,
+ GTK_RESPONSE_CANCEL,
+ GTK_STOCK_SAVE,
+ GTK_RESPONSE_OK, NULL);
+gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
+#endif
+
+if (dir && *dir) {
+ gtk_file_chooser_set_current_folder_uri(GTK_FILE_CHOOSER(dialog), *dir);
+} else {
+ gtk_file_chooser_set_current_folder_uri(GTK_FILE_CHOOSER(dialog), g_get_home_dir());
+}
+
+if (file && *file) {
+ gtk_file_chooser_set_uri(GTK_FILE_CHOOSER(dialog), *file);
+ if (strlen(*file)==0)
+ gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "track.gpx");
+} else
+ gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "track.gpx");
+
+if (gtk_dialog_run(GTK_DIALOG(dialog))==GTK_RESPONSE_OK) {
+ gchar *file_uri_str;
+ GnomeVFSResult vfs_result;
- printf("B");
+ file_uri_str = gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(dialog));
+ vfs_result = gnome_vfs_create(vfs_handle, file_uri_str, GNOME_VFS_OPEN_WRITE, FALSE, 0664);
+ g_free(file_uri_str);
- if (success) {
- /* Success!. */
+ if (vfs_result!=GNOME_VFS_OK) {
+ success=FALSE;
+ } else {
if (dir) {
g_free(*dir);
- *dir =
- gtk_file_chooser_get_current_folder_uri
- (GTK_FILE_CHOOSER(dialog));
+ *dir = gtk_file_chooser_get_current_folder_uri(GTK_FILE_CHOOSER(dialog));
}
-
- /* If desired, save the file for later. */
if (file) {
g_free(*file);
- *file =
- gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(dialog));
+ *file = gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(dialog));
}
+ success=TRUE;
+ }
+}
- printf("C");
+gtk_widget_destroy(dialog);
+return success;
+}
- }
+gboolean
+file_open_get_contents(gchar **dir, gchar **buffer, guint *bytes)
+{
+gchar *file_uri_str;
+GnomeVFSResult vfs_result;
+
+if (!file_open(dir, &file_uri_str)) {
+ buffer=NULL;
+ return FALSE;
+}
- gtk_widget_destroy(dialog);
+vfs_result=gnome_vfs_read_entire_file(file_uri_str, bytes, buffer);
+if (vfs_result!=GNOME_VFS_OK)
+ return FALSE;
- vprintf("%s(): return %d\n", __PRETTY_FUNCTION__, success);
- return success;
+return TRUE;
}