zathura/ft/document.c

297 lines
6.3 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-11-18 02:35:33 +01:00
#include "../utils.h"
#include "pdf/pdf.h"
#define LENGTH(x) (sizeof(x)/sizeof((x)[0]))
zathura_document_plugin_t zathura_document_plugins[] = {
{ "pdf", pdf_document_open },
};
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) {
return NULL;
}
if(!file_exists(path)) {
fprintf(stderr, "error: file does not exist\n");
return NULL;
}
const char* file_extension = file_get_extension(path);
if(!file_extension) {
fprintf(stderr, "error: could not determine file type\n");
return NULL;
}
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
char* real_path = malloc(sizeof(char) * path_max);
if(!real_path) {
return NULL;
}
if(!realpath(path, real_path)) {
free(real_path);
return NULL;
}
2010-11-18 02:35:33 +01:00
zathura_document_t* document = malloc(sizeof(zathura_document_t));
if(!document) {
2010-11-18 13:54:35 +01:00
free(real_path);
2010-11-18 02:35:33 +01:00
return NULL;
}
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;
document->scale = 100;
document->rotate = 0;
2010-11-18 03:15:32 +01:00
document->data = 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)) {
return document;
2010-11-18 13:54:35 +01:00
} else {
fprintf(stderr, "error: could not open file\n");
free(real_path);
free(document);
return NULL;
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-11-18 13:54:35 +01:00
free(real_path);
free(document);
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-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
}
zathura_list_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
cairo_surface_t*
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);
}