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

Port to QXMlStreamReader

parent a1eba222
...@@ -147,8 +147,8 @@ void PimSettingExporterConsole::start() ...@@ -147,8 +147,8 @@ void PimSettingExporterConsole::start()
{ {
//Load template if necessary //Load template if necessary
if (!mTemplateFileName.isEmpty()) { if (!mTemplateFileName.isEmpty()) {
TemplateSelection selection(mTemplateFileName); TemplateSelection selection;
const QHash<Utils::AppsType, Utils::importExportParameters> templateElements = selection.loadTemplate(); const QHash<Utils::AppsType, Utils::importExportParameters> templateElements = selection.loadTemplate(mTemplateFileName);
mPimSettingsBackupRestore->setStoredParameters(templateElements); mPimSettingsBackupRestore->setStoredParameters(templateElements);
} }
switch (mMode) { switch (mMode) {
......
...@@ -22,105 +22,97 @@ ...@@ -22,105 +22,97 @@
#include <QFile> #include <QFile>
#include <QXmlStreamWriter> #include <QXmlStreamWriter>
TemplateSelection::TemplateSelection(const QString &path) TemplateSelection::TemplateSelection()
: mStreamWriter(nullptr) : 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() TemplateSelection::~TemplateSelection()
{ {
delete mStreamWriter; delete mStreamWriter;
delete mStreamReader;
} }
Utils::StoredTypes TemplateSelection::loadStoredTypes(const QDomElement &element, int &numberOfStep) Utils::StoredTypes TemplateSelection::loadStoredTypes(int &numberOfStep)
{ {
Utils::StoredTypes types = Utils::None; Utils::StoredTypes types = Utils::None;
QDomNode n = element.firstChild(); while(mStreamReader->readNextStartElement()) {
while (!n.isNull()) { if (mStreamReader->name() == QLatin1String("mailtransport")) {
QDomElement e = n.toElement(); types |= Utils::MailTransport;
if (!e.isNull()) { numberOfStep++;
const QString tagName(e.tagName()); } else if (mStreamReader->name() == QLatin1String("mail")) {
if (tagName == QLatin1String("mailtransport")) { types |= Utils::Mails;
types |= Utils::MailTransport; numberOfStep++;
numberOfStep++; } else if (mStreamReader->name() == QLatin1String("resources")) {
} else if (tagName == QLatin1String("mail")) { types |= Utils::Resources;
types |= Utils::Mails; numberOfStep++;
numberOfStep++; } else if (mStreamReader->name() == QLatin1String("identity")) {
} else if (tagName == QLatin1String("resources")) { types |= Utils::Identity;
types |= Utils::Resources; numberOfStep++;
numberOfStep++; } else if (mStreamReader->name() == QLatin1String("config")) {
} else if (tagName == QLatin1String("identity")) { types |= Utils::Config;
types |= Utils::Identity; numberOfStep++;
numberOfStep++; } else if (mStreamReader->name() == QLatin1String("data")) {
} else if (tagName == QLatin1String("config")) { types |= Utils::Data;
types |= Utils::Config; numberOfStep++;
numberOfStep++;
} else if (tagName == QLatin1String("data")) {
types |= Utils::Data;
numberOfStep++;
}
} }
n = n.nextSibling(); mStreamReader->skipCurrentElement();
} }
return types; 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; QHash<Utils::AppsType, Utils::importExportParameters> value;
QDomElement docElem = mDocument.documentElement(); if (mStreamReader->readNextStartElement()) {
QDomNode n = docElem.firstChild(); if (mStreamReader->name() == QLatin1String("pimsettingexporter")) {
while (!n.isNull()) { while(mStreamReader->readNextStartElement()) {
QDomElement e = n.toElement(); Utils::AppsType type = Utils::Unknown;
if (!e.isNull()) { if (mStreamReader->name() == QLatin1String("kmail")) {
const QString tagName(e.tagName()); type = Utils::KMail;
//qCDebug(PIMSETTINGEXPORTERCORE_LOG) << "tag :" << tagName; } else if (mStreamReader->name() == QLatin1String("kaddressbook")) {
Utils::AppsType type = Utils::Unknown; type = Utils::KAddressBook;
if (tagName == QLatin1String("kmail")) { } else if (mStreamReader->name() == QLatin1String("kalarm")) {
type = Utils::KMail; type = Utils::KAlarm;
} else if (tagName == QLatin1String("kaddressbook")) { } else if (mStreamReader->name() == QLatin1String("korganizer")) {
type = Utils::KAddressBook; type = Utils::KOrganizer;
} else if (tagName == QLatin1String("kalarm")) { } else if (mStreamReader->name() == QLatin1String("knotes")) {
type = Utils::KAlarm; type = Utils::KNotes;
} else if (tagName == QLatin1String("korganizer")) { } else if (mStreamReader->name() == QLatin1String("akregator")) {
type = Utils::KOrganizer; type = Utils::Akregator;
} else if (tagName == QLatin1String("knotes")) { } else if (mStreamReader->name() == QLatin1String("blogilo")) {
type = Utils::KNotes; type = Utils::Blogilo;
} else if (tagName == QLatin1String("akregator")) { }
type = Utils::Akregator; if (type != Utils::Unknown) {
} else if (tagName == QLatin1String("blogilo")) { int numberOfSteps = 0;
type = Utils::Blogilo; Utils::StoredTypes storedType = loadStoredTypes(numberOfSteps);
} if (storedType != Utils::None) {
if (type != Utils::Unknown) { Utils::importExportParameters utils;
int numberOfSteps = 0; utils.types = storedType;
Utils::StoredTypes storedType = loadStoredTypes(e, numberOfSteps); utils.numberSteps = numberOfSteps;
if (storedType != Utils::None) { value.insert(type, utils);
Utils::importExportParameters utils; }
utils.types = storedType;
utils.numberSteps = numberOfSteps;
value.insert(type, utils);
} }
} }
} else {
qCDebug(PIMSETTINGEXPORTERCORE_LOG) << "Toplevel xml is not correct";
} }
n = n.nextSibling(); } else {
qCDebug(PIMSETTINGEXPORTERCORE_LOG) << "Impossible to parse file";
} }
return value; return value;
} }
......
...@@ -22,26 +22,26 @@ ...@@ -22,26 +22,26 @@
#include "pimsettingexporter_export.h" #include "pimsettingexporter_export.h"
#include <QHash> #include <QHash>
#include <QDomDocument>
#include "utils.h" #include "utils.h"
class QXmlStreamWriter; class QXmlStreamWriter;
class QXmlStreamReader;
class PIMSETTINGEXPORTER_EXPORT TemplateSelection class PIMSETTINGEXPORTER_EXPORT TemplateSelection
{ {
public: public:
TemplateSelection(const QString &path = QString()); TemplateSelection();
~TemplateSelection(); ~TemplateSelection();
void createTemplate(const QHash<Utils::AppsType, Utils::importExportParameters> &stored); 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; QString saveTemplate() const;
private: private:
static Utils::StoredTypes loadStoredTypes(const QDomElement &element, int &numberOfStep); Utils::StoredTypes loadStoredTypes(int &numberOfStep);
void saveParameters(Utils::StoredTypes type); void saveParameters(Utils::StoredTypes type);
QDomDocument mDocument;
QXmlStreamWriter *mStreamWriter; QXmlStreamWriter *mStreamWriter;
QXmlStreamReader *mStreamReader;
QString mSaveTemplate; QString mSaveTemplate;
}; };
......
...@@ -308,8 +308,8 @@ void SelectionTypeTreeWidget::slotItemChanged(QTreeWidgetItem *item, int column) ...@@ -308,8 +308,8 @@ void SelectionTypeTreeWidget::slotItemChanged(QTreeWidgetItem *item, int column)
void SelectionTypeTreeWidget::loadFileName(const QString &fileName) void SelectionTypeTreeWidget::loadFileName(const QString &fileName)
{ {
unSelectAllItems(); unSelectAllItems();
TemplateSelection templateSelection(fileName); TemplateSelection templateSelection;
const QHash<Utils::AppsType, Utils::importExportParameters> params = templateSelection.loadTemplate(); const QHash<Utils::AppsType, Utils::importExportParameters> params = templateSelection.loadTemplate(fileName);
setParameters(params); 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