Commit 0eef3771 authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Extract import contact file

parent 0e4b3fba
Pipeline #29696 failed with stage
in 10 minutes and 3 seconds
set(kaddressbook_importexport_windowscontact_SRCS
windowscontactimportexportplugin.cpp
windowscontactimportexportplugininterface.cpp
importwindowcontact.cpp
)
......
/*
Copyright (C) 2020 Laurent Montel <montel@kde.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "importwindowcontact.h"
#include "importexportwindowscontactplugin_debug.h"
#include <KLocalizedString>
#include <KMessageBox>
#include <QDomDocument>
#include <QFile>
ImportWindowContact::ImportWindowContact()
{
}
ImportWindowContact::~ImportWindowContact()
{
}
void ImportWindowContact::importFile(const QString &fileName)
{
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) {
const QString msg = i18n("<qt>Unable to open <b>%1</b> for reading.</qt>", fileName);
KMessageBox::error(mParentWidget, msg);
return;
}
QDomDocument doc;
if (loadDomElement(doc, &file)) {
QDomElement list = doc.documentElement();
if (list.isNull()) {
qCWarning(IMPORTEXPORTWINDOWSCONTACTPLUGIN_LOG) << "No list defined in file";
} else {
for (QDomElement e = list.firstChildElement(); !e.isNull(); e = e.nextSiblingElement()) {
const QString tag = e.tagName();
qDebug() << " tahg " << tag;
}
}
}
}
bool ImportWindowContact::loadDomElement(QDomDocument &doc, QFile *file)
{
QString errorMsg;
int errorRow;
int errorCol;
if (!doc.setContent(file, &errorMsg, &errorRow, &errorCol)) {
qCWarning(IMPORTEXPORTWINDOWSCONTACTPLUGIN_LOG) << "Unable to load document.Parse error in line " << errorRow
<< ", col " << errorCol << ": " << errorMsg;
return false;
}
return true;
}
void ImportWindowContact::setParentWidget(QWidget *parentWidget)
{
mParentWidget = parentWidget;
}
/*
Copyright (C) 2020 Laurent Montel <montel@kde.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef IMPORTCONTACTEXPORT_H
#define IMPORTCONTACTEXPORT_H
#include <QString>
class QDomDocument;
class QFile;
class QWidget;
class ImportWindowContact
{
public:
ImportWindowContact();
~ImportWindowContact();
void importFile(const QString &fileName);
void setParentWidget(QWidget *parentWidget);
private:
Q_REQUIRED_RESULT bool loadDomElement(QDomDocument &doc, QFile *file);
QWidget *mParentWidget = nullptr;
};
#endif // IMPORTCONTACTEXPORT_H
/*
Copyright (C) 2016-2020 Laurent Montel <montel@kde.org>
Copyright (C) 2020 Laurent Montel <montel@kde.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
......@@ -20,6 +20,7 @@
#include "windowscontactimportexportplugininterface.h"
#include "../shared/importexportengine.h"
#include "importexportwindowscontactplugin_debug.h"
#include "importwindowcontact.h"
#include <KLocalizedString>
#include <KActionCollection>
#include <QAction>
......@@ -87,20 +88,6 @@ void WindowsContactImportExportPluginInterface::exec()
}
}
bool WindowsContactImportExportPluginInterface::loadDomElement(QDomDocument &doc, QFile *file)
{
QString errorMsg;
int errorRow;
int errorCol;
if (!doc.setContent(file, &errorMsg, &errorRow, &errorCol)) {
qCWarning(IMPORTEXPORTWINDOWSCONTACTPLUGIN_LOG) << "Unable to load document.Parse error in line " << errorRow
<< ", col " << errorCol << ": " << errorMsg;
return false;
}
return true;
}
void WindowsContactImportExportPluginInterface::importWindowsContact()
{
KAddressBookImportExport::KAddressBookImportExportContactList contactList;
......@@ -111,24 +98,9 @@ void WindowsContactImportExportPluginInterface::importWindowsContact()
return;
}
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) {
const QString msg = i18n("<qt>Unable to open <b>%1</b> for reading.</qt>", fileName);
KMessageBox::error(parentWidget(), msg);
return;
}
QDomDocument doc;
if (loadDomElement(doc, &file)) {
QDomElement list = doc.documentElement();
if (list.isNull()) {
qCWarning(IMPORTEXPORTWINDOWSCONTACTPLUGIN_LOG) << "No list defined in file";
} else {
for (QDomElement e = list.firstChildElement(); !e.isNull(); e = e.nextSiblingElement()) {
const QString tag = e.tagName();
qDebug() << " tahg " << tag;
}
}
}
ImportWindowContact importer;
importer.setParentWidget(parentWidget());
importer.importFile(fileName);
}
void WindowsContactImportExportPluginInterface::exportWindowsContact()
......
/*
Copyright (C) 2016-2020 Laurent Montel <montel@kde.org>
Copyright (C) 2020 Laurent Montel <montel@kde.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
......@@ -22,8 +22,6 @@
#include <KAddressBookImportExport/KAddressBookImportExportPluginInterface>
#include <KContacts/Addressee>
class QFile;
class QDomDocument;
class WindowsContactImportExportPluginInterface : public KAddressBookImportExport::KAddressBookImportExportPluginInterface
{
Q_OBJECT
......@@ -42,7 +40,6 @@ private:
void slotImportWindowsContact();
void importWindowsContact();
void exportWindowsContact();
Q_REQUIRED_RESULT bool loadDomElement(QDomDocument &doc, QFile *file);
};
#endif
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