Commit 1d606ef0 authored by Moritz Lipp's avatar Moritz Lipp
Browse files

Ask for password if file is encrypted

parent 7ca0f3b2
......@@ -220,3 +220,52 @@ cb_file_monitor(GFileMonitor* monitor, GFile* file, GFile* UNUSED(other_file), G
sc_reload(session, NULL, NULL, 0);
}
bool
cb_password_dialog(GtkEntry* entry, zathura_password_dialog_info_t* dialog)
{
if (entry == NULL || dialog == NULL) {
goto error_ret;
}
if (dialog->path == NULL) {
free(dialog);
goto error_ret;
}
if (dialog->zathura == NULL) {
goto error_free;
}
char* input = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
/* no or empty password: ask again */
if (input == NULL || strlen(input) == 0) {
if (input != NULL) {
g_free(input);
}
girara_dialog(dialog->zathura->ui.session, "Enter password:", true, NULL,
(girara_callback_inputbar_activate_t) cb_password_dialog, dialog);
return false;
}
/* try to open document again */
document_open(dialog->zathura, dialog->path, input);
g_free(input);
g_free(dialog->path);
free(dialog);
return true;
error_free:
g_free(dialog->path);
free(dialog);
error_ret:
return false;
}
......@@ -78,4 +78,13 @@ bool cb_sc_follow(GtkEntry* entry, girara_session_t* session);
void cb_file_monitor(GFileMonitor* monitor, GFile* file, GFile* other_file,
GFileMonitorEvent event, girara_session_t* session);
/**
* Callback to read new password for file that should be opened
*
* @param entry The password entry
* @param dialog The dialog information
* @return true if input has been handled
*/
bool cb_password_dialog(GtkEntry* entry, zathura_password_dialog_info_t* dialog);
#endif // CALLBACKS_H
......@@ -28,6 +28,7 @@
#include <girara/datastructures.h>
#include <girara/utils.h>
#include <girara/statusbar.h>
#include <girara/session.h>
/**
* Register document plugin
......@@ -262,6 +263,36 @@ zathura_document_open(zathura_t* zathura, const char* path, const char* password
document->scale = 1.0;
document->zathura = zathura;
/* open document */
if (plugin->open_function == NULL) {
girara_error("plugin has no open function\n");
goto error_free;
}
zathura_plugin_error_t error = plugin->open_function(document);
if (error != ZATHURA_PLUGIN_ERROR_OK) {
while (error == ZATHURA_PLUGIN_ERROR_INVALID_PASSWORD) {
zathura_password_dialog_info_t* password_dialog_info = malloc(sizeof(zathura_password_dialog_info_t));
if (password_dialog_info != NULL) {
password_dialog_info->path = g_strdup(path);
password_dialog_info->zathura = zathura;
if (path != NULL) {
girara_dialog(zathura->ui.session, "Enter password:", true, NULL,
(girara_callback_inputbar_activate_t) cb_password_dialog, password_dialog_info);
goto error_free;
} else {
free(password_dialog_info);
}
}
goto error_free;
}
girara_error("could not open document\n");
goto error_free;
}
/* read history file */
int offset = 0;
zathura_db_get_fileinfo(zathura->database, document->file_path,
&document->current_page_number, &offset, &document->scale);
......@@ -276,11 +307,6 @@ zathura_document_open(zathura_t* zathura, const char* path, const char* password
document->current_page_number = 1;
}
if (plugin->open_function == NULL || plugin->open_function(document) != ZATHURA_PLUGIN_ERROR_OK) {
girara_error("could not open file\n");
goto error_free;
}
/* update statusbar */
girara_statusbar_item_set_text(zathura->ui.session, zathura->ui.statusbar.file, real_path);
......
......@@ -67,6 +67,12 @@ typedef enum zathura_document_meta_e
ZATHURA_DOCUMENT_MODIFICATION_DATE /**< Modification data */
} zathura_document_meta_t;
typedef struct zathura_password_dialog_info_s
{
char* path; /**< Path to the file */
zathura_t* zathura; /**< Zathura session */
} zathura_password_dialog_info_t;
/**
* Function prototype that is called to register a document plugin
*
......@@ -460,7 +466,7 @@ zathura_plugin_error_t zathura_page_form_fields_free(girara_list_t* list);
* @param page Page
* @param error Set to an error value (see \ref zathura_plugin_error_t) if an
* error occured
* @return List of images or NULL if an error occured
* @return List of images or NULL if an error occured
*/
girara_list_t* zathura_page_images_get(zathura_page_t* page, zathura_plugin_error_t* error);
......
......@@ -147,7 +147,7 @@ sc_follow(girara_session_t* session, girara_argument_t* UNUSED(argument),
/* ask for input */
if (show_links == true) {
girara_dialog(zathura->ui.session, "Follow link:", FALSE, NULL, (girara_callback_inputbar_activate_t) cb_sc_follow);
girara_dialog(zathura->ui.session, "Follow link:", FALSE, NULL, (girara_callback_inputbar_activate_t) cb_sc_follow, NULL);
}
return false;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment