Backup basic settings as XML

This patch enables XML backup. We can now save the settings
to an XML file. Currently this backs up just the basic stuff
such as custom text, language and brightness.

Signed-off-by: Joseph W. Joshua <joejoshw@gmail.com>
Signed-off-by: Thiago Macieira <thiago@macieira.org>
This commit is contained in:
Joseph W. Joshua 2014-06-10 19:19:28 +03:00 committed by Thiago Macieira
parent 3534e29ae2
commit e54d7d9178
5 changed files with 90 additions and 11 deletions

View file

@ -1,6 +1,8 @@
#include "configuredivecomputer.h"
#include "libdivecomputer/hw.h"
#include <QDebug>
#include <QFile>
ConfigureDiveComputer::ConfigureDiveComputer(QObject *parent) :
QObject(parent),
readThread(0),
@ -42,6 +44,36 @@ void ConfigureDiveComputer::saveDeviceDetails(DeviceDetails *details, device_dat
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 += "<backup>";
xml += "\n<divecomputer vendor='" + vendor
+ "' model = '" + product + "'"
+ " />";
xml += "\n<settings>";
xml += "\n<setting name='CustomText' value = '" + details->customText() + "' />";
xml += "\n<setting name='Brightness' value = '" + QString::number(details->brightness()) + "' />";
xml += "\n<setting name='Language' value = '" + QString::number(details->language()) + "' />";
xml += "\n<setting name='DateFormat' value = '" + QString::number(details->dateFormat()) + "' />";
xml += "\n</settings>";
xml += "\n</backup>";
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;
}
void ConfigureDiveComputer::setState(ConfigureDiveComputer::states newState)
{
currentState = newState;

View file

@ -30,7 +30,7 @@ public:
device_data_t *m_data;
void saveDeviceDetails(DeviceDetails *details, device_data_t *data);
void fetchDeviceDetails();
bool saveXMLBackup(QString fileName, DeviceDetails *details, device_data_t *data, QString errorText);
signals:
void message(QString msg);
void error(QString err);

View file

@ -7,9 +7,8 @@
#include "../display.h"
#include "../divelist.h"
#include "configuredivecomputer.h"
#include <QInputDialog>
#include <QDebug>
#include <QFileDialog>
#include <QMessageBox>
struct product {
const char *product;
dc_descriptor_t *descriptor;
@ -126,6 +125,15 @@ void ConfigureDiveComputerDialog::fill_device_list(int dc_type)
ui->device->setCurrentIndex(deviceIndex);
}
void ConfigureDiveComputerDialog::populateDeviceDetails()
{
deviceDetails->setBrightness(ui->brightnessComboBox->currentIndex());
deviceDetails->setLanguage(ui->languageComboBox->currentIndex());
deviceDetails->setDateFormat(ui->dateFormatComboBox->currentIndex());
deviceDetails->setCustomText(ui->customTextLlineEdit->text());
deviceDetails->setSyncTime(ui->dateTimeSyncCheckBox->isChecked());
}
void ConfigureDiveComputerDialog::on_vendor_currentIndexChanged(const QString &vendor)
{
int dcType = DC_TYPE_SERIAL;
@ -200,11 +208,7 @@ void ConfigureDiveComputerDialog::deviceReadFinished()
void ConfigureDiveComputerDialog::on_saveSettingsPushButton_clicked()
{
deviceDetails->setBrightness(ui->brightnessComboBox->currentIndex());
deviceDetails->setLanguage(ui->languageComboBox->currentIndex());
deviceDetails->setDateFormat(ui->dateFormatComboBox->currentIndex());
deviceDetails->setCustomText(ui->customTextLlineEdit->text());
deviceDetails->setSyncTime(ui->dateTimeSyncCheckBox->isChecked());
populateDeviceDetails();
getDeviceData();
config->saveDeviceDetails(deviceDetails, &device_data);
}
@ -225,3 +229,29 @@ void ConfigureDiveComputerDialog::reloadValues()
ui->dateFormatComboBox->setCurrentIndex(deviceDetails->dateFormat());
}
void ConfigureDiveComputerDialog::on_backupButton_clicked()
{
QString filename = existing_filename ?: prefs.default_filename;
QFileInfo fi(filename);
filename = fi.absolutePath().append(QDir::separator()).append("Backup.xml");
QString backupPath = QFileDialog::getSaveFileName(this, tr("Backup Dive Computer Settings"),
filename, tr("Backup files (*.xml)")
);
if (!backupPath.isEmpty()) {
populateDeviceDetails();
getDeviceData();
QString errorText = "";
if (!config->saveXMLBackup(backupPath, deviceDetails, &device_data, errorText)) {
QMessageBox::critical(this, tr("XML Backup Error"),
tr("An eror occurred while saving the backup file.\n%1")
.arg(errorText)
);
} else {
QMessageBox::information(this, tr("Backup succeeded"),
tr("Your settings have been saved to: %1")
.arg(filename)
);
}
}
}

View file

@ -31,6 +31,8 @@ private slots:
void on_saveSettingsPushButton_clicked();
void deviceDetailsReceived(DeviceDetails *newDeviceDetails);
void reloadValues();
void on_backupButton_clicked();
private:
Ui::ConfigureDiveComputerDialog *ui;
@ -48,6 +50,7 @@ private:
void fill_device_list(int dc_type);
DeviceDetails *deviceDetails;
void populateDeviceDetails();
};
#endif // CONFIGUREDIVECOMPUTERDIALOG_H

View file

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>364</width>
<height>416</height>
<width>472</width>
<height>472</height>
</rect>
</property>
<property name="windowTitle">
@ -89,6 +89,20 @@
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="backupButton">
<property name="text">
<string>Backup</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="restoreBackupButton">
<property name="text">
<string>Restore Backup</string>
</property>
</widget>
</item>
</layout>
</item>
<item>