zathura/document.c

832 lines
24 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 700
2010-11-18 13:54:35 +01:00
// 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>
#include <sys/stat.h>
2011-03-05 21:00:41 +01:00
#include <dlfcn.h>
#include <errno.h>
#include <glib.h>
#include <gio/gio.h>
#include <glib/gi18n.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"
#include "database.h"
2012-02-07 18:30:46 +01:00
#include "page_widget.h"
2010-11-18 02:35:33 +01:00
#include <girara/datastructures.h>
#include <girara/utils.h>
#include <girara/statusbar.h>
2012-02-08 23:21:27 +01:00
#include <girara/session.h>
2012-02-09 01:46:51 +01:00
#include <girara/settings.h>
2011-10-21 23:33:18 +02:00
/**
* Register document plugin
*/
static bool zathura_document_plugin_register(zathura_t* zathura, zathura_document_plugin_t* new_plugin);
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
{
2011-10-21 23:33:18 +02:00
GIRARA_LIST_FOREACH(zathura->plugins.path, char*, iter, plugindir)
/* read all files in the plugin directory */
GDir* dir = g_dir_open(plugindir, 0, NULL);
if (dir == NULL) {
girara_error("could not open plugin directory: %s", plugindir);
girara_list_iterator_next(iter);
continue;
}
char* name = NULL;
while ((name = (char*) g_dir_read_name(dir)) != NULL) {
char* path = g_build_filename(plugindir, name, NULL);
GFile* file = g_file_new_for_path(path);
GError* error = NULL;
GFileInfo* file_info = g_file_query_info(file, G_FILE_ATTRIBUTE_UNIX_MODE, 0, NULL, &error);
if (file_info == NULL) {
girara_error("failed to query file info for %s: %s", path, error->message);
g_error_free(error);
g_object_unref(file);
g_free(path);
2011-10-29 13:09:16 +02:00
continue;
}
const guint mode = g_file_info_get_attribute_uint32(file_info, G_FILE_ATTRIBUTE_UNIX_MODE);
g_object_unref(file_info);
g_object_unref(file);
if (S_ISREG(mode) == 0) {
girara_info("%s is not a regular file. Skipping.", path);
g_free(path);
continue;
}
2011-03-05 21:00:41 +01:00
void* handle = NULL;
zathura_document_plugin_t* plugin = NULL;
2011-03-05 21:00:41 +01:00
/* load plugin */
handle = dlopen(path, RTLD_NOW);
if (handle == NULL) {
girara_error("could not load plugin %s (%s)", path, dlerror());
g_free(path);
continue;
}
2011-03-05 21:00:41 +01:00
/* resolve symbol and check API version*/
zathura_plugin_api_version_t api_version;
*(void**)(&api_version) = dlsym(handle, PLUGIN_API_VERSION_FUNCTION);
if (api_version != NULL) {
if (api_version() != ZATHURA_API_VERSION) {
girara_error("plugin %s has been built againt zathura with a different API version (plugin: %d, zathura: %d)",
path, api_version(), ZATHURA_API_VERSION);
g_free(path);
dlclose(handle);
continue;
}
} else {
girara_warning("could not find '%s' function in plugin %s ... loading anyway", PLUGIN_API_VERSION_FUNCTION, path);
}
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) {
girara_error("could not find '%s' function in plugin %s", PLUGIN_REGISTER_FUNCTION, path);
g_free(path);
dlclose(handle);
continue;
}
2011-03-05 21:00:41 +01:00
2011-10-21 23:33:18 +02:00
plugin = g_malloc0(sizeof(zathura_document_plugin_t));
plugin->content_types = girara_list_new2(g_free);
plugin->handle = handle;
register_plugin(plugin);
2011-10-21 23:33:18 +02:00
bool r = zathura_document_plugin_register(zathura, plugin);
if (r == false) {
girara_error("could not register plugin %s", path);
2011-10-21 23:33:18 +02:00
zathura_document_plugin_free(plugin);
2011-12-11 20:19:57 +01:00
} else {
girara_info("successfully loaded plugin %s", path);
}
g_free(path);
}
g_dir_close(dir);
2011-10-21 23:33:18 +02:00
GIRARA_LIST_FOREACH_END(zathura->plugins.path, char*, iter, plugindir);
2011-03-05 21:00:41 +01:00
}
void
2011-09-29 15:23:13 +02:00
zathura_document_plugin_free(zathura_document_plugin_t* plugin)
2011-03-05 21:00:41 +01:00
{
2011-09-29 15:23:13 +02:00
if (plugin == NULL) {
2011-04-18 18:19:41 +02:00
return;
2011-03-05 21:00:41 +01:00
}
2011-10-21 23:33:18 +02:00
dlclose(plugin->handle);
2011-09-29 15:23:13 +02:00
girara_list_free(plugin->content_types);
2011-10-21 23:33:18 +02:00
g_free(plugin);
2011-03-05 21:00:41 +01:00
}
2011-10-21 23:33:18 +02:00
static bool
zathura_document_plugin_register(zathura_t* zathura, zathura_document_plugin_t* new_plugin)
{
2011-10-21 23:36:17 +02:00
if (new_plugin == NULL || new_plugin->content_types == NULL || new_plugin->open_function == NULL) {
2011-04-18 18:19:41 +02:00
girara_error("plugin: could not register\n");
return false;
}
2011-09-29 15:23:13 +02:00
bool atleastone = false;
GIRARA_LIST_FOREACH(new_plugin->content_types, gchar*, iter, type)
if (!zathura_type_plugin_mapping_new(zathura, type, new_plugin)) {
girara_error("plugin: already registered for filetype %s\n", type);
2011-10-21 23:36:17 +02:00
} else {
atleastone = true;
2011-09-29 15:23:13 +02:00
}
2011-10-17 11:16:14 +02:00
GIRARA_LIST_FOREACH_END(new_plugin->content_types, gchar*, iter, type);
2011-09-29 15:23:13 +02:00
if (atleastone) {
girara_list_append(zathura->plugins.plugins, new_plugin);
}
return atleastone;
}
static const gchar*
guess_type(const char* path)
2010-11-17 22:51:15 +01:00
{
gboolean uncertain;
const gchar* content_type = g_content_type_guess(path, NULL, 0, &uncertain);
if (content_type == NULL) {
2011-09-29 15:23:13 +02:00
return NULL;
2010-11-18 02:35:33 +01:00
}
FILE* f = fopen(path, "r");
if (f == NULL) {
2011-09-29 15:23:13 +02:00
return NULL;
2010-11-18 02:35:33 +01:00
}
const int fd = fileno(f);
guchar* content = NULL;
size_t length = 0u;
while (uncertain == TRUE) {
g_free((void*)content_type);
content_type = NULL;
2011-10-23 19:27:57 +02:00
content = g_realloc(content, length + BUFSIZ);
const ssize_t r = read(fd, content + length, BUFSIZ);
if (r == -1) {
break;
}
length += r;
content_type = g_content_type_guess(NULL, content, length, &uncertain);
2010-11-18 02:35:33 +01:00
}
fclose(f);
if (uncertain == TRUE) {
g_free((void*)content_type);
content_type = NULL;
}
g_free(content);
return content_type;
}
zathura_document_t*
zathura_document_open(zathura_t* zathura, const char* path, const char* password)
{
if (path == NULL) {
return NULL;
}
if (file_exists(path) == false) {
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;
}
2010-11-18 13:54:35 +01:00
/* determine real path */
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);
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;
}
zathura_document_plugin_t* plugin = NULL;
GIRARA_LIST_FOREACH(zathura->plugins.type_plugin_mapping, zathura_type_plugin_mapping_t*, iter, mapping)
if (g_content_type_equals(content_type, mapping->type)) {
plugin = mapping->plugin;
break;
}
2011-10-17 11:16:14 +02:00
GIRARA_LIST_FOREACH_END(zathura->plugins.type_plugin_mapping, zathura_type_plugin_mapping_t*, iter, mapping);
2011-09-29 15:23:13 +02:00
g_free((void*)content_type);
if (plugin == NULL) {
girara_error("unknown file type\n");
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
2010-11-18 13:54:35 +01:00
document->file_path = real_path;
2010-11-18 02:35:33 +01:00
document->password = password;
2010-12-26 20:35:26 +01:00
document->scale = 1.0;
document->zathura = zathura;
2010-11-18 02:35:33 +01:00
2012-02-08 23:21:27 +01:00
/* open document */
if (plugin->open_function == NULL) {
girara_error("plugin has no open function\n");
goto error_free;
}
zathura_plugin_error_t error = plugin->open_function(document);
if (error != ZATHURA_PLUGIN_ERROR_OK) {
2012-02-08 23:33:31 +01:00
if (error == ZATHURA_PLUGIN_ERROR_INVALID_PASSWORD) {
2012-02-08 23:21:27 +01:00
zathura_password_dialog_info_t* password_dialog_info = malloc(sizeof(zathura_password_dialog_info_t));
if (password_dialog_info != NULL) {
password_dialog_info->path = g_strdup(path);
password_dialog_info->zathura = zathura;
if (path != NULL) {
girara_dialog(zathura->ui.session, "Enter password:", true, NULL,
(girara_callback_inputbar_activate_t) cb_password_dialog, password_dialog_info);
goto error_free;
} else {
free(password_dialog_info);
}
}
goto error_free;
}
girara_error("could not open document\n");
goto error_free;
}
/* read history file */
int offset = 0;
zathura_db_get_fileinfo(zathura->database, document->file_path,
2012-02-08 22:23:45 +01:00
&document->current_page_number, &offset, &document->scale, &document->rotate);
/* check for valid scale value */
2012-01-13 17:39:46 +01:00
if (document->scale <= FLT_EPSILON) {
2012-01-13 17:33:19 +01:00
girara_warning("document info: '%s' has non positive scale", document->file_path);
document->scale = 1;
}
/* check current page number */
2012-02-08 23:37:34 +01:00
if (document->current_page_number > document->number_of_pages) {
girara_warning("document info: '%s' has an invalid page number", document->file_path);
2012-02-08 23:37:34 +01:00
document->current_page_number = 0;
}
2012-02-07 19:25:47 +01:00
/* update statusbar */
girara_statusbar_item_set_text(zathura->ui.session, zathura->ui.statusbar.file, real_path);
2011-09-29 15:23:13 +02:00
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-02-07 21:01:54 +01:00
zathura_page_t* page = zathura_page_get(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;
}
2012-02-09 01:46:51 +01:00
/* apply open adjustment */
2012-02-09 11:25:00 +01:00
char* adjust_open = "best-fit";
document->adjust_mode = ADJUST_BESTFIT;
if (girara_setting_get(zathura->ui.session, "adjust-open", &(adjust_open)) == true) {
if (g_strcmp0(adjust_open, "best-fit") == 0) {
document->adjust_mode = ADJUST_BESTFIT;
} else if (g_strcmp0(adjust_open, "width") == 0) {
document->adjust_mode = ADJUST_WIDTH;
} else {
document->adjust_mode = ADJUST_NONE;
2012-02-09 11:25:00 +01:00
}
g_free(adjust_open);
}
2012-02-09 01:46:51 +01:00
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-02-07 21:01:54 +01:00
zathura_plugin_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
{
if (document == NULL || document->zathura == NULL || document->zathura->ui.session == NULL) {
2012-03-05 00:15:09 +01:00
return ZATHURA_PLUGIN_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]);
document->pages[page_id] = NULL;
2010-12-28 00:47:41 +01:00
}
free(document->pages);
/* free document */
2012-03-05 00:15:09 +01:00
zathura_plugin_error_t error = ZATHURA_PLUGIN_ERROR_OK;
2011-09-21 00:25:41 +02:00
if (document->functions.document_free == NULL) {
girara_notify(document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
2011-04-02 23:40:57 +02:00
girara_error("%s not implemented", __FUNCTION__);
2012-03-05 00:15:09 +01:00
error = ZATHURA_PLUGIN_ERROR_NOT_IMPLEMENTED;
2011-10-21 23:40:16 +02:00
} else {
2012-03-05 08:22:16 +01:00
error = document->functions.document_free(document);
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);
}
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-02-07 21:01:54 +01:00
zathura_plugin_error_t
2010-11-17 23:15:08 +01:00
zathura_document_save_as(zathura_document_t* document, const char* path)
{
if (document == NULL || path == NULL || document->zathura == NULL || document->zathura->ui.session == NULL) {
return ZATHURA_PLUGIN_ERROR_UNKNOWN;
2010-11-17 23:15:08 +01:00
}
2011-09-21 00:25:41 +02:00
if (document->functions.document_save_as == NULL) {
girara_notify(document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
2011-04-02 23:40:57 +02:00
girara_error("%s not implemented", __FUNCTION__);
return ZATHURA_PLUGIN_ERROR_NOT_IMPLEMENTED;
2010-11-17 23:15:08 +01:00
}
return document->functions.document_save_as(document, path);
2010-11-17 22:51:15 +01:00
}
girara_tree_node_t*
2012-02-07 21:01:54 +01:00
zathura_document_index_generate(zathura_document_t* document, zathura_plugin_error_t* error)
2010-11-17 22:51:15 +01:00
{
if (document == NULL || document->zathura == NULL || document->zathura->ui.session == NULL) {
2012-03-05 00:15:09 +01:00
if (error != NULL) {
*error = ZATHURA_PLUGIN_ERROR_INVALID_ARGUMENTS;
}
2010-11-17 23:15:08 +01:00
return NULL;
}
2011-09-21 00:25:41 +02:00
if (document->functions.document_index_generate == NULL) {
girara_notify(document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
2011-04-02 23:40:57 +02:00
girara_error("%s not implemented", __FUNCTION__);
2012-03-05 00:15:09 +01:00
if (error != NULL) {
*error = ZATHURA_PLUGIN_ERROR_NOT_IMPLEMENTED;
}
2010-11-17 23:15:08 +01:00
return NULL;
}
2012-02-07 21:01:54 +01:00
return document->functions.document_index_generate(document, error);
2010-11-17 22:51:15 +01:00
}
2011-10-22 00:11:42 +02:00
girara_list_t*
2012-02-07 21:01:54 +01:00
zathura_document_attachments_get(zathura_document_t* document, zathura_plugin_error_t* error)
2010-11-17 22:51:15 +01:00
{
if (document == NULL || document->zathura == NULL || document->zathura->ui.session == NULL) {
2012-03-05 00:15:09 +01:00
if (error != NULL) {
*error = ZATHURA_PLUGIN_ERROR_INVALID_ARGUMENTS;
}
2010-11-17 23:15:08 +01:00
return NULL;
}
2011-09-21 00:25:41 +02:00
if (document->functions.document_attachments_get == NULL) {
girara_notify(document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
2011-04-02 23:40:57 +02:00
girara_error("%s not implemented", __FUNCTION__);
2012-03-05 00:15:09 +01:00
if (error != NULL) {
*error = ZATHURA_PLUGIN_ERROR_NOT_IMPLEMENTED;
}
2010-11-17 23:15:08 +01:00
return NULL;
}
2012-02-07 21:01:54 +01:00
return document->functions.document_attachments_get(document, error);
2010-11-17 22:51:15 +01:00
}
2012-02-07 21:01:54 +01:00
zathura_plugin_error_t
zathura_document_attachment_save(zathura_document_t* document, const char* attachment, const char* file)
2010-11-17 22:51:15 +01:00
{
if (document == NULL || document->zathura == NULL || document->zathura->ui.session == NULL) {
2012-02-07 21:01:54 +01:00
return ZATHURA_PLUGIN_ERROR_INVALID_ARGUMENTS;
2012-01-13 18:25:17 +01:00
}
if (document->functions.document_attachment_save == NULL) {
girara_notify(document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
girara_error("%s not implemented", __FUNCTION__);
2012-02-07 21:01:54 +01:00
return ZATHURA_PLUGIN_ERROR_NOT_IMPLEMENTED;
2012-01-13 18:25:17 +01:00
}
return document->functions.document_attachment_save(document, attachment, file);
2010-11-17 22:51:15 +01:00
}
2011-10-01 23:29:40 +02:00
char*
2012-02-07 21:01:54 +01:00
zathura_document_meta_get(zathura_document_t* document, zathura_document_meta_t meta, zathura_plugin_error_t* error)
2011-10-01 23:29:40 +02:00
{
if (document == NULL || document->zathura == NULL || document->zathura->ui.session == NULL) {
2012-02-07 21:01:54 +01:00
if (error != NULL) {
*error = ZATHURA_PLUGIN_ERROR_INVALID_ARGUMENTS;
2012-02-07 21:01:54 +01:00
}
2011-10-01 23:29:40 +02:00
return NULL;
}
if (document->functions.document_meta_get == NULL) {
2012-02-07 21:01:54 +01:00
if (error != NULL) {
*error = ZATHURA_PLUGIN_ERROR_NOT_IMPLEMENTED;
}
girara_notify(document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
girara_error("%s not implemented", __FUNCTION__);
2011-10-01 23:29:40 +02:00
return NULL;
}
2012-02-07 21:01:54 +01:00
return document->functions.document_meta_get(document, meta, error);
2011-10-01 23:29:40 +02:00
}
2010-11-17 23:15:08 +01:00
zathura_page_t*
2012-02-07 21:01:54 +01:00
zathura_page_get(zathura_document_t* document, unsigned int page_id, zathura_plugin_error_t* error)
2010-11-17 22:51:15 +01:00
{
if (document == NULL || document->zathura == NULL || document->zathura->ui.session == NULL) {
if (error != NULL) {
*error = ZATHURA_PLUGIN_ERROR_INVALID_ARGUMENTS;
}
2010-11-17 23:15:08 +01:00
return NULL;
}
2011-09-21 00:25:41 +02:00
if (document->functions.page_get == NULL) {
girara_notify(document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
2011-04-02 23:40:57 +02:00
girara_error("%s not implemented", __FUNCTION__);
if (error != NULL) {
*error = ZATHURA_PLUGIN_ERROR_NOT_IMPLEMENTED;
}
2010-11-17 23:15:08 +01:00
return NULL;
}
2012-02-07 21:01:54 +01:00
zathura_page_t* page = document->functions.page_get(document, page_id, error);
2011-09-21 00:25:41 +02:00
if (page != NULL) {
2011-03-20 02:53:24 +01:00
page->number = page_id;
page->visible = false;
2012-02-07 18:30:46 +01:00
page->drawing_area = zathura_page_widget_new(page);
if (page->drawing_area == NULL) {
2012-03-05 00:15:09 +01:00
if (error != NULL) {
*error = ZATHURA_PLUGIN_ERROR_UNKNOWN;
}
girara_error("Couldn't create page widget");
zathura_page_free(page);
return NULL;
}
2012-03-05 00:15:09 +01:00
page->document = document;
2011-03-20 02:53:24 +01:00
2012-02-08 22:23:45 +01:00
unsigned int page_height = 0;
2012-03-05 00:15:09 +01:00
unsigned int page_width = 0;
2012-02-08 22:23:45 +01:00
page_calc_height_width(page, &page_height, &page_width, true);
gtk_widget_set_size_request(page->drawing_area, page_width, page_height);
}
return page;
2010-11-17 22:51:15 +01:00
}
2012-02-07 21:01:54 +01:00
zathura_plugin_error_t
2010-11-18 02:35:33 +01:00
zathura_page_free(zathura_page_t* page)
{
if (page == NULL || page->document == NULL || page->document->zathura == NULL || page->document->zathura->ui.session == NULL) {
return ZATHURA_PLUGIN_ERROR_INVALID_ARGUMENTS;
2010-11-18 02:35:33 +01:00
}
2011-09-21 00:25:41 +02:00
if (page->document->functions.page_free == NULL) {
girara_notify(page->document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
2011-04-02 23:40:57 +02:00
girara_error("%s not implemented", __FUNCTION__);
return ZATHURA_PLUGIN_ERROR_NOT_IMPLEMENTED;
2010-11-18 02:35:33 +01:00
}
return page->document->functions.page_free(page);
}
2011-10-22 00:11:42 +02:00
girara_list_t*
2012-02-07 21:01:54 +01:00
zathura_page_search_text(zathura_page_t* page, const char* text, zathura_plugin_error_t* error)
2010-11-17 22:51:15 +01:00
{
if (page == NULL || page->document == NULL || text == NULL ||
page->document->zathura == NULL || page->document->zathura->ui.session == NULL) {
if (error != NULL) {
*error = ZATHURA_PLUGIN_ERROR_INVALID_ARGUMENTS;
}
2010-11-17 23:15:08 +01:00
return NULL;
}
2011-09-21 00:25:41 +02:00
if (page->document->functions.page_search_text == NULL) {
girara_notify(page->document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
2011-04-02 23:40:57 +02:00
girara_error("%s not implemented", __FUNCTION__);
if (error != NULL) {
*error = ZATHURA_PLUGIN_ERROR_NOT_IMPLEMENTED;
}
2010-11-17 23:15:08 +01:00
return NULL;
}
2012-02-07 21:01:54 +01:00
return page->document->functions.page_search_text(page, text, error);
2010-11-17 22:51:15 +01:00
}
2011-10-22 00:11:42 +02:00
girara_list_t*
2012-02-07 21:01:54 +01:00
zathura_page_links_get(zathura_page_t* page, zathura_plugin_error_t* error)
2010-11-17 22:51:15 +01:00
{
if (page == NULL || page->document == NULL || page->document->zathura == NULL
|| page->document->zathura->ui.session == NULL) {
if (error != NULL) {
*error = ZATHURA_PLUGIN_ERROR_INVALID_ARGUMENTS;
}
2010-11-17 23:15:08 +01:00
return NULL;
}
2011-09-21 00:25:41 +02:00
if (page->document->functions.page_links_get == NULL) {
girara_notify(page->document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
2011-04-02 23:40:57 +02:00
girara_error("%s not implemented", __FUNCTION__);
if (error != NULL) {
*error = ZATHURA_PLUGIN_ERROR_NOT_IMPLEMENTED;
}
2010-11-17 23:15:08 +01:00
return NULL;
}
2012-02-07 21:01:54 +01:00
return page->document->functions.page_links_get(page, error);
2010-11-17 22:51:15 +01:00
}
2012-02-07 21:01:54 +01:00
zathura_plugin_error_t
2011-10-22 00:11:42 +02:00
zathura_page_links_free(girara_list_t* UNUSED(list))
2010-11-17 22:51:15 +01:00
{
return false;
}
2011-10-22 00:11:42 +02:00
girara_list_t*
2012-02-07 21:01:54 +01:00
zathura_page_form_fields_get(zathura_page_t* page, zathura_plugin_error_t* error)
2010-11-17 22:51:15 +01:00
{
if (page == NULL || page->document == NULL || page->document->zathura == NULL
|| page->document->zathura->ui.session == NULL) {
if (error != NULL) {
*error = ZATHURA_PLUGIN_ERROR_INVALID_ARGUMENTS;
}
2010-11-17 23:15:08 +01:00
return NULL;
}
2011-09-21 00:25:41 +02:00
if (page->document->functions.page_form_fields_get == NULL) {
girara_notify(page->document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
2011-04-02 23:40:57 +02:00
girara_error("%s not implemented", __FUNCTION__);
if (error != NULL) {
*error = ZATHURA_PLUGIN_ERROR_NOT_IMPLEMENTED;
}
2010-11-17 23:15:08 +01:00
return NULL;
}
2012-02-07 21:01:54 +01:00
return page->document->functions.page_form_fields_get(page, error);
2010-11-17 22:51:15 +01:00
}
2012-02-07 21:01:54 +01:00
zathura_plugin_error_t
2011-10-22 00:11:42 +02:00
zathura_page_form_fields_free(girara_list_t* UNUSED(list))
2010-11-17 22:51:15 +01:00
{
return ZATHURA_PLUGIN_ERROR_NOT_IMPLEMENTED;
2010-11-17 22:51:15 +01:00
}
2010-11-17 23:15:08 +01:00
girara_list_t*
2012-02-07 21:01:54 +01:00
zathura_page_images_get(zathura_page_t* page, zathura_plugin_error_t* error)
{
if (page == NULL || page->document == NULL || page->document->zathura == NULL
|| page->document->zathura->ui.session == NULL) {
if (error != NULL) {
*error = ZATHURA_PLUGIN_ERROR_INVALID_ARGUMENTS;
}
return NULL;
}
if (page->document->functions.page_images_get == NULL) {
girara_notify(page->document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
girara_error("%s not implemented", __FUNCTION__);
if (error != NULL) {
*error = ZATHURA_PLUGIN_ERROR_NOT_IMPLEMENTED;
}
return NULL;
}
2012-02-07 21:01:54 +01:00
return page->document->functions.page_images_get(page, error);
}
cairo_surface_t*
zathura_page_image_get_cairo(zathura_page_t* page, zathura_image_t* image, zathura_plugin_error_t* error)
{
if (page == NULL || page->document == NULL || image == NULL ||
page->document->zathura == NULL || page->document->zathura->ui.session ==
NULL) {
if (error != NULL) {
*error = ZATHURA_PLUGIN_ERROR_INVALID_ARGUMENTS;
}
return NULL;
}
if (page->document->functions.page_image_get_cairo == NULL) {
girara_notify(page->document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
girara_error("%s not implemented", __FUNCTION__);
if (error != NULL) {
*error = ZATHURA_PLUGIN_ERROR_NOT_IMPLEMENTED;
}
return NULL;
}
return page->document->functions.page_image_get_cairo(page, image, error);
}
2012-02-08 23:20:22 +01:00
char* zathura_page_get_text(zathura_page_t* page, zathura_rectangle_t rectangle, zathura_plugin_error_t* error)
{
if (page == NULL || page->document == NULL || page->document->zathura == NULL || page->document->zathura->ui.session == NULL) {
2012-02-08 23:20:22 +01:00
if (error) {
*error = ZATHURA_PLUGIN_ERROR_INVALID_ARGUMENTS;
}
return NULL;
}
if (page->document->functions.page_get_text == NULL) {
girara_notify(page->document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
2012-02-08 23:20:22 +01:00
girara_error("%s not implemented", __FUNCTION__);
if (error) {
*error = ZATHURA_PLUGIN_ERROR_NOT_IMPLEMENTED;
}
return NULL;
}
return page->document->functions.page_get_text(page, rectangle, error);
}
2012-02-07 21:01:54 +01:00
zathura_plugin_error_t
zathura_page_render(zathura_page_t* page, cairo_t* cairo, bool printing)
2010-11-17 23:15:08 +01:00
{
if (page == NULL || page->document == NULL || cairo == NULL ||
page->document->zathura == NULL || page->document->zathura->ui.session ==
NULL) {
2012-02-07 21:01:54 +01:00
return ZATHURA_PLUGIN_ERROR_INVALID_ARGUMENTS;
2010-11-17 23:15:08 +01:00
}
2011-09-29 12:35:52 +02:00
if (page->document->functions.page_render_cairo == NULL) {
girara_notify(page->document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
2011-04-02 23:40:57 +02:00
girara_error("%s not implemented", __FUNCTION__);
2012-02-07 21:01:54 +01:00
return ZATHURA_PLUGIN_ERROR_NOT_IMPLEMENTED;
2010-11-17 23:15:08 +01:00
}
return page->document->functions.page_render_cairo(page, cairo, printing);
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-09-21 00:25:41 +02:00
if (title == NULL) {
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
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-09-21 00:25:41 +02:00
if (index == NULL) {
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);
2011-04-19 16:02:54 +02:00
free(image_buffer);
}
2011-09-29 15:23:13 +02:00
bool
zathura_type_plugin_mapping_new(zathura_t* zathura, const gchar* type, zathura_document_plugin_t* plugin)
{
g_return_val_if_fail(zathura && type && plugin, false);
GIRARA_LIST_FOREACH(zathura->plugins.type_plugin_mapping, zathura_type_plugin_mapping_t*, iter, mapping)
if (g_content_type_equals(type, mapping->type)) {
girara_list_iterator_free(iter);
return false;
}
2011-10-17 11:16:14 +02:00
GIRARA_LIST_FOREACH_END(zathura->plugins.type_plugin_mapping, zathura_type_plugin_mapping_t*, iter, mapping);
2011-09-29 15:23:13 +02:00
zathura_type_plugin_mapping_t* mapping = g_malloc(sizeof(zathura_type_plugin_mapping_t));
mapping->type = g_strdup(type);
mapping->plugin = plugin;
girara_list_append(zathura->plugins.type_plugin_mapping, mapping);
return true;
}
void
zathura_type_plugin_mapping_free(zathura_type_plugin_mapping_t* mapping)
{
if (mapping == NULL) {
return;
}
g_free((void*)mapping->type);
g_free(mapping);
}
2012-02-07 17:41:40 +01:00
void
zathura_link_free(zathura_link_t* link)
{
if (link == NULL) {
return;
}
if (link->type == ZATHURA_LINK_EXTERNAL) {
g_free(link->target.value);
}
g_free(link);
}