commands.c 5.64 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"
Moritz Lipp's avatar
Moritz Lipp committed
13 14

bool
15 16
cmd_bookmark_create(girara_session_t* UNUSED(session), girara_list_t*
    UNUSED(argument_list))
Moritz Lipp's avatar
Moritz Lipp committed
17 18 19 20 21
{
  return true;
}

bool
Moritz Lipp's avatar
Moritz Lipp committed
22
cmd_bookmark_delete(girara_session_t* session, girara_list_t* argument_list)
Moritz Lipp's avatar
Moritz Lipp committed
23
{
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
  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
45 46 47 48
  return true;
}

bool
49 50
cmd_bookmark_open(girara_session_t* UNUSED(session), girara_list_t*
    UNUSED(argument_list))
Moritz Lipp's avatar
Moritz Lipp committed
51 52 53 54 55
{
  return true;
}

bool
56
cmd_close(girara_session_t* session, girara_list_t* UNUSED(argument_list))
Moritz Lipp's avatar
Moritz Lipp committed
57
{
58 59 60
  g_return_val_if_fail(session != NULL, false);
  g_return_val_if_fail(session->global.data != NULL, false);
  zathura_t* zathura = session->global.data;
61 62 63 64
  if (zathura->document == NULL) {
    // nothing needs to be done
    return true;
  }
65 66

  document_close(zathura);
Moritz Lipp's avatar
Moritz Lipp committed
67

Moritz Lipp's avatar
Moritz Lipp committed
68 69 70 71
  return true;
}

bool
Moritz Lipp's avatar
Moritz Lipp committed
72
cmd_info(girara_session_t* session, girara_list_t* UNUSED(argument_list))
Moritz Lipp's avatar
Moritz Lipp committed
73
{
Moritz Lipp's avatar
Moritz Lipp committed
74 75 76 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 113 114 115 116 117 118 119 120 121 122 123 124 125 126
  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++) {
    char* tmp = zathura_document_meta_get(zathura->document, meta_fields[i].field);
    if (tmp != NULL) {
      char* text = g_strdup_printf("<b>%s:</b> %s\n", meta_fields[i].name, tmp);
      if (text == NULL) {
        g_free(tmp);
        return true;
      }

      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);
    girara_notify(session, GIRARA_INFO, string->str);
  }

  g_string_free(string, TRUE);

  return false;
Moritz Lipp's avatar
Moritz Lipp committed
127 128
}

Sebastian Ramacher's avatar
Sebastian Ramacher committed
129
bool
130 131
cmd_help(girara_session_t* UNUSED(session), girara_list_t*
    UNUSED(argument_list))
Sebastian Ramacher's avatar
Sebastian Ramacher committed
132 133 134 135
{
  return true;
}

136 137 138
bool
cmd_open(girara_session_t* session, girara_list_t* argument_list)
{
Sebastian Ramacher's avatar
Sebastian Ramacher committed
139 140 141 142 143 144
  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) {
145
    girara_notify(session, GIRARA_ERROR, "Too many arguments.");
Sebastian Ramacher's avatar
Sebastian Ramacher committed
146 147 148
    return false;
  }
  else if (argc >= 1) {
149 150 151 152
    if (zathura->document) {
      document_close(zathura);
    }

Sebastian Ramacher's avatar
Sebastian Ramacher committed
153 154 155
    document_open(zathura, girara_list_nth(argument_list, 0), (argc == 2) ? girara_list_nth(argument_list, 1) :  NULL);
  }
  else {
156
    girara_notify(session, GIRARA_ERROR, "No arguments given.");
Sebastian Ramacher's avatar
Sebastian Ramacher committed
157 158 159
    return false;
  }

160 161 162
  return true;
}

Moritz Lipp's avatar
Moritz Lipp committed
163
bool
164
cmd_print(girara_session_t* session, girara_list_t* UNUSED(argument_list))
Moritz Lipp's avatar
Moritz Lipp committed
165
{
166 167 168 169
  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
170
  if (zathura->document == NULL) {
171
    girara_notify(session, GIRARA_ERROR, "No open document.");
Pavel Borzenkov's avatar
Pavel Borzenkov committed
172 173
    return false;
  }
174

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

Moritz Lipp's avatar
Moritz Lipp committed
177 178 179 180
  return true;
}

bool
Moritz Lipp's avatar
Moritz Lipp committed
181
cmd_save(girara_session_t* session, girara_list_t* argument_list)
Moritz Lipp's avatar
Moritz Lipp committed
182
{
183 184 185 186
  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
187 188 189 190
  if (zathura->document == NULL) {
    girara_notify(session, GIRARA_ERROR, "No open document.");
    return false;
  }
191 192 193 194 195

  if (girara_list_size(argument_list) == 1) {
    document_save(zathura, girara_list_nth(argument_list, 0), false);
  }
  else {
196
    girara_notify(session, GIRARA_ERROR, "Invalid number of arguments.");
197 198 199 200 201 202 203 204 205 206 207 208 209
    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
210 211
  if (zathura->document == NULL) {
    girara_notify(session, GIRARA_ERROR, "No open document.");
212
    return false;
Pavel Borzenkov's avatar
Pavel Borzenkov committed
213
  }
214 215 216 217 218

  if (girara_list_size(argument_list) == 1) {
    document_save(zathura, girara_list_nth(argument_list, 0), true);
  }
  else {
219
    girara_notify(session, GIRARA_ERROR, "Invalid number of arguments.");
220 221 222
    return false;
  }

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