commands.c 10.5 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);
Moritz Lipp's avatar
Moritz Lipp committed
171 172 173 174 175
  }

  g_string_free(string, TRUE);

  return false;
Moritz Lipp's avatar
Moritz Lipp committed
176 177
}

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

185 186 187
bool
cmd_open(girara_session_t* session, girara_list_t* argument_list)
{
Sebastian Ramacher's avatar
Sebastian Ramacher committed
188 189 190 191 192 193
  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) {
194
    girara_notify(session, GIRARA_ERROR, _("Too many arguments."));
Sebastian Ramacher's avatar
Sebastian Ramacher committed
195
    return false;
Moritz Lipp's avatar
Moritz Lipp committed
196
  } else if (argc >= 1) {
Moritz Lipp's avatar
Moritz Lipp committed
197 198
    if (zathura->document != NULL) {
      document_close(zathura, false);
199 200
    }

Sebastian Ramacher's avatar
Sebastian Ramacher committed
201
    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
202
  } else {
203
    girara_notify(session, GIRARA_ERROR, _("No arguments given."));
Sebastian Ramacher's avatar
Sebastian Ramacher committed
204 205 206
    return false;
  }

207 208 209
  return true;
}

Moritz Lipp's avatar
Moritz Lipp committed
210
bool
211
cmd_print(girara_session_t* session, girara_list_t* UNUSED(argument_list))
Moritz Lipp's avatar
Moritz Lipp committed
212
{
213 214 215 216
  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
217
  if (zathura->document == NULL) {
218
    girara_notify(session, GIRARA_ERROR, _("No open document."));
Pavel Borzenkov's avatar
Pavel Borzenkov committed
219 220
    return false;
  }
221

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

Moritz Lipp's avatar
Moritz Lipp committed
224 225 226 227
  return true;
}

bool
Moritz Lipp's avatar
Moritz Lipp committed
228
cmd_save(girara_session_t* session, girara_list_t* argument_list)
Moritz Lipp's avatar
Moritz Lipp committed
229
{
230 231 232 233
  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
234
  if (zathura->document == NULL) {
235
    girara_notify(session, GIRARA_ERROR, _("No open document."));
Pavel Borzenkov's avatar
Pavel Borzenkov committed
236 237
    return false;
  }
238 239

  if (girara_list_size(argument_list) == 1) {
240
    if (document_save(zathura, girara_list_nth(argument_list, 0), false) == true) {
241
      girara_notify(session, GIRARA_INFO, _("Document saved."));
242
    } else {
243
      girara_notify(session, GIRARA_INFO, _("Failed to save document."));
244
    }
Moritz Lipp's avatar
Moritz Lipp committed
245
  } else {
246
    girara_notify(session, GIRARA_ERROR, _("Invalid number of arguments."));
247 248 249 250 251 252 253 254 255 256 257 258 259
    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
260
  if (zathura->document == NULL) {
261
    girara_notify(session, GIRARA_ERROR, _("No open document."));
262
    return false;
Pavel Borzenkov's avatar
Pavel Borzenkov committed
263
  }
264 265

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

Moritz Lipp's avatar
Moritz Lipp committed
276 277
  return true;
}
Moritz Lipp's avatar
Moritz Lipp committed
278 279

bool
Sebastian Ramacher's avatar
Sebastian Ramacher committed
280
cmd_search(girara_session_t* session, const char* input, girara_argument_t* argument)
Moritz Lipp's avatar
Moritz Lipp committed
281 282 283 284 285 286 287
{
  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
288
  if (zathura->document == NULL || strlen(input) == 0) {
Moritz Lipp's avatar
Moritz Lipp committed
289 290 291
    return false;
  }

292
  bool firsthit = true;
293 294
  zathura_plugin_error_t error = ZATHURA_PLUGIN_ERROR_OK;

Sebastian Ramacher's avatar
Sebastian Ramacher committed
295
  for (unsigned int page_id = 0; page_id < zathura->document->number_of_pages; ++page_id) {
296 297
    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
298 299 300
      continue;
    }

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

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

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

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

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

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) {
335
    girara_notify(session, GIRARA_ERROR, _("No document opened."));
Sebastian Ramacher's avatar
Sebastian Ramacher committed
336 337 338 339 340
    return false;
  }

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

  const char* attachment_name = girara_list_nth(argument_list, 0);
346 347 348 349 350
  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
351
  char* file_name2 = girara_fix_path(file_name);
352 353

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

  g_free(file_name2);
360

Sebastian Ramacher's avatar
Sebastian Ramacher committed
361 362
  return true;
}