2010-11-10 20:31:15 +01:00
|
|
|
/* See LICENSE file for license and copyright information */
|
|
|
|
|
2011-10-01 23:29:40 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2010-11-10 20:31:15 +01:00
|
|
|
#include "commands.h"
|
2011-09-03 13:40:28 +02:00
|
|
|
#include "bookmarks.h"
|
|
|
|
#include "database.h"
|
2011-10-01 23:29:40 +02:00
|
|
|
#include "document.h"
|
2011-03-12 11:32:24 +01:00
|
|
|
#include "zathura.h"
|
2011-04-29 00:28:19 +02:00
|
|
|
#include "print.h"
|
2011-09-03 13:40:28 +02:00
|
|
|
#include "document.h"
|
2011-10-01 23:29:40 +02:00
|
|
|
#include "utils.h"
|
2012-02-07 18:30:46 +01:00
|
|
|
#include "page_widget.h"
|
2012-02-07 12:08:00 +01:00
|
|
|
|
2010-11-10 20:31:15 +01:00
|
|
|
|
2011-10-23 17:01:15 +02:00
|
|
|
#include <girara/session.h>
|
|
|
|
#include <girara/datastructures.h>
|
2012-01-13 18:54:09 +01:00
|
|
|
#include <girara/utils.h>
|
2011-10-23 17:01:15 +02:00
|
|
|
|
2010-11-10 20:31:15 +01:00
|
|
|
bool
|
2011-10-06 18:07:02 +02:00
|
|
|
cmd_bookmark_create(girara_session_t* session, girara_list_t* argument_list)
|
2010-11-10 20:31:15 +01:00
|
|
|
{
|
2011-10-06 18:07:02 +02:00
|
|
|
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) {
|
2011-10-06 18:33:54 +02:00
|
|
|
bookmark->page = zathura->document->current_page_number + 1;
|
2011-10-06 18:07:02 +02:00
|
|
|
girara_notify(session, GIRARA_INFO, "Bookmark successfuly updated: %s", bookmark_name);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-10-06 18:33:54 +02:00
|
|
|
bookmark = zathura_bookmark_add(zathura, bookmark_name, zathura->document->current_page_number + 1);
|
2011-10-06 18:07:02 +02:00
|
|
|
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);
|
2010-11-10 20:31:15 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2011-04-19 10:38:59 +02:00
|
|
|
cmd_bookmark_delete(girara_session_t* session, girara_list_t* argument_list)
|
2010-11-10 20:31:15 +01:00
|
|
|
{
|
2011-09-03 13:40:28 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2010-11-10 20:31:15 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2011-10-06 18:01:15 +02:00
|
|
|
cmd_bookmark_open(girara_session_t* session, girara_list_t* argument_list)
|
2010-11-10 20:31:15 +01:00
|
|
|
{
|
2011-10-06 18:01:15 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-10-06 18:33:54 +02:00
|
|
|
return page_set(zathura, bookmark->page - 1);
|
2010-11-10 20:31:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2011-09-21 00:46:03 +02:00
|
|
|
cmd_close(girara_session_t* session, girara_list_t* UNUSED(argument_list))
|
2010-11-10 20:31:15 +01:00
|
|
|
{
|
2011-04-18 18:29:50 +02:00
|
|
|
g_return_val_if_fail(session != NULL, false);
|
|
|
|
g_return_val_if_fail(session->global.data != NULL, false);
|
|
|
|
zathura_t* zathura = session->global.data;
|
2011-08-30 23:40:13 +02:00
|
|
|
if (zathura->document == NULL) {
|
|
|
|
return true;
|
|
|
|
}
|
2011-04-18 18:29:50 +02:00
|
|
|
|
2012-02-20 20:07:24 +01:00
|
|
|
document_close(zathura, false);
|
2011-03-12 11:32:24 +01:00
|
|
|
|
2010-11-10 20:31:15 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2011-10-01 23:29:40 +02:00
|
|
|
cmd_info(girara_session_t* session, girara_list_t* UNUSED(argument_list))
|
2010-11-10 20:31:15 +01:00
|
|
|
{
|
2011-10-01 23:29:40 +02:00
|
|
|
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++) {
|
2012-02-07 21:01:54 +01:00
|
|
|
char* tmp = zathura_document_meta_get(zathura->document, meta_fields[i].field, NULL);
|
2011-10-01 23:29:40 +02:00
|
|
|
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);
|
2011-11-19 17:14:02 +01:00
|
|
|
girara_notify(session, GIRARA_INFO, "%s", string->str);
|
2011-10-01 23:29:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
g_string_free(string, TRUE);
|
|
|
|
|
|
|
|
return false;
|
2010-11-10 20:31:15 +01:00
|
|
|
}
|
|
|
|
|
2011-09-01 13:15:27 +02:00
|
|
|
bool
|
2011-09-21 00:46:03 +02:00
|
|
|
cmd_help(girara_session_t* UNUSED(session), girara_list_t*
|
|
|
|
UNUSED(argument_list))
|
2011-09-01 13:15:27 +02:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-05-25 00:24:43 +02:00
|
|
|
bool
|
|
|
|
cmd_open(girara_session_t* session, girara_list_t* argument_list)
|
|
|
|
{
|
2011-08-31 00:08:33 +02:00
|
|
|
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) {
|
2011-09-03 13:40:28 +02:00
|
|
|
girara_notify(session, GIRARA_ERROR, "Too many arguments.");
|
2011-08-31 00:08:33 +02:00
|
|
|
return false;
|
2011-10-12 16:18:40 +02:00
|
|
|
} else if (argc >= 1) {
|
2012-02-20 20:07:24 +01:00
|
|
|
if (zathura->document != NULL) {
|
|
|
|
document_close(zathura, false);
|
2011-09-03 13:40:28 +02:00
|
|
|
}
|
|
|
|
|
2011-08-31 00:08:33 +02:00
|
|
|
document_open(zathura, girara_list_nth(argument_list, 0), (argc == 2) ? girara_list_nth(argument_list, 1) : NULL);
|
2011-10-12 16:18:40 +02:00
|
|
|
} else {
|
2011-09-03 13:40:28 +02:00
|
|
|
girara_notify(session, GIRARA_ERROR, "No arguments given.");
|
2011-08-31 00:08:33 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-05-25 00:24:43 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-11-10 20:31:15 +01:00
|
|
|
bool
|
2011-09-21 00:46:03 +02:00
|
|
|
cmd_print(girara_session_t* session, girara_list_t* UNUSED(argument_list))
|
2010-11-10 20:31:15 +01:00
|
|
|
{
|
2011-04-29 00:28:19 +02:00
|
|
|
g_return_val_if_fail(session != NULL, false);
|
|
|
|
g_return_val_if_fail(session->global.data != NULL, false);
|
|
|
|
zathura_t* zathura = session->global.data;
|
|
|
|
|
2011-09-21 09:22:12 +02:00
|
|
|
if (zathura->document == NULL) {
|
2011-09-03 13:40:28 +02:00
|
|
|
girara_notify(session, GIRARA_ERROR, "No open document.");
|
2011-09-21 09:22:12 +02:00
|
|
|
return false;
|
|
|
|
}
|
2011-04-29 00:28:19 +02:00
|
|
|
|
2011-09-21 09:22:12 +02:00
|
|
|
print((zathura_t*) session->global.data);
|
2011-04-29 00:28:19 +02:00
|
|
|
|
2010-11-10 20:31:15 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2011-04-19 10:38:59 +02:00
|
|
|
cmd_save(girara_session_t* session, girara_list_t* argument_list)
|
2010-11-10 20:31:15 +01:00
|
|
|
{
|
2011-09-01 11:51:49 +02:00
|
|
|
g_return_val_if_fail(session != NULL, false);
|
|
|
|
g_return_val_if_fail(session->global.data != NULL, false);
|
|
|
|
zathura_t* zathura = session->global.data;
|
|
|
|
|
2011-09-21 09:22:12 +02:00
|
|
|
if (zathura->document == NULL) {
|
|
|
|
girara_notify(session, GIRARA_ERROR, "No open document.");
|
|
|
|
return false;
|
|
|
|
}
|
2011-09-01 11:51:49 +02:00
|
|
|
|
|
|
|
if (girara_list_size(argument_list) == 1) {
|
|
|
|
document_save(zathura, girara_list_nth(argument_list, 0), false);
|
2011-10-12 16:18:40 +02:00
|
|
|
} else {
|
2011-09-03 13:40:28 +02:00
|
|
|
girara_notify(session, GIRARA_ERROR, "Invalid number of arguments.");
|
2011-09-01 11:51:49 +02:00
|
|
|
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;
|
|
|
|
|
2011-09-21 09:22:12 +02:00
|
|
|
if (zathura->document == NULL) {
|
|
|
|
girara_notify(session, GIRARA_ERROR, "No open document.");
|
2011-09-03 13:40:28 +02:00
|
|
|
return false;
|
2011-09-21 09:22:12 +02:00
|
|
|
}
|
2011-09-01 11:51:49 +02:00
|
|
|
|
|
|
|
if (girara_list_size(argument_list) == 1) {
|
|
|
|
document_save(zathura, girara_list_nth(argument_list, 0), true);
|
2011-10-12 16:18:40 +02:00
|
|
|
} else {
|
2011-09-03 13:40:28 +02:00
|
|
|
girara_notify(session, GIRARA_ERROR, "Invalid number of arguments.");
|
2011-09-01 11:51:49 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-11-10 20:31:15 +01:00
|
|
|
return true;
|
|
|
|
}
|
2011-10-22 16:35:38 +02:00
|
|
|
|
|
|
|
bool
|
2011-12-09 14:48:16 +01:00
|
|
|
cmd_search(girara_session_t* session, const char* input, girara_argument_t* argument)
|
2011-10-22 16:35:38 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2012-02-07 12:08:00 +01:00
|
|
|
if (zathura->document == NULL || strlen(input) == 0) {
|
2011-10-22 16:35:38 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-02-07 18:19:48 +01:00
|
|
|
bool firsthit = true;
|
2012-02-18 09:21:34 +01:00
|
|
|
zathura_plugin_error_t error = ZATHURA_PLUGIN_ERROR_OK;
|
|
|
|
|
2012-02-07 12:08:00 +01:00
|
|
|
for (unsigned int page_id = 0; page_id < zathura->document->number_of_pages; ++page_id) {
|
2012-02-07 18:19:48 +01:00
|
|
|
zathura_page_t* page = zathura->document->pages[(page_id + zathura->document->current_page_number) % zathura->document->number_of_pages];
|
|
|
|
if (page == NULL) {
|
2012-02-07 12:08:00 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-02-07 16:39:02 +01:00
|
|
|
g_object_set(page->drawing_area, "draw-links", FALSE, NULL);
|
2012-02-07 12:08:00 +01:00
|
|
|
|
2012-02-18 09:21:34 +01:00
|
|
|
girara_list_t* result = zathura_page_search_text(page, input, &error);
|
2012-02-07 12:08:00 +01:00
|
|
|
if (result == NULL || girara_list_size(result) == 0) {
|
|
|
|
girara_list_free(result);
|
2012-02-07 16:59:42 +01:00
|
|
|
g_object_set(page->drawing_area, "search-results", NULL, NULL);
|
2012-02-18 09:21:34 +01:00
|
|
|
|
|
|
|
if (error == ZATHURA_PLUGIN_ERROR_NOT_IMPLEMENTED) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
2012-02-07 12:08:00 +01:00
|
|
|
}
|
|
|
|
|
2012-02-07 16:59:42 +01:00
|
|
|
g_object_set(page->drawing_area, "search-results", result, NULL);
|
2012-02-07 18:19:48 +01:00
|
|
|
if (firsthit == true) {
|
|
|
|
if (page_id != 0) {
|
|
|
|
page_set_delayed(zathura, page->number);
|
|
|
|
}
|
2012-02-07 19:15:01 +01:00
|
|
|
g_object_set(page->drawing_area, "search-current", 0, NULL);
|
2012-02-07 18:19:48 +01:00
|
|
|
firsthit = false;
|
|
|
|
}
|
2012-02-07 12:08:00 +01:00
|
|
|
}
|
|
|
|
|
2011-10-22 16:35:38 +02:00
|
|
|
return true;
|
|
|
|
}
|
2012-01-13 18:54:09 +01:00
|
|
|
|
|
|
|
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);
|
2012-02-11 17:53:28 +01:00
|
|
|
const char* file_name = girara_list_nth(argument_list, 1);
|
|
|
|
|
|
|
|
if (file_name == NULL || attachment_name == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-01-13 18:54:09 +01:00
|
|
|
char* file_name2 = girara_fix_path(file_name);
|
2012-02-11 17:53:28 +01:00
|
|
|
|
|
|
|
if (zathura_document_attachment_save(zathura->document, attachment_name, file_name) == false) {
|
2012-01-13 18:54:09 +01:00
|
|
|
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);
|
2012-02-11 17:53:28 +01:00
|
|
|
|
2012-01-13 18:54:09 +01:00
|
|
|
return true;
|
|
|
|
}
|