zathura/document.c

442 lines
9.4 KiB
C
Raw Normal View History

2010-11-17 22:51:15 +01:00
/* See LICENSE file for license and copyright information */
2010-11-18 13:54:35 +01:00
#define _BSD_SOURCE
#define _XOPEN_SOURCE 500
// TODO: Implement realpath
2010-11-17 22:51:15 +01:00
#include <stdlib.h>
2010-11-17 23:15:08 +01:00
#include <stdio.h>
2010-11-18 03:15:32 +01:00
#include <string.h>
2010-11-18 13:54:35 +01:00
#include <limits.h>
2010-11-17 22:51:15 +01:00
#include "document.h"
#include "utils.h"
#include "zathura.h"
2010-11-18 02:35:33 +01:00
#define LENGTH(x) (sizeof(x)/sizeof((x)[0]))
zathura_document_plugin_t* zathura_document_plugins = NULL;
bool
zathura_document_register_plugin(char* file_extension, zathura_document_open_t open_function)
{
if( (file_extension == NULL) || (open_function == NULL) ) {
fprintf(stderr, "plugin: could not register\n");
return false;
}
/* search existing plugins */
zathura_document_plugin_t* plugin = zathura_document_plugins;
while (plugin) {
if (!strcmp(plugin->file_extension, file_extension)) {
fprintf(stderr, "plugin: already registered for filetype %s\n", file_extension);
return false;
}
if (plugin->next == NULL) {
break;
}
plugin = plugin->next;
}
/* create new plugin */
zathura_document_plugin_t* new_plugin = malloc(sizeof(zathura_document_plugin_t));
if (new_plugin == NULL) {
return false;
}
new_plugin->file_extension = file_extension;
new_plugin->open_function = open_function;
new_plugin->next = NULL;
/* append to list */
if (plugin == NULL) {
zathura_document_plugins = new_plugin;
} else {
plugin->next = new_plugin;
}
return true;
}
void
zathura_document_plugin_free(void)
{
/* free registered plugins */
zathura_document_plugin_t* plugin = zathura_document_plugins;
while (plugin) {
zathura_document_plugin_t* tmp = plugin->next;
free(plugin);
plugin = tmp;
}
zathura_document_plugins = NULL;
}
2010-11-17 22:51:15 +01:00
2010-11-17 23:15:08 +01:00
zathura_document_t*
2010-11-18 02:35:33 +01:00
zathura_document_open(const char* path, const char* password)
2010-11-17 22:51:15 +01:00
{
2011-02-09 12:29:09 +01:00
if (!path) {
2010-12-28 00:47:41 +01:00
goto error_out;
2010-11-18 02:35:33 +01:00
}
2011-02-09 12:29:09 +01:00
if (!file_exists(path)) {
2010-11-18 02:35:33 +01:00
fprintf(stderr, "error: file does not exist\n");
2010-12-28 00:47:41 +01:00
goto error_out;
2010-11-18 02:35:33 +01:00
}
const char* file_extension = file_get_extension(path);
2011-02-09 12:29:09 +01:00
if (!file_extension) {
2010-11-18 02:35:33 +01:00
fprintf(stderr, "error: could not determine file type\n");
2010-12-28 00:47:41 +01:00
goto error_out;
2010-11-18 02:35:33 +01:00
}
2010-11-18 13:54:35 +01:00
/* determine real path */
size_t path_max;
#ifdef PATH_MAX
path_max = PATH_MAX;
#else
path_max = pathconf(path,_PC_PATH_MAX);
2011-02-09 12:29:09 +01:00
if (path_max <= 0)
2010-11-18 13:54:35 +01:00
path_max = 4096;
#endif
2010-12-28 00:47:41 +01:00
char* real_path = NULL;
zathura_document_t* document = NULL;
real_path = malloc(sizeof(char) * path_max);
2011-02-09 12:29:09 +01:00
if (!real_path) {
2010-12-28 00:47:41 +01:00
goto error_out;
2010-11-18 13:54:35 +01:00
}
2011-02-09 12:29:09 +01:00
if (!realpath(path, real_path)) {
2010-12-28 00:47:41 +01:00
goto error_free;
2010-11-18 13:54:35 +01:00
}
2010-12-28 00:47:41 +01:00
document = malloc(sizeof(zathura_document_t));
2011-02-09 12:29:09 +01:00
if (!document) {
2010-12-28 00:47:41 +01:00
goto error_free;
2010-11-18 02:35:33 +01:00
}
2010-11-18 13:54:35 +01:00
document->file_path = real_path;
2010-11-18 02:35:33 +01:00
document->password = password;
document->current_page_number = 0;
document->number_of_pages = 0;
2010-12-26 20:35:26 +01:00
document->scale = 1.0;
2010-11-18 02:35:33 +01:00
document->rotate = 0;
2010-11-18 03:15:32 +01:00
document->data = NULL;
2010-12-28 00:47:41 +01:00
document->pages = NULL;
2010-11-18 02:35:33 +01:00
document->functions.document_free = NULL;
document->functions.document_index_generate = NULL;
document->functions.document_save_as = NULL;
document->functions.document_attachments_get = NULL;
document->functions.page_get = NULL;
document->functions.page_free = NULL;
document->functions.page_search_text = NULL;
document->functions.page_links_get = NULL;
document->functions.page_form_fields_get = NULL;
document->functions.page_render = NULL;
/* init plugin with associated file type */
zathura_document_plugin_t* plugin = zathura_document_plugins;
while (plugin) {
if (!strcmp(file_extension, plugin->file_extension)) {
if (plugin->open_function) {
if (plugin->open_function(document)) {
2010-12-28 00:47:41 +01:00
/* update statusbar */
2010-11-18 21:22:43 +01:00
girara_statusbar_item_set_text(Zathura.UI.session, Zathura.UI.statusbar.file, real_path);
2010-12-28 00:47:41 +01:00
/* read all pages */
document->pages = calloc(document->number_of_pages, sizeof(zathura_page_t*));
2011-02-09 12:29:09 +01:00
if (!document->pages) {
2010-12-28 00:47:41 +01:00
goto error_free;
}
2010-12-29 11:46:13 +01:00
double offset = 0;
2011-02-09 21:28:36 +01:00
for (unsigned int page_id = 0; page_id < document->number_of_pages; page_id++) {
2010-12-28 00:47:41 +01:00
zathura_page_t* page = zathura_page_get(document, page_id);
2011-02-09 12:29:09 +01:00
if (!page) {
2010-12-28 00:47:41 +01:00
goto error_free;
}
page->offset = offset;
2010-12-29 11:46:13 +01:00
offset += page->height;
2010-12-28 00:47:41 +01:00
document->pages[page_id] = page;
}
2010-11-18 03:15:32 +01:00
return document;
2010-11-18 13:54:35 +01:00
} else {
fprintf(stderr, "error: could not open file\n");
2010-12-28 00:47:41 +01:00
goto error_free;
2010-11-18 03:15:32 +01:00
}
}
}
plugin = plugin->next;
2010-11-18 02:35:33 +01:00
}
2010-11-18 03:15:32 +01:00
fprintf(stderr, "error: unknown file type\n");
2010-12-28 00:47:41 +01:00
error_free:
2010-11-18 13:54:35 +01:00
free(real_path);
2010-12-28 00:47:41 +01:00
2011-02-09 12:29:09 +01:00
if (document && document->pages) {
for (unsigned int page_id = 0; page_id < document->number_of_pages; page_id++)
2010-12-28 00:47:41 +01:00
{
zathura_page_free(document->pages[page_id]);
}
free(document->pages);
}
2010-11-18 13:54:35 +01:00
free(document);
2010-12-28 00:47:41 +01:00
error_out:
2010-11-17 22:51:15 +01:00
return NULL;
}
2010-11-17 23:15:08 +01:00
bool
zathura_document_free(zathura_document_t* document)
2010-11-17 22:51:15 +01:00
{
2011-02-09 12:29:09 +01:00
if (!document) {
2010-11-18 03:15:32 +01:00
return false;
2010-11-17 23:15:08 +01:00
}
2010-12-28 00:47:41 +01:00
/* free pages */
2011-02-09 12:29:09 +01:00
for (unsigned int page_id = 0; page_id < document->number_of_pages; page_id++)
2010-12-28 00:47:41 +01:00
{
zathura_page_free(document->pages[page_id]);
}
free(document->pages);
/* free document */
2011-02-09 12:29:09 +01:00
if (!document->functions.document_free) {
2010-11-17 23:15:08 +01:00
fprintf(stderr, "error: %s not implemented\n", __FUNCTION__);
2010-11-18 13:54:35 +01:00
2011-02-09 12:29:09 +01:00
if (document->file_path) {
2010-11-18 13:54:35 +01:00
free(document->file_path);
}
2010-11-18 03:15:32 +01:00
free(document);
return true;
2010-11-17 23:15:08 +01:00
}
2010-11-18 02:35:33 +01:00
bool r = document->functions.document_free(document);
2011-02-09 12:29:09 +01:00
if (document->file_path) {
2010-11-18 13:54:35 +01:00
free(document->file_path);
}
2010-11-18 02:35:33 +01:00
free(document);
return r;
2010-11-17 23:15:08 +01:00
}
bool
zathura_document_save_as(zathura_document_t* document, const char* path)
{
2011-02-09 12:29:09 +01:00
if (!document || !path) {
2010-11-17 23:15:08 +01:00
return false;
}
2011-02-09 12:29:09 +01:00
if (!document->functions.document_save_as) {
2010-11-17 23:15:08 +01:00
fprintf(stderr, "error: %s not implemented\n", __FUNCTION__);
return false;
}
return document->functions.document_save_as(document, path);
2010-11-17 22:51:15 +01:00
}
girara_tree_node_t*
2010-11-17 23:15:08 +01:00
zathura_document_index_generate(zathura_document_t* document)
2010-11-17 22:51:15 +01:00
{
2011-02-09 12:29:09 +01:00
if (!document) {
2010-11-17 23:15:08 +01:00
return NULL;
}
2011-02-09 12:29:09 +01:00
if (!document->functions.document_index_generate) {
2010-11-17 23:15:08 +01:00
fprintf(stderr, "error: %s not implemented\n", __FUNCTION__);
return NULL;
}
return document->functions.document_index_generate(document);
2010-11-17 22:51:15 +01:00
}
zathura_list_t*
2010-11-17 23:15:08 +01:00
zathura_document_attachments_get(zathura_document_t* document)
2010-11-17 22:51:15 +01:00
{
2011-02-09 12:29:09 +01:00
if (!document) {
2010-11-17 23:15:08 +01:00
return NULL;
}
2011-02-09 12:29:09 +01:00
if (!document->functions.document_attachments_get) {
2010-11-17 23:15:08 +01:00
fprintf(stderr, "error: %s not implemented\n", __FUNCTION__);
return NULL;
}
return document->functions.document_attachments_get(document);
2010-11-17 22:51:15 +01:00
}
bool
2010-11-17 23:15:08 +01:00
zathura_document_attachments_free(zathura_list_t* list)
2010-11-17 22:51:15 +01:00
{
return false;
}
2010-11-17 23:15:08 +01:00
zathura_page_t*
zathura_page_get(zathura_document_t* document, unsigned int page_id)
2010-11-17 22:51:15 +01:00
{
2011-02-09 12:29:09 +01:00
if (!document) {
2010-11-17 23:15:08 +01:00
return NULL;
}
2011-02-09 12:29:09 +01:00
if (!document->functions.page_get) {
2010-11-17 23:15:08 +01:00
fprintf(stderr, "error: %s not implemented\n", __FUNCTION__);
return NULL;
}
zathura_page_t* page = document->functions.page_get(document, page_id);
2011-02-09 12:29:09 +01:00
if (page) {
page->number = page_id;
page->rendered = false;
g_static_mutex_init(&(page->lock));
}
return page;
2010-11-17 22:51:15 +01:00
}
2010-11-18 02:35:33 +01:00
bool
zathura_page_free(zathura_page_t* page)
{
2011-02-09 12:29:09 +01:00
if (!page || !page->document) {
2010-11-18 02:35:33 +01:00
return false;
}
2011-02-09 12:29:09 +01:00
if (!page->document->functions.page_free) {
2010-11-18 02:35:33 +01:00
fprintf(stderr, "error: %s not implemented\n", __FUNCTION__);
return false;
}
return page->document->functions.page_free(page);
}
2010-11-17 22:51:15 +01:00
zathura_list_t*
2010-11-17 23:15:08 +01:00
zathura_page_search_text(zathura_page_t* page, const char* text)
2010-11-17 22:51:15 +01:00
{
2011-02-09 12:29:09 +01:00
if (!page || !page->document || !text) {
2010-11-17 23:15:08 +01:00
return NULL;
}
2011-02-09 12:29:09 +01:00
if (!page->document->functions.page_search_text) {
2010-11-17 23:15:08 +01:00
fprintf(stderr, "error: %s not implemented\n", __FUNCTION__);
return NULL;
}
return page->document->functions.page_search_text(page, text);
2010-11-17 22:51:15 +01:00
}
2010-11-17 23:15:08 +01:00
zathura_list_t*
zathura_page_links_get(zathura_page_t* page)
2010-11-17 22:51:15 +01:00
{
2011-02-09 12:29:09 +01:00
if (!page || !page->document) {
2010-11-17 23:15:08 +01:00
return NULL;
}
2011-02-09 12:29:09 +01:00
if (!page->document->functions.page_links_get) {
2010-11-17 23:15:08 +01:00
fprintf(stderr, "error: %s not implemented\n", __FUNCTION__);
return NULL;
}
return page->document->functions.page_links_get(page);
2010-11-17 22:51:15 +01:00
}
bool
2010-11-17 23:15:08 +01:00
zathura_page_links_free(zathura_list_t* list)
2010-11-17 22:51:15 +01:00
{
return false;
}
zathura_list_t*
2010-11-17 23:15:08 +01:00
zathura_page_form_fields_get(zathura_page_t* page)
2010-11-17 22:51:15 +01:00
{
2011-02-09 12:29:09 +01:00
if (!page || !page->document) {
2010-11-17 23:15:08 +01:00
return NULL;
}
2011-02-09 12:29:09 +01:00
if (!page->document->functions.page_form_fields_get) {
2010-11-17 23:15:08 +01:00
fprintf(stderr, "error: %s not implemented\n", __FUNCTION__);
return NULL;
}
return page->document->functions.page_form_fields_get(page);
2010-11-17 22:51:15 +01:00
}
bool
2010-11-17 23:15:08 +01:00
zathura_page_form_fields_free(zathura_list_t* list)
2010-11-17 22:51:15 +01:00
{
return false;
}
2010-11-17 23:15:08 +01:00
GtkWidget*
2010-11-17 23:15:08 +01:00
zathura_page_render(zathura_page_t* page)
{
2011-02-09 12:29:09 +01:00
if (!page || !page->document) {
2010-11-17 23:15:08 +01:00
return NULL;
}
2011-02-09 12:29:09 +01:00
if (!page->document->functions.page_render) {
2010-11-17 23:15:08 +01:00
fprintf(stderr, "error: %s not implemented\n", __FUNCTION__);
return NULL;
}
2011-01-06 08:59:10 +01:00
GtkWidget* widget = page->document->functions.page_render(page);
2011-02-09 12:29:09 +01:00
if (widget) {
2011-01-06 08:59:10 +01:00
page->rendered = true;
}
return widget;
2010-11-17 23:15:08 +01:00
}
2010-12-25 00:47:52 +01:00
zathura_index_element_t*
zathura_index_element_new(const char* title)
{
2011-02-09 12:29:09 +01:00
if (!title) {
2010-12-25 00:47:52 +01:00
return NULL;
2010-12-26 01:12:20 +01:00
}
2010-12-25 00:47:52 +01:00
zathura_index_element_t* res = g_malloc0(sizeof(zathura_index_element_t));
2010-12-26 01:12:20 +01:00
2011-02-09 12:29:09 +01:00
if (!res) {
2010-12-25 00:47:52 +01:00
return NULL;
2010-12-26 01:12:20 +01:00
}
2010-12-25 00:47:52 +01:00
res->title = g_strdup(title);
2010-12-26 01:12:20 +01:00
2010-12-25 00:47:52 +01:00
return res;
}
void
zathura_index_element_free(zathura_index_element_t* index)
{
2011-02-09 12:29:09 +01:00
if (!index) {
2010-12-25 00:47:52 +01:00
return;
2010-12-26 01:12:20 +01:00
}
2010-12-25 00:47:52 +01:00
g_free(index->title);
2010-12-26 01:12:20 +01:00
2011-02-09 12:29:09 +01:00
if (index->type == ZATHURA_LINK_EXTERNAL) {
2010-12-25 00:47:52 +01:00
g_free(index->target.uri);
2010-12-26 01:12:20 +01:00
}
2010-12-25 00:47:52 +01:00
g_free(index);
}