diff --git a/callbacks.c b/callbacks.c index 0cc76a2..67dc552 100644 --- a/callbacks.c +++ b/callbacks.c @@ -211,3 +211,17 @@ error_ret: return FALSE; } + +void +cb_file_monitor(GFileMonitor* monitor, GFile* file, GFile* UNUSED(other_file), GFileMonitorEvent event, girara_session_t* session) +{ + g_return_if_fail(monitor != NULL); + g_return_if_fail(file != NULL); + g_return_if_fail(session != NULL); + + if (event != G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT) { + return; + } + + sc_reload(session, NULL, NULL, 0); +} diff --git a/callbacks.h b/callbacks.h index afb9d73..4fb9842 100644 --- a/callbacks.h +++ b/callbacks.h @@ -7,6 +7,8 @@ #include #include +#include "document.h" + /** * Quits the current zathura session * @@ -60,4 +62,15 @@ void cb_index_row_activated(GtkTreeView* tree_view, GtkTreePath* path, */ bool cb_sc_follow(GtkEntry* entry, girara_session_t* session); +/** + * Emitted when file has been changed + * + * @param monitor The file monitor + * @param file The file + * @param other_file A file or NULL + * @param event The monitor event + * @param session The girara session + */ +void cb_file_monitor(GFileMonitor* monitor, GFile* file, GFile* other_file, GFileMonitorEvent event, girara_session_t* session); + #endif // CALLBACKS_H diff --git a/document.c b/document.c index c3eca86..3de028d 100644 --- a/document.c +++ b/document.c @@ -214,6 +214,8 @@ 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 @@ -268,34 +270,57 @@ zathura_document_open(zathura_t* zathura, const char* path, const char* password document->scale = 1; } - if (plugin->open_function != NULL) { - if (plugin->open_function(document) == true) { - /* update statusbar */ - girara_statusbar_item_set_text(zathura->ui.session, zathura->ui.statusbar.file, real_path); - - /* read all pages */ - document->pages = calloc(document->number_of_pages, sizeof(zathura_page_t*)); - if (document->pages == NULL) { - goto error_free; - } - - for (unsigned int page_id = 0; page_id < document->number_of_pages; page_id++) { - zathura_page_t* page = zathura_page_get(document, page_id); - if (page == NULL) { - goto error_free; - } - - document->pages[page_id] = page; - } - - return document; - } + if (plugin->open_function == NULL || plugin->open_function(document) == false) { + girara_error("could not open file\n"); + goto error_free; } - girara_error("could not open file\n"); + /* update statusbar */ + girara_statusbar_item_set_text(zathura->ui.session, zathura->ui.statusbar.file, real_path); + + /* read all pages */ + document->pages = calloc(document->number_of_pages, sizeof(zathura_page_t*)); + if (document->pages == NULL) { + goto error_free; + } + + for (unsigned int page_id = 0; page_id < document->number_of_pages; page_id++) { + zathura_page_t* page = zathura_page_get(document, page_id); + if (page == NULL) { + goto error_free; + } + + 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), document); + + 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 5392756..d4d8ce8 100644 --- a/document.h +++ b/document.h @@ -261,6 +261,14 @@ 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; }; /**