From e9a0cfe1a901f41456575a2cd07158f1947b873c Mon Sep 17 00:00:00 2001 From: Milan Crha Date: Thu, 17 Apr 2008 16:59:08 +0000 Subject: ** Fix for bug #526741 (gnome-vfs to gio/gvfs port) 2008-04-17 Milan Crha ** Fix for bug #526741 (gnome-vfs to gio/gvfs port) * configure.in: Drop dependency on gnome-vfs, depend on gio instead. * addressbook/gui/contact-editor/e-contact-editor.c: Do not include gnome-vfs. * calendar/gui/e-meeting-store.c: (start_async_read), (freebusy_async), (async_read): Read data with gio, not with gnome-vfs. * calendar/gui/e-meeting-store.c: (async_close): Function removed. * calendar/gui/migration.c: * calendar/gui/e-memos.c: * calendar/gui/alarm-notify/notify-main.c: * calendar/gui/e-tasks.c: * calendar/gui/main.c: * calendar/gui/e-meeting-list-view.c: Do not include gnome-vfs. * calendar/gui/dialogs/comp-editor.c: (set_attachment_list): * calendar/gui/dialogs/comp-editor.c: (attachment_guess_mime_type): Function removed, use e-util function instead. * calendar/gui/e-cal-popup.c: (temp_save_part): Reflect changes in CamelStreamVFS. * calendar/gui/e-cal-popup.c: (ecalp_apps_open_in), (ecalp_apps_popup_free), (ecalp_standard_menu_factory): Use gio to fill popup and launch app. * calendar/gui/e-cal-component-preview.h: (e_cal_comp_preview_url_requested_cb): * calendar/gui/e-cal-component-preview.c: (e_cal_comp_preview_url_requested_cb): New helper function to manage file:// urls for GtkHTML component. * calendar/gui/e-cal-component-preview.c: (e_cal_component_preview_init): * calendar/gui/e-cal-component-memo-preview.c: (e_cal_comp_preview_url_requested_cb): Use above helper function. * calendar/gui/e-cal-component-memo-preview.c: (url_requested_cb): * calendar/gui/e-cal-component-preview.c: (url_requested_cb): Function removed. * composer/e-msg-composer.h: (e_msg_composer_guess_mime_type): * composer/e-msg-composer.c: (e_msg_composer_guess_mime_type), (handle_uri), (e_msg_composer_add_inline_image_from_file): Use shared e_util_guess_mime_type instead of its own. * e-util/e-gui-utils.c: Do not include gnome-vfs. * e-util/e-util.c: (e_write_file_uri), (e_file_get_save_path): * e-util/e-dialog-utils.c: (e_file_can_save), (e_file_check_local): Use gio instead of gnome-vfs. * e-util/e-util.h: * e-util/e-util.c: (e_util_guess_mime_type), (e_util_filename_to_uri), (e_util_uri_to_filename), (e_util_read_file): New helper functions. * mail/Makefile.am: * mail/em-vfs-stream.h: * mail/em-vfs-stream.c: Removed from project, not used anywhere. * mail/em-menu.c: * mail/em-config.c: Do not include gnome-vfs. * mail/mail-ops.c: (save_messages_exec), (save_part_exec): Changes related to Data Server change of CamelStreamVFS. * mail/em-format.c: (em_format_describe_part): * mail/em-format-html-display.c: (efhd_bonobo_object), (efhd_find_handler), (efhd_use_component), (efhd_bonobo_unknown), (efhd_check_server_prop), (efhd_format_attachment): Drop use of deprecated gnome-vfs functions. * mail/em-utils.c: (em_utils_snoop_type): Use gio to guess the mime_type. * mail/em-popup.c: (emp_apps_open_in), (emp_apps_popup_free), (emp_standard_menu_factory): Use gio to fill popup and launch app. * plugins/import-ics-attachments/icsimporter.c: Do not include gnome-vfs. * plugins/ipod-sync/ical-format.c: (do_save_calendar_ical): * plugins/ipod-sync/ipod-sync.c: (destination_save_addressbook): Write to gio GOutputStream instead of gnome-vfs handle. * plugins/ipod-sync/format-handler.h: (open_for_writing): * plugins/ipod-sync/ipod-sync.c: (open_for_writing): New helper function. * plugins/publish-calendar/publish-format-fb.h: (publish_calendar_as_fb): * plugins/publish-calendar/publish-format-fb.c: (write_calendar), (publish_calendar_as_fb): * plugins/publish-calendar/publish-format-ical.h: (publish_calendar_as_ical): * plugins/publish-calendar/publish-format-ical.c: (write_calendar), (publish_calendar_as_ical): Use gio GOutputStream instead of gnome-vfs handle. * plugins/publish-calendar/url-editor-dialog.c: (create_uri): Use glib function to escape URI. * plugins/publish-calendar/publish-location.c: (migrateURI): Use EUri to parse URI. * plugins/publish-calendar/publish-calendar.c: (publish_online), (unmount_done_cb), (struct mnt_struct), (mount_ready_cb), (ask_password), (ask_question), (mount_first), (publish): Use gio instead of gnome-vfs for opening (remote) files. * plugins/save-calendar/ical-format.c: (do_save_calendar_ical): * plugins/save-calendar/csv-format.c: (do_save_calendar_csv): * plugins/save-calendar/rdf-format.c: (do_save_calendar_rdf): Use gio GOutpuStream instead of gnome-vfs handle. * plugins/save-calendar/format-handler.h: (open_for_writing): * plugins/save-calendar/save-calendar.c: (open_for_writing): New helper function. * shell/e-shell-window-commands.c: (command_quick_reference): Use gio instead of gnome-vfs. * widgets/misc/e-attachment.h: (struct _EAttachment): * widgets/misc/e-attachment.c: (finalise), (init), (attachment_guess_mime_type), (e_attachment_new), (struct DownloadInfo), (download_info_free), (data_ready_cb), (download_to_local_path), (e_attachment_new_remote_file): Use gio instead of gnome-vfs to download remote files. * widgets/misc/e-image-chooser.c: (image_drag_data_received_cb): Use new util function to read file. * widgets/misc/e-attachment-bar.c: (size_to_string): Stolen from gnome-vfs. svn path=/trunk/; revision=35378 --- plugins/publish-calendar/publish-calendar.c | 285 ++++++++++++++++++++++------ 1 file changed, 232 insertions(+), 53 deletions(-) (limited to 'plugins/publish-calendar/publish-calendar.c') diff --git a/plugins/publish-calendar/publish-calendar.c b/plugins/publish-calendar/publish-calendar.c index fd5b283c1a..29788afa39 100644 --- a/plugins/publish-calendar/publish-calendar.c +++ b/plugins/publish-calendar/publish-calendar.c @@ -24,7 +24,8 @@ #include #include #include -#include +#include +#include #include #include #include @@ -62,16 +63,229 @@ publish_uri_async (EPublishUri *uri) } } +static void +publish_online (EPublishUri *uri, GFile *file, GError **perror) +{ + GOutputStream *stream; + GError *error = NULL; + + stream = G_OUTPUT_STREAM (g_file_replace (file, NULL, FALSE, G_FILE_CREATE_NONE, NULL, &error)); + + if (!stream || error) { + if (stream) + g_object_unref (stream); + + if (perror) { + *perror = error; + } else if (error) { + g_warning ("Couldn't open %s: %s", uri->location, error->message); + g_error_free (error); + } else { + g_warning ("Couldn't open %s: Unknown error", uri->location); + } + return; + } + + switch (uri->publish_format) { + case URI_PUBLISH_AS_ICAL: + publish_calendar_as_ical (stream, uri); + break; + case URI_PUBLISH_AS_FB: + publish_calendar_as_fb (stream, uri); + break; + /* + case URI_PUBLISH_AS_HTML: + publish_calendar_as_html (handle, uri); + break; + */ + } + + update_timestamp (uri); + + g_output_stream_close (stream, NULL, NULL); + g_object_unref (stream); +} + +static void +unmount_done_cb (GObject *source_object, GAsyncResult *res, gpointer user_data) +{ + GError *error = NULL; + + g_mount_unmount_finish (G_MOUNT (source_object), res, &error); + + if (error) { + g_warning ("Unmount failed: %s", error->message); + g_error_free (error); + } + + g_object_unref (source_object); +} + +struct mnt_struct { + EPublishUri *uri; + GFile *file; + GMountOperation *mount_op; +}; + +static void +mount_ready_cb (GObject *source_object, GAsyncResult *res, gpointer user_data) +{ + struct mnt_struct *ms = (struct mnt_struct *) user_data; + GError *error = NULL; + GMount *mount; + + g_file_mount_enclosing_volume_finish (G_FILE (source_object), res, &error); + + if (error) { + if (error->code != G_IO_ERROR_CANCELLED) + g_warning ("Mount of %s failed: %s", ms->uri->location, error->message); + + g_error_free (error); + if (ms) + g_object_unref (ms->mount_op); + g_free (ms); + + g_object_unref (source_object); + + return; + } + + g_return_if_fail (ms != NULL); + + publish_online (ms->uri, ms->file, NULL); + + g_object_unref (ms->mount_op); + g_free (ms); + + mount = g_file_find_enclosing_mount (G_FILE (source_object), NULL, NULL); + if (mount) + g_mount_unmount (mount, G_MOUNT_UNMOUNT_NONE, NULL, unmount_done_cb, NULL); + + g_object_unref (source_object); +} + +static void +ask_password (GMountOperation *op, const gchar *message, const gchar *default_user, const gchar *default_domain, GAskPasswordFlags flags, gpointer user_data) +{ + struct mnt_struct *ms = (struct mnt_struct *) user_data; + gchar *username, *password; + gboolean req_pass = FALSE; + EUri *euri; + + g_return_if_fail (ms != NULL); + + /* we can ask only for a password */ + if ((flags & G_ASK_PASSWORD_NEED_PASSWORD) == 0) + return; + + euri = e_uri_new (ms->uri->location); + username = euri->user; + password = e_passwords_get_password ("Calendar", ms->uri->location); + req_pass = ((username && *username) && !(ms->uri->service_type == TYPE_ANON_FTP && + !strcmp (username, "anonymous"))) ? TRUE:FALSE; + + if (!password && req_pass) { + gboolean remember = FALSE; + + password = e_passwords_ask_password (_("Enter password"), "", ms->uri->location, message, + E_PASSWORDS_REMEMBER_FOREVER|E_PASSWORDS_SECRET|E_PASSWORDS_ONLINE, + &remember, + NULL); + + if (!password) { + /* user canceled password dialog */ + g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED); + e_uri_free (euri); + + return; + } + } + + if (!req_pass) + g_mount_operation_set_anonymous (op, TRUE); + else { + g_mount_operation_set_anonymous (op, FALSE); + g_mount_operation_set_username (op, username); + g_mount_operation_set_password (op, password); + } + + g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED); + + e_uri_free (euri); +} + +static void +ask_question (GMountOperation *op, const char *message, const char *choices[]) +{ + /* this has been stolen from file-chooser */ + GtkWidget *dialog; + int cnt, len; + char *primary; + const char *secondary = NULL; + int res; + + primary = strstr (message, "\n"); + if (primary) { + secondary = primary + 1; + primary = g_strndup (message, strlen (message) - strlen (primary)); + } + + gdk_threads_enter (); + dialog = gtk_message_dialog_new (NULL, + 0, GTK_MESSAGE_QUESTION, + GTK_BUTTONS_NONE, "%s", primary); + g_free (primary); + + if (secondary) { + gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), + "%s", secondary); + } + + if (choices) { + /* First count the items in the list then + * add the buttons in reverse order */ + for (len = 0; choices[len] != NULL; len++) { + ; + } + + for (cnt = len - 1; cnt >= 0; cnt--) { + gtk_dialog_add_button (GTK_DIALOG (dialog), choices[cnt], cnt); + } + } + + res = gtk_dialog_run (GTK_DIALOG (dialog)); + if (res >= 0) { + g_mount_operation_set_choice (op, res); + g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED); + } else { + g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED); + } + + gtk_widget_destroy (GTK_WIDGET (dialog)); + gdk_threads_leave (); +} + +static void +mount_first (EPublishUri *uri, GFile *file) +{ + struct mnt_struct *ms = g_malloc (sizeof (struct mnt_struct)); + + ms->uri = uri; + ms->file = g_object_ref (file); + ms->mount_op = g_mount_operation_new (); + + g_signal_connect (ms->mount_op, "ask-password", G_CALLBACK (ask_password), ms); + g_signal_connect (ms->mount_op, "ask-question", G_CALLBACK (ask_question), ms); + + g_file_mount_enclosing_volume (file, G_MOUNT_MOUNT_NONE, ms->mount_op, NULL, mount_ready_cb, ms); +} + static void publish (EPublishUri *uri) { if (online) { - GnomeVFSURI *vfs_uri = NULL; - GnomeVFSResult result; - GnomeVFSHandle *handle; - gchar *password; - const char *username; - gboolean req_pass = FALSE; + GError *error = NULL; + GFile *file; if (g_slist_find (queued_publishes, uri)) queued_publishes = g_slist_remove (queued_publishes, uri); @@ -79,60 +293,25 @@ publish (EPublishUri *uri) if (!uri->enabled) return; - vfs_uri = gnome_vfs_uri_new (uri->location); + file = g_file_new_for_uri (uri->location); - password = e_passwords_get_password ("Calendar", uri->location); - username = gnome_vfs_uri_get_user_name (vfs_uri); - req_pass = ((username && *username) && !(uri->service_type == TYPE_ANON_FTP && - !strcmp (username, "anonymous"))) ? TRUE:FALSE; + g_return_if_fail (file != NULL); - if (!password && req_pass) { - gboolean remember = FALSE; - char *prompt = g_strdup_printf (_("Enter the password for `%s'"), uri->location); + publish_online (uri, file, &error); - password = e_passwords_ask_password (_("Enter password"), "", uri->location, prompt, - E_PASSWORDS_REMEMBER_FOREVER|E_PASSWORDS_SECRET|E_PASSWORDS_ONLINE, - &remember, - NULL); - g_free (prompt); - } - - gnome_vfs_uri_set_password (vfs_uri, password); - - if (vfs_uri == NULL) { - fprintf (stderr, "Couldn't create uri %s\n", uri->location); - /* FIXME: EError */ - g_free (password); - return; - } + if (error && error->domain == G_IO_ERROR && error->code == G_IO_ERROR_NOT_MOUNTED) { + g_error_free (error); + error = NULL; - result = gnome_vfs_create_uri (&handle, vfs_uri, GNOME_VFS_OPEN_WRITE, FALSE, 0666); - if (result != GNOME_VFS_OK) { - /* FIXME: EError */ - fprintf (stderr, "Couldn't open %s: %s\n", uri->location, gnome_vfs_result_to_string (result)); - g_free (password); - return; + mount_first (uri, file); } - switch (uri->publish_format) { - case URI_PUBLISH_AS_ICAL: - publish_calendar_as_ical (handle, uri); - break; - case URI_PUBLISH_AS_FB: - publish_calendar_as_fb (handle, uri); - break; -/* - case URI_PUBLISH_AS_HTML: - publish_calendar_as_html (handle, uri); - break; -*/ + if (error) { + g_warning ("Couldn't open %s: %s", uri->location, error->message); + g_error_free (error); } - update_timestamp (uri); - - result = gnome_vfs_close (handle); - gnome_vfs_uri_unref (vfs_uri); - g_free (password); + g_object_unref (file); } else { if (g_slist_find (queued_publishes, uri) == NULL) queued_publishes = g_slist_prepend (queued_publishes, uri); -- cgit v1.2.3