Commit cc642462 authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Now we can import address/name

CCBUG: 424940
parent bd7dbb03
Pipeline #29746 failed with stage
in 14 minutes and 7 seconds
......@@ -20,6 +20,7 @@
#include "importwindowcontacttest.h"
#include "../importwindowcontact.h"
#include <QTest>
#include <KContacts/Addressee>
QTEST_MAIN(ImportWindowContactTest)
ImportWindowContactTest::ImportWindowContactTest(QObject *parent)
: QObject(parent)
......@@ -31,6 +32,34 @@ void ImportWindowContactTest::shouldImportWindowContact_data()
QTest::addColumn<QString>("filename");
QTest::addColumn<KContacts::Addressee::List>("result");
QTest::newRow("empty") << QString() << KContacts::Addressee::List();
{
KContacts::Addressee::List result;
KContacts::Addressee address;
address.setName(QStringLiteral("John Doe"));
address.setUid(QStringLiteral("foo"));
KContacts::Email email;
email.setEmail(QStringLiteral("sample-email@kde.org"));
address.setEmailList({email});
result << address;
QTest::newRow("test1") << QStringLiteral("test1.contact") << result;
}
{
KContacts::Addressee::List result;
KContacts::Addressee address;
address.setName(QStringLiteral("John Anon"));
address.setFamilyName(QStringLiteral("Anon"));
address.setGivenName(QStringLiteral("John"));
address.setUid(QStringLiteral("foo"));
KContacts::Email email;
email.setEmail(QStringLiteral("sampleaddress@yahoo.com"));
address.setEmailList({email});
result << address;
QTest::newRow("sample2") << QStringLiteral("sample2.contact") << result;
}
}
void ImportWindowContactTest::shouldImportWindowContact()
......@@ -38,7 +67,22 @@ void ImportWindowContactTest::shouldImportWindowContact()
QFETCH(QString, filename);
QFETCH(KContacts::Addressee::List, result);
ImportWindowContact contact;
contact.setShowMessageBox(false);
contact.setAutoTests(true);
const QString fullPath = QStringLiteral(WINDOWSCONTACT_DATADIR "/data/") + filename;
QCOMPARE(contact.importFile(fullPath), result);
const KContacts::Addressee::List importData = contact.importFile(fullPath);
const bool contactEqual = (importData == result);
//Pb with setUid which is different.
if (!contactEqual) {
qDebug() << " RESULT";
for (const KContacts::Addressee &address : importData) {
qDebug() << "Data: " << address.toString();
}
qDebug() << " EXPECTED";
for (const KContacts::Addressee &address : result) {
qDebug() << "Data: " << address.toString();
}
}
QVERIFY(contactEqual);
}
......@@ -39,7 +39,7 @@ KContacts::Addressee::List ImportWindowContact::importFile(const QString &fileNa
KContacts::Addressee::List lst;
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) {
if (mShowMessageBox) {
if (!mAutoTest) {
const QString msg = i18n("<qt>Unable to open <b>%1</b> for reading.</qt>", fileName);
KMessageBox::error(mParentWidget, msg);
} else {
......@@ -53,10 +53,62 @@ KContacts::Addressee::List ImportWindowContact::importFile(const QString &fileNa
if (list.isNull()) {
qCWarning(IMPORTEXPORTWINDOWSCONTACTPLUGIN_LOG) << "No list defined in file";
} else {
KContacts::Addressee contact;
if (mAutoTest) {
contact.setUid(QStringLiteral("foo"));
}
for (QDomElement e = list.firstChildElement(); !e.isNull(); e = e.nextSiblingElement()) {
const QString tag = e.tagName();
qDebug() << " tag " << tag;
if (tag == QLatin1String("c:EmailAddressCollection")) {
KContacts::Email::List lstEmails;
for (QDomElement emails = e.firstChildElement(); !emails.isNull(); emails = emails.nextSiblingElement()) {
const QString emailsTag = emails.tagName();
qDebug() << " emailstag "<< emailsTag;
if (emailsTag == QLatin1String("c:EmailAddress")) {
KContacts::Email email;
for (QDomElement addresses = emails.firstChildElement(); !addresses.isNull(); addresses = addresses.nextSiblingElement()) {
const QString addressesTag = addresses.tagName();
if (addressesTag == QLatin1String("c:Type")) {
} else if (addressesTag == QLatin1String("c:Address")) {
email.setEmail(addresses.text());
} else {
qDebug() << " address tag not supported yet " << addressesTag;
}
}
lstEmails << email;
}
contact.setEmailList(lstEmails);
}
} else if (tag == QLatin1String("c:NameCollection")) {
for (QDomElement name = e.firstChildElement(); !name.isNull(); name = name.nextSiblingElement()) {
const QString nameTag = name.tagName();
if (nameTag == QLatin1String("c:Name")) {
for (QDomElement nameInfo = name.firstChildElement(); !nameInfo.isNull(); nameInfo = nameInfo.nextSiblingElement()) {
const QString nameInfoTag = nameInfo.tagName();
if (nameInfoTag == QLatin1String("c:FormattedName")) {
contact.setName(nameInfo.text());
} else if (nameInfoTag == QLatin1String("c:GivenName")) {
contact.setGivenName(nameInfo.text());
} else if (nameInfoTag == QLatin1String("c:FamilyName")) {
contact.setFamilyName(nameInfo.text());
} else if (nameInfoTag == QLatin1String("c:FormattedName")) {
contact.setFormattedName(nameInfo.text());
} else {
qDebug() << " name tag not supported yet " << nameInfoTag;
}
}
} else {
qDebug() << " name tag unknow:" << nameTag;
}
}
} else if (tag == QLatin1String("c:PhotoCollection")) {
} else {
qDebug() << "unknown tag " << tag;
}
}
lst << contact;
}
}
return lst;
......@@ -80,7 +132,7 @@ void ImportWindowContact::setParentWidget(QWidget *parentWidget)
mParentWidget = parentWidget;
}
void ImportWindowContact::setShowMessageBox(bool b)
void ImportWindowContact::setAutoTests(bool b)
{
mShowMessageBox = b;
mAutoTest = b;
}
......@@ -33,11 +33,11 @@ public:
Q_REQUIRED_RESULT KContacts::Addressee::List importFile(const QString &fileName);
void setParentWidget(QWidget *parentWidget);
void setShowMessageBox(bool b); //Only for autotest
void setAutoTests(bool b); //Only for autotest
private:
Q_REQUIRED_RESULT bool loadDomElement(QDomDocument &doc, QFile *file);
bool mShowMessageBox = true;
bool mAutoTest = false;
QWidget *mParentWidget = nullptr;
};
......
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