Added code to cancel the thread.

I think it's self explanatory - When user clicks on
'Cancel', the interface will wait for the trhead to quit
then will close itself.

Signed-off-by: Tomaz Canabrava <tcanabrava@kde.org>
This commit is contained in:
Tomaz Canabrava 2013-05-20 17:02:17 -03:00
parent c7a5d0490f
commit a542b25bde
4 changed files with 24 additions and 13 deletions

View file

@ -652,7 +652,7 @@ static void event_cb(dc_device_t *device, dc_event_type_t event, const void *dat
} }
} }
static int import_thread_done = 0, import_thread_cancelled; int import_thread_cancelled;
static int static int
cancel_cb(void *userdata) cancel_cb(void *userdata)

View file

@ -29,6 +29,10 @@ typedef struct device_data_t {
const char *do_libdivecomputer_import(device_data_t *data); const char *do_libdivecomputer_import(device_data_t *data);
extern int import_thread_cancelled;
extern const char *progress_bar_text;
extern double progress_bar_fraction;
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View file

@ -10,11 +10,8 @@ namespace DownloadFromDcGlobal{
const char *err_string; const char *err_string;
}; };
extern const char *progress_bar_text;
extern double progress_bar_fraction;
DownloadFromDCWidget::DownloadFromDCWidget(QWidget* parent, Qt::WindowFlags f) : DownloadFromDCWidget::DownloadFromDCWidget(QWidget* parent, Qt::WindowFlags f) :
QDialog(parent, f), ui(new Ui::DownloadFromDiveComputer), thread(0) QDialog(parent, f), ui(new Ui::DownloadFromDiveComputer), thread(0), downloading(false)
{ {
ui->setupUi(this); ui->setupUi(this);
ui->progressBar->hide(); ui->progressBar->hide();
@ -24,11 +21,19 @@ DownloadFromDCWidget::DownloadFromDCWidget(QWidget* parent, Qt::WindowFlags f) :
void DownloadFromDCWidget::on_cancel_clicked() void DownloadFromDCWidget::on_cancel_clicked()
{ {
import_thread_cancelled = true;
if(thread){
thread->wait();
thread->deleteLater();
thread = 0;
}
close(); close();
} }
void DownloadFromDCWidget::on_ok_clicked() void DownloadFromDCWidget::on_ok_clicked()
{ {
if (downloading)
return;
ui->progressBar->setValue(0); ui->progressBar->setValue(0);
ui->progressBar->show(); ui->progressBar->show();
@ -40,9 +45,13 @@ void DownloadFromDCWidget::on_ok_clicked()
device_data_t data; device_data_t data;
// still need to fill the data info here. // still need to fill the data info here.
thread = new InterfaceThread(this, &data); thread = new InterfaceThread(this, &data);
connect(thread, SIGNAL(updateInterface(int)), ui->progressBar, SLOT(setValue(int)), Qt::QueuedConnection); // Qt::QueuedConnection == threadsafe. connect(thread, SIGNAL(updateInterface(int)),
connect(thread, SIGNAL(updateInterface(int)), this, SLOT(setValue(int)), Qt::QueuedConnection); // Qt::QueuedConnection == threadsafe. ui->progressBar, SLOT(setValue(int)), Qt::QueuedConnection); // Qt::QueuedConnection == threadsafe.
connect(thread, SIGNAL(finished()), this, SLOT(close()));
thread->start(); thread->start();
downloading = true;
} }
DownloadThread::DownloadThread(device_data_t* data): data(data) DownloadThread::DownloadThread(device_data_t* data): data(data)
@ -52,12 +61,10 @@ DownloadThread::DownloadThread(device_data_t* data): data(data)
void DownloadThread::run() void DownloadThread::run()
{ {
do_libdivecomputer_import(data); do_libdivecomputer_import(data);
qDebug() << "Download thread started";
} }
InterfaceThread::InterfaceThread(QObject* parent, device_data_t* data): QThread(parent), data(data) InterfaceThread::InterfaceThread(QObject* parent, device_data_t* data): QThread(parent), data(data)
{ {
} }
void InterfaceThread::run() void InterfaceThread::run()

View file

@ -40,7 +40,7 @@ public slots:
private: private:
Ui::DownloadFromDiveComputer *ui; Ui::DownloadFromDiveComputer *ui;
InterfaceThread *thread; InterfaceThread *thread;
bool downloading;
}; };
#endif #endif