2013-05-20 19:43:33 +00:00
|
|
|
#include "downloadfromdivecomputer.h"
|
|
|
|
|
|
|
|
#include "../libdivecomputer.h"
|
2013-05-23 06:24:33 +00:00
|
|
|
#include "../helpers.h"
|
|
|
|
#include "../display.h"
|
2013-05-30 08:58:59 +00:00
|
|
|
#include "../divelist.h"
|
|
|
|
#include "mainwindow.h"
|
2013-05-20 20:58:06 +00:00
|
|
|
#include <cstdlib>
|
2013-05-20 19:43:33 +00:00
|
|
|
#include <QThread>
|
|
|
|
#include <QDebug>
|
2013-05-20 20:58:06 +00:00
|
|
|
#include <QStringListModel>
|
2013-08-25 22:02:30 +00:00
|
|
|
#include <QTimer>
|
2013-09-01 14:14:54 +00:00
|
|
|
#include <QMessageBox>
|
2013-05-20 20:58:06 +00: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 19:43:33 +00:00
|
|
|
|
|
|
|
namespace DownloadFromDcGlobal{
|
|
|
|
const char *err_string;
|
|
|
|
};
|
|
|
|
|
2013-05-30 08:58:59 +00:00
|
|
|
DownloadFromDCWidget *DownloadFromDCWidget::instance()
|
|
|
|
{
|
|
|
|
static DownloadFromDCWidget *dialog = new DownloadFromDCWidget();
|
2013-06-28 12:20:42 +00:00
|
|
|
dialog->setAttribute(Qt::WA_QuitOnClose, false);
|
2013-05-30 08:58:59 +00:00
|
|
|
return dialog;
|
|
|
|
}
|
|
|
|
|
2013-05-20 19:43:33 +00:00
|
|
|
DownloadFromDCWidget::DownloadFromDCWidget(QWidget* parent, Qt::WindowFlags f) :
|
2013-10-03 18:54:25 +00:00
|
|
|
QDialog(parent, f), thread(0), timer(new QTimer(this)),
|
2013-08-25 22:02:30 +00:00
|
|
|
currentState(INITIAL)
|
2013-05-20 19:43:33 +00:00
|
|
|
{
|
2013-10-03 18:54:25 +00:00
|
|
|
ui.setupUi(this);
|
|
|
|
ui.progressBar->hide();
|
|
|
|
ui.progressBar->setMinimum(0);
|
|
|
|
ui.progressBar->setMaximum(100);
|
2013-09-16 21:04:42 +00:00
|
|
|
|
|
|
|
fill_device_list();
|
2013-05-20 20:58:06 +00:00
|
|
|
fill_computer_list();
|
|
|
|
|
|
|
|
vendorModel = new QStringListModel(vendorList);
|
2013-10-03 18:54:25 +00:00
|
|
|
ui.vendor->setModel(vendorModel);
|
2013-05-23 06:24:33 +00:00
|
|
|
if (default_dive_computer_vendor) {
|
2013-10-03 18:54:25 +00:00
|
|
|
ui.vendor->setCurrentIndex(ui.vendor->findText(default_dive_computer_vendor));
|
2013-05-23 06:24:33 +00:00
|
|
|
productModel = new QStringListModel(productList[default_dive_computer_vendor]);
|
2013-10-03 18:54:25 +00:00
|
|
|
ui.product->setModel(productModel);
|
2013-05-23 06:24:33 +00:00
|
|
|
if (default_dive_computer_product)
|
2013-10-03 18:54:25 +00:00
|
|
|
ui.product->setCurrentIndex(ui.product->findText(default_dive_computer_product));
|
2013-05-23 06:24:33 +00:00
|
|
|
}
|
|
|
|
if (default_dive_computer_device)
|
2013-10-03 18:54:25 +00:00
|
|
|
ui.device->setEditText(default_dive_computer_device);
|
2013-08-25 22:02:30 +00:00
|
|
|
|
|
|
|
timer->setInterval(200);
|
|
|
|
connect(timer, SIGNAL(timeout()), this, SLOT(updateProgressBar()));
|
|
|
|
updateState(INITIAL);
|
2013-05-20 20:58:06 +00:00
|
|
|
}
|
|
|
|
|
2013-05-30 08:58:59 +00:00
|
|
|
void DownloadFromDCWidget::runDialog()
|
|
|
|
{
|
2013-08-25 22:02:30 +00:00
|
|
|
updateState(INITIAL);
|
2013-06-28 12:12:09 +00:00
|
|
|
exec();
|
2013-05-30 08:58:59 +00:00
|
|
|
}
|
|
|
|
|
2013-08-25 22:02:30 +00:00
|
|
|
void DownloadFromDCWidget::updateProgressBar()
|
2013-05-30 09:21:08 +00:00
|
|
|
{
|
2013-10-03 18:54:25 +00:00
|
|
|
ui.progressBar->setValue(progress_bar_fraction *100);
|
2013-08-25 22:02:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadFromDCWidget::updateState(states state)
|
|
|
|
{
|
|
|
|
if (state == currentState)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (state == INITIAL) {
|
2013-09-16 21:04:42 +00:00
|
|
|
fill_device_list();
|
2013-10-03 18:54:25 +00:00
|
|
|
ui.progressBar->hide();
|
2013-08-25 22:02:30 +00:00
|
|
|
markChildrenAsEnabled();
|
|
|
|
timer->stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
// tries to cancel an on going download
|
|
|
|
else if (currentState == DOWNLOADING && state == CANCELLING) {
|
|
|
|
import_thread_cancelled = true;
|
2013-10-03 18:54:25 +00:00
|
|
|
ui.cancel->setEnabled(false);
|
2013-08-25 22:02:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// user pressed cancel but the application isn't doing anything.
|
|
|
|
// means close the window
|
2013-09-01 14:14:54 +00:00
|
|
|
else if ((currentState == INITIAL || currentState == CANCELLED || currentState == DONE || currentState == ERROR)
|
2013-08-25 22:02:30 +00:00
|
|
|
&& state == CANCELLING) {
|
|
|
|
timer->stop();
|
|
|
|
reject();
|
2013-11-09 06:57:36 +00:00
|
|
|
ui.ok->setText(tr("OK"));
|
2013-08-25 22:02:30 +00:00
|
|
|
}
|
|
|
|
|
2013-09-01 14:14:54 +00:00
|
|
|
// the cancelation process is finished
|
2013-08-25 22:02:30 +00:00
|
|
|
else if (currentState == CANCELLING && (state == DONE || state == CANCELLED)) {
|
|
|
|
timer->stop();
|
|
|
|
state = CANCELLED;
|
2013-10-03 18:54:25 +00:00
|
|
|
ui.progressBar->setValue(0);
|
|
|
|
ui.progressBar->hide();
|
2013-08-25 22:02:30 +00: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 18:54:25 +00:00
|
|
|
ui.progressBar->setValue(100);
|
2013-08-25 22:02:30 +00:00
|
|
|
markChildrenAsEnabled();
|
2013-11-09 06:57:36 +00:00
|
|
|
ui.ok->setText(tr("OK"));
|
2013-08-25 22:02:30 +00:00
|
|
|
accept();
|
|
|
|
}
|
|
|
|
|
|
|
|
// DOWNLOAD is started.
|
|
|
|
else if (state == DOWNLOADING) {
|
|
|
|
timer->start();
|
2013-10-03 18:54:25 +00:00
|
|
|
ui.progressBar->setValue(0);
|
|
|
|
ui.progressBar->show();
|
2013-08-25 22:02:30 +00:00
|
|
|
markChildrenAsDisabled();
|
|
|
|
}
|
|
|
|
|
2013-09-01 14:14:54 +00:00
|
|
|
// got an error
|
|
|
|
else if (state == ERROR) {
|
2013-11-21 12:23:10 +00:00
|
|
|
QMessageBox::critical(this, TITLE_OR_TEXT(tr("Error"), this->thread->error), QMessageBox::Ok);
|
2013-09-01 14:14:54 +00:00
|
|
|
|
|
|
|
markChildrenAsEnabled();
|
2013-10-03 18:54:25 +00:00
|
|
|
ui.progressBar->hide();
|
2013-11-09 06:57:36 +00:00
|
|
|
ui.ok->setText(tr("Retry"));
|
2013-09-01 14:14:54 +00:00
|
|
|
}
|
|
|
|
|
2013-08-25 22:02:30 +00:00
|
|
|
// properly updating the widget state
|
|
|
|
currentState = state;
|
2013-05-30 09:21:08 +00:00
|
|
|
}
|
|
|
|
|
2013-05-20 20:58:06 +00:00
|
|
|
void DownloadFromDCWidget::on_vendor_currentIndexChanged(const QString& vendor)
|
|
|
|
{
|
2013-10-03 18:54:25 +00:00
|
|
|
QAbstractItemModel *currentModel = ui.product->model();
|
2013-05-20 20:58:06 +00:00
|
|
|
if (!currentModel)
|
|
|
|
return;
|
|
|
|
|
|
|
|
productModel = new QStringListModel(productList[vendor]);
|
2013-10-03 18:54:25 +00:00
|
|
|
ui.product->setModel(productModel);
|
2013-05-20 20:58:06 +00: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-23 04:25:05 +00:00
|
|
|
if (!vendorList.contains(vendor))
|
|
|
|
vendorList.append(vendor);
|
2013-05-20 20:58:06 +00:00
|
|
|
|
2013-05-23 04:25:05 +00:00
|
|
|
if (!productList[vendor].contains(product))
|
|
|
|
productList[vendor].push_back(product);
|
2013-05-21 04:55:56 +00:00
|
|
|
|
|
|
|
descriptorLookup[QString(vendor) + QString(product)] = descriptor;
|
2013-05-20 20:58:06 +00: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-23 04:25:05 +00:00
|
|
|
if (!vendorList.contains("Uemis"))
|
2013-05-20 20:58:06 +00:00
|
|
|
vendorList.append("Uemis");
|
|
|
|
|
2013-05-23 04:25:05 +00:00
|
|
|
if (!productList["Uemis"].contains("Zurich"))
|
|
|
|
productList["Uemis"].push_back("Zurich");
|
2013-05-21 04:55:56 +00:00
|
|
|
|
|
|
|
descriptorLookup[QString("UemisZurich")] = (dc_descriptor_t *)mydescriptor;
|
2013-05-20 19:43:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadFromDCWidget::on_cancel_clicked()
|
|
|
|
{
|
2013-08-25 22:02:30 +00:00
|
|
|
updateState(CANCELLING);
|
2013-05-20 19:43:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadFromDCWidget::on_ok_clicked()
|
|
|
|
{
|
2013-08-25 22:02:30 +00:00
|
|
|
updateState(DOWNLOADING);
|
2013-05-20 19:43:33 +00:00
|
|
|
|
2013-08-25 22:02:30 +00:00
|
|
|
// I don't really think that create/destroy the thread
|
|
|
|
// is really necessary.
|
2013-05-23 04:25:05 +00:00
|
|
|
if (thread) {
|
2013-05-20 19:43:33 +00:00
|
|
|
thread->deleteLater();
|
|
|
|
}
|
|
|
|
|
2013-10-03 18:54:25 +00: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 13:01:59 +00:00
|
|
|
|
2013-10-03 18:54:25 +00:00
|
|
|
data.descriptor = descriptorLookup[ui.vendor->currentText() + ui.product->currentText()];
|
|
|
|
data.force_download = ui.forceDownload->isChecked();
|
2013-06-16 00:48:31 +00:00
|
|
|
data.deviceid = data.diveid = 0;
|
2013-05-23 06:24:33 +00:00
|
|
|
set_default_dive_computer(data.vendor, data.product);
|
|
|
|
set_default_dive_computer_device(data.devname);
|
2013-05-21 04:55:56 +00:00
|
|
|
|
2013-08-25 22:02:30 +00:00
|
|
|
thread = new DownloadThread(this, &data);
|
2013-05-20 20:02:17 +00:00
|
|
|
|
2013-08-25 22:02:30 +00:00
|
|
|
connect(thread, SIGNAL(finished()),
|
|
|
|
this, SLOT(onDownloadThreadFinished()), Qt::QueuedConnection);
|
|
|
|
|
|
|
|
MainWindow *w = mainWindow();
|
|
|
|
connect(thread, SIGNAL(finished()), w, SLOT(refreshDisplay()));
|
2013-05-20 20:02:17 +00:00
|
|
|
|
2013-05-20 19:43:33 +00:00
|
|
|
thread->start();
|
|
|
|
}
|
|
|
|
|
2013-05-30 08:58:59 +00:00
|
|
|
bool DownloadFromDCWidget::preferDownloaded()
|
|
|
|
{
|
2013-10-03 18:54:25 +00:00
|
|
|
return ui.preferDownloaded->isChecked();
|
2013-05-30 08:58:59 +00:00
|
|
|
}
|
|
|
|
|
2013-08-25 13:01:59 +00:00
|
|
|
void DownloadFromDCWidget::reject()
|
|
|
|
{
|
|
|
|
// we don't want the download window being able to close
|
|
|
|
// while we're still downloading.
|
2013-08-25 22:02:30 +00:00
|
|
|
if (currentState != DOWNLOADING && currentState != CANCELLING)
|
2013-08-25 13:01:59 +00:00
|
|
|
QDialog::reject();
|
|
|
|
}
|
|
|
|
|
2013-10-15 17:25:53 +00:00
|
|
|
void DownloadFromDCWidget::onDownloadThreadFinished()
|
2013-08-25 22:02:30 +00:00
|
|
|
{
|
2013-09-01 14:14:54 +00:00
|
|
|
if (currentState == DOWNLOADING) {
|
|
|
|
if (thread->error.isEmpty())
|
|
|
|
updateState(DONE);
|
|
|
|
else
|
|
|
|
updateState(ERROR);
|
2013-09-17 21:36:22 +00:00
|
|
|
|
|
|
|
// I'm not sure if we should really call process_dives even
|
|
|
|
// if there's an error or a cancelation
|
|
|
|
process_dives(TRUE, preferDownloaded());
|
2013-09-01 14:14:54 +00:00
|
|
|
} else
|
2013-08-25 22:02:30 +00:00
|
|
|
updateState(CANCELLED);
|
|
|
|
}
|
|
|
|
|
2013-08-25 13:01:59 +00:00
|
|
|
void DownloadFromDCWidget::markChildrenAsDisabled()
|
|
|
|
{
|
2013-10-03 18:54:25 +00: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 13:01:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadFromDCWidget::markChildrenAsEnabled()
|
|
|
|
{
|
2013-10-03 18:54:25 +00: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 13:01:59 +00:00
|
|
|
}
|
|
|
|
|
2013-09-16 21:04:42 +00: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 18:54:25 +00:00
|
|
|
ui.device->clear();
|
|
|
|
deviceIndex = enumerate_devices(fillDeviceList, ui.device);
|
2013-09-16 21:04:42 +00:00
|
|
|
if (deviceIndex >= 0)
|
2013-10-03 18:54:25 +00:00
|
|
|
ui.device->setCurrentIndex(deviceIndex);
|
2013-09-16 21:04:42 +00:00
|
|
|
}
|
|
|
|
|
2013-08-25 22:02:30 +00:00
|
|
|
DownloadThread::DownloadThread(QObject* parent, device_data_t* data): QThread(parent),
|
|
|
|
data(data)
|
2013-05-20 19:43:33 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-09-01 14:14:54 +00: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 19:43:33 +00:00
|
|
|
void DownloadThread::run()
|
|
|
|
{
|
2013-09-01 14:14:54 +00:00
|
|
|
const char *error;
|
2013-06-24 22:14:07 +00:00
|
|
|
if (!strcmp(data->vendor, "Uemis"))
|
2013-09-01 14:14:54 +00:00
|
|
|
error = do_uemis_import(data->devname, data->force_download);
|
2013-06-24 22:14:07 +00:00
|
|
|
else
|
2013-09-01 14:14:54 +00:00
|
|
|
error = do_libdivecomputer_import(data);
|
2013-10-11 13:21:04 +00:00
|
|
|
if (error)
|
2013-09-01 14:14:54 +00:00
|
|
|
this->error = str_error(error, data->devname, data->vendor, data->product);
|
2013-05-20 19:43:33 +00:00
|
|
|
}
|