From e912e532fd67211aa87ac79414efb30f30a00fab Mon Sep 17 00:00:00 2001 From: Carlos Garcia Campos Date: Fri, 25 Jan 2008 12:30:28 +0000 Subject: Port to gio and drop gnome-vfs dependency. Fixes bug #510401. Based on 2008-01-25 Carlos Garcia Campos * configure.ac: * backend/comics/comics-document.c: (comics_document_load): * libdocument/ev-attachment.[ch]: (ev_attachment_finalize), (ev_attachment_set_property), (ev_attachment_init), (ev_attachment_save), (ev_attachment_launch_app), (ev_attachment_open): * libdocument/ev-document-factory.c: (get_document_from_uri): * libdocument/ev-file-helpers.[ch]: (ev_tmp_file_get), (ev_tmp_file_unlink), (ev_tmp_uri_unlink), (ev_xfer_uri_simple),: * shell/ev-jobs.c: * shell/ev-password.c: (ev_password_dialog_set_property), (ev_password_dialog_save_password): * shell/ev-sidebar-attachments.c: (ev_sidebar_attachments_drag_data_get): * shell/ev-window-title.c: (get_filename_from_uri): * shell/ev-window.c: (ev_window_clear_temp_file), (ev_window_load_job_cb), (window_open_file_copy_ready_cb), (ev_window_open_uri), (window_save_file_copy_ready_cb), (ev_window_save_remote), (ev_window_cmd_save_as), (launch_action), (launch_external_uri), (image_save_dialog_response_cb), (attachment_save_dialog_response_cb): * shell/main.c: (load_files), (load_files_remote), (main): * thumbnailer/evince-thumbnailer.c: (main): Port to gio and drop gnome-vfs dependency. Fixes bug #510401. Based on patch by Cosimo Cecchi. svn path=/trunk/; revision=2858 --- (limited to 'libdocument') diff --git a/libdocument/ev-attachment.c b/libdocument/ev-attachment.c index 7592c4f..02d5f3d 100644 --- a/libdocument/ev-attachment.c +++ b/libdocument/ev-attachment.c @@ -20,9 +20,6 @@ #include #include #include -#include -#include -#include #include "ev-file-helpers.h" #include "ev-attachment.h" @@ -46,8 +43,8 @@ struct _EvAttachmentPrivate { gchar *data; gchar *mime_type; - GnomeVFSMimeApplication *app; - gchar *tmp_uri; + GAppInfo *app; + GFile *tmp_file; }; #define EV_ATTACHMENT_GET_PRIVATE(object) \ @@ -93,14 +90,14 @@ ev_attachment_finalize (GObject *object) } if (attachment->priv->app) { - gnome_vfs_mime_application_free (attachment->priv->app); + g_object_unref (attachment->priv->app); attachment->priv->app = NULL; } - if (attachment->priv->tmp_uri) { - ev_tmp_filename_unlink (attachment->priv->tmp_uri); - g_free (attachment->priv->tmp_uri); - attachment->priv->tmp_uri = NULL; + if (attachment->priv->tmp_file) { + ev_tmp_file_unlink (attachment->priv->tmp_file); + g_object_unref (attachment->priv->tmp_file); + attachment->priv->tmp_file = NULL; } (* G_OBJECT_CLASS (ev_attachment_parent_class)->finalize) (object); @@ -132,9 +129,10 @@ ev_attachment_set_property (GObject *object, break; case PROP_DATA: attachment->priv->data = g_value_get_pointer (value); - attachment->priv->mime_type = - g_strdup (gnome_vfs_get_mime_type_for_data (attachment->priv->data, - attachment->priv->size)); + attachment->priv->mime_type = g_content_type_guess (attachment->priv->name, + (guchar *) attachment->priv->data, + attachment->priv->size, + NULL); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, @@ -217,7 +215,7 @@ ev_attachment_init (EvAttachment *attachment) attachment->priv->data = NULL; attachment->priv->mime_type = NULL; - attachment->priv->tmp_uri = NULL; + attachment->priv->tmp_file = NULL; } EvAttachment * @@ -284,78 +282,92 @@ ev_attachment_get_mime_type (EvAttachment *attachment) gboolean ev_attachment_save (EvAttachment *attachment, - const gchar *uri, + GFile *file, GError **error) { - GnomeVFSHandle *handle = NULL; - GnomeVFSFileSize written; - GnomeVFSResult result; - + GFileOutputStream *output_stream; + GError *ioerror = NULL; + gssize written_bytes; + g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), FALSE); - g_return_val_if_fail (uri != NULL, FALSE); + g_return_val_if_fail (G_IS_FILE (file), FALSE); - result = gnome_vfs_create (&handle, uri, - GNOME_VFS_OPEN_WRITE, - FALSE, 0644); - if (result != GNOME_VFS_OK) { + output_stream = g_file_create (file, 0, NULL, &ioerror); + if (output_stream == NULL) { + char *uri; + + uri = g_file_get_uri (file); g_set_error (error, EV_ATTACHMENT_ERROR, - (gint) result, + ioerror->code, _("Couldn't save attachment “%s”: %s"), uri, - gnome_vfs_result_to_string (result)); + ioerror->message); + + g_error_free (ioerror); + g_free (uri); return FALSE; } - - result = gnome_vfs_write (handle, attachment->priv->data, - attachment->priv->size, &written); - if (result != GNOME_VFS_OK || written < attachment->priv->size){ + + written_bytes = g_output_stream_write (G_OUTPUT_STREAM (output_stream), + attachment->priv->data, + attachment->priv->size, + NULL, &ioerror); + if (written_bytes == -1) { + char *uri; + + uri = g_file_get_uri (file); g_set_error (error, EV_ATTACHMENT_ERROR, - (gint) result, + ioerror->code, _("Couldn't save attachment “%s”: %s"), uri, - gnome_vfs_result_to_string (result)); + ioerror->message); - gnome_vfs_close (handle); + g_output_stream_close (G_OUTPUT_STREAM (output_stream), NULL, NULL); + g_error_free (ioerror); + g_free (uri); return FALSE; } - gnome_vfs_close (handle); + g_output_stream_close (G_OUTPUT_STREAM (output_stream), NULL, NULL); return TRUE; + } static gboolean ev_attachment_launch_app (EvAttachment *attachment, GError **error) { - GnomeVFSResult result; - GList *uris = NULL; + gboolean result; + GList *files = NULL; + GError *ioerror = NULL; - g_assert (attachment->priv->tmp_uri != NULL); - g_assert (attachment->priv->app != NULL); + g_assert (G_IS_FILE (attachment->priv->tmp_file)); + g_assert (G_IS_APP_INFO (attachment->priv->app)); - uris = g_list_prepend (uris, attachment->priv->tmp_uri); - result = gnome_vfs_mime_application_launch (attachment->priv->app, - uris); + files = g_list_prepend (files, attachment->priv->tmp_file); + result = g_app_info_launch (attachment->priv->app, files, + NULL, &ioerror); - if (result != GNOME_VFS_OK) { + if (!result) { g_set_error (error, EV_ATTACHMENT_ERROR, (gint) result, _("Couldn't open attachment “%s”: %s"), attachment->priv->name, - gnome_vfs_result_to_string (result)); + ioerror->message); - g_list_free (uris); + g_list_free (files); + g_error_free (ioerror); return FALSE; } - g_list_free (uris); + g_list_free (files); return TRUE; } @@ -364,15 +376,14 @@ gboolean ev_attachment_open (EvAttachment *attachment, GError **error) { - - gboolean retval = FALSE; - GnomeVFSMimeApplication *default_app = NULL; + GAppInfo *app_info; + gboolean retval = FALSE; g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), FALSE); if (!attachment->priv->app) { - default_app = gnome_vfs_mime_get_default_application (attachment->priv->mime_type); - attachment->priv->app = default_app; + app_info = g_app_info_get_default_for_type (attachment->priv->mime_type, TRUE); + attachment->priv->app = app_info; } if (!attachment->priv->app) { @@ -385,26 +396,28 @@ ev_attachment_open (EvAttachment *attachment, return FALSE; } - if (attachment->priv->tmp_uri && - g_file_test (attachment->priv->tmp_uri, G_FILE_TEST_EXISTS)) { + if (attachment->priv->tmp_file && + g_file_query_exists (attachment->priv->tmp_file, NULL)) { retval = ev_attachment_launch_app (attachment, error); } else { - gchar *uri, *filename; + GFile *tmpdir; + GFile *file; - filename = g_build_filename (ev_tmp_dir (), attachment->priv->name, NULL); - uri = g_filename_to_uri (filename, NULL, NULL); + tmpdir = g_file_new_for_path (ev_tmp_dir ()); + file = g_file_get_child (tmpdir, attachment->priv->name); - if (ev_attachment_save (attachment, uri, error)) { - if (attachment->priv->tmp_uri) - g_free (attachment->priv->tmp_uri); - attachment->priv->tmp_uri = g_strdup (filename); + if (ev_attachment_save (attachment, file, error)) { + if (attachment->priv->tmp_file) + g_object_unref (attachment->priv->tmp_file); + attachment->priv->tmp_file = g_object_ref (file); retval = ev_attachment_launch_app (attachment, error); } - g_free (filename); - g_free (uri); + g_object_unref (file); + g_object_unref (tmpdir); } return retval; } + diff --git a/libdocument/ev-attachment.h b/libdocument/ev-attachment.h index 994b654..8403bee 100644 --- a/libdocument/ev-attachment.h +++ b/libdocument/ev-attachment.h @@ -21,6 +21,7 @@ #define __EV_ATTACHMENT_H__ #include +#include G_BEGIN_DECLS @@ -62,7 +63,7 @@ GTime ev_attachment_get_modification_date (EvAttachment *attachment); GTime ev_attachment_get_creation_date (EvAttachment *attachment); const gchar *ev_attachment_get_mime_type (EvAttachment *attachment); gboolean ev_attachment_save (EvAttachment *attachment, - const gchar *uri, + GFile *file, GError **error); gboolean ev_attachment_open (EvAttachment *attachment, GError **error); diff --git a/libdocument/ev-document-factory.c b/libdocument/ev-document-factory.c index 21e0b4a..1bc67ef 100644 --- a/libdocument/ev-document-factory.c +++ b/libdocument/ev-document-factory.c @@ -23,11 +23,9 @@ #endif #include +#include #include #include -#include -#include -#include #include #include "ev-backends-manager.h" @@ -112,55 +110,57 @@ get_document_from_uri (const char *uri, GError **error) { EvDocument *document = NULL; - GnomeVFSFileInfo *info; - GnomeVFSResult result; + GFile *file; + GFileInfo *file_info; + const gchar *mime_type; *compression = EV_COMPRESSION_NONE; - info = gnome_vfs_file_info_new (); - result = gnome_vfs_get_file_info (uri, info, - GNOME_VFS_FILE_INFO_GET_MIME_TYPE | - GNOME_VFS_FILE_INFO_FOLLOW_LINKS | - (slow ? GNOME_VFS_FILE_INFO_FORCE_SLOW_MIME_TYPE : 0)); - if (result != GNOME_VFS_OK) { + file = g_file_new_for_uri (uri); + file_info = g_file_query_info (file, + G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, + 0, NULL, NULL); + g_object_unref (file); + + if (file_info == NULL) { g_set_error (error, EV_DOCUMENT_ERROR, 0, - gnome_vfs_result_to_string (result)); - gnome_vfs_file_info_unref (info); + _("Failed to get info for document")); return NULL; - } - - if (info->mime_type == NULL) { + } + mime_type = g_file_info_get_content_type (file_info); + + if (mime_type == NULL) { g_set_error (error, EV_DOCUMENT_ERROR, 0, _("Unknown MIME Type")); - gnome_vfs_file_info_unref (info); + g_object_unref (file_info); return NULL; } #ifdef ENABLE_PIXBUF - if (mime_type_supported_by_gdk_pixbuf (info->mime_type)) { + if (mime_type_supported_by_gdk_pixbuf (mime_type)) document = ev_backends_manager_get_document ("image/*"); - } else - document = ev_backends_manager_get_document (info->mime_type); + else + document = ev_backends_manager_get_document (mime_type); #else - document = ev_backends_manager_get_document (info->mime_type); + document = ev_backends_manager_get_document (mime_type); #endif /* ENABLE_PIXBUF */ if (document == NULL) { g_set_error (error, EV_DOCUMENT_ERROR, 0, - _("Unhandled MIME type: “%s”"), info->mime_type); - gnome_vfs_file_info_unref (info); + _("Unhandled MIME type: “%s”"), mime_type); + g_object_unref (file_info); return NULL; } - *compression = get_compression_from_mime_type (info->mime_type); + *compression = get_compression_from_mime_type (mime_type); - gnome_vfs_file_info_unref (info); + g_object_unref (file_info); return document; } diff --git a/libdocument/ev-file-helpers.c b/libdocument/ev-file-helpers.c index dd3b3e1..20c7bc4 100644 --- a/libdocument/ev-file-helpers.c +++ b/libdocument/ev-file-helpers.c @@ -29,10 +29,6 @@ #include #include #include -#include -#include -#include -#include #if WITH_GNOME #include @@ -122,6 +118,20 @@ ev_file_helpers_shutdown (void) tmp_dir = NULL; } +GFile * +ev_tmp_file_get (const gchar *prefix) +{ + gchar *path; + GFile *file; + + path = ev_tmp_filename (prefix); + file = g_file_new_for_path (path); + + g_free (path); + + return file; +} + gchar * ev_tmp_filename (const gchar *prefix) { @@ -161,25 +171,40 @@ ev_tmp_filename_unlink (const gchar *filename) } void +ev_tmp_file_unlink (GFile *file) +{ + gboolean res; + + if (!file) + return; + + res = g_file_delete (file, NULL, NULL); + if (!res) { + char *uri; + + uri = g_file_get_uri (file); + g_warning ("Unable to delete temp file %s\n", uri); + g_free (uri); + } +} + +void ev_tmp_uri_unlink (const gchar *uri) { - GnomeVFSURI *vfs_uri; - gchar *filename; + GFile *file; if (!uri) return; - vfs_uri = gnome_vfs_uri_new (uri); - if (!gnome_vfs_uri_is_local (vfs_uri)) { - g_warning ("Attempting to delete non local uri: %s\n", uri); - gnome_vfs_uri_unref (vfs_uri); + file = g_file_new_for_uri (uri); + if (!g_file_is_native (file)) { + g_warning ("Attempting to delete non native uri: %s\n", uri); + g_object_unref (file); return; } - gnome_vfs_uri_unref (vfs_uri); - - filename = g_filename_from_uri (uri, NULL, NULL); - ev_tmp_filename_unlink (filename); - g_free (filename); + + ev_tmp_file_unlink (file); + g_object_unref (file); } gboolean @@ -187,31 +212,28 @@ ev_xfer_uri_simple (const char *from, const char *to, GError **error) { - GnomeVFSResult result; - GnomeVFSURI *source_uri; - GnomeVFSURI *target_uri; + GFile *source_file; + GFile *target_file; + GError *ioerror; + gboolean result; if (!from) return FALSE; - source_uri = gnome_vfs_uri_new (from); - target_uri = gnome_vfs_uri_new (to); - - result = gnome_vfs_xfer_uri (source_uri, target_uri, - GNOME_VFS_XFER_DEFAULT | GNOME_VFS_XFER_FOLLOW_LINKS, - GNOME_VFS_XFER_ERROR_MODE_ABORT, - GNOME_VFS_XFER_OVERWRITE_MODE_REPLACE, - NULL, - NULL); - gnome_vfs_uri_unref (target_uri); - gnome_vfs_uri_unref (source_uri); + source_file = g_file_new_for_uri (from); + target_file = g_file_new_for_uri (to); + + result = g_file_copy (source_file, target_file, + G_FILE_COPY_OVERWRITE, + NULL, NULL, NULL, &ioerror); + + g_object_unref (target_file); + g_object_unref (source_file); - if (result != GNOME_VFS_OK) - g_set_error (error, - G_FILE_ERROR, - G_FILE_ERROR_FAILED, - gnome_vfs_result_to_string (result)); - return (result == GNOME_VFS_OK); + if (!result) { + g_propagate_error (error, ioerror); + } + return result; } diff --git a/libdocument/ev-file-helpers.h b/libdocument/ev-file-helpers.h index 6aad12a..cd86320 100644 --- a/libdocument/ev-file-helpers.h +++ b/libdocument/ev-file-helpers.h @@ -18,10 +18,11 @@ * $Id$ */ -#ifndef EPHY_FILE_HELPERS_H -#define EPHY_FILE_HELPERS_H +#ifndef EV_FILE_HELPERS_H +#define EV_FILE_HELPERS_H #include +#include G_BEGIN_DECLS @@ -39,8 +40,10 @@ void ev_file_helpers_init (void); void ev_file_helpers_shutdown (void); +GFile *ev_tmp_file_get (const gchar *prefix); gchar *ev_tmp_filename (const char *prefix); void ev_tmp_filename_unlink (const gchar *filename); +void ev_tmp_file_unlink (GFile *file); void ev_tmp_uri_unlink (const gchar *uri); gboolean ev_xfer_uri_simple (const char *from, @@ -57,4 +60,4 @@ gchar *ev_file_compress (const gchar *uri, G_END_DECLS -#endif /* EPHY_FILE_HELPERS_H */ +#endif /* EV_FILE_HELPERS_H */ -- cgit v0.9.1