document.c 23.1 KB
Newer Older
Moritz Lipp's avatar
Moritz Lipp committed
1 2
/* See LICENSE file for license and copyright information */

Moritz Lipp's avatar
Moritz Lipp committed
3
#define _BSD_SOURCE
4
#define _XOPEN_SOURCE 700
Moritz Lipp's avatar
Moritz Lipp committed
5 6
// TODO: Implement realpath

Moritz Lipp's avatar
Moritz Lipp committed
7
#include <stdlib.h>
Moritz Lipp's avatar
Moritz Lipp committed
8
#include <stdio.h>
Moritz Lipp's avatar
Moritz Lipp committed
9
#include <string.h>
Moritz Lipp's avatar
Moritz Lipp committed
10
#include <limits.h>
11
#include <sys/stat.h>
Moritz Lipp's avatar
Moritz Lipp committed
12
#include <dlfcn.h>
13
#include <errno.h>
14
#include <glib.h>
15
#include <gio/gio.h>
16
#include <glib/gi18n.h>
Moritz Lipp's avatar
Moritz Lipp committed
17 18

#include "document.h"
19 20
#include "utils.h"
#include "zathura.h"
Moritz Lipp's avatar
Moritz Lipp committed
21
#include "render.h"
Pavel Borzenkov's avatar
Pavel Borzenkov committed
22
#include "database.h"
23
#include "page_widget.h"
Moritz Lipp's avatar
Moritz Lipp committed
24

25 26 27
#include <girara/datastructures.h>
#include <girara/utils.h>
#include <girara/statusbar.h>
28
#include <girara/session.h>
29
#include <girara/settings.h>
30

31 32 33 34 35
/**
 * Register document plugin
 */
static bool zathura_document_plugin_register(zathura_t* zathura, zathura_document_plugin_t* new_plugin);

Moritz Lipp's avatar
Moritz Lipp committed
36
void
Sebastian Ramacher's avatar
Sebastian Ramacher committed
37
zathura_document_plugins_load(zathura_t* zathura)
Moritz Lipp's avatar
Moritz Lipp committed
38
{
39
  GIRARA_LIST_FOREACH(zathura->plugins.path, char*, iter, plugindir)
40
    /* read all files in the plugin directory */
41
    GDir* dir = g_dir_open(plugindir, 0, NULL);
42 43
    if (dir == NULL) {
      girara_error("could not open plugin directory: %s", plugindir);
44
      girara_list_iterator_next(iter);
45 46 47
      continue;
    }

48 49 50 51 52 53 54 55 56 57 58
    char* name = NULL;
    while ((name = (char*) g_dir_read_name(dir)) != NULL) {
      char* path           = g_build_filename(plugindir, name, NULL);
      GFile* file          = g_file_new_for_path(path);
      GError* error        = NULL;
      GFileInfo* file_info = g_file_query_info(file, G_FILE_ATTRIBUTE_UNIX_MODE, 0, NULL, &error);
      if (file_info == NULL) {
        girara_error("failed to query file info for %s: %s", path, error->message);
        g_error_free(error);
        g_object_unref(file);
        g_free(path);
Moritz Lipp's avatar
Moritz Lipp committed
59 60 61
        continue;
      }

62 63 64
      const guint mode = g_file_info_get_attribute_uint32(file_info, G_FILE_ATTRIBUTE_UNIX_MODE);
      g_object_unref(file_info);
      g_object_unref(file);
65

66 67 68
      if (S_ISREG(mode) == 0) {
        girara_info("%s is not a regular file. Skipping.", path);
        g_free(path);
69 70
        continue;
      }
Moritz Lipp's avatar
Moritz Lipp committed
71

72 73
      void* handle                      = NULL;
      zathura_document_plugin_t* plugin = NULL;
Moritz Lipp's avatar
Moritz Lipp committed
74

75 76 77 78 79 80 81
      /* load plugin */
      handle = dlopen(path, RTLD_NOW);
      if (handle == NULL) {
        girara_error("could not load plugin %s (%s)", path, dlerror());
        g_free(path);
        continue;
      }
Moritz Lipp's avatar
Moritz Lipp committed
82

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
      /* resolve symbol and check API version*/
      zathura_plugin_api_version_t api_version;
      *(void**)(&api_version) = dlsym(handle, PLUGIN_API_VERSION_FUNCTION);
      if (api_version != NULL) {
        if (api_version() != ZATHURA_API_VERSION) {
          girara_error("plugin %s has been built againt zathura with a different API version (plugin: %d, zathura: %d)",
              path, api_version(), ZATHURA_API_VERSION);
          g_free(path);
          dlclose(handle);
          continue;
        }
      } else {
        girara_warning("could not find '%s' function in plugin %s ... loading anyway", PLUGIN_API_VERSION_FUNCTION, path);
      }

98 99
      zathura_plugin_register_service_t register_plugin;
      *(void**)(&register_plugin) = dlsym(handle, PLUGIN_REGISTER_FUNCTION);
Moritz Lipp's avatar
Moritz Lipp committed
100

101 102 103 104 105 106
      if (register_plugin == NULL) {
        girara_error("could not find '%s' function in plugin %s", PLUGIN_REGISTER_FUNCTION, path);
        g_free(path);
        dlclose(handle);
        continue;
      }
Moritz Lipp's avatar
Moritz Lipp committed
107

108 109 110
      plugin = g_malloc0(sizeof(zathura_document_plugin_t));
      plugin->content_types = girara_list_new2(g_free);
      plugin->handle = handle;
111

112
      register_plugin(plugin);
113

114
      bool r = zathura_document_plugin_register(zathura, plugin);
115

116 117
      if (r == false) {
        girara_error("could not register plugin %s", path);
118
        zathura_document_plugin_free(plugin);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
119
      } else {
120 121
        girara_info("successfully loaded plugin %s", path);
      }
122

123
      g_free(path);
124
    }
125
    g_dir_close(dir);
126
  GIRARA_LIST_FOREACH_END(zathura->plugins.path, char*, iter, plugindir);
Moritz Lipp's avatar
Moritz Lipp committed
127 128 129
}

void
130
zathura_document_plugin_free(zathura_document_plugin_t* plugin)
Moritz Lipp's avatar
Moritz Lipp committed
131
{
132
  if (plugin == NULL) {
Sebastian Ramacher's avatar
Sebastian Ramacher committed
133
    return;
Moritz Lipp's avatar
Moritz Lipp committed
134 135
  }

136
  dlclose(plugin->handle);
137
  girara_list_free(plugin->content_types);
138
  g_free(plugin);
Moritz Lipp's avatar
Moritz Lipp committed
139 140
}

141 142
static bool
zathura_document_plugin_register(zathura_t* zathura, zathura_document_plugin_t* new_plugin)
143
{
Sebastian Ramacher's avatar
Sebastian Ramacher committed
144
  if (new_plugin == NULL || new_plugin->content_types == NULL || new_plugin->open_function == NULL) {
Sebastian Ramacher's avatar
Sebastian Ramacher committed
145
    girara_error("plugin: could not register\n");
146 147 148
    return false;
  }

149 150 151 152
  bool atleastone = false;
  GIRARA_LIST_FOREACH(new_plugin->content_types, gchar*, iter, type)
    if (!zathura_type_plugin_mapping_new(zathura, type, new_plugin)) {
      girara_error("plugin: already registered for filetype %s\n", type);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
153 154
    } else {
      atleastone = true;
155
    }
Sebastian Ramacher's avatar
Sebastian Ramacher committed
156
  GIRARA_LIST_FOREACH_END(new_plugin->content_types, gchar*, iter, type);
157

158 159 160 161
  if (atleastone) {
    girara_list_append(zathura->plugins.plugins, new_plugin);
  }
  return atleastone;
162 163
}

164 165
static const gchar*
guess_type(const char* path)
Moritz Lipp's avatar
Moritz Lipp committed
166
{
167 168 169
  gboolean uncertain;
  const gchar* content_type = g_content_type_guess(path, NULL, 0, &uncertain);
  if (content_type == NULL) {
170
    return NULL;
Moritz Lipp's avatar
Moritz Lipp committed
171 172
  }

173 174
  FILE* f = fopen(path, "r");
  if (f == NULL) {
175
    return NULL;
Moritz Lipp's avatar
Moritz Lipp committed
176 177
  }

178 179 180 181 182 183 184
  const int fd = fileno(f);
  guchar* content = NULL;
  size_t length = 0u;
  while (uncertain == TRUE) {
    g_free((void*)content_type);
    content_type = NULL;

Sebastian Ramacher's avatar
Sebastian Ramacher committed
185 186
    content = g_realloc(content, length + BUFSIZ);
    const ssize_t r = read(fd, content + length, BUFSIZ);
187 188 189 190 191 192
    if (r == -1) {
      break;
    }

    length += r;
    content_type = g_content_type_guess(NULL, content, length, &uncertain);
Moritz Lipp's avatar
Moritz Lipp committed
193 194
  }

195
  fclose(f);
196 197 198
  if (uncertain == TRUE) {
    g_free((void*)content_type);
    content_type = NULL;
199
  }
200

201 202 203
  g_free(content);
  return content_type;
}
204

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
zathura_document_t*
zathura_document_open(zathura_t* zathura, const char* path, const char* password)
{
  if (path == NULL) {
    return NULL;
  }

  if (file_exists(path) == false) {
    girara_error("File '%s' does not exist", path);
    return NULL;
  }

  const gchar* content_type = guess_type(path);
  if (content_type == NULL) {
    girara_error("Could not determine file type.");
    return NULL;
221 222
  }

Moritz Lipp's avatar
Moritz Lipp committed
223
  /* determine real path */
224
  long path_max;
Moritz Lipp's avatar
Moritz Lipp committed
225 226 227 228
#ifdef PATH_MAX
  path_max = PATH_MAX;
#else
  path_max = pathconf(path,_PC_PATH_MAX);
229
  if (path_max <= 0)
Moritz Lipp's avatar
Moritz Lipp committed
230 231 232
    path_max = 4096;
#endif

Moritz Lipp's avatar
Moritz Lipp committed
233 234 235 236
  char* real_path              = NULL;
  zathura_document_t* document = NULL;

  real_path = malloc(sizeof(char) * path_max);
Moritz Lipp's avatar
Moritz Lipp committed
237
  if (real_path == NULL) {
238 239
    g_free((void*)content_type);
    return NULL;
Moritz Lipp's avatar
Moritz Lipp committed
240 241
  }

Moritz Lipp's avatar
Moritz Lipp committed
242
  if (realpath(path, real_path) == NULL) {
243 244 245 246 247 248 249 250 251 252 253
    g_free((void*)content_type);
    free(real_path);
    return NULL;
  }

  zathura_document_plugin_t* plugin = NULL;
  GIRARA_LIST_FOREACH(zathura->plugins.type_plugin_mapping, zathura_type_plugin_mapping_t*, iter, mapping)
    if (g_content_type_equals(content_type, mapping->type)) {
      plugin = mapping->plugin;
      break;
    }
Sebastian Ramacher's avatar
Sebastian Ramacher committed
254
  GIRARA_LIST_FOREACH_END(zathura->plugins.type_plugin_mapping, zathura_type_plugin_mapping_t*, iter, mapping);
255 256 257 258
  g_free((void*)content_type);

  if (plugin == NULL) {
    girara_error("unknown file type\n");
259
    goto error_free;
Moritz Lipp's avatar
Moritz Lipp committed
260 261
  }

262
  document = g_malloc0(sizeof(zathura_document_t));
Moritz Lipp's avatar
Moritz Lipp committed
263

Moritz Lipp's avatar
Moritz Lipp committed
264
  document->file_path           = real_path;
Moritz Lipp's avatar
Moritz Lipp committed
265
  document->password            = password;
Moritz Lipp's avatar
Moritz Lipp committed
266
  document->scale               = 1.0;
267
  document->zathura             = zathura;
Moritz Lipp's avatar
Moritz Lipp committed
268

269 270 271 272 273 274 275 276
  /* open document */
  if (plugin->open_function == NULL) {
    girara_error("plugin has no open function\n");
    goto error_free;
  }

  zathura_plugin_error_t error = plugin->open_function(document);
  if (error != ZATHURA_PLUGIN_ERROR_OK) {
Moritz Lipp's avatar
Moritz Lipp committed
277
    if (error == ZATHURA_PLUGIN_ERROR_INVALID_PASSWORD) {
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
      zathura_password_dialog_info_t* password_dialog_info = malloc(sizeof(zathura_password_dialog_info_t));
      if (password_dialog_info != NULL) {
        password_dialog_info->path    = g_strdup(path);
        password_dialog_info->zathura = zathura;

        if (path != NULL) {
          girara_dialog(zathura->ui.session, "Enter password:", true, NULL,
              (girara_callback_inputbar_activate_t) cb_password_dialog, password_dialog_info);
          goto error_free;
        } else {
          free(password_dialog_info);
        }
      }
      goto error_free;
    }

    girara_error("could not open document\n");
    goto error_free;
  }

  /* read history file */
Pavel Borzenkov's avatar
Pavel Borzenkov committed
299 300
  int offset = 0;
  zathura_db_get_fileinfo(zathura->database, document->file_path,
301
      &document->current_page_number, &offset, &document->scale, &document->rotate);
302

303
  /* check for valid scale value */
304
  if (document->scale <= FLT_EPSILON) {
305 306 307
    girara_warning("document info: '%s' has non positive scale", document->file_path);
    document->scale = 1;
  }
Pavel Borzenkov's avatar
Pavel Borzenkov committed
308

309
  /* check current page number */
Moritz Lipp's avatar
Moritz Lipp committed
310
  if (document->current_page_number > document->number_of_pages) {
311
    girara_warning("document info: '%s' has an invalid page number", document->file_path);
Moritz Lipp's avatar
Moritz Lipp committed
312
    document->current_page_number = 0;
313 314
  }

Moritz Lipp's avatar
Moritz Lipp committed
315 316
  /* update statusbar */
  girara_statusbar_item_set_text(zathura->ui.session, zathura->ui.statusbar.file, real_path);
317

Moritz Lipp's avatar
Moritz Lipp committed
318 319 320 321 322
  /* read all pages */
  document->pages = calloc(document->number_of_pages, sizeof(zathura_page_t*));
  if (document->pages == NULL) {
    goto error_free;
  }
323

Moritz Lipp's avatar
Moritz Lipp committed
324
  for (unsigned int page_id = 0; page_id < document->number_of_pages; page_id++) {
325
    zathura_page_t* page = zathura_page_get(document, page_id, NULL);
Moritz Lipp's avatar
Moritz Lipp committed
326 327
    if (page == NULL) {
      goto error_free;
Moritz Lipp's avatar
Moritz Lipp committed
328
    }
Moritz Lipp's avatar
Moritz Lipp committed
329 330 331 332

    document->pages[page_id] = page;
  }

333
  /* apply open adjustment */
Moritz Lipp's avatar
Moritz Lipp committed
334 335 336 337 338 339 340
  char* adjust_open = "best-fit";
  document->adjust_mode = ADJUST_BESTFIT;
  if (girara_setting_get(zathura->ui.session, "adjust-open", &(adjust_open)) == true) {
    if (g_strcmp0(adjust_open, "best-fit") == 0) {
      document->adjust_mode = ADJUST_BESTFIT;
    } else if (g_strcmp0(adjust_open, "width") == 0) {
      document->adjust_mode = ADJUST_WIDTH;
341 342
    } else {
      document->adjust_mode = ADJUST_NONE;
Moritz Lipp's avatar
Moritz Lipp committed
343 344 345
    }
    g_free(adjust_open);
  }
346

Moritz Lipp's avatar
Moritz Lipp committed
347
  return document;
Moritz Lipp's avatar
Moritz Lipp committed
348

Moritz Lipp's avatar
Moritz Lipp committed
349 350
error_free:

Moritz Lipp's avatar
Moritz Lipp committed
351
  free(real_path);
Moritz Lipp's avatar
Moritz Lipp committed
352

Moritz Lipp's avatar
Moritz Lipp committed
353 354
  if (document != NULL && document->pages != NULL) {
    for (unsigned int page_id = 0; page_id < document->number_of_pages; page_id++) {
Moritz Lipp's avatar
Moritz Lipp committed
355 356 357 358 359 360
      zathura_page_free(document->pages[page_id]);
    }

    free(document->pages);
  }

361
  g_free(document);
Moritz Lipp's avatar
Moritz Lipp committed
362 363 364
  return NULL;
}

365
zathura_plugin_error_t
Moritz Lipp's avatar
Moritz Lipp committed
366
zathura_document_free(zathura_document_t* document)
Moritz Lipp's avatar
Moritz Lipp committed
367
{
368
  if (document == NULL || document->zathura == NULL || document->zathura->ui.session == NULL) {
Moritz Lipp's avatar
Moritz Lipp committed
369
    return false;
Moritz Lipp's avatar
Moritz Lipp committed
370 371
  }

Moritz Lipp's avatar
Moritz Lipp committed
372
  /* free pages */
Moritz Lipp's avatar
Moritz Lipp committed
373
  for (unsigned int page_id = 0; page_id < document->number_of_pages; page_id++) {
Moritz Lipp's avatar
Moritz Lipp committed
374
    zathura_page_free(document->pages[page_id]);
Pavel Borzenkov's avatar
Pavel Borzenkov committed
375
    document->pages[page_id] = NULL;
Moritz Lipp's avatar
Moritz Lipp committed
376 377 378 379 380
  }

  free(document->pages);

  /* free document */
Sebastian Ramacher's avatar
Sebastian Ramacher committed
381
  bool r = true;
Moritz Lipp's avatar
Moritz Lipp committed
382
  if (document->functions.document_free == NULL) {
383
    girara_notify(document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
Moritz Lipp's avatar
Moritz Lipp committed
384
    girara_error("%s not implemented", __FUNCTION__);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
385 386
  } else {
    r = document->functions.document_free(document);
Moritz Lipp's avatar
Moritz Lipp committed
387 388
  }

Moritz Lipp's avatar
Moritz Lipp committed
389
  if (document->file_path != NULL) {
Moritz Lipp's avatar
Moritz Lipp committed
390 391 392
    free(document->file_path);
  }

393
  g_free(document);
Moritz Lipp's avatar
Moritz Lipp committed
394 395

  return r;
Moritz Lipp's avatar
Moritz Lipp committed
396 397
}

398
zathura_plugin_error_t
Moritz Lipp's avatar
Moritz Lipp committed
399 400
zathura_document_save_as(zathura_document_t* document, const char* path)
{
401
  if (document == NULL || path == NULL || document->zathura == NULL || document->zathura->ui.session == NULL) {
Moritz Lipp's avatar
Moritz Lipp committed
402 403 404
    return false;
  }

Moritz Lipp's avatar
Moritz Lipp committed
405
  if (document->functions.document_save_as == NULL) {
406
    girara_notify(document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
Moritz Lipp's avatar
Moritz Lipp committed
407
    girara_error("%s not implemented", __FUNCTION__);
Moritz Lipp's avatar
Moritz Lipp committed
408 409 410 411
    return false;
  }

  return document->functions.document_save_as(document, path);
Moritz Lipp's avatar
Moritz Lipp committed
412 413
}

414
girara_tree_node_t*
415
zathura_document_index_generate(zathura_document_t* document, zathura_plugin_error_t* error)
Moritz Lipp's avatar
Moritz Lipp committed
416
{
417
  if (document == NULL || document->zathura == NULL || document->zathura->ui.session == NULL) {
Moritz Lipp's avatar
Moritz Lipp committed
418 419 420
    return NULL;
  }

Moritz Lipp's avatar
Moritz Lipp committed
421
  if (document->functions.document_index_generate == NULL) {
422
    girara_notify(document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
Moritz Lipp's avatar
Moritz Lipp committed
423
    girara_error("%s not implemented", __FUNCTION__);
Moritz Lipp's avatar
Moritz Lipp committed
424 425 426
    return NULL;
  }

427
  return document->functions.document_index_generate(document, error);
Moritz Lipp's avatar
Moritz Lipp committed
428 429
}

Sebastian Ramacher's avatar
Sebastian Ramacher committed
430
girara_list_t*
431
zathura_document_attachments_get(zathura_document_t* document, zathura_plugin_error_t* error)
Moritz Lipp's avatar
Moritz Lipp committed
432
{
433
  if (document == NULL || document->zathura == NULL || document->zathura->ui.session == NULL) {
Moritz Lipp's avatar
Moritz Lipp committed
434 435 436
    return NULL;
  }

Moritz Lipp's avatar
Moritz Lipp committed
437
  if (document->functions.document_attachments_get == NULL) {
438
    girara_notify(document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
Moritz Lipp's avatar
Moritz Lipp committed
439
    girara_error("%s not implemented", __FUNCTION__);
Moritz Lipp's avatar
Moritz Lipp committed
440 441 442
    return NULL;
  }

443
  return document->functions.document_attachments_get(document, error);
Moritz Lipp's avatar
Moritz Lipp committed
444 445
}

446 447
zathura_plugin_error_t
zathura_document_attachment_save(zathura_document_t* document, const char* attachment, const char* file)
Moritz Lipp's avatar
Moritz Lipp committed
448
{
449
  if (document == NULL || document->zathura == NULL || document->zathura->ui.session == NULL) {
450
    return ZATHURA_PLUGIN_ERROR_INVALID_ARGUMENTS;
Sebastian Ramacher's avatar
Sebastian Ramacher committed
451 452 453
  }

  if (document->functions.document_attachment_save == NULL) {
454
    girara_notify(document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
455
    girara_error("%s not implemented", __FUNCTION__);
456
    return ZATHURA_PLUGIN_ERROR_NOT_IMPLEMENTED;
Sebastian Ramacher's avatar
Sebastian Ramacher committed
457 458 459
  }

  return document->functions.document_attachment_save(document, attachment, file);
Moritz Lipp's avatar
Moritz Lipp committed
460 461
}

Moritz Lipp's avatar
Moritz Lipp committed
462
char*
463
zathura_document_meta_get(zathura_document_t* document, zathura_document_meta_t meta, zathura_plugin_error_t* error)
Moritz Lipp's avatar
Moritz Lipp committed
464
{
465
  if (document == NULL || document->zathura == NULL || document->zathura->ui.session == NULL) {
466
    if (error != NULL) {
467
      *error = ZATHURA_PLUGIN_ERROR_INVALID_ARGUMENTS;
468
    }
Moritz Lipp's avatar
Moritz Lipp committed
469 470 471 472
    return NULL;
  }

  if (document->functions.document_meta_get == NULL) {
473 474 475
    if (error != NULL) {
      *error = ZATHURA_PLUGIN_ERROR_NOT_IMPLEMENTED;
    }
476
    girara_notify(document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
477
    girara_error("%s not implemented", __FUNCTION__);
Moritz Lipp's avatar
Moritz Lipp committed
478 479 480
    return NULL;
  }

481
  return document->functions.document_meta_get(document, meta, error);
Moritz Lipp's avatar
Moritz Lipp committed
482 483
}

Moritz Lipp's avatar
Moritz Lipp committed
484
zathura_page_t*
485
zathura_page_get(zathura_document_t* document, unsigned int page_id, zathura_plugin_error_t* error)
Moritz Lipp's avatar
Moritz Lipp committed
486
{
487 488 489 490
  if (document == NULL || document->zathura == NULL || document->zathura->ui.session == NULL) {
    if (error != NULL) {
      *error = ZATHURA_PLUGIN_ERROR_INVALID_ARGUMENTS;
    }
Moritz Lipp's avatar
Moritz Lipp committed
491 492 493
    return NULL;
  }

Moritz Lipp's avatar
Moritz Lipp committed
494
  if (document->functions.page_get == NULL) {
495
    girara_notify(document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
Moritz Lipp's avatar
Moritz Lipp committed
496
    girara_error("%s not implemented", __FUNCTION__);
497 498 499
    if (error != NULL) {
      *error = ZATHURA_PLUGIN_ERROR_NOT_IMPLEMENTED;
    }
Moritz Lipp's avatar
Moritz Lipp committed
500 501 502
    return NULL;
  }

503
  zathura_page_t* page = document->functions.page_get(document, page_id, error);
504

Moritz Lipp's avatar
Moritz Lipp committed
505
  if (page != NULL) {
Moritz Lipp's avatar
Moritz Lipp committed
506
    page->number       = page_id;
507
    page->visible      = false;
508
    page->drawing_area = zathura_page_widget_new(page);
509 510 511 512 513 514
    if (page->drawing_area == NULL) {
      girara_error("couldn't create page widget");
      zathura_page_free(page);
      return NULL;
    }

515
    page->document     = document;
Moritz Lipp's avatar
Moritz Lipp committed
516

517 518 519 520 521
    unsigned int page_height = 0;
    unsigned int page_width = 0;
    page_calc_height_width(page, &page_height, &page_width, true);

    gtk_widget_set_size_request(page->drawing_area, page_width, page_height);
522 523 524
  }

  return page;
Moritz Lipp's avatar
Moritz Lipp committed
525 526
}

527
zathura_plugin_error_t
Moritz Lipp's avatar
Moritz Lipp committed
528 529
zathura_page_free(zathura_page_t* page)
{
530 531
  if (page == NULL || page->document == NULL || page->document->zathura == NULL || page->document->zathura->ui.session == NULL) {
    return ZATHURA_PLUGIN_ERROR_INVALID_ARGUMENTS;
Moritz Lipp's avatar
Moritz Lipp committed
532 533
  }

Moritz Lipp's avatar
Moritz Lipp committed
534
  if (page->document->functions.page_free == NULL) {
535
    girara_notify(page->document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
Moritz Lipp's avatar
Moritz Lipp committed
536
    girara_error("%s not implemented", __FUNCTION__);
537
    return ZATHURA_PLUGIN_ERROR_NOT_IMPLEMENTED;
Moritz Lipp's avatar
Moritz Lipp committed
538 539 540 541 542
  }

  return page->document->functions.page_free(page);
}

Sebastian Ramacher's avatar
Sebastian Ramacher committed
543
girara_list_t*
544
zathura_page_search_text(zathura_page_t* page, const char* text, zathura_plugin_error_t* error)
Moritz Lipp's avatar
Moritz Lipp committed
545
{
546 547 548 549 550
  if (page == NULL || page->document == NULL || text == NULL ||
      page->document->zathura == NULL || page->document->zathura->ui.session == NULL) {
    if (error != NULL) {
      *error = ZATHURA_PLUGIN_ERROR_INVALID_ARGUMENTS;
    }
Moritz Lipp's avatar
Moritz Lipp committed
551 552 553
    return NULL;
  }

Moritz Lipp's avatar
Moritz Lipp committed
554
  if (page->document->functions.page_search_text == NULL) {
555
    girara_notify(page->document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
Moritz Lipp's avatar
Moritz Lipp committed
556
    girara_error("%s not implemented", __FUNCTION__);
557 558 559
    if (error != NULL) {
      *error = ZATHURA_PLUGIN_ERROR_NOT_IMPLEMENTED;
    }
Moritz Lipp's avatar
Moritz Lipp committed
560 561 562
    return NULL;
  }

563
  return page->document->functions.page_search_text(page, text, error);
Moritz Lipp's avatar
Moritz Lipp committed
564 565
}

Sebastian Ramacher's avatar
Sebastian Ramacher committed
566
girara_list_t*
567
zathura_page_links_get(zathura_page_t* page, zathura_plugin_error_t* error)
Moritz Lipp's avatar
Moritz Lipp committed
568
{
569 570 571 572 573
  if (page == NULL || page->document == NULL || page->document->zathura == NULL
      || page->document->zathura->ui.session == NULL) {
    if (error != NULL) {
      *error = ZATHURA_PLUGIN_ERROR_INVALID_ARGUMENTS;
    }
Moritz Lipp's avatar
Moritz Lipp committed
574 575 576
    return NULL;
  }

Moritz Lipp's avatar
Moritz Lipp committed
577
  if (page->document->functions.page_links_get == NULL) {
578
    girara_notify(page->document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
Moritz Lipp's avatar
Moritz Lipp committed
579
    girara_error("%s not implemented", __FUNCTION__);
580 581 582
    if (error != NULL) {
      *error = ZATHURA_PLUGIN_ERROR_NOT_IMPLEMENTED;
    }
Moritz Lipp's avatar
Moritz Lipp committed
583 584 585
    return NULL;
  }

586
  return page->document->functions.page_links_get(page, error);
Moritz Lipp's avatar
Moritz Lipp committed
587 588
}

589
zathura_plugin_error_t
Sebastian Ramacher's avatar
Sebastian Ramacher committed
590
zathura_page_links_free(girara_list_t* UNUSED(list))
Moritz Lipp's avatar
Moritz Lipp committed
591 592 593 594
{
  return false;
}

Sebastian Ramacher's avatar
Sebastian Ramacher committed
595
girara_list_t*
596
zathura_page_form_fields_get(zathura_page_t* page, zathura_plugin_error_t* error)
Moritz Lipp's avatar
Moritz Lipp committed
597
{
598 599 600 601 602
  if (page == NULL || page->document == NULL || page->document->zathura == NULL
      || page->document->zathura->ui.session == NULL) {
    if (error != NULL) {
      *error = ZATHURA_PLUGIN_ERROR_INVALID_ARGUMENTS;
    }
Moritz Lipp's avatar
Moritz Lipp committed
603 604 605
    return NULL;
  }

Moritz Lipp's avatar
Moritz Lipp committed
606
  if (page->document->functions.page_form_fields_get == NULL) {
607
    girara_notify(page->document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
Moritz Lipp's avatar
Moritz Lipp committed
608
    girara_error("%s not implemented", __FUNCTION__);
609 610 611
    if (error != NULL) {
      *error = ZATHURA_PLUGIN_ERROR_NOT_IMPLEMENTED;
    }
Moritz Lipp's avatar
Moritz Lipp committed
612 613 614
    return NULL;
  }

615
  return page->document->functions.page_form_fields_get(page, error);
Moritz Lipp's avatar
Moritz Lipp committed
616 617
}

618
zathura_plugin_error_t
Sebastian Ramacher's avatar
Sebastian Ramacher committed
619
zathura_page_form_fields_free(girara_list_t* UNUSED(list))
Moritz Lipp's avatar
Moritz Lipp committed
620
{
621
  return ZATHURA_PLUGIN_ERROR_NOT_IMPLEMENTED;
Moritz Lipp's avatar
Moritz Lipp committed
622
}
Moritz Lipp's avatar
Moritz Lipp committed
623

624
girara_list_t*
625
zathura_page_images_get(zathura_page_t* page, zathura_plugin_error_t* error)
626
{
627 628 629 630 631
  if (page == NULL || page->document == NULL || page->document->zathura == NULL
      || page->document->zathura->ui.session == NULL) {
    if (error != NULL) {
      *error = ZATHURA_PLUGIN_ERROR_INVALID_ARGUMENTS;
    }
632 633 634 635
    return NULL;
  }

  if (page->document->functions.page_images_get == NULL) {
636
    girara_notify(page->document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
637
    girara_error("%s not implemented", __FUNCTION__);
638 639 640 641
    if (error != NULL) {
      *error = ZATHURA_PLUGIN_ERROR_NOT_IMPLEMENTED;
    }
    return NULL;
642 643
  }

644
  return page->document->functions.page_images_get(page, error);
645 646
}

647 648
cairo_surface_t*
zathura_page_image_get_cairo(zathura_page_t* page, zathura_image_t* image, zathura_plugin_error_t* error)
649
{
650 651 652 653 654 655 656
  if (page == NULL || page->document == NULL || image == NULL ||
      page->document->zathura == NULL || page->document->zathura->ui.session ==
      NULL) {
    if (error != NULL) {
      *error = ZATHURA_PLUGIN_ERROR_INVALID_ARGUMENTS;
    }
    return NULL;
657 658
  }

659
  if (page->document->functions.page_image_get_cairo == NULL) {
660
    girara_notify(page->document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
661
    girara_error("%s not implemented", __FUNCTION__);
662 663 664 665
    if (error != NULL) {
      *error = ZATHURA_PLUGIN_ERROR_NOT_IMPLEMENTED;
    }
    return NULL;
666 667
  }

668
  return page->document->functions.page_image_get_cairo(page, image, error);
669 670
}

671 672
char* zathura_page_get_text(zathura_page_t* page, zathura_rectangle_t rectangle, zathura_plugin_error_t* error)
{
673
  if (page == NULL || page->document == NULL || page->document->zathura == NULL || page->document->zathura->ui.session == NULL) {
674 675 676 677 678 679 680
    if (error) {
      *error = ZATHURA_PLUGIN_ERROR_INVALID_ARGUMENTS;
    }
    return NULL;
  }

  if (page->document->functions.page_get_text == NULL) {
681
    girara_notify(page->document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
682 683 684 685 686 687 688 689 690 691
    girara_error("%s not implemented", __FUNCTION__);
    if (error) {
      *error = ZATHURA_PLUGIN_ERROR_NOT_IMPLEMENTED;
    }
    return NULL;
  }

  return page->document->functions.page_get_text(page, rectangle, error);
}

692
zathura_plugin_error_t
693
zathura_page_render(zathura_page_t* page, cairo_t* cairo, bool printing)
Moritz Lipp's avatar
Moritz Lipp committed
694
{
695 696 697
  if (page == NULL || page->document == NULL || cairo == NULL ||
      page->document->zathura == NULL || page->document->zathura->ui.session ==
      NULL) {
698
    return ZATHURA_PLUGIN_ERROR_INVALID_ARGUMENTS;
Moritz Lipp's avatar
Moritz Lipp committed
699 700
  }

Moritz Lipp's avatar
Moritz Lipp committed
701
  if (page->document->functions.page_render_cairo == NULL) {
702
    girara_notify(page->document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
Moritz Lipp's avatar
Moritz Lipp committed
703
    girara_error("%s not implemented", __FUNCTION__);
704
    return ZATHURA_PLUGIN_ERROR_NOT_IMPLEMENTED;
Moritz Lipp's avatar
Moritz Lipp committed
705 706
  }

707
  return page->document->functions.page_render_cairo(page, cairo, printing);
Moritz Lipp's avatar
Moritz Lipp committed
708
}
709 710 711 712

zathura_index_element_t*
zathura_index_element_new(const char* title)
{
Moritz Lipp's avatar
Moritz Lipp committed
713
  if (title == NULL) {
714
    return NULL;
Moritz Lipp's avatar
Moritz Lipp committed
715
  }
716 717

  zathura_index_element_t* res = g_malloc0(sizeof(zathura_index_element_t));
Moritz Lipp's avatar
Moritz Lipp committed
718

719
  res->title = g_strdup(title);
Moritz Lipp's avatar
Moritz Lipp committed
720

721 722 723 724 725 726
  return res;
}

void
zathura_index_element_free(zathura_index_element_t* index)
{
Moritz Lipp's avatar
Moritz Lipp committed
727
  if (index == NULL) {
728
    return;
Moritz Lipp's avatar
Moritz Lipp committed
729
  }
730 731

  g_free(index->title);
Moritz Lipp's avatar
Moritz Lipp committed
732

Moritz Lipp's avatar
Moritz Lipp committed
733
  if (index->type == ZATHURA_LINK_EXTERNAL) {
734
    g_free(index->target.uri);
Moritz Lipp's avatar
Moritz Lipp committed
735 736
  }

737 738
  g_free(index);
}
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755

zathura_image_buffer_t*
zathura_image_buffer_create(unsigned int width, unsigned int height)
{
  zathura_image_buffer_t* image_buffer = malloc(sizeof(zathura_image_buffer_t));

  if (image_buffer == NULL) {
    return NULL;
  }

  image_buffer->data = calloc(width * height * 3, sizeof(unsigned char));

  if (image_buffer->data == NULL) {
    free(image_buffer);
    return NULL;
  }

Moritz Lipp's avatar
Moritz Lipp committed
756 757 758
  image_buffer->width     = width;
  image_buffer->height    = height;
  image_buffer->rowstride = width * 3;
759 760 761 762 763 764 765 766 767 768 769 770

  return image_buffer;
}

void
zathura_image_buffer_free(zathura_image_buffer_t* image_buffer)
{
  if (image_buffer == NULL) {
    return;
  }

  free(image_buffer->data);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
771
  free(image_buffer);
772
}
773 774 775 776 777 778 779 780 781 782 783

bool
zathura_type_plugin_mapping_new(zathura_t* zathura, const gchar* type, zathura_document_plugin_t* plugin)
{
  g_return_val_if_fail(zathura && type && plugin, false);

  GIRARA_LIST_FOREACH(zathura->plugins.type_plugin_mapping, zathura_type_plugin_mapping_t*, iter, mapping)
    if (g_content_type_equals(type, mapping->type)) {
      girara_list_iterator_free(iter);
      return false;
    }
Sebastian Ramacher's avatar
Sebastian Ramacher committed
784
  GIRARA_LIST_FOREACH_END(zathura->plugins.type_plugin_mapping, zathura_type_plugin_mapping_t*, iter, mapping);
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802

  zathura_type_plugin_mapping_t* mapping = g_malloc(sizeof(zathura_type_plugin_mapping_t));
  mapping->type = g_strdup(type);
  mapping->plugin = plugin;
  girara_list_append(zathura->plugins.type_plugin_mapping, mapping);
  return true;
}

void
zathura_type_plugin_mapping_free(zathura_type_plugin_mapping_t* mapping)
{
  if (mapping == NULL) {
    return;
  }

  g_free((void*)mapping->type);
  g_free(mapping);
}
Sebastian Ramacher's avatar
Sebastian Ramacher committed
803 804 805 806 807 808 809 810 811 812 813 814 815

void
zathura_link_free(zathura_link_t* link)
{
  if (link == NULL) {
    return;
  }

  if (link->type == ZATHURA_LINK_EXTERNAL) {
    g_free(link->target.value);
  }
  g_free(link);
}