From 95a87c5349f71f491715999aaf2d988cfa09309d Mon Sep 17 00:00:00 2001 From: Kaj-Michael Lang Date: Mon, 29 Oct 2007 16:43:07 +0200 Subject: [PATCH] Remove the big and messy open_file function. --- src/file.c | 127 ++++++++-------------------------------------------- src/file.h | 6 +-- src/route.c | 5 +-- src/track.c | 36 ++++++--------- 4 files changed, 37 insertions(+), 137 deletions(-) diff --git a/src/file.c b/src/file.c index f640920..3c4f57b 100644 --- a/src/file.c +++ b/src/file.c @@ -17,30 +17,33 @@ * Open a file for reading. */ gboolean -file_open(gchar **dir, gchar **file, gchar **new_file) +file_open(gchar **dir, gchar **new_file) { 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); + 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()); } -if (file && *file) - gtk_file_chooser_set_uri(GTK_FILE_CHOOSER(dialog), *file); - *new_file=NULL; if (gtk_dialog_run(GTK_DIALOG(dialog))==GTK_RESPONSE_OK) { @@ -82,6 +85,8 @@ if (dir && *dir) { if (file && *file) gtk_file_chooser_set_uri(GTK_FILE_CHOOSER(dialog), *file); +else + gtk_file_chooser_set_uri(GTK_FILE_CHOOSER(dialog), "track.gpx"); if (gtk_dialog_run(GTK_DIALOG(dialog))==GTK_RESPONSE_OK) { gchar *file_uri_str; @@ -111,113 +116,19 @@ return success; } gboolean -file_open_get_content(gchar *file, gchar *dir, gchar **buffer, guint *bytes) +file_open_get_contents(gchar **dir, gchar **buffer, guint *bytes) { gchar *file_uri_str; GnomeVFSResult vfs_result; -if (!file_open(&file,&dir,&file_uri_str)) { +if (!file_open(dir, &file_uri_str)) { buffer=NULL; return TRUE; } -vfs_result = gnome_vfs_read_entire_file(file_uri_str, bytes, buffer); -if (vfs_result != GNOME_VFS_OK) { - return FALSE; -} else { - return TRUE; -} -} - -/** - * 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. - */ -gboolean -open_file(gchar ** bytes_out, GnomeVFSHandle ** handle_out, gint * size_out, - gchar ** dir, gchar ** file, GtkFileChooserAction chooser_action) -{ - GtkWidget *dialog; - gboolean success = FALSE; - - 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); - } - } - - gtk_widget_show_all(dialog); - g_printf("A\n"); - - while (!success && gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK) { - gchar *file_uri_str; - GnomeVFSResult vfs_result; - - /* Get the selected filename. */ - file_uri_str = gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(dialog)); - - g_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); - } - - g_printf("B\n"); - - if (success) { - /* Success!. */ - if (dir) { - g_free(*dir); - *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)); - } - - g_printf("C\n"); - - } - - gtk_widget_destroy(dialog); +vfs_result=gnome_vfs_read_entire_file(file_uri_str, bytes, buffer); +if (vfs_result!=GNOME_VFS_OK) + return FALSE; - return success; +return TRUE; } diff --git a/src/file.h b/src/file.h index 00f7751..57a6553 100644 --- a/src/file.h +++ b/src/file.h @@ -2,8 +2,8 @@ #define _MAPPER_FILE_H gboolean file_save(gchar **dir, gchar **file, GnomeVFSHandle **vfs); -gboolean file_open(gchar **dir, gchar **file, gchar **new_file); -gboolean open_file(gchar ** bytes_out, GnomeVFSHandle ** handle_out, gint * size_out, - gchar ** dir, gchar ** file, GtkFileChooserAction chooser_action); +gboolean file_open(gchar **dir, gchar **new_file); + +gboolean file_open_get_contents(gchar **dir, gchar **buffer, guint *bytes); #endif diff --git a/src/route.c b/src/route.c index 969daf7..ec690b0 100644 --- a/src/route.c +++ b/src/route.c @@ -85,16 +85,15 @@ route_open_file(void) gchar *buffer; gint size; -if (open_file(&buffer, NULL, &size, &_route_dir_uri, NULL, GTK_FILE_CHOOSER_ACTION_OPEN)) { +if (file_open_get_contents(&_route_dir_uri, &buffer, &size)) { /* If auto is enabled, append the route, otherwise replace it. */ if (parse_gpx(&_route, buffer, size, _autoroute_data.enabled ? 0 : 1)) { cancel_autoroute(FALSE); + MACRO_BANNER_SHOW_INFO(_window, _("Route Opened")); /* Find the nearest route point, if we're connected. */ route_find_nearest_point(); - map_force_redraw(); - MACRO_BANNER_SHOW_INFO(_window, _("Route Opened")); route_set_destination_from_last(); return TRUE; } else { diff --git a/src/track.c b/src/track.c index 786acba..a4642cd 100644 --- a/src/track.c +++ b/src/track.c @@ -12,6 +12,8 @@ #include "track.h" #include "latlon.h" #include "path.h" +#include "gpx.h" +#include "speak.h" struct sql_select_stmt { sqlite3_stmt *select_track; @@ -215,14 +217,14 @@ if ((x1 < BUF_WIDTH_PIXELS) && ((unsigned)y1 < BUF_HEIGHT_PIXELS)) } } -gboolean +gboolean track_open(void) { gchar *buffer; gint size; gboolean r = FALSE; -if (open_file(&buffer, NULL, &size, NULL, &_track_file_uri, GTK_FILE_CHOOSER_ACTION_OPEN)) { +if (file_open_get_contents(&_track_file_uri, &buffer, &size)) { if (parse_gpx(&_track, buffer, size, -1)) { map_force_redraw(); MACRO_BANNER_SHOW_INFO(_window, _("Track Opened")); @@ -235,7 +237,7 @@ if (open_file(&buffer, NULL, &size, NULL, &_track_file_uri, GTK_FILE_CHOOSER_ACT return r; } -gboolean +gboolean track_save(void) { GnomeVFSHandle *handle; @@ -288,39 +290,27 @@ dialog = gtk_dialog_new_with_buttons(_("Insert Mark"), GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL); -gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), - table = gtk_table_new(2, 2, FALSE), TRUE, TRUE, 0); +gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), table = gtk_table_new(2, 2, FALSE), TRUE, TRUE, 0); -gtk_table_attach(GTK_TABLE(table), - label = gtk_label_new(_("Lat, Lon")), - 0, 1, 0, 1, GTK_FILL, 0, 2, 4); +gtk_table_attach(GTK_TABLE(table), label = gtk_label_new(_("Lat, Lon")), 0, 1, 0, 1, GTK_FILL, 0, 2, 4); gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f); unit2latlon(_pos.unitx, _pos.unity, lat, lon); lat_format(lat, tmp1); lon_format(lon, tmp2); p_latlon = g_strdup_printf("%s, %s", tmp1, tmp2); -gtk_table_attach(GTK_TABLE(table), - label = gtk_label_new(p_latlon), - 1, 2, 0, 1, GTK_FILL, 0, 2, 4); +gtk_table_attach(GTK_TABLE(table), label = gtk_label_new(p_latlon), 1, 2, 0, 1, GTK_FILL, 0, 2, 4); gtk_misc_set_alignment(GTK_MISC(label), 0.0f, 0.5f); g_free(p_latlon); -gtk_table_attach(GTK_TABLE(table), - label = gtk_label_new(_("Description")), - 0, 1, 1, 2, GTK_FILL, 0, 2, 4); +gtk_table_attach(GTK_TABLE(table), label = gtk_label_new(_("Description")), 0, 1, 1, 2, GTK_FILL, 0, 2, 4); gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f); txt_scroll = gtk_scrolled_window_new(NULL, NULL); -gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(txt_scroll), - GTK_SHADOW_IN); -gtk_table_attach(GTK_TABLE(table), - txt_scroll, - 1, 2, 1, 2, GTK_EXPAND | GTK_FILL, 0, 2, 4); - -gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(txt_scroll), - GTK_POLICY_AUTOMATIC, - GTK_POLICY_AUTOMATIC); +gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(txt_scroll), GTK_SHADOW_IN); +gtk_table_attach(GTK_TABLE(table), txt_scroll, 1, 2, 1, 2, GTK_EXPAND | GTK_FILL, 0, 2, 4); + +gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(txt_scroll), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); txt_desc = gtk_text_view_new(); gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(txt_desc), GTK_WRAP_WORD); -- 2.39.5