Skeleton code for a non-blocking UI thread for downloading dives from the DC

This is the skeleton code for a non-blocking ui-thread
It already creates the first-thread ( 'do not block the ui' )
and the second thread ('download from the dive computer')
We can in the future merge both in the same place - I didn't
want to do that now because the download function is written
in the libdivecomputer.c code, and I cant just transform that
to a QThread and use signals, so I used two threads for that.

Signed-off-by: Tomaz Canabrava <tcanabrava@kde.org>
This commit is contained in:
Tomaz Canabrava 2013-05-20 16:43:33 -03:00
parent 15bb4fccbb
commit c7a5d0490f
8 changed files with 249 additions and 89 deletions

View file

@ -1,5 +1,4 @@
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <inttypes.h>
#include <glib/gi18n.h>
@ -21,8 +20,9 @@
#define NOT_FROG
#endif
static const char *progress_bar_text = "";
static double progress_bar_fraction = 0.0;
const char *progress_bar_text = "";
double progress_bar_fraction = 0.0;
static int stoptime, stopdepth, ndl, po2, cns;
static gboolean in_deco, first_temp_is_air;
@ -686,7 +686,7 @@ static const char *do_device_import(device_data_t *data)
return NULL;
}
static const char *do_libdivecomputer_import(device_data_t *data)
const char *do_libdivecomputer_import(device_data_t *data)
{
dc_status_t rc;
const char *err;
@ -709,70 +709,3 @@ static const char *do_libdivecomputer_import(device_data_t *data)
dc_context_free(data->context);
return err;
}
#if USE_GTK_UI
static void *pthread_wrapper(void *_data)
{
device_data_t *data = _data;
const char *err_string = do_libdivecomputer_import(data);
import_thread_done = 1;
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)
{
pthread_t pthread;
void *retval;
GtkDialog *dialog = data->dialog;
/* I'm sure there is some better interface for waiting on a thread in a UI main loop */
import_thread_done = 0;
progress_bar_text = "";
progress_bar_fraction = 0.0;
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) {
if (!import_thread_cancelled) {
int result;
g_timeout_add(100, timeout_func, dialog);
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)
retval = _("Odd pthread error return");
if (retval)
return error(retval, data->vendor, data->product, data->devname);
return NULL;
}
#endif