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
|
2011-04-19 00:36:56 +02:00
|
|
|
#define _XOPEN_SOURCE 700
|
2010-11-18 13:54:35 +01:00
|
|
|
|
2012-04-01 21:15:32 +02:00
|
|
|
#include <sys/wait.h>
|
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-04-19 00:36:56 +02:00
|
|
|
#include <errno.h>
|
2011-10-23 19:26:06 +02:00
|
|
|
#include <glib.h>
|
2012-03-04 18:45:58 +01:00
|
|
|
#include <glib/gi18n.h>
|
2013-02-08 10:58:57 +01:00
|
|
|
#ifdef WITH_MAGIC
|
|
|
|
#include <magic.h>
|
|
|
|
#endif
|
2013-07-25 21:57:39 +02:00
|
|
|
#include <unistd.h>
|
2010-11-17 22:51:15 +01:00
|
|
|
|
2012-03-27 13:30:04 +02:00
|
|
|
#include <girara/datastructures.h>
|
|
|
|
#include <girara/utils.h>
|
|
|
|
#include <girara/statusbar.h>
|
|
|
|
#include <girara/session.h>
|
|
|
|
#include <girara/settings.h>
|
|
|
|
|
2013-10-23 09:46:00 +02:00
|
|
|
#include "adjustment.h"
|
2010-11-17 22:51:15 +01:00
|
|
|
#include "document.h"
|
2011-03-05 19:46:05 +01:00
|
|
|
#include "utils.h"
|
|
|
|
#include "zathura.h"
|
2011-04-18 21:22:35 +02:00
|
|
|
#include "render.h"
|
2011-10-20 15:55:10 +02:00
|
|
|
#include "database.h"
|
2012-03-26 14:44:56 +02:00
|
|
|
#include "page.h"
|
2012-03-16 14:37:54 +01:00
|
|
|
#include "page-widget.h"
|
2012-03-27 13:30:04 +02:00
|
|
|
#include "plugin.h"
|
2011-10-23 17:01:15 +02:00
|
|
|
|
2012-03-23 18:08:24 +01:00
|
|
|
/** Read a most GT_MAX_READ bytes before falling back to file. */
|
|
|
|
static const size_t GT_MAX_READ = 1 << 16;
|
|
|
|
|
2012-03-27 13:30:04 +02:00
|
|
|
static const gchar* guess_type(const char* path);
|
2011-10-21 15:00:57 +02:00
|
|
|
|
2012-03-27 21:59:35 +02:00
|
|
|
/**
|
|
|
|
* Document
|
|
|
|
*/
|
2012-10-09 01:12:18 +02:00
|
|
|
struct zathura_document_s {
|
2012-03-27 21:59:35 +02:00
|
|
|
char* file_path; /**< File path of the document */
|
2013-07-22 15:01:05 +02:00
|
|
|
char* basename; /**< Basename of the document */
|
2012-03-27 21:59:35 +02:00
|
|
|
const char* password; /**< Password of the document */
|
|
|
|
unsigned int current_page_number; /**< Current page number */
|
|
|
|
unsigned int number_of_pages; /**< Number of pages */
|
|
|
|
double scale; /**< Scale value */
|
|
|
|
unsigned int rotate; /**< Rotation */
|
|
|
|
void* data; /**< Custom data */
|
|
|
|
zathura_adjust_mode_t adjust_mode; /**< Adjust mode (best-fit, width) */
|
2013-05-08 18:28:32 +02:00
|
|
|
int page_offset; /**< Page offset */
|
2013-10-20 17:00:07 +02:00
|
|
|
double cell_width; /**< width of a page cell in the document (not ransformed by scale and rotation) */
|
|
|
|
double cell_height; /**< height of a page cell in the document (not ransformed by scale and rotation) */
|
2013-10-25 11:33:34 +02:00
|
|
|
unsigned int view_width; /**< width of current viewport */
|
|
|
|
unsigned int view_height; /**< height of current viewport */
|
2013-10-20 17:05:15 +02:00
|
|
|
unsigned int pages_per_row; /**< number of pages in a row */
|
|
|
|
unsigned int first_page_column; /**< column of the first page */
|
|
|
|
unsigned int page_padding; /**< padding between pages */
|
2013-10-25 11:33:34 +02:00
|
|
|
double position_x; /**< X adjustment */
|
|
|
|
double position_y; /**< Y adjustment */
|
2012-03-27 21:59:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Document pages
|
|
|
|
*/
|
|
|
|
zathura_page_t** pages;
|
|
|
|
|
2012-03-30 18:24:00 +02:00
|
|
|
/**
|
|
|
|
* Used plugin
|
|
|
|
*/
|
|
|
|
zathura_plugin_t* plugin;
|
2012-03-27 21:59:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-10-23 19:26:06 +02:00
|
|
|
zathura_document_t*
|
2012-04-03 09:02:45 +02:00
|
|
|
zathura_document_open(zathura_plugin_manager_t* plugin_manager, const char*
|
2012-10-09 01:12:18 +02:00
|
|
|
path, const char* password, zathura_error_t* error)
|
2011-10-23 19:26:06 +02:00
|
|
|
{
|
|
|
|
if (path == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-19 17:03:27 +01:00
|
|
|
if (g_file_test(path, G_FILE_TEST_EXISTS) == FALSE) {
|
2011-10-23 19:26:06 +02:00
|
|
|
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;
|
2011-10-21 15:00:57 +02:00
|
|
|
}
|
|
|
|
|
2010-11-18 13:54:35 +01:00
|
|
|
/* determine real path */
|
2011-10-05 14:20:59 +02:00
|
|
|
long path_max;
|
2010-11-18 13:54:35 +01:00
|
|
|
#ifdef PATH_MAX
|
|
|
|
path_max = PATH_MAX;
|
|
|
|
#else
|
|
|
|
path_max = pathconf(path,_PC_PATH_MAX);
|
2011-10-05 14:20:59 +02: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-09-21 00:25:41 +02:00
|
|
|
if (real_path == NULL) {
|
2011-09-29 15:23:13 +02:00
|
|
|
g_free((void*)content_type);
|
|
|
|
return NULL;
|
2010-11-18 13:54:35 +01:00
|
|
|
}
|
|
|
|
|
2011-09-21 00:25:41 +02:00
|
|
|
if (realpath(path, real_path) == NULL) {
|
2011-09-29 15:23:13 +02:00
|
|
|
g_free((void*)content_type);
|
|
|
|
free(real_path);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-04-03 09:02:45 +02:00
|
|
|
zathura_plugin_t* plugin = zathura_plugin_manager_get_plugin(plugin_manager, content_type);
|
2011-09-29 15:23:13 +02:00
|
|
|
g_free((void*)content_type);
|
|
|
|
|
|
|
|
if (plugin == NULL) {
|
|
|
|
girara_error("unknown file type\n");
|
2013-05-28 23:39:18 +02:00
|
|
|
*error = ZATHURA_ERROR_UNKNOWN;
|
2011-09-30 12:32:29 +02:00
|
|
|
goto error_free;
|
2010-11-18 13:54:35 +01:00
|
|
|
}
|
|
|
|
|
2011-09-29 12:07:07 +02:00
|
|
|
document = g_malloc0(sizeof(zathura_document_t));
|
2010-11-18 02:35:33 +01:00
|
|
|
|
2012-04-21 00:00:03 +02:00
|
|
|
document->file_path = real_path;
|
2013-07-22 15:01:05 +02:00
|
|
|
document->basename = g_path_get_basename(real_path);
|
2012-04-21 00:00:03 +02:00
|
|
|
document->password = password;
|
|
|
|
document->scale = 1.0;
|
|
|
|
document->plugin = plugin;
|
|
|
|
document->adjust_mode = ZATHURA_ADJUST_NONE;
|
2013-10-20 17:00:07 +02:00
|
|
|
document->cell_width = 0.0;
|
|
|
|
document->cell_height = 0.0;
|
2013-10-25 11:33:34 +02:00
|
|
|
document->view_height = 0;
|
|
|
|
document->view_width = 0;
|
|
|
|
document->position_x = 0.0;
|
|
|
|
document->position_y = 0.0;
|
2010-11-18 02:35:33 +01:00
|
|
|
|
2012-02-08 23:21:27 +01:00
|
|
|
/* open document */
|
2012-06-13 16:08:24 +02:00
|
|
|
zathura_plugin_functions_t* functions = zathura_plugin_get_functions(plugin);
|
|
|
|
if (functions->document_open == NULL) {
|
2012-02-08 23:21:27 +01:00
|
|
|
girara_error("plugin has no open function\n");
|
|
|
|
goto error_free;
|
|
|
|
}
|
|
|
|
|
2012-06-13 16:08:24 +02:00
|
|
|
zathura_error_t int_error = functions->document_open(document);
|
2012-04-03 09:02:45 +02:00
|
|
|
if (int_error != ZATHURA_ERROR_OK) {
|
|
|
|
if (error != NULL) {
|
|
|
|
*error = int_error;
|
2012-02-08 23:21:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
girara_error("could not open document\n");
|
|
|
|
goto error_free;
|
|
|
|
}
|
|
|
|
|
2012-02-07 19:25:47 +01:00
|
|
|
/* read all pages */
|
|
|
|
document->pages = calloc(document->number_of_pages, sizeof(zathura_page_t*));
|
|
|
|
if (document->pages == NULL) {
|
|
|
|
goto error_free;
|
|
|
|
}
|
2011-09-29 15:23:13 +02:00
|
|
|
|
2012-02-07 19:25:47 +01:00
|
|
|
for (unsigned int page_id = 0; page_id < document->number_of_pages; page_id++) {
|
2012-03-26 15:17:01 +02:00
|
|
|
zathura_page_t* page = zathura_page_new(document, page_id, NULL);
|
2012-02-07 19:25:47 +01:00
|
|
|
if (page == NULL) {
|
|
|
|
goto error_free;
|
2010-11-18 03:15:32 +01:00
|
|
|
}
|
2012-02-07 19:25:47 +01:00
|
|
|
|
|
|
|
document->pages[page_id] = page;
|
2013-10-20 17:00:07 +02:00
|
|
|
|
|
|
|
/* cell_width and cell_height is the maximum of all the pages width and height */
|
2013-11-05 02:23:52 +01:00
|
|
|
const double width = zathura_page_get_width(page);
|
2013-10-20 17:00:07 +02:00
|
|
|
if (document->cell_width < width)
|
|
|
|
document->cell_width = width;
|
|
|
|
|
2013-11-05 02:23:52 +01:00
|
|
|
const double height = zathura_page_get_height(page);
|
2013-10-20 17:00:07 +02:00
|
|
|
if (document->cell_height < height)
|
|
|
|
document->cell_height = height;
|
2012-02-07 19:25:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return document;
|
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-09-21 00:25:41 +02:00
|
|
|
if (document != NULL && document->pages != NULL) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-09-29 12:07:07 +02:00
|
|
|
g_free(document);
|
2010-11-17 22:51:15 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-27 13:30:04 +02:00
|
|
|
zathura_error_t
|
2010-11-17 23:15:08 +01:00
|
|
|
zathura_document_free(zathura_document_t* document)
|
2010-11-17 22:51:15 +01:00
|
|
|
{
|
2012-03-27 21:59:35 +02:00
|
|
|
if (document == NULL || document->plugin == NULL) {
|
2012-03-27 13:30:04 +02:00
|
|
|
return ZATHURA_ERROR_INVALID_ARGUMENTS;
|
2010-11-17 23:15:08 +01:00
|
|
|
}
|
|
|
|
|
2010-12-28 00:47:41 +01:00
|
|
|
/* free pages */
|
2011-09-21 00:25:41 +02: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]);
|
2011-07-21 14:39:25 +02:00
|
|
|
document->pages[page_id] = NULL;
|
2010-12-28 00:47:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
free(document->pages);
|
|
|
|
|
|
|
|
/* free document */
|
2012-03-27 13:30:04 +02:00
|
|
|
zathura_error_t error = ZATHURA_ERROR_OK;
|
2012-06-13 16:08:24 +02:00
|
|
|
zathura_plugin_functions_t* functions = zathura_plugin_get_functions(document->plugin);
|
|
|
|
if (functions->document_free == NULL) {
|
2012-03-27 13:30:04 +02:00
|
|
|
error = ZATHURA_ERROR_NOT_IMPLEMENTED;
|
2011-10-21 23:40:16 +02:00
|
|
|
} else {
|
2012-06-13 16:08:24 +02:00
|
|
|
error = functions->document_free(document, document->data);
|
2010-11-17 23:15:08 +01:00
|
|
|
}
|
|
|
|
|
2011-09-21 00:25:41 +02:00
|
|
|
if (document->file_path != NULL) {
|
2010-11-18 13:54:35 +01:00
|
|
|
free(document->file_path);
|
|
|
|
}
|
2013-07-22 15:01:05 +02:00
|
|
|
g_free(document->basename);
|
2010-11-18 13:54:35 +01:00
|
|
|
|
2011-09-29 12:07:07 +02:00
|
|
|
g_free(document);
|
2010-11-18 02:35:33 +01:00
|
|
|
|
2012-03-05 08:22:16 +01:00
|
|
|
return error;
|
2010-11-17 23:15:08 +01:00
|
|
|
}
|
|
|
|
|
2012-03-27 14:44:09 +02:00
|
|
|
const char*
|
|
|
|
zathura_document_get_path(zathura_document_t* document)
|
|
|
|
{
|
|
|
|
if (document == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return document->file_path;
|
|
|
|
}
|
|
|
|
|
2013-07-22 15:01:05 +02:00
|
|
|
const char*
|
|
|
|
zathura_document_get_basename(zathura_document_t* document)
|
|
|
|
{
|
|
|
|
if (document == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return document->basename;
|
|
|
|
}
|
|
|
|
|
2012-03-27 14:44:09 +02:00
|
|
|
const char*
|
|
|
|
zathura_document_get_password(zathura_document_t* document)
|
|
|
|
{
|
|
|
|
if (document == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return document->password;
|
|
|
|
}
|
|
|
|
|
2012-03-27 21:59:35 +02:00
|
|
|
zathura_page_t*
|
|
|
|
zathura_document_get_page(zathura_document_t* document, unsigned int index)
|
|
|
|
{
|
|
|
|
if (document == NULL || document->pages == NULL || (document->number_of_pages <= index)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return document->pages[index];
|
|
|
|
}
|
|
|
|
|
2012-03-27 14:44:09 +02:00
|
|
|
void*
|
|
|
|
zathura_document_get_data(zathura_document_t* document)
|
|
|
|
{
|
|
|
|
if (document == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return document->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
zathura_document_set_data(zathura_document_t* document, void* data)
|
|
|
|
{
|
|
|
|
if (document == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
document->data = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int
|
|
|
|
zathura_document_get_number_of_pages(zathura_document_t* document)
|
|
|
|
{
|
|
|
|
if (document == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return document->number_of_pages;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
zathura_document_set_number_of_pages(zathura_document_t* document, unsigned int number_of_pages)
|
|
|
|
{
|
|
|
|
if (document == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
document->number_of_pages = number_of_pages;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int
|
2012-03-27 21:59:35 +02:00
|
|
|
zathura_document_get_current_page_number(zathura_document_t* document)
|
2012-03-27 14:44:09 +02:00
|
|
|
{
|
|
|
|
if (document == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return document->current_page_number;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-03-27 21:59:35 +02:00
|
|
|
zathura_document_set_current_page_number(zathura_document_t* document, unsigned int
|
2012-03-27 14:44:09 +02:00
|
|
|
current_page)
|
|
|
|
{
|
|
|
|
if (document == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
document->current_page_number = current_page;
|
|
|
|
}
|
|
|
|
|
2013-10-25 11:33:34 +02:00
|
|
|
double
|
|
|
|
zathura_document_get_position_x(zathura_document_t* document)
|
|
|
|
{
|
|
|
|
if (document == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return document->position_x;
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
zathura_document_get_position_y(zathura_document_t* document)
|
|
|
|
{
|
|
|
|
if (document == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return document->position_y;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
zathura_document_set_position_x(zathura_document_t* document, double position_x)
|
|
|
|
{
|
|
|
|
if (document == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
document->position_x = position_x;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
zathura_document_set_position_y(zathura_document_t* document, double position_y)
|
|
|
|
{
|
|
|
|
if (document == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
document->position_y = position_y;
|
|
|
|
}
|
|
|
|
|
2012-03-27 21:59:35 +02:00
|
|
|
double
|
|
|
|
zathura_document_get_scale(zathura_document_t* document)
|
|
|
|
{
|
|
|
|
if (document == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return document->scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
zathura_document_set_scale(zathura_document_t* document, double scale)
|
|
|
|
{
|
|
|
|
if (document == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
document->scale = scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int
|
|
|
|
zathura_document_get_rotation(zathura_document_t* document)
|
|
|
|
{
|
|
|
|
if (document == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return document->rotate;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
zathura_document_set_rotation(zathura_document_t* document, unsigned int rotation)
|
|
|
|
{
|
|
|
|
if (document == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
document->rotate = rotation % 360;
|
|
|
|
|
2012-03-28 16:44:46 +02:00
|
|
|
if (document->rotate > 0 && document->rotate <= 90) {
|
2012-03-27 21:59:35 +02:00
|
|
|
document->rotate = 90;
|
2012-03-28 16:44:46 +02:00
|
|
|
} else if (document->rotate > 0 && document->rotate <= 180) {
|
2012-03-27 21:59:35 +02:00
|
|
|
document->rotate = 180;
|
2012-03-28 16:44:46 +02:00
|
|
|
} else if (document->rotate > 0 && document->rotate <= 270) {
|
2012-03-27 21:59:35 +02:00
|
|
|
document->rotate = 270;
|
|
|
|
} else {
|
|
|
|
document->rotate = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
zathura_adjust_mode_t
|
|
|
|
zathura_document_get_adjust_mode(zathura_document_t* document)
|
|
|
|
{
|
|
|
|
if (document == NULL) {
|
|
|
|
return ZATHURA_ADJUST_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return document->adjust_mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
zathura_document_set_adjust_mode(zathura_document_t* document, zathura_adjust_mode_t mode)
|
|
|
|
{
|
|
|
|
if (document == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-23 11:07:17 +01:00
|
|
|
document->adjust_mode = mode;
|
2012-03-27 21:59:35 +02:00
|
|
|
}
|
|
|
|
|
2013-05-08 18:28:32 +02:00
|
|
|
int
|
2012-03-27 21:59:35 +02:00
|
|
|
zathura_document_get_page_offset(zathura_document_t* document)
|
|
|
|
{
|
|
|
|
if (document == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return document->page_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
zathura_document_set_page_offset(zathura_document_t* document, unsigned int page_offset)
|
|
|
|
{
|
|
|
|
if (document == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-08 18:28:32 +02:00
|
|
|
document->page_offset = page_offset;
|
2012-03-27 21:59:35 +02:00
|
|
|
}
|
|
|
|
|
2013-10-25 11:33:34 +02:00
|
|
|
void
|
|
|
|
zathura_document_set_viewport_width(zathura_document_t* document, unsigned int width)
|
|
|
|
{
|
|
|
|
if (document == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
document->view_width = width;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
zathura_document_set_viewport_height(zathura_document_t* document, unsigned int height)
|
|
|
|
{
|
|
|
|
if (document == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
document->view_height = height;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
zathura_document_get_viewport_size(zathura_document_t* document,
|
|
|
|
unsigned int *height, unsigned int* width)
|
|
|
|
{
|
|
|
|
g_return_if_fail(document != NULL && height != NULL && width != NULL);
|
|
|
|
*height = document->view_height;
|
|
|
|
*width = document->view_width;
|
|
|
|
}
|
|
|
|
|
2012-12-13 12:43:48 +01:00
|
|
|
void
|
|
|
|
zathura_document_get_cell_size(zathura_document_t* document,
|
|
|
|
unsigned int* height, unsigned int* width)
|
|
|
|
{
|
|
|
|
g_return_if_fail(document != NULL && height != NULL && width != NULL);
|
|
|
|
|
2013-10-20 17:00:07 +02:00
|
|
|
page_calc_height_width(document, document->cell_height, document->cell_width,
|
|
|
|
height, width, true);
|
2012-12-13 12:43:48 +01:00
|
|
|
}
|
|
|
|
|
2013-10-20 16:40:56 +02:00
|
|
|
void
|
|
|
|
zathura_document_get_document_size(zathura_document_t* document,
|
|
|
|
unsigned int* height, unsigned int* width)
|
|
|
|
{
|
|
|
|
g_return_if_fail(document != NULL && height != NULL && width != NULL);
|
|
|
|
|
|
|
|
unsigned int npag = zathura_document_get_number_of_pages(document);
|
|
|
|
unsigned int ncol = zathura_document_get_pages_per_row(document);
|
|
|
|
unsigned int c0 = zathura_document_get_first_page_column(document);
|
|
|
|
unsigned int nrow = (npag + c0 - 1 + ncol - 1) / ncol; /* number of rows */
|
|
|
|
unsigned int pad = zathura_document_get_page_padding(document);
|
|
|
|
|
2013-11-05 02:23:52 +01:00
|
|
|
unsigned int cell_height = 0;
|
|
|
|
unsigned int cell_width = 0;
|
2013-10-20 16:40:56 +02:00
|
|
|
zathura_document_get_cell_size(document, &cell_height, &cell_width);
|
|
|
|
|
|
|
|
*width = ncol * cell_width + (ncol - 1) * pad;
|
|
|
|
*height = nrow * cell_height + (nrow - 1) * pad;
|
|
|
|
}
|
|
|
|
|
2013-10-20 17:05:15 +02:00
|
|
|
void
|
|
|
|
zathura_document_set_page_layout(zathura_document_t* document, unsigned int page_padding,
|
|
|
|
unsigned int pages_per_row, unsigned int first_page_column)
|
|
|
|
{
|
|
|
|
g_return_if_fail(document != NULL);
|
|
|
|
document->page_padding = page_padding;
|
|
|
|
document->pages_per_row = pages_per_row;
|
|
|
|
document->first_page_column = first_page_column;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int
|
|
|
|
zathura_document_get_page_padding(zathura_document_t* document)
|
|
|
|
{
|
|
|
|
if (document == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return document->page_padding;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int
|
|
|
|
zathura_document_get_pages_per_row(zathura_document_t* document)
|
|
|
|
{
|
|
|
|
if (document == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return document->pages_per_row;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int
|
|
|
|
zathura_document_get_first_page_column(zathura_document_t* document)
|
|
|
|
{
|
|
|
|
if (document == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return document->first_page_column;
|
|
|
|
}
|
|
|
|
|
2012-03-27 13:30:04 +02:00
|
|
|
zathura_error_t
|
2010-11-17 23:15:08 +01:00
|
|
|
zathura_document_save_as(zathura_document_t* document, const char* path)
|
|
|
|
{
|
2012-03-27 21:59:35 +02:00
|
|
|
if (document == NULL || document->plugin == NULL || path == NULL) {
|
2012-03-27 13:30:04 +02:00
|
|
|
return ZATHURA_ERROR_UNKNOWN;
|
2010-11-17 23:15:08 +01:00
|
|
|
}
|
|
|
|
|
2012-06-13 16:08:24 +02:00
|
|
|
zathura_plugin_functions_t* functions = zathura_plugin_get_functions(document->plugin);
|
|
|
|
if (functions->document_save_as == NULL) {
|
2012-03-27 13:30:04 +02:00
|
|
|
return ZATHURA_ERROR_NOT_IMPLEMENTED;
|
2010-11-17 23:15:08 +01:00
|
|
|
}
|
|
|
|
|
2012-06-13 16:08:24 +02:00
|
|
|
return functions->document_save_as(document, document->data, path);
|
2010-11-17 22:51:15 +01:00
|
|
|
}
|
|
|
|
|
2010-12-24 16:21:54 +01:00
|
|
|
girara_tree_node_t*
|
2012-03-27 13:30:04 +02:00
|
|
|
zathura_document_index_generate(zathura_document_t* document, zathura_error_t* error)
|
2010-11-17 22:51:15 +01:00
|
|
|
{
|
2012-03-27 21:59:35 +02:00
|
|
|
if (document == NULL || document->plugin == NULL) {
|
2012-03-05 00:15:09 +01:00
|
|
|
if (error != NULL) {
|
2012-03-27 13:30:04 +02:00
|
|
|
*error = ZATHURA_ERROR_INVALID_ARGUMENTS;
|
2012-03-05 00:15:09 +01:00
|
|
|
}
|
2010-11-17 23:15:08 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-06-13 16:08:24 +02:00
|
|
|
zathura_plugin_functions_t* functions = zathura_plugin_get_functions(document->plugin);
|
|
|
|
if (functions->document_index_generate == NULL) {
|
2012-03-05 00:15:09 +01:00
|
|
|
if (error != NULL) {
|
2012-03-27 13:30:04 +02:00
|
|
|
*error = ZATHURA_ERROR_NOT_IMPLEMENTED;
|
2012-03-05 00:15:09 +01:00
|
|
|
}
|
2010-11-17 23:15:08 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-06-13 16:08:24 +02:00
|
|
|
return functions->document_index_generate(document, document->data, error);
|
2010-11-17 22:51:15 +01:00
|
|
|
}
|
|
|
|
|
2011-10-22 00:11:42 +02:00
|
|
|
girara_list_t*
|
2012-03-27 13:30:04 +02:00
|
|
|
zathura_document_attachments_get(zathura_document_t* document, zathura_error_t* error)
|
2010-11-17 22:51:15 +01:00
|
|
|
{
|
2012-03-27 21:59:35 +02:00
|
|
|
if (document == NULL || document->plugin == NULL) {
|
2012-03-05 00:15:09 +01:00
|
|
|
if (error != NULL) {
|
2012-03-27 13:30:04 +02:00
|
|
|
*error = ZATHURA_ERROR_INVALID_ARGUMENTS;
|
2012-03-05 00:15:09 +01:00
|
|
|
}
|
2010-11-17 23:15:08 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-06-13 16:08:24 +02:00
|
|
|
zathura_plugin_functions_t* functions = zathura_plugin_get_functions(document->plugin);
|
|
|
|
if (functions->document_attachments_get == NULL) {
|
2012-03-05 00:15:09 +01:00
|
|
|
if (error != NULL) {
|
2012-03-27 13:30:04 +02:00
|
|
|
*error = ZATHURA_ERROR_NOT_IMPLEMENTED;
|
2012-03-05 00:15:09 +01:00
|
|
|
}
|
2010-11-17 23:15:08 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-06-13 16:08:24 +02:00
|
|
|
return functions->document_attachments_get(document, document->data, error);
|
2010-11-17 22:51:15 +01:00
|
|
|
}
|
|
|
|
|
2012-03-27 13:30:04 +02:00
|
|
|
zathura_error_t
|
2012-02-07 21:01:54 +01:00
|
|
|
zathura_document_attachment_save(zathura_document_t* document, const char* attachment, const char* file)
|
2010-11-17 22:51:15 +01:00
|
|
|
{
|
2012-03-27 21:59:35 +02:00
|
|
|
if (document == NULL || document->plugin == NULL) {
|
2012-03-27 13:30:04 +02:00
|
|
|
return ZATHURA_ERROR_INVALID_ARGUMENTS;
|
2012-01-13 18:25:17 +01:00
|
|
|
}
|
|
|
|
|
2012-06-13 16:08:24 +02:00
|
|
|
zathura_plugin_functions_t* functions = zathura_plugin_get_functions(document->plugin);
|
|
|
|
if (functions->document_attachment_save == NULL) {
|
2012-03-27 13:30:04 +02:00
|
|
|
return ZATHURA_ERROR_NOT_IMPLEMENTED;
|
2012-01-13 18:25:17 +01:00
|
|
|
}
|
|
|
|
|
2012-06-13 16:08:24 +02:00
|
|
|
return functions->document_attachment_save(document, document->data, attachment, file);
|
2010-11-17 22:51:15 +01:00
|
|
|
}
|
|
|
|
|
2012-03-30 18:24:00 +02:00
|
|
|
girara_list_t*
|
|
|
|
zathura_document_get_information(zathura_document_t* document, zathura_error_t* error)
|
2011-10-01 23:29:40 +02:00
|
|
|
{
|
2012-03-27 21:59:35 +02:00
|
|
|
if (document == NULL || document->plugin == NULL) {
|
2012-02-07 21:01:54 +01:00
|
|
|
if (error != NULL) {
|
2012-03-27 13:30:04 +02:00
|
|
|
*error = ZATHURA_ERROR_INVALID_ARGUMENTS;
|
2012-02-07 21:01:54 +01:00
|
|
|
}
|
2011-10-01 23:29:40 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-06-13 16:08:24 +02:00
|
|
|
zathura_plugin_functions_t* functions = zathura_plugin_get_functions(document->plugin);
|
|
|
|
if (functions->document_get_information == NULL) {
|
2012-02-07 21:01:54 +01:00
|
|
|
if (error != NULL) {
|
2012-03-27 13:30:04 +02:00
|
|
|
*error = ZATHURA_ERROR_NOT_IMPLEMENTED;
|
2012-02-07 21:01:54 +01:00
|
|
|
}
|
2011-10-01 23:29:40 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-06-13 16:08:24 +02:00
|
|
|
girara_list_t* result = functions->document_get_information(document, document->data, error);
|
2012-03-30 18:24:00 +02:00
|
|
|
if (result != NULL) {
|
|
|
|
girara_list_set_free_function(result, (girara_free_function_t) zathura_document_information_entry_free);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2011-10-01 23:29:40 +02:00
|
|
|
}
|
|
|
|
|
2012-03-27 13:30:04 +02:00
|
|
|
static const gchar*
|
|
|
|
guess_type(const char* path)
|
2010-12-25 00:47:52 +01:00
|
|
|
{
|
2013-02-19 15:27:23 +01:00
|
|
|
const gchar* content_type = NULL;
|
2013-02-08 10:58:57 +01:00
|
|
|
#ifdef WITH_MAGIC
|
2013-02-13 15:41:38 +01:00
|
|
|
const char* mime_type = NULL;
|
2013-02-13 15:43:02 +01:00
|
|
|
|
|
|
|
/* creat magic cookie */
|
2013-02-08 10:58:57 +01:00
|
|
|
const int flags =
|
|
|
|
MAGIC_MIME_TYPE |
|
|
|
|
MAGIC_SYMLINK |
|
|
|
|
MAGIC_NO_CHECK_APPTYPE |
|
|
|
|
MAGIC_NO_CHECK_CDF |
|
|
|
|
MAGIC_NO_CHECK_ELF |
|
|
|
|
MAGIC_NO_CHECK_ENCODING;
|
|
|
|
magic_t magic = magic_open(flags);
|
|
|
|
if (magic == NULL) {
|
2013-03-26 15:21:03 +01:00
|
|
|
girara_debug("failed creating the magic cookie");
|
2013-02-13 15:41:38 +01:00
|
|
|
goto cleanup;
|
2013-02-08 10:58:57 +01:00
|
|
|
}
|
2013-02-13 15:43:02 +01:00
|
|
|
|
|
|
|
/* ... and load mime database */
|
2013-02-08 10:58:57 +01:00
|
|
|
if (magic_load(magic, NULL) < 0) {
|
2013-03-26 15:21:03 +01:00
|
|
|
girara_debug("failed loading the magic database: %s", magic_error(magic));
|
2013-02-08 10:58:57 +01:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2013-02-13 15:43:02 +01:00
|
|
|
|
|
|
|
/* get the mime type */
|
2013-02-13 15:41:38 +01:00
|
|
|
mime_type = magic_file(magic, path);
|
2013-02-08 10:58:57 +01:00
|
|
|
if (mime_type == NULL) {
|
2013-03-26 15:21:03 +01:00
|
|
|
girara_debug("failed guessing filetype: %s", magic_error(magic));
|
2013-02-19 15:27:23 +01:00
|
|
|
goto cleanup;
|
2013-02-08 10:58:57 +01:00
|
|
|
}
|
2013-02-13 15:43:02 +01:00
|
|
|
|
2013-03-26 15:21:03 +01:00
|
|
|
girara_debug("magic detected filetype: %s", mime_type);
|
2013-02-19 15:27:23 +01:00
|
|
|
content_type = g_strdup(mime_type);
|
|
|
|
|
2013-02-08 10:58:57 +01:00
|
|
|
cleanup:
|
2013-02-13 15:41:38 +01:00
|
|
|
if (magic != NULL) {
|
2013-02-11 14:11:15 +01:00
|
|
|
magic_close(magic);
|
|
|
|
}
|
2013-02-19 15:27:23 +01:00
|
|
|
|
|
|
|
if (content_type != NULL) {
|
|
|
|
return content_type;
|
2013-02-13 15:41:38 +01:00
|
|
|
}
|
|
|
|
/* else fallback to g_content_type_guess method */
|
2013-02-11 14:11:15 +01:00
|
|
|
#endif /*WITH_MAGIC*/
|
2013-02-08 10:58:57 +01:00
|
|
|
gboolean uncertain = FALSE;
|
2013-02-19 15:27:23 +01:00
|
|
|
content_type = g_content_type_guess(path, NULL, 0, &uncertain);
|
2012-03-27 13:30:04 +02:00
|
|
|
if (content_type == NULL) {
|
2013-02-19 15:19:10 +01:00
|
|
|
girara_debug("g_content_type failed\n");
|
|
|
|
} else {
|
|
|
|
if (uncertain == FALSE) {
|
2013-03-26 15:21:03 +01:00
|
|
|
girara_debug("g_content_type detected filetype: %s", content_type);
|
2013-02-19 15:19:10 +01:00
|
|
|
return content_type;
|
|
|
|
}
|
2013-03-26 15:21:03 +01:00
|
|
|
girara_debug("g_content_type is uncertain, guess: %s", content_type);
|
2010-12-26 01:12:20 +01:00
|
|
|
}
|
|
|
|
|
2012-03-27 13:30:04 +02:00
|
|
|
FILE* f = fopen(path, "rb");
|
|
|
|
if (f == NULL) {
|
2011-03-18 13:27:21 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-27 13:30:04 +02:00
|
|
|
const int fd = fileno(f);
|
|
|
|
guchar* content = NULL;
|
|
|
|
size_t length = 0u;
|
2013-01-17 10:13:31 +01:00
|
|
|
ssize_t bytes_read = -1;
|
|
|
|
while (uncertain == TRUE && length < GT_MAX_READ && bytes_read != 0) {
|
2012-03-27 13:30:04 +02:00
|
|
|
g_free((void*)content_type);
|
|
|
|
content_type = NULL;
|
2011-03-18 13:27:21 +01:00
|
|
|
|
2012-03-27 13:30:04 +02:00
|
|
|
content = g_realloc(content, length + BUFSIZ);
|
2013-01-17 10:13:31 +01:00
|
|
|
bytes_read = read(fd, content + length, BUFSIZ);
|
|
|
|
if (bytes_read == -1) {
|
2012-03-27 13:30:04 +02:00
|
|
|
break;
|
|
|
|
}
|
2011-03-18 13:27:21 +01:00
|
|
|
|
2013-01-17 10:13:31 +01:00
|
|
|
length += bytes_read;
|
2012-03-27 13:30:04 +02:00
|
|
|
content_type = g_content_type_guess(NULL, content, length, &uncertain);
|
2013-03-26 15:21:03 +01:00
|
|
|
girara_debug("new guess: %s uncertain: %d, read: %zu", content_type, uncertain, length);
|
2012-03-27 13:30:04 +02:00
|
|
|
}
|
2011-03-18 13:27:21 +01:00
|
|
|
|
2012-03-27 13:30:04 +02:00
|
|
|
fclose(f);
|
|
|
|
g_free(content);
|
|
|
|
if (uncertain == FALSE) {
|
|
|
|
return content_type;
|
2011-03-18 13:27:21 +01:00
|
|
|
}
|
|
|
|
|
2012-03-27 13:30:04 +02:00
|
|
|
g_free((void*)content_type);
|
|
|
|
content_type = NULL;
|
2011-09-29 15:23:13 +02:00
|
|
|
|
2012-03-27 13:30:04 +02:00
|
|
|
girara_debug("falling back to file");
|
2011-09-29 15:23:13 +02:00
|
|
|
|
2012-03-27 13:30:04 +02:00
|
|
|
GString* command = g_string_new("file -b --mime-type ");
|
|
|
|
char* tmp = g_shell_quote(path);
|
2011-09-29 15:23:13 +02:00
|
|
|
|
2012-03-27 13:30:04 +02:00
|
|
|
g_string_append(command, tmp);
|
|
|
|
g_free(tmp);
|
2011-09-29 15:23:13 +02:00
|
|
|
|
2012-03-27 13:30:04 +02:00
|
|
|
GError* error = NULL;
|
|
|
|
char* out = NULL;
|
|
|
|
int ret = 0;
|
|
|
|
g_spawn_command_line_sync(command->str, &out, NULL, &ret, &error);
|
|
|
|
g_string_free(command, TRUE);
|
|
|
|
if (error != NULL) {
|
|
|
|
girara_warning("failed to execute command: %s", error->message);
|
|
|
|
g_error_free(error);
|
|
|
|
g_free(out);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (WEXITSTATUS(ret) != 0) {
|
|
|
|
girara_warning("file failed with error code: %d", WEXITSTATUS(ret));
|
|
|
|
g_free(out);
|
|
|
|
return NULL;
|
2011-09-29 15:23:13 +02:00
|
|
|
}
|
|
|
|
|
2012-03-27 13:30:04 +02:00
|
|
|
g_strdelimit(out, "\n\r", '\0');
|
|
|
|
return out;
|
2011-09-29 15:23:13 +02:00
|
|
|
}
|
2012-03-27 21:59:35 +02:00
|
|
|
|
|
|
|
zathura_plugin_t*
|
|
|
|
zathura_document_get_plugin(zathura_document_t* document)
|
|
|
|
{
|
|
|
|
if (document == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return document->plugin;
|
|
|
|
}
|