links.c 7.6 KB
Newer Older
1
/* SPDX-License-Identifier: Zlib */
Moritz Lipp's avatar
Moritz Lipp committed
2 3 4 5 6

#include <glib.h>
#include <glib/gi18n.h>
#include <girara/utils.h>
#include <girara/session.h>
7
#include <girara/settings.h>
Moritz Lipp's avatar
Moritz Lipp committed
8

9
#include "adjustment.h"
Moritz Lipp's avatar
Moritz Lipp committed
10 11 12
#include "links.h"
#include "zathura.h"
#include "document.h"
13
#include "utils.h"
14
#include "page.h"
15
#include "render.h"
Moritz Lipp's avatar
Moritz Lipp committed
16

Moritz Lipp's avatar
Moritz Lipp committed
17
struct zathura_link_s {
Moritz Lipp's avatar
Moritz Lipp committed
18 19 20 21 22 23 24
  zathura_rectangle_t position; /**< Position of the link */
  zathura_link_type_t type; /**< Link type */
  zathura_link_target_t target; /**< Link target */
};

zathura_link_t*
zathura_link_new(zathura_link_type_t type, zathura_rectangle_t position,
Moritz Lipp's avatar
Moritz Lipp committed
25
                 zathura_link_target_t target)
Moritz Lipp's avatar
Moritz Lipp committed
26
{
27 28 29 30
  zathura_link_t* link = g_try_malloc0(sizeof(zathura_link_t));
  if (link == NULL) {
    return NULL;
  }
Moritz Lipp's avatar
Moritz Lipp committed
31 32 33 34 35

  link->type     = type;
  link->position = position;

  switch (type) {
36
    case ZATHURA_LINK_NONE:
Moritz Lipp's avatar
Moritz Lipp committed
37
    case ZATHURA_LINK_GOTO_DEST:
38 39 40 41 42
      link->target = target;

      if (target.value != NULL) {
        link->target.value = g_strdup(target.value);
      }
Moritz Lipp's avatar
Moritz Lipp committed
43 44 45 46
      break;
    case ZATHURA_LINK_GOTO_REMOTE:
    case ZATHURA_LINK_URI:
    case ZATHURA_LINK_LAUNCH:
47
    case ZATHURA_LINK_NAMED:
Moritz Lipp's avatar
Moritz Lipp committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
      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) {
71
    case ZATHURA_LINK_NONE:
72 73
    case ZATHURA_LINK_GOTO_DEST:
    case ZATHURA_LINK_GOTO_REMOTE:
Moritz Lipp's avatar
Moritz Lipp committed
74 75
    case ZATHURA_LINK_URI:
    case ZATHURA_LINK_LAUNCH:
76
    case ZATHURA_LINK_NAMED:
Moritz Lipp's avatar
Moritz Lipp committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
      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) {
Sebastian Ramacher's avatar
Sebastian Ramacher committed
113
    zathura_link_target_t target = { 0, NULL, 0, 0, 0, 0, 0, 0 };
Moritz Lipp's avatar
Moritz Lipp committed
114 115 116 117 118 119
    return target;
  }

  return link->target;
}

Sebastian Ramacher's avatar
Sebastian Ramacher committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
static void
link_goto_dest(zathura_t* zathura, const zathura_link_t* link)
{
  if (link->target.destination_type == ZATHURA_LINK_DESTINATION_UNKNOWN) {
    girara_warning("link destination type unknown");
    return;
  }

  bool link_zoom = true;
  girara_setting_get(zathura->ui.session, "link-zoom", &link_zoom);

  if (link->target.zoom >= DBL_EPSILON && link_zoom == true) {
    zathura_document_set_zoom(zathura->document,
        zathura_correct_zoom_value(zathura->ui.session, link->target.zoom));
    render_all(zathura);
  }

  /* get page */
  zathura_page_t* page = zathura_document_get_page(zathura->document,
      link->target.page_number);
  if (page == NULL) {
    girara_warning("link to non-existing page %u", link->target.page_number);
    return;
  }

  /* compute the position with the page aligned to the top and left
     of the viewport */
  double pos_x = 0;
  double pos_y = 0;
  page_number_to_position(zathura->document, link->target.page_number,
                          0.0, 0.0, &pos_x, &pos_y);

  /* correct to place the target position at the top of the viewport     */
  /* NOTE: link->target is in page units, needs to be scaled and rotated */
  unsigned int cell_height = 0;
  unsigned int cell_width = 0;
  zathura_document_get_cell_size(zathura->document, &cell_height, &cell_width);

  unsigned int doc_height = 0;
  unsigned int doc_width = 0;
  zathura_document_get_document_size(zathura->document, &doc_height, &doc_width);

  bool link_hadjust = true;
  girara_setting_get(zathura->ui.session, "link-hadjust", &link_hadjust);

  /* scale and rotate */
  double scale = zathura_document_get_scale(zathura->document);
  double shiftx = link->target.left * scale / (double)cell_width;
  double shifty = link->target.top * scale / (double)cell_height;
  page_calc_position(zathura->document, shiftx, shifty, &shiftx, &shifty);

  /* shift the position or set to auto */
  if (link->target.destination_type == ZATHURA_LINK_DESTINATION_XYZ &&
      link->target.left != -1 && link_hadjust == true) {
    pos_x += shiftx / (double)doc_width;
  } else {
    pos_x = -1;     /* -1 means automatic */
  }

  if (link->target.destination_type == ZATHURA_LINK_DESTINATION_XYZ &&
      link->target.top != -1) {
    pos_y += shifty / (double)doc_height;
  } else {
    pos_y = -1;     /* -1 means automatic */
  }

  /* move to position */
  zathura_jumplist_add(zathura);
  zathura_document_set_current_page_number(zathura->document, link->target.page_number);
  position_set(zathura, pos_x, pos_y);
  zathura_jumplist_add(zathura);
}

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
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
  };

  GError* error = NULL;
  if (g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, &error) == FALSE) {
    girara_error("Failed to execute command: %s", error->message);
    g_error_free(error);
  }

  g_free(uri);
  g_free(dir);
}

static void
Sebastian Ramacher's avatar
Sebastian Ramacher committed
221
link_launch(zathura_t* zathura, const zathura_link_t* link)
222 223 224 225 226 227
{
  /* get file path */
  if (link->target.value == NULL) {
    return;
  };

228 229
  const char* document = zathura_document_get_path(zathura->document);
  char* dir  = g_path_get_dirname(document);
230

231
  if (girara_xdg_open_with_working_directory(link->target.value, dir) == false) {
232 233 234
    girara_notify(zathura->ui.session, GIRARA_ERROR, _("Failed to run xdg-open."));
  }

235
  g_free(dir);
236 237
}

Moritz Lipp's avatar
Moritz Lipp committed
238 239 240
void
zathura_link_evaluate(zathura_t* zathura, zathura_link_t* link)
{
241
  if (zathura == NULL || zathura->document == NULL || link == NULL) {
Moritz Lipp's avatar
Moritz Lipp committed
242 243 244
    return;
  }

245 246 247 248 249
  if (link->type != ZATHURA_LINK_GOTO_DEST && zathura->global.sandbox == ZATHURA_SANDBOX_STRICT) {
    girara_notify(zathura->ui.session, GIRARA_ERROR, _("Opening external applications in strict sandbox mode is not permitted"));
    return;
  }

Moritz Lipp's avatar
Moritz Lipp committed
250 251
  switch (link->type) {
    case ZATHURA_LINK_GOTO_DEST:
252
      girara_debug("Going to link destination: page: %d", link->target.page_number);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
253
      link_goto_dest(zathura, link);
Moritz Lipp's avatar
Moritz Lipp committed
254 255
      break;
    case ZATHURA_LINK_GOTO_REMOTE:
256
      girara_debug("Going to remote destination: %s", link->target.value);
Moritz Lipp's avatar
Moritz Lipp committed
257 258 259
      link_remote(zathura, link->target.value);
      break;
    case ZATHURA_LINK_URI:
260
      girara_debug("Opening URI: %s", link->target.value);
261
      link_launch(zathura, link);
Moritz Lipp's avatar
Moritz Lipp committed
262 263
      break;
    case ZATHURA_LINK_LAUNCH:
264
      girara_debug("Launching link: %s", link->target.value);
Moritz Lipp's avatar
Moritz Lipp committed
265 266 267 268 269 270 271
      link_launch(zathura, link);
      break;
    default:
      break;
  }
}

272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
void
zathura_link_display(zathura_t* zathura, zathura_link_t* link)
{
  zathura_link_type_t type = zathura_link_get_type(link);
  zathura_link_target_t target = zathura_link_get_target(link);
  switch (type) {
    case ZATHURA_LINK_GOTO_DEST:
      girara_notify(zathura->ui.session, GIRARA_INFO, _("Link: page %d"),
          target.page_number);
      break;
    case ZATHURA_LINK_GOTO_REMOTE:
    case ZATHURA_LINK_URI:
    case ZATHURA_LINK_LAUNCH:
    case ZATHURA_LINK_NAMED:
      girara_notify(zathura->ui.session, GIRARA_INFO, _("Link: %s"),
          target.value);
      break;
    default:
      girara_notify(zathura->ui.session, GIRARA_ERROR, _("Link: Invalid"));
  }
}