Commit 77d4f7f8 authored by Laurent Montel's avatar Laurent Montel 😁

Port to QXMlStreamReader

parent a1eba222
......@@ -147,8 +147,8 @@ void PimSettingExporterConsole::start()
{
//Load template if necessary
if (!mTemplateFileName.isEmpty()) {
TemplateSelection selection(mTemplateFileName);
const QHash<Utils::AppsType, Utils::importExportParameters> templateElements = selection.loadTemplate();
TemplateSelection selection;
const QHash<Utils::AppsType, Utils::importExportParameters> templateElements = selection.loadTemplate(mTemplateFileName);
mPimSettingsBackupRestore->setStoredParameters(templateElements);
}
switch (mMode) {
......
......@@ -22,95 +22,82 @@
#include <QFile>
#include <QXmlStreamWriter>
TemplateSelection::TemplateSelection(const QString &path)
: mStreamWriter(nullptr)
TemplateSelection::TemplateSelection()
: mStreamWriter(nullptr),
mStreamReader(nullptr)
{
if (!path.isEmpty()) {
QDomDocument doc;
QString errorMsg;
int errorRow;
int errorCol;
QFile file(path);
if (file.open(QIODevice::ReadOnly)) {
if (!doc.setContent(&file, &errorMsg, &errorRow, &errorCol)) {
qCDebug(PIMSETTINGEXPORTERCORE_LOG) << "Unable to load document.Parse error in line " << errorRow
<< ", col " << errorCol << ": " << errorMsg;
} else {
mDocument = doc;
}
} else {
qCDebug(PIMSETTINGEXPORTERCORE_LOG) << "Unable to load file:" << path;
}
}
}
TemplateSelection::~TemplateSelection()
{
delete mStreamWriter;
delete mStreamReader;
}
Utils::StoredTypes TemplateSelection::loadStoredTypes(const QDomElement &element, int &numberOfStep)
Utils::StoredTypes TemplateSelection::loadStoredTypes(int &numberOfStep)
{
Utils::StoredTypes types = Utils::None;
QDomNode n = element.firstChild();
while (!n.isNull()) {
QDomElement e = n.toElement();
if (!e.isNull()) {
const QString tagName(e.tagName());
if (tagName == QLatin1String("mailtransport")) {
while(mStreamReader->readNextStartElement()) {
if (mStreamReader->name() == QLatin1String("mailtransport")) {
types |= Utils::MailTransport;
numberOfStep++;
} else if (tagName == QLatin1String("mail")) {
} else if (mStreamReader->name() == QLatin1String("mail")) {
types |= Utils::Mails;
numberOfStep++;
} else if (tagName == QLatin1String("resources")) {
} else if (mStreamReader->name() == QLatin1String("resources")) {
types |= Utils::Resources;
numberOfStep++;
} else if (tagName == QLatin1String("identity")) {
} else if (mStreamReader->name() == QLatin1String("identity")) {
types |= Utils::Identity;
numberOfStep++;
} else if (tagName == QLatin1String("config")) {
} else if (mStreamReader->name() == QLatin1String("config")) {
types |= Utils::Config;
numberOfStep++;
} else if (tagName == QLatin1String("data")) {
} else if (mStreamReader->name() == QLatin1String("data")) {
types |= Utils::Data;
numberOfStep++;
}
}
n = n.nextSibling();
mStreamReader->skipCurrentElement();
}
return types;
}
QHash<Utils::AppsType, Utils::importExportParameters> TemplateSelection::loadTemplate()
QHash<Utils::AppsType, Utils::importExportParameters> TemplateSelection::loadTemplate(const QString &path)
{
if (path.isEmpty()) {
return {};
}
QFile file(path);
if (!file.open(QIODevice::ReadOnly|QIODevice::Text)) {
qCDebug(PIMSETTINGEXPORTERCORE_LOG) << "Unable to load file:" << path;
return {};
} else {
mStreamReader = new QXmlStreamReader(&file);
}
QHash<Utils::AppsType, Utils::importExportParameters> value;
QDomElement docElem = mDocument.documentElement();
QDomNode n = docElem.firstChild();
while (!n.isNull()) {
QDomElement e = n.toElement();
if (!e.isNull()) {
const QString tagName(e.tagName());
//qCDebug(PIMSETTINGEXPORTERCORE_LOG) << "tag :" << tagName;
if (mStreamReader->readNextStartElement()) {
if (mStreamReader->name() == QLatin1String("pimsettingexporter")) {
while(mStreamReader->readNextStartElement()) {
Utils::AppsType type = Utils::Unknown;
if (tagName == QLatin1String("kmail")) {
if (mStreamReader->name() == QLatin1String("kmail")) {
type = Utils::KMail;
} else if (tagName == QLatin1String("kaddressbook")) {
} else if (mStreamReader->name() == QLatin1String("kaddressbook")) {
type = Utils::KAddressBook;
} else if (tagName == QLatin1String("kalarm")) {
} else if (mStreamReader->name() == QLatin1String("kalarm")) {
type = Utils::KAlarm;
} else if (tagName == QLatin1String("korganizer")) {
} else if (mStreamReader->name() == QLatin1String("korganizer")) {
type = Utils::KOrganizer;
} else if (tagName == QLatin1String("knotes")) {
} else if (mStreamReader->name() == QLatin1String("knotes")) {
type = Utils::KNotes;
} else if (tagName == QLatin1String("akregator")) {
} else if (mStreamReader->name() == QLatin1String("akregator")) {
type = Utils::Akregator;
} else if (tagName == QLatin1String("blogilo")) {
} else if (mStreamReader->name() == QLatin1String("blogilo")) {
type = Utils::Blogilo;
}
if (type != Utils::Unknown) {
int numberOfSteps = 0;
Utils::StoredTypes storedType = loadStoredTypes(e, numberOfSteps);
Utils::StoredTypes storedType = loadStoredTypes(numberOfSteps);
if (storedType != Utils::None) {
Utils::importExportParameters utils;
utils.types = storedType;
......@@ -119,8 +106,13 @@ QHash<Utils::AppsType, Utils::importExportParameters> TemplateSelection::loadTem
}
}
}
n = n.nextSibling();
} else {
qCDebug(PIMSETTINGEXPORTERCORE_LOG) << "Toplevel xml is not correct";
}
} else {
qCDebug(PIMSETTINGEXPORTERCORE_LOG) << "Impossible to parse file";
}
return value;
}
......
......@@ -22,26 +22,26 @@
#include "pimsettingexporter_export.h"
#include <QHash>
#include <QDomDocument>
#include "utils.h"
class QXmlStreamWriter;
class QXmlStreamReader;
class PIMSETTINGEXPORTER_EXPORT TemplateSelection
{
public:
TemplateSelection(const QString &path = QString());
TemplateSelection();
~TemplateSelection();
void createTemplate(const QHash<Utils::AppsType, Utils::importExportParameters> &stored);
QHash<Utils::AppsType, Utils::importExportParameters> loadTemplate();
QHash<Utils::AppsType, Utils::importExportParameters> loadTemplate(const QString &path);
QString saveTemplate() const;
private:
static Utils::StoredTypes loadStoredTypes(const QDomElement &element, int &numberOfStep);
Utils::StoredTypes loadStoredTypes(int &numberOfStep);
void saveParameters(Utils::StoredTypes type);
QDomDocument mDocument;
QXmlStreamWriter *mStreamWriter;
QXmlStreamReader *mStreamReader;
QString mSaveTemplate;
};
......
......@@ -308,8 +308,8 @@ void SelectionTypeTreeWidget::slotItemChanged(QTreeWidgetItem *item, int column)
void SelectionTypeTreeWidget::loadFileName(const QString &fileName)
{
unSelectAllItems();
TemplateSelection templateSelection(fileName);
const QHash<Utils::AppsType, Utils::importExportParameters> params = templateSelection.loadTemplate();
TemplateSelection templateSelection;
const QHash<Utils::AppsType, Utils::importExportParameters> params = templateSelection.loadTemplate(fileName);
setParameters(params);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment