zathura.c 42.8 KB
Newer Older
1
2
/* See LICENSE file for license and copyright information */

3
#define _DEFAULT_SOURCE
Sebastian Ramacher's avatar
Sebastian Ramacher committed
4
#define _XOPEN_SOURCE 700
5

6
#include <errno.h>
Moritz Lipp's avatar
Moritz Lipp committed
7
#include <stdlib.h>
Pavel Borzenkov's avatar
Pavel Borzenkov committed
8
#include <unistd.h>
9
#include <math.h>
Moritz Lipp's avatar
Moritz Lipp committed
10

11
12
13
14
15
#include <girara/datastructures.h>
#include <girara/utils.h>
#include <girara/session.h>
#include <girara/statusbar.h>
#include <girara/settings.h>
16
#include <girara/shortcuts.h>
Sebastian Ramacher's avatar
Sebastian Ramacher committed
17
#include <girara/template.h>
18
#include <glib/gstdio.h>
19
#include <glib/gi18n.h>
20

Moritz Lipp's avatar
Moritz Lipp committed
21
22
23
24
#ifdef G_OS_UNIX
#include <glib-unix.h>
#endif

25
#include "bookmarks.h"
Moritz Lipp's avatar
Moritz Lipp committed
26
27
#include "callbacks.h"
#include "config.h"
28
29
30
31
#ifdef WITH_SQLITE
#include "database-sqlite.h"
#endif
#include "database-plain.h"
32
#include "document.h"
Moritz Lipp's avatar
Moritz Lipp committed
33
#include "shortcuts.h"
Moritz Lipp's avatar
Moritz Lipp committed
34
#include "zathura.h"
Moritz Lipp's avatar
Moritz Lipp committed
35
#include "utils.h"
Moritz Lipp's avatar
Moritz Lipp committed
36
#include "marks.h"
Sebastian Ramacher's avatar
Sebastian Ramacher committed
37
#include "render.h"
Moritz Lipp's avatar
Moritz Lipp committed
38
#include "page.h"
39
#include "page-widget.h"
40
#include "plugin.h"
41
#include "adjustment.h"
Sebastian Ramacher's avatar
Sebastian Ramacher committed
42
#include "dbus-interface.h"
Sebastian Ramacher's avatar
Sebastian Ramacher committed
43
#include "css-definitions.h"
Moritz Lipp's avatar
Moritz Lipp committed
44

Moritz Lipp's avatar
Moritz Lipp committed
45
typedef struct zathura_document_info_s {
Moritz Lipp's avatar
Update    
Moritz Lipp committed
46
47
48
  zathura_t* zathura;
  const char* path;
  const char* password;
49
  int page_number;
Sebastian Ramacher's avatar
Sebastian Ramacher committed
50
  const char* mode;
Moritz Lipp's avatar
Update    
Moritz Lipp committed
51
52
} zathura_document_info_t;

53

54
static gboolean document_info_open(gpointer data);
55
56
57
static void zathura_jumplist_reset_current(zathura_t* zathura);
static void zathura_jumplist_append_jump(zathura_t* zathura);
static void zathura_jumplist_save(zathura_t* zathura);
Moritz Lipp's avatar
Moritz Lipp committed
58

Moritz Lipp's avatar
Moritz Lipp committed
59
60
61
62
#ifdef G_OS_UNIX
static gboolean zathura_signal_sigterm(gpointer data);
#endif

Moritz Lipp's avatar
Moritz Lipp committed
63
/* function implementation */
Moritz Lipp's avatar
Moritz Lipp committed
64
zathura_t*
65
zathura_create(void)
66
{
67
68
69
70
  zathura_t* zathura = g_try_malloc0(sizeof(zathura_t));
  if (zathura == NULL) {
    return NULL;
  }
71

72
  /* global settings */
73
  zathura->global.search_direction = FORWARD;
74

75
  /* plugins */
Moritz Lipp's avatar
Moritz Lipp committed
76
77
  zathura->plugins.manager = zathura_plugin_manager_new();
  if (zathura->plugins.manager == NULL) {
78
    goto error_out;
Moritz Lipp's avatar
Moritz Lipp committed
79
  }
80

81
82
83
  /* UI */
  if ((zathura->ui.session = girara_session_create()) == NULL) {
    goto error_out;
Sebastian Ramacher's avatar
Sebastian Ramacher committed
84
85
  }

Moritz Lipp's avatar
Moritz Lipp committed
86
87
88
89
90
#ifdef G_OS_UNIX
  /* signal handler */
  zathura->signals.sigterm = g_unix_signal_add(SIGTERM, zathura_signal_sigterm, zathura);
#endif

91
92
93
94
95
96
97
98
99
100
  zathura->ui.session->global.data = zathura;

  return zathura;

error_out:

  zathura_free(zathura);

  return NULL;
}
Sebastian Ramacher's avatar
Sebastian Ramacher committed
101

102
103
104
105
106
bool
zathura_init(zathura_t* zathura)
{
  if (zathura == NULL) {
    return false;
107
108
  }

109
  /* create zathura (config/data) directory */
110
111
112
113
114
115
116
  if (g_mkdir_with_parents(zathura->config.config_dir, 0771) == -1) {
    girara_error("Could not create '%s': %s", zathura->config.config_dir, strerror(errno));
  }

  if (g_mkdir_with_parents(zathura->config.data_dir, 0771) == -1) {
    girara_error("Could not create '%s': %s", zathura->config.data_dir, strerror(errno));
  }
117

118
119
120
  /* load plugins */
  zathura_plugin_manager_load(zathura->plugins.manager);

121
122
  /* configuration */
  config_load_default(zathura);
123
  config_load_files(zathura);
124

125
  /* UI */
Sebastian Ramacher's avatar
Sebastian Ramacher committed
126
  if (girara_session_init(zathura->ui.session, "zathura") == false) {
Moritz Lipp's avatar
Moritz Lipp committed
127
    goto error_free;
128
129
130
  }

  /* girara events */
Moritz Lipp's avatar
Moritz Lipp committed
131
132
  zathura->ui.session->events.buffer_changed  = cb_buffer_changed;
  zathura->ui.session->events.unknown_command = cb_unknown_command;
133

134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
  /* zathura signals */
  zathura->signals.refresh_view = g_signal_new("refresh-view",
                                               GTK_TYPE_WIDGET,
                                               G_SIGNAL_RUN_LAST,
                                               0,
                                               NULL,
                                               NULL,
                                               g_cclosure_marshal_generic,
                                               G_TYPE_NONE,
                                               1,
                                               G_TYPE_POINTER);

  g_signal_connect(G_OBJECT(zathura->ui.session->gtk.view), "refresh-view",
                   G_CALLBACK(cb_refresh_view), zathura);

149
  /* page view */
Moritz Lipp's avatar
Moritz Lipp committed
150
  zathura->ui.page_widget = gtk_grid_new();
151
152
  gtk_grid_set_row_homogeneous(GTK_GRID(zathura->ui.page_widget), TRUE);
  gtk_grid_set_column_homogeneous(GTK_GRID(zathura->ui.page_widget), TRUE);
Moritz Lipp's avatar
Moritz Lipp committed
153
  if (zathura->ui.page_widget == NULL) {
154
155
156
    goto error_free;
  }

157
  g_signal_connect(G_OBJECT(zathura->ui.session->gtk.window), "size-allocate", G_CALLBACK(cb_view_resized), zathura);
158

159
  GtkAdjustment* hadjustment = gtk_scrolled_window_get_hadjustment(
160
                 GTK_SCROLLED_WINDOW(zathura->ui.session->gtk.view));
161
162
163

  /* Connect hadjustment signals */
  g_signal_connect(G_OBJECT(hadjustment), "value-changed",
164
      G_CALLBACK(cb_view_hadjustment_value_changed), zathura);
165
166
167
168
  g_signal_connect(G_OBJECT(hadjustment), "changed",
      G_CALLBACK(cb_view_hadjustment_changed), zathura);

  GtkAdjustment* vadjustment = gtk_scrolled_window_get_vadjustment(
169
                 GTK_SCROLLED_WINDOW(zathura->ui.session->gtk.view));
170
171
172
173
174
175

  /* Connect vadjustment signals */
  g_signal_connect(G_OBJECT(vadjustment), "value-changed",
      G_CALLBACK(cb_view_vadjustment_value_changed), zathura);
  g_signal_connect(G_OBJECT(vadjustment), "changed",
      G_CALLBACK(cb_view_vadjustment_changed), zathura);
Moritz Lipp's avatar
Moritz Lipp committed
176

177
  /* page view alignment */
178
179
  gtk_widget_set_halign(zathura->ui.page_widget, GTK_ALIGN_CENTER);
  gtk_widget_set_valign(zathura->ui.page_widget, GTK_ALIGN_CENTER);
180

181
182
183
184
  gtk_widget_set_hexpand_set(zathura->ui.page_widget, TRUE);
  gtk_widget_set_hexpand(zathura->ui.page_widget, FALSE);
  gtk_widget_set_vexpand_set(zathura->ui.page_widget, TRUE);
  gtk_widget_set_vexpand(zathura->ui.page_widget, FALSE);
185

186
  gtk_widget_show(zathura->ui.page_widget);
Moritz Lipp's avatar
Moritz Lipp committed
187

188
  /* statusbar */
Moritz Lipp's avatar
Moritz Lipp committed
189
190
  zathura->ui.statusbar.file = girara_statusbar_item_add(zathura->ui.session, TRUE, TRUE, TRUE, NULL);
  if (zathura->ui.statusbar.file == NULL) {
Moritz Lipp's avatar
Moritz Lipp committed
191
    goto error_free;
Moritz Lipp's avatar
Moritz Lipp committed
192
193
  }

Moritz Lipp's avatar
Moritz Lipp committed
194
195
  zathura->ui.statusbar.buffer = girara_statusbar_item_add(zathura->ui.session, FALSE, FALSE, FALSE, NULL);
  if (zathura->ui.statusbar.buffer == NULL) {
Moritz Lipp's avatar
Moritz Lipp committed
196
    goto error_free;
Moritz Lipp's avatar
Moritz Lipp committed
197
198
  }

Moritz Lipp's avatar
Moritz Lipp committed
199
  zathura->ui.statusbar.page_number = girara_statusbar_item_add(zathura->ui.session, FALSE, FALSE, FALSE, NULL);
Moritz Lipp's avatar
Moritz Lipp committed
200
  if (zathura->ui.statusbar.page_number == NULL) {
Moritz Lipp's avatar
Moritz Lipp committed
201
    goto error_free;
Moritz Lipp's avatar
Moritz Lipp committed
202
203
  }

Sebastian Ramacher's avatar
Sebastian Ramacher committed
204
  girara_statusbar_item_set_text(zathura->ui.session, zathura->ui.statusbar.file, _("[No name]"));
Moritz Lipp's avatar
Moritz Lipp committed
205

Moritz Lipp's avatar
Moritz Lipp committed
206
  /* signals */
207
  g_signal_connect(G_OBJECT(zathura->ui.session->gtk.window), "destroy", G_CALLBACK(cb_destroy), zathura);
208

209
  /* database */
210
211
212
213
  char* database = NULL;
  girara_setting_get(zathura->ui.session, "database", &database);

  if (g_strcmp0(database, "plain") == 0) {
214
    girara_debug("Using plain database backend.");
215
216
217
    zathura->database = zathura_plaindatabase_new(zathura->config.data_dir);
#ifdef WITH_SQLITE
  } else if (g_strcmp0(database, "sqlite") == 0) {
218
    girara_debug("Using sqlite database backend.");
219
220
221
222
    char* tmp = g_build_filename(zathura->config.data_dir, "bookmarks.sqlite", NULL);
    zathura->database = zathura_sqldatabase_new(tmp);
    g_free(tmp);
#endif
Sebastian Ramacher's avatar
Sebastian Ramacher committed
223
  } else if (g_strcmp0(database, "null") != 0) {
224
225
226
    girara_error("Database backend '%s' is not supported.", database);
  }

Sebastian Ramacher's avatar
Sebastian Ramacher committed
227
  if (zathura->database == NULL && g_strcmp0(database, "null") != 0) {
Sebastian Ramacher's avatar
Sebastian Ramacher committed
228
    girara_error("Unable to initialize database. Bookmarks won't be available.");
229
  } else {
Sebastian Ramacher's avatar
Sebastian Ramacher committed
230
    g_object_set(G_OBJECT(zathura->ui.session->command_history), "io", zathura->database, NULL);
231
  }
Sebastian Ramacher's avatar
Sebastian Ramacher committed
232
  g_free(database);
233

234
  /* bookmarks */
235
  zathura->bookmarks.bookmarks = girara_sorted_list_new2((girara_compare_function_t) zathura_bookmarks_compare,
Moritz Lipp's avatar
Moritz Lipp committed
236
                                 (girara_free_function_t) zathura_bookmark_free);
237

238
  /* jumplist */
239
240
  int jumplist_size = 20;
  girara_setting_get(zathura->ui.session, "jumplist-size", &jumplist_size);
241

242
243
  zathura->jumplist.max_size = jumplist_size < 0 ? 0 : jumplist_size;
  zathura->jumplist.list = NULL;
244
245
  zathura->jumplist.size = 0;
  zathura->jumplist.cur = NULL;
246

Sebastian Ramacher's avatar
Sebastian Ramacher committed
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
  /* CSS for index mode */
  GiraraTemplate* csstemplate = girara_session_get_template(zathura->ui.session);

  static const char* index_settings[] = {
    "index-fg",
    "index-bg",
    "index-active-fg",
    "index-active-bg"
  };

  for (size_t s = 0; s < LENGTH(index_settings); ++s) {
    girara_template_add_variable(csstemplate, index_settings[s]);

    char* tmp_value = NULL;
    GdkRGBA rgba = { 0, 0, 0, 0 };
    girara_setting_get(zathura->ui.session, index_settings[s], &tmp_value);
    if (tmp_value != NULL) {
      gdk_rgba_parse(&rgba, tmp_value);
      g_free(tmp_value);
    }

    char* color = gdk_rgba_to_string(&rgba);
    girara_template_set_variable_value(csstemplate,
        index_settings[s], color);
    g_free(color);
  }

  char* css = g_strdup_printf("%s\n%s", girara_template_get_base(csstemplate), CSS_TEMPLATE_INDEX);
  girara_template_set_base(csstemplate, css);
  g_free(css);

278
  /* Start D-Bus service */
Sebastian Ramacher's avatar
Sebastian Ramacher committed
279
280
281
282
  bool dbus = true;
  girara_setting_get(zathura->ui.session, "dbus-service", &dbus);
  if (dbus == true) {
    zathura->dbus = zathura_dbus_new(zathura);
283
284
  }

285
  return true;
Moritz Lipp's avatar
Moritz Lipp committed
286
287
288

error_free:

Moritz Lipp's avatar
Moritz Lipp committed
289
  if (zathura->ui.page_widget != NULL) {
290
    g_object_unref(zathura->ui.page_widget);
291
292
  }

293
  return false;
Moritz Lipp's avatar
Moritz Lipp committed
294
295
296
297
298
299
300
301
302
}

void
zathura_free(zathura_t* zathura)
{
  if (zathura == NULL) {
    return;
  }

Moritz Lipp's avatar
Moritz Lipp committed
303
  document_close(zathura, false);
304

305
  /* stop D-Bus */
Sebastian Ramacher's avatar
Sebastian Ramacher committed
306
307
308
  if (zathura->dbus != NULL) {
    g_object_unref(zathura->dbus);
    zathura->dbus = NULL;
309
310
  }

Moritz Lipp's avatar
Moritz Lipp committed
311
312
313
314
  if (zathura->ui.session != NULL) {
    girara_session_destroy(zathura->ui.session);
  }

315
316
317
318
319
320
  /* stdin support */
  if (zathura->stdin_support.file != NULL) {
    g_unlink(zathura->stdin_support.file);
    g_free(zathura->stdin_support.file);
  }

321
322
323
  /* bookmarks */
  girara_list_free(zathura->bookmarks.bookmarks);

324
  /* database */
Sebastian Ramacher's avatar
Sebastian Ramacher committed
325
326
327
  if (zathura->database != NULL) {
    g_object_unref(G_OBJECT(zathura->database));
  }
328

329
  /* free print settings */
Moritz Lipp's avatar
Moritz Lipp committed
330
  if (zathura->print.settings != NULL) {
331
332
333
334
335
336
    g_object_unref(zathura->print.settings);
  }

  if (zathura->print.page_setup != NULL) {
    g_object_unref(zathura->print.page_setup);
  }
337

Moritz Lipp's avatar
Moritz Lipp committed
338
  /* free registered plugins */
Moritz Lipp's avatar
Moritz Lipp committed
339
  zathura_plugin_manager_free(zathura->plugins.manager);
340
341
342
343

  /* free config variables */
  g_free(zathura->config.config_dir);
  g_free(zathura->config.data_dir);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
344
  g_free(zathura->config.cache_dir);
Moritz Lipp's avatar
Moritz Lipp committed
345

346
347
348
349
350
351
352
353
354
  /* free jumplist */
  if (zathura->jumplist.list != NULL) {
    girara_list_free(zathura->jumplist.list);
  }

  if (zathura->jumplist.cur != NULL) {
    girara_list_iterator_free(zathura->jumplist.cur);
  }

355
  g_free(zathura);
356
357
}

358
#ifdef GDK_WINDOWING_X11
359
360
361
362
363
364
365
void
zathura_set_xid(zathura_t* zathura, Window xid)
{
  g_return_if_fail(zathura != NULL);

  zathura->ui.session->gtk.embed = xid;
}
366
#endif
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384

void
zathura_set_config_dir(zathura_t* zathura, const char* dir)
{
  g_return_if_fail(zathura != NULL);

  if (dir != NULL) {
    zathura->config.config_dir = g_strdup(dir);
  } else {
    gchar* path = girara_get_xdg_path(XDG_CONFIG);
    zathura->config.config_dir = g_build_filename(path, "zathura", NULL);
    g_free(path);
  }
}

void
zathura_set_data_dir(zathura_t* zathura, const char* dir)
{
Sebastian Ramacher's avatar
Sebastian Ramacher committed
385
386
  g_return_if_fail(zathura != NULL);

387
388
389
390
391
392
393
  if (dir != NULL) {
    zathura->config.data_dir = g_strdup(dir);
  } else {
    gchar* path = girara_get_xdg_path(XDG_DATA);
    zathura->config.data_dir = g_build_filename(path, "zathura", NULL);
    g_free(path);
  }
Sebastian Ramacher's avatar
Sebastian Ramacher committed
394
}
395

Sebastian Ramacher's avatar
Sebastian Ramacher committed
396
397
398
void
zathura_set_cache_dir(zathura_t* zathura, const char* dir)
{
399
  g_return_if_fail(zathura != NULL);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
400
401
402
403
404
405
406
407

  if (dir != NULL) {
    zathura->config.cache_dir = g_strdup(dir);
  } else {
    gchar* path = girara_get_xdg_path(XDG_CACHE);
    zathura->config.cache_dir = g_build_filename(path, "zathura", NULL);
    g_free(path);
  }
408
409
410
411
412
413
414
415
416
417
418
}

void
zathura_set_plugin_dir(zathura_t* zathura, const char* dir)
{
  g_return_if_fail(zathura != NULL);
  g_return_if_fail(zathura->plugins.manager != NULL);

  if (dir != NULL) {
    girara_list_t* paths = girara_split_path_array(dir);
    GIRARA_LIST_FOREACH(paths, char*, iter, path)
Moritz Lipp's avatar
Moritz Lipp committed
419
    zathura_plugin_manager_add_dir(zathura->plugins.manager, path);
420
421
422
423
424
425
    GIRARA_LIST_FOREACH_END(paths, char*, iter, path);
    girara_list_free(paths);
  } else {
#ifdef ZATHURA_PLUGINDIR
    girara_list_t* paths = girara_split_path_array(ZATHURA_PLUGINDIR);
    GIRARA_LIST_FOREACH(paths, char*, iter, path)
Moritz Lipp's avatar
Moritz Lipp committed
426
    zathura_plugin_manager_add_dir(zathura->plugins.manager, path);
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
    GIRARA_LIST_FOREACH_END(paths, char*, iter, path);
    girara_list_free(paths);
#endif
  }

}

void
zathura_set_argv(zathura_t* zathura, char** argv)
{
  g_return_if_fail(zathura != NULL);

  zathura->global.arguments = argv;
}

442
static gchar*
443
prepare_document_open_from_stdin(zathura_t* zathura, const char* path)
444
445
446
{
  g_return_val_if_fail(zathura, NULL);

447
448
449
450
451
452
453
454
455
456
457
458
459
460
  int infileno = -1;
  if (g_strcmp0(path, "-") == 0) {
    infileno = fileno(stdin);
  } else if (g_str_has_prefix(path, "/proc/self/fd/") == true) {
    char* begin = g_strrstr(path, "/") + 1;
    gint64 temp = g_ascii_strtoll(begin, NULL, 0);
    if (temp > INT_MAX || temp < 0) {
      return NULL;
    }
    infileno = (int) temp;
  } else {
    return NULL;
  }

461
462
463
  GError* error = NULL;
  gchar* file = NULL;
  gint handle = g_file_open_tmp("zathura.stdin.XXXXXX", &file, &error);
Moritz Lipp's avatar
Moritz Lipp committed
464
  if (handle == -1) {
465
466
467
468
    if (error != NULL) {
      girara_error("Can not create temporary file: %s", error->message);
      g_error_free(error);
    }
469
470
471
    return NULL;
  }

472
473
474
  // read and dump to temporary file
  if (infileno == -1) {
    girara_error("Can not read from file descriptor.");
475
476
477
478
479
480
481
482
    close(handle);
    g_unlink(file);
    g_free(file);
    return NULL;
  }

  char buffer[BUFSIZ];
  ssize_t count = 0;
483
  while ((count = read(infileno, buffer, BUFSIZ)) > 0) {
Moritz Lipp's avatar
Moritz Lipp committed
484
    if (write(handle, buffer, count) != count) {
485
486
487
488
489
490
491
      girara_error("Can not write to temporary file: %s", file);
      close(handle);
      g_unlink(file);
      g_free(file);
      return NULL;
    }
  }
Moritz Lipp's avatar
Moritz Lipp committed
492

493
494
  close(handle);

Moritz Lipp's avatar
Moritz Lipp committed
495
  if (count != 0) {
496
    girara_error("Can not read from file descriptor.");
497
498
499
500
501
502
503
504
    g_unlink(file);
    g_free(file);
    return NULL;
  }

  return file;
}

505
static gboolean
Moritz Lipp's avatar
Update    
Moritz Lipp committed
506
507
508
509
510
document_info_open(gpointer data)
{
  zathura_document_info_t* document_info = data;
  g_return_val_if_fail(document_info != NULL, FALSE);

511
  if (document_info->zathura != NULL && document_info->path != NULL) {
512
    char* file = NULL;
513
514
515
    if (g_strcmp0(document_info->path, "-") == 0 ||
        g_str_has_prefix(document_info->path, "/proc/self/fd/") == true) {
      file = prepare_document_open_from_stdin(document_info->zathura, document_info->path);
516
517
      if (file == NULL) {
        girara_notify(document_info->zathura->ui.session, GIRARA_ERROR,
518
                      _("Could not read file from stdin and write it to a temporary file."));
519
520
      } else {
        document_info->zathura->stdin_support.file = g_strdup(file);
521
522
523
524
525
526
      }
    } else {
      file = g_strdup(document_info->path);
    }

    if (file != NULL) {
527
528
      document_open(document_info->zathura, file, document_info->password,
                    document_info->page_number);
529
      g_free(file);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
530
531
532
533
534
535
536
537
538
539
540
541

      if (document_info->mode != NULL) {
        if (g_strcmp0(document_info->mode, "presentation") == 0) {
          sc_toggle_presentation(document_info->zathura->ui.session, NULL, NULL,
                                 0);
        } else if (g_strcmp0(document_info->mode, "fullscreen") == 0) {
          sc_toggle_fullscreen(document_info->zathura->ui.session, NULL, NULL,
                               0);
        } else {
          girara_error("Unknown mode: %s", document_info->mode);
        }
      }
542
    }
Moritz Lipp's avatar
Update    
Moritz Lipp committed
543
544
  }

545
  g_free(document_info);
Moritz Lipp's avatar
Update    
Moritz Lipp committed
546
  return FALSE;
547
548
}

Moritz Lipp's avatar
Moritz Lipp committed
549
bool
550
551
document_open(zathura_t* zathura, const char* path, const char* password,
              int page_number)
Moritz Lipp's avatar
Moritz Lipp committed
552
{
553
  if (zathura == NULL || zathura->plugins.manager == NULL || path == NULL) {
554
    goto error_out;
Moritz Lipp's avatar
Moritz Lipp committed
555
556
  }

557
  gchar* file_uri = NULL;
558
559
  zathura_error_t error = ZATHURA_ERROR_OK;
  zathura_document_t* document = zathura_document_open(zathura->plugins.manager, path, password, &error);
Moritz Lipp's avatar
Moritz Lipp committed
560

Moritz Lipp's avatar
Moritz Lipp committed
561
  if (document == NULL) {
562
563
564
565
566
567
568
569
    if (error == ZATHURA_ERROR_INVALID_PASSWORD) {
      zathura_password_dialog_info_t* password_dialog_info = malloc(sizeof(zathura_password_dialog_info_t));
      if (password_dialog_info != NULL) {
        password_dialog_info->zathura = zathura;

        if (path != NULL) {
          password_dialog_info->path = g_strdup(path);
          girara_dialog(zathura->ui.session, "Enter password:", true, NULL,
Moritz Lipp's avatar
Moritz Lipp committed
570
                        (girara_callback_inputbar_activate_t) cb_password_dialog, password_dialog_info);
571
572
573
574
575
576
577
          goto error_out;
        } else {
          free(password_dialog_info);
        }
      }
      goto error_out;
    }
Moritz Lipp's avatar
Moritz Lipp committed
578
579
580
    if (error == ZATHURA_ERROR_OK ) {
      girara_notify(zathura->ui.session, GIRARA_ERROR, _("Unsupported file type. Please install the necessary plugin."));
    }
581
    goto error_out;
Moritz Lipp's avatar
Moritz Lipp committed
582
583
  }

584
585
  const char* file_path        = zathura_document_get_path(document);
  unsigned int number_of_pages = zathura_document_get_number_of_pages(document);
586

587
588
589
590
591
592
  if (number_of_pages == 0) {
    girara_notify(zathura->ui.session, GIRARA_WARNING,
        _("Document does not contain any pages"));
    goto error_free;
  }

593
  /* read history file */
594
595
596
597
598
599
600
601
602
603
604
  zathura_fileinfo_t file_info = {
    .current_page = 0,
    .page_offset = 0,
    .scale = 1,
    .rotation = 0,
    .pages_per_row = 0,
    .first_page_column = 0,
    .position_x = 0,
    .position_y = 0
  };
  const bool known_file = zathura_db_get_fileinfo(zathura->database, file_path, &file_info);
605

Moritz Lipp's avatar
Moritz Lipp committed
606
  /* set page offset */
607
  zathura_document_set_page_offset(document, file_info.page_offset);
608

609
  /* check for valid scale value */
610
611
612
  if (file_info.scale <= DBL_EPSILON) {
    file_info.scale = 1;
  }
Moritz Lipp's avatar
Moritz Lipp committed
613
614
  zathura_document_set_scale(document,
      zathura_correct_scale_value(zathura->ui.session, file_info.scale));
615
616

  /* check current page number */
617
618
619
620
621
622
  /* if it wasn't specified on the command-line, get it from file_info */
  if (page_number == ZATHURA_PAGE_NUMBER_UNSPECIFIED)
    page_number = file_info.current_page;
  if (page_number < 0)
    page_number += number_of_pages;
  if ((unsigned)page_number > number_of_pages) {
623
624
    girara_warning("document info: '%s' has an invalid page number", file_path);
    zathura_document_set_current_page_number(document, 0);
625
  } else {
626
    zathura_document_set_current_page_number(document, page_number);
627
628
629
  }

  /* check for valid rotation */
630
  if (file_info.rotation % 90 != 0) {
631
632
633
    girara_warning("document info: '%s' has an invalid rotation", file_path);
    zathura_document_set_rotation(document, 0);
  } else {
634
    zathura_document_set_rotation(document, file_info.rotation % 360);
635
636
637
638
639
640
641
642
643
644
645
  }

  /* jump to first page if setting enabled */
  bool always_first_page = false;
  girara_setting_get(zathura->ui.session, "open-first-page", &always_first_page);
  if (always_first_page == true) {
    zathura_document_set_current_page_number(document, 0);
  }

  /* apply open adjustment */
  char* adjust_open = "best-fit";
646
  if (known_file == false && girara_setting_get(zathura->ui.session, "adjust-open", &(adjust_open)) == true) {
647
648
649
650
651
652
653
654
    if (g_strcmp0(adjust_open, "best-fit") == 0) {
      zathura_document_set_adjust_mode(document, ZATHURA_ADJUST_BESTFIT);
    } else if (g_strcmp0(adjust_open, "width") == 0) {
      zathura_document_set_adjust_mode(document, ZATHURA_ADJUST_WIDTH);
    } else {
      zathura_document_set_adjust_mode(document, ZATHURA_ADJUST_NONE);
    }
    g_free(adjust_open);
655
656
  } else {
    zathura_document_set_adjust_mode(document, ZATHURA_ADJUST_NONE);
657
658
  }

659
660
661
662
663
  /* initialize bisect state */
  zathura->bisect.start = 0;
  zathura->bisect.last_jump = zathura_document_get_current_page_number(document);
  zathura->bisect.end = number_of_pages - 1;

664
  /* update statusbar */
665
666
667
668
669
  bool basename_only = false;
  girara_setting_get(zathura->ui.session, "statusbar-basename", &basename_only);
  if (basename_only == false) {
    girara_statusbar_item_set_text(zathura->ui.session, zathura->ui.statusbar.file, file_path);
  } else {
670
    girara_statusbar_item_set_text(zathura->ui.session, zathura->ui.statusbar.file, zathura_document_get_basename(document));
671
  }
672

Moritz Lipp's avatar
Moritz Lipp committed
673
  /* install file monitor */
674
  file_uri = g_filename_to_uri(file_path, NULL, NULL);
Moritz Lipp's avatar
Moritz Lipp committed
675
676
677
678
679
  if (file_uri == NULL) {
    goto error_free;
  }

  if (zathura->file_monitor.file == NULL) {
680
681
682
683
    zathura->file_monitor.file = g_file_new_for_uri(file_uri);
    if (zathura->file_monitor.file == NULL) {
      goto error_free;
    }
Moritz Lipp's avatar
Moritz Lipp committed
684
685
686
  }

  if (zathura->file_monitor.monitor == NULL) {
687
688
689
690
691
    zathura->file_monitor.monitor = g_file_monitor_file(zathura->file_monitor.file, G_FILE_MONITOR_NONE, NULL, NULL);
    if (zathura->file_monitor.monitor == NULL) {
      goto error_free;
    }
    g_signal_connect(G_OBJECT(zathura->file_monitor.monitor), "changed", G_CALLBACK(cb_file_monitor), zathura->ui.session);
Moritz Lipp's avatar
Moritz Lipp committed
692
693
694
  }

  if (zathura->file_monitor.file_path == NULL) {
695
    zathura->file_monitor.file_path = g_strdup(file_path);
696
697
698
    if (zathura->file_monitor.file_path == NULL) {
      goto error_free;
    }
Moritz Lipp's avatar
Moritz Lipp committed
699
700
  }

701
  if (password != NULL) {
702
    g_free(zathura->file_monitor.password);
703
    zathura->file_monitor.password = g_strdup(password);
Moritz Lipp's avatar
Moritz Lipp committed
704
705
706
707
708
    if (zathura->file_monitor.password == NULL) {
      goto error_free;
    }
  }

Moritz Lipp's avatar
Moritz Lipp committed
709
710
711
712
713
714
  /* create marks list */
  zathura->global.marks = girara_list_new2((girara_free_function_t) mark_free);
  if (zathura->global.marks == NULL) {
    goto error_free;
  }

Moritz Lipp's avatar
Moritz Lipp committed
715
  zathura->document = document;
Moritz Lipp's avatar
Moritz Lipp committed
716

717
718
719
720
721
722
723
724
725
  /* page cache size */
  int cache_size = 0;
  girara_setting_get(zathura->ui.session, "page-cache-size", &cache_size);
  if (cache_size <= 0) {
    girara_warning("page-cache-size is not positive, using %d instead",
        ZATHURA_PAGE_CACHE_DEFAULT_SIZE);
    cache_size = ZATHURA_PAGE_CACHE_DEFAULT_SIZE;
  }

726
  /* threads */
727
  zathura->sync.render_thread = zathura_renderer_new(cache_size);
728
729
730
731
732

  if (zathura->sync.render_thread == NULL) {
    goto error_free;
  }

Sebastian Ramacher's avatar
Sebastian Ramacher committed
733
  /* set up recolor info in ZathuraRenderer */
734
735
736
737
738
739
740
741
742
  char* recolor_dark = NULL;
  char* recolor_light = NULL;
  girara_setting_get(zathura->ui.session, "recolor-darkcolor", &recolor_dark);
  girara_setting_get(zathura->ui.session, "recolor-lightcolor", &recolor_light);
  zathura_renderer_set_recolor_colors_str(zathura->sync.render_thread,
      recolor_light, recolor_dark);
  g_free(recolor_dark);
  g_free(recolor_light);

743
744
745
746
747
  bool recolor = false;
  girara_setting_get(zathura->ui.session, "recolor", &recolor);
  zathura_renderer_enable_recolor(zathura->sync.render_thread, recolor);
  girara_setting_get(zathura->ui.session, "recolor-keephue", &recolor);
  zathura_renderer_enable_recolor_hue(zathura->sync.render_thread, recolor);
748
749
  girara_setting_get(zathura->ui.session, "recolor-reverse-video", &recolor);
  zathura_renderer_enable_recolor_reverse_video(zathura->sync.render_thread, recolor);
750

751
752
753
754
755
756
  /* get view port size */
  GtkAdjustment* hadjustment = gtk_scrolled_window_get_hadjustment(
                 GTK_SCROLLED_WINDOW(zathura->ui.session->gtk.view));
  GtkAdjustment* vadjustment = gtk_scrolled_window_get_vadjustment(
                 GTK_SCROLLED_WINDOW(zathura->ui.session->gtk.view));

757
  const unsigned int view_width = (unsigned int)floor(gtk_adjustment_get_page_size(hadjustment));
758
  zathura_document_set_viewport_width(zathura->document, view_width);
759
  const unsigned int view_height = (unsigned int)floor(gtk_adjustment_get_page_size(vadjustment));
760
761
  zathura_document_set_viewport_height(zathura->document, view_height);

762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
  /* create blank pages */
  zathura->pages = calloc(number_of_pages, sizeof(GtkWidget*));
  if (zathura->pages == NULL) {
    goto error_free;
  }

  for (unsigned int page_id = 0; page_id < number_of_pages; page_id++) {
    zathura_page_t* page = zathura_document_get_page(document, page_id);
    if (page == NULL) {
      goto error_free;
    }

    GtkWidget* page_widget = zathura_page_widget_new(zathura, page);
    if (page_widget == NULL) {
      goto error_free;
    }

akbjker's avatar
akbjker committed
779
    g_object_ref(page_widget);
780
781
    zathura->pages[page_id] = page_widget;

782
783
784
    gtk_widget_set_halign(page_widget, GTK_ALIGN_CENTER);
    gtk_widget_set_valign(page_widget, GTK_ALIGN_CENTER);

785
786
787
    g_signal_connect(G_OBJECT(page_widget), "text-selected",
        G_CALLBACK(cb_page_widget_text_selected), zathura);
    g_signal_connect(G_OBJECT(page_widget), "image-selected",
788
        G_CALLBACK(cb_page_widget_image_selected), zathura);
789
790
791
792
    g_signal_connect(G_OBJECT(page_widget), "enter-link",
        G_CALLBACK(cb_page_widget_link), (gpointer) true);
    g_signal_connect(G_OBJECT(page_widget), "leave-link",
        G_CALLBACK(cb_page_widget_link), (gpointer) false);
793
794
    g_signal_connect(G_OBJECT(page_widget), "scaled-button-release",
        G_CALLBACK(cb_page_widget_scaled_button_release), zathura);
795
796
  }

797
  /* view mode */
798
799
800
801
802
803
  unsigned int pages_per_row = 1;
  unsigned int first_page_column = 1;
  unsigned int page_padding = 1;

  girara_setting_get(zathura->ui.session, "page-padding", &page_padding);

804
805
806
807
808
  if (file_info.pages_per_row > 0) {
    pages_per_row = file_info.pages_per_row;
  } else {
    girara_setting_get(zathura->ui.session, "pages-per-row", &pages_per_row);
  }
Moritz Lipp's avatar
Moritz Lipp committed
809

810
811
812
813
814
815
  if (file_info.first_page_column > 0) {
    first_page_column = file_info.first_page_column;
  } else {
    girara_setting_get(zathura->ui.session, "first-page-column", &first_page_column);
  }

Moritz Lipp's avatar
Moritz Lipp committed
816
  girara_setting_set(zathura->ui.session, "pages-per-row", &pages_per_row);
817
  girara_setting_set(zathura->ui.session, "first-page-column", &first_page_column);
818
819
820

  page_widget_set_mode(zathura, page_padding, pages_per_row, first_page_column);
  zathura_document_set_page_layout(zathura->document, page_padding, pages_per_row, first_page_column);
Moritz Lipp's avatar
Moritz Lipp committed
821

822
  girara_set_view(zathura->ui.session, zathura->ui.page_widget);
Moritz Lipp's avatar
Moritz Lipp committed
823

Moritz Lipp's avatar
Moritz Lipp committed
824

825
  /* bookmarks */
826
827
828
  if (zathura_bookmarks_load(zathura, file_path) == false) {
    girara_warning("Failed to loads bookmarks.");
  }
829

830
831
832
833
834
  /* jumplist */
  if (zathura_jumplist_load(zathura, file_path) == false) {
    zathura->jumplist.list = girara_list_new2(g_free);
  }

Moritz Lipp's avatar
Moritz Lipp committed
835
  /* update title */
836
  basename_only = false;
837
838
839
840
  girara_setting_get(zathura->ui.session, "window-title-basename", &basename_only);
  if (basename_only == false) {
    girara_set_window_title(zathura->ui.session, file_path);
  } else {
841
    girara_set_window_title(zathura->ui.session, zathura_document_get_basename(document));
842
  }
Moritz Lipp's avatar
Moritz Lipp committed
843

844
  g_free(file_uri);
Moritz Lipp's avatar
Moritz Lipp committed
845

846
  /* adjust_view */
847
  adjust_view(zathura);
848
849
850
851
852
853
854
855
856
  for (unsigned int page_id = 0; page_id < number_of_pages; page_id++) {
    /* set widget size */
    zathura_page_t* page = zathura_document_get_page(document, page_id);
    unsigned int page_height = 0;
    unsigned int page_width  = 0;

    /* adjust_view calls render_all in some cases and render_all calls
     * gtk_widget_set_size_request. To be sure that it's really called, do it
     * here once again. */
Sebastian Ramacher's avatar
Sebastian Ramacher committed
857
858
    const double height = zathura_page_get_height(page);
    const double width = zathura_page_get_width(page);
859
860
861
862
863
864
    page_calc_height_width(zathura->document, height, width, &page_height, &page_width, true);
    gtk_widget_set_size_request(zathura->pages[page_id], page_width, page_height);

    /* show widget */
    gtk_widget_show(zathura->pages[page_id]);
  }
865

866
  /* Set page */
867
  page_set(zathura, zathura_document_get_current_page_number(document));
868
869

  /* Set position (only if restoring from history file) */
Sebastian Ramacher's avatar
CS    
Sebastian Ramacher committed
870
871
872
  if (file_info.current_page == zathura_document_get_current_page_number(document) &&
      (file_info.position_x != 0 || file_info.position_y != 0)) {
    position_set(zathura, file_info.position_x, file_info.position_y);
873
874
  }

875
876
  update_visible_pages(zathura);

Moritz Lipp's avatar
Moritz Lipp committed
877
  return true;
878
879
880

error_free:

Moritz Lipp's avatar
Moritz Lipp committed
881
882
883
884
  if (file_uri != NULL) {
    g_free(file_uri);
  }

885
886
887
888
889
  zathura_document_free(document);

error_out:

  return false;
Moritz Lipp's avatar
Moritz Lipp committed
890
891
}

892
void
893
document_open_idle(zathura_t* zathura, const char* path, const char* password,
Sebastian Ramacher's avatar
Sebastian Ramacher committed
894
                   int page_number, const char* mode)
895
896
897
898
899
{
  if (zathura == NULL || path == NULL) {
    return;
  }

900
901
902
903
  zathura_document_info_t* document_info = g_try_malloc0(sizeof(zathura_document_info_t));
  if (document_info == NULL) {
    return;
  }
904

905
906
907
908
  document_info->zathura     = zathura;
  document_info->path        = path;
  document_info->password    = password;
  document_info->page_number = page_number;
Sebastian Ramacher's avatar
Sebastian Ramacher committed
909
  document_info->mode        = mode;
910
911
912
913

  gdk_threads_add_idle(document_info_open, document_info);
}

914
915
916
917
918
919
920
921
bool
document_save(zathura_t* zathura, const char* path, bool overwrite)
{
  g_return_val_if_fail(zathura, false);
  g_return_val_if_fail(zathura->document, false);
  g_return_val_if_fail(path, false);

  gchar* file_path = girara_fix_path(path);
922
923
924
925
926
927
928
929
930
  /* use current basename if path points to a directory  */
  if (g_file_test(file_path, G_FILE_TEST_IS_DIR) == TRUE) {
    char* basename = g_path_get_basename(zathura_document_get_path(zathura->document));
    char* tmp = file_path;
    file_path = g_strconcat(file_path, "/", basename, NULL);
    g_free(tmp);
    g_free(basename);
  }

Moritz Lipp's avatar
Moritz Lipp committed
931
  if ((overwrite == false) && g_file_test(file_path, G_FILE_TEST_EXISTS)) {
932
    girara_error("File already exists: %s. Use :write! to overwrite it.", file_path);
933
    g_free(file_path);
934
935
936
    return false;
  }

937
  zathura_error_t error = zathura_document_save_as(zathura->document, file_path);
938
  g_free(file_path);
939

940
  return (error == ZATHURA_ERROR_OK) ? true : false;
941
942
}

Pavel Borzenkov's avatar
Pavel Borzenkov committed
943
static void
944
remove_page_from_table(GtkWidget* page, gpointer UNUSED(permanent))
Pavel Borzenkov's avatar
Pavel Borzenkov committed
945
{
946
  gtk_container_remove(GTK_CONTAINER(gtk_widget_get_parent(page)), page);
Pavel Borzenkov's avatar
Pavel Borzenkov committed
947
948
}

Moritz Lipp's avatar
Moritz Lipp committed
949
bool
Moritz Lipp's avatar
Moritz Lipp committed
950
document_close(zathura_t* zathura, bool keep_monitor)
Moritz Lipp's avatar
Moritz Lipp committed
951
{
Moritz Lipp's avatar
Moritz Lipp committed
952
  if (zathura == NULL || zathura->document == NULL) {
Moritz Lipp's avatar
Moritz Lipp committed
953
954
955
    return false;
  }

956
957
958
  /* stop rendering */
  zathura_renderer_stop(zathura->sync.render_thread);

Moritz Lipp's avatar
Moritz Lipp committed
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
  /* remove monitor */
  if (keep_monitor == false) {
    if (zathura->file_monitor.monitor != NULL) {
      g_file_monitor_cancel(zathura->file_monitor.monitor);
      g_object_unref(zathura->file_monitor.monitor);
      zathura->file_monitor.monitor = NULL;
    }

    if (zathura->file_monitor.file != NULL) {
      g_object_unref(zathura->file_monitor.file);
      zathura->file_monitor.file = NULL;
    }

    if (zathura->file_monitor.file_path != NULL) {
      g_free(zathura->file_monitor.file_path);
      zathura->file_monitor.file_path = NULL;
    }

    if (zathura->file_monitor.password != NULL) {
      g_free(zathura->file_monitor.password);
      zathura->file_monitor.password = NULL;
    }
  }

Moritz Lipp's avatar
Moritz Lipp committed
983
984
985
986
987
988
  /* remove marks */
  if (zathura->global.marks != NULL) {
    girara_list_free(zathura->global.marks);
    zathura->global.marks = NULL;
  }

989
990
  /* store file information */
  const char* path = zathura_document_get_path(zathura->document);
991

992
  zathura_fileinfo_t file_info = { 0, 0, 1, 0, 1, 1, 0, 0 };
993
994
995
996
997
  file_info.current_page = zathura_document_get_current_page_number(zathura->document);
  file_info.page_offset  = zathura_document_get_page_offset(zathura->document);
  file_info.scale        = zathura_document_get_scale(zathura->document);
  file_info.rotation     = zathura_document_get_rotation(zathura->document);

998
  girara_setting_get(zathura->ui.session, "pages-per-row", &(file_info.pages_per_row));
999
  girara_setting_get(zathura->ui.session, "first-page-column", &(file_info.first_page_column));
1000
1001

  /* get position */
1002
1003
  file_info.position_x = zathura_document_get_position_x(zathura->document);
  file_info.position_y = zathura_document_get_position_y(zathura->document);
1004
1005

  /* save file info */
1006
  zathura_db_set_fileinfo(zathura->database, path, &file_info);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
1007

1008
1009
1010
1011
1012
1013
1014
1015
  /* save jumplist */
  zathura_db_save_jumplist(zathura->database, path, zathura->jumplist.list);
  girara_list_iterator_free(zathura->jumplist.cur);
  zathura->jumplist.cur = NULL;
  girara_list_free(zathura->jumplist.list);
  zathura->jumplist.list = NULL;
  zathura->jumplist.size = 0;

1016
  /* release render thread */
1017
  g_object_unref(zathura->sync.render_thread);
Pavel Borzenkov's avatar
Pavel Borzenkov committed
1018
1019
  zathura->sync.render_thread = NULL;

1020
  /* remove widgets */
1021
  gtk_container_foreach(GTK_CONTAINER(zathura->ui.page_widget), remove_page_from_table, NULL);
Moritz Lipp's avatar
Moritz Lipp committed
1022
1023
1024
  for (unsigned int i = 0; i < zathura_document_get_number_of_pages(zathura->document); i++) {
    g_object_unref(zathura->pages[i]);
  }
1025
1026
  free(zathura->pages);
  zathura->pages = NULL;
Moritz Lipp's avatar
Moritz Lipp committed
1027

1028
  /* remove document */
Moritz Lipp's avatar
Moritz Lipp committed
1029
  zathura_document_free(zathura->document);
Pavel Borzenkov's avatar
Pavel Borzenkov committed
1030
1031
  zathura->document = NULL;

1032
1033
1034
1035
1036
1037
  /* remove index */
  if (zathura->ui.index != NULL) {
    g_object_ref_sink(zathura->ui.index);
    zathura->ui.index = NULL;
  }

1038
  gtk_widget_hide(zathura->ui.page_widget);
1039
1040
1041
1042

  statusbar_page_number_update(zathura);

  if (zathura->ui.session != NULL && zathura->ui.statusbar.file != NULL) {
Sebastian Ramacher's avatar
Sebastian Ramacher committed
1043
    girara_statusbar_item_set_text(zathura->ui.session, zathura->ui.statusbar.file, _("[No name]"));
1044
  }
Moritz Lipp's avatar
Moritz Lipp committed
1045

Moritz Lipp's avatar
Moritz Lipp committed
1046
1047
1048
  /* update title */
  girara_set_window_title(zathura->ui.session, "zathura");

Moritz Lipp's avatar
Moritz Lipp committed
1049
1050
1051
1052
  return true;
}