Implemented auto reload basics

This commit is contained in:
Moritz Lipp 2012-02-07 19:25:47 +01:00
parent 59e36d84ae
commit 30f62a1f3a
4 changed files with 83 additions and 23 deletions

View file

@ -211,3 +211,17 @@ error_ret:
return FALSE; 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);
}

View file

@ -7,6 +7,8 @@
#include <girara/types.h> #include <girara/types.h>
#include <girara/macros.h> #include <girara/macros.h>
#include "document.h"
/** /**
* Quits the current zathura session * 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); 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 #endif // CALLBACKS_H

View file

@ -214,6 +214,8 @@ zathura_document_open(zathura_t* zathura, const char* path, const char* password
return NULL; return NULL;
} }
char* file_uri = NULL;
/* determine real path */ /* determine real path */
long path_max; long path_max;
#ifdef PATH_MAX #ifdef PATH_MAX
@ -268,34 +270,57 @@ zathura_document_open(zathura_t* zathura, const char* path, const char* password
document->scale = 1; document->scale = 1;
} }
if (plugin->open_function != NULL) { if (plugin->open_function == NULL || plugin->open_function(document) == false) {
if (plugin->open_function(document) == true) { girara_error("could not open file\n");
/* update statusbar */ goto error_free;
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;
}
} }
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: error_free:
if (file_uri != NULL) {
g_free(file_uri);
}
free(real_path); free(real_path);
if (document != NULL && document->pages != NULL) { if (document != NULL && document->pages != NULL) {

View file

@ -261,6 +261,14 @@ struct zathura_document_s
* Document pages * Document pages
*/ */
zathura_page_t** pages; zathura_page_t** pages;
/**
* File monitor
*/
struct {
GFileMonitor* monitor; /**< File monitor */
GFile* file; /**< File for file monitor */
} file_monitor;
}; };
/** /**