mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
Allow the user to cancel a dive computer download
The code pretended to support this for libdivecomputer based downloads, but it had never been hooked up when the native Uemis downloader was implemented. When I finally decided to close that feature gap I realized that the original code was, shall we say, "aspirational" or "completely bogus" and therefore never worked. So instead of just hooking up the code for the Uemis downloader I instead implemented this correctly for the first time for both libdivecomputer and the native Uemis downloader. In order not to have to mess with multithreaded Gtk development I simply opted for a helper function that fires on a 100ms timeout and have it end the dialog without a response. This way we can run the dialog while waiting for the download to finish, still update the progress bar and respond in a useful manner to the user clicking cancel. Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
parent
d1571ead2d
commit
a8d413551e
5 changed files with 97 additions and 18 deletions
|
@ -114,6 +114,6 @@ extern GtkTreeViewColumn *tree_view_column(GtkWidget *tree_view, int index, cons
|
||||||
data_func_t data_func, unsigned int flags);
|
data_func_t data_func, unsigned int flags);
|
||||||
|
|
||||||
GError *uemis_download(const char *path, char **divenr, char **xml_buffer,
|
GError *uemis_download(const char *path, char **divenr, char **xml_buffer,
|
||||||
progressbar_t *progress, gboolean force_download);
|
progressbar_t *progress, GtkDialog *dialog, gboolean force_download);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -1678,7 +1678,7 @@ static GError *setup_uemis_import(device_data_t *data)
|
||||||
GError *error = NULL;
|
GError *error = NULL;
|
||||||
char *buf = NULL;
|
char *buf = NULL;
|
||||||
|
|
||||||
error = uemis_download(data->devname, &uemis_max_dive_data, &buf, &data->progress, data->force_download);
|
error = uemis_download(data->devname, &uemis_max_dive_data, &buf, &data->progress, data->dialog, data->force_download);
|
||||||
if (buf && strlen(buf) > 1) {
|
if (buf && strlen(buf) > 1) {
|
||||||
#if UEMIS_DEBUG > 3
|
#if UEMIS_DEBUG > 3
|
||||||
fprintf(debugfile, "xml buffer \"%s\"\n\n", buf);
|
fprintf(debugfile, "xml buffer \"%s\"\n\n", buf);
|
||||||
|
@ -1742,7 +1742,7 @@ void download_dialog(GtkWidget *w, gpointer data)
|
||||||
GTK_WINDOW(main_window),
|
GTK_WINDOW(main_window),
|
||||||
GTK_DIALOG_DESTROY_WITH_PARENT,
|
GTK_DIALOG_DESTROY_WITH_PARENT,
|
||||||
GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
|
GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
|
||||||
GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
|
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
|
||||||
NULL);
|
NULL);
|
||||||
g_signal_connect(dialog, "delete-event", G_CALLBACK(download_dialog_delete), NULL);
|
g_signal_connect(dialog, "delete-event", G_CALLBACK(download_dialog_delete), NULL);
|
||||||
|
|
||||||
|
@ -1819,6 +1819,7 @@ repeat:
|
||||||
while (*(--ne) == ' ' || *ne == '\t')
|
while (*(--ne) == ' ' || *ne == '\t')
|
||||||
*ne = '\0';
|
*ne = '\0';
|
||||||
devicedata.devname = ns;
|
devicedata.devname = ns;
|
||||||
|
devicedata.dialog = GTK_DIALOG(dialog);
|
||||||
devicedata.force_download = force_download;
|
devicedata.force_download = force_download;
|
||||||
force_download = FALSE; /* when retrying we don't want to restart */
|
force_download = FALSE; /* when retrying we don't want to restart */
|
||||||
info = import_dive_computer(&devicedata, GTK_DIALOG(dialog));
|
info = import_dive_computer(&devicedata, GTK_DIALOG(dialog));
|
||||||
|
|
|
@ -423,21 +423,55 @@ static void *pthread_wrapper(void *_data)
|
||||||
return (void *)err_string;
|
return (void *)err_string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* this simply ends the dialog without a response and asks not to be fired again
|
||||||
|
* as we set this function up in every loop while uemis_download is waiting for
|
||||||
|
* the download to finish */
|
||||||
|
static gboolean timeout_func(gpointer _data)
|
||||||
|
{
|
||||||
|
GtkDialog *dialog = _data;
|
||||||
|
if (!import_thread_cancelled)
|
||||||
|
gtk_dialog_response(dialog, GTK_RESPONSE_NONE);
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
GError *do_import(device_data_t *data)
|
GError *do_import(device_data_t *data)
|
||||||
{
|
{
|
||||||
pthread_t pthread;
|
pthread_t pthread;
|
||||||
void *retval;
|
void *retval;
|
||||||
|
GtkDialog *dialog = data->dialog;
|
||||||
|
|
||||||
/* I'm sure there is some better interface for waiting on a thread in a UI main loop */
|
/* I'm sure there is some better interface for waiting on a thread in a UI main loop */
|
||||||
import_thread_done = 0;
|
import_thread_done = 0;
|
||||||
progress_bar_text = "";
|
progress_bar_text = "";
|
||||||
progress_bar_fraction = 0.0;
|
progress_bar_fraction = 0.0;
|
||||||
pthread_create(&pthread, NULL, pthread_wrapper, data);
|
pthread_create(&pthread, NULL, pthread_wrapper, data);
|
||||||
|
/* loop here until the import is done or was cancelled by the user;
|
||||||
|
* in order to get control back from gtk we register a timeout function
|
||||||
|
* that ends the dialog with no response every 100ms; we then update the
|
||||||
|
* progressbar and setup the timeout again - unless of course the user
|
||||||
|
* pressed cancel, in which case we just wait for the download thread
|
||||||
|
* to react to that and exit */
|
||||||
while (!import_thread_done) {
|
while (!import_thread_done) {
|
||||||
import_thread_cancelled = process_ui_events();
|
if (!import_thread_cancelled) {
|
||||||
update_progressbar(&data->progress, progress_bar_fraction);
|
int result;
|
||||||
update_progressbar_text(&data->progress, progress_bar_text);
|
g_timeout_add(100, timeout_func, dialog);
|
||||||
usleep(100000);
|
update_progressbar(&data->progress, progress_bar_fraction);
|
||||||
|
update_progressbar_text(&data->progress, progress_bar_text);
|
||||||
|
result = gtk_dialog_run(dialog);
|
||||||
|
switch (result) {
|
||||||
|
case GTK_RESPONSE_CANCEL:
|
||||||
|
import_thread_cancelled = TRUE;
|
||||||
|
progress_bar_text = "Cancelled...";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
/* nothing */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
update_progressbar(&data->progress, progress_bar_fraction);
|
||||||
|
update_progressbar_text(&data->progress, progress_bar_text);
|
||||||
|
usleep(100000);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (pthread_join(pthread, &retval) < 0)
|
if (pthread_join(pthread, &retval) < 0)
|
||||||
retval = _("Odd pthread error return");
|
retval = _("Odd pthread error return");
|
||||||
|
|
|
@ -18,6 +18,7 @@ typedef struct device_data_t {
|
||||||
progressbar_t progress;
|
progressbar_t progress;
|
||||||
int preexisting;
|
int preexisting;
|
||||||
gboolean force_download;
|
gboolean force_download;
|
||||||
|
GtkDialog *dialog;
|
||||||
} device_data_t;
|
} device_data_t;
|
||||||
|
|
||||||
extern GError *do_import(device_data_t *data);
|
extern GError *do_import(device_data_t *data);
|
||||||
|
|
|
@ -510,6 +510,8 @@ static gboolean uemis_get_answer(const char *path, char *request, int n_param_in
|
||||||
mbuf = NULL;
|
mbuf = NULL;
|
||||||
mbuf_size = 0;
|
mbuf_size = 0;
|
||||||
while (searching || assembling_mbuf) {
|
while (searching || assembling_mbuf) {
|
||||||
|
if (import_thread_cancelled)
|
||||||
|
return FALSE;
|
||||||
progress_bar_fraction = filenr / 4000.0;
|
progress_bar_fraction = filenr / 4000.0;
|
||||||
snprintf(fl, 13, "ANS%d.TXT", filenr - 1);
|
snprintf(fl, 13, "ANS%d.TXT", filenr - 1);
|
||||||
ans_path = g_build_filename(path, "ANS", fl, NULL);
|
ans_path = g_build_filename(path, "ANS", fl, NULL);
|
||||||
|
@ -519,7 +521,7 @@ static gboolean uemis_get_answer(const char *path, char *request, int n_param_in
|
||||||
#if UEMIS_DEBUG > 3
|
#if UEMIS_DEBUG > 3
|
||||||
tmp[100]='\0';
|
tmp[100]='\0';
|
||||||
fprintf(debugfile, "::t %s \"%s\"\n", ans_path, tmp);
|
fprintf(debugfile, "::t %s \"%s\"\n", ans_path, tmp);
|
||||||
#elsif UEMIS_DEBUG > 1
|
#elif UEMIS_DEBUG > 1
|
||||||
char pbuf[4];
|
char pbuf[4];
|
||||||
pbuf[0] = tmp[0];
|
pbuf[0] = tmp[0];
|
||||||
pbuf[1] = tmp[1];
|
pbuf[1] = tmp[1];
|
||||||
|
@ -816,6 +818,9 @@ static char *do_uemis_download(struct argument_block *args)
|
||||||
uemis_info("Start download");
|
uemis_info("Start download");
|
||||||
if (! uemis_get_answer(mountpath, "processSync", 0, 2, &result))
|
if (! uemis_get_answer(mountpath, "processSync", 0, 2, &result))
|
||||||
goto bail;
|
goto bail;
|
||||||
|
/* before starting the long download, check if user pressed cancel */
|
||||||
|
if (import_thread_cancelled)
|
||||||
|
goto bail;
|
||||||
param_buff[1] = "notempty";
|
param_buff[1] = "notempty";
|
||||||
/* if we have an empty divelist then the user will almost
|
/* if we have an empty divelist then the user will almost
|
||||||
* certainly want to start downloading from the first dive on
|
* certainly want to start downloading from the first dive on
|
||||||
|
@ -841,6 +846,9 @@ static char *do_uemis_download(struct argument_block *args)
|
||||||
/* if we got an error, deal with it */
|
/* if we got an error, deal with it */
|
||||||
if (!success)
|
if (!success)
|
||||||
break;
|
break;
|
||||||
|
/* if the user clicked cancel, exit gracefully but ignore everything read */
|
||||||
|
if (import_thread_cancelled)
|
||||||
|
goto bail;
|
||||||
/* also, if we got nothing back, we should stop trying */
|
/* also, if we got nothing back, we should stop trying */
|
||||||
if (!param_buff[3])
|
if (!param_buff[3])
|
||||||
break;
|
break;
|
||||||
|
@ -871,22 +879,24 @@ static char *do_uemis_download(struct argument_block *args)
|
||||||
buffer_insert(xml_buffer, &xml_buffer_size, next_detail);
|
buffer_insert(xml_buffer, &xml_buffer_size, next_detail);
|
||||||
free(next_detail);
|
free(next_detail);
|
||||||
}
|
}
|
||||||
if (!success)
|
if (!success || import_thread_cancelled)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (! uemis_get_answer(mountpath, "terminateSync", 0, 3, &result))
|
bail:
|
||||||
goto bail;
|
(void) uemis_get_answer(mountpath, "terminateSync", 0, 3, &result);
|
||||||
if (! strcmp(param_buff[0], "error")) {
|
if (! strcmp(param_buff[0], "error")) {
|
||||||
if (! strcmp(param_buff[2],"Out of Memory"))
|
if (! strcmp(param_buff[2],"Out of Memory"))
|
||||||
result = _(ERR_FS_FULL);
|
result = _(ERR_FS_FULL);
|
||||||
else
|
else
|
||||||
result = param_buff[2];
|
result = param_buff[2];
|
||||||
}
|
}
|
||||||
buffer_add(xml_buffer, &xml_buffer_size, "</list></dives>");
|
if (import_thread_cancelled)
|
||||||
|
xml_buffer[0] = 0;
|
||||||
|
else
|
||||||
|
buffer_add(xml_buffer, &xml_buffer_size, "</list></dives>");
|
||||||
#if UEMIS_DEBUG > 5
|
#if UEMIS_DEBUG > 5
|
||||||
fprintf(debugfile, "XML buffer \"%s\"", *xml_buffer);
|
fprintf(debugfile, "XML buffer \"%s\"", *xml_buffer);
|
||||||
#endif
|
#endif
|
||||||
bail:
|
|
||||||
free(deviceid);
|
free(deviceid);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -899,8 +909,19 @@ static void *pthread_wrapper(void *_data)
|
||||||
return (void *)err_string;
|
return (void *)err_string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* this simply ends the dialog without a response and asks not to be fired again
|
||||||
|
* as we set this function up in every loop while uemis_download is waiting for
|
||||||
|
* the download to finish */
|
||||||
|
static gboolean timeout_func(gpointer _data)
|
||||||
|
{
|
||||||
|
GtkDialog *dialog = _data;
|
||||||
|
if (!import_thread_cancelled)
|
||||||
|
gtk_dialog_response(dialog, GTK_RESPONSE_NONE);
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
GError *uemis_download(const char *mountpath, char **max_dive_data, char **xml_buffer, progressbar_t *progress,
|
GError *uemis_download(const char *mountpath, char **max_dive_data, char **xml_buffer, progressbar_t *progress,
|
||||||
gboolean force_download)
|
GtkDialog *dialog, gboolean force_download)
|
||||||
{
|
{
|
||||||
pthread_t pthread;
|
pthread_t pthread;
|
||||||
void *retval;
|
void *retval;
|
||||||
|
@ -911,11 +932,33 @@ GError *uemis_download(const char *mountpath, char **max_dive_data, char **xml_b
|
||||||
progress_bar_text = "";
|
progress_bar_text = "";
|
||||||
progress_bar_fraction = 0.0;
|
progress_bar_fraction = 0.0;
|
||||||
pthread_create(&pthread, NULL, pthread_wrapper, &args);
|
pthread_create(&pthread, NULL, pthread_wrapper, &args);
|
||||||
|
/* loop here until the import is done or was cancelled by the user;
|
||||||
|
* in order to get control back from gtk we register a timeout function
|
||||||
|
* that ends the dialog with no response every 100ms; we then update the
|
||||||
|
* progressbar and setup the timeout again - unless of course the user
|
||||||
|
* pressed cancel, in which case we just wait for the download thread
|
||||||
|
* to react to that and exit */
|
||||||
while (!import_thread_done) {
|
while (!import_thread_done) {
|
||||||
import_thread_cancelled = process_ui_events();
|
if (!import_thread_cancelled) {
|
||||||
update_progressbar(args.progress, progress_bar_fraction);
|
int result;
|
||||||
update_progressbar_text(args.progress, progress_bar_text);
|
g_timeout_add(100, timeout_func, dialog);
|
||||||
usleep(100000);
|
update_progressbar(args.progress, progress_bar_fraction);
|
||||||
|
update_progressbar_text(args.progress, progress_bar_text);
|
||||||
|
result = gtk_dialog_run(dialog);
|
||||||
|
switch (result) {
|
||||||
|
case GTK_RESPONSE_CANCEL:
|
||||||
|
import_thread_cancelled = TRUE;
|
||||||
|
progress_bar_text = "Cancelled...";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
/* nothing */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
update_progressbar(args.progress, progress_bar_fraction);
|
||||||
|
update_progressbar_text(args.progress, progress_bar_text);
|
||||||
|
usleep(100000);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (pthread_join(pthread, &retval) < 0)
|
if (pthread_join(pthread, &retval) < 0)
|
||||||
return error("Pthread return with error");
|
return error("Pthread return with error");
|
||||||
|
|
Loading…
Add table
Reference in a new issue