commands.c 10.6 KB
Newer Older
Moritz Lipp's avatar
Moritz Lipp committed
1 2
/* See LICENSE file for license and copyright information */

Moritz Lipp's avatar
Moritz Lipp committed
3
#include <string.h>
4
#include <glib/gi18n.h>
Moritz Lipp's avatar
Moritz Lipp committed
5

Moritz Lipp's avatar
Moritz Lipp committed
6
#include "commands.h"
7 8
#include "bookmarks.h"
#include "database.h"
Moritz Lipp's avatar
Moritz Lipp committed
9
#include "document.h"
Moritz Lipp's avatar
Moritz Lipp committed
10
#include "zathura.h"
11
#include "print.h"
12
#include "document.h"
Moritz Lipp's avatar
Moritz Lipp committed
13
#include "utils.h"
14
#include "page-widget.h"
Sebastian Ramacher's avatar
Sebastian Ramacher committed
15

Moritz Lipp's avatar
Moritz Lipp committed
16

17 18
#include <girara/session.h>
#include <girara/datastructures.h>
Sebastian Ramacher's avatar
Sebastian Ramacher committed
19
#include <girara/utils.h>
20

Moritz Lipp's avatar
Moritz Lipp committed
21
bool
22
cmd_bookmark_create(girara_session_t* session, girara_list_t* argument_list)
Moritz Lipp's avatar
Moritz Lipp committed
23
{
24 25 26 27
  g_return_val_if_fail(session != NULL, false);
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;
  if (zathura->document == NULL) {
28
    girara_notify(session, GIRARA_ERROR, _("No document opened."));
29 30 31 32 33
    return false;
  }

  const unsigned int argc = girara_list_size(argument_list);
  if (argc != 1) {
34
    girara_notify(session, GIRARA_ERROR, _("Invalid number of arguments given."));
35 36 37 38 39 40
    return false;
  }

  const char* bookmark_name = girara_list_nth(argument_list, 0);
  zathura_bookmark_t* bookmark = zathura_bookmark_get(zathura, bookmark_name);
  if (bookmark != NULL) {
41
    bookmark->page = zathura->document->current_page_number + 1;
42
    girara_notify(session, GIRARA_INFO, _("Bookmark successfuly updated: %s"), bookmark_name);
43 44 45
    return true;
  }

46
  bookmark = zathura_bookmark_add(zathura, bookmark_name, zathura->document->current_page_number + 1);
47
  if (bookmark == NULL) {
48
    girara_notify(session, GIRARA_ERROR, _("Could not create bookmark: %s"), bookmark_name);
49 50 51
    return false;
  }

52
  girara_notify(session, GIRARA_INFO, _("Bookmark successfuly created: %s"), bookmark_name);
Moritz Lipp's avatar
Moritz Lipp committed
53 54 55 56
  return true;
}

bool
Moritz Lipp's avatar
Moritz Lipp committed
57
cmd_bookmark_delete(girara_session_t* session, girara_list_t* argument_list)
Moritz Lipp's avatar
Moritz Lipp committed
58
{
59 60 61 62
  g_return_val_if_fail(session != NULL, false);
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;
  if (zathura->document == NULL) {
63
    girara_notify(session, GIRARA_ERROR, _("No document opened."));
64 65 66 67 68
    return false;
  }

  const unsigned int argc = girara_list_size(argument_list);
  if (argc != 1) {
69
    girara_notify(session, GIRARA_ERROR, _("Invalid number of arguments given."));
70 71 72 73 74
    return false;
  }

  const char* bookmark = girara_list_nth(argument_list, 0);
  if (zathura_bookmark_remove(zathura, bookmark)) {
75
    girara_notify(session, GIRARA_INFO, _("Removed bookmark: %s"), bookmark);
76
  } else {
77
    girara_notify(session, GIRARA_ERROR, _("Failed to remove bookmark: %s"), bookmark);
78 79
  }

Moritz Lipp's avatar
Moritz Lipp committed
80 81 82 83
  return true;
}

bool
Sebastian Ramacher's avatar
Sebastian Ramacher committed
84
cmd_bookmark_open(girara_session_t* session, girara_list_t* argument_list)
Moritz Lipp's avatar
Moritz Lipp committed
85
{
Sebastian Ramacher's avatar
Sebastian Ramacher committed
86 87 88 89
  g_return_val_if_fail(session != NULL, false);
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;
  if (zathura->document == NULL) {
90
    girara_notify(session, GIRARA_ERROR, _("No document opened."));
Sebastian Ramacher's avatar
Sebastian Ramacher committed
91 92 93 94 95
    return false;
  }

  const unsigned int argc = girara_list_size(argument_list);
  if (argc != 1) {
96
    girara_notify(session, GIRARA_ERROR, _("Invalid number of arguments given."));
Sebastian Ramacher's avatar
Sebastian Ramacher committed
97 98 99 100 101 102
    return false;
  }

  const char* bookmark_name = girara_list_nth(argument_list, 0);
  zathura_bookmark_t* bookmark = zathura_bookmark_get(zathura, bookmark_name);
  if (bookmark == NULL) {
103
    girara_notify(session, GIRARA_ERROR, _("No such bookmark: %s"), bookmark_name);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
104 105 106
    return false;
  }

107
  return page_set(zathura, bookmark->page - 1);
Moritz Lipp's avatar
Moritz Lipp committed
108 109 110
}

bool
111
cmd_close(girara_session_t* session, girara_list_t* UNUSED(argument_list))
Moritz Lipp's avatar
Moritz Lipp committed
112
{
113 114 115
  g_return_val_if_fail(session != NULL, false);
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;
116 117 118
  if (zathura->document == NULL) {
    return true;
  }
119

Moritz Lipp's avatar
Moritz Lipp committed
120
  document_close(zathura, false);
Moritz Lipp's avatar
Moritz Lipp committed
121

Moritz Lipp's avatar
Moritz Lipp committed
122 123 124 125
  return true;
}

bool
Moritz Lipp's avatar
Moritz Lipp committed
126
cmd_info(girara_session_t* session, girara_list_t* UNUSED(argument_list))
Moritz Lipp's avatar
Moritz Lipp committed
127
{
Moritz Lipp's avatar
Moritz Lipp committed
128 129 130 131
  g_return_val_if_fail(session != NULL, false);
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;
  if (zathura->document == NULL) {
132
    girara_notify(session, GIRARA_ERROR, _("No document opened."));
Moritz Lipp's avatar
Moritz Lipp committed
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
    return false;
  }

  struct meta_field {
    char* name;
    zathura_document_meta_t field;
  };

  struct meta_field meta_fields[] = {
    { "Title",            ZATHURA_DOCUMENT_TITLE },
    { "Author",           ZATHURA_DOCUMENT_AUTHOR },
    { "Subject",          ZATHURA_DOCUMENT_SUBJECT },
    { "Keywords",         ZATHURA_DOCUMENT_KEYWORDS },
    { "Creator",          ZATHURA_DOCUMENT_CREATOR },
    { "Producer",         ZATHURA_DOCUMENT_PRODUCER },
    { "Creation date",    ZATHURA_DOCUMENT_CREATION_DATE },
    { "Modiciation date", ZATHURA_DOCUMENT_MODIFICATION_DATE }
  };

  GString* string = g_string_new(NULL);
  if (string == NULL) {
    return true;
  }

  for (unsigned int i = 0; i < LENGTH(meta_fields); i++) {
158
    char* tmp = zathura_document_meta_get(zathura->document, meta_fields[i].field, NULL);
Moritz Lipp's avatar
Moritz Lipp committed
159 160 161 162 163 164 165 166 167 168 169
    if (tmp != NULL) {
      char* text = g_strdup_printf("<b>%s:</b> %s\n", meta_fields[i].name, tmp);
      g_string_append(string, text);

      g_free(text);
      g_free(tmp);
    }
  }

  if (strlen(string->str) > 0) {
    g_string_erase(string, strlen(string->str) - 1, 1);
170
    girara_notify(session, GIRARA_INFO, "%s", string->str);
171 172
  } else {
    girara_notify(session, GIRARA_INFO, _("No information available."));
Moritz Lipp's avatar
Moritz Lipp committed
173 174 175 176 177
  }

  g_string_free(string, TRUE);

  return false;
Moritz Lipp's avatar
Moritz Lipp committed
178 179
}

Sebastian Ramacher's avatar
Sebastian Ramacher committed
180
bool
181 182
cmd_help(girara_session_t* UNUSED(session), girara_list_t*
    UNUSED(argument_list))
Sebastian Ramacher's avatar
Sebastian Ramacher committed
183 184 185 186
{
  return true;
}

187 188 189
bool
cmd_open(girara_session_t* session, girara_list_t* argument_list)
{
Sebastian Ramacher's avatar
Sebastian Ramacher committed
190 191 192 193 194 195
  g_return_val_if_fail(session != NULL, false);
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;

  const int argc = girara_list_size(argument_list);
  if (argc > 2) {
196
    girara_notify(session, GIRARA_ERROR, _("Too many arguments."));
Sebastian Ramacher's avatar
Sebastian Ramacher committed
197
    return false;
Moritz Lipp's avatar
Moritz Lipp committed
198
  } else if (argc >= 1) {
Moritz Lipp's avatar
Moritz Lipp committed
199 200
    if (zathura->document != NULL) {
      document_close(zathura, false);
201 202
    }

Sebastian Ramacher's avatar
Sebastian Ramacher committed
203
    document_open(zathura, girara_list_nth(argument_list, 0), (argc == 2) ? girara_list_nth(argument_list, 1) :  NULL);
Moritz Lipp's avatar
Moritz Lipp committed
204
  } else {
205
    girara_notify(session, GIRARA_ERROR, _("No arguments given."));
Sebastian Ramacher's avatar
Sebastian Ramacher committed
206 207 208
    return false;
  }

209 210 211
  return true;
}

Moritz Lipp's avatar
Moritz Lipp committed
212
bool
213
cmd_print(girara_session_t* session, girara_list_t* UNUSED(argument_list))
Moritz Lipp's avatar
Moritz Lipp committed
214
{
215 216 217 218
  g_return_val_if_fail(session != NULL, false);
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;

Pavel Borzenkov's avatar
Pavel Borzenkov committed
219
  if (zathura->document == NULL) {
Sebastian Ramacher's avatar
Sebastian Ramacher committed
220
    girara_notify(session, GIRARA_ERROR, _("No document opened."));
Pavel Borzenkov's avatar
Pavel Borzenkov committed
221 222
    return false;
  }
223

Pavel Borzenkov's avatar
Pavel Borzenkov committed
224
  print((zathura_t*) session->global.data);
225

Moritz Lipp's avatar
Moritz Lipp committed
226 227 228 229
  return true;
}

bool
Moritz Lipp's avatar
Moritz Lipp committed
230
cmd_save(girara_session_t* session, girara_list_t* argument_list)
Moritz Lipp's avatar
Moritz Lipp committed
231
{
232 233 234 235
  g_return_val_if_fail(session != NULL, false);
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;

Pavel Borzenkov's avatar
Pavel Borzenkov committed
236
  if (zathura->document == NULL) {
Sebastian Ramacher's avatar
Sebastian Ramacher committed
237
    girara_notify(session, GIRARA_ERROR, _("No document opened."));
Pavel Borzenkov's avatar
Pavel Borzenkov committed
238 239
    return false;
  }
240 241

  if (girara_list_size(argument_list) == 1) {
242
    if (document_save(zathura, girara_list_nth(argument_list, 0), false) == true) {
243
      girara_notify(session, GIRARA_INFO, _("Document saved."));
244
    } else {
245
      girara_notify(session, GIRARA_INFO, _("Failed to save document."));
246
    }
Moritz Lipp's avatar
Moritz Lipp committed
247
  } else {
248
    girara_notify(session, GIRARA_ERROR, _("Invalid number of arguments."));
249 250 251 252 253 254 255 256 257 258 259 260 261
    return false;
  }

  return true;
}

bool
cmd_savef(girara_session_t* session, girara_list_t* argument_list)
{
  g_return_val_if_fail(session != NULL, false);
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;

Pavel Borzenkov's avatar
Pavel Borzenkov committed
262
  if (zathura->document == NULL) {
Sebastian Ramacher's avatar
Sebastian Ramacher committed
263
    girara_notify(session, GIRARA_ERROR, _("No document opened."));
264
    return false;
Pavel Borzenkov's avatar
Pavel Borzenkov committed
265
  }
266 267

  if (girara_list_size(argument_list) == 1) {
268
    if (document_save(zathura, girara_list_nth(argument_list, 0), true) == true) {
269
      girara_notify(session, GIRARA_INFO, _("Document saved."));
270
    } else {
271
      girara_notify(session, GIRARA_INFO, _("Failed to save document."));
272
    }
Moritz Lipp's avatar
Moritz Lipp committed
273
  } else {
274
    girara_notify(session, GIRARA_ERROR, _("Invalid number of arguments."));
275 276 277
    return false;
  }

Moritz Lipp's avatar
Moritz Lipp committed
278 279
  return true;
}
Moritz Lipp's avatar
Moritz Lipp committed
280 281

bool
Sebastian Ramacher's avatar
Sebastian Ramacher committed
282
cmd_search(girara_session_t* session, const char* input, girara_argument_t* argument)
Moritz Lipp's avatar
Moritz Lipp committed
283 284 285 286 287 288 289
{
  g_return_val_if_fail(session != NULL, false);
  g_return_val_if_fail(input != NULL, false);
  g_return_val_if_fail(argument != NULL, false);
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;

Sebastian Ramacher's avatar
Sebastian Ramacher committed
290
  if (zathura->document == NULL || strlen(input) == 0) {
Moritz Lipp's avatar
Moritz Lipp committed
291 292 293
    return false;
  }

294
  bool firsthit = true;
295 296
  zathura_plugin_error_t error = ZATHURA_PLUGIN_ERROR_OK;

Sebastian Ramacher's avatar
Sebastian Ramacher committed
297
  for (unsigned int page_id = 0; page_id < zathura->document->number_of_pages; ++page_id) {
298 299
    zathura_page_t* page = zathura->document->pages[(page_id + zathura->document->current_page_number) % zathura->document->number_of_pages];
    if (page == NULL) {
Sebastian Ramacher's avatar
Sebastian Ramacher committed
300 301 302
      continue;
    }

Moritz Lipp's avatar
Moritz Lipp committed
303
    g_object_set(page->drawing_area, "draw-links", FALSE, NULL);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
304

305
    girara_list_t* result = zathura_page_search_text(page, input, &error);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
306 307
    if (result == NULL || girara_list_size(result) == 0) {
      girara_list_free(result);
308
      g_object_set(page->drawing_area, "search-results", NULL, NULL);
309 310 311 312 313 314

      if (error == ZATHURA_PLUGIN_ERROR_NOT_IMPLEMENTED) {
        break;
      } else {
        continue;
      }
Sebastian Ramacher's avatar
Sebastian Ramacher committed
315 316
    }

317
    g_object_set(page->drawing_area, "search-results", result, NULL);
318 319 320 321
    if (firsthit == true) {
      if (page_id != 0) {
        page_set_delayed(zathura, page->number);
      }
322
      g_object_set(page->drawing_area, "search-current", 0, NULL);
323 324
      firsthit = false;
    }
Sebastian Ramacher's avatar
Sebastian Ramacher committed
325 326
  }

Moritz Lipp's avatar
Moritz Lipp committed
327 328
  return true;
}
Sebastian Ramacher's avatar
Sebastian Ramacher committed
329 330 331 332 333 334 335 336

bool
cmd_export(girara_session_t* session, girara_list_t* argument_list)
{
  g_return_val_if_fail(session != NULL, false);
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;
  if (zathura->document == NULL) {
337
    girara_notify(session, GIRARA_ERROR, _("No document opened."));
Sebastian Ramacher's avatar
Sebastian Ramacher committed
338 339 340 341 342
    return false;
  }

  const unsigned int argc = girara_list_size(argument_list);
  if (argc != 2) {
343
    girara_notify(session, GIRARA_ERROR, _("Invalid number of arguments given."));
Sebastian Ramacher's avatar
Sebastian Ramacher committed
344 345 346 347
    return false;
  }

  const char* attachment_name = girara_list_nth(argument_list, 0);
348 349 350 351 352
  const char* file_name       = girara_list_nth(argument_list, 1);

  if (file_name == NULL || attachment_name == NULL) {
    return false;
  }
Sebastian Ramacher's avatar
Sebastian Ramacher committed
353
  char* file_name2 = girara_fix_path(file_name);
354 355

  if (zathura_document_attachment_save(zathura->document, attachment_name, file_name) == false) {
356
    girara_notify(session, GIRARA_ERROR, _("Couldn't write attachment '%s' to '%s'."), attachment_name, file_name);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
357
  } else {
358
    girara_notify(session, GIRARA_INFO, _("Wrote attachment '%s' to '%s'."), attachment_name, file_name2);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
359 360 361
  }

  g_free(file_name2);
362

Sebastian Ramacher's avatar
Sebastian Ramacher committed
363 364
  return true;
}