mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
Working XML Backup and Restore
The ConfigureDiveComputer class now has functions for complete XML backup and restore. These dump the loaded settings on a dive computer to an XML file, and there is an option to restore them. Signed-off-by: Joseph W. Joshua <joejoshw@gmail.com> Signed-off-by: Thiago Macieira <thiago@macieira.org>
This commit is contained in:
parent
e54d7d9178
commit
aad60ef6da
4 changed files with 115 additions and 13 deletions
|
@ -2,6 +2,11 @@
|
||||||
#include "libdivecomputer/hw.h"
|
#include "libdivecomputer/hw.h"
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
|
#include <libxml/parser.h>
|
||||||
|
#include <libxml/parserInternals.h>
|
||||||
|
#include <libxml/tree.h>
|
||||||
|
#include <libxslt/transform.h>
|
||||||
|
#include <QStringList>
|
||||||
|
|
||||||
ConfigureDiveComputer::ConfigureDiveComputer(QObject *parent) :
|
ConfigureDiveComputer::ConfigureDiveComputer(QObject *parent) :
|
||||||
QObject(parent),
|
QObject(parent),
|
||||||
|
@ -49,17 +54,18 @@ bool ConfigureDiveComputer::saveXMLBackup(QString fileName, DeviceDetails *detai
|
||||||
QString xml = "";
|
QString xml = "";
|
||||||
QString vendor = data->vendor;
|
QString vendor = data->vendor;
|
||||||
QString product = data->product;
|
QString product = data->product;
|
||||||
xml += "<backup>";
|
xml += "<DiveComputerSettingsBackup>";
|
||||||
xml += "\n<divecomputer vendor='" + vendor
|
xml += "\n<DiveComputer>";
|
||||||
+ "' model = '" + product + "'"
|
xml += addSettingToXML("Vendor", vendor);
|
||||||
+ " />";
|
xml += addSettingToXML("Product", product);
|
||||||
xml += "\n<settings>";
|
xml += "\n</DiveComputer>";
|
||||||
xml += "\n<setting name='CustomText' value = '" + details->customText() + "' />";
|
xml += "\n<Settings>";
|
||||||
xml += "\n<setting name='Brightness' value = '" + QString::number(details->brightness()) + "' />";
|
xml += addSettingToXML("CustomText", details->customText());
|
||||||
xml += "\n<setting name='Language' value = '" + QString::number(details->language()) + "' />";
|
xml += addSettingToXML("Brightness", details->brightness());
|
||||||
xml += "\n<setting name='DateFormat' value = '" + QString::number(details->dateFormat()) + "' />";
|
xml += addSettingToXML("Language", details->language());
|
||||||
xml += "\n</settings>";
|
xml += addSettingToXML("DateFormat", details->dateFormat());
|
||||||
xml += "\n</backup>";
|
xml += "\n</Settings>";
|
||||||
|
xml += "\n</DiveComputerSettingsBackup>";
|
||||||
QFile file(fileName);
|
QFile file(fileName);
|
||||||
if (!file.open(QIODevice::WriteOnly)) {
|
if (!file.open(QIODevice::WriteOnly)) {
|
||||||
errorText = tr("Could not save the backup file %1. Error Message: %2")
|
errorText = tr("Could not save the backup file %1. Error Message: %2")
|
||||||
|
@ -74,12 +80,77 @@ bool ConfigureDiveComputer::saveXMLBackup(QString fileName, DeviceDetails *detai
|
||||||
return true;
|
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 == "Brightness")
|
||||||
|
details->setBrightness(keyString.toInt());
|
||||||
|
|
||||||
|
if (settingName == "Language")
|
||||||
|
details->setLanguage(keyString.toInt());
|
||||||
|
|
||||||
|
if (settingName == "DateFormat")
|
||||||
|
details->setDateFormat(keyString.toInt());
|
||||||
|
}
|
||||||
|
|
||||||
|
settingNode = settingNode->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
child = child->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void ConfigureDiveComputer::setState(ConfigureDiveComputer::states newState)
|
void ConfigureDiveComputer::setState(ConfigureDiveComputer::states newState)
|
||||||
{
|
{
|
||||||
currentState = newState;
|
currentState = newState;
|
||||||
emit stateChanged(currentState);
|
emit stateChanged(currentState);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
QString ConfigureDiveComputer::addSettingToXML(QString settingName, QVariant value)
|
||||||
|
{
|
||||||
|
return "\n<" + settingName + ">" + value.toString() + "</" + settingName + ">";
|
||||||
|
}
|
||||||
|
|
||||||
void ConfigureDiveComputer::setError(QString err)
|
void ConfigureDiveComputer::setError(QString err)
|
||||||
{
|
{
|
||||||
lastError = err;
|
lastError = err;
|
||||||
|
|
|
@ -8,6 +8,8 @@
|
||||||
#include "configuredivecomputerthreads.h"
|
#include "configuredivecomputerthreads.h"
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
|
|
||||||
|
#include "libxml/xmlreader.h"
|
||||||
|
|
||||||
class ConfigureDiveComputer : public QObject
|
class ConfigureDiveComputer : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
@ -31,6 +33,7 @@ public:
|
||||||
void saveDeviceDetails(DeviceDetails *details, device_data_t *data);
|
void saveDeviceDetails(DeviceDetails *details, device_data_t *data);
|
||||||
void fetchDeviceDetails();
|
void fetchDeviceDetails();
|
||||||
bool saveXMLBackup(QString fileName, DeviceDetails *details, device_data_t *data, QString errorText);
|
bool saveXMLBackup(QString fileName, DeviceDetails *details, device_data_t *data, QString errorText);
|
||||||
|
bool restoreXMLBackup(QString fileName, DeviceDetails *details, QString errorText);
|
||||||
signals:
|
signals:
|
||||||
void message(QString msg);
|
void message(QString msg);
|
||||||
void error(QString err);
|
void error(QString err);
|
||||||
|
@ -43,7 +46,7 @@ private:
|
||||||
ReadSettingsThread *readThread;
|
ReadSettingsThread *readThread;
|
||||||
WriteSettingsThread *writeThread;
|
WriteSettingsThread *writeThread;
|
||||||
void setState(states newState);
|
void setState(states newState);
|
||||||
|
QString addSettingToXML(QString settingName, QVariant value);
|
||||||
private slots:
|
private slots:
|
||||||
void readThreadFinished();
|
void readThreadFinished();
|
||||||
void writeThreadFinished();
|
void writeThreadFinished();
|
||||||
|
|
|
@ -244,7 +244,7 @@ void ConfigureDiveComputerDialog::on_backupButton_clicked()
|
||||||
QString errorText = "";
|
QString errorText = "";
|
||||||
if (!config->saveXMLBackup(backupPath, deviceDetails, &device_data, errorText)) {
|
if (!config->saveXMLBackup(backupPath, deviceDetails, &device_data, errorText)) {
|
||||||
QMessageBox::critical(this, tr("XML Backup Error"),
|
QMessageBox::critical(this, tr("XML Backup Error"),
|
||||||
tr("An eror occurred while saving the backup file.\n%1")
|
tr("An error occurred while saving the backup file.\n%1")
|
||||||
.arg(errorText)
|
.arg(errorText)
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
@ -255,3 +255,29 @@ void ConfigureDiveComputerDialog::on_backupButton_clicked()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ConfigureDiveComputerDialog::on_restoreBackupButton_clicked()
|
||||||
|
{
|
||||||
|
QString filename = existing_filename ?: prefs.default_filename;
|
||||||
|
QFileInfo fi(filename);
|
||||||
|
filename = fi.absolutePath().append(QDir::separator()).append("Backup.xml");
|
||||||
|
QString restorePath = QFileDialog::getOpenFileName(this, tr("Restore Dive Computer Settings"),
|
||||||
|
filename, tr("Backup files (*.xml)")
|
||||||
|
);
|
||||||
|
if (!restorePath.isEmpty()) {
|
||||||
|
QString errorText = "";
|
||||||
|
if (!config->restoreXMLBackup(restorePath, deviceDetails, errorText)) {
|
||||||
|
QMessageBox::critical(this, tr("XML Restore Error"),
|
||||||
|
tr("An error occurred while restoring the backup file.\n%1")
|
||||||
|
.arg(errorText)
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
reloadValues();
|
||||||
|
//getDeviceData();
|
||||||
|
//config->saveDeviceDetails(deviceDetails, &device_data);
|
||||||
|
QMessageBox::information(this, tr("Restore succeeded"),
|
||||||
|
tr("Your settings have been restored successfully.")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -33,6 +33,8 @@ private slots:
|
||||||
void reloadValues();
|
void reloadValues();
|
||||||
void on_backupButton_clicked();
|
void on_backupButton_clicked();
|
||||||
|
|
||||||
|
void on_restoreBackupButton_clicked();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Ui::ConfigureDiveComputerDialog *ui;
|
Ui::ConfigureDiveComputerDialog *ui;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue