diff options
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | divecomputer.cpp | 187 | ||||
-rw-r--r-- | divecomputer.h | 38 | ||||
-rw-r--r-- | helpers.h | 2 | ||||
-rw-r--r-- | qt-ui/divelistview.cpp | 2 | ||||
-rw-r--r-- | qt-ui/downloadfromdivecomputer.cpp | 3 | ||||
-rw-r--r-- | qt-ui/models.h | 2 | ||||
-rw-r--r-- | qthelper.cpp | 178 | ||||
-rw-r--r-- | qthelper.h | 28 | ||||
-rw-r--r-- | subsurface.pro | 2 |
10 files changed, 232 insertions, 211 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index ba318ca60..153f8d628 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -104,6 +104,7 @@ SET(SUBSURFACE_CORE_LIB_SRCS gettextfromc.cpp #dirk ported some core functionality to c++. qthelper.cpp + divecomputer.cpp ) #the interface, in C++ diff --git a/divecomputer.cpp b/divecomputer.cpp new file mode 100644 index 000000000..c02bbea55 --- /dev/null +++ b/divecomputer.cpp @@ -0,0 +1,187 @@ +#include "divecomputer.h" +#include "dive.h" + +#include <QSettings> + +const char *default_dive_computer_vendor; +const char *default_dive_computer_product; +const char *default_dive_computer_device; +DiveComputerList dcList; + +DiveComputerList::DiveComputerList() +{ +} + +DiveComputerList::~DiveComputerList() +{ +} + +bool DiveComputerNode::operator==(const DiveComputerNode &a) const +{ + return model == a.model && + deviceId == a.deviceId && + firmware == a.firmware && + serialNumber == a.serialNumber && + nickName == a.nickName; +} + +bool DiveComputerNode::operator!=(const DiveComputerNode &a) const +{ + return !(*this == a); +} + +bool DiveComputerNode::changesValues(const DiveComputerNode &b) const +{ + if (model != b.model || deviceId != b.deviceId) { + qDebug("DiveComputerNodes were not for the same DC"); + return false; + } + return (firmware != b.firmware) || + (serialNumber != b.serialNumber) || + (nickName != b.nickName); +} + +const DiveComputerNode *DiveComputerList::getExact(const QString& m, uint32_t d) +{ + for (QMap<QString, DiveComputerNode>::iterator it = dcMap.find(m); it != dcMap.end() && it.key() == m; ++it) + if (it->deviceId == d) + return &*it; + return NULL; +} + +const DiveComputerNode *DiveComputerList::get(const QString& m) +{ + QMap<QString, DiveComputerNode>::iterator it = dcMap.find(m); + if (it != dcMap.end()) + return &*it; + return NULL; +} + +void DiveComputerList::addDC(const QString& m, uint32_t d, const QString& n, const QString& s,const QString& f) +{ + if (m.isEmpty() || d == 0) + return; + const DiveComputerNode *existNode = this->getExact(m, d); + DiveComputerNode newNode(m, d, s, f, n); + if (existNode) { + if (newNode.changesValues(*existNode)) { + if (n.size() && existNode->nickName != n) + qDebug("new nickname %s for DC model %s deviceId 0x%x", n.toUtf8().data(), m.toUtf8().data(), d); + if (f.size() && existNode->firmware != f) + qDebug("new firmware version %s for DC model %s deviceId 0x%x", f.toUtf8().data(), m.toUtf8().data(), d); + if (s.size() && existNode->serialNumber != s) + qDebug("new serial number %s for DC model %s deviceId 0x%x", s.toUtf8().data(), m.toUtf8().data(), d); + } else { + return; + } + dcMap.remove(m, *existNode); + } + dcMap.insert(m, newNode); +} + +void DiveComputerList::rmDC(const QString& m, uint32_t d) +{ + const DiveComputerNode *existNode = this->getExact(m, d); + dcMap.remove(m, *existNode); +} + + +extern "C" void create_device_node(const char *model, uint32_t deviceid, const char *serial, const char *firmware, const char *nickname) +{ + dcList.addDC(model, deviceid, nickname, serial, firmware); +} + +extern "C" bool compareDC(const DiveComputerNode &a, const DiveComputerNode &b) +{ + return a.deviceId < b.deviceId; +} + +extern "C" void call_for_each_dc(void *f, void (*callback)(void *, const char *, uint32_t, + const char *, const char *, const char *)) +{ + QList<DiveComputerNode> values = dcList.dcMap.values(); + qSort(values.begin(), values.end(), compareDC); + for (int i = 0; i < values.size(); i++) { + const DiveComputerNode *node = &values.at(i); + callback(f, node->model.toUtf8().data(), node->deviceId, node->nickName.toUtf8().data(), + node->serialNumber.toUtf8().data(), node->firmware.toUtf8().data()); + } +} + + +extern "C" int is_default_dive_computer(const char *vendor, const char *product) +{ + return default_dive_computer_vendor && !strcmp(vendor, default_dive_computer_vendor) && + default_dive_computer_product && !strcmp(product, default_dive_computer_product); +} + +extern "C" int is_default_dive_computer_device(const char *name) +{ + return default_dive_computer_device && !strcmp(name, default_dive_computer_device); +} + +void set_default_dive_computer(const char *vendor, const char *product) +{ + QSettings s; + + if (!vendor || !*vendor) + return; + if (!product || !*product) + return; + if (is_default_dive_computer(vendor, product)) + return; + if (default_dive_computer_vendor) + free((void *)default_dive_computer_vendor); + if (default_dive_computer_product) + free((void *)default_dive_computer_product); + default_dive_computer_vendor = strdup(vendor); + default_dive_computer_product = strdup(product); + s.beginGroup("DiveComputer"); + s.setValue("dive_computer_vendor", vendor); + s.setValue("dive_computer_product", product); + s.endGroup(); +} + +void set_default_dive_computer_device(const char *name) +{ + QSettings s; + + if (!name || !*name) + return; + if (is_default_dive_computer_device(name)) + return; + if (default_dive_computer_device) + free((void *)default_dive_computer_device); + default_dive_computer_device = strdup(name); + s.beginGroup("DiveComputer"); + s.setValue("dive_computer_device", name); + s.endGroup(); +} + +extern "C" void set_dc_nickname(struct dive *dive) +{ + if (!dive) + return; + + struct divecomputer *dc = &dive->dc; + + while (dc) { + if (dc->model && *dc->model && dc->deviceid && + !dcList.getExact(dc->model, dc->deviceid)) { + // we don't have this one, yet + const DiveComputerNode *existNode = dcList.get(dc->model); + if (existNode) { + // we already have this model but a different deviceid + QString simpleNick(dc->model); + if (dc->deviceid == 0) + simpleNick.append(" (unknown deviceid)"); + else + simpleNick.append(" (").append(QString::number(dc->deviceid, 16)).append(")"); + dcList.addDC(dc->model, dc->deviceid, simpleNick); + } else { + dcList.addDC(dc->model, dc->deviceid); + } + } + dc = dc->next; + } +} diff --git a/divecomputer.h b/divecomputer.h new file mode 100644 index 000000000..e515a6b06 --- /dev/null +++ b/divecomputer.h @@ -0,0 +1,38 @@ +#ifndef DIVECOMPUTER_H +#define DIVECOMPUTER_H + +#include <QString> +#include <QMap> +#include <stdint.h> + +class DiveComputerNode { +public: + DiveComputerNode(QString m, uint32_t d, QString s, QString f, QString n) + : model(m), deviceId(d), serialNumber(s), firmware(f), nickName(n) {}; + bool operator==(const DiveComputerNode &a) const; + bool operator!=(const DiveComputerNode &a) const; + bool changesValues(const DiveComputerNode &b) const; + QString model; + uint32_t deviceId; + QString serialNumber; + QString firmware; + QString nickName; +}; + +class DiveComputerList { +public: + DiveComputerList(); + ~DiveComputerList(); + const DiveComputerNode *getExact(const QString& m, uint32_t d); + const DiveComputerNode *get(const QString& m); + void addDC(const QString& m, uint32_t d,const QString& n = QString(),const QString& s = QString(), const QString& f = QString()); + void rmDC(const QString& m, uint32_t d); + DiveComputerNode matchDC(const QString& m, uint32_t d); + DiveComputerNode matchModel(const QString& m); + QMultiMap<QString, DiveComputerNode> dcMap; + QMultiMap<QString, DiveComputerNode> dcWorkingMap; +}; + +extern DiveComputerList dcList; + +#endif
\ No newline at end of file @@ -33,8 +33,6 @@ QString get_dive_date_string(timestamp_t when); QString get_short_dive_date_string(timestamp_t when); QString get_trip_date_string(timestamp_t when, int nr); -extern DiveComputerList dcList; - #define M_OR_FT(_m, _f) ((prefs.units.length == units::METERS) ? ((_m) * 1000) : (feet_to_mm(_f))) #if defined __APPLE__ diff --git a/qt-ui/divelistview.cpp b/qt-ui/divelistview.cpp index 00906c6b4..6fd5dee10 100644 --- a/qt-ui/divelistview.cpp +++ b/qt-ui/divelistview.cpp @@ -25,7 +25,7 @@ #include <QFileDialog> #include <string> #include <iostream> - +#include "../qthelper.h" DiveListView::DiveListView(QWidget *parent) : QTreeView(parent), mouseClickSelection(false), sortColumn(0), currentOrder(Qt::DescendingOrder), searchBox(this) { diff --git a/qt-ui/downloadfromdivecomputer.cpp b/qt-ui/downloadfromdivecomputer.cpp index 4f93cefa3..bc829249c 100644 --- a/qt-ui/downloadfromdivecomputer.cpp +++ b/qt-ui/downloadfromdivecomputer.cpp @@ -1,9 +1,10 @@ #include "downloadfromdivecomputer.h" - +#include "../divecomputer.h" #include "../libdivecomputer.h" #include "../helpers.h" #include "../display.h" #include "../divelist.h" + #include "mainwindow.h" #include <cstdlib> #include <QThread> diff --git a/qt-ui/models.h b/qt-ui/models.h index d638e7de3..34d7d4827 100644 --- a/qt-ui/models.h +++ b/qt-ui/models.h @@ -14,7 +14,7 @@ #include "../dive.h" #include "../divelist.h" -#include "../qthelper.h" +#include "../divecomputer.h" QFont defaultModelFont(); diff --git a/qthelper.cpp b/qthelper.cpp index f056f5238..101530dba 100644 --- a/qthelper.cpp +++ b/qthelper.cpp @@ -10,87 +10,7 @@ #define tr(_arg) QObject::tr(_arg) -const char *default_dive_computer_vendor; -const char *default_dive_computer_product; -const char *default_dive_computer_device; -DiveComputerList dcList; -DiveComputerList::DiveComputerList() -{ -} - -DiveComputerList::~DiveComputerList() -{ -} - -bool DiveComputerNode::operator==(const DiveComputerNode &a) const -{ - return model == a.model && - deviceId == a.deviceId && - firmware == a.firmware && - serialNumber == a.serialNumber && - nickName == a.nickName; -} - -bool DiveComputerNode::operator!=(const DiveComputerNode &a) const -{ - return !(*this == a); -} - -bool DiveComputerNode::changesValues(const DiveComputerNode &b) const -{ - if (model != b.model || deviceId != b.deviceId) { - qDebug("DiveComputerNodes were not for the same DC"); - return false; - } - return (firmware != b.firmware) || - (serialNumber != b.serialNumber) || - (nickName != b.nickName); -} - -const DiveComputerNode *DiveComputerList::getExact(const QString& m, uint32_t d) -{ - for (QMap<QString, DiveComputerNode>::iterator it = dcMap.find(m); it != dcMap.end() && it.key() == m; ++it) - if (it->deviceId == d) - return &*it; - return NULL; -} - -const DiveComputerNode *DiveComputerList::get(const QString& m) -{ - QMap<QString, DiveComputerNode>::iterator it = dcMap.find(m); - if (it != dcMap.end()) - return &*it; - return NULL; -} - -void DiveComputerList::addDC(const QString& m, uint32_t d, const QString& n, const QString& s,const QString& f) -{ - if (m.isEmpty() || d == 0) - return; - const DiveComputerNode *existNode = this->getExact(m, d); - DiveComputerNode newNode(m, d, s, f, n); - if (existNode) { - if (newNode.changesValues(*existNode)) { - if (n.size() && existNode->nickName != n) - qDebug("new nickname %s for DC model %s deviceId 0x%x", n.toUtf8().data(), m.toUtf8().data(), d); - if (f.size() && existNode->firmware != f) - qDebug("new firmware version %s for DC model %s deviceId 0x%x", f.toUtf8().data(), m.toUtf8().data(), d); - if (s.size() && existNode->serialNumber != s) - qDebug("new serial number %s for DC model %s deviceId 0x%x", s.toUtf8().data(), m.toUtf8().data(), d); - } else { - return; - } - dcMap.remove(m, *existNode); - } - dcMap.insert(m, newNode); -} - -void DiveComputerList::rmDC(const QString& m, uint32_t d) -{ - const DiveComputerNode *existNode = this->getExact(m, d); - dcMap.remove(m, *existNode); -} QString weight_string(int weight_in_grams) { @@ -306,27 +226,6 @@ int dive_getUniqID(struct dive *d) return id; } -extern "C" void create_device_node(const char *model, uint32_t deviceid, const char *serial, const char *firmware, const char *nickname) -{ - dcList.addDC(model, deviceid, nickname, serial, firmware); -} - -extern "C" bool compareDC(const DiveComputerNode &a, const DiveComputerNode &b) -{ - return a.deviceId < b.deviceId; -} - -extern "C" void call_for_each_dc(void *f, void (*callback)(void *, const char *, uint32_t, - const char *, const char *, const char *)) -{ - QList<DiveComputerNode> values = dcList.dcMap.values(); - qSort(values.begin(), values.end(), compareDC); - for (int i = 0; i < values.size(); i++) { - const DiveComputerNode *node = &values.at(i); - callback(f, node->model.toUtf8().data(), node->deviceId, node->nickName.toUtf8().data(), - node->serialNumber.toUtf8().data(), node->firmware.toUtf8().data()); - } -} static xmlDocPtr get_stylesheet_doc(const xmlChar *uri, xmlDictPtr, int, void *, xsltLoadType) { @@ -360,80 +259,3 @@ extern "C" xsltStylesheetPtr get_stylesheet(const char *name) return xslt; } - -extern "C" int is_default_dive_computer(const char *vendor, const char *product) -{ - return default_dive_computer_vendor && !strcmp(vendor, default_dive_computer_vendor) && - default_dive_computer_product && !strcmp(product, default_dive_computer_product); -} - -extern "C" int is_default_dive_computer_device(const char *name) -{ - return default_dive_computer_device && !strcmp(name, default_dive_computer_device); -} - -void set_default_dive_computer(const char *vendor, const char *product) -{ - QSettings s; - - if (!vendor || !*vendor) - return; - if (!product || !*product) - return; - if (is_default_dive_computer(vendor, product)) - return; - if (default_dive_computer_vendor) - free((void *)default_dive_computer_vendor); - if (default_dive_computer_product) - free((void *)default_dive_computer_product); - default_dive_computer_vendor = strdup(vendor); - default_dive_computer_product = strdup(product); - s.beginGroup("DiveComputer"); - s.setValue("dive_computer_vendor", vendor); - s.setValue("dive_computer_product", product); - s.endGroup(); -} - -void set_default_dive_computer_device(const char *name) -{ - QSettings s; - - if (!name || !*name) - return; - if (is_default_dive_computer_device(name)) - return; - if (default_dive_computer_device) - free((void *)default_dive_computer_device); - default_dive_computer_device = strdup(name); - s.beginGroup("DiveComputer"); - s.setValue("dive_computer_device", name); - s.endGroup(); -} - -extern "C" void set_dc_nickname(struct dive *dive) -{ - if (!dive) - return; - - struct divecomputer *dc = &dive->dc; - - while (dc) { - if (dc->model && *dc->model && dc->deviceid && - !dcList.getExact(dc->model, dc->deviceid)) { - // we don't have this one, yet - const DiveComputerNode *existNode = dcList.get(dc->model); - if (existNode) { - // we already have this model but a different deviceid - QString simpleNick(dc->model); - if (dc->deviceid == 0) - simpleNick.append(" (unknown deviceid)"); - else - simpleNick.append(" (").append(QString::number(dc->deviceid, 16)).append(")"); - dcList.addDC(dc->model, dc->deviceid, simpleNick); - } else { - dcList.addDC(dc->model, dc->deviceid); - } - } - dc = dc->next; - } -} diff --git a/qthelper.h b/qthelper.h index 6067e1359..116bb0d3b 100644 --- a/qthelper.h +++ b/qthelper.h @@ -11,34 +11,6 @@ // global pointers for our translation extern QTranslator *qtTranslator, *ssrfTranslator; -class DiveComputerNode { -public: - DiveComputerNode(QString m, uint32_t d, QString s, QString f, QString n) - : model(m), deviceId(d), serialNumber(s), firmware(f), nickName(n) {}; - bool operator==(const DiveComputerNode &a) const; - bool operator!=(const DiveComputerNode &a) const; - bool changesValues(const DiveComputerNode &b) const; - QString model; - uint32_t deviceId; - QString serialNumber; - QString firmware; - QString nickName; -}; - -class DiveComputerList { -public: - DiveComputerList(); - ~DiveComputerList(); - const DiveComputerNode *getExact(const QString& m, uint32_t d); - const DiveComputerNode *get(const QString& m); - void addDC(const QString& m, uint32_t d,const QString& n = QString(),const QString& s = QString(), const QString& f = QString()); - void rmDC(const QString& m, uint32_t d); - DiveComputerNode matchDC(const QString& m, uint32_t d); - DiveComputerNode matchModel(const QString& m); - QMultiMap<QString, DiveComputerNode> dcMap; - QMultiMap<QString, DiveComputerNode> dcWorkingMap; -}; - QString weight_string(int weight_in_grams); bool gpsHasChanged(struct dive *dive, struct dive *master, const QString &gps_text, bool *parsed); QString printGPSCoords(int lat, int lon); diff --git a/subsurface.pro b/subsurface.pro index 829a459a4..7762318c5 100644 --- a/subsurface.pro +++ b/subsurface.pro @@ -39,6 +39,7 @@ HEADERS = \ profile.h \ qt-gui.h \ qthelper.h \ + divecomputer.h \ qt-ui/about.h \ qt-ui/completionmodels.h \ qt-ui/divecomputermanagementdialog.h \ @@ -105,6 +106,7 @@ SOURCES = \ parse-xml.c \ planner.c \ profile.c \ + divecomputer.cpp \ worldmap-save.c \ qt-gui.cpp \ qthelper.cpp \ |