2013-05-20 19:43:33 +00:00
|
|
|
#include "downloadfromdivecomputer.h"
|
|
|
|
#include "ui_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>
|
|
|
|
|
|
|
|
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-05-20 20:02:17 +00:00
|
|
|
QDialog(parent, f), ui(new Ui::DownloadFromDiveComputer), thread(0), downloading(false)
|
2013-05-20 19:43:33 +00:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
ui->progressBar->hide();
|
|
|
|
ui->progressBar->setMinimum(0);
|
|
|
|
ui->progressBar->setMaximum(100);
|
2013-05-20 20:58:06 +00:00
|
|
|
fill_computer_list();
|
|
|
|
|
|
|
|
vendorModel = new QStringListModel(vendorList);
|
|
|
|
ui->vendor->setModel(vendorModel);
|
2013-05-23 06:24:33 +00:00
|
|
|
if (default_dive_computer_vendor) {
|
|
|
|
ui->vendor->setCurrentIndex(ui->vendor->findText(default_dive_computer_vendor));
|
|
|
|
productModel = new QStringListModel(productList[default_dive_computer_vendor]);
|
|
|
|
ui->product->setModel(productModel);
|
|
|
|
if (default_dive_computer_product)
|
|
|
|
ui->product->setCurrentIndex(ui->product->findText(default_dive_computer_product));
|
|
|
|
}
|
|
|
|
if (default_dive_computer_device)
|
|
|
|
ui->device->setText(default_dive_computer_device);
|
2013-05-20 20:58:06 +00:00
|
|
|
}
|
|
|
|
|
2013-05-30 08:58:59 +00:00
|
|
|
void DownloadFromDCWidget::runDialog()
|
|
|
|
{
|
|
|
|
ui->progressBar->hide();
|
|
|
|
show();
|
|
|
|
}
|
|
|
|
|
2013-05-30 09:21:08 +00:00
|
|
|
void DownloadFromDCWidget::stoppedDownloading()
|
|
|
|
{
|
|
|
|
downloading = false;
|
|
|
|
}
|
|
|
|
|
2013-05-20 20:58:06 +00:00
|
|
|
void DownloadFromDCWidget::on_vendor_currentIndexChanged(const QString& vendor)
|
|
|
|
{
|
2013-05-21 04:55:56 +00:00
|
|
|
QAbstractItemModel *currentModel = ui->product->model();
|
2013-05-20 20:58:06 +00:00
|
|
|
if (!currentModel)
|
|
|
|
return;
|
|
|
|
|
|
|
|
productModel = new QStringListModel(productList[vendor]);
|
2013-05-21 04:55:56 +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-05-20 20:02:17 +00:00
|
|
|
import_thread_cancelled = true;
|
2013-05-23 04:25:05 +00:00
|
|
|
if (thread) {
|
2013-05-20 20:02:17 +00:00
|
|
|
thread->wait();
|
|
|
|
thread->deleteLater();
|
|
|
|
thread = 0;
|
|
|
|
}
|
2013-05-20 19:43:33 +00:00
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadFromDCWidget::on_ok_clicked()
|
|
|
|
{
|
2013-05-20 20:02:17 +00:00
|
|
|
if (downloading)
|
|
|
|
return;
|
2013-05-20 19:43:33 +00:00
|
|
|
|
|
|
|
ui->progressBar->setValue(0);
|
|
|
|
ui->progressBar->show();
|
|
|
|
|
2013-05-23 04:25:05 +00:00
|
|
|
if (thread) {
|
2013-05-20 19:43:33 +00:00
|
|
|
thread->deleteLater();
|
|
|
|
}
|
|
|
|
|
2013-05-21 04:55:56 +00:00
|
|
|
data.devname = strdup(ui->device->text().toUtf8().data());
|
|
|
|
data.vendor = strdup(ui->vendor->currentText().toUtf8().data());
|
|
|
|
data.product = strdup(ui->product->currentText().toUtf8().data());
|
|
|
|
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-05-20 19:43:33 +00:00
|
|
|
thread = new InterfaceThread(this, &data);
|
2013-05-20 20:02:17 +00:00
|
|
|
connect(thread, SIGNAL(updateInterface(int)),
|
|
|
|
ui->progressBar, SLOT(setValue(int)), Qt::QueuedConnection); // Qt::QueuedConnection == threadsafe.
|
|
|
|
|
|
|
|
connect(thread, SIGNAL(finished()), this, SLOT(close()));
|
|
|
|
|
2013-05-20 19:43:33 +00:00
|
|
|
thread->start();
|
2013-05-20 20:02:17 +00:00
|
|
|
downloading = true;
|
2013-05-20 19:43:33 +00:00
|
|
|
}
|
|
|
|
|
2013-05-30 08:58:59 +00:00
|
|
|
bool DownloadFromDCWidget::preferDownloaded()
|
|
|
|
{
|
|
|
|
return ui->preferDownloaded->isChecked();
|
|
|
|
}
|
|
|
|
|
2013-05-20 19:43:33 +00:00
|
|
|
DownloadThread::DownloadThread(device_data_t* data): data(data)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadThread::run()
|
|
|
|
{
|
2013-05-30 08:58:59 +00:00
|
|
|
DownloadFromDCWidget *dfdcw = DownloadFromDCWidget::instance();
|
2013-06-24 22:14:07 +00:00
|
|
|
if (!strcmp(data->vendor, "Uemis"))
|
|
|
|
do_uemis_import(data->devname, data->force_download);
|
|
|
|
else
|
|
|
|
do_libdivecomputer_import(data);
|
2013-05-30 08:58:59 +00:00
|
|
|
process_dives(TRUE, dfdcw->preferDownloaded());
|
2013-05-30 09:21:08 +00:00
|
|
|
dfdcw->stoppedDownloading();
|
2013-05-20 19:43:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
InterfaceThread::InterfaceThread(QObject* parent, device_data_t* data): QThread(parent), data(data)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterfaceThread::run()
|
|
|
|
{
|
|
|
|
DownloadThread *download = new DownloadThread(data);
|
2013-05-30 08:58:59 +00:00
|
|
|
MainWindow *w = mainWindow();
|
|
|
|
connect(download, SIGNAL(finished()), w, SLOT(refreshDisplay()));
|
2013-05-20 19:43:33 +00:00
|
|
|
download->start();
|
2013-05-23 04:25:05 +00:00
|
|
|
while (download->isRunning()) {
|
2013-05-20 19:43:33 +00:00
|
|
|
msleep(200);
|
2013-05-20 20:02:17 +00:00
|
|
|
updateInterface(progress_bar_fraction *100);
|
|
|
|
}
|
2013-05-20 19:43:33 +00:00
|
|
|
updateInterface(100);
|
|
|
|
}
|