2014-05-30 06:56:27 +00:00
|
|
|
#include "configuredivecomputer.h"
|
|
|
|
#include "libdivecomputer/hw.h"
|
|
|
|
#include <QDebug>
|
2014-06-10 16:19:28 +00:00
|
|
|
#include <QFile>
|
2014-06-11 06:37:27 +00:00
|
|
|
#include <libxml/parser.h>
|
|
|
|
#include <libxml/parserInternals.h>
|
|
|
|
#include <libxml/tree.h>
|
|
|
|
#include <libxslt/transform.h>
|
|
|
|
#include <QStringList>
|
2014-06-10 16:19:28 +00:00
|
|
|
|
2014-05-30 06:56:27 +00:00
|
|
|
ConfigureDiveComputer::ConfigureDiveComputer(QObject *parent) :
|
|
|
|
QObject(parent),
|
2014-05-30 07:49:58 +00:00
|
|
|
readThread(0),
|
2014-06-10 15:25:25 +00:00
|
|
|
writeThread(0)
|
2014-05-30 06:56:27 +00:00
|
|
|
{
|
|
|
|
setState(INITIAL);
|
|
|
|
}
|
|
|
|
|
2014-06-10 15:25:25 +00:00
|
|
|
void ConfigureDiveComputer::readSettings(device_data_t *data)
|
2014-05-30 06:56:27 +00:00
|
|
|
{
|
|
|
|
setState(READING);
|
|
|
|
|
|
|
|
if (readThread)
|
|
|
|
readThread->deleteLater();
|
|
|
|
|
2014-06-10 15:25:25 +00:00
|
|
|
readThread = new ReadSettingsThread(this, data);
|
2014-05-30 06:56:27 +00:00
|
|
|
connect (readThread, SIGNAL(finished()),
|
|
|
|
this, SLOT(readThreadFinished()), Qt::QueuedConnection);
|
|
|
|
connect (readThread, SIGNAL(error(QString)), this, SLOT(setError(QString)));
|
2014-06-10 15:25:25 +00:00
|
|
|
connect (readThread, SIGNAL(devicedetails(DeviceDetails*)), this,
|
|
|
|
SIGNAL(deviceDetailsChanged(DeviceDetails*)));
|
2014-05-30 06:56:27 +00:00
|
|
|
|
|
|
|
readThread->start();
|
|
|
|
}
|
|
|
|
|
2014-06-10 15:25:25 +00:00
|
|
|
void ConfigureDiveComputer::saveDeviceDetails(DeviceDetails *details, device_data_t *data)
|
2014-05-30 07:49:58 +00:00
|
|
|
{
|
2014-06-10 15:25:25 +00:00
|
|
|
setState(WRITING);
|
2014-05-30 07:49:58 +00:00
|
|
|
|
2014-06-10 15:25:25 +00:00
|
|
|
if (writeThread)
|
|
|
|
writeThread->deleteLater();
|
|
|
|
|
|
|
|
writeThread = new WriteSettingsThread(this, data);
|
|
|
|
connect (writeThread, SIGNAL(finished()),
|
|
|
|
this, SLOT(writeThreadFinished()), Qt::QueuedConnection);
|
|
|
|
connect (writeThread, SIGNAL(error(QString)), this, SLOT(setError(QString)));
|
|
|
|
|
|
|
|
writeThread->setDeviceDetails(details);
|
|
|
|
writeThread->start();
|
2014-06-07 18:56:44 +00:00
|
|
|
}
|
|
|
|
|
2014-06-10 16:19:28 +00:00
|
|
|
bool ConfigureDiveComputer::saveXMLBackup(QString fileName, DeviceDetails *details, device_data_t *data, QString errorText)
|
|
|
|
{
|
|
|
|
QString xml = "";
|
|
|
|
QString vendor = data->vendor;
|
|
|
|
QString product = data->product;
|
2014-06-11 06:37:27 +00:00
|
|
|
xml += "<DiveComputerSettingsBackup>";
|
|
|
|
xml += "\n<DiveComputer>";
|
|
|
|
xml += addSettingToXML("Vendor", vendor);
|
|
|
|
xml += addSettingToXML("Product", product);
|
|
|
|
xml += "\n</DiveComputer>";
|
|
|
|
xml += "\n<Settings>";
|
|
|
|
xml += addSettingToXML("CustomText", details->customText());
|
2014-06-21 06:31:19 +00:00
|
|
|
//Add gasses
|
|
|
|
QString gas1 = QString("%1,%2,%3,%4")
|
|
|
|
.arg(QString::number(details->gas1().oxygen),
|
|
|
|
QString::number(details->gas1().helium),
|
|
|
|
QString::number(details->gas1().type),
|
|
|
|
QString::number(details->gas1().depth)
|
|
|
|
);
|
|
|
|
QString gas2 = QString("%1,%2,%3,%4")
|
|
|
|
.arg(QString::number(details->gas2().oxygen),
|
|
|
|
QString::number(details->gas2().helium),
|
|
|
|
QString::number(details->gas2().type),
|
|
|
|
QString::number(details->gas2().depth)
|
|
|
|
);
|
|
|
|
QString gas3 = QString("%1,%2,%3,%4")
|
|
|
|
.arg(QString::number(details->gas3().oxygen),
|
|
|
|
QString::number(details->gas3().helium),
|
|
|
|
QString::number(details->gas3().type),
|
|
|
|
QString::number(details->gas3().depth)
|
|
|
|
);
|
|
|
|
QString gas4 = QString("%1,%2,%3,%4")
|
|
|
|
.arg(QString::number(details->gas4().oxygen),
|
|
|
|
QString::number(details->gas4().helium),
|
|
|
|
QString::number(details->gas4().type),
|
|
|
|
QString::number(details->gas4().depth)
|
|
|
|
);
|
|
|
|
QString gas5 = QString("%1,%2,%3,%4")
|
|
|
|
.arg(QString::number(details->gas5().oxygen),
|
|
|
|
QString::number(details->gas5().helium),
|
|
|
|
QString::number(details->gas5().type),
|
|
|
|
QString::number(details->gas5().depth)
|
|
|
|
);
|
|
|
|
xml += addSettingToXML("Gas1", gas1);
|
|
|
|
xml += addSettingToXML("Gas2", gas2);
|
|
|
|
xml += addSettingToXML("Gas3", gas3);
|
|
|
|
xml += addSettingToXML("Gas4", gas4);
|
|
|
|
xml += addSettingToXML("Gas5", gas5);
|
|
|
|
//
|
2014-06-21 06:53:05 +00:00
|
|
|
//Add dil values
|
|
|
|
QString dil1 = QString("%1,%2,%3,%4")
|
|
|
|
.arg(QString::number(details->dil1().oxygen),
|
|
|
|
QString::number(details->dil1().helium),
|
|
|
|
QString::number(details->dil1().type),
|
|
|
|
QString::number(details->dil1().depth)
|
|
|
|
);
|
|
|
|
QString dil2 = QString("%1,%2,%3,%4")
|
|
|
|
.arg(QString::number(details->dil2().oxygen),
|
|
|
|
QString::number(details->dil2().helium),
|
|
|
|
QString::number(details->dil2().type),
|
|
|
|
QString::number(details->dil2().depth)
|
|
|
|
);
|
|
|
|
QString dil3 = QString("%1,%2,%3,%4")
|
|
|
|
.arg(QString::number(details->dil3().oxygen),
|
|
|
|
QString::number(details->dil3().helium),
|
|
|
|
QString::number(details->dil3().type),
|
|
|
|
QString::number(details->dil3().depth)
|
|
|
|
);
|
|
|
|
QString dil4 = QString("%1,%2,%3,%4")
|
|
|
|
.arg(QString::number(details->dil4().oxygen),
|
|
|
|
QString::number(details->dil4().helium),
|
|
|
|
QString::number(details->dil4().type),
|
|
|
|
QString::number(details->dil4().depth)
|
|
|
|
);
|
|
|
|
QString dil5 = QString("%1,%2,%3,%4")
|
|
|
|
.arg(QString::number(details->dil5().oxygen),
|
|
|
|
QString::number(details->dil5().helium),
|
|
|
|
QString::number(details->dil5().type),
|
|
|
|
QString::number(details->dil5().depth)
|
|
|
|
);
|
|
|
|
xml += addSettingToXML("Dil1", dil1);
|
|
|
|
xml += addSettingToXML("Dil2", dil2);
|
|
|
|
xml += addSettingToXML("Dil3", dil3);
|
|
|
|
xml += addSettingToXML("Dil4", dil4);
|
|
|
|
xml += addSettingToXML("Dil5", dil5);
|
|
|
|
//
|
2014-06-13 14:10:10 +00:00
|
|
|
xml += addSettingToXML("DiveMode", details->diveMode());
|
|
|
|
xml += addSettingToXML("Saturation", details->saturation());
|
2014-06-20 04:51:32 +00:00
|
|
|
xml += addSettingToXML("Desaturation", details->desaturation());
|
2014-06-11 08:09:01 +00:00
|
|
|
xml += addSettingToXML("LastDeco", details->lastDeco());
|
2014-06-11 06:37:27 +00:00
|
|
|
xml += addSettingToXML("Brightness", details->brightness());
|
2014-06-11 08:09:01 +00:00
|
|
|
xml += addSettingToXML("Units", details->units());
|
|
|
|
xml += addSettingToXML("SamplingRate", details->samplingRate());
|
|
|
|
xml += addSettingToXML("Salinity", details->salinity());
|
|
|
|
xml += addSettingToXML("DiveModeColor", details->diveModeColor());
|
2014-06-11 06:37:27 +00:00
|
|
|
xml += addSettingToXML("Language", details->language());
|
|
|
|
xml += addSettingToXML("DateFormat", details->dateFormat());
|
2014-06-11 08:09:01 +00:00
|
|
|
xml += addSettingToXML("CompassGain", details->compassGain());
|
2014-06-11 06:37:27 +00:00
|
|
|
xml += "\n</Settings>";
|
|
|
|
xml += "\n</DiveComputerSettingsBackup>";
|
2014-06-10 16:19:28 +00:00
|
|
|
QFile file(fileName);
|
|
|
|
if (!file.open(QIODevice::WriteOnly)) {
|
|
|
|
errorText = tr("Could not save the backup file %1. Error Message: %2")
|
|
|
|
.arg(fileName, file.errorString());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
//file open successful. write data and save.
|
|
|
|
QTextStream out(&file);
|
|
|
|
out << xml;
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-11 06:37:27 +00:00
|
|
|
bool ConfigureDiveComputer::restoreXMLBackup(QString fileName, DeviceDetails *details, QString errorText)
|
|
|
|
{
|
|
|
|
xmlDocPtr doc;
|
|
|
|
xmlNodePtr node;
|
|
|
|
xmlChar *key;
|
|
|
|
|
|
|
|
doc = xmlParseFile(fileName.toUtf8().data());
|
|
|
|
|
|
|
|
if (doc == NULL) {
|
|
|
|
errorText = tr("Could not read the backup file.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
node = xmlDocGetRootElement(doc);
|
|
|
|
if (node == NULL) {
|
|
|
|
errorText = tr("The specified file is invalid.");
|
|
|
|
xmlFreeDoc(doc);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xmlStrcmp(node->name, (const xmlChar *) "DiveComputerSettingsBackup")) {
|
|
|
|
errorText = tr("The specified file does not contain a valid backup.");
|
|
|
|
xmlFreeDoc(doc);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
xmlNodePtr child = node->children;
|
|
|
|
|
|
|
|
while (child != NULL) {
|
|
|
|
QString nodeName = (char *)child->name;
|
|
|
|
if (nodeName == "Settings") {
|
|
|
|
xmlNodePtr settingNode = child->children;
|
|
|
|
while (settingNode != NULL) {
|
|
|
|
QString settingName = (char *)settingNode->name;
|
|
|
|
key = xmlNodeListGetString(doc, settingNode->xmlChildrenNode, 1);
|
|
|
|
QString keyString = (char *)key;
|
|
|
|
if (settingName != "text") {
|
|
|
|
if (settingName == "CustomText")
|
|
|
|
details->setCustomText(keyString);
|
|
|
|
|
2014-06-21 06:31:19 +00:00
|
|
|
if (settingName == "Gas1") {
|
|
|
|
QStringList gasData = keyString.split(",");
|
|
|
|
gas gas1;
|
|
|
|
gas1.oxygen = gasData.at(0).toInt();
|
|
|
|
gas1.helium = gasData.at(1).toInt();
|
|
|
|
gas1.type = gasData.at(2).toInt();
|
|
|
|
gas1.depth = gasData.at(3).toInt();
|
|
|
|
details->setGas1(gas1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (settingName == "Gas2") {
|
|
|
|
QStringList gasData = keyString.split(",");
|
|
|
|
gas gas2;
|
|
|
|
gas2.oxygen = gasData.at(0).toInt();
|
|
|
|
gas2.helium = gasData.at(1).toInt();
|
|
|
|
gas2.type = gasData.at(2).toInt();
|
|
|
|
gas2.depth = gasData.at(3).toInt();
|
|
|
|
details->setGas1(gas2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (settingName == "Gas3") {
|
|
|
|
QStringList gasData = keyString.split(",");
|
|
|
|
gas gas3;
|
|
|
|
gas3.oxygen = gasData.at(0).toInt();
|
|
|
|
gas3.helium = gasData.at(1).toInt();
|
|
|
|
gas3.type = gasData.at(2).toInt();
|
|
|
|
gas3.depth = gasData.at(3).toInt();
|
|
|
|
details->setGas3(gas3);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (settingName == "Gas4") {
|
|
|
|
QStringList gasData = keyString.split(",");
|
|
|
|
gas gas4;
|
|
|
|
gas4.oxygen = gasData.at(0).toInt();
|
|
|
|
gas4.helium = gasData.at(1).toInt();
|
|
|
|
gas4.type = gasData.at(2).toInt();
|
|
|
|
gas4.depth = gasData.at(3).toInt();
|
|
|
|
details->setGas4(gas4);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (settingName == "Gas5") {
|
|
|
|
QStringList gasData = keyString.split(",");
|
|
|
|
gas gas5;
|
|
|
|
gas5.oxygen = gasData.at(0).toInt();
|
|
|
|
gas5.helium = gasData.at(1).toInt();
|
|
|
|
gas5.type = gasData.at(2).toInt();
|
|
|
|
gas5.depth = gasData.at(3).toInt();
|
|
|
|
details->setGas5(gas5);
|
|
|
|
}
|
|
|
|
|
2014-06-21 06:53:05 +00:00
|
|
|
if (settingName == "Dil1") {
|
|
|
|
QStringList dilData = keyString.split(",");
|
|
|
|
gas dil1;
|
|
|
|
dil1.oxygen = dilData.at(0).toInt();
|
|
|
|
dil1.helium = dilData.at(1).toInt();
|
|
|
|
dil1.type = dilData.at(2).toInt();
|
|
|
|
dil1.depth = dilData.at(3).toInt();
|
|
|
|
details->setDil1(dil1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (settingName == "Dil2") {
|
|
|
|
QStringList dilData = keyString.split(",");
|
|
|
|
gas dil2;
|
|
|
|
dil2.oxygen = dilData.at(0).toInt();
|
|
|
|
dil2.helium = dilData.at(1).toInt();
|
|
|
|
dil2.type = dilData.at(2).toInt();
|
|
|
|
dil2.depth = dilData.at(3).toInt();
|
|
|
|
details->setDil1(dil2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (settingName == "Dil3") {
|
|
|
|
QStringList dilData = keyString.split(",");
|
|
|
|
gas dil3;
|
|
|
|
dil3.oxygen = dilData.at(0).toInt();
|
|
|
|
dil3.helium = dilData.at(1).toInt();
|
|
|
|
dil3.type = dilData.at(2).toInt();
|
|
|
|
dil3.depth = dilData.at(3).toInt();
|
|
|
|
details->setDil3(dil3);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (settingName == "Dil4") {
|
|
|
|
QStringList dilData = keyString.split(",");
|
|
|
|
gas dil4;
|
|
|
|
dil4.oxygen = dilData.at(0).toInt();
|
|
|
|
dil4.helium = dilData.at(1).toInt();
|
|
|
|
dil4.type = dilData.at(2).toInt();
|
|
|
|
dil4.depth = dilData.at(3).toInt();
|
|
|
|
details->setDil4(dil4);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (settingName == "Dil5") {
|
|
|
|
QStringList dilData = keyString.split(",");
|
|
|
|
gas dil5;
|
|
|
|
dil5.oxygen = dilData.at(0).toInt();
|
|
|
|
dil5.helium = dilData.at(1).toInt();
|
|
|
|
dil5.type = dilData.at(2).toInt();
|
|
|
|
dil5.depth = dilData.at(3).toInt();
|
|
|
|
details->setDil5(dil5);
|
|
|
|
}
|
|
|
|
|
2014-06-13 14:10:10 +00:00
|
|
|
if (settingName == "Saturation")
|
|
|
|
details->setSaturation(keyString.toInt());
|
|
|
|
|
2014-06-20 04:51:32 +00:00
|
|
|
if (settingName == "Desaturation")
|
|
|
|
details->setDesaturation(keyString.toInt());
|
|
|
|
|
2014-06-13 14:10:10 +00:00
|
|
|
if (settingName == "DiveMode")
|
|
|
|
details->setDiveMode(keyString.toInt());
|
|
|
|
|
2014-06-11 08:09:01 +00:00
|
|
|
if (settingName == "LastDeco")
|
|
|
|
details->setLastDeco(keyString.toInt());
|
|
|
|
|
2014-06-11 06:37:27 +00:00
|
|
|
if (settingName == "Brightness")
|
|
|
|
details->setBrightness(keyString.toInt());
|
|
|
|
|
2014-06-11 08:09:01 +00:00
|
|
|
if (settingName == "Units")
|
|
|
|
details->setUnits(keyString.toInt());
|
|
|
|
|
|
|
|
if (settingName == "SamplingRate")
|
|
|
|
details->setSamplingRate(keyString.toInt());
|
|
|
|
|
|
|
|
if (settingName == "Salinity")
|
|
|
|
details->setSalinity(keyString.toInt());
|
|
|
|
|
|
|
|
if (settingName == "DiveModeColour")
|
|
|
|
details->setDiveModeColor(keyString.toInt());
|
|
|
|
|
2014-06-11 06:37:27 +00:00
|
|
|
if (settingName == "Language")
|
|
|
|
details->setLanguage(keyString.toInt());
|
|
|
|
|
|
|
|
if (settingName == "DateFormat")
|
|
|
|
details->setDateFormat(keyString.toInt());
|
2014-06-11 08:09:01 +00:00
|
|
|
|
|
|
|
if (settingName == "CompassGain")
|
|
|
|
details->setCompassGain(keyString.toInt());
|
2014-06-11 06:37:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
settingNode = settingNode->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
child = child->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-30 06:56:27 +00:00
|
|
|
void ConfigureDiveComputer::setState(ConfigureDiveComputer::states newState)
|
|
|
|
{
|
|
|
|
currentState = newState;
|
|
|
|
emit stateChanged(currentState);
|
|
|
|
}
|
|
|
|
|
2014-06-11 06:37:27 +00:00
|
|
|
|
|
|
|
QString ConfigureDiveComputer::addSettingToXML(QString settingName, QVariant value)
|
|
|
|
{
|
|
|
|
return "\n<" + settingName + ">" + value.toString() + "</" + settingName + ">";
|
|
|
|
}
|
|
|
|
|
2014-05-30 06:56:27 +00:00
|
|
|
void ConfigureDiveComputer::setError(QString err)
|
|
|
|
{
|
|
|
|
lastError = err;
|
|
|
|
emit error(err);
|
|
|
|
}
|
|
|
|
|
2014-05-30 07:49:58 +00:00
|
|
|
void ConfigureDiveComputer::readThreadFinished()
|
2014-05-30 06:56:27 +00:00
|
|
|
{
|
2014-05-30 07:49:58 +00:00
|
|
|
setState(DONE);
|
2014-06-10 12:03:26 +00:00
|
|
|
emit readFinished();
|
2014-05-30 06:56:27 +00:00
|
|
|
}
|
|
|
|
|
2014-05-30 07:49:58 +00:00
|
|
|
void ConfigureDiveComputer::writeThreadFinished()
|
2014-05-30 06:56:27 +00:00
|
|
|
{
|
|
|
|
setState(DONE);
|
2014-05-30 07:49:58 +00:00
|
|
|
if (writeThread->lastError.isEmpty()) {
|
|
|
|
//No error
|
|
|
|
emit message(tr("Setting successfully written to device"));
|
|
|
|
}
|
2014-05-30 06:56:27 +00:00
|
|
|
}
|