commands.c 10.1 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 4
#include <string.h>

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

Moritz Lipp's avatar
Moritz Lipp committed
15

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

Moritz Lipp's avatar
Moritz Lipp committed
20
bool
21
cmd_bookmark_create(girara_session_t* session, girara_list_t* argument_list)
Moritz Lipp's avatar
Moritz Lipp committed
22
{
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
  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) {
    girara_notify(session, GIRARA_ERROR, "No document opened.");
    return false;
  }

  const unsigned int argc = girara_list_size(argument_list);
  if (argc != 1) {
    girara_notify(session, GIRARA_ERROR, "Invalid number of arguments given.");
    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) {
40
    bookmark->page = zathura->document->current_page_number + 1;
41 42 43 44
    girara_notify(session, GIRARA_INFO, "Bookmark successfuly updated: %s", bookmark_name);
    return true;
  }

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

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

bool
Moritz Lipp's avatar
Moritz Lipp committed
56
cmd_bookmark_delete(girara_session_t* session, girara_list_t* argument_list)
Moritz Lipp's avatar
Moritz Lipp committed
57
{
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  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) {
    girara_notify(session, GIRARA_ERROR, "No document opened.");
    return false;
  }

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

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

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

bool
Sebastian Ramacher's avatar
Sebastian Ramacher committed
83
cmd_bookmark_open(girara_session_t* session, girara_list_t* argument_list)
Moritz Lipp's avatar
Moritz Lipp committed
84
{
Sebastian Ramacher's avatar
Sebastian Ramacher committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  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) {
    girara_notify(session, GIRARA_ERROR, "No document opened.");
    return false;
  }

  const unsigned int argc = girara_list_size(argument_list);
  if (argc != 1) {
    girara_notify(session, GIRARA_ERROR, "Invalid number of arguments given.");
    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) {
    girara_notify(session, GIRARA_ERROR, "No such bookmark: %s", bookmark_name);
    return false;
  }

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

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

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

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

bool
Moritz Lipp's avatar
Moritz Lipp committed
125
cmd_info(girara_session_t* session, girara_list_t* UNUSED(argument_list))
Moritz Lipp's avatar
Moritz Lipp committed
126
{
Moritz Lipp's avatar
Moritz Lipp committed
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
  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) {
    girara_notify(session, GIRARA_ERROR, "No document opened.");
    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++) {
157
    char* tmp = zathura_document_meta_get(zathura->document, meta_fields[i].field, NULL);
Moritz Lipp's avatar
Moritz Lipp committed
158 159 160 161 162 163 164 165 166 167 168
    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);
169
    girara_notify(session, GIRARA_INFO, "%s", string->str);
Moritz Lipp's avatar
Moritz Lipp committed
170 171 172 173 174
  }

  g_string_free(string, TRUE);

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

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

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

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

206 207 208
  return true;
}

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

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

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

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

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

  if (girara_list_size(argument_list) == 1) {
    document_save(zathura, girara_list_nth(argument_list, 0), true);
Moritz Lipp's avatar
Moritz Lipp committed
262
  } else {
263
    girara_notify(session, GIRARA_ERROR, "Invalid number of arguments.");
264 265 266
    return false;
  }

Moritz Lipp's avatar
Moritz Lipp committed
267 268
  return true;
}
Moritz Lipp's avatar
Moritz Lipp committed
269 270

bool
Sebastian Ramacher's avatar
Sebastian Ramacher committed
271
cmd_search(girara_session_t* session, const char* input, girara_argument_t* argument)
Moritz Lipp's avatar
Moritz Lipp committed
272 273 274 275 276 277 278
{
  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
279
  if (zathura->document == NULL || strlen(input) == 0) {
Moritz Lipp's avatar
Moritz Lipp committed
280 281 282
    return false;
  }

283
  bool firsthit = true;
284 285
  zathura_plugin_error_t error = ZATHURA_PLUGIN_ERROR_OK;

Sebastian Ramacher's avatar
Sebastian Ramacher committed
286
  for (unsigned int page_id = 0; page_id < zathura->document->number_of_pages; ++page_id) {
287 288
    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
289 290 291
      continue;
    }

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

294
    girara_list_t* result = zathura_page_search_text(page, input, &error);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
295 296
    if (result == NULL || girara_list_size(result) == 0) {
      girara_list_free(result);
297
      g_object_set(page->drawing_area, "search-results", NULL, NULL);
298 299 300 301 302 303

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

306
    g_object_set(page->drawing_area, "search-results", result, NULL);
307 308 309 310
    if (firsthit == true) {
      if (page_id != 0) {
        page_set_delayed(zathura, page->number);
      }
311
      g_object_set(page->drawing_area, "search-current", 0, NULL);
312 313
      firsthit = false;
    }
Sebastian Ramacher's avatar
Sebastian Ramacher committed
314 315
  }

Moritz Lipp's avatar
Moritz Lipp committed
316 317
  return true;
}
Sebastian Ramacher's avatar
Sebastian Ramacher committed
318 319 320 321 322 323 324 325 326 327 328 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) {
    girara_notify(session, GIRARA_ERROR, "No document opened.");
    return false;
  }

  const unsigned int argc = girara_list_size(argument_list);
  if (argc != 2) {
    girara_notify(session, GIRARA_ERROR, "Invalid number of arguments given.");
    return false;
  }

  const char* attachment_name = girara_list_nth(argument_list, 0);
337 338 339 340 341
  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
342
  char* file_name2 = girara_fix_path(file_name);
343 344

  if (zathura_document_attachment_save(zathura->document, attachment_name, file_name) == false) {
Sebastian Ramacher's avatar
Sebastian Ramacher committed
345 346 347 348 349 350
    girara_notify(session, GIRARA_ERROR, "Couldn't write attachment '%s' to '%s'.", attachment_name, file_name);
  } else {
    girara_notify(session, GIRARA_INFO, "Wrote attachment '%s' to '%s'.", attachment_name, file_name2);
  }

  g_free(file_name2);
351

Sebastian Ramacher's avatar
Sebastian Ramacher committed
352 353
  return true;
}