two more, this makes the core compile and link against a test app.
On Wed, Apr 16, 2014 at 11:56 PM, Tomaz Canabrava <tcanabr...@kde.org>wrote: > More TODO's on the code: > > important thing: All the .c files should compile and link against a test > app, this isn't happening right now because some of the C stuff depends on > GUI ( it was way worse on the gtk version, but it's still bad. ) > > > > On Wed, Apr 16, 2014 at 10:57 PM, Tomaz Canabrava <tcanabr...@kde.org>wrote: > >> Dirk, >> >> Read this patch carefully, if I got anything wrong here I can crash the >> app, or worse, make your cat pregnant. >> >> This is an important patch regarding to the 'unittest' feature: it starts >> to decouple the app from the tangled web that it is today. >> >> @all: I added a lot of TODO's on the code, I'll work on them in due time >> ( and by due time I mean "help me finish that" ) >> >> Thanks, >> >> Tomaz >> > >
From a347b3c63d0738ec00fd5aeb8392a95037b4240a Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava <tomaz.canabr...@intel.com> Date: Thu, 17 Apr 2014 11:34:21 -0300 Subject: [PATCH 3/4] Removed the globals 'userid' and 'save_userid_local' variables This is a preferences setting, it should belong to the preferences structure. Signed-off-by: Tomaz Canabrava <tomaz.canabr...@intel.com> --- CMakeLists.txt | 7 ++++++- dive.c | 15 +++++++++++++++ dive.h | 3 --- parse-xml.c | 2 +- pref.h | 2 ++ qt-ui/subsurfacewebservices.cpp | 25 ++++--------------------- qt-ui/subsurfacewebservices.h | 2 -- save-git.c | 4 ++-- save-xml.c | 4 ++-- 9 files changed, 32 insertions(+), 32 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 69d9949..95872d1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -100,6 +100,8 @@ SET(SUBSURFACE_CORE_LIB_SRCS uemis.c uemis-downloader.c linux.c + #gettextfrommoc should be added because we are using it on the c-code. + gettextfromc.cpp ) #the interface, in C++ @@ -152,7 +154,6 @@ SET(SUBSURFACE_PROFILE_LIB_SRCS #the main app. SET(SUBSURFACE_APP main.cpp - gettextfromc.cpp qt-gui.cpp qthelper.cpp ) @@ -193,3 +194,7 @@ ENABLE_TESTING() ADD_EXECUTABLE( TestUnitConversion tests/testunitconversion.cpp ) TARGET_LINK_LIBRARIES( TestUnitConversion ${QT_LIBRARIES}) ADD_TEST( NAME TestUnitConversion COMMAND TestUnitConversion) + +ADD_EXECUTABLE( TestProfile tests/testprofile.cpp ) +TARGET_LINK_LIBRARIES( TestProfile ${QT_LIBRARIES} ${SUBSURFACE_LINK_LIBRARIES} -lzip -ldivecomputer subsurface_corelib) +ADD_TEST( NAME TestProfile COMMAND TestProfile) \ No newline at end of file diff --git a/dive.c b/dive.c index 535a4ed..543da51 100644 --- a/dive.c +++ b/dive.c @@ -2,6 +2,7 @@ /* maintains the internal dive list structure */ #include <string.h> #include <stdio.h> +#include <stdlib.h> #include <limits.h> #include "gettext.h" #include "dive.h" @@ -2218,3 +2219,17 @@ void merge_selected_dives() } } } + +#define MAX_USERID_SIZE 32 +void set_save_userid_local(short value) +{ + prefs.save_userid_local = value; +} + +void set_userid(char *rUserId) +{ + prefs.userid = (char *) malloc(MAX_USERID_SIZE); + if (prefs.userid && rUserId) + strcpy(prefs.userid, rUserId); +} +#undef MAX_USERID_SIZE \ No newline at end of file diff --git a/dive.h b/dive.h index c658083..b0a690a 100644 --- a/dive.h +++ b/dive.h @@ -895,9 +895,6 @@ extern double strtod_flags(const char *str, const char **ptr, unsigned int flags #define ascii_strtod(str, ptr) strtod_flags(str, ptr, STRTOD_ASCII) -extern short save_userid_local; -extern char* userid; - extern void set_save_userid_local(short value); extern void set_userid(char* user_id); diff --git a/parse-xml.c b/parse-xml.c index 966e567..f6806d2 100644 --- a/parse-xml.c +++ b/parse-xml.c @@ -861,7 +861,7 @@ static void try_to_fill_sample(struct sample *sample, const char *name, char *bu void try_to_fill_userid(const char *name, char *buf) { - if (save_userid_local) + if (prefs.save_userid_local) set_userid(buf); } diff --git a/pref.h b/pref.h index fd7aa82..6ee02a3 100644 --- a/pref.h +++ b/pref.h @@ -43,6 +43,8 @@ struct preferences { short show_average_depth; short zoomed_plot; short hrgraph; + short save_userid_local; + char *userid; }; enum unit_system_values { METRIC, diff --git a/qt-ui/subsurfacewebservices.cpp b/qt-ui/subsurfacewebservices.cpp index feb98ba..084e38b 100644 --- a/qt-ui/subsurfacewebservices.cpp +++ b/qt-ui/subsurfacewebservices.cpp @@ -313,16 +313,16 @@ void WebServices::resetState() SubsurfaceWebServices::SubsurfaceWebServices(QWidget *parent, Qt::WindowFlags f) : WebServices(parent, f) { QSettings s; - if (!save_userid_local || !*userid) + if (!prefs.save_userid_local || !*prefs.userid) ui.userID->setText(s.value("subsurface_webservice_uid").toString().toUpper()); else - ui.userID->setText(userid); + ui.userID->setText(prefs.userid); hidePassword(); hideUpload(); ui.progressBar->setFormat("Enter User ID and click Download"); ui.progressBar->setRange(0, 1); ui.progressBar->setValue(-1); - ui.saveUidLocal->setChecked(save_userid_local); + ui.saveUidLocal->setChecked(prefs.save_userid_local); } void SubsurfaceWebServices::buttonClicked(QAbstractButton *button) @@ -353,7 +353,7 @@ void SubsurfaceWebServices::buttonClicked(QAbstractButton *button) set_save_userid_local(qSaveUid); if (qSaveUid) { QString qSettingUid = s.value("subsurface_webservice_uid").toString(); - QString qFileUid = QString::fromStdString(userid); + QString qFileUid = QString(prefs.userid); bool s_eq_d = (qSettingUid == qDialogUid); bool d_eq_f = (qDialogUid == qFileUid); if (!d_eq_f || s_eq_d) @@ -905,20 +905,3 @@ void DivelogsDeWebServices::buttonClicked(QAbstractButton *button) break; } } - -#define MAX_USERID_SIZE 32 -short save_userid_local = false; -char *userid = NULL; -void set_save_userid_local(short value) -{ - QSettings s; - s.setValue("save_uid_local", value); - save_userid_local = value; -} - -void set_userid(char *rUserId) -{ - userid = (char *) malloc(MAX_USERID_SIZE); - if (userid && rUserId) - strcpy(userid, rUserId); -} diff --git a/qt-ui/subsurfacewebservices.h b/qt-ui/subsurfacewebservices.h index bcd56e9..10ab01a 100644 --- a/qt-ui/subsurfacewebservices.h +++ b/qt-ui/subsurfacewebservices.h @@ -99,8 +99,6 @@ private: #ifdef __cplusplus extern "C" { #endif -extern short save_userid_local; -extern char *userid; extern void set_save_userid_local(short value); extern void set_userid(char *user_id); #ifdef __cplusplus diff --git a/save-git.c b/save-git.c index 76175a1..e418b98 100644 --- a/save-git.c +++ b/save-git.c @@ -681,8 +681,8 @@ static struct dir *mktree(struct dir *dir, const char *fmt, ...) static void save_userid(void *_b) { struct membuffer *b = _b; - if (save_userid_local) - put_format(b, "userid %30s", userid); + if (prefs.save_userid_local) + put_format(b, "userid %30s", prefs.userid); } static void save_one_device(void *_b, const char *model, uint32_t deviceid, diff --git a/save-xml.c b/save-xml.c index 0aed920..4ad03db 100644 --- a/save-xml.c +++ b/save-xml.c @@ -508,8 +508,8 @@ void save_dives_buffer(struct membuffer *b, const bool select_only) put_format(b, "<divelog program='subsurface' version='%d'>\n<settings>\n", VERSION); - if (save_userid_local) - put_format(b, " <userid>%s</userid>\n", userid); + if (prefs.save_userid_local) + put_format(b, " <userid>%s</userid>\n", prefs.userid); /* save the dive computer nicknames, if any */ call_for_each_dc(b, save_one_device); -- 1.9.2
From 9715fe2285f5f9d6ee0f023d276fdf5bdfebfefe Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava <tomaz.canabr...@intel.com> Date: Thu, 17 Apr 2014 12:21:39 -0300 Subject: [PATCH 4/4] 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. I'v tested the app and it seems to behave ok, but the test wasn't extensive so, something could have gone wrong. Signed-off-by: Tomaz Canabrava <tomaz.canabr...@intel.com> --- CMakeLists.txt | 4 +- qt-gui.cpp | 139 ----------------------------------------- qthelper.cpp | 144 ++++++++++++++++++++++++++++++++++++++++++- tests/testprofile.cpp | 9 +++ tests/testprofile.h | 12 ++++ tests/testunitconversion.cpp | 1 + 6 files changed, 167 insertions(+), 142 deletions(-) create mode 100644 tests/testprofile.cpp create mode 100644 tests/testprofile.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 95872d1..ba318ca 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -102,6 +102,8 @@ SET(SUBSURFACE_CORE_LIB_SRCS linux.c #gettextfrommoc should be added because we are using it on the c-code. gettextfromc.cpp + #dirk ported some core functionality to c++. + qthelper.cpp ) #the interface, in C++ @@ -192,7 +194,7 @@ ADD_DEPENDENCIES(subsurface_corelib version) ENABLE_TESTING() ADD_EXECUTABLE( TestUnitConversion tests/testunitconversion.cpp ) -TARGET_LINK_LIBRARIES( TestUnitConversion ${QT_LIBRARIES}) +TARGET_LINK_LIBRARIES( TestUnitConversion ${QT_LIBRARIES} ${SUBSURFACE_LINK_LIBRARIES} -lzip -ldivecomputer subsurface_corelib) ADD_TEST( NAME TestUnitConversion COMMAND TestUnitConversion) ADD_EXECUTABLE( TestProfile tests/testprofile.cpp ) diff --git a/qt-gui.cpp b/qt-gui.cpp index ce25910..7e3bc13 100644 --- a/qt-gui.cpp +++ b/qt-gui.cpp @@ -9,8 +9,6 @@ #include <sys/time.h> #include <ctype.h> -#include <libxslt/documents.h> - #include "dive.h" #include "divelist.h" #include "display.h" @@ -45,11 +43,6 @@ // this will create a warning when executing lupdate #define translate(_context, arg) QObject::tr(arg) -const char *default_dive_computer_vendor; -const char *default_dive_computer_product; -const char *default_dive_computer_device; -DiveComputerList dcList; - static QApplication *application = NULL; static MainWindow *window = NULL; @@ -198,34 +191,6 @@ const QString get_dc_nickname(const char *model, uint32_t deviceid) return model; } -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; - } -} - QString get_depth_string(int mm, bool showunit, bool showdecimal) { if (prefs.units.length == units::METERS) { @@ -359,55 +324,6 @@ double get_screen_dpi() return mydesk->physicalDpiX(); } -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); -} - -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(); -} - QString getSubsurfaceDataPath(QString folderToFind) { QString execdir; @@ -441,28 +357,6 @@ QString getSubsurfaceDataPath(QString folderToFind) return QString(""); } -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); -} - -bool compareDC(const DiveComputerNode &a, const DiveComputerNode &b) -{ - return a.deviceId < b.deviceId; -} - -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()); - } -} - int gettimezoneoffset() { QDateTime dt1 = QDateTime::currentDateTime(); @@ -531,36 +425,3 @@ QString get_trip_date_string(timestamp_t when, int nr) .arg(monthname(tm.tm_mon)) .arg(tm.tm_year + 1900); } - -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; -} - -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; -} diff --git a/qthelper.cpp b/qthelper.cpp index 4035347..c7ad117 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; + 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; + } +} \ No newline at end of file diff --git a/tests/testprofile.cpp b/tests/testprofile.cpp new file mode 100644 index 0000000..d8aea3c --- /dev/null +++ b/tests/testprofile.cpp @@ -0,0 +1,9 @@ +#include "testprofile.h" +#include "dive.h" + +void TestProfile::testRedCeiling() +{ + parse_file("../dives/deep.xml"); +} + +QTEST_MAIN(TestProfile) \ No newline at end of file diff --git a/tests/testprofile.h b/tests/testprofile.h new file mode 100644 index 0000000..25f094b --- /dev/null +++ b/tests/testprofile.h @@ -0,0 +1,12 @@ +#ifndef TESTPROFILE_H +#define TESTPROFILE_H + +#include <QtTest> + +class TestProfile : public QObject{ + Q_OBJECT +private slots: + void testRedCeiling(); +}; + +#endif diff --git a/tests/testunitconversion.cpp b/tests/testunitconversion.cpp index b4d0406..3adf975 100644 --- a/tests/testunitconversion.cpp +++ b/tests/testunitconversion.cpp @@ -20,6 +20,7 @@ void TestUnitConversion::testUnitConversions() QCOMPARE(IS_FP_SAME(bar_to_atm(1.013), 1), true); QCOMPARE(IS_FP_SAME(mbar_to_atm(1013), 1), true); QCOMPARE(mbar_to_PSI(1013), (int)15); + get_units(); } QTEST_MAIN(TestUnitConversion) -- 1.9.2
_______________________________________________ subsurface mailing list subsurface@hohndel.org http://lists.hohndel.org/cgi-bin/mailman/listinfo/subsurface