diff --git a/commands.c b/commands.c index 82ba01e..c8fba1d 100644 --- a/commands.c +++ b/commands.c @@ -1,11 +1,15 @@ /* See LICENSE file for license and copyright information */ +#include + #include "commands.h" #include "bookmarks.h" #include "database.h" +#include "document.h" #include "zathura.h" #include "print.h" #include "document.h" +#include "utils.h" bool cmd_bookmark_create(girara_session_t* UNUSED(session), girara_list_t* @@ -65,10 +69,61 @@ cmd_close(girara_session_t* session, girara_list_t* UNUSED(argument_list)) } bool -cmd_info(girara_session_t* UNUSED(session), girara_list_t* - UNUSED(argument_list)) +cmd_info(girara_session_t* session, girara_list_t* UNUSED(argument_list)) { - return true; + 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("%s: %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; } bool diff --git a/document.c b/document.c index b7955fe..32577fe 100644 --- a/document.c +++ b/document.c @@ -20,8 +20,7 @@ #include "utils.h" #include "zathura.h" #include "render.h" - -#define LENGTH(x) (sizeof(x)/sizeof((x)[0])) +#include "utils.h" void zathura_document_plugins_load(zathura_t* zathura) @@ -209,7 +208,7 @@ zathura_document_open(zathura_t* zathura, const char* path, const char* password if (plugin == NULL) { girara_error("unknown file type\n"); - free(real_path); + goto error_free; } document = g_malloc0(sizeof(zathura_document_t)); @@ -353,6 +352,21 @@ zathura_document_attachments_free(zathura_list_t* UNUSED(list)) return false; } +char* +zathura_document_meta_get(zathura_document_t* document, zathura_document_meta_t meta) +{ + if (document == NULL) { + return NULL; + } + + if (document->functions.document_meta_get == NULL) { + girara_error("%s not implemented", __FUNCTION__); + return NULL; + } + + return document->functions.document_meta_get(document, meta); +} + zathura_page_t* zathura_page_get(zathura_document_t* document, unsigned int page_id) { diff --git a/document.h b/document.h index 181199f..cdf92d3 100644 --- a/document.h +++ b/document.h @@ -33,6 +33,18 @@ typedef struct zathura_type_plugin_mapping_s zathura_document_plugin_t* plugin; } zathura_type_plugin_mapping_t; +typedef enum zathura_document_meta_e +{ + ZATHURA_DOCUMENT_TITLE, + ZATHURA_DOCUMENT_AUTHOR, + ZATHURA_DOCUMENT_SUBJECT, + ZATHURA_DOCUMENT_KEYWORDS, + ZATHURA_DOCUMENT_CREATOR, + ZATHURA_DOCUMENT_PRODUCER, + ZATHURA_DOCUMENT_CREATION_DATE, + ZATHURA_DOCUMENT_MODIFICATION_DATE +} zathura_document_meta_t; + /** * Function prototype that is called to register a document plugin * @@ -195,6 +207,11 @@ struct zathura_document_s */ zathura_list_t* (*document_attachments_get)(zathura_document_t* document); + /** + * Get document information + */ + char* (*document_meta_get)(zathura_document_t* document, zathura_document_meta_t info); + /** * Gets the page object */ @@ -307,6 +324,15 @@ zathura_list_t* zathura_document_attachments_get(zathura_document_t* document); */ bool zathura_document_attachments_free(zathura_list_t* list); +/** + * Returns a string of the requested information + * + * @param document The zathura document + * @param meta The information field + * @return String or NULL if information could not be retreived + */ +char* zathura_document_meta_get(zathura_document_t* document, zathura_document_meta_t meta); + /** * Get the page object * diff --git a/shortcuts.c b/shortcuts.c index fe01ace..820dc57 100644 --- a/shortcuts.c +++ b/shortcuts.c @@ -22,10 +22,64 @@ sc_abort(girara_session_t* session, girara_argument_t* UNUSED(argument), } bool -sc_adjust_window(girara_session_t* session, girara_argument_t* UNUSED(argument), +sc_adjust_window(girara_session_t* session, girara_argument_t* argument, unsigned int UNUSED(t)) { g_return_val_if_fail(session != NULL, false); + g_return_val_if_fail(session->global.data != NULL, false); + zathura_t* zathura = session->global.data; + g_return_val_if_fail(argument != NULL, false); + + unsigned int* pages_per_row = girara_setting_get(session, "pages-per-row"); + + if (zathura->ui.page_view == NULL || zathura->document == NULL || pages_per_row == NULL) { + goto error_ret; + } + + /* get window size */ + /* TODO: Get correct size of the view widget */ + gint width; + gint height; + gtk_window_get_size(GTK_WINDOW(session->gtk.window), &width, &height); + + /* calculate total width and max-height */ + double total_width = 0; + double max_height = 0; + + for (unsigned int page_id = 0; page_id < *pages_per_row; page_id++) { + if (page_id == zathura->document->number_of_pages) { + break; + } + + zathura_page_t* page = zathura->document->pages[page_id]; + if (page == NULL) { + goto error_free; + } + + if (page->height > max_height) { + max_height = page->height; + } + + total_width += page->width; + } + + if (argument->n == ADJUST_WIDTH) { + zathura->document->scale = width / total_width; + } else if (argument->n == ADJUST_BESTFIT) { + zathura->document->scale = height / max_height; + } else { + goto error_free; + } + + /* re-render all pages */ + render_all(zathura); + +error_free: + + /* cleanup */ + free(pages_per_row); + +error_ret: return false; } @@ -121,6 +175,26 @@ sc_reload(girara_session_t* session, girara_argument_t* UNUSED(argument), unsigned int UNUSED(t)) { 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 || zathura->document->file_path == NULL) { + return false; + } + + /* save current document path and password */ + char* path = g_strdup(zathura->document->file_path); + char* password = zathura->document->password ? g_strdup(zathura->document->password) : NULL; + + /* close current document */ + document_close(zathura); + + /* reopen document */ + document_open(zathura, path, password); + + /* clean up */ + g_free(path); + g_free(password); return false; } @@ -157,9 +231,9 @@ sc_scroll(girara_session_t* session, girara_argument_t* argument, unsigned int GtkAdjustment* adjustment = NULL; if ( (argument->n == LEFT) || (argument->n == RIGHT) ) - adjustment = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(zathura->ui.session->gtk.view)); + adjustment = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(session->gtk.view)); else - adjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(zathura->ui.session->gtk.view)); + adjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(session->gtk.view)); gdouble view_size = gtk_adjustment_get_page_size(adjustment); gdouble value = gtk_adjustment_get_value(adjustment); diff --git a/utils.h b/utils.h index 3637b58..f073476 100644 --- a/utils.h +++ b/utils.h @@ -9,6 +9,8 @@ #include "document.h" +#define LENGTH(x) (sizeof(x)/sizeof((x)[0])) + typedef struct page_offset_s { int x; diff --git a/zathura.c b/zathura.c index 9026c9c..fd47d84 100644 --- a/zathura.c +++ b/zathura.c @@ -63,7 +63,7 @@ zathura_init(int argc, char* argv[]) /* plugins */ zathura->plugins.plugins = girara_list_new(); - girara_list_set_free_function(zathura->plugins.plugins, + girara_list_set_free_function(zathura->plugins.plugins, (girara_free_function_t)zathura_document_plugin_free); zathura->plugins.path = girara_list_new(); girara_list_set_free_function(zathura->plugins.path, g_free); @@ -274,6 +274,8 @@ zathura_free(zathura_t* zathura) /* free config variables */ g_free(zathura->config.config_dir); g_free(zathura->config.data_dir); + + free(zathura); } gboolean