[PATCH] Separate download code sharable between desktop & mobile
This is a very first step that extracts code from
downloadfromdivecomputer into a separate class called
"downloadmanager". The purpose of this patch is to do
some separation of sharable code without affecting the desktop
functionality. The code for mobile and its operation is not
affected at all.
Signed-off-by: Willem Ferguson <[email protected]>
This is a concept that needs review by individuals better with C++ than
I am (almost everyone on the list).
I tested this code by downloading dives using my Uwatec dc. Next step is
to define a model class for transferring dive computer
names to the QML front end. The models used in the existing widget are
almost surely not usable for interacting with the QML.
Kind regards,
willem
>From 8a3c620d5536d1ccf4e66a53da6081322a496bea Mon Sep 17 00:00:00 2001
From: Willem Ferguson <[email protected]>
Date: Sun, 31 Jan 2016 19:35:23 +0200
Subject: [PATCH 4/4] [PATCH] Separate download code sharable between desktop &
mobile
This is a very first step that extracts code from
downloadfromdivecomputer into a separate class called
"downloadmanager". The purpose of this patch is to do
some separation of sharable code without affecting the desktop
functionality. The code for mobile and its operation is not
affected at all.
Signed-off-by: Willem Ferguson <[email protected]>
---
desktop-widgets/CMakeLists.txt | 1 +
desktop-widgets/downloadfromdivecomputer.cpp | 74 ++++++---------------------
desktop-widgets/downloadfromdivecomputer.h | 12 ++---
desktop-widgets/downloadmanager.cpp | 75 ++++++++++++++++++++++++++++
desktop-widgets/downloadmanager.h | 35 +++++++++++++
5 files changed, 133 insertions(+), 64 deletions(-)
create mode 100644 desktop-widgets/downloadmanager.cpp
create mode 100644 desktop-widgets/downloadmanager.h
diff --git a/desktop-widgets/CMakeLists.txt b/desktop-widgets/CMakeLists.txt
index dc55035..ec077d7 100644
--- a/desktop-widgets/CMakeLists.txt
+++ b/desktop-widgets/CMakeLists.txt
@@ -57,6 +57,7 @@ set(SUBSURFACE_INTERFACE
diveplanner.cpp
diveshareexportdialog.cpp
downloadfromdivecomputer.cpp
+ downloadmanager.cpp
globe.cpp
kmessagewidget.cpp
maintab.cpp
diff --git a/desktop-widgets/downloadfromdivecomputer.cpp b/desktop-widgets/downloadfromdivecomputer.cpp
index 4c8fa6b..20375ac 100644
--- a/desktop-widgets/downloadfromdivecomputer.cpp
+++ b/desktop-widgets/downloadfromdivecomputer.cpp
@@ -5,6 +5,7 @@
#include "display.h"
#include "uemis.h"
#include "models.h"
+#include "downloadmanager.h"
#include <QTimer>
#include <QFileDialog>
@@ -47,6 +48,9 @@ DownloadFromDCWidget::DownloadFromDCWidget(QWidget *parent, Qt::WindowFlags f) :
ostcFirmwareCheck(0),
currentState(INITIAL)
{
+
+ downloadHelper = new DownloadManager();
+
clear_table(&downloadTable);
ui.setupUi(this);
ui.progressBar->hide();
@@ -64,8 +68,7 @@ DownloadFromDCWidget::DownloadFromDCWidget(QWidget *parent, Qt::WindowFlags f) :
progress_bar_text = "";
- fill_computer_list();
-
+ downloadHelper->fill_computer_list();
ui.chooseDumpFile->setEnabled(ui.dumpToFile->isChecked());
connect(ui.chooseDumpFile, SIGNAL(clicked()), this, SLOT(pickDumpFile()));
connect(ui.dumpToFile, SIGNAL(stateChanged(int)), this, SLOT(checkDumpFile(int)));
@@ -76,19 +79,20 @@ DownloadFromDCWidget::DownloadFromDCWidget(QWidget *parent, Qt::WindowFlags f) :
ui.unselectAllButton->setEnabled(false);
connect(ui.selectAllButton, SIGNAL(clicked()), diveImportedModel, SLOT(selectAll()));
connect(ui.unselectAllButton, SIGNAL(clicked()), diveImportedModel, SLOT(selectNone()));
- vendorModel = new QStringListModel(vendorList);
- ui.vendor->setModel(vendorModel);
- if (default_dive_computer_vendor) {
+ vendorModel = new QStringListModel(downloadHelper->vendorList);
+ ui.vendor->setModel(vendorModel);
+
+ 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);
+ productModel = new QStringListModel(downloadHelper->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)
+ if (default_dive_computer_device)
ui.device->setEditText(default_dive_computer_device);
- timer->setInterval(200);
+ timer->setInterval(200);
connect(timer, SIGNAL(timeout()), this, SLOT(updateProgressBar()));
updateState(INITIAL);
memset(&data, 0, sizeof(data));
@@ -199,7 +203,7 @@ void DownloadFromDCWidget::on_vendor_currentIndexChanged(const QString &vendor)
if (!currentModel)
return;
- productModel = new QStringListModel(productList[vendor]);
+ productModel = new QStringListModel(downloadHelper->productList[vendor]);
ui.product->setModel(productModel);
if (vendor == QString("Uemis"))
@@ -214,7 +218,7 @@ void DownloadFromDCWidget::on_product_currentIndexChanged(const QString &product
{
// Set up the DC descriptor
dc_descriptor_t *descriptor = NULL;
- descriptor = descriptorLookup[ui.vendor->currentText() + product];
+ descriptor = downloadHelper->descriptorLookup[ui.vendor->currentText() + product];
// call dc_descriptor_get_transport to see if the dc_transport_t is DC_TRANSPORT_SERIAL
if (dc_descriptor_get_transport(descriptor) == DC_TRANSPORT_SERIAL) {
@@ -226,52 +230,6 @@ void DownloadFromDCWidget::on_product_currentIndexChanged(const QString &product
}
}
-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);
-
- if (!vendorList.contains(vendor))
- vendorList.append(vendor);
-
- if (!productList[vendor].contains(product))
- productList[vendor].push_back(product);
-
- descriptorLookup[QString(vendor) + QString(product)] = descriptor;
- }
- dc_iterator_free(iterator);
- Q_FOREACH (QString vendor, vendorList)
- qSort(productList[vendor]);
-
- /* 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;
-
- if (!vendorList.contains("Uemis"))
- vendorList.append("Uemis");
-
- if (!productList["Uemis"].contains("Zurich"))
- productList["Uemis"].push_back("Zurich");
-
- descriptorLookup["UemisZurich"] = (dc_descriptor_t *)mydescriptor;
-
- qSort(vendorList);
-}
void DownloadFromDCWidget::on_search_clicked()
{
@@ -332,7 +290,7 @@ void DownloadFromDCWidget::on_downloadCancelRetryButton_clicked()
} else {
data.devname = strdup(ui.device->currentText().toUtf8().data());
}
- data.descriptor = descriptorLookup[ui.vendor->currentText() + ui.product->currentText()];
+ data.descriptor = downloadHelper->descriptorLookup[ui.vendor->currentText() + ui.product->currentText()];
data.force_download = ui.forceDownload->isChecked();
data.create_new_trip = ui.createNewTrip->isChecked();
data.trip = NULL;
diff --git a/desktop-widgets/downloadfromdivecomputer.h b/desktop-widgets/downloadfromdivecomputer.h
index 7acd49e..0549573 100644
--- a/desktop-widgets/downloadfromdivecomputer.h
+++ b/desktop-widgets/downloadfromdivecomputer.h
@@ -10,6 +10,7 @@
#include "libdivecomputer.h"
#include "configuredivecomputerdialog.h"
#include "ui_downloadfromdivecomputer.h"
+#include "downloadmanager.h"
#if defined(BT_SUPPORT)
#include "btdeviceselectiondialog.h"
@@ -57,7 +58,7 @@ private:
class DownloadFromDCWidget : public QDialog {
Q_OBJECT
public:
- explicit DownloadFromDCWidget(QWidget *parent = 0, Qt::WindowFlags f = 0);
+ explicit DownloadFromDCWidget(QWidget *parent = 0, Qt::WindowFlags f = 0);
void reject();
enum states {
@@ -96,14 +97,11 @@ private:
DownloadThread *thread;
bool downloading;
- QStringList vendorList;
- QHash<QString, QStringList> productList;
- QMap<QString, dc_descriptor_t *> descriptorLookup;
device_data_t data;
int previousLast;
- QStringListModel *vendorModel;
- QStringListModel *productModel;
+ QStringListModel *vendorModel;
+ QStringListModel *productModel;
void fill_computer_list();
void fill_device_list(int dc_type);
QString logFile;
@@ -120,6 +118,8 @@ public:
bool preferDownloaded();
void updateState(states state);
states currentState;
+ DownloadManager *downloadHelper; // *******************
+
};
#endif // DOWNLOADFROMDIVECOMPUTER_H
diff --git a/desktop-widgets/downloadmanager.cpp b/desktop-widgets/downloadmanager.cpp
new file mode 100644
index 0000000..60adf71
--- /dev/null
+++ b/desktop-widgets/downloadmanager.cpp
@@ -0,0 +1,75 @@
+#include <QStringList>
+#include <QString>
+#include <QHash>
+#include <QMap>
+#include "libdivecomputer.h"
+#include "divecomputer.h"
+#include "downloadmanager.h"
+
+DownloadManager::DownloadManager(QObject *parent) :
+ QObject(parent) {}
+
+DownloadManager::~DownloadManager() {}
+
+/* ------------------------ Fill_computer_list --------------------------
+1) Fill the QstringList structures containing the vendor names as well as the
+ products of all the supported dive computers.
+2) Set up the hash table that points to the descriptor structure for each
+ dive computer.
+This method is used by both the desktop and the mobile software.
+ ----------------------------------------------------------------------- */
+void DownloadManager::fill_computer_list()
+{
+ struct mydescriptor {
+ const char *vendor;
+ const char *product;
+ dc_family_t type;
+ unsigned int model;
+ };
+
+ struct mydescriptor *mydescriptor;
+
+ dc_iterator_t *iterator = NULL;
+ dc_descriptor_t *descriptor = NULL;
+
+ 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);
+
+ if (!vendorList.contains(vendor))
+ vendorList.append(vendor);
+
+ if (!productList[vendor].contains(product))
+ productList[vendor].push_back(product);
+
+ descriptorLookup[QString(vendor) + QString(product)] = descriptor;
+ }
+ dc_iterator_free(iterator);
+ Q_FOREACH (QString vendor, vendorList)
+ qSort(productList[vendor]);
+
+ /* 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;
+
+ if (!vendorList.contains("Uemis"))
+ vendorList.append("Uemis");
+
+ if (!productList["Uemis"].contains("Zurich"))
+ productList["Uemis"].push_back("Zurich");
+
+ descriptorLookup["UemisZurich"] = (dc_descriptor_t *)mydescriptor;
+
+ qSort(vendorList);
+
+}
+
diff --git a/desktop-widgets/downloadmanager.h b/desktop-widgets/downloadmanager.h
new file mode 100644
index 0000000..55fb57b
--- /dev/null
+++ b/desktop-widgets/downloadmanager.h
@@ -0,0 +1,35 @@
+#ifndef DOWNLOADMANAGER_H
+#define DOWNLOADMANAGER_H
+
+#include <QObject>
+#include <QStringList>
+#include <QStringListModel>
+#include <QString>
+#include <QHash>
+#include <QMap>
+#include <QDebug>
+#include "libdivecomputer.h"
+
+class DownloadManager : public QObject
+{
+ Q_OBJECT
+public:
+ DownloadManager(QObject *parent = 0);
+ ~DownloadManager();
+
+ QStringList vendorList;
+
+ QHash<QString, QStringList> productList;
+ QMap<QString, dc_descriptor_t *> descriptorLookup;
+
+ void fill_computer_list();
+
+private:
+
+signals:
+
+public slots:
+
+};
+
+#endif // DOWNLOADMANAGER_H
--
2.1.4
_______________________________________________
subsurface mailing list
[email protected]
http://lists.subsurface-divelog.org/cgi-bin/mailman/listinfo/subsurface