From 82525556a31fd80149ae0b313cb17d691ecc684d Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Fri, 13 Jan 2012 18:54:09 +0100 Subject: [PATCH] implement :export --- commands.c | 31 +++++++++++++++++++++++++++++++ commands.h | 9 +++++++++ completion.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ completion.h | 9 +++++++++ config.c | 2 ++ 5 files changed, 98 insertions(+) diff --git a/commands.c b/commands.c index d431b07..26a9592 100644 --- a/commands.c +++ b/commands.c @@ -13,6 +13,7 @@ #include #include +#include bool cmd_bookmark_create(girara_session_t* session, girara_list_t* argument_list) @@ -284,3 +285,33 @@ cmd_search(girara_session_t* session, const char* input, girara_argument_t* argu return true; } + +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); + const char* file_name = girara_list_nth(argument_list, 1); + char* file_name2 = girara_fix_path(file_name); + if (!zathura_document_attachment_save(zathura->document, attachment_name, file_name)) { + 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); + return true; +} diff --git a/commands.h b/commands.h index 4318430..f1dc4aa 100644 --- a/commands.h +++ b/commands.h @@ -106,4 +106,13 @@ bool cmd_savef(girara_session_t* session, girara_list_t* argument_list); */ bool cmd_search(girara_session_t* session, const char* input, girara_argument_t* argument); +/** + * Save attachment to a file + * + * @param session The used girara session + * @param argument_list List of passed arguments + * @return true if no error occured + */ +bool cmd_export(girara_session_t* session, girara_list_t* argument_list); + #endif // COMMANDS_H diff --git a/completion.c b/completion.c index a0983cb..03a1571 100644 --- a/completion.c +++ b/completion.c @@ -217,3 +217,50 @@ error_free: return NULL; } + +girara_completion_t* +cc_export(girara_session_t* session, const char* input) +{ + if (input == NULL) { + return NULL; + } + + g_return_val_if_fail(session != NULL, NULL); + g_return_val_if_fail(session->global.data != NULL, NULL); + zathura_t* zathura = session->global.data; + + girara_completion_t* completion = girara_completion_init(); + girara_completion_group_t* group = girara_completion_group_create(session, NULL); + + if (completion == NULL || group == NULL) { + goto error_free; + } + + const size_t input_length = strlen(input); + girara_list_t* attachments = zathura_document_attachments_get(zathura->document); + if (!attachments) { + goto error_free; + } + + GIRARA_LIST_FOREACH(attachments, const char*, iter, attachment) + if (input_length <= strlen(attachment) && !strncmp(input, attachment, input_length)) { + girara_completion_group_add_element(group, attachment, NULL); + } + GIRARA_LIST_FOREACH_END(zathura->bookmarks.bookmarks, zathura_bookmark_t*, iter, bookmark); + + girara_completion_add_group(completion, group); + girara_list_free(attachments); + return completion; + +error_free: + + if (completion) { + girara_completion_free(completion); + } + + if (group) { + girara_completion_group_free(group); + } + + return NULL; +} diff --git a/completion.h b/completion.h index 6bead72..7b8f67a 100644 --- a/completion.h +++ b/completion.h @@ -24,5 +24,14 @@ girara_completion_t* cc_open(girara_session_t* session, const char* input); */ girara_completion_t* cc_bookmarks(girara_session_t* session, const char* input); +/** + * Completion for the export command - Creates a list of attachments + * + * @param session the girara session + * @param input the current input + * @return completion object, NULL on error + */ +girara_completion_t* cc_export(girara_session_t* session, const char* input); + #endif // COMPLETION_H diff --git a/config.c b/config.c index 6bde5f5..2ac7f5f 100644 --- a/config.c +++ b/config.c @@ -127,6 +127,8 @@ config_load_default(zathura_t* zathura) girara_inputbar_command_add(gsession, "print", NULL, cmd_print, NULL, "Print document"); girara_inputbar_command_add(gsession, "write", NULL, cmd_save, NULL, "Save document"); girara_inputbar_command_add(gsession, "write!", NULL, cmd_savef, NULL, "Save document (and force overwriting)"); + girara_inputbar_command_add(gsession, "export", NULL, cmd_export, cc_export, "Save attachments"); + girara_special_command_add(gsession, '/', cmd_search, true, FORWARD, NULL); girara_special_command_add(gsession, '?', cmd_search, true, BACKWARD, NULL);