X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=blobdiff_plain;ds=inline;f=backend%2Fpdf%2Fev-poppler.cc;h=93064456aede70e507db2309163f1793144e8cba;hb=c24535f0f7164b69d309eb62c36b020568c7e4bd;hp=ec2bbc0835b7f01636344c939e2930d2145c09ce;hpb=c561c0a1ff7ff46f1a1cf781b0d9b30a1599d1d7;p=evince.git diff --git a/backend/pdf/ev-poppler.cc b/backend/pdf/ev-poppler.cc index ec2bbc08..93064456 100644 --- a/backend/pdf/ev-poppler.cc +++ b/backend/pdf/ev-poppler.cc @@ -35,6 +35,7 @@ #include "ev-poppler.h" #include "ev-file-exporter.h" +#include "ev-mapping.h" #include "ev-document-find.h" #include "ev-document-misc.h" #include "ev-document-links.h" @@ -47,11 +48,17 @@ #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" #include "ev-image.h" +#include +#include +#include +#include + #if (defined (HAVE_POPPLER_PAGE_RENDER)) && (defined (HAVE_CAIRO_PDF) || defined (HAVE_CAIRO_PS)) #define HAVE_CAIRO_PRINT #endif @@ -85,12 +92,12 @@ typedef struct { struct _PdfDocumentClass { - GObjectClass parent_class; + EvDocumentClass parent_class; }; struct _PdfDocument { - GObject parent_instance; + EvDocument parent_instance; PopplerDocument *document; gchar *password; @@ -106,7 +113,6 @@ struct _PdfDocument GList *layers; }; -static void pdf_document_document_iface_init (EvDocumentIface *iface); static void pdf_document_security_iface_init (EvDocumentSecurityIface *iface); static void pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface); static void pdf_document_document_links_iface_init (EvDocumentLinksIface *iface); @@ -118,6 +124,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); @@ -157,6 +164,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, @@ -223,14 +232,6 @@ pdf_document_dispose (GObject *object) G_OBJECT_CLASS (pdf_document_parent_class)->dispose (object); } -static void -pdf_document_class_init (PdfDocumentClass *klass) -{ - GObjectClass *g_object_class = G_OBJECT_CLASS (klass); - - g_object_class->dispose = pdf_document_dispose; -} - static void pdf_document_init (PdfDocument *pdf_document) { @@ -355,122 +356,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, @@ -481,14 +366,11 @@ pdf_page_render (PopplerPage *page, #ifdef HAVE_POPPLER_PAGE_RENDER cairo_t *cr; - - surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, + + surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height); - memset (cairo_image_surface_get_data (surface), 0xff, - cairo_image_surface_get_height (surface) * - cairo_image_surface_get_stride (surface)); - cr = cairo_create (surface); + switch (rc->rotation) { case 90: cairo_translate (cr, width, 0); @@ -505,6 +387,11 @@ pdf_page_render (PopplerPage *page, cairo_scale (cr, rc->scale, rc->scale); cairo_rotate (cr, rc->rotation * G_PI / 180.0); poppler_page_render (page, cr); + + cairo_set_operator (cr, CAIRO_OPERATOR_DEST_OVER); + cairo_set_source_rgb (cr, 1., 1., 1.); + cairo_paint (cr); + cairo_destroy (cr); #else /* HAVE_POPPLER_PAGE_RENDER */ GdkPixbuf *pixbuf; @@ -552,27 +439,93 @@ pdf_document_render (EvDocument *document, width, height, rc); } -/* EvDocumentSecurity */ +/* reference: +http://www.pdfa.org/lib/exe/fetch.php?id=pdfa%3Aen%3Atechdoc&cache=cache&media=pdfa:techdoc:tn0001_pdfa-1_and_namespaces_2008-03-18.pdf */ +static char * +pdf_document_get_format_from_metadata (const char *metadata) +{ + xmlDocPtr doc; + xmlXPathContextPtr xpathCtx; + xmlXPathObjectPtr xpathObj; + xmlChar *part = NULL; + xmlChar *conf = NULL; + char *result = NULL; + int i; -static gboolean -pdf_document_has_document_security (EvDocumentSecurity *document_security) -{ - /* FIXME: do we really need to have this? */ - return FALSE; -} + doc = xmlParseMemory (metadata, strlen (metadata)); + if (doc == NULL) + return NULL; /* invalid xml metadata */ -static void -pdf_document_set_password (EvDocumentSecurity *document_security, - const char *password) -{ - PdfDocument *document = PDF_DOCUMENT (document_security); + xpathCtx = xmlXPathNewContext (doc); + if (xpathCtx == NULL) { + xmlFreeDoc (doc); + return NULL; /* invalid xpath context */ + } - if (document->password) - g_free (document->password); + /* add pdf/a namespaces */ + xmlXPathRegisterNs (xpathCtx, BAD_CAST "x", BAD_CAST "adobe:ns:meta/"); + xmlXPathRegisterNs (xpathCtx, BAD_CAST "rdf", BAD_CAST "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); + xmlXPathRegisterNs (xpathCtx, BAD_CAST "pdfaid", BAD_CAST "http://www.aiim.org/pdfa/ns/id/"); - document->password = g_strdup (password); + /* reads pdf/a part */ + /* first syntax: child node */ + xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/pdfaid:part", xpathCtx); + if (xpathObj != NULL) { + if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0) + part = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]); + + xmlXPathFreeObject (xpathObj); + } + if (part == NULL) { + /* second syntax: attribute */ + xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/@pdfaid:part", xpathCtx); + if (xpathObj != NULL) { + if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0) + part = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]); + + xmlXPathFreeObject (xpathObj); + } + } + + /* reads pdf/a conformance */ + /* first syntax: child node */ + xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/pdfaid:conformance", xpathCtx); + if (xpathObj != NULL) { + if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0) + conf = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]); + + xmlXPathFreeObject (xpathObj); + } + if (conf == NULL) { + /* second syntax: attribute */ + xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/@pdfaid:conformance", xpathCtx); + if (xpathObj != NULL) { + if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0) + conf = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]); + + xmlXPathFreeObject (xpathObj); + } + } + + if (part != NULL && conf != NULL) { + /* makes conf lowercase */ + for (i = 0; conf[i]; i++) + conf[i] = g_ascii_tolower (conf[i]); + + /* return buffer */ + result = g_strdup_printf ("PDF/A - %s%s", part, conf); + } + + /* Cleanup */ + xmlFree (part); + xmlFree (conf); + xmlXPathFreeContext (xpathCtx); + xmlFreeDoc (doc); + + return result; } + static EvDocumentInfo * pdf_document_get_info (EvDocument *document) { @@ -582,6 +535,8 @@ pdf_document_get_info (EvDocument *document) PopplerViewerPreferences view_prefs; PopplerPermissions permissions; EvPage *page; + char *metadata; + char *fmt; info = g_new0 (EvDocumentInfo, 1); @@ -618,18 +573,24 @@ pdf_document_get_info (EvDocument *document) "creation-date", &(info->creation_date), "mod-date", &(info->modified_date), "linearized", &(info->linearized), + "metadata", &metadata, NULL); + if (metadata != NULL) { + fmt = pdf_document_get_format_from_metadata(metadata); + if (fmt != NULL) { + g_free (info->format); + info->format = fmt; + } + g_free (metadata); + } + info->n_pages = ev_document_get_n_pages (document); if (info->n_pages > 0) { - page = ev_document_get_page (document, 0); - ev_document_get_page_size (document, page, + ev_document_get_page_size (document, 0, &(info->paper_width), &(info->paper_height)); - g_object_unref (page); - - // Convert to mm. info->paper_width = info->paper_width / 72.0f * 25.4f; info->paper_height = info->paper_height / 72.0f * 25.4f; @@ -725,19 +686,42 @@ pdf_document_get_info (EvDocument *document) } static void -pdf_document_document_iface_init (EvDocumentIface *iface) -{ - iface->save = pdf_document_save; - iface->load = pdf_document_load; - iface->get_n_pages = pdf_document_get_n_pages; - 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; -}; +pdf_document_class_init (PdfDocumentClass *klass) +{ + GObjectClass *g_object_class = G_OBJECT_CLASS (klass); + EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass); + + g_object_class->dispose = pdf_document_dispose; + + ev_document_class->save = pdf_document_save; + ev_document_class->load = pdf_document_load; + ev_document_class->get_n_pages = pdf_document_get_n_pages; + ev_document_class->get_page = pdf_document_get_page; + ev_document_class->get_page_size = pdf_document_get_page_size; + ev_document_class->get_page_label = pdf_document_get_page_label; + ev_document_class->render = pdf_document_render; + ev_document_class->get_info = pdf_document_get_info; +} + +/* EvDocumentSecurity */ +static gboolean +pdf_document_has_document_security (EvDocumentSecurity *document_security) +{ + /* FIXME: do we really need to have this? */ + return FALSE; +} + +static void +pdf_document_set_password (EvDocumentSecurity *document_security, + const char *password) +{ + PdfDocument *document = PDF_DOCUMENT (document_security); + + if (document->password) + g_free (document->password); + + document->password = g_strdup (password); +} static void pdf_document_security_iface_init (EvDocumentSecurityIface *iface) @@ -910,9 +894,11 @@ ev_link_dest_from_dest (PdfDocument *pdf_document, g_object_unref (poppler_page); } break; - case POPPLER_DEST_FIT: + case POPPLER_DEST_FITB: + case POPPLER_DEST_FIT: ev_dest = ev_link_dest_new_fit (dest->page_num - 1); break; + case POPPLER_DEST_FITBH: case POPPLER_DEST_FITH: { PopplerPage *poppler_page; double height; @@ -926,6 +912,7 @@ ev_link_dest_from_dest (PdfDocument *pdf_document, g_object_unref (poppler_page); } break; + case POPPLER_DEST_FITBV: case POPPLER_DEST_FITV: ev_dest = ev_link_dest_new_fitv (dest->page_num - 1, dest->left, @@ -946,15 +933,6 @@ ev_link_dest_from_dest (PdfDocument *pdf_document, g_object_unref (poppler_page); } break; - case POPPLER_DEST_FITB: - unimplemented_dest = "POPPLER_DEST_FITB"; - break; - case POPPLER_DEST_FITBH: - unimplemented_dest = "POPPLER_DEST_FITBH"; - break; - case POPPLER_DEST_FITBV: - unimplemented_dest = "POPPLER_DEST_FITBV"; - break; case POPPLER_DEST_NAMED: ev_dest = ev_link_dest_new_named (dest->named_dest); break; @@ -1154,17 +1132,17 @@ pdf_document_links_get_links (EvDocumentLinks *document_links, for (list = mapping_list; list; list = list->next) { PopplerLinkMapping *link_mapping; - EvLinkMapping *ev_link_mapping; + EvMapping *ev_link_mapping; link_mapping = (PopplerLinkMapping *)list->data; - ev_link_mapping = g_new (EvLinkMapping, 1); - ev_link_mapping->link = ev_link_from_action (pdf_document, + ev_link_mapping = g_new (EvMapping, 1); + ev_link_mapping->data = ev_link_from_action (pdf_document, link_mapping->action); - ev_link_mapping->x1 = link_mapping->area.x1; - ev_link_mapping->x2 = link_mapping->area.x2; + ev_link_mapping->area.x1 = link_mapping->area.x1; + ev_link_mapping->area.x2 = link_mapping->area.x2; /* Invert this for X-style coordinates */ - ev_link_mapping->y1 = height - link_mapping->area.y2; - ev_link_mapping->y2 = height - link_mapping->area.y1; + ev_link_mapping->area.y1 = height - link_mapping->area.y2; + ev_link_mapping->area.y2 = height - link_mapping->area.y1; retval = g_list_prepend (retval, ev_link_mapping); } @@ -1219,17 +1197,17 @@ pdf_document_images_get_image_mapping (EvDocumentImages *document_images, for (list = mapping_list; list; list = list->next) { PopplerImageMapping *image_mapping; - EvImageMapping *ev_image_mapping; + EvMapping *ev_image_mapping; image_mapping = (PopplerImageMapping *)list->data; - ev_image_mapping = g_new (EvImageMapping, 1); + ev_image_mapping = g_new (EvMapping, 1); - ev_image_mapping->image = ev_image_new (page, image_mapping->image_id); - ev_image_mapping->x1 = image_mapping->area.x1; - ev_image_mapping->x2 = image_mapping->area.x2; - ev_image_mapping->y1 = image_mapping->area.y1; - ev_image_mapping->y2 = image_mapping->area.y2; + ev_image_mapping->data = ev_image_new (page, image_mapping->image_id); + ev_image_mapping->area.x1 = image_mapping->area.x1; + ev_image_mapping->area.y1 = image_mapping->area.y1; + ev_image_mapping->area.x2 = image_mapping->area.x2; + ev_image_mapping->area.y2 = image_mapping->area.y2; retval = g_list_prepend (retval, ev_image_mapping); } @@ -1374,9 +1352,9 @@ pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnail poppler_page_get_size (POPPLER_PAGE (rc->page->backend_page), &page_width, &page_height); - *width = (gint) MAX (page_width * rc->scale, 1); - *height = (gint) MAX (page_height * rc->scale, 1); - + *width = MAX ((gint)(page_width * rc->scale + 0.5), 1); + *height = MAX ((gint)(page_height * rc->scale + 0.5), 1); + if (rc->rotation == 90 || rc->rotation == 270) { gint temp; @@ -2127,7 +2105,7 @@ pdf_document_forms_get_form_fields (EvDocumentForms *document, for (list = fields; list; list = list->next) { PopplerFormFieldMapping *mapping; - EvFormFieldMapping *field_mapping; + EvMapping *field_mapping; EvFormField *ev_field; mapping = (PopplerFormFieldMapping *)list->data; @@ -2136,13 +2114,13 @@ pdf_document_forms_get_form_fields (EvDocumentForms *document, if (!ev_field) continue; - field_mapping = g_new0 (EvFormFieldMapping, 1); - field_mapping->x1 = mapping->area.x1; - field_mapping->x2 = mapping->area.x2; - field_mapping->y1 = height - mapping->area.y2; - field_mapping->y2 = height - mapping->area.y1; - field_mapping->field = ev_field; - field_mapping->field->page = EV_PAGE (g_object_ref (page)); + field_mapping = g_new0 (EvMapping, 1); + field_mapping->area.x1 = mapping->area.x1; + field_mapping->area.x2 = mapping->area.x2; + field_mapping->area.y1 = height - mapping->area.y2; + field_mapping->area.y2 = height - mapping->area.y1; + field_mapping->data = ev_field; + ev_field->page = EV_PAGE (g_object_ref (page)); g_object_set_data_full (G_OBJECT (ev_field), "poppler-field", @@ -2430,13 +2408,13 @@ ev_annot_from_poppler_annot (PopplerAnnot *poppler_annot, PopplerAnnotMarkup *markup; gchar *label; gdouble opacity; - gboolean is_open; PopplerRectangle poppler_rect; markup = POPPLER_ANNOT_MARKUP (poppler_annot); if (poppler_annot_markup_get_popup_rectangle (markup, &poppler_rect)) { EvRectangle ev_rect; + gboolean is_open; gdouble height; poppler_page_get_size (POPPLER_PAGE (page->backend_page), @@ -2446,17 +2424,28 @@ ev_annot_from_poppler_annot (PopplerAnnot *poppler_annot, ev_rect.y1 = height - poppler_rect.y2; ev_rect.y2 = height - poppler_rect.y1; - g_object_set (ev_annot, "rectangle", &ev_rect, NULL); + is_open = poppler_annot_markup_get_popup_is_open (markup); + + g_object_set (ev_annot, + "rectangle", &ev_rect, + "is_open", is_open, + "has_popup", TRUE, + NULL); + } else { + /* FIXME: Use poppler_annot_markup_has_popup() when + * new poppler is released. + */ + g_object_set (ev_annot, + "has_popup", FALSE, + NULL); } label = poppler_annot_markup_get_label (markup); opacity = poppler_annot_markup_get_opacity (markup); - is_open = poppler_annot_markup_get_popup_is_open (markup); g_object_set (ev_annot, "label", label, "opacity", opacity, - "is_open", is_open, NULL); g_free (label); @@ -2485,7 +2474,7 @@ pdf_document_annotations_get_annotations (EvDocumentAnnotations *document_annota for (list = annots; list; list = list->next) { PopplerAnnotMapping *mapping; - EvAnnotationMapping *annot_mapping; + EvMapping *annot_mapping; EvAnnotation *ev_annot; mapping = (PopplerAnnotMapping *)list->data; @@ -2500,12 +2489,12 @@ pdf_document_annotations_get_annotations (EvDocumentAnnotations *document_annota if (!ev_annot->name) ev_annot->name = g_strdup_printf ("annot-%d-%d", page->index, i); - annot_mapping = g_new0 (EvAnnotationMapping, 1); - annot_mapping->x1 = mapping->area.x1; - annot_mapping->x2 = mapping->area.x2; - annot_mapping->y1 = height - mapping->area.y2; - annot_mapping->y2 = height - mapping->area.y1; - annot_mapping->annotation = ev_annot; + annot_mapping = g_new (EvMapping, 1); + annot_mapping->area.x1 = mapping->area.x1; + annot_mapping->area.x2 = mapping->area.x2; + annot_mapping->area.y1 = height - mapping->area.y2; + annot_mapping->area.y2 = height - mapping->area.y1; + annot_mapping->data = ev_annot; g_object_set_data_full (G_OBJECT (ev_annot), "poppler-annot", @@ -2542,6 +2531,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)