summaryrefslogtreecommitdiffstats
path: root/qthelper.cpp
diff options
context:
space:
mode:
authorGravatar Tomaz Canabrava <tomaz.canabrava@intel.com>2014-04-17 12:21:39 -0300
committerGravatar Dirk Hohndel <dirk@hohndel.org>2014-04-17 08:46:29 -0700
commit55d010bad839b503cbfa759498d18690f4a809ae (patch)
tree9139b3108d7eefa8a6a0b73cd109a93b02812e66 /qthelper.cpp
parent95984628302bee02af3e82ac305b5e3bb4842533 (diff)
downloadsubsurface-55d010bad839b503cbfa759498d18690f4a809ae.tar.gz
Moved code around to make possible to compile the C part alone.
The C code should be compilable without the need to compile the Gui part, too. This is expecially good for unit testing as we can test all the algorithms without a window appearing out of nowhere. Signed-off-by: Tomaz Canabrava <tomaz.canabrava@intel.com> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'qthelper.cpp')
-rw-r--r--qthelper.cpp146
1 files changed, 143 insertions, 3 deletions
diff --git a/qthelper.cpp b/qthelper.cpp
index 8ad85402a..c8f4668ff 100644
--- a/qthelper.cpp
+++ b/qthelper.cpp
@@ -1,12 +1,19 @@
#include "qthelper.h"
-#include "qt-ui/mainwindow.h"
#include "qt-gui.h"
#include <QRegExp>
#include <QDir>
-#include <QFileDialog>
+
#include <QDebug>
+#include <QSettings>
+#include <libxslt/documents.h>
+
+#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;
-#define tr(_arg) MainWindow::instance()->information()->tr(_arg)
DiveComputerList::DiveComputerList()
{
}
@@ -265,3 +272,136 @@ int getUniqID(struct dive *d)
ids.insert(id);
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)
+{
+ QFile f(QLatin1String(":/xslt/") + (const char *)uri);
+ if (!f.open(QIODevice::ReadOnly))
+ return NULL;
+
+ /* Load and parse the data */
+ QByteArray source = f.readAll();
+
+ xmlDocPtr doc = xmlParseMemory(source, source.size());
+ return doc;
+}
+
+extern "C" xsltStylesheetPtr get_stylesheet(const char *name)
+{
+ // this needs to be done only once, but doesn't hurt to run every time
+ xsltSetLoaderFunc(get_stylesheet_doc);
+
+ // get main document:
+ xmlDocPtr doc = get_stylesheet_doc((const xmlChar *)name, NULL, 0, NULL, XSLT_LOAD_START);
+ if (!doc)
+ return NULL;
+
+ // xsltSetGenericErrorFunc(stderr, NULL);
+ xsltStylesheetPtr xslt = xsltParseStylesheetDoc(doc);
+ if (!xslt) {
+ xmlFreeDoc(doc);
+ return NULL;
+ }
+
+ 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;
+ }
+}