implement :export

This commit is contained in:
Sebastian Ramacher 2012-01-13 18:54:09 +01:00
parent 82501190af
commit 82525556a3
5 changed files with 98 additions and 0 deletions

View file

@ -13,6 +13,7 @@
#include <girara/session.h>
#include <girara/datastructures.h>
#include <girara/utils.h>
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;
}

View file

@ -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

View file

@ -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;
}

View file

@ -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

View file

@ -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);