+/* 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 EvAnnotationTextIcon
+get_annot_text_icon (PopplerAnnotText *poppler_annot)
+{
+#ifdef HAVE_POPPLER_PAGE_ADD_ANNOT
+ gchar *icon = poppler_annot_text_get_icon (poppler_annot);
+ EvAnnotationTextIcon retval;
+
+ if (!icon)
+ return EV_ANNOTATION_TEXT_ICON_UNKNOWN;
+
+ if (strcmp (icon, POPPLER_ANNOT_TEXT_ICON_NOTE) == 0)
+ retval = EV_ANNOTATION_TEXT_ICON_NOTE;
+ else if (strcmp (icon, POPPLER_ANNOT_TEXT_ICON_COMMENT) == 0)
+ retval = EV_ANNOTATION_TEXT_ICON_COMMENT;
+ else if (strcmp (icon, POPPLER_ANNOT_TEXT_ICON_KEY) == 0)
+ retval = EV_ANNOTATION_TEXT_ICON_KEY;
+ else if (strcmp (icon, POPPLER_ANNOT_TEXT_ICON_HELP) == 0)
+ retval = EV_ANNOTATION_TEXT_ICON_HELP;
+ else if (strcmp (icon, POPPLER_ANNOT_TEXT_ICON_NEW_PARAGRAPH) == 0)
+ retval = EV_ANNOTATION_TEXT_ICON_NEW_PARAGRAPH;
+ else if (strcmp (icon, POPPLER_ANNOT_TEXT_ICON_PARAGRAPH) == 0)
+ retval = EV_ANNOTATION_TEXT_ICON_PARAGRAPH;
+ else if (strcmp (icon, POPPLER_ANNOT_TEXT_ICON_INSERT) == 0)
+ retval = EV_ANNOTATION_TEXT_ICON_INSERT;
+ else if (strcmp (icon, POPPLER_ANNOT_TEXT_ICON_CROSS) == 0)
+ retval = EV_ANNOTATION_TEXT_ICON_CROSS;
+ else if (strcmp (icon, POPPLER_ANNOT_TEXT_ICON_CIRCLE) == 0)
+ retval = EV_ANNOTATION_TEXT_ICON_CIRCLE;
+ else
+ retval = EV_ANNOTATION_TEXT_ICON_UNKNOWN;
+
+ g_free (icon);
+
+ return retval;
+#else
+ return EV_ANNOTATION_TEXT_ICON_UNKNOWN;
+#endif
+}
+
+static const gchar *
+get_poppler_annot_text_icon (EvAnnotationTextIcon icon)
+{
+#ifdef HAVE_POPPLER_PAGE_ADD_ANNOT
+ switch (icon) {
+ case EV_ANNOTATION_TEXT_ICON_NOTE:
+ return POPPLER_ANNOT_TEXT_ICON_NOTE;
+ case EV_ANNOTATION_TEXT_ICON_COMMENT:
+ return POPPLER_ANNOT_TEXT_ICON_COMMENT;
+ case EV_ANNOTATION_TEXT_ICON_KEY:
+ return POPPLER_ANNOT_TEXT_ICON_KEY;
+ case EV_ANNOTATION_TEXT_ICON_HELP:
+ return POPPLER_ANNOT_TEXT_ICON_HELP;
+ case EV_ANNOTATION_TEXT_ICON_NEW_PARAGRAPH:
+ return POPPLER_ANNOT_TEXT_ICON_NEW_PARAGRAPH;
+ case EV_ANNOTATION_TEXT_ICON_PARAGRAPH:
+ return POPPLER_ANNOT_TEXT_ICON_PARAGRAPH;
+ case EV_ANNOTATION_TEXT_ICON_INSERT:
+ return POPPLER_ANNOT_TEXT_ICON_INSERT;
+ case EV_ANNOTATION_TEXT_ICON_CROSS:
+ return POPPLER_ANNOT_TEXT_ICON_CROSS;
+ case EV_ANNOTATION_TEXT_ICON_CIRCLE:
+ return POPPLER_ANNOT_TEXT_ICON_CIRCLE;
+ case EV_ANNOTATION_TEXT_ICON_UNKNOWN:
+ default:
+ return POPPLER_ANNOT_TEXT_ICON_NOTE;
+ }
+#else
+ return "Note";
+#endif
+}
+
+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_annotation_text_set_is_open (ev_annot_text,
+ poppler_annot_text_get_is_open (poppler_text));
+ ev_annotation_text_set_icon (ev_annot_text, get_annot_text_icon (poppler_text));
+ }
+ break;
+ case POPPLER_ANNOT_FILE_ATTACHMENT: {
+ PopplerAnnotFileAttachment *poppler_annot_attachment;
+ EvAnnotationAttachment *ev_annot_attachment;
+ PopplerAttachment *poppler_attachment;
+ gchar *data = NULL;
+ gsize size;
+ GError *error = NULL;
+
+ poppler_annot_attachment = POPPLER_ANNOT_FILE_ATTACHMENT (poppler_annot);
+ poppler_attachment = poppler_annot_file_attachment_get_attachment (poppler_annot_attachment);
+
+ if (poppler_attachment &&
+ attachment_save_to_buffer (poppler_attachment, &data, &size, &error)) {
+ EvAttachment *ev_attachment;
+
+ ev_attachment = ev_attachment_new (poppler_attachment->name,
+ poppler_attachment->description,
+ poppler_attachment->mtime,
+ poppler_attachment->ctime,
+ size, data);
+ ev_annot = ev_annotation_attachment_new (page, ev_attachment);
+ g_object_unref (ev_attachment);
+ } else if (error) {
+ g_warning ("%s", error->message);
+ g_error_free (error);
+ }
+
+ if (poppler_attachment)
+ g_object_unref (poppler_attachment);
+ }
+ 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) {
+ time_t utime;
+ gchar *modified;
+ gchar *contents;
+ gchar *name;
+ GdkColor color;
+
+ contents = poppler_annot_get_contents (poppler_annot);
+ if (contents) {
+ ev_annotation_set_contents (ev_annot, contents);
+ g_free (contents);
+ }
+
+ name = poppler_annot_get_name (poppler_annot);
+ if (name) {
+ ev_annotation_set_name (ev_annot, name);
+ g_free (name);
+ }
+
+ modified = poppler_annot_get_modified (poppler_annot);
+ if (poppler_date_parse (modified, &utime)) {
+ ev_annotation_set_modified_from_time (ev_annot, utime);
+ } else {
+ ev_annotation_set_modified (ev_annot, modified);
+ }
+ g_free (modified);
+
+ poppler_annot_color_to_gdk_color (poppler_annot, &color);
+ ev_annotation_set_color (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,
+ "popup_is_open", is_open,
+ "has_popup", TRUE,
+ NULL);
+ } else {
+ 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 EvMappingList *
+pdf_document_annotations_get_annotations (EvDocumentAnnotations *document_annotations,
+ EvPage *page)
+{
+ GList *retval = NULL;
+ PdfDocument *pdf_document;
+ PopplerPage *poppler_page;
+ EvMappingList *mapping_list;
+ GList *annots;
+ GList *list;
+ gdouble height;
+ gint i = 0;
+
+ pdf_document = PDF_DOCUMENT (document_annotations);
+ poppler_page = POPPLER_PAGE (page->backend_page);
+
+ if (pdf_document->annots) {
+ mapping_list = (EvMappingList *)g_hash_table_lookup (pdf_document->annots,
+ GINT_TO_POINTER (page->index));
+ if (mapping_list)
+ return ev_mapping_list_ref (mapping_list);
+ }
+
+ 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_annotation_get_name (ev_annot)) {
+ gchar *name = g_strdup_printf ("annot-%d-%d", page->index, i);
+
+ ev_annotation_set_name (ev_annot, name);
+ g_free (name);
+ }
+
+ 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);
+
+ if (!retval)
+ return NULL;
+
+ if (!pdf_document->annots) {
+ pdf_document->annots = g_hash_table_new_full (g_direct_hash,
+ g_direct_equal,
+ (GDestroyNotify)NULL,
+ (GDestroyNotify)ev_mapping_list_unref);
+ }
+
+ mapping_list = ev_mapping_list_new (page->index, g_list_reverse (retval), (GDestroyNotify)g_object_unref);
+ g_hash_table_insert (pdf_document->annots,
+ GINT_TO_POINTER (page->index),
+ ev_mapping_list_ref (mapping_list));
+
+ return mapping_list;
+}
+
+static gboolean
+pdf_document_annotations_document_is_modified (EvDocumentAnnotations *document_annotations)
+{
+ return PDF_DOCUMENT (document_annotations)->annots_modified;
+}
+
+#ifdef HAVE_POPPLER_PAGE_ADD_ANNOT
+static void
+pdf_document_annotations_add_annotation (EvDocumentAnnotations *document_annotations,
+ EvAnnotation *annot,
+ EvRectangle *rect)
+{
+ PopplerAnnot *poppler_annot;
+ PdfDocument *pdf_document;
+ EvPage *page;
+ PopplerPage *poppler_page;
+ GList *list = NULL;
+ EvMappingList *mapping_list;
+ EvMapping *annot_mapping;
+ PopplerRectangle poppler_rect;
+ gdouble height;
+ PopplerColor poppler_color;
+ GdkColor color;
+ time_t utime;
+ gchar *modified;
+ gchar *name;
+
+ pdf_document = PDF_DOCUMENT (document_annotations);
+ page = ev_annotation_get_page (annot);
+ poppler_page = POPPLER_PAGE (page->backend_page);
+
+ poppler_page_get_size (poppler_page, NULL, &height);
+ poppler_rect.x1 = rect->x1;
+ poppler_rect.x2 = rect->x2;
+ poppler_rect.y1 = height - rect->y2;
+ poppler_rect.y2 = height - rect->y1;
+ poppler_annot = poppler_annot_text_new (pdf_document->document, &poppler_rect);
+
+ ev_annotation_get_color (annot, &color);
+ poppler_color.red = color.red;
+ poppler_color.green = color.green;
+ poppler_color.blue = color.blue;
+ poppler_annot_set_color (poppler_annot, &poppler_color);
+
+ if (EV_IS_ANNOTATION_MARKUP (annot)) {
+ EvAnnotationMarkup *markup = EV_ANNOTATION_MARKUP (annot);
+ const gchar *label;
+
+ if (ev_annotation_markup_has_popup (markup)) {
+ EvRectangle popup_rect;
+
+ ev_annotation_markup_get_rectangle (markup, &popup_rect);
+ poppler_rect.x1 = popup_rect.x1;
+ poppler_rect.x2 = popup_rect.x2;
+ poppler_rect.y1 = height - popup_rect.y2;
+ poppler_rect.y2 = height - popup_rect.y1;
+ poppler_annot_markup_set_popup (POPPLER_ANNOT_MARKUP (poppler_annot), &poppler_rect);
+ poppler_annot_markup_set_popup_is_open (POPPLER_ANNOT_MARKUP (poppler_annot),
+ ev_annotation_markup_get_popup_is_open (markup));
+ }
+
+ label = ev_annotation_markup_get_label (markup);
+ if (label)
+ poppler_annot_markup_set_label (POPPLER_ANNOT_MARKUP (poppler_annot), label);
+ }
+
+ if (EV_IS_ANNOTATION_TEXT (annot)) {
+ EvAnnotationText *text = EV_ANNOTATION_TEXT (annot);
+ EvAnnotationTextIcon icon;
+
+ icon = ev_annotation_text_get_icon (text);
+ poppler_annot_text_set_icon (POPPLER_ANNOT_TEXT (poppler_annot),
+ get_poppler_annot_text_icon (icon));
+ }
+ poppler_page_add_annot (poppler_page, poppler_annot);
+
+ annot_mapping = g_new (EvMapping, 1);
+ annot_mapping->area = *rect;
+ annot_mapping->data = annot;
+ g_object_set_data_full (G_OBJECT (annot),
+ "poppler-annot",
+ g_object_ref (poppler_annot),
+ (GDestroyNotify) g_object_unref);
+
+ if (pdf_document->annots) {
+ mapping_list = (EvMappingList *)g_hash_table_lookup (pdf_document->annots,
+ GINT_TO_POINTER (page->index));
+ list = ev_mapping_list_get_list (mapping_list);
+ name = g_strdup_printf ("annot-%d-%d", page->index, g_list_length (list) + 1);
+ ev_annotation_set_name (annot, name);
+ g_free (name);
+ list = g_list_append (list, annot_mapping);
+ } else {
+ pdf_document->annots = g_hash_table_new_full (g_direct_hash,
+ g_direct_equal,
+ (GDestroyNotify)NULL,
+ (GDestroyNotify)ev_mapping_list_unref);
+ name = g_strdup_printf ("annot-%d-0", page->index);
+ ev_annotation_set_name (annot, name);
+ g_free (name);
+ list = g_list_append (list, annot_mapping);
+ mapping_list = ev_mapping_list_new (page->index, list, (GDestroyNotify)g_object_unref);
+ g_hash_table_insert (pdf_document->annots,
+ GINT_TO_POINTER (page->index),
+ ev_mapping_list_ref (mapping_list));
+ }
+
+ pdf_document->annots_modified = TRUE;
+}
+#endif /* HAVE_POPPLER_PAGE_ADD_ANNOT */
+
+static void
+pdf_document_annotations_save_annotation (EvDocumentAnnotations *document_annotations,
+ EvAnnotation *annot,
+ EvAnnotationsSaveMask mask)
+{
+ PopplerAnnot *poppler_annot;
+
+ poppler_annot = POPPLER_ANNOT (g_object_get_data (G_OBJECT (annot), "poppler-annot"));
+ if (!poppler_annot)
+ return;
+
+ if (mask & EV_ANNOTATIONS_SAVE_CONTENTS)
+ poppler_annot_set_contents (poppler_annot,
+ ev_annotation_get_contents (annot));
+
+#ifdef HAVE_POPPLER_PAGE_ADD_ANNOT
+ if (mask & EV_ANNOTATIONS_SAVE_COLOR) {
+ PopplerColor color;
+ GdkColor ev_color;
+
+ ev_annotation_get_color (annot, &ev_color);
+ color.red = ev_color.red;
+ color.green = ev_color.green;
+ color.blue = ev_color.blue;
+ poppler_annot_set_color (poppler_annot, &color);
+ }
+
+ if (EV_IS_ANNOTATION_MARKUP (annot)) {
+ EvAnnotationMarkup *ev_markup = EV_ANNOTATION_MARKUP (annot);
+ PopplerAnnotMarkup *markup = POPPLER_ANNOT_MARKUP (poppler_annot);
+
+ if (mask & EV_ANNOTATIONS_SAVE_LABEL)
+ poppler_annot_markup_set_label (markup, ev_annotation_markup_get_label (ev_markup));
+ if (mask & EV_ANNOTATIONS_SAVE_OPACITY)
+ poppler_annot_markup_set_opacity (markup, ev_annotation_markup_get_opacity (ev_markup));
+ if (mask & EV_ANNOTATIONS_SAVE_POPUP_IS_OPEN)
+ poppler_annot_markup_set_popup_is_open (markup, ev_annotation_markup_get_popup_is_open (ev_markup));
+ }
+
+ if (EV_IS_ANNOTATION_TEXT (annot)) {
+ EvAnnotationText *ev_text = EV_ANNOTATION_TEXT (annot);
+ PopplerAnnotText *text = POPPLER_ANNOT_TEXT (poppler_annot);
+
+ if (mask & EV_ANNOTATIONS_SAVE_TEXT_IS_OPEN) {
+ poppler_annot_text_set_is_open (text,
+ ev_annotation_text_get_is_open (ev_text));
+ }
+ if (mask & EV_ANNOTATIONS_SAVE_TEXT_ICON) {
+ EvAnnotationTextIcon icon;
+
+ icon = ev_annotation_text_get_icon (ev_text);
+ poppler_annot_text_set_icon (text, get_poppler_annot_text_icon (icon));
+ }
+ }
+#endif /* HAVE_POPPLER_PAGE_ADD_ANNOT */
+ PDF_DOCUMENT (document_annotations)->annots_modified = TRUE;
+}
+
+static void
+pdf_document_document_annotations_iface_init (EvDocumentAnnotationsInterface *iface)
+{
+ iface->get_annotations = pdf_document_annotations_get_annotations;
+ iface->document_is_modified = pdf_document_annotations_document_is_modified;
+#ifdef HAVE_POPPLER_PAGE_ADD_ANNOT
+ iface->add_annotation = pdf_document_annotations_add_annotation;
+#endif
+ iface->save_annotation = pdf_document_annotations_save_annotation;
+}
+
+/* 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 (EvDocumentAttachmentsInterface *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 (EvDocumentLayersInterface *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;
+}