| 
									
										
										
										
											2013-05-20 16:43:33 -03:00
										 |  |  | #include "downloadfromdivecomputer.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "../libdivecomputer.h"
 | 
					
						
							| 
									
										
										
										
											2013-05-22 23:24:33 -07:00
										 |  |  | #include "../helpers.h"
 | 
					
						
							|  |  |  | #include "../display.h"
 | 
					
						
							| 
									
										
										
										
											2013-05-30 17:58:59 +09:00
										 |  |  | #include "../divelist.h"
 | 
					
						
							|  |  |  | #include "mainwindow.h"
 | 
					
						
							| 
									
										
										
										
											2013-05-20 17:58:06 -03:00
										 |  |  | #include <cstdlib>
 | 
					
						
							| 
									
										
										
										
											2013-05-20 16:43:33 -03:00
										 |  |  | #include <QThread>
 | 
					
						
							|  |  |  | #include <QDebug>
 | 
					
						
							| 
									
										
										
										
											2013-05-20 17:58:06 -03:00
										 |  |  | #include <QStringListModel>
 | 
					
						
							| 
									
										
										
										
											2013-08-25 19:02:30 -03:00
										 |  |  | #include <QTimer>
 | 
					
						
							| 
									
										
										
										
											2013-09-01 11:14:54 -03:00
										 |  |  | #include <QMessageBox>
 | 
					
						
							| 
									
										
										
										
											2013-05-20 17:58:06 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | struct product { | 
					
						
							|  |  |  | 	const char *product; | 
					
						
							|  |  |  | 	dc_descriptor_t *descriptor; | 
					
						
							|  |  |  | 	struct product *next; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct vendor { | 
					
						
							|  |  |  | 	const char *vendor; | 
					
						
							|  |  |  | 	struct product *productlist; | 
					
						
							|  |  |  | 	struct vendor *next; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct mydescriptor { | 
					
						
							|  |  |  | 	const char *vendor; | 
					
						
							|  |  |  | 	const char *product; | 
					
						
							|  |  |  | 	dc_family_t type; | 
					
						
							|  |  |  | 	unsigned int model; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2013-05-20 16:43:33 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace DownloadFromDcGlobal{ | 
					
						
							|  |  |  | 	const char *err_string; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-30 17:58:59 +09:00
										 |  |  | DownloadFromDCWidget *DownloadFromDCWidget::instance() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-11-30 09:18:04 -08:00
										 |  |  | 	static DownloadFromDCWidget *dialog = new DownloadFromDCWidget(mainWindow()); | 
					
						
							| 
									
										
										
										
											2013-06-28 15:20:42 +03:00
										 |  |  | 	dialog->setAttribute(Qt::WA_QuitOnClose, false); | 
					
						
							| 
									
										
										
										
											2013-05-30 17:58:59 +09:00
										 |  |  | 	return dialog; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-20 16:43:33 -03:00
										 |  |  | DownloadFromDCWidget::DownloadFromDCWidget(QWidget* parent, Qt::WindowFlags f) : | 
					
						
							| 
									
										
										
										
											2013-10-03 11:54:25 -07:00
										 |  |  |     QDialog(parent, f), thread(0), timer(new QTimer(this)), | 
					
						
							| 
									
										
										
										
											2013-08-25 19:02:30 -03:00
										 |  |  | 	currentState(INITIAL) | 
					
						
							| 
									
										
										
										
											2013-05-20 16:43:33 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-10-03 11:54:25 -07:00
										 |  |  | 	ui.setupUi(this); | 
					
						
							|  |  |  | 	ui.progressBar->hide(); | 
					
						
							|  |  |  | 	ui.progressBar->setMinimum(0); | 
					
						
							|  |  |  | 	ui.progressBar->setMaximum(100); | 
					
						
							| 
									
										
										
										
											2013-09-16 18:04:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	fill_device_list(); | 
					
						
							| 
									
										
										
										
											2013-05-20 17:58:06 -03:00
										 |  |  | 	fill_computer_list(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	vendorModel = new QStringListModel(vendorList); | 
					
						
							| 
									
										
										
										
											2013-10-03 11:54:25 -07:00
										 |  |  | 	ui.vendor->setModel(vendorModel); | 
					
						
							| 
									
										
										
										
											2013-05-22 23:24:33 -07:00
										 |  |  | 	if (default_dive_computer_vendor) { | 
					
						
							| 
									
										
										
										
											2013-10-03 11:54:25 -07:00
										 |  |  | 		ui.vendor->setCurrentIndex(ui.vendor->findText(default_dive_computer_vendor)); | 
					
						
							| 
									
										
										
										
											2013-05-22 23:24:33 -07:00
										 |  |  | 		productModel = new QStringListModel(productList[default_dive_computer_vendor]); | 
					
						
							| 
									
										
										
										
											2013-10-03 11:54:25 -07:00
										 |  |  | 		ui.product->setModel(productModel); | 
					
						
							| 
									
										
										
										
											2013-05-22 23:24:33 -07:00
										 |  |  | 		if (default_dive_computer_product) | 
					
						
							| 
									
										
										
										
											2013-10-03 11:54:25 -07:00
										 |  |  | 			ui.product->setCurrentIndex(ui.product->findText(default_dive_computer_product)); | 
					
						
							| 
									
										
										
										
											2013-05-22 23:24:33 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if (default_dive_computer_device) | 
					
						
							| 
									
										
										
										
											2013-10-03 11:54:25 -07:00
										 |  |  | 		ui.device->setEditText(default_dive_computer_device); | 
					
						
							| 
									
										
										
										
											2013-08-25 19:02:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	timer->setInterval(200); | 
					
						
							|  |  |  | 	connect(timer, SIGNAL(timeout()), this, SLOT(updateProgressBar())); | 
					
						
							|  |  |  | 	updateState(INITIAL); | 
					
						
							| 
									
										
										
										
											2013-05-20 17:58:06 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-30 17:58:59 +09:00
										 |  |  | void DownloadFromDCWidget::runDialog() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-08-25 19:02:30 -03:00
										 |  |  | 	updateState(INITIAL); | 
					
						
							| 
									
										
										
										
											2013-06-28 15:12:09 +03:00
										 |  |  | 	exec(); | 
					
						
							| 
									
										
										
										
											2013-05-30 17:58:59 +09:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-25 19:02:30 -03:00
										 |  |  | void DownloadFromDCWidget::updateProgressBar() | 
					
						
							| 
									
										
										
										
											2013-05-30 18:21:08 +09:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-10-03 11:54:25 -07:00
										 |  |  | 	ui.progressBar->setValue(progress_bar_fraction *100); | 
					
						
							| 
									
										
										
										
											2013-08-25 19:02:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DownloadFromDCWidget::updateState(states state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (state == currentState) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (state == INITIAL) { | 
					
						
							| 
									
										
										
										
											2013-09-16 18:04:42 -03:00
										 |  |  | 		fill_device_list(); | 
					
						
							| 
									
										
										
										
											2013-10-03 11:54:25 -07:00
										 |  |  | 		ui.progressBar->hide(); | 
					
						
							| 
									
										
										
										
											2013-08-25 19:02:30 -03:00
										 |  |  | 		markChildrenAsEnabled(); | 
					
						
							|  |  |  | 		timer->stop(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// tries to cancel an on going download
 | 
					
						
							|  |  |  | 	else if (currentState == DOWNLOADING && state == CANCELLING) { | 
					
						
							|  |  |  | 		import_thread_cancelled = true; | 
					
						
							| 
									
										
										
										
											2013-10-03 11:54:25 -07:00
										 |  |  | 		ui.cancel->setEnabled(false); | 
					
						
							| 
									
										
										
										
											2013-08-25 19:02:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// user pressed cancel but the application isn't doing anything.
 | 
					
						
							|  |  |  | 	// means close the window
 | 
					
						
							| 
									
										
										
										
											2013-09-01 11:14:54 -03:00
										 |  |  | 	else if ((currentState == INITIAL || currentState == CANCELLED || currentState == DONE || currentState == ERROR) | 
					
						
							| 
									
										
										
										
											2013-08-25 19:02:30 -03:00
										 |  |  | 		&& state == CANCELLING) { | 
					
						
							|  |  |  | 		timer->stop(); | 
					
						
							|  |  |  | 		reject(); | 
					
						
							| 
									
										
										
										
											2013-11-09 15:57:36 +09:00
										 |  |  | 		ui.ok->setText(tr("OK")); | 
					
						
							| 
									
										
										
										
											2013-08-25 19:02:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-01 11:14:54 -03:00
										 |  |  | 	// the cancelation process is finished
 | 
					
						
							| 
									
										
										
										
											2013-08-25 19:02:30 -03:00
										 |  |  | 	else if (currentState == CANCELLING && (state == DONE || state == CANCELLED)) { | 
					
						
							|  |  |  | 		timer->stop(); | 
					
						
							|  |  |  | 		state = CANCELLED; | 
					
						
							| 
									
										
										
										
											2013-10-03 11:54:25 -07:00
										 |  |  | 		ui.progressBar->setValue(0); | 
					
						
							|  |  |  | 		ui.progressBar->hide(); | 
					
						
							| 
									
										
										
										
											2013-08-25 19:02:30 -03:00
										 |  |  | 		markChildrenAsEnabled(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// DOWNLOAD is finally done, close the dialog and go back to the main window
 | 
					
						
							|  |  |  | 	else if (currentState == DOWNLOADING && state == DONE) { | 
					
						
							|  |  |  | 		timer->stop(); | 
					
						
							| 
									
										
										
										
											2013-10-03 11:54:25 -07:00
										 |  |  | 		ui.progressBar->setValue(100); | 
					
						
							| 
									
										
										
										
											2013-08-25 19:02:30 -03:00
										 |  |  | 		markChildrenAsEnabled(); | 
					
						
							| 
									
										
										
										
											2013-11-09 15:57:36 +09:00
										 |  |  | 		ui.ok->setText(tr("OK")); | 
					
						
							| 
									
										
										
										
											2013-08-25 19:02:30 -03:00
										 |  |  | 		accept(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// DOWNLOAD is started.
 | 
					
						
							|  |  |  | 	else if (state == DOWNLOADING) { | 
					
						
							|  |  |  | 		timer->start(); | 
					
						
							| 
									
										
										
										
											2013-10-03 11:54:25 -07:00
										 |  |  | 		ui.progressBar->setValue(0); | 
					
						
							|  |  |  | 		ui.progressBar->show(); | 
					
						
							| 
									
										
										
										
											2013-08-25 19:02:30 -03:00
										 |  |  | 		markChildrenAsDisabled(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-01 11:14:54 -03:00
										 |  |  | 	// got an error
 | 
					
						
							|  |  |  | 	else if (state == ERROR) { | 
					
						
							| 
									
										
										
										
											2013-11-21 04:23:10 -08:00
										 |  |  | 		QMessageBox::critical(this, TITLE_OR_TEXT(tr("Error"), this->thread->error), QMessageBox::Ok); | 
					
						
							| 
									
										
										
										
											2013-09-01 11:14:54 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		markChildrenAsEnabled(); | 
					
						
							| 
									
										
										
										
											2013-10-03 11:54:25 -07:00
										 |  |  | 		ui.progressBar->hide(); | 
					
						
							| 
									
										
										
										
											2013-11-09 15:57:36 +09:00
										 |  |  | 		ui.ok->setText(tr("Retry")); | 
					
						
							| 
									
										
										
										
											2013-09-01 11:14:54 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-25 19:02:30 -03:00
										 |  |  | 	// properly updating the widget state
 | 
					
						
							|  |  |  | 	currentState = state; | 
					
						
							| 
									
										
										
										
											2013-05-30 18:21:08 +09:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-20 17:58:06 -03:00
										 |  |  | void DownloadFromDCWidget::on_vendor_currentIndexChanged(const QString& vendor) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-10-03 11:54:25 -07:00
										 |  |  | 	QAbstractItemModel *currentModel = ui.product->model(); | 
					
						
							| 
									
										
										
										
											2013-05-20 17:58:06 -03:00
										 |  |  | 	if (!currentModel) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	productModel = new QStringListModel(productList[vendor]); | 
					
						
							| 
									
										
										
										
											2013-10-03 11:54:25 -07:00
										 |  |  | 	ui.product->setModel(productModel); | 
					
						
							| 
									
										
										
										
											2013-05-20 17:58:06 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Memleak - but deleting gives me a crash.
 | 
					
						
							|  |  |  | 	//currentModel->deleteLater();
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DownloadFromDCWidget::fill_computer_list() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	dc_iterator_t *iterator = NULL; | 
					
						
							|  |  |  | 	dc_descriptor_t *descriptor = NULL; | 
					
						
							|  |  |  | 	struct mydescriptor *mydescriptor; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	QStringList computer; | 
					
						
							|  |  |  | 	dc_descriptor_iterator(&iterator); | 
					
						
							|  |  |  | 	while (dc_iterator_next (iterator, &descriptor) == DC_STATUS_SUCCESS) { | 
					
						
							|  |  |  | 		const char *vendor = dc_descriptor_get_vendor(descriptor); | 
					
						
							|  |  |  | 		const char *product = dc_descriptor_get_product(descriptor); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-22 21:25:05 -07:00
										 |  |  | 		if (!vendorList.contains(vendor)) | 
					
						
							|  |  |  | 			vendorList.append(vendor); | 
					
						
							| 
									
										
										
										
											2013-05-20 17:58:06 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-22 21:25:05 -07:00
										 |  |  | 		if (!productList[vendor].contains(product)) | 
					
						
							|  |  |  | 			productList[vendor].push_back(product); | 
					
						
							| 
									
										
										
										
											2013-05-20 21:55:56 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		descriptorLookup[QString(vendor) + QString(product)] = descriptor; | 
					
						
							| 
									
										
										
										
											2013-05-20 17:58:06 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	dc_iterator_free(iterator); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* and add the Uemis Zurich which we are handling internally
 | 
					
						
							|  |  |  | 	   THIS IS A HACK as we magically have a data structure here that | 
					
						
							|  |  |  | 	   happens to match a data structure that is internal to libdivecomputer; | 
					
						
							|  |  |  | 	   this WILL BREAK if libdivecomputer changes the dc_descriptor struct... | 
					
						
							|  |  |  | 	   eventually the UEMIS code needs to move into libdivecomputer, I guess */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	mydescriptor = (struct mydescriptor*) malloc(sizeof(struct mydescriptor)); | 
					
						
							|  |  |  | 	mydescriptor->vendor = "Uemis"; | 
					
						
							|  |  |  | 	mydescriptor->product = "Zurich"; | 
					
						
							|  |  |  | 	mydescriptor->type = DC_FAMILY_NULL; | 
					
						
							|  |  |  | 	mydescriptor->model = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-22 21:25:05 -07:00
										 |  |  | 	if (!vendorList.contains("Uemis")) | 
					
						
							| 
									
										
										
										
											2013-05-20 17:58:06 -03:00
										 |  |  | 		vendorList.append("Uemis"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-22 21:25:05 -07:00
										 |  |  | 	if (!productList["Uemis"].contains("Zurich")) | 
					
						
							|  |  |  | 		productList["Uemis"].push_back("Zurich"); | 
					
						
							| 
									
										
										
										
											2013-05-20 21:55:56 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	descriptorLookup[QString("UemisZurich")] = (dc_descriptor_t *)mydescriptor; | 
					
						
							| 
									
										
										
										
											2013-05-20 16:43:33 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DownloadFromDCWidget::on_cancel_clicked() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-08-25 19:02:30 -03:00
										 |  |  | 	updateState(CANCELLING); | 
					
						
							| 
									
										
										
										
											2013-05-20 16:43:33 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DownloadFromDCWidget::on_ok_clicked() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-08-25 19:02:30 -03:00
										 |  |  | 	updateState(DOWNLOADING); | 
					
						
							| 
									
										
										
										
											2013-05-20 16:43:33 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-25 19:02:30 -03:00
										 |  |  | 	// I don't really think that create/destroy the thread
 | 
					
						
							|  |  |  | 	// is really necessary.
 | 
					
						
							| 
									
										
										
										
											2013-05-22 21:25:05 -07:00
										 |  |  | 	if (thread) { | 
					
						
							| 
									
										
										
										
											2013-05-20 16:43:33 -03:00
										 |  |  | 		thread->deleteLater(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-03 11:54:25 -07:00
										 |  |  | 	data.devname = strdup(ui.device->currentText().toUtf8().data()); | 
					
						
							|  |  |  | 	data.vendor = strdup(ui.vendor->currentText().toUtf8().data()); | 
					
						
							|  |  |  | 	data.product = strdup(ui.product->currentText().toUtf8().data()); | 
					
						
							| 
									
										
										
										
											2013-08-25 10:01:59 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-03 11:54:25 -07:00
										 |  |  | 	data.descriptor = descriptorLookup[ui.vendor->currentText() + ui.product->currentText()]; | 
					
						
							|  |  |  | 	data.force_download = ui.forceDownload->isChecked(); | 
					
						
							| 
									
										
										
										
											2013-06-15 14:48:31 -10:00
										 |  |  | 	data.deviceid = data.diveid = 0; | 
					
						
							| 
									
										
										
										
											2013-05-22 23:24:33 -07:00
										 |  |  | 	set_default_dive_computer(data.vendor, data.product); | 
					
						
							|  |  |  | 	set_default_dive_computer_device(data.devname); | 
					
						
							| 
									
										
										
										
											2013-05-20 21:55:56 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-25 19:02:30 -03:00
										 |  |  | 	thread = new DownloadThread(this, &data); | 
					
						
							| 
									
										
										
										
											2013-05-20 17:02:17 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-25 19:02:30 -03:00
										 |  |  | 	connect(thread, SIGNAL(finished()), | 
					
						
							|  |  |  | 			this, SLOT(onDownloadThreadFinished()), Qt::QueuedConnection); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	MainWindow *w = mainWindow(); | 
					
						
							|  |  |  | 	connect(thread, SIGNAL(finished()), w, SLOT(refreshDisplay())); | 
					
						
							| 
									
										
										
										
											2013-05-20 17:02:17 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-08 06:33:46 +01:00
										 |  |  | 	// before we start, remember where the dive_table ended
 | 
					
						
							|  |  |  | 	previousLast = dive_table.nr; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-20 16:43:33 -03:00
										 |  |  | 	thread->start(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-30 17:58:59 +09:00
										 |  |  | bool DownloadFromDCWidget::preferDownloaded() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-10-03 11:54:25 -07:00
										 |  |  | 	return ui.preferDownloaded->isChecked(); | 
					
						
							| 
									
										
										
										
											2013-05-30 17:58:59 +09:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-25 10:01:59 -03:00
										 |  |  | void DownloadFromDCWidget::reject() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	// we don't want the download window being able to close
 | 
					
						
							|  |  |  | 	// while we're still downloading.
 | 
					
						
							| 
									
										
										
										
											2013-08-25 19:02:30 -03:00
										 |  |  | 	if (currentState != DOWNLOADING && currentState != CANCELLING) | 
					
						
							| 
									
										
										
										
											2013-08-25 10:01:59 -03:00
										 |  |  | 		QDialog::reject(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-15 10:25:53 -07:00
										 |  |  | void DownloadFromDCWidget::onDownloadThreadFinished() | 
					
						
							| 
									
										
										
										
											2013-08-25 19:02:30 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-09-01 11:14:54 -03:00
										 |  |  | 	if (currentState == DOWNLOADING) { | 
					
						
							|  |  |  | 		if (thread->error.isEmpty()) | 
					
						
							|  |  |  | 			updateState(DONE); | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			updateState(ERROR); | 
					
						
							| 
									
										
										
										
											2013-09-17 18:36:22 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// I'm not sure if we should really call process_dives even
 | 
					
						
							| 
									
										
										
										
											2013-12-08 06:33:46 +01:00
										 |  |  | 		// if there's an error
 | 
					
						
							|  |  |  | 		if (import_thread_cancelled) { | 
					
						
							|  |  |  | 			// walk backwards so we don't keep moving the dives
 | 
					
						
							|  |  |  | 			// down in the dive_table
 | 
					
						
							|  |  |  | 			for (int i = dive_table.nr - 1; i >= previousLast; i--) | 
					
						
							|  |  |  | 				delete_single_dive(i); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			process_dives(TRUE, preferDownloaded()); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2013-08-25 19:02:30 -03:00
										 |  |  | 		updateState(CANCELLED); | 
					
						
							| 
									
										
										
										
											2013-12-08 06:33:46 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-08-25 19:02:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-25 10:01:59 -03:00
										 |  |  | void DownloadFromDCWidget::markChildrenAsDisabled() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-10-03 11:54:25 -07:00
										 |  |  | 	ui.device->setDisabled(true); | 
					
						
							|  |  |  | 	ui.vendor->setDisabled(true); | 
					
						
							|  |  |  | 	ui.product->setDisabled(true); | 
					
						
							|  |  |  | 	ui.forceDownload->setDisabled(true); | 
					
						
							|  |  |  | 	ui.preferDownloaded->setDisabled(true); | 
					
						
							|  |  |  | 	ui.ok->setDisabled(true); | 
					
						
							|  |  |  | 	ui.search->setDisabled(true); | 
					
						
							| 
									
										
										
										
											2013-08-25 10:01:59 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DownloadFromDCWidget::markChildrenAsEnabled() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-10-03 11:54:25 -07:00
										 |  |  | 	ui.device->setDisabled(false); | 
					
						
							|  |  |  | 	ui.vendor->setDisabled(false); | 
					
						
							|  |  |  | 	ui.product->setDisabled(false); | 
					
						
							|  |  |  | 	ui.forceDownload->setDisabled(false); | 
					
						
							|  |  |  | 	ui.preferDownloaded->setDisabled(false); | 
					
						
							|  |  |  | 	ui.ok->setDisabled(false); | 
					
						
							|  |  |  | 	ui.cancel->setDisabled(false); | 
					
						
							|  |  |  | 	ui.search->setDisabled(false); | 
					
						
							| 
									
										
										
										
											2013-08-25 10:01:59 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-16 18:04:42 -03:00
										 |  |  | static void fillDeviceList(const char *name, void *data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	QComboBox *comboBox = (QComboBox *)data; | 
					
						
							|  |  |  | 	comboBox->addItem(name); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DownloadFromDCWidget::fill_device_list() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int deviceIndex; | 
					
						
							| 
									
										
										
										
											2013-10-03 11:54:25 -07:00
										 |  |  | 	ui.device->clear(); | 
					
						
							|  |  |  | 	deviceIndex = enumerate_devices(fillDeviceList, ui.device); | 
					
						
							| 
									
										
										
										
											2013-09-16 18:04:42 -03:00
										 |  |  | 	if (deviceIndex >= 0) | 
					
						
							| 
									
										
										
										
											2013-10-03 11:54:25 -07:00
										 |  |  | 		ui.device->setCurrentIndex(deviceIndex); | 
					
						
							| 
									
										
										
										
											2013-09-16 18:04:42 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-25 19:02:30 -03:00
										 |  |  | DownloadThread::DownloadThread(QObject* parent, device_data_t* data): QThread(parent), | 
					
						
							|  |  |  | 	data(data) | 
					
						
							| 
									
										
										
										
											2013-05-20 16:43:33 -03:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-01 11:14:54 -03:00
										 |  |  | static QString str_error(const char *fmt, ...) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	va_list args; | 
					
						
							|  |  |  | 	va_start(args, fmt); | 
					
						
							|  |  |  | 	const QString str = QString().vsprintf( fmt, args ); | 
					
						
							|  |  |  | 	va_end(args); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return str; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-20 16:43:33 -03:00
										 |  |  | void DownloadThread::run() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-09-01 11:14:54 -03:00
										 |  |  | 	const char *error; | 
					
						
							| 
									
										
										
										
											2013-12-10 08:09:42 +01:00
										 |  |  | 	import_thread_cancelled = false; | 
					
						
							| 
									
										
										
										
											2013-06-24 15:14:07 -07:00
										 |  |  | 	if (!strcmp(data->vendor, "Uemis")) | 
					
						
							| 
									
										
										
										
											2013-09-01 11:14:54 -03:00
										 |  |  | 		error = do_uemis_import(data->devname, data->force_download); | 
					
						
							| 
									
										
										
										
											2013-06-24 15:14:07 -07:00
										 |  |  | 	else | 
					
						
							| 
									
										
										
										
											2013-09-01 11:14:54 -03:00
										 |  |  | 		error = do_libdivecomputer_import(data); | 
					
						
							| 
									
										
										
										
											2013-10-11 10:21:04 -03:00
										 |  |  | 	if (error) | 
					
						
							| 
									
										
										
										
											2013-09-01 11:14:54 -03:00
										 |  |  | 		this->error =  str_error(error, data->devname, data->vendor, data->product); | 
					
						
							| 
									
										
										
										
											2013-05-20 16:43:33 -03:00
										 |  |  | } |