From: Carlos Garcia Campos Date: Tue, 18 Aug 2009 08:18:55 +0000 (+0200) Subject: Remove attachments from EvDocument interface and use EvDocumentAttachments instead X-Git-Tag: EVINCE_2_29_1~110 X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=commitdiff_plain;h=3d3328b2597e9b85afcb552ffc6abdeca1e3e3a7;p=evince.git Remove attachments from EvDocument interface and use EvDocumentAttachments instead --- diff --git a/backend/pdf/ev-poppler.cc b/backend/pdf/ev-poppler.cc index 0a073ca0..0333d715 100644 --- a/backend/pdf/ev-poppler.cc +++ b/backend/pdf/ev-poppler.cc @@ -48,6 +48,7 @@ #include "ev-document-layers.h" #include "ev-document-print.h" #include "ev-document-annotations.h" +#include "ev-document-attachments.h" #include "ev-selection.h" #include "ev-transition-effect.h" #include "ev-attachment.h" @@ -124,6 +125,7 @@ static void pdf_document_document_layers_iface_init (EvDocumentLayersIface static void pdf_document_document_print_iface_init (EvDocumentPrintIface *iface); #endif static void pdf_document_document_annotations_iface_init (EvDocumentAnnotationsIface *iface); +static void pdf_document_document_attachments_iface_init (EvDocumentAttachmentsIface *iface); static void pdf_document_find_iface_init (EvDocumentFindIface *iface); static void pdf_document_file_exporter_iface_init (EvFileExporterIface *iface); static void pdf_selection_iface_init (EvSelectionIface *iface); @@ -163,6 +165,8 @@ EV_BACKEND_REGISTER_WITH_CODE (PdfDocument, pdf_document, #endif EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_ANNOTATIONS, pdf_document_document_annotations_iface_init); + EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_ATTACHMENTS, + pdf_document_document_attachments_iface_init); EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND, pdf_document_find_iface_init); EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER, @@ -361,122 +365,6 @@ pdf_document_get_page_label (EvDocument *document, return label; } -static gboolean -pdf_document_has_attachments (EvDocument *document) -{ - PdfDocument *pdf_document; - - pdf_document = PDF_DOCUMENT (document); - - return poppler_document_has_attachments (pdf_document->document); -} - -struct SaveToBufferData { - gchar *buffer; - gsize len, max; -}; - -static gboolean -attachment_save_to_buffer_callback (const gchar *buf, - gsize count, - gpointer user_data, - GError **error) -{ - struct SaveToBufferData *sdata = (SaveToBufferData *)user_data; - gchar *new_buffer; - gsize new_max; - - if (sdata->len + count > sdata->max) { - new_max = MAX (sdata->max * 2, sdata->len + count); - new_buffer = (gchar *)g_realloc (sdata->buffer, new_max); - - sdata->buffer = new_buffer; - sdata->max = new_max; - } - - memcpy (sdata->buffer + sdata->len, buf, count); - sdata->len += count; - - return TRUE; -} - -static gboolean -attachment_save_to_buffer (PopplerAttachment *attachment, - gchar **buffer, - gsize *buffer_size, - GError **error) -{ - static const gint initial_max = 1024; - struct SaveToBufferData sdata; - - *buffer = NULL; - *buffer_size = 0; - - sdata.buffer = (gchar *) g_malloc (initial_max); - sdata.max = initial_max; - sdata.len = 0; - - if (! poppler_attachment_save_to_callback (attachment, - attachment_save_to_buffer_callback, - &sdata, - error)) { - g_free (sdata.buffer); - return FALSE; - } - - *buffer = sdata.buffer; - *buffer_size = sdata.len; - - return TRUE; -} - -static GList * -pdf_document_get_attachments (EvDocument *document) -{ - PdfDocument *pdf_document; - GList *attachments; - GList *list; - GList *retval = NULL; - - pdf_document = PDF_DOCUMENT (document); - - if (!pdf_document_has_attachments (document)) - return NULL; - - attachments = poppler_document_get_attachments (pdf_document->document); - - for (list = attachments; list; list = list->next) { - PopplerAttachment *attachment; - EvAttachment *ev_attachment; - gchar *data = NULL; - gsize size; - GError *error = NULL; - - attachment = (PopplerAttachment *) list->data; - - if (attachment_save_to_buffer (attachment, &data, &size, &error)) { - ev_attachment = ev_attachment_new (attachment->name, - attachment->description, - attachment->mtime, - attachment->ctime, - size, data); - - retval = g_list_prepend (retval, ev_attachment); - } else { - if (error) { - g_warning ("%s", error->message); - g_error_free (error); - - g_free (data); - } - } - - g_object_unref (attachment); - } - - return g_list_reverse (retval); -} - static cairo_surface_t * pdf_page_render (PopplerPage *page, gint width, @@ -841,8 +729,6 @@ pdf_document_document_iface_init (EvDocumentIface *iface) iface->get_page = pdf_document_get_page; iface->get_page_size = pdf_document_get_page_size; iface->get_page_label = pdf_document_get_page_label; - iface->has_attachments = pdf_document_has_attachments; - iface->get_attachments = pdf_document_get_attachments; iface->render = pdf_document_render; iface->get_info = pdf_document_get_info; }; @@ -2655,6 +2541,123 @@ pdf_document_document_annotations_iface_init (EvDocumentAnnotationsIface *iface) iface->annotation_set_contents = pdf_document_annotations_annotation_set_contents; } +/* Attachments */ +struct SaveToBufferData { + gchar *buffer; + gsize len, max; +}; + +static gboolean +attachment_save_to_buffer_callback (const gchar *buf, + gsize count, + gpointer user_data, + GError **error) +{ + struct SaveToBufferData *sdata = (SaveToBufferData *)user_data; + gchar *new_buffer; + gsize new_max; + + if (sdata->len + count > sdata->max) { + new_max = MAX (sdata->max * 2, sdata->len + count); + new_buffer = (gchar *)g_realloc (sdata->buffer, new_max); + + sdata->buffer = new_buffer; + sdata->max = new_max; + } + + memcpy (sdata->buffer + sdata->len, buf, count); + sdata->len += count; + + return TRUE; +} + +static gboolean +attachment_save_to_buffer (PopplerAttachment *attachment, + gchar **buffer, + gsize *buffer_size, + GError **error) +{ + static const gint initial_max = 1024; + struct SaveToBufferData sdata; + + *buffer = NULL; + *buffer_size = 0; + + sdata.buffer = (gchar *) g_malloc (initial_max); + sdata.max = initial_max; + sdata.len = 0; + + if (! poppler_attachment_save_to_callback (attachment, + attachment_save_to_buffer_callback, + &sdata, + error)) { + g_free (sdata.buffer); + return FALSE; + } + + *buffer = sdata.buffer; + *buffer_size = sdata.len; + + return TRUE; +} + +static GList * +pdf_document_attachments_get_attachments (EvDocumentAttachments *document) +{ + PdfDocument *pdf_document = PDF_DOCUMENT (document); + GList *attachments; + GList *list; + GList *retval = NULL; + + attachments = poppler_document_get_attachments (pdf_document->document); + + for (list = attachments; list; list = list->next) { + PopplerAttachment *attachment; + EvAttachment *ev_attachment; + gchar *data = NULL; + gsize size; + GError *error = NULL; + + attachment = (PopplerAttachment *) list->data; + + if (attachment_save_to_buffer (attachment, &data, &size, &error)) { + ev_attachment = ev_attachment_new (attachment->name, + attachment->description, + attachment->mtime, + attachment->ctime, + size, data); + + retval = g_list_prepend (retval, ev_attachment); + } else { + if (error) { + g_warning ("%s", error->message); + g_error_free (error); + + g_free (data); + } + } + + g_object_unref (attachment); + } + + return g_list_reverse (retval); +} + +static gboolean +pdf_document_attachments_has_attachments (EvDocumentAttachments *document) +{ + PdfDocument *pdf_document = PDF_DOCUMENT (document); + + return poppler_document_has_attachments (pdf_document->document); +} + +static void +pdf_document_document_attachments_iface_init (EvDocumentAttachmentsIface *iface) +{ + iface->has_attachments = pdf_document_attachments_has_attachments; + iface->get_attachments = pdf_document_attachments_get_attachments; +} + /* Layers */ static gboolean pdf_document_layers_has_layers (EvDocumentLayers *document) diff --git a/libdocument/ev-document.c b/libdocument/ev-document.c index e1521b7b..9d1144a1 100644 --- a/libdocument/ev-document.c +++ b/libdocument/ev-document.c @@ -221,30 +221,6 @@ ev_document_get_info (EvDocument *document) return iface->get_info (document); } -gboolean -ev_document_has_attachments (EvDocument *document) -{ - EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document); - - if (iface->has_attachments == NULL) - return FALSE; - - return iface->has_attachments (document); -} - -GList * -ev_document_get_attachments (EvDocument *document) -{ - EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document); - GList *retval; - - if (iface->get_attachments == NULL) - return NULL; - retval = iface->get_attachments (document); - - return retval; -} - cairo_surface_t * ev_document_render (EvDocument *document, EvRenderContext *rc) diff --git a/libdocument/ev-document.h b/libdocument/ev-document.h index 5afdb035..e74c49ab 100644 --- a/libdocument/ev-document.h +++ b/libdocument/ev-document.h @@ -86,8 +86,6 @@ struct _EvDocumentIface double *height); char * (* get_page_label) (EvDocument *document, EvPage *page); - gboolean (* has_attachments) (EvDocument *document); - GList * (* get_attachments) (EvDocument *document); cairo_surface_t * (* render) (EvDocument *document, EvRenderContext *rc); EvDocumentInfo * (* get_info) (EvDocument *document); @@ -124,8 +122,6 @@ void ev_document_get_page_size (EvDocument *document, double *height); char *ev_document_get_page_label (EvDocument *document, EvPage *page); -gboolean ev_document_has_attachments (EvDocument *document); -GList *ev_document_get_attachments (EvDocument *document); cairo_surface_t *ev_document_render (EvDocument *document, EvRenderContext *rc); diff --git a/libview/ev-jobs.c b/libview/ev-jobs.c index 55e76a45..c0aa0b48 100644 --- a/libview/ev-jobs.c +++ b/libview/ev-jobs.c @@ -35,6 +35,7 @@ #include "ev-document-layers.h" #include "ev-document-print.h" #include "ev-document-annotations.h" +#include "ev-document-attachments.h" #include "ev-debug.h" #include @@ -402,13 +403,14 @@ ev_job_attachments_run (EvJob *job) ev_debug_message (DEBUG_JOBS, NULL); ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job); - + ev_document_doc_mutex_lock (); - job_attachments->attachments = ev_document_get_attachments (job->document); + job_attachments->attachments = + ev_document_attachments_get_attachments (EV_DOCUMENT_ATTACHMENTS (job->document)); ev_document_doc_mutex_unlock (); - + ev_job_succeeded (job); - + return FALSE; } diff --git a/shell/ev-sidebar-attachments.c b/shell/ev-sidebar-attachments.c index 677d61fa..e0779f01 100644 --- a/shell/ev-sidebar-attachments.c +++ b/shell/ev-sidebar-attachments.c @@ -31,6 +31,7 @@ #include #include +#include "ev-document-attachments.h" #include "ev-jobs.h" #include "ev-job-scheduler.h" #include "ev-file-helpers.h" @@ -656,9 +657,6 @@ ev_sidebar_attachments_set_document (EvSidebarPage *page, { EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (page); EvJob *job; - - if (!ev_document_has_attachments (document)) - return; if (!ev_attachbar->priv->icon_theme) { GdkScreen *screen; @@ -688,7 +686,8 @@ static gboolean ev_sidebar_attachments_support_document (EvSidebarPage *sidebar_page, EvDocument *document) { - return ev_document_has_attachments (document); + return (EV_IS_DOCUMENT_ATTACHMENTS (document) && + ev_document_attachments_has_attachments (document)); } static const gchar*