summaryrefslogtreecommitdiffstats
path: root/qt-ui/configuredivecomputer.cpp
blob: 01df1c744b65a43b1ce8b271379cf8c30bc1bec8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "configuredivecomputer.h"
#include "libdivecomputer/hw.h"
#include <QDebug>
#include <QFile>
#include <libxml/parser.h>
#include <libxml/parserInternals.h>
#include <libxml/tree.h>
#include <libxslt/transform.h>
#include <QStringList>

ConfigureDiveComputer::ConfigureDiveComputer(QObject *parent) :
	QObject(parent),
	readThread(0),
	writeThread(0)
{
	setState(INITIAL);
}

void ConfigureDiveComputer::readSettings(device_data_t *data)
{
	setState(READING);

	if (readThread)
		readThread->deleteLater();

	readThread = new ReadSettingsThread(this, data);
	connect (readThread, SIGNAL(finished()),
		 this, SLOT(readThreadFinished()), Qt::QueuedConnection);
	connect (readThread, SIGNAL(error(QString)), this, SLOT(setError(QString)));
	connect (readThread, SIGNAL(devicedetails(DeviceDetails*)), this,
		 SIGNAL(deviceDetailsChanged(DeviceDetails*)));

	readThread->start();
}

void ConfigureDiveComputer::saveDeviceDetails(DeviceDetails *details, device_data_t *data)
{
	setState(WRITING);

	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();
}

bool ConfigureDiveComputer::saveXMLBackup(QString fileName, DeviceDetails *details, device_data_t *data, QString errorText)
{
	QString xml = "";
	QString vendor = data->vendor;
	QString product = data->product;
	xml += "<DiveComputerSettingsBackup>";
	xml += "\n<DiveComputer>";
	xml += addSettingToXML("Vendor", vendor);
	xml += addSettingToXML("Product", product);
	xml += "\n</DiveComputer>";
	xml += "\n<Settings>";
	xml += addSettingToXML("CustomText", details->customText());
	xml += addSettingToXML("DiveMode", details->diveMode());
	xml += addSettingToXML("Saturation", details->saturation());
	xml += addSettingToXML("Desaturation", details->desaturation());
	xml += addSettingToXML("LastDeco", details->lastDeco());
	xml += addSettingToXML("Brightness", details->brightness());
	xml += addSettingToXML("Units", details->units());
	xml += addSettingToXML("SamplingRate", details->samplingRate());
	xml += addSettingToXML("Salinity", details->salinity());
	xml += addSettingToXML("DiveModeColor", details->diveModeColor());
	xml += addSettingToXML("Language", details->language());
	xml += addSettingToXML("DateFormat", details->dateFormat());
	xml += addSettingToXML("CompassGain", details->compassGain());
	xml += "\n</Settings>";
	xml += "\n</DiveComputerSettingsBackup>";
	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;
}

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);

					if (settingName == "Saturation")
						details->setSaturation(keyString.toInt());

					if (settingName == "Desaturation")
						details->setDesaturation(keyString.toInt());

					if (settingName == "DiveMode")
						details->setDiveMode(keyString.toInt());

					if (settingName == "LastDeco")
						details->setLastDeco(keyString.toInt());

					if (settingName == "Brightness")
						details->setBrightness(keyString.toInt());

					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());

					if (settingName == "Language")
						details->setLanguage(keyString.toInt());

					if (settingName == "DateFormat")
						details->setDateFormat(keyString.toInt());

					if (settingName == "CompassGain")
						details->setCompassGain(keyString.toInt());
				}

				settingNode = settingNode->next;
			}
		}
		child = child->next;
	}

	return true;
}

void ConfigureDiveComputer::setState(ConfigureDiveComputer::states newState)
{
	currentState = newState;
	emit stateChanged(currentState);
}


QString ConfigureDiveComputer::addSettingToXML(QString settingName, QVariant value)
{
	return "\n<" + settingName + ">" + value.toString() + "</" + settingName + ">";
}

void ConfigureDiveComputer::setError(QString err)
{
	lastError = err;
	emit error(err);
}

void ConfigureDiveComputer::readThreadFinished()
{
	setState(DONE);
	emit readFinished();
}

void ConfigureDiveComputer::writeThreadFinished()
{
	setState(DONE);
	if (writeThread->lastError.isEmpty()) {
		//No error
		emit message(tr("Setting successfully written to device"));
	}
}