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

#include "commands.h"
4 5
#include "bookmarks.h"
#include "database.h"
Moritz Lipp's avatar
Moritz Lipp committed
6
#include "zathura.h"
7
#include "print.h"
8
#include "document.h"
Moritz Lipp's avatar
Moritz Lipp committed
9 10

bool
11 12
cmd_bookmark_create(girara_session_t* UNUSED(session), girara_list_t*
    UNUSED(argument_list))
Moritz Lipp's avatar
Moritz Lipp committed
13 14 15 16 17
{
  return true;
}

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

bool
45 46
cmd_bookmark_open(girara_session_t* UNUSED(session), girara_list_t*
    UNUSED(argument_list))
Moritz Lipp's avatar
Moritz Lipp committed
47 48 49 50 51
{
  return true;
}

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

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

Moritz Lipp's avatar
Moritz Lipp committed
64 65 66 67
  return true;
}

bool
68 69
cmd_info(girara_session_t* UNUSED(session), girara_list_t*
    UNUSED(argument_list))
Moritz Lipp's avatar
Moritz Lipp committed
70 71 72 73
{
  return true;
}

Sebastian Ramacher's avatar
Sebastian Ramacher committed
74
bool
75 76
cmd_help(girara_session_t* UNUSED(session), girara_list_t*
    UNUSED(argument_list))
Sebastian Ramacher's avatar
Sebastian Ramacher committed
77 78 79 80
{
  return true;
}

81 82 83
bool
cmd_open(girara_session_t* session, girara_list_t* argument_list)
{
Sebastian Ramacher's avatar
Sebastian Ramacher committed
84 85 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;

  const int argc = girara_list_size(argument_list);
  if (argc > 2) {
90
    girara_notify(session, GIRARA_ERROR, "Too many arguments.");
Sebastian Ramacher's avatar
Sebastian Ramacher committed
91 92 93
    return false;
  }
  else if (argc >= 1) {
94 95 96 97
    if (zathura->document) {
      document_close(zathura);
    }

Sebastian Ramacher's avatar
Sebastian Ramacher committed
98 99 100
    document_open(zathura, girara_list_nth(argument_list, 0), (argc == 2) ? girara_list_nth(argument_list, 1) :  NULL);
  }
  else {
101
    girara_notify(session, GIRARA_ERROR, "No arguments given.");
Sebastian Ramacher's avatar
Sebastian Ramacher committed
102 103 104
    return false;
  }

105 106 107
  return true;
}

Moritz Lipp's avatar
Moritz Lipp committed
108
bool
109
cmd_print(girara_session_t* session, girara_list_t* UNUSED(argument_list))
Moritz Lipp's avatar
Moritz Lipp committed
110
{
111 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;

	if (zathura->document == NULL) {
116
    girara_notify(session, GIRARA_ERROR, "No open document.");
117 118 119
		return false;
	}

Sebastian Ramacher's avatar
Sebastian Ramacher committed
120
	print((zathura_t*) session->global.data);
121

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

bool
Moritz Lipp's avatar
Moritz Lipp committed
126
cmd_save(girara_session_t* session, girara_list_t* argument_list)
Moritz Lipp's avatar
Moritz Lipp committed
127
{
128 129 130 131 132
  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) {
133
		girara_notify(session, GIRARA_ERROR, "No open document.");
134 135 136 137 138 139 140
		return false;
	}

  if (girara_list_size(argument_list) == 1) {
    document_save(zathura, girara_list_nth(argument_list, 0), false);
  }
  else {
141
    girara_notify(session, GIRARA_ERROR, "Invalid number of arguments.");
142 143 144 145 146 147 148 149 150 151 152 153 154 155
    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;

	if (zathura->document == NULL) {
156 157
		girara_notify(session, GIRARA_ERROR, "No open document.");
    return false;
158 159 160 161 162 163
	}

  if (girara_list_size(argument_list) == 1) {
    document_save(zathura, girara_list_nth(argument_list, 0), true);
  }
  else {
164
    girara_notify(session, GIRARA_ERROR, "Invalid number of arguments.");
165 166 167
    return false;
  }

Moritz Lipp's avatar
Moritz Lipp committed
168 169
  return true;
}