From b6f1868e7f8032c7086f1d0996ccfb2ded13731d Mon Sep 17 00:00:00 2001 From: Moritz Lipp Date: Mon, 20 Feb 2012 20:07:24 +0100 Subject: [PATCH 1/2] Fix automatic file reloading --- callbacks.c | 11 +++++---- commands.c | 6 ++--- document.c | 26 --------------------- document.h | 8 ------- shortcuts.c | 14 +++--------- zathura.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++--- zathura.h | 13 ++++++++++- 7 files changed, 88 insertions(+), 56 deletions(-) diff --git a/callbacks.c b/callbacks.c index 984c902..e3784cb 100644 --- a/callbacks.c +++ b/callbacks.c @@ -221,11 +221,14 @@ cb_file_monitor(GFileMonitor* monitor, GFile* file, GFile* UNUSED(other_file), G g_return_if_fail(file != NULL); g_return_if_fail(session != NULL); - if (event != G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT) { - return; + switch (event) { + case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT: + case G_FILE_MONITOR_EVENT_CREATED: + sc_reload(session, NULL, NULL, 0); + break; + default: + return; } - - sc_reload(session, NULL, NULL, 0); } static gboolean diff --git a/commands.c b/commands.c index 5796e64..fe0bae6 100644 --- a/commands.c +++ b/commands.c @@ -116,7 +116,7 @@ cmd_close(girara_session_t* session, girara_list_t* UNUSED(argument_list)) return true; } - document_close(zathura); + document_close(zathura, false); return true; } @@ -198,8 +198,8 @@ cmd_open(girara_session_t* session, girara_list_t* argument_list) girara_notify(session, GIRARA_ERROR, "Too many arguments."); return false; } else if (argc >= 1) { - if (zathura->document) { - document_close(zathura); + if (zathura->document != NULL) { + document_close(zathura, false); } document_open(zathura, girara_list_nth(argument_list, 0), (argc == 2) ? girara_list_nth(argument_list, 1) : NULL); diff --git a/document.c b/document.c index 857070f..fbaef7c 100644 --- a/document.c +++ b/document.c @@ -216,8 +216,6 @@ zathura_document_open(zathura_t* zathura, const char* path, const char* password return NULL; } - char* file_uri = NULL; - /* determine real path */ long path_max; #ifdef PATH_MAX @@ -328,24 +326,6 @@ zathura_document_open(zathura_t* zathura, const char* path, const char* password document->pages[page_id] = page; } - /* install file monitor */ - file_uri = g_filename_to_uri(real_path, NULL, NULL); - if (file_uri == NULL) { - goto error_free; - } - - document->file_monitor.file = g_file_new_for_uri(file_uri); - if (document->file_monitor.file == NULL) { - goto error_free; - } - - document->file_monitor.monitor = g_file_monitor_file(document->file_monitor.file, G_FILE_MONITOR_NONE, NULL, NULL); - if (document->file_monitor.monitor == NULL) { - goto error_free; - } - - g_signal_connect(G_OBJECT(document->file_monitor.monitor), "changed", G_CALLBACK(cb_file_monitor), zathura->ui.session); - /* apply open adjustment */ char* adjust_open = "best-fit"; document->adjust_mode = ADJUST_BESTFIT; @@ -360,16 +340,10 @@ zathura_document_open(zathura_t* zathura, const char* path, const char* password g_free(adjust_open); } - g_free(file_uri); - return document; error_free: - if (file_uri != NULL) { - g_free(file_uri); - } - free(real_path); if (document != NULL && document->pages != NULL) { diff --git a/document.h b/document.h index 604470b..d17fa42 100644 --- a/document.h +++ b/document.h @@ -300,14 +300,6 @@ struct zathura_document_s * Document pages */ zathura_page_t** pages; - - /** - * File monitor - */ - struct { - GFileMonitor* monitor; /**< File monitor */ - GFile* file; /**< File for file monitor */ - } file_monitor; }; /** diff --git a/shortcuts.c b/shortcuts.c index d71b3de..09167fc 100644 --- a/shortcuts.c +++ b/shortcuts.c @@ -375,23 +375,15 @@ sc_reload(girara_session_t* session, girara_argument_t* UNUSED(argument), 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) { + if (zathura->file_monitor.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); + document_close(zathura, true); /* reopen document */ - document_open(zathura, path, password); - - /* clean up */ - g_free(path); - g_free(password); + document_open(zathura, zathura->file_monitor.file_path, zathura->file_monitor.password); return false; } diff --git a/zathura.c b/zathura.c index 76d4653..eb4a558 100644 --- a/zathura.c +++ b/zathura.c @@ -273,7 +273,7 @@ zathura_free(zathura_t* zathura) return; } - document_close(zathura); + document_close(zathura, false); if (zathura->ui.session != NULL) { girara_session_destroy(zathura->ui.session); @@ -408,6 +408,36 @@ document_open(zathura_t* zathura, const char* path, const char* password) goto error_out; } + /* install file monitor */ + gchar* file_uri = g_filename_to_uri(document->file_path, NULL, NULL); + if (file_uri == NULL) { + goto error_free; + } + + zathura->file_monitor.file = g_file_new_for_uri(file_uri); + if (zathura->file_monitor.file == NULL) { + goto error_free; + } + + zathura->file_monitor.monitor = g_file_monitor_file(zathura->file_monitor.file, G_FILE_MONITOR_NONE, NULL, NULL); + if (zathura->file_monitor.monitor == NULL) { + goto error_free; + } + + g_signal_connect(G_OBJECT(zathura->file_monitor.monitor), "changed", G_CALLBACK(cb_file_monitor), zathura->ui.session); + + zathura->file_monitor.file_path = g_strdup(document->file_path); + if (zathura->file_monitor.file_path == NULL) { + goto error_free; + } + + if (document->password != NULL) { + zathura->file_monitor.password = g_strdup(document->password); + if (zathura->file_monitor.password == NULL) { + goto error_free; + } + } + zathura->document = document; /* view mode */ @@ -436,10 +466,16 @@ document_open(zathura_t* zathura, const char* path, const char* password) page_set_delayed(zathura, document->current_page_number - 1); cb_view_vadjustment_value_changed(NULL, zathura); + free(file_uri); + return true; error_free: + if (file_uri != NULL) { + g_free(file_uri); + } + zathura_document_free(document); error_out: @@ -478,12 +514,36 @@ remove_page_from_table(GtkWidget* page, gpointer permanent) } bool -document_close(zathura_t* zathura) +document_close(zathura_t* zathura, bool keep_monitor) { - if (zathura->document == NULL) { + if (zathura == NULL || zathura->document == NULL) { return false; } + /* remove monitor */ + if (keep_monitor == false) { + if (zathura->file_monitor.monitor != NULL) { + g_file_monitor_cancel(zathura->file_monitor.monitor); + g_object_unref(zathura->file_monitor.monitor); + zathura->file_monitor.monitor = NULL; + } + + if (zathura->file_monitor.file != NULL) { + g_object_unref(zathura->file_monitor.file); + zathura->file_monitor.file = NULL; + } + + if (zathura->file_monitor.file_path != NULL) { + g_free(zathura->file_monitor.file_path); + zathura->file_monitor.file_path = NULL; + } + + if (zathura->file_monitor.password != NULL) { + g_free(zathura->file_monitor.password); + zathura->file_monitor.password = NULL; + } + } + /* store last seen page */ zathura_db_set_fileinfo(zathura->database, zathura->document->file_path, zathura->document->current_page_number, /* zathura->document->offset TODO */ 0, zathura->document->scale, diff --git a/zathura.h b/zathura.h index 2c27467..b2d14d3 100644 --- a/zathura.h +++ b/zathura.h @@ -109,6 +109,16 @@ typedef struct zathura_s zathura_document_t* document; /**< The current document */ zathura_database_t* database; /**< The database */ + + /** + * File monitor + */ + struct { + GFileMonitor* monitor; /**< File monitor */ + GFile* file; /**< File for file monitor */ + gchar* file_path; /**< Save file path */ + gchar* password; /**< Save password */ + } file_monitor; } zathura_t; /** @@ -153,9 +163,10 @@ bool document_save(zathura_t* zathura, const char* path, bool overwrite); * Closes the current opened document * * @param zathura The zathura session + * @param keep_monitor Set to true if monitor should be kept (sc_reload) * @return If no error occured true, otherwise false, is returned. */ -bool document_close(zathura_t* zathura); +bool document_close(zathura_t* zathura, bool keep_monitor); /** * Opens the page with the given number From 8219d48ebaae8b40009a1a2c88260a69b01e600a Mon Sep 17 00:00:00 2001 From: Moritz Lipp Date: Mon, 20 Feb 2012 20:13:53 +0100 Subject: [PATCH 2/2] Reload correct page when re-opening known file --- zathura.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zathura.c b/zathura.c index eb4a558..a31e7f7 100644 --- a/zathura.c +++ b/zathura.c @@ -463,7 +463,7 @@ document_open(zathura_t* zathura, const char* path, const char* password) /* bookmarks */ zathura_bookmarks_load(zathura, zathura->document->file_path); - page_set_delayed(zathura, document->current_page_number - 1); + page_set_delayed(zathura, document->current_page_number); cb_view_vadjustment_value_changed(NULL, zathura); free(file_uri);