+
+/* Annotations */
+static void
+poppler_annot_color_to_gdk_color (PopplerAnnot *poppler_annot,
+ GdkColor *color)
+{
+ PopplerColor *poppler_color;
+
+ poppler_color = poppler_annot_get_color (poppler_annot);
+ if (poppler_color) {
+ color->red = poppler_color->red;
+ color->green = poppler_color->green;
+ color->blue = poppler_color->blue;
+
+ g_free (poppler_color);
+ } /* TODO: else use a default color */
+}
+
+static EvAnnotation *
+ev_annot_from_poppler_annot (PopplerAnnot *poppler_annot,
+ EvPage *page)
+{
+ EvAnnotation *ev_annot = NULL;
+ const gchar *unimplemented_annot = NULL;
+
+ switch (poppler_annot_get_annot_type (poppler_annot)) {
+ case POPPLER_ANNOT_TEXT:
+ PopplerAnnotText *poppler_text;
+ EvAnnotationText *ev_annot_text;
+
+ poppler_text = POPPLER_ANNOT_TEXT (poppler_annot);
+
+ ev_annot = ev_annotation_text_new (page);
+
+ ev_annot_text = EV_ANNOTATION_TEXT (ev_annot);
+ ev_annot_text->is_open = poppler_annot_text_get_is_open (poppler_text);
+
+ break;
+ case POPPLER_ANNOT_LINK:
+ case POPPLER_ANNOT_WIDGET:
+ /* Ignore link and widgets annots since they are already handled */
+ break;
+ default: {
+ GEnumValue *enum_value;
+
+ enum_value = g_enum_get_value ((GEnumClass *) g_type_class_ref (POPPLER_TYPE_ANNOT_TYPE),
+ poppler_annot_get_annot_type (poppler_annot));
+ unimplemented_annot = enum_value ? enum_value->value_name : "Unknown annotation";
+ }
+ }
+
+ if (unimplemented_annot) {
+ g_warning ("Unimplemented annotation: %s, please post a "
+ "bug report in Evince bugzilla "
+ "(http://bugzilla.gnome.org) with a testcase.",
+ unimplemented_annot);
+ }
+
+ if (ev_annot) {
+ ev_annot->contents = poppler_annot_get_contents (poppler_annot);
+ ev_annot->name = poppler_annot_get_name (poppler_annot);
+ ev_annot->modified = poppler_annot_get_modified (poppler_annot);
+ poppler_annot_color_to_gdk_color (poppler_annot, &ev_annot->color);
+
+ if (POPPLER_IS_ANNOT_MARKUP (poppler_annot)) {
+ PopplerAnnotMarkup *markup;
+ gchar *label;
+ gdouble opacity;
+ 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),
+ NULL, &height);
+ ev_rect.x1 = poppler_rect.x1;
+ ev_rect.x2 = poppler_rect.x2;
+ ev_rect.y1 = height - poppler_rect.y2;
+ ev_rect.y2 = height - poppler_rect.y1;
+
+ 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);
+
+ g_object_set (ev_annot,
+ "label", label,
+ "opacity", opacity,
+ NULL);
+
+ g_free (label);
+ }
+ }
+
+ return ev_annot;
+}
+
+static GList *
+pdf_document_annotations_get_annotations (EvDocumentAnnotations *document_annotations,
+ EvPage *page)
+{
+ GList *retval = NULL;
+ PdfDocument *pdf_document;
+ PopplerPage *poppler_page;
+ GList *annots;
+ GList *list;
+ gdouble height;
+ gint i = 0;
+
+ pdf_document = PDF_DOCUMENT (document_annotations);
+ poppler_page = POPPLER_PAGE (page->backend_page);
+ annots = poppler_page_get_annot_mapping (poppler_page);
+ poppler_page_get_size (poppler_page, NULL, &height);
+
+ for (list = annots; list; list = list->next) {
+ PopplerAnnotMapping *mapping;
+ EvMapping *annot_mapping;
+ EvAnnotation *ev_annot;
+
+ mapping = (PopplerAnnotMapping *)list->data;
+
+ ev_annot = ev_annot_from_poppler_annot (mapping->annot, page);
+ if (!ev_annot)
+ continue;
+
+ i++;
+
+ /* Make sure annot has a unique name */
+ if (!ev_annot->name)
+ ev_annot->name = g_strdup_printf ("annot-%d-%d", page->index, i);
+
+ 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",
+ g_object_ref (mapping->annot),
+ (GDestroyNotify) g_object_unref);
+
+ retval = g_list_prepend (retval, annot_mapping);
+ }
+
+ poppler_page_free_annot_mapping (annots);
+
+ return g_list_reverse (retval);
+}
+
+static void
+pdf_document_annotations_annotation_set_contents (EvDocumentAnnotations *document,
+ EvAnnotation *annot,
+ const gchar *contents)
+{
+ PopplerAnnot *poppler_annot;
+
+ poppler_annot = POPPLER_ANNOT (g_object_get_data (G_OBJECT (annot), "poppler-annot"));
+ if (!poppler_annot)
+ return;
+
+ poppler_annot_set_contents (poppler_annot, contents);
+ PDF_DOCUMENT (document)->modified = TRUE;
+}
+
+static void
+pdf_document_document_annotations_iface_init (EvDocumentAnnotationsIface *iface)
+{
+ iface->get_annotations = pdf_document_annotations_get_annotations;
+ 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)
+{
+ PdfDocument *pdf_document = PDF_DOCUMENT (document);
+ PopplerLayersIter *iter;
+
+ iter = poppler_layers_iter_new (pdf_document->document);
+ if (!iter)
+ return FALSE;
+ poppler_layers_iter_free (iter);
+
+ return TRUE;
+}
+
+static void
+build_layers_tree (PdfDocument *pdf_document,
+ GtkTreeModel *model,
+ GtkTreeIter *parent,
+ PopplerLayersIter *iter)
+{
+ do {
+ GtkTreeIter tree_iter;
+ PopplerLayersIter *child;
+ PopplerLayer *layer;
+ EvLayer *ev_layer = NULL;
+ gboolean visible;
+ gchar *markup;
+ gint rb_group = 0;
+
+ layer = poppler_layers_iter_get_layer (iter);
+ if (layer) {
+ markup = g_markup_escape_text (poppler_layer_get_title (layer), -1);
+ visible = poppler_layer_is_visible (layer);
+ rb_group = poppler_layer_get_radio_button_group_id (layer);
+ pdf_document->layers = g_list_append (pdf_document->layers,
+ g_object_ref (layer));
+ ev_layer = ev_layer_new (g_list_length (pdf_document->layers) - 1,
+ poppler_layer_is_parent (layer),
+ rb_group);
+ } else {
+ gchar *title;
+
+ title = poppler_layers_iter_get_title (iter);
+ markup = g_markup_escape_text (title, -1);
+ g_free (title);
+
+ visible = FALSE;
+ layer = NULL;
+ }
+
+ gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
+ gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
+ EV_DOCUMENT_LAYERS_COLUMN_TITLE, markup,
+ EV_DOCUMENT_LAYERS_COLUMN_VISIBLE, visible,
+ EV_DOCUMENT_LAYERS_COLUMN_ENABLED, TRUE, /* FIXME */
+ EV_DOCUMENT_LAYERS_COLUMN_SHOWTOGGLE, (layer != NULL),
+ EV_DOCUMENT_LAYERS_COLUMN_RBGROUP, rb_group,
+ EV_DOCUMENT_LAYERS_COLUMN_LAYER, ev_layer,
+ -1);
+ if (ev_layer)
+ g_object_unref (ev_layer);
+ g_free (markup);
+
+ child = poppler_layers_iter_get_child (iter);
+ if (child)
+ build_layers_tree (pdf_document, model, &tree_iter, child);
+ poppler_layers_iter_free (child);
+ } while (poppler_layers_iter_next (iter));
+}
+
+static GtkTreeModel *
+pdf_document_layers_get_layers (EvDocumentLayers *document)
+{
+ GtkTreeModel *model = NULL;
+ PdfDocument *pdf_document = PDF_DOCUMENT (document);
+ PopplerLayersIter *iter;
+
+ iter = poppler_layers_iter_new (pdf_document->document);
+ if (iter) {
+ model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LAYERS_N_COLUMNS,
+ G_TYPE_STRING, /* TITLE */
+ G_TYPE_OBJECT, /* LAYER */
+ G_TYPE_BOOLEAN, /* VISIBLE */
+ G_TYPE_BOOLEAN, /* ENABLED */
+ G_TYPE_BOOLEAN, /* SHOWTOGGLE */
+ G_TYPE_INT); /* RBGROUP */
+ build_layers_tree (pdf_document, model, NULL, iter);
+ poppler_layers_iter_free (iter);
+ }
+ return model;
+}
+
+static void
+pdf_document_layers_show_layer (EvDocumentLayers *document,
+ EvLayer *layer)
+{
+ PdfDocument *pdf_document = PDF_DOCUMENT (document);
+ guint layer_id = ev_layer_get_id (layer);
+
+ poppler_layer_show (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
+}
+
+static void
+pdf_document_layers_hide_layer (EvDocumentLayers *document,
+ EvLayer *layer)
+{
+ PdfDocument *pdf_document = PDF_DOCUMENT (document);
+ guint layer_id = ev_layer_get_id (layer);
+
+ poppler_layer_hide (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
+}
+
+static gboolean
+pdf_document_layers_layer_is_visible (EvDocumentLayers *document,
+ EvLayer *layer)
+{
+ PdfDocument *pdf_document = PDF_DOCUMENT (document);
+ guint layer_id = ev_layer_get_id (layer);
+
+ return poppler_layer_is_visible (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
+}
+
+static void
+pdf_document_document_layers_iface_init (EvDocumentLayersIface *iface)
+{
+ iface->has_layers = pdf_document_layers_has_layers;
+ iface->get_layers = pdf_document_layers_get_layers;
+ iface->show_layer = pdf_document_layers_show_layer;
+ iface->hide_layer = pdf_document_layers_hide_layer;
+ iface->layer_is_visible = pdf_document_layers_layer_is_visible;
+}