Commit b6f1868e authored by Moritz Lipp's avatar Moritz Lipp

Fix automatic file reloading

parent a2addfab
...@@ -221,11 +221,14 @@ cb_file_monitor(GFileMonitor* monitor, GFile* file, GFile* UNUSED(other_file), G ...@@ -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(file != NULL);
g_return_if_fail(session != NULL); g_return_if_fail(session != NULL);
if (event != G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT) { switch (event) {
return; 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 static gboolean
......
...@@ -116,7 +116,7 @@ cmd_close(girara_session_t* session, girara_list_t* UNUSED(argument_list)) ...@@ -116,7 +116,7 @@ cmd_close(girara_session_t* session, girara_list_t* UNUSED(argument_list))
return true; return true;
} }
document_close(zathura); document_close(zathura, false);
return true; return true;
} }
...@@ -198,8 +198,8 @@ cmd_open(girara_session_t* session, girara_list_t* argument_list) ...@@ -198,8 +198,8 @@ cmd_open(girara_session_t* session, girara_list_t* argument_list)
girara_notify(session, GIRARA_ERROR, "Too many arguments."); girara_notify(session, GIRARA_ERROR, "Too many arguments.");
return false; return false;
} else if (argc >= 1) { } else if (argc >= 1) {
if (zathura->document) { if (zathura->document != NULL) {
document_close(zathura); document_close(zathura, false);
} }
document_open(zathura, girara_list_nth(argument_list, 0), (argc == 2) ? girara_list_nth(argument_list, 1) : NULL); document_open(zathura, girara_list_nth(argument_list, 0), (argc == 2) ? girara_list_nth(argument_list, 1) : NULL);
......
...@@ -216,8 +216,6 @@ zathura_document_open(zathura_t* zathura, const char* path, const char* password ...@@ -216,8 +216,6 @@ 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
...@@ -328,24 +326,6 @@ zathura_document_open(zathura_t* zathura, const char* path, const char* password ...@@ -328,24 +326,6 @@ zathura_document_open(zathura_t* zathura, const char* path, const char* password
document->pages[page_id] = page; 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 */ /* apply open adjustment */
char* adjust_open = "best-fit"; char* adjust_open = "best-fit";
document->adjust_mode = ADJUST_BESTFIT; document->adjust_mode = ADJUST_BESTFIT;
...@@ -360,16 +340,10 @@ zathura_document_open(zathura_t* zathura, const char* path, const char* password ...@@ -360,16 +340,10 @@ zathura_document_open(zathura_t* zathura, const char* path, const char* password
g_free(adjust_open); g_free(adjust_open);
} }
g_free(file_uri);
return document; 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) {
......
...@@ -300,14 +300,6 @@ struct zathura_document_s ...@@ -300,14 +300,6 @@ 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;
}; };
/** /**
......
...@@ -375,23 +375,15 @@ sc_reload(girara_session_t* session, girara_argument_t* UNUSED(argument), ...@@ -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); g_return_val_if_fail(session->global.data != NULL, false);
zathura_t* zathura = session->global.data; zathura_t* zathura = session->global.data;
if (zathura->document == NULL || zathura->document->file_path == NULL) { if (zathura->file_monitor.file_path == NULL) {
return false; 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 */ /* close current document */
document_close(zathura); document_close(zathura, true);
/* reopen document */ /* reopen document */
document_open(zathura, path, password); document_open(zathura, zathura->file_monitor.file_path, zathura->file_monitor.password);
/* clean up */
g_free(path);
g_free(password);
return false; return false;
} }
......
...@@ -273,7 +273,7 @@ zathura_free(zathura_t* zathura) ...@@ -273,7 +273,7 @@ zathura_free(zathura_t* zathura)
return; return;
} }
document_close(zathura); document_close(zathura, false);
if (zathura->ui.session != NULL) { if (zathura->ui.session != NULL) {
girara_session_destroy(zathura->ui.session); girara_session_destroy(zathura->ui.session);
...@@ -408,6 +408,36 @@ document_open(zathura_t* zathura, const char* path, const char* password) ...@@ -408,6 +408,36 @@ document_open(zathura_t* zathura, const char* path, const char* password)
goto error_out; 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; zathura->document = document;
/* view mode */ /* view mode */
...@@ -436,10 +466,16 @@ document_open(zathura_t* zathura, const char* path, const char* password) ...@@ -436,10 +466,16 @@ document_open(zathura_t* zathura, const char* path, const char* password)
page_set_delayed(zathura, document->current_page_number - 1); page_set_delayed(zathura, document->current_page_number - 1);
cb_view_vadjustment_value_changed(NULL, zathura); cb_view_vadjustment_value_changed(NULL, zathura);
free(file_uri);
return true; return true;
error_free: error_free:
if (file_uri != NULL) {
g_free(file_uri);
}
zathura_document_free(document); zathura_document_free(document);
error_out: error_out:
...@@ -478,12 +514,36 @@ remove_page_from_table(GtkWidget* page, gpointer permanent) ...@@ -478,12 +514,36 @@ remove_page_from_table(GtkWidget* page, gpointer permanent)
} }
bool 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; 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 */ /* store last seen page */
zathura_db_set_fileinfo(zathura->database, zathura->document->file_path, zathura->document->current_page_number, zathura_db_set_fileinfo(zathura->database, zathura->document->file_path, zathura->document->current_page_number,
/* zathura->document->offset TODO */ 0, zathura->document->scale, /* zathura->document->offset TODO */ 0, zathura->document->scale,
......
...@@ -109,6 +109,16 @@ typedef struct zathura_s ...@@ -109,6 +109,16 @@ typedef struct zathura_s
zathura_document_t* document; /**< The current document */ zathura_document_t* document; /**< The current document */
zathura_database_t* database; /**< The database */ 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; } zathura_t;
/** /**
...@@ -153,9 +163,10 @@ bool document_save(zathura_t* zathura, const char* path, bool overwrite); ...@@ -153,9 +163,10 @@ bool document_save(zathura_t* zathura, const char* path, bool overwrite);
* Closes the current opened document * Closes the current opened document
* *
* @param zathura The zathura session * @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. * @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 * Opens the page with the given number
......
Markdown is supported
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