From 365dc9a66e9a7601aac9dcced9537c41f6443749 Mon Sep 17 00:00:00 2001 From: Moritz Lipp Date: Mon, 28 May 2012 12:43:22 +0200 Subject: [PATCH] Improve zathura_link_t --- Makefile | 2 +- callbacks.c | 1 + internal.h | 7 -- links.c | 191 ++++++++++++++++++++++++++++++++++++++++++++++++++ links.h | 59 ++++++++++++++++ page-widget.c | 7 +- plugin-api.h | 1 + types.c | 87 +---------------------- types.h | 71 +++++++------------ utils.c | 80 +-------------------- utils.h | 19 ----- 11 files changed, 285 insertions(+), 240 deletions(-) create mode 100644 links.c create mode 100644 links.h diff --git a/Makefile b/Makefile index 96862d0..aa0fc6a 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ include common.mk PROJECT = zathura OSOURCE = $(wildcard *.c) HEADER = $(wildcard *.h) -HEADERINST = version.h document.h macros.h page.h types.h plugin-api.h +HEADERINST = version.h document.h macros.h page.h types.h plugin-api.h links.h ifneq (${WITH_SQLITE},0) INCS += $(SQLITE_INC) diff --git a/callbacks.c b/callbacks.c index 8337d98..81a1183 100644 --- a/callbacks.c +++ b/callbacks.c @@ -10,6 +10,7 @@ #include #include "callbacks.h" +#include "links.h" #include "zathura.h" #include "render.h" #include "document.h" diff --git a/internal.h b/internal.h index 4483a99..4ca2fac 100644 --- a/internal.h +++ b/internal.h @@ -21,13 +21,6 @@ struct zathura_document_information_entry_s char* value; /**< Value */ }; -struct zathura_link_s -{ - zathura_rectangle_t position; /**< Position of the link */ - zathura_link_type_t type; /**< Link type */ - zathura_link_target_t target; /**< Link target */ -}; - /** * Returns the associated plugin * diff --git a/links.c b/links.c new file mode 100644 index 0000000..730bf42 --- /dev/null +++ b/links.c @@ -0,0 +1,191 @@ +/* See LICENSE file for license and copyright information */ + +#include +#include +#include +#include + +#include "links.h" +#include "zathura.h" +#include "document.h" + +struct zathura_link_s +{ + zathura_rectangle_t position; /**< Position of the link */ + zathura_link_type_t type; /**< Link type */ + zathura_link_target_t target; /**< Link target */ +}; + +/* forward declarations */ +static void link_remote(zathura_t* zathura, const char* file); +static void link_launch(zathura_t* zathura, zathura_link_t* link); + +zathura_link_t* +zathura_link_new(zathura_link_type_t type, zathura_rectangle_t position, + zathura_link_target_t target) +{ + zathura_link_t* link = g_malloc0(sizeof(zathura_link_t)); + + link->type = type; + link->position = position; + + switch (type) { + case ZATHURA_LINK_GOTO_DEST: + link->target.page_number = target.page_number; + break; + case ZATHURA_LINK_GOTO_REMOTE: + case ZATHURA_LINK_URI: + if (target.value == NULL) { + g_free(link); + return NULL; + } + + link->target.value = g_strdup(target.value); + break; + case ZATHURA_LINK_LAUNCH: + if (target.value == NULL) { + g_free(link); + return NULL; + } + + link->target.value = g_strdup(target.value); + break; + default: + g_free(link); + return NULL; + } + + return link; +} + +void +zathura_link_free(zathura_link_t* link) +{ + if (link == NULL) { + return; + } + + switch (link->type) { + case ZATHURA_LINK_URI: + case ZATHURA_LINK_LAUNCH: + if (link->target.value != NULL) { + g_free(link->target.value); + } + break; + default: + break; + } + + g_free(link); +} + +zathura_link_type_t +zathura_link_get_type(zathura_link_t* link) +{ + if (link == NULL) { + return ZATHURA_LINK_INVALID; + } + + return link->type; +} + +zathura_rectangle_t +zathura_link_get_position(zathura_link_t* link) +{ + if (link == NULL) { + zathura_rectangle_t position = { 0, 0, 0, 0 }; + return position; + } + + return link->position; +} + +zathura_link_target_t +zathura_link_get_target(zathura_link_t* link) +{ + if (link == NULL) { + zathura_link_target_t target = { 0 }; + return target; + } + + return link->target; +} + +void +zathura_link_evaluate(zathura_t* zathura, zathura_link_t* link) +{ + if (zathura == NULL || link == NULL) { + return; + } + + switch (link->type) { + case ZATHURA_LINK_GOTO_DEST: + page_set_delayed(zathura, link->target.page_number); + break; + case ZATHURA_LINK_GOTO_REMOTE: + link_remote(zathura, link->target.value); + break; + case ZATHURA_LINK_URI: + if (girara_xdg_open(link->target.value) == false) { + girara_notify(zathura->ui.session, GIRARA_ERROR, _("Failed to run xdg-open.")); + } + break; + case ZATHURA_LINK_LAUNCH: + link_launch(zathura, link); + break; + default: + break; + } +} + +static void +link_remote(zathura_t* zathura, const char* file) +{ + if (zathura == NULL || file == NULL || zathura->document == NULL) { + return; + } + + const char* path = zathura_document_get_path(zathura->document); + char* dir = g_path_get_dirname(path); + char* uri = g_build_filename(dir, file, NULL); + + char* argv[] = { + *(zathura->global.arguments), + uri, + NULL + }; + + g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL); + + g_free(uri); + g_free(dir); +} + +static void +link_launch(zathura_t* zathura, zathura_link_t* link) +{ + if (zathura == NULL || link == NULL || zathura->document == NULL) { + return; + } + + /* get file path */ + if (link->target.value == NULL) { + return; + }; + + char* path = NULL; + if (g_path_is_absolute(link->target.value) == TRUE) { + path = g_strdup(link->target.value); + } else { + const char* document = zathura_document_get_path(zathura->document); + char* dir = g_path_get_dirname(document); + path = g_build_filename(dir, link->target.value, NULL); + g_free(dir); + } + + if (girara_xdg_open(path) == false) { + girara_notify(zathura->ui.session, GIRARA_ERROR, _("Failed to run xdg-open.")); + } + + g_free(path); +} diff --git a/links.h b/links.h new file mode 100644 index 0000000..3dbf8df --- /dev/null +++ b/links.h @@ -0,0 +1,59 @@ +/* See LICENSE file for license and copyright information */ + +#ifndef LINK_H +#define LINK_H + +#include "types.h" + +/** + * Creates a new zathura link + * + * @param type Type of the link + * @param position Position of the link + * @param target Target + * @return New zathura link + */ +zathura_link_t* +zathura_link_new(zathura_link_type_t type, zathura_rectangle_t position, + zathura_link_target_t target); + +/** + * Free link + * + * @param link The link + */ +void zathura_link_free(zathura_link_t* link); + +/** + * Returns the type of the link + * + * @param link The link + * @return The target type of the link + */ +zathura_link_type_t zathura_link_get_type(zathura_link_t* link); + +/** + * Returns the position of the link + * + * @param link The link + * @return The position of the link + */ +zathura_rectangle_t zathura_link_get_position(zathura_link_t* link); + +/** + * The target value of the link + * + * @param link The link + * @return Returns the target of the link (depends on the link type) + */ +zathura_link_target_t zathura_link_get_target(zathura_link_t* link); + +/** + * Evaluate link + * + * @param zathura Zathura instance + * @param link The link + */ +void zathura_link_evaluate(zathura_t* zathura, zathura_link_t* link); + +#endif // LINK_H diff --git a/page-widget.c b/page-widget.c index b4c80fb..d52e14d 100644 --- a/page-widget.c +++ b/page-widget.c @@ -7,6 +7,7 @@ #include #include +#include "links.h" #include "page-widget.h" #include "page.h" #include "render.h" @@ -224,7 +225,7 @@ zathura_page_widget_set_property(GObject* object, guint prop_id, const GValue* v if (priv->links.retrieved == true && priv->links.list != NULL) { GIRARA_LIST_FOREACH(priv->links.list, zathura_link_t*, iter, link) if (link != NULL) { - zathura_rectangle_t rectangle = recalc_rectangle(priv->page, link->position); + zathura_rectangle_t rectangle = recalc_rectangle(priv->page, zathura_link_get_position(link)); redraw_rect(pageview, &rectangle); } GIRARA_LIST_FOREACH_END(priv->links.list, zathura_link_t*, iter, link); @@ -379,7 +380,7 @@ zathura_page_widget_draw(GtkWidget* widget, cairo_t* cairo) unsigned int link_counter = 0; GIRARA_LIST_FOREACH(priv->links.list, zathura_link_t*, iter, link) if (link != NULL) { - zathura_rectangle_t rectangle = recalc_rectangle(priv->page, link->position); + zathura_rectangle_t rectangle = recalc_rectangle(priv->page, zathura_link_get_position(link)); /* draw position */ GdkColor color = priv->zathura->ui.colors.highlight_color; @@ -588,7 +589,7 @@ cb_zathura_page_widget_button_release_event(GtkWidget* widget, GdkEventButton* b if (priv->links.list != NULL && priv->links.n > 0) { GIRARA_LIST_FOREACH(priv->links.list, zathura_link_t*, iter, link) - zathura_rectangle_t rect = recalc_rectangle(priv->page, link->position); + zathura_rectangle_t rect = recalc_rectangle(priv->page, zathura_link_get_position(link)); if (rect.x1 <= button->x && rect.x2 >= button->x && rect.y1 <= button->y && rect.y2 >= button->y) { zathura_link_evaluate(priv->zathura, link); diff --git a/plugin-api.h b/plugin-api.h index 0954a67..95946c4 100644 --- a/plugin-api.h +++ b/plugin-api.h @@ -5,6 +5,7 @@ #include "page.h" #include "document.h" +#include "links.h" #include "version.h" typedef struct zathura_plugin_functions_s zathura_plugin_functions_t; diff --git a/types.c b/types.c index 568c867..8027920 100644 --- a/types.c +++ b/types.c @@ -5,94 +5,9 @@ #include #include "types.h" +#include "links.h" #include "internal.h" -zathura_link_t* -zathura_link_new(zathura_link_type_t type, zathura_rectangle_t position, - zathura_link_target_t target) -{ - zathura_link_t* link = g_malloc0(sizeof(zathura_link_t)); - - link->type = type; - link->position = position; - - switch (type) { - case ZATHURA_LINK_GOTO_DEST: - link->target.page_number = target.page_number; - break; - case ZATHURA_LINK_GOTO_REMOTE: - case ZATHURA_LINK_URI: - if (target.value == NULL) { - g_free(link); - return NULL; - } - - link->target.value = g_strdup(target.value); - break; - case ZATHURA_LINK_LAUNCH: - if (target.value == NULL) { - g_free(link); - return NULL; - } - - link->target.value = g_strdup(target.value); - break; - default: - g_free(link); - return NULL; - } - - return link; -} - -void -zathura_link_free(zathura_link_t* link) -{ - if (link == NULL) { - return; - } - - if (link->type == ZATHURA_LINK_URI) { - if (link->target.value != NULL) { - g_free(link->target.value); - } - } - - g_free(link); -} - -zathura_link_type_t -zathura_link_get_type(zathura_link_t* link) -{ - if (link == NULL) { - return ZATHURA_LINK_INVALID; - } - - return link->type; -} - -zathura_rectangle_t -zathura_link_get_position(zathura_link_t* link) -{ - if (link == NULL) { - zathura_rectangle_t position = { 0, 0, 0, 0 }; - return position; - } - - return link->position; -} - -zathura_link_target_t -zathura_link_get_target(zathura_link_t* link) -{ - if (link == NULL) { - zathura_link_target_t target = { 0 }; - return target; - } - - return link->target; -} - zathura_index_element_t* zathura_index_element_new(const char* title) { diff --git a/types.h b/types.h index 594497d..a72ee1d 100644 --- a/types.h +++ b/types.h @@ -3,6 +3,8 @@ #ifndef TYPES_H #define TYPES_H +#include + #include "macros.h" /** @@ -130,17 +132,38 @@ typedef struct zathura_image_s typedef enum zathura_link_type_e { ZATHURA_LINK_INVALID, /**< Invalid type */ + ZATHURA_LINK_NONE, /**< No action */ ZATHURA_LINK_GOTO_DEST, /**< Links to a page */ ZATHURA_LINK_GOTO_REMOTE, /**< Links to a page */ ZATHURA_LINK_URI, /**< Links to an external source */ ZATHURA_LINK_LAUNCH, /**< Links to an external source */ - ZATHURA_LINK_NAMED, /**< Links to an external source */ + ZATHURA_LINK_NAMED /**< Links to an external source */ } zathura_link_type_t; -typedef union zathura_link_target_u +typedef enum zathura_link_destionation_type_e { - unsigned int page_number; /**< Page number */ + ZATHURA_LINK_DESTINATION_UNKNOWN, + ZATHURA_LINK_DESTINATION_XYZ, + ZATHURA_LINK_DESTINATION_FIT, + ZATHURA_LINK_DESTINATION_FITH, + ZATHURA_LINK_DESTINATION_FITV, + ZATHURA_LINK_DESTINATION_FITR, + ZATHURA_LINK_DESTINATION_FITB, + ZATHURA_LINK_DESTINATION_FITBH, + ZATHURA_LINK_DESTINATION_FITBV, + ZATHURA_LINK_DESTINATION_NAMED +} zathura_link_destionation_type_t; + +typedef struct zathura_link_target_s +{ + zathura_link_destionation_type_t destination_type; char* value; /**< Value */ + unsigned int page_number; /**< Page number */ + double left; /**< Left coordinate */ + double right; /**< Right coordinate */ + double top; /**< Top coordinate */ + double bottom; /**< Bottom coordinate */ + double scale; /**< Scale */ } zathura_link_target_t; /** @@ -190,48 +213,6 @@ zathura_index_element_t* zathura_index_element_new(const char* title); */ void zathura_index_element_free(zathura_index_element_t* index); -/** - * Creates a new zathura link - * - * @param type Type of the link - * @param position Position of the link - * @param target Target - * @return New zathura link - */ -zathura_link_t* zathura_link_new(zathura_link_type_t type, zathura_rectangle_t position, - zathura_link_target_t target); - -/** - * Free link - * - * @param link The link - */ -void zathura_link_free(zathura_link_t* link); - -/** - * Returns the type of the link - * - * @param link The link - * @return The target type of the link - */ -zathura_link_type_t zathura_link_get_type(zathura_link_t* link); - -/** - * Returns the position of the link - * - * @param link The link - * @return The position of the link - */ -zathura_rectangle_t zathura_link_get_position(zathura_link_t* link); - -/** - * The target value of the link - * - * @param link The link - * @return Returns the target of the link (depends on the link type) - */ -zathura_link_target_t zathura_link_get_target(zathura_link_t* link); - /** * Creates a list that should be used to store \ref * zathura_document_information_entry_t entries diff --git a/utils.c b/utils.c index 3c30ba2..135861f 100644 --- a/utils.c +++ b/utils.c @@ -13,6 +13,7 @@ #include #include +#include "links.h" #include "utils.h" #include "zathura.h" #include "internal.h" @@ -327,85 +328,6 @@ readjust_view_after_zooming(zathura_t *zathura, float old_zoom) { position_set_delayed(zathura, valx, valy); } -static void -link_launch(zathura_t* zathura, zathura_link_t* link) -{ - if (zathura == NULL || link == NULL || zathura->document == NULL) { - return; - } - - /* get file path */ - if (link->target.value == NULL) { - return; - }; - - char* path = NULL; - if (g_path_is_absolute(link->target.value) == TRUE) { - path = g_strdup(link->target.value); - } else { - const char* document = zathura_document_get_path(zathura->document); - char* dir = g_path_get_dirname(document); - path = g_build_filename(dir, link->target.value, NULL); - g_free(dir); - } - - if (girara_xdg_open(path) == false) { - girara_notify(zathura->ui.session, GIRARA_ERROR, _("Failed to run xdg-open.")); - } - - g_free(path); -} - -void -zathura_link_evaluate(zathura_t* zathura, zathura_link_t* link) -{ - if (zathura == NULL || link == NULL) { - return; - } - - switch (link->type) { - case ZATHURA_LINK_GOTO_DEST: - page_set_delayed(zathura, link->target.page_number); - break; - case ZATHURA_LINK_GOTO_REMOTE: - open_remote(zathura, link->target.value); - break; - case ZATHURA_LINK_URI: - if (girara_xdg_open(link->target.value) == false) { - girara_notify(zathura->ui.session, GIRARA_ERROR, _("Failed to run xdg-open.")); - } - break; - case ZATHURA_LINK_LAUNCH: - link_launch(zathura, link); - break; - default: - break; - } -} - -void -open_remote(zathura_t* zathura, const char* file) -{ - if (zathura == NULL || file == NULL || zathura->document == NULL) { - return; - } - - const char* path = zathura_document_get_path(zathura->document); - char* dir = g_path_get_dirname(path); - char* uri = g_build_filename(dir, file, NULL); - - char* argv[] = { - *(zathura->global.arguments), - uri, - NULL - }; - - g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL); - - g_free(uri); - g_free(dir); -} - void document_draw_search_results(zathura_t* zathura, bool value) { diff --git a/utils.h b/utils.h index 3787b54..504c7e2 100644 --- a/utils.h +++ b/utils.h @@ -122,25 +122,6 @@ GtkWidget* zathura_page_get_widget(zathura_t* zathura, zathura_page_t* page); */ void readjust_view_after_zooming(zathura_t* zathura, float old_zoom); -/** - * Evaluate link - * - * @param zathura Zathura instance - * @param link The link - */ -void zathura_link_evaluate(zathura_t* zathura, zathura_link_t* link); - -/** - * Opens a remote file - * - * Determines the path of the current document and tries to open a given file - * in that path - * - * @param zathura Zathura instance - * @param file File name - */ -void open_remote(zathura_t* zathura, const char* file); - /** * Set if the search results should be drawn or not *