attachments.c 2.01 KB
Newer Older
1 2 3 4 5 6 7
/* See LICENSE file for license and copyright information */

#include <girara/utils.h>

#include "plugin.h"

girara_list_t*
8
pdf_document_attachments_get(zathura_document_t* document, void* data, zathura_error_t* error)
9
{
10
  if (document == NULL || data == NULL) {
11 12 13 14 15 16
    if (error != NULL) {
      *error = ZATHURA_ERROR_INVALID_ARGUMENTS;
    }
    return NULL;
  }

17
  PopplerDocument* poppler_document = data;
18 19 20 21 22 23 24 25
  if (poppler_document_has_attachments(poppler_document) == FALSE) {
    girara_warning("PDF file has no attachments");
    if (error != NULL) {
      *error = ZATHURA_ERROR_UNKNOWN;
    }
    return NULL;
  }

26
  girara_list_t* res = girara_sorted_list_new2((girara_compare_function_t) g_strcmp0, g_free);
27 28 29 30 31 32 33 34
  if (res == NULL) {
    if (error != NULL) {
      *error = ZATHURA_ERROR_OUT_OF_MEMORY;
    }
    return NULL;
  }

  GList* attachment_list = poppler_document_get_attachments(poppler_document);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
35
  for (GList* attachments = attachment_list; attachments != NULL; attachments = g_list_next(attachments)) {
36 37 38 39 40 41 42 43 44
    PopplerAttachment* attachment = (PopplerAttachment*) attachments->data;
    girara_list_append(res, g_strdup(attachment->name));
  }

  return res;
}

zathura_error_t
pdf_document_attachment_save(zathura_document_t* document,
45
    void* data, const char* attachmentname, const char* file)
46
{
47
  if (document == NULL || data == NULL) {
48 49 50
    return ZATHURA_ERROR_INVALID_ARGUMENTS;
  }

51
  PopplerDocument* poppler_document = data;
52 53 54 55 56 57 58
  if (poppler_document_has_attachments(poppler_document) == FALSE) {
    girara_warning("PDF file has no attachments");
    return ZATHURA_ERROR_INVALID_ARGUMENTS;
  }


  GList* attachment_list = poppler_document_get_attachments(poppler_document);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
59
  for (GList* attachments = attachment_list; attachments != NULL; attachments = g_list_next(attachments)) {
60 61 62 63 64 65 66 67 68 69
    PopplerAttachment* attachment = (PopplerAttachment*) attachments->data;
    if (g_strcmp0(attachment->name, attachmentname) != 0) {
      continue;
    }

    return poppler_attachment_save(attachment, file, NULL);
  }

  return ZATHURA_ERROR_OK;
}