From: Kaj-Michael Lang Date: Fri, 20 Jul 2007 14:50:25 +0000 (+0300) Subject: More work on the split open/save functions. X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5e5807ed765e86c62b15d4d077f7771c146554ae;p=mapper More work on the split open/save functions. Fix the problem with hildon and gtk file dialogs returning different value (OK/ACCEPT) --- diff --git a/src/file.c b/src/file.c index 0db786b..fdf4c13 100644 --- a/src/file.c +++ b/src/file.c @@ -11,11 +11,15 @@ #include "ui-common.h" #include "hildon-wrappers.h" +/** + * Open a file for reading. + */ gboolean -file_open(gchar *dir, gchar *file, GnomeVFSHandle **vfs) +file_open(gchar *dir, gchar *file, gchar *new_file) { GtkWidget *dialog; -gboolean success; +gint r; +gboolean success=FALSE; #ifdef WITH_HILDON dialog = hildon_file_chooser_dialog_new(GTK_WINDOW(_window), GTK_FILE_CHOOSER_ACTION_OPEN); @@ -33,25 +37,29 @@ if (dir) if (file) gtk_file_chooser_set_uri(GTK_FILE_CHOOSER(dialog), file); -while (!success && gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK) { - gchar *file_uri_str; - GnomeVFSResult vfs_result; +new_file=NULL; +r=gtk_dialog_run(GTK_DIALOG(dialog)); +if (r==GTK_RESPONSE_ACCEPT || r==GTK_RESPONSE_OK) { /* Get the selected filename. */ - file_uri_str = gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(dialog)); - - printf("file: %s\n", file_uri_str); - - g_free(file_uri_str); + new_file = gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(dialog)); + g_printf("file: %s\n", new_file); + success=TRUE; } +gtk_widget_destroy(dialog); +return success; } +/** + * Open a file for writing. Return a GnomeVFSHandle to the file for writing. + */ gboolean -file_save(gchar *dir, gchar *file, GnomeVFSHandle **vfs) +file_save(gchar *dir, gchar *file, GnomeVFSHandle **vfs_handle) { GtkWidget *dialog; -gboolean success; +gint r; +gboolean success=FALSE; #ifdef WITH_HILDON dialog = hildon_file_chooser_dialog_new(GTK_WINDOW(_window), GTK_FILE_CHOOSER_ACTION_SAVE); @@ -70,13 +78,13 @@ if (dir) if (file) gtk_file_chooser_set_uri(GTK_FILE_CHOOSER(dialog), file); -success=FALSE; -if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { +r=gtk_dialog_run(GTK_DIALOG(dialog)); +if (r==GTK_RESPONSE_ACCEPT || r==GTK_RESPONSE_OK) { gchar *file_uri_str; GnomeVFSResult vfs_result; file_uri_str = gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(dialog)); - vfs_result = gnome_vfs_create(vfs, file_uri_str, GNOME_VFS_OPEN_WRITE, FALSE, 0664); + vfs_result = gnome_vfs_create(vfs_handle, file_uri_str, GNOME_VFS_OPEN_WRITE, FALSE, 0664); g_free(file_uri_str); if (vfs_result!=GNOME_VFS_OK) { @@ -98,6 +106,24 @@ gtk_widget_destroy(dialog); return success; } +gboolean +file_open_get_content(gchar *file, gchar *dir, gchar **buffer, guint *bytes) +{ +gchar *file_uri_str; +GnomeVFSResult vfs_result; + +if (!file_open(file,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 @@ -131,43 +157,33 @@ open_file(gchar ** bytes_out, GnomeVFSHandle ** handle_out, gint * size_out, g_object_set_property(G_OBJECT(dialog), "autonaming", &val); gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER - (dialog), - strrchr(*file, - '/') + 1); + (dialog), strrchr(*file, '/') + 1); } } gtk_widget_show_all(dialog); - printf("A"); + g_printf("A\n"); - while (!success - && gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK) { + while (!success && gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { gchar *file_uri_str; GnomeVFSResult vfs_result; /* Get the selected filename. */ - file_uri_str = - gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(dialog)); + file_uri_str = gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(dialog)); - printf("file: %s\n", file_uri_str); + 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, + (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)))) { + && 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 ? + 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)); @@ -178,25 +194,23 @@ open_file(gchar ** bytes_out, GnomeVFSHandle ** handle_out, gint * size_out, g_free(file_uri_str); } - printf("B"); + g_printf("B\n"); if (success) { /* Success!. */ if (dir) { g_free(*dir); - *dir = - gtk_file_chooser_get_current_folder_uri + *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)); } - printf("C"); + g_printf("C\n"); }