zathura/ft/document.c

382 lines
8.1 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"
2010-12-23 19:41:07 +01:00
#include "pdf/pdf.h"
#include "djvu/djvu.h"
2010-11-18 02:35:33 +01:00
#include "../utils.h"
2010-11-18 21:22:43 +01:00
#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[] = {
2010-12-23 19:41:07 +01:00
{ "pdf", pdf_document_open },
{ "djvu", djvu_document_open },
2010-11-18 02:35:33 +01:00
};
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
{
2010-11-18 02:35:33 +01:00
if(!path) {
2010-12-28 00:47:41 +01:00
goto error_out;
2010-11-18 02:35:33 +01:00
}
if(!file_exists(path)) {
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);
if(!file_extension) {
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);
if(path_max <= 0)
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);
2010-11-18 13:54:35 +01:00
if(!real_path) {
2010-12-28 00:47:41 +01:00
goto error_out;
2010-11-18 13:54:35 +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));
2010-11-18 02:35:33 +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 */
for(unsigned int i = 0; i < LENGTH(zathura_document_plugins); i++)
{
2010-11-18 03:15:32 +01:00
if(!strcmp(file_extension, zathura_document_plugins[i].file_extension)) {
if(zathura_document_plugins[i].open_function) {
if(zathura_document_plugins[i].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*));
if(!document->pages) {
goto error_free;
}
2010-12-29 11:46:13 +01:00
double offset = 0;
2010-12-28 00:47:41 +01:00
for(unsigned int page_id = 0; page_id < document->number_of_pages; page_id++)
{
zathura_page_t* page = zathura_page_get(document, page_id);
if(!page) {
goto error_free;
}
2010-12-29 11:46:13 +01:00
page->offset = offset;
page->number = page_id;
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
}
}
}
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
if(document && document->pages) {
for(unsigned int page_id = 0; page_id < document->number_of_pages; page_id++)
{
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
{
2010-11-17 23:15:08 +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 */
for(unsigned int page_id = 0; page_id < document->number_of_pages; page_id++)
{
zathura_page_free(document->pages[page_id]);
}
free(document->pages);
/* free document */
2010-11-18 03:15:32 +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
if(document->file_path) {
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);
2010-11-18 13:54:35 +01:00
if(document->file_path) {
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)
{
if(!document || !path) {
return false;
}
if(!document->functions.document_save_as) {
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
{
2010-11-17 23:15:08 +01:00
if(!document) {
return NULL;
}
if(!document->functions.document_index_generate) {
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
}
bool
2010-11-17 23:15:08 +01:00
zathura_document_index_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_document_attachments_get(zathura_document_t* document)
2010-11-17 22:51:15 +01:00
{
2010-11-17 23:15:08 +01:00
if(!document) {
return NULL;
}
if(!document->functions.document_attachments_get) {
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)
2010-11-17 22:51:15 +01:00
{
2010-11-18 14:51:13 +01:00
if(!document) {
2010-11-17 23:15:08 +01:00
return NULL;
}
if(!document->functions.page_get) {
fprintf(stderr, "error: %s not implemented\n", __FUNCTION__);
return NULL;
}
return document->functions.page_get(document, 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)
{
if(!page || !page->document) {
return false;
}
if(!page->document->functions.page_free) {
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
{
2010-11-17 23:15:08 +01:00
if(!page || !page->document || !text) {
return NULL;
}
if(!page->document->functions.page_search_text) {
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
{
2010-11-17 23:15:08 +01:00
if(!page || !page->document) {
return NULL;
}
if(!page->document->functions.page_links_get) {
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
{
2010-11-17 23:15:08 +01:00
if(!page || !page->document) {
return NULL;
}
if(!page->document->functions.page_form_fields_get) {
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)
{
if(!page || !page->document) {
return NULL;
}
if(!page->document->functions.page_render) {
fprintf(stderr, "error: %s not implemented\n", __FUNCTION__);
return NULL;
}
return page->document->functions.page_render(page);
}
2010-12-25 00:47:52 +01:00
zathura_index_element_t*
zathura_index_element_new(const char* title)
{
2010-12-26 01:12:20 +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
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)
{
2010-12-26 01:12:20 +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
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);
}