zathura/document.c

558 lines
12 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>
2011-03-05 21:00:41 +01:00
#include <sys/types.h>
#include <dirent.h>
#include <dlfcn.h>
2010-11-17 22:51:15 +01:00
#include "document.h"
#include "utils.h"
#include "zathura.h"
2011-04-18 21:22:35 +02:00
#include "render.h"
2010-11-18 02:35:33 +01:00
#define LENGTH(x) (sizeof(x)/sizeof((x)[0]))
2011-03-05 21:00:41 +01:00
void
2011-04-18 18:19:41 +02:00
zathura_document_plugins_load(zathura_t* zathura)
2011-03-05 21:00:41 +01:00
{
/* read all files in the plugin directory */
DIR* dir = opendir(PLUGIN_DIR);
if (dir == NULL) {
fprintf(stderr, "error: could not open plugin directory: %s\n", PLUGIN_DIR);
return;
}
struct dirent* entry;
while ((entry = readdir(dir)) != NULL) {
/* check if entry is a file */
if (entry->d_type != 0x8) {
continue;
}
void* handle = NULL;
zathura_document_plugin_t* plugin = NULL;
char* path = NULL;
/* get full path */
path = string_concat(PLUGIN_DIR, "/", entry->d_name, NULL);
if (path == NULL) {
goto error_continue;
}
/* load plugin */
handle = dlopen(path, RTLD_NOW);
2011-03-05 21:00:41 +01:00
if (handle == NULL) {
fprintf(stderr, "error: could not load plugin (%s)\n", dlerror());
goto error_free;
}
/* resolve symbol */
zathura_plugin_register_service_t register_plugin;
*(void**)(&register_plugin) = dlsym(handle, PLUGIN_REGISTER_FUNCTION);
2011-03-05 21:00:41 +01:00
if (register_plugin == NULL) {
fprintf(stderr, "error: could not find '%s' function in the plugin\n", PLUGIN_REGISTER_FUNCTION);
goto error_free;
}
2011-03-05 21:00:41 +01:00
plugin = malloc(sizeof(zathura_document_plugin_t));
2011-03-05 21:00:41 +01:00
if (plugin == NULL) {
goto error_free;
}
2011-03-05 21:00:41 +01:00
plugin->file_extension = NULL;
plugin->open_function = NULL;
2011-03-05 21:00:41 +01:00
register_plugin(plugin);
2011-03-05 21:00:41 +01:00
2011-04-18 18:19:41 +02:00
bool r = zathura_document_plugin_register(zathura, plugin, handle);
2011-03-05 21:00:41 +01:00
if (r == false) {
fprintf(stderr, "error: could not register plugin (%s)\n", path);
goto error_free;
2011-03-05 21:00:41 +01:00
}
free(path);
continue;
error_free:
free(path);
free(plugin);
if (handle) {
dlclose(handle);
}
error_continue:
continue;
2011-03-05 21:00:41 +01:00
}
if (closedir(dir) == -1) {
fprintf(stderr, "error: could not close plugin directory: %s\n", PLUGIN_DIR);
}
}
void
2011-04-18 18:19:41 +02:00
zathura_document_plugins_free(zathura_t* zathura)
2011-03-05 21:00:41 +01:00
{
2011-04-18 18:19:41 +02:00
if (zathura == NULL) {
return;
2011-03-05 21:00:41 +01:00
}
2011-04-18 18:19:41 +02:00
girara_list_iterator_t* iter = girara_list_iterator(zathura->plugins.plugins);
if (iter == NULL) {
return;
}
do {
zathura_document_plugin_t* plugin = (zathura_document_plugin_t*) girara_list_iterator_data(iter);
free(plugin->file_extension);
free(plugin);
} while (girara_list_iterator_next(iter));
girara_list_iterator_free(iter);
2011-03-05 21:00:41 +01:00
}
bool
2011-04-18 18:19:41 +02:00
zathura_document_plugin_register(zathura_t* zathura, zathura_document_plugin_t* new_plugin, void* handle)
{
if( (new_plugin == NULL) || (new_plugin->file_extension == NULL) || (new_plugin->open_function == NULL)
|| (handle == NULL) ) {
2011-04-18 18:19:41 +02:00
girara_error("plugin: could not register\n");
return false;
}
/* search existing plugins */
2011-04-18 18:19:41 +02:00
girara_list_iterator_t* iter = girara_list_iterator(zathura->plugins.plugins);
if (iter) {
do {
zathura_document_plugin_t* plugin = (zathura_document_plugin_t*) girara_list_iterator_data(iter);
if (!strcmp(plugin->file_extension, new_plugin->file_extension)) {
girara_error("plugin: already registered for filetype %s\n", plugin->file_extension);
girara_list_iterator_free(iter);
return false;
}
} while (girara_list_iterator_next(iter));
girara_list_iterator_free(iter);
}
2011-04-18 18:19:41 +02:00
girara_list_append(zathura->plugins.plugins, new_plugin);
return true;
}
2010-11-17 23:15:08 +01:00
zathura_document_t*
2011-04-18 18:19:41 +02:00
zathura_document_open(zathura_t* zathura, 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;
2011-04-18 18:19:41 +02:00
girara_list_iterator_t* iter = girara_list_iterator(zathura->plugins.plugins);
if (iter == NULL) {
2011-04-18 18:23:34 +02:00
goto error_free;
2011-04-18 18:19:41 +02:00
}
do {
zathura_document_plugin_t* plugin = (zathura_document_plugin_t*) girara_list_iterator_data(iter);
if (!strcmp(file_extension, plugin->file_extension)) {
2011-04-18 18:19:41 +02:00
girara_list_iterator_free(iter);
if (plugin->open_function) {
if (plugin->open_function(document)) {
2010-12-28 00:47:41 +01:00
/* update statusbar */
2011-04-18 18:19:41 +02: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;
}
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;
}
document->pages[page_id] = page;
}
2010-11-18 03:15:32 +01:00
return document;
2010-11-18 13:54:35 +01:00
} else {
2011-04-18 18:19:41 +02:00
girara_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
}
}
}
2011-04-18 18:19:41 +02:00
} while (girara_list_iterator_next(iter));
girara_list_iterator_free(iter);
2011-04-18 18:19:41 +02:00
girara_error("unknown file type\n");
2010-11-18 03:15:32 +01:00
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) {
2011-03-20 02:53:24 +01:00
page->number = page_id;
page->rendered = false;
page->event_box = gtk_event_box_new();
page->drawing_area = gtk_drawing_area_new();
2011-04-18 21:22:35 +02:00
g_signal_connect(page->drawing_area, "expose-event", G_CALLBACK(page_expose_event), page);
2011-03-20 02:53:24 +01:00
gtk_container_add(GTK_CONTAINER(page->event_box), page->drawing_area);
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
zathura_image_buffer_t*
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;
}
zathura_image_buffer_t* buffer = page->document->functions.page_render(page);
2011-01-06 08:59:10 +01:00
if (buffer) {
2011-01-06 08:59:10 +01:00
page->rendered = true;
}
return buffer;
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);
}
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;
}
2011-03-20 02:09:04 +01:00
image_buffer->width = width;
image_buffer->height = height;
image_buffer->rowstride = width * 3;
return image_buffer;
}
void
zathura_image_buffer_free(zathura_image_buffer_t* image_buffer)
{
if (image_buffer == NULL) {
return;
}
free(image_buffer->data);
}