diff --git a/autotests/contactgrouptest.cpp b/autotests/contactgrouptest.cpp index 41e3f87e..8d852139 100644 --- a/autotests/contactgrouptest.cpp +++ b/autotests/contactgrouptest.cpp @@ -1,385 +1,385 @@ /* This file is part of the KContacts framework. Copyright (c) 2008 Kevin Krammer This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include "kcontacts/contactgroup.h" #include "kcontacts/contactgrouptool.h" #include using namespace KContacts; class ContactGroupTest : public QObject { Q_OBJECT private Q_SLOTS: void contactGroupContactReference(); void contactGroupContactGroupReference(); void contactGroupData(); void contactGroup(); void testGroupRoundTrip(); void testGroupListRoundTrip(); }; QTEST_MAIN(ContactGroupTest) void ContactGroupTest::contactGroupContactReference() { const QLatin1String uid("MyReferenceId"); const QLatin1String gid("GID"); const QLatin1String preferredEMail("tokoe@kde.org"); const QLatin1String customKey("MyCustomKey"); const QLatin1String customValue("MyCustomValue"); // uid test { ContactGroup::ContactReference ref(uid); QCOMPARE(ref.uid(), uid); } // uid test { ContactGroup::ContactReference ref; ref.setUid(uid); QCOMPARE(ref.uid(), uid); } // preferredEmail test { ContactGroup::ContactReference ref(uid); ref.setPreferredEmail(preferredEMail); QCOMPARE(ref.preferredEmail(), preferredEMail); } // custom test { ContactGroup::ContactReference ref(uid); ref.insertCustom(customKey, customValue); QCOMPARE(ref.custom(customKey), customValue); ref.removeCustom(customKey); QCOMPARE(ref.custom(customKey), QString()); } // assignment test { ContactGroup::ContactReference ref(uid); ref.setGid(gid); ref.setPreferredEmail(preferredEMail); ref.insertCustom(customKey, customValue); ContactGroup::ContactReference ref2(ref); QCOMPARE(ref.uid(), ref2.uid()); QCOMPARE(ref.gid(), ref2.gid()); QCOMPARE(ref.preferredEmail(), ref2.preferredEmail()); QCOMPARE(ref.custom(customKey), ref2.custom(customKey)); QVERIFY(ref == ref2); } // const test { ContactGroup::ContactReference ref(uid); ref.setPreferredEmail(preferredEMail); ref.insertCustom(customKey, customValue); const ContactGroup::ContactReference constRef(ref); - constRef.uid(); - constRef.preferredEmail(); - constRef.custom(customKey); - constRef.gid(); + QCOMPARE(constRef.uid(), uid); + QCOMPARE(constRef.preferredEmail(), preferredEMail); + QCOMPARE(constRef.custom(customKey), customValue); + QVERIFY(constRef.gid().isEmpty()); } } void ContactGroupTest::contactGroupContactGroupReference() { const QLatin1String uid("MyReferenceId"); const QLatin1String customKey("MyCustomKey"); const QLatin1String customValue("MyCustomValue"); // uid test { ContactGroup::ContactGroupReference ref(uid); QCOMPARE(ref.uid(), uid); } // uid test { ContactGroup::ContactGroupReference ref; ref.setUid(uid); QCOMPARE(ref.uid(), uid); } // custom test { ContactGroup::ContactGroupReference ref(uid); ref.insertCustom(customKey, customValue); QCOMPARE(ref.custom(customKey), customValue); ref.removeCustom(customKey); QCOMPARE(ref.custom(customKey), QString()); } // assignment test { ContactGroup::ContactGroupReference ref(uid); ref.insertCustom(customKey, customValue); ContactGroup::ContactGroupReference ref2(ref); QCOMPARE(ref.uid(), ref2.uid()); QCOMPARE(ref.custom(customKey), ref2.custom(customKey)); QVERIFY(ref == ref2); } // const test { ContactGroup::ContactGroupReference ref(uid); ref.insertCustom(customKey, customValue); const ContactGroup::ContactGroupReference constRef(ref); constRef.uid(); constRef.custom(customKey); } } void ContactGroupTest::contactGroupData() { const QLatin1String name("Tobias Koenig"); const QLatin1String email("tokoe@kde.org"); const QLatin1String customKey("MyCustomKey"); const QLatin1String customValue("MyCustomValue"); // name/email test { ContactGroup::Data data(name, email); QCOMPARE(data.name(), name); QCOMPARE(data.email(), email); } // name test { ContactGroup::Data data; data.setName(name); QCOMPARE(data.name(), name); } // email test { ContactGroup::Data data; data.setEmail(email); QCOMPARE(data.email(), email); } // custom test { ContactGroup::Data data(name, email); data.insertCustom(customKey, customValue); QCOMPARE(data.custom(customKey), customValue); data.removeCustom(customKey); QCOMPARE(data.custom(customKey), QString()); } // assignment test { ContactGroup::Data data(name, email); data.insertCustom(customKey, customValue); ContactGroup::Data data2(data); QCOMPARE(data.name(), data2.name()); QCOMPARE(data.email(), data2.email()); QCOMPARE(data.custom(customKey), data2.custom(customKey)); QVERIFY(data == data2); } // const test { ContactGroup::Data data(name, email); data.insertCustom(customKey, customValue); const ContactGroup::Data constData(data); - constData.name(); - constData.email(); - constData.custom(customKey); + QCOMPARE(constData.name(), name); + QCOMPARE(constData.email(), email); + QCOMPARE(constData.custom(customKey), customValue); } } void ContactGroupTest::contactGroup() { const QLatin1String groupName("MyGroupName"); const QLatin1String groupId("MyGroupID"); const QLatin1String name("Tobias Koenig"); const QLatin1String email("tokoe@kde.org"); const QLatin1String uid("MyUid"); // name test { ContactGroup group(groupName); QCOMPARE(group.name(), groupName); } // id test { ContactGroup group(groupName); group.setId(groupId); QCOMPARE(group.id(), groupId); } // contact reference test { ContactGroup group(groupName); QCOMPARE(group.contactReferenceCount(), (unsigned int)0); ContactGroup::ContactReference ref(uid); ref.setPreferredEmail(email); group.append(ref); QCOMPARE(group.contactReferenceCount(), (unsigned int)1); const ContactGroup::ContactReference ref2 = group.contactReference(0); QCOMPARE(ref, ref2); group.remove(ref); QCOMPARE(group.contactReferenceCount(), (unsigned int)0); } // contact group reference test { ContactGroup group(groupName); QCOMPARE(group.contactGroupReferenceCount(), (unsigned int)0); ContactGroup::ContactGroupReference ref(uid); group.append(ref); QCOMPARE(group.contactGroupReferenceCount(), (unsigned int)1); const ContactGroup::ContactGroupReference ref2 = group.contactGroupReference(0); QCOMPARE(ref, ref2); group.remove(ref); QCOMPARE(group.contactGroupReferenceCount(), (unsigned int)0); } // data test { ContactGroup group(groupName); QCOMPARE(group.dataCount(), (unsigned int)0); ContactGroup::Data data(name, email); group.append(data); QCOMPARE(group.dataCount(), (unsigned int)1); const ContactGroup::Data data2 = group.data(0); QCOMPARE(data, data2); group.remove(data); QCOMPARE(group.dataCount(), (unsigned int)0); } // mimetype test { ContactGroup group(groupName); QCOMPARE(group.mimeType(), QLatin1String("application/x-vnd.kde.contactgroup")); } } void ContactGroupTest::testGroupRoundTrip() { // TODO should also test empty group ContactGroup group(QLatin1String("TestGroup")); group.append(ContactGroup::ContactReference(QLatin1String("Xggdjetw"))); ContactGroup::ContactReference gidReference; gidReference.setGid(QLatin1String("gid")); group.append(gidReference); group.append(ContactGroup::ContactGroupReference(QLatin1String("aaXggdjetw"))); group.append(ContactGroup::Data(QLatin1String("Tobias Koenig"), QLatin1String("tokoe@kde.org"))); group.append(ContactGroup::Data(QLatin1String("Kevin Krammer"), QLatin1String("kevin.krammer@gmx.at"))); QBuffer buffer; buffer.open(QIODevice::WriteOnly); QString errorMessage; bool result = ContactGroupTool::convertToXml(group, &buffer, &errorMessage); QVERIFY(result); QVERIFY(errorMessage.isEmpty()); buffer.close(); QVERIFY(buffer.size() > 0); buffer.open(QIODevice::ReadOnly); ContactGroup group2; result = ContactGroupTool::convertFromXml(&buffer, group2, &errorMessage); QVERIFY(result); QVERIFY(errorMessage.isEmpty()); QCOMPARE(group, group2); } void ContactGroupTest::testGroupListRoundTrip() { // TODO should also test empty list ContactGroup::List list; ContactGroup group1(QLatin1String("TestGroup1")); group1.append(ContactGroup::ContactReference(QLatin1String("Xggdjetw"))); group1.append(ContactGroup::Data(QLatin1String("Tobias Koenig"), QLatin1String("tokoe@kde.org"))); group1.append(ContactGroup::Data(QLatin1String("Kevin Krammer"), QLatin1String("kevin.krammer@gmx.at"))); list.append(group1); ContactGroup group2(QLatin1String("TestGroup2")); group2.append(ContactGroup::ContactReference(QLatin1String("Xggdjetw"))); group2.append(ContactGroup::Data(QLatin1String("Tobias Koenig"), QLatin1String("tokoe@kde.org"))); group2.append(ContactGroup::Data(QLatin1String("Kevin Krammer"), QLatin1String("kevin.krammer@gmx.at"))); list.append(group2); QBuffer buffer; buffer.open(QIODevice::WriteOnly); QString errorMessage; bool result = ContactGroupTool::convertToXml(list, &buffer, &errorMessage); QVERIFY(result); QVERIFY(errorMessage.isEmpty()); buffer.close(); QVERIFY(buffer.size() > 0); buffer.open(QIODevice::ReadOnly); ContactGroup::List list2; result = ContactGroupTool::convertFromXml(&buffer, list2, &errorMessage); QVERIFY(result); QVERIFY(errorMessage.isEmpty()); QVERIFY(list2.size() == 2); QCOMPARE(list2[0], group1); QCOMPARE(list2[1], group2); } #include "contactgrouptest.moc" diff --git a/autotests/ldifconvertertest.cpp b/autotests/ldifconvertertest.cpp index a7c44b33..5c3644c0 100644 --- a/autotests/ldifconvertertest.cpp +++ b/autotests/ldifconvertertest.cpp @@ -1,548 +1,567 @@ /* This file is part of libkabc. Copyright (C) 2015-2019 Laurent Montel This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "ldifconvertertest.h" #include "converter/ldifconverter.h" #include using namespace KContacts; LDifConverterTest::LDifConverterTest(QObject *parent) : QObject(parent) { } LDifConverterTest::~LDifConverterTest() { } void LDifConverterTest::shouldImportEmail() { QString str = QStringLiteral("dn: cn=laurent,mail=foo@kde.org\n" "sn: laurent\n" "cn: laurent\n" "uid: d1d5cdd4-7d5d-484b-828d-58864d8efe74\n" "mail: foo@kde.org\n" "objectclass: top_n" "objectclass: person\n" "objectclass: organizationalPerson"); AddresseeList lst; ContactGroup::List contactGroup; bool result = LDIFConverter::LDIFToAddressee(str, lst, contactGroup); QVERIFY(result); QCOMPARE(lst.count(), 1); QCOMPARE(lst.at(0).emails().count(), 1); QCOMPARE(lst.at(0).emails().at(0), QStringLiteral("foo@kde.org")); QCOMPARE(contactGroup.count(), 0); } void LDifConverterTest::shouldImportMultiEmails() { QString str = QStringLiteral("dn: cn=laurent,mail=foo@kde.org\n" "sn: laurent\n" "cn: laurent\n" "uid: d1d5cdd4-7d5d-484b-828d-58864d8efe74\n" "mail: foo@kde.org\n" "mail: foo2@kde.org\n" "objectclass: top_n" "objectclass: person\n" "objectclass: organizationalPerson"); AddresseeList lst; ContactGroup::List contactGroup; bool result = LDIFConverter::LDIFToAddressee(str, lst, contactGroup); QVERIFY(result); QCOMPARE(lst.count(), 1); QCOMPARE(lst.at(0).emails().count(), 2); QCOMPARE(lst.at(0).emails().at(0), QStringLiteral("foo@kde.org")); QCOMPARE(lst.at(0).emails().at(1), QStringLiteral("foo2@kde.org")); QCOMPARE(contactGroup.count(), 0); } void LDifConverterTest::shouldImportStandardBirthday() { QString str = QStringLiteral("dn: cn=laurent,mail=foo@kde.org\n" "sn: laurent\n" "cn: laurent\n" "uid: d1d5cdd4-7d5d-484b-828d-58864d8efe74\n" "birthyear: 2015\n" "birthmonth: 3\n" "birthday: 19\n" "mail: foo@kde.org\n" "objectclass: top_n" "objectclass: person\n" "objectclass: organizationalPerson"); AddresseeList lst; ContactGroup::List contactGroup; bool result = LDIFConverter::LDIFToAddressee(str, lst, contactGroup); QVERIFY(result); QCOMPARE(lst.count(), 1); QVERIFY(lst.at(0).birthday().date().isValid()); QCOMPARE(lst.at(0).birthday().date(), QDate(2015, 3, 19)); QVERIFY(!lst.at(0).birthdayHasTime()); QCOMPARE(contactGroup.count(), 0); } void LDifConverterTest::shouldImportStandardBirthdayWithoutYear() { QString str = QStringLiteral("dn: cn=laurent,mail=foo@kde.org\n" "sn: laurent\n" "cn: laurent\n" "uid: d1d5cdd4-7d5d-484b-828d-58864d8efe74\n" "birthmonth: 3\n" "birthday: 19\n" "mail: foo@kde.org\n" "objectclass: top_n" "objectclass: person\n" "objectclass: organizationalPerson"); AddresseeList lst; ContactGroup::List contactGroup; bool result = LDIFConverter::LDIFToAddressee(str, lst, contactGroup); QVERIFY(result); QCOMPARE(lst.count(), 1); QVERIFY(lst.at(0).birthday().date().isValid()); QCOMPARE(lst.at(0).birthday().date(), QDate(-1, 3, 19)); QVERIFY(!lst.at(0).birthdayHasTime()); QCOMPARE(contactGroup.count(), 0); } void LDifConverterTest::shouldImportTheBatsBirthday() { QString str = QStringLiteral("dn: cn=laurent,mail=foo@kde.org\n" "sn: laurent\n" "cn: laurent\n" "uid: d1d5cdd4-7d5d-484b-828d-58864d8efe74\n" "xbatBirthday: 20150319\n" "mail: foo@kde.org\n" "objectclass: top_n" "objectclass: person\n" "objectclass: organizationalPerson"); AddresseeList lst; ContactGroup::List contactGroup; bool result = LDIFConverter::LDIFToAddressee(str, lst, contactGroup); QVERIFY(result); QCOMPARE(lst.count(), 1); QVERIFY(lst.at(0).birthday().date().isValid()); QCOMPARE(lst.at(0).birthday().date(), QDate(2015, 3, 19)); QVERIFY(!lst.at(0).birthdayHasTime()); QCOMPARE(contactGroup.count(), 0); } void LDifConverterTest::shouldImportTheBatsEmails() { QString str = QStringLiteral("dn: cn=laurent,mail=foo@kde.org\n" "sn: laurent\n" "cn: laurent\n" "uid: d1d5cdd4-7d5d-484b-828d-58864d8efe74\n" "mail: foo@kde.org\n" "othermailbox: foo2@kde.org\n" "othermailbox: foo3@kde.org\n" "objectclass: top_n" "objectclass: person\n" "objectclass: organizationalPerson"); AddresseeList lst; ContactGroup::List contactGroup; bool result = LDIFConverter::LDIFToAddressee(str, lst, contactGroup); QVERIFY(result); QCOMPARE(lst.count(), 1); QCOMPARE(lst.at(0).emails().count(), 3); QCOMPARE(lst.at(0).emails().at(0), QStringLiteral("foo@kde.org")); QCOMPARE(lst.at(0).emails().at(1), QStringLiteral("foo2@kde.org")); QCOMPARE(lst.at(0).emails().at(2), QStringLiteral("foo3@kde.org")); QCOMPARE(contactGroup.count(), 0); } void LDifConverterTest::shouldImportTitle() { QString str = QStringLiteral("dn: cn=laurent,mail=foo@kde.org\n" "sn: laurent\n" "cn: laurent\n" "uid: d1d5cdd4-7d5d-484b-828d-58864d8efe74\n" "title: foo\n" "mail: foo@kde.org\n" "objectclass: top_n" "objectclass: person\n" "objectclass: organizationalPerson"); AddresseeList lst; ContactGroup::List contactGroup; bool result = LDIFConverter::LDIFToAddressee(str, lst, contactGroup); QVERIFY(result); QCOMPARE(lst.count(), 1); QCOMPARE(lst.at(0).title(), QStringLiteral("foo")); QCOMPARE(contactGroup.count(), 0); } void LDifConverterTest::shouldImportWorkStreet() { QString str = QStringLiteral("dn: cn=laurent,mail=foo@kde.org\n" "sn: laurent\n" "cn: laurent\n" "uid: d1d5cdd4-7d5d-484b-828d-58864d8efe74\n" "title: foo\n" "mail: foo@kde.org\n" "street: work address\n" "mozillaWorkStreet2: work address next\n" "objectclass: top_n" "objectclass: person\n" "objectclass: organizationalPerson"); AddresseeList lst; ContactGroup::List contactGroup; bool result = LDIFConverter::LDIFToAddressee(str, lst, contactGroup); QVERIFY(result); QCOMPARE(lst.count(), 1); QCOMPARE(lst.at(0).address(Address::Work).street(), QStringLiteral("work address\nwork address next")); QCOMPARE(contactGroup.count(), 0); } void LDifConverterTest::shouldImportContactGroup() { QString str = QStringLiteral("dn: cn=test,mail=\n" "cn: test\n" "modifyTimeStamp: 20080526T234914Z\n" "display-name: Test\n" "objectclass: top\n" "objectclass: groupOfNames\n" "member: cn=Jim Doe,mail=jim.doe@foobar.com\n" "member: cn=Jane Doe,mail=jane.doe@foobar.com\n" "member: cn=John Doe,mail=john.doe@foobar.com\n"); AddresseeList lst; ContactGroup::List contactGroup; bool result = LDIFConverter::LDIFToAddressee(str, lst, contactGroup); QVERIFY(result); QCOMPARE(lst.count(), 0); QCOMPARE(contactGroup.count(), 1); ContactGroup first = contactGroup.first(); QCOMPARE(first.name(), QStringLiteral("Test")); QCOMPARE((int)first.count(), 3); } void LDifConverterTest::shouldImportMultiEntries() { QString str = QStringLiteral("dn: cn=test1,mail=test1@test.test\n" "sn: test1\n" "cn: test1\n" "uid: jpgdf2NrLQ\n" "mail: test1@test.test\n" "modifytimestamp: 20121219T140848Z\n" "objectclass: top\n" "objectclass: person\n" "objectclass: organizationalPerson\n" "\n" "dn: cn=test2,mail=test2@test.test\n" "sn: test2\n" "cn: test2\n" "uid: ow2mwdUb6A\n" "mail: test2@test.test\n" "objectclass: top\n" "objectclass: person\n" "objectclass: organizationalPerson\n"); AddresseeList lst; ContactGroup::List contactGroup; bool result = LDIFConverter::LDIFToAddressee(str, lst, contactGroup); QVERIFY(result); QCOMPARE(lst.count(), 2); QCOMPARE(contactGroup.count(), 0); QCOMPARE(lst.at(0).emails().at(0), QStringLiteral("test1@test.test")); QCOMPARE(lst.at(1).emails().at(0), QStringLiteral("test2@test.test")); } void LDifConverterTest::shouldImportGroupAndAddress() { QString str = QStringLiteral("dn: cn=laurent,mail=foo@kde.org\n" "sn: laurent\n" "cn: laurent\n" "uid: d1d5cdd4-7d5d-484b-828d-58864d8efe74\n" "title: foo\n" "mail: foo@kde.org\n" "street: work address\n" "mozillaWorkStreet2: work address next\n" "objectclass: top_n" "objectclass: person\n" "objectclass: organizationalPerson\n" "\n" "dn: cn=test,mail=\n" "cn: test\n" "modifyTimeStamp: 20080526T234914Z\n" "display-name: Test\n" "objectclass: top\n" "objectclass: groupOfNames\n" "member: cn=Jim Doe,mail=jim.doe@foobar.com\n" "member: cn=Jane Doe,mail=jane.doe@foobar.com\n" "member: cn=John Doe,mail=john.doe@foobar.com\n"); AddresseeList lst; ContactGroup::List contactGroup; bool result = LDIFConverter::LDIFToAddressee(str, lst, contactGroup); QVERIFY(result); QCOMPARE(lst.count(), 1); QCOMPARE(contactGroup.count(), 1); } void LDifConverterTest::shouldExportEmail() { AddresseeList lst; ContactGroup::List contactGroup; Addressee addr; addr.setEmails(QStringList() << QStringLiteral("foo@kde.org")); addr.setUid(QStringLiteral("testuid")); lst << addr; QString str; bool result = LDIFConverter::addresseeAndContactGroupToLDIF(lst, contactGroup, str); QVERIFY(result); QString expected = QStringLiteral("dn: cn=,mail=foo@kde.org\n" "objectclass: top\n" "objectclass: person\n" "objectclass: organizationalPerson\n" "uid: testuid\n" "mail: foo@kde.org\n\n"); QCOMPARE(str, expected); } void LDifConverterTest::shouldExportMultiEmails() { AddresseeList lst; ContactGroup::List contactGroup; Addressee addr; addr.setEmails(QStringList() << QStringLiteral("foo@kde.org") << QStringLiteral("foo2@kde.org") << QStringLiteral("foo3@kde.org")); addr.setUid(QStringLiteral("testuid")); lst << addr; QString str; bool result = LDIFConverter::addresseeAndContactGroupToLDIF(lst, contactGroup, str); QVERIFY(result); QString expected = QStringLiteral("dn: cn=,mail=foo@kde.org\n" "objectclass: top\n" "objectclass: person\n" "objectclass: organizationalPerson\n" "uid: testuid\n" "mail: foo@kde.org\n" "mozillasecondemail: foo2@kde.org\n" "othermailbox: foo3@kde.org\n" "\n"); QCOMPARE(str, expected); } void LDifConverterTest::shouldExportBirthday() { AddresseeList lst; ContactGroup::List contactGroup; Addressee addr; QDate date(2015, 3, 3); addr.setBirthday(date); addr.setEmails(QStringList() << QStringLiteral("foo@kde.org")); addr.setUid(QStringLiteral("testuid")); lst << addr; QString str; bool result = LDIFConverter::addresseeAndContactGroupToLDIF(lst, contactGroup, str); QVERIFY(result); QString expected = QStringLiteral("dn: cn=,mail=foo@kde.org\n" "objectclass: top\n" "objectclass: person\n" "objectclass: organizationalPerson\n" "uid: testuid\n" "mail: foo@kde.org\n" "birthyear: 2015\n" "birthmonth: 3\n" "birthday: 3\n" "\n"); QCOMPARE(str, expected); } void LDifConverterTest::shouldExportBirthdayWithoutYear() { AddresseeList lst; ContactGroup::List contactGroup; Addressee addr; QDate date(-1, 3, 3); addr.setBirthday(date); addr.setEmails(QStringList() << QStringLiteral("foo@kde.org")); addr.setUid(QStringLiteral("testuid")); lst << addr; QString str; bool result = LDIFConverter::addresseeAndContactGroupToLDIF(lst, contactGroup, str); QVERIFY(result); QString expected = QStringLiteral("dn: cn=,mail=foo@kde.org\n" "objectclass: top\n" "objectclass: person\n" "objectclass: organizationalPerson\n" "uid: testuid\n" "mail: foo@kde.org\n" "birthmonth: 3\n" "birthday: 3\n" "\n"); QCOMPARE(str, expected); } void LDifConverterTest::shouldExportTitle() { AddresseeList lst; ContactGroup::List contactGroup; Addressee addr; addr.setEmails(QStringList() << QStringLiteral("foo@kde.org")); addr.setTitle(QStringLiteral("foo")); addr.setUid(QStringLiteral("testuid")); lst << addr; QString str; bool result = LDIFConverter::addresseeAndContactGroupToLDIF(lst, contactGroup, str); QVERIFY(result); QString expected = QStringLiteral("dn: cn=,mail=foo@kde.org\n" "objectclass: top\n" "objectclass: person\n" "objectclass: organizationalPerson\n" "uid: testuid\n" "mail: foo@kde.org\n" "title: foo\n" "\n"); QCOMPARE(str, expected); } void LDifConverterTest::shouldExportMultiEntries() { AddresseeList lst; ContactGroup::List contactGroup; Addressee addr; addr.setEmails(QStringList() << QStringLiteral("foo@kde.org")); addr.setTitle(QStringLiteral("foo")); addr.setUid(QStringLiteral("testuid")); lst << addr; Addressee addr2; QDate date(2015, 3, 3); addr2.setBirthday(date); addr2.setEmails(QStringList() << QStringLiteral("foo@kde.org")); addr2.setUid(QStringLiteral("testuid")); lst << addr2; QString str; bool result = LDIFConverter::addresseeAndContactGroupToLDIF(lst, contactGroup, str); QVERIFY(result); const QString expected = QStringLiteral("dn: cn=,mail=foo@kde.org\n" "objectclass: top\n" "objectclass: person\n" "objectclass: organizationalPerson\n" "uid: testuid\n" "mail: foo@kde.org\n" "title: foo\n" "\n" "dn: cn=,mail=foo@kde.org\n" "objectclass: top\n" "objectclass: person\n" "objectclass: organizationalPerson\n" "uid: testuid\n" "mail: foo@kde.org\n" "birthyear: 2015\n" "birthmonth: 3\n" "birthday: 3\n" "\n"); QCOMPARE(str, expected); } void LDifConverterTest::shouldExportGroup() { AddresseeList lst; ContactGroup::List contactGroup; ContactGroup grp; ContactGroup::Data data; data.setEmail(QStringLiteral("foo@kde.org")); data.setName(QStringLiteral("foo")); grp.append(data); ContactGroup::Data data2; data2.setEmail(QStringLiteral("foo2@kde.org")); data2.setName(QStringLiteral("foo2")); grp.append(data2); contactGroup.append(grp); QString str; bool result = LDIFConverter::addresseeAndContactGroupToLDIF(lst, contactGroup, str); QVERIFY(result); const QString expected = QStringLiteral("objectclass: top\n" "objectclass: groupOfNames\n" "member: cn=foo,mail=foo@kde.org\n" "member: cn=foo2,mail=foo2@kde.org\n" "\n"); QCOMPARE(str, expected); } void LDifConverterTest::shouldExportWorkStreet() { AddresseeList lst; Addressee addr; addr.setEmails(QStringList() << QStringLiteral("foo@kde.org")); addr.setUid(QStringLiteral("testuid")); Address address(Address::Work); address.setStreet(QStringLiteral("work address")); address.setPostalCode(QStringLiteral("postal")); addr.insertAddress(address); lst << addr; ContactGroup::List contactGroup; QString str; bool result = LDIFConverter::addresseeAndContactGroupToLDIF(lst, contactGroup, str); QVERIFY(result); const QString expected = QStringLiteral("dn: cn=,mail=foo@kde.org\n" "objectclass: top\n" "objectclass: person\n" "objectclass: organizationalPerson\n" "uid: testuid\n" "mail: foo@kde.org\n" "postalcode: postal\n" "streetaddress: work address\n" "street: work address\n" "\n"); QCOMPARE(str, expected); } void LDifConverterTest::shouldExportFullName() { AddresseeList lst; Addressee addr; addr.setEmails(QStringList() << QStringLiteral("foo@kde.org")); addr.setUid(QStringLiteral("testuid")); addr.setName(QStringLiteral("name")); addr.setNickName(QStringLiteral("nickname")); addr.setFamilyName(QStringLiteral("familyname")); lst << addr; ContactGroup::List contactGroup; QString str; bool result = LDIFConverter::addresseeAndContactGroupToLDIF(lst, contactGroup, str); QVERIFY(result); const QString expected = QStringLiteral("dn: cn=,mail=foo@kde.org\n" "objectclass: top\n" "objectclass: person\n" "objectclass: organizationalPerson\n" "sn: familyname\n" "uid: testuid\n" "nickname: nickname\n" "xmozillanickname: nickname\n" "mozillanickname: nickname\n" "mail: foo@kde.org\n" "\n"); QCOMPARE(str, expected); } +void LDifConverterTest::testGarbage() +{ + AddresseeList lst; + ContactGroup::List contactGroup; + QString str; + bool result; + + result = LDIFConverter::addresseeAndContactGroupToLDIF(lst, contactGroup, str); + QVERIFY(!result); + result = LDIFConverter::contactGroupToLDIF(contactGroup, str); + QVERIFY(!result); + result = LDIFConverter::addresseeToLDIF(lst, str); + QVERIFY(!result); + + Addressee addr; + result = LDIFConverter::addresseeToLDIF(addr, str); + QVERIFY(!result); +} + QTEST_MAIN(LDifConverterTest) diff --git a/autotests/ldifconvertertest.h b/autotests/ldifconvertertest.h index 408c4d08..d89241dc 100644 --- a/autotests/ldifconvertertest.h +++ b/autotests/ldifconvertertest.h @@ -1,59 +1,62 @@ /* This file is part of libkabc. Copyright (C) 2015-2019 Laurent Montel This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef LDIFCONVERTERTEST_H #define LDIFCONVERTERTEST_H #include class LDifConverterTest : public QObject { Q_OBJECT public: explicit LDifConverterTest(QObject *parent = nullptr); ~LDifConverterTest(); private Q_SLOTS: //Import void shouldImportEmail(); void shouldImportMultiEmails(); void shouldImportStandardBirthday(); void shouldImportStandardBirthdayWithoutYear(); void shouldImportTheBatsBirthday(); void shouldImportTheBatsEmails(); void shouldImportTitle(); void shouldImportWorkStreet(); void shouldImportContactGroup(); void shouldImportMultiEntries(); void shouldImportGroupAndAddress(); //Export void shouldExportEmail(); void shouldExportBirthday(); void shouldExportBirthdayWithoutYear(); void shouldExportTitle(); void shouldExportMultiEntries(); void shouldExportGroup(); void shouldExportWorkStreet(); void shouldExportMultiEmails(); void shouldExportFullName(); + + //Garbage tests + void testGarbage(); }; #endif // LDIFCONVERTERTEST_H diff --git a/autotests/picturetest.cpp b/autotests/picturetest.cpp index 9398d6f6..1405b244 100644 --- a/autotests/picturetest.cpp +++ b/autotests/picturetest.cpp @@ -1,267 +1,267 @@ /* This file is part of the KContacts framework. Copyright (c) 2007 Tobias Koenig This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "picturetest.h" #include "kcontacts/picture.h" #include #include #include QTEST_MAIN(PictureTest) static QImage testImage() { static QImage image; if (image.isNull()) { image = QImage(200, 200, QImage::Format_RGB32); QPainter p(&image); p.drawRect(10, 10, 50, 50); } return image; } static QByteArray testImageRawPNG() { static QByteArray raw; if (raw.isNull()) { QBuffer buffer(&raw); buffer.open(QIODevice::WriteOnly); testImage().save(&buffer, "PNG"); } return raw; } static QByteArray testImageRawJPEG() { static QByteArray raw; if (raw.isNull()) { QBuffer buffer(&raw); buffer.open(QIODevice::WriteOnly); testImage().save(&buffer, "JPEG"); } return raw; } void PictureTest::emptyTest() { KContacts::Picture picture; QVERIFY(picture.isEmpty()); } void PictureTest::storeTestInternImage() { KContacts::Picture picture; picture.setData(testImage()); QVERIFY(picture.isEmpty() == false); QVERIFY(picture.isIntern() == true); QVERIFY(picture.type() == QLatin1String("jpeg")); QVERIFY(picture.data() == testImage()); QVERIFY(picture.rawData() == testImageRawJPEG()); } void PictureTest::storeTestInternRawData() { KContacts::Picture picture; picture.setRawData(testImageRawPNG(), QStringLiteral("png")); QVERIFY(picture.isEmpty() == false); QVERIFY(picture.isIntern() == true); QVERIFY(picture.type() == QLatin1String("png")); QVERIFY(picture.rawData() == testImageRawPNG()); QVERIFY(picture.data() == testImage()); } void PictureTest::storeTestExtern() { KContacts::Picture picture; picture.setUrl(QStringLiteral("http://myhomepage.com/foto.png"), QStringLiteral("png")); QVERIFY(picture.isEmpty() == false); QVERIFY(picture.isIntern() == false); QVERIFY(picture.type() == QLatin1String("png")); QVERIFY(picture.url() == QLatin1String("http://myhomepage.com/foto.png")); } void PictureTest::equalsTestInternImage() { KContacts::Picture picture1, picture2; picture1.setData(testImage()); picture2.setData(testImage()); QVERIFY(picture1 == picture2); // access rawData() so a QByteArray is created - picture1.rawData(); + QVERIFY(!picture1.rawData().isNull()); QVERIFY(picture1 == picture2); } void PictureTest::equalsTestInternRawData() { KContacts::Picture picture1, picture2; picture1.setRawData(testImageRawPNG(), QStringLiteral("png")); picture2.setRawData(testImageRawPNG(), QStringLiteral("png")); QVERIFY(picture1 == picture2); // access data() so a QImage is created - picture1.data(); + QVERIFY(!picture1.data().isNull()); QVERIFY(picture1 == picture2); } void PictureTest::equalsTestInternImageAndRawData() { KContacts::Picture picture1, picture2; picture1.setData(testImage()); picture2.setRawData(testImageRawJPEG(), QStringLiteral("jpeg")); QVERIFY(picture1.rawData() == picture2.rawData()); } void PictureTest::equalsTestExtern() { KContacts::Picture picture1, picture2; picture1.setUrl(QStringLiteral("http://myhomepage.com/foto.png"), QStringLiteral("png")); picture2.setUrl(QStringLiteral("http://myhomepage.com/foto.png"), QStringLiteral("png")); QVERIFY(picture1 == picture2); } void PictureTest::differsTest() { KContacts::Picture picture1, picture2; picture1.setUrl(QStringLiteral("http://myhomepage.com/foto.png"), QStringLiteral("png")); picture2.setData(testImage()); QVERIFY(picture1 != picture2); } void PictureTest::differsTestInternRawData() { KContacts::Picture picture1, picture2; picture1.setRawData(testImageRawJPEG(), QStringLiteral("jpeg")); picture2.setRawData(testImageRawPNG(), QStringLiteral("png")); QVERIFY(picture1 != picture2); QVERIFY(picture1.rawData() != picture2.rawData()); } void PictureTest::differsTestExtern() { KContacts::Picture picture1, picture2; picture1.setUrl(QStringLiteral("http://myhomepage.com/foto.png"), QStringLiteral("png")); picture1.setUrl(QStringLiteral("http://myhomepage.com/foto2.png"), QStringLiteral("png")); QVERIFY(picture1 != picture2); } void PictureTest::assignmentTestIntern() { KContacts::Picture picture1, picture2; picture1.setData(testImage()); picture2 = picture1; QVERIFY(picture1 == picture2); } void PictureTest::assignmentTestExtern() { KContacts::Picture picture1, picture2; picture1.setUrl(QStringLiteral("http://myhomepage.com/foto.png"), QStringLiteral("png")); picture2 = picture1; QVERIFY(picture1 == picture2); } void PictureTest::serializeTestInternImage() { KContacts::Picture picture1, picture2; picture1.setData(testImage()); QByteArray data; QDataStream s(&data, QIODevice::WriteOnly); s << picture1; QDataStream t(&data, QIODevice::ReadOnly); t >> picture2; QVERIFY(picture1 == picture2); } void PictureTest::serializeTestInternRawData() { KContacts::Picture picture1, picture2; picture1.setRawData(testImageRawPNG(), QStringLiteral("png")); QByteArray data; QDataStream s(&data, QIODevice::WriteOnly); s << picture1; QDataStream t(&data, QIODevice::ReadOnly); t >> picture2; QVERIFY(picture1 == picture2); } void PictureTest::serializeTestExtern() { KContacts::Picture picture1, picture2; picture1.setUrl(QStringLiteral("http://myhomepage.com/foto.png"), QStringLiteral("png")); QByteArray data; QDataStream s(&data, QIODevice::WriteOnly); s << picture1; QDataStream t(&data, QIODevice::ReadOnly); t >> picture2; QVERIFY(picture1 == picture2); } diff --git a/src/converter/ldifconverter.cpp b/src/converter/ldifconverter.cpp index be717218..3ad62526 100644 --- a/src/converter/ldifconverter.cpp +++ b/src/converter/ldifconverter.cpp @@ -1,638 +1,648 @@ /* This file is part of the KContacts framework. Copyright (c) 2003 Helge Deller This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ /* Useful links: - http://tldp.org/HOWTO/LDAP-Implementation-HOWTO/schemas.html - http://www.faqs.org/rfcs/rfc2849.html Not yet handled items: - objectclass microsoftaddressbook - info, - initials, - otherfacsimiletelephonenumber, - otherpager, - physicaldeliveryofficename, */ #include "ldifconverter.h" #include "vcardconverter.h" #include "address.h" #include "addressee.h" #include "kcontacts_debug.h" #include "ldif_p.h" #include #include #include #include using namespace KContacts; /* internal functions - do not use !! */ namespace KContacts { /** @internal Evaluates @p fieldname and sets the @p value at the addressee or the address objects when appropriate. @param a The addressee to store information into @param homeAddr The home address to store respective information into @param workAddr The work address to store respective information into @param fieldname LDIF field name to evaluate @param value The value of the field addressed by @p fieldname */ void evaluatePair(Addressee &a, Address &homeAddr, Address &workAddr, QString &fieldname, QString &value, int &birthday, int &birthmonth, int &birthyear, ContactGroup &contactGroup); } /* generate LDIF stream */ static void ldif_out(QTextStream &t, const QString &formatStr, const QString &value) { if (value.isEmpty()) { return; } const QByteArray txt = Ldif::assembleLine(formatStr, value, 72); // write the string t << QString::fromUtf8(txt) << "\n"; } bool LDIFConverter::addresseeAndContactGroupToLDIF(const AddresseeList &addrList, const ContactGroup::List &contactGroupList, QString &str) { bool result = addresseeToLDIF(addrList, str); if (!contactGroupList.isEmpty()) { - result = contactGroupToLDIF(contactGroupList, str); + result = (contactGroupToLDIF(contactGroupList, str) || result); //order matters } return result; } bool LDIFConverter::contactGroupToLDIF(const ContactGroup &contactGroup, QString &str) { if (contactGroup.dataCount() <= 0) { return false; } QTextStream t(&str, QIODevice::WriteOnly | QIODevice::Append); t.setCodec(QTextCodec::codecForName("UTF-8")); t << "objectclass: top\n"; t << "objectclass: groupOfNames\n"; for (int i = 0; i < contactGroup.dataCount(); ++i) { ContactGroup::Data data = contactGroup.data(i); const QString value = QStringLiteral("cn=%1,mail=%2").arg(data.name(), data.email()); ldif_out(t, QStringLiteral("member"), value); } t << "\n"; return true; } bool LDIFConverter::contactGroupToLDIF(const ContactGroup::List &contactGroupList, QString &str) { + if (contactGroupList.count() <= 0) { + return false; + } + + bool result = true; ContactGroup::List::ConstIterator it; const ContactGroup::List::ConstIterator end(contactGroupList.constEnd()); for (it = contactGroupList.constBegin(); it != end; ++it) { - contactGroupToLDIF(*it, str); + result = (contactGroupToLDIF(*it, str) || result); //order matters } - return true; + return result; } bool LDIFConverter::addresseeToLDIF(const AddresseeList &addrList, QString &str) { + if (addrList.count() <= 0) { + return false; + } + + bool result = true; AddresseeList::ConstIterator it; const AddresseeList::ConstIterator end(addrList.constEnd()); for (it = addrList.constBegin(); it != end; ++it) { - addresseeToLDIF(*it, str); + result = (addresseeToLDIF(*it, str) || result); //order matters } - return true; + return result; } bool LDIFConverter::addresseeToLDIF(const Addressee &addr, QString &str) { if (addr.isEmpty()) { return false; } QTextStream t(&str, QIODevice::WriteOnly | QIODevice::Append); t.setCodec(QTextCodec::codecForName("UTF-8")); const Address homeAddr = addr.address(Address::Home); const Address workAddr = addr.address(Address::Work); ldif_out(t, QStringLiteral("dn"), QStringLiteral("cn=%1,mail=%2"). arg(addr.formattedName().simplified(), addr.preferredEmail())); t << "objectclass: top\n"; t << "objectclass: person\n"; t << "objectclass: organizationalPerson\n"; ldif_out(t, QStringLiteral("givenname"), addr.givenName()); ldif_out(t, QStringLiteral("sn"), addr.familyName()); ldif_out(t, QStringLiteral("cn"), addr.formattedName().simplified()); ldif_out(t, QStringLiteral("uid"), addr.uid()); ldif_out(t, QStringLiteral("nickname"), addr.nickName()); ldif_out(t, QStringLiteral("xmozillanickname"), addr.nickName()); ldif_out(t, QStringLiteral("mozillanickname"), addr.nickName()); ldif_out(t, QStringLiteral("mail"), addr.preferredEmail()); const QStringList emails = addr.emails(); const int numEmails = emails.count(); for (int i = 1; i < numEmails; ++i) { if (i == 0) { //nothing } else if (i == 1) { ldif_out(t, QStringLiteral("mozillasecondemail"), emails[1]); } else { ldif_out(t, QStringLiteral("othermailbox"), emails[i]); } } //ldif_out( t, "mozilla_AIMScreenName: %1\n", "screen_name" ); ldif_out(t, QStringLiteral("telephonenumber"), addr.phoneNumber(PhoneNumber::Work).number()); ldif_out(t, QStringLiteral("facsimiletelephonenumber"), addr.phoneNumber(PhoneNumber::Fax).number()); ldif_out(t, QStringLiteral("homephone"), addr.phoneNumber(PhoneNumber::Home).number()); ldif_out(t, QStringLiteral("mobile"), addr.phoneNumber(PhoneNumber::Cell).number()); // Netscape 7 ldif_out(t, QStringLiteral("cellphone"), addr.phoneNumber(PhoneNumber::Cell).number()); // Netscape 4.x ldif_out(t, QStringLiteral("pager"), addr.phoneNumber(PhoneNumber::Pager).number()); ldif_out(t, QStringLiteral("pagerphone"), addr.phoneNumber(PhoneNumber::Pager).number()); ldif_out(t, QStringLiteral("streethomeaddress"), homeAddr.street()); ldif_out(t, QStringLiteral("postalcode"), workAddr.postalCode()); ldif_out(t, QStringLiteral("postofficebox"), workAddr.postOfficeBox()); QStringList streets = homeAddr.street().split(QLatin1Char('\n')); const int numberOfStreets(streets.count()); if (numberOfStreets > 0) { ldif_out(t, QStringLiteral("homepostaladdress"), streets.at(0)); // Netscape 7 } if (numberOfStreets > 1) { ldif_out(t, QStringLiteral("mozillahomepostaladdress2"), streets.at(1)); // Netscape 7 } ldif_out(t, QStringLiteral("mozillahomelocalityname"), homeAddr.locality()); // Netscape 7 ldif_out(t, QStringLiteral("mozillahomestate"), homeAddr.region()); ldif_out(t, QStringLiteral("mozillahomepostalcode"), homeAddr.postalCode()); ldif_out(t, QStringLiteral("mozillahomecountryname"), Address::ISOtoCountry(homeAddr.country())); ldif_out(t, QStringLiteral("locality"), workAddr.locality()); ldif_out(t, QStringLiteral("streetaddress"), workAddr.street()); // Netscape 4.x streets = workAddr.street().split(QLatin1Char('\n')); const int streetsCount = streets.count(); if (streetsCount > 0) { ldif_out(t, QStringLiteral("street"), streets.at(0)); } if (streetsCount > 1) { ldif_out(t, QStringLiteral("mozillaworkstreet2"), streets.at(1)); } ldif_out(t, QStringLiteral("countryname"), Address::ISOtoCountry(workAddr.country())); ldif_out(t, QStringLiteral("l"), workAddr.locality()); ldif_out(t, QStringLiteral("c"), Address::ISOtoCountry(workAddr.country())); ldif_out(t, QStringLiteral("st"), workAddr.region()); ldif_out(t, QStringLiteral("title"), addr.title()); ldif_out(t, QStringLiteral("vocation"), addr.prefix()); ldif_out(t, QStringLiteral("ou"), addr.role()); ldif_out(t, QStringLiteral("o"), addr.organization()); ldif_out(t, QStringLiteral("organization"), addr.organization()); ldif_out(t, QStringLiteral("organizationname"), addr.organization()); // Compatibility with older kabc versions. if (!addr.department().isEmpty()) { ldif_out(t, QStringLiteral("department"), addr.department()); } else { ldif_out(t, QStringLiteral("department"), addr.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-Department"))); } ldif_out(t, QStringLiteral("workurl"), addr.url().url().toDisplayString()); ldif_out(t, QStringLiteral("homeurl"), addr.url().url().toDisplayString()); ldif_out(t, QStringLiteral("mozillahomeurl"), addr.url().url().toDisplayString()); ldif_out(t, QStringLiteral("description"), addr.note()); if (addr.revision().isValid()) { ldif_out(t, QStringLiteral("modifytimestamp"), dateToVCardString(addr.revision())); } const QDate birthday = addr.birthday().date(); if (birthday.isValid()) { const int year = birthday.year(); if (year > 0) { ldif_out(t, QStringLiteral("birthyear"), QString::number(year)); } ldif_out(t, QStringLiteral("birthmonth"), QString::number(birthday.month())); ldif_out(t, QStringLiteral("birthday"), QString::number(birthday.day())); } t << "\n"; return true; } /* convert from LDIF stream */ bool LDIFConverter::LDIFToAddressee(const QString &str, AddresseeList &addrList, ContactGroup::List &contactGroupList, const QDateTime &dt) { if (str.isEmpty()) { return true; } bool endldif = false, end = false; Ldif ldif; Ldif::ParseValue ret; Addressee a; Address homeAddr, workAddr; int birthday = -1; int birthmonth = -1; int birthyear = -1; ContactGroup contactGroup; ldif.setLdif(str.toLatin1()); QDateTime qdt = dt; if (!qdt.isValid()) { qdt = QDateTime::currentDateTime(); } a.setRevision(qdt); homeAddr = Address(Address::Home); workAddr = Address(Address::Work); do { ret = ldif.nextItem(); switch (ret) { case Ldif::Item: { QString fieldname = ldif.attr().toLower(); QString value = QString::fromUtf8(ldif.value()); evaluatePair(a, homeAddr, workAddr, fieldname, value, birthday, birthmonth, birthyear, contactGroup); break; } case Ldif::EndEntry: if (contactGroup.count() == 0) { // if the new address is not empty, append it QDate birthDate(birthyear, birthmonth, birthday); if (birthDate.isValid()) { a.setBirthday(birthDate); } if (!a.formattedName().isEmpty() || !a.name().isEmpty() || !a.familyName().isEmpty()) { if (!homeAddr.isEmpty()) { a.insertAddress(homeAddr); } if (!workAddr.isEmpty()) { a.insertAddress(workAddr); } addrList.append(a); } } else { contactGroupList.append(contactGroup); } a = Addressee(); contactGroup = ContactGroup(); a.setRevision(qdt); homeAddr = Address(Address::Home); workAddr = Address(Address::Work); break; case Ldif::MoreData: if (endldif) { end = true; } else { ldif.endLdif(); endldif = true; break; } default: break; } } while (!end); return true; } void KContacts::evaluatePair(Addressee &a, Address &homeAddr, Address &workAddr, QString &fieldname, QString &value, int &birthday, int &birthmonth, int &birthyear, ContactGroup &contactGroup) { if (fieldname == QLatin1String("dn")) { // ignore return; } if (fieldname.startsWith(QLatin1Char('#'))) { return; } if (fieldname.isEmpty() && !a.note().isEmpty()) { // some LDIF export filters are borken and add additional // comments on stand-alone lines. Just add them to the notes for now. a.setNote(a.note() + QLatin1Char('\n') + value); return; } if (fieldname == QLatin1String("givenname")) { a.setGivenName(value); return; } if (fieldname == QLatin1String("xmozillanickname") || fieldname == QLatin1String("nickname") || fieldname == QLatin1String("mozillanickname")) { a.setNickName(value); return; } if (fieldname == QLatin1String("sn")) { a.setFamilyName(value); return; } if (fieldname == QLatin1String("uid")) { a.setUid(value); return; } if (fieldname == QLatin1String("mail") || fieldname == QLatin1String("mozillasecondemail") /* mozilla */ || fieldname == QLatin1String("othermailbox") /*TheBat!*/) { if (a.emails().indexOf(value) == -1) { a.insertEmail(value); } return; } if (fieldname == QLatin1String("title")) { a.setTitle(value); return; } if (fieldname == QLatin1String("vocation")) { a.setPrefix(value); return; } if (fieldname == QLatin1String("cn")) { a.setFormattedName(value); return; } if (fieldname == QLatin1String("o") || fieldname == QLatin1String("organization") // Exchange || fieldname == QLatin1String("organizationname")) { // Exchange a.setOrganization(value); return; } if (fieldname == QLatin1String("description") || fieldname == QLatin1String("mozillacustom1") || fieldname == QLatin1String("mozillacustom2") || fieldname == QLatin1String("mozillacustom3") || fieldname == QLatin1String("mozillacustom4") || fieldname == QLatin1String("custom1") || fieldname == QLatin1String("custom2") || fieldname == QLatin1String("custom3") || fieldname == QLatin1String("custom4")) { if (!a.note().isEmpty()) { a.setNote(a.note() + QLatin1Char('\n')); } a.setNote(a.note() + value); return; } if (fieldname == QLatin1String("homeurl") || fieldname == QLatin1String("workurl") || fieldname == QLatin1String("mozillahomeurl")) { if (a.url().url().isEmpty()) { ResourceLocatorUrl url; url.setUrl(QUrl(value)); a.setUrl(url); return; } if (a.url().url().toDisplayString() == QUrl(value).toDisplayString()) { return; } // TODO: current version of kabc only supports one URL. // TODO: change this with KDE 4 } if (fieldname == QLatin1String("homephone")) { a.insertPhoneNumber(PhoneNumber(value, PhoneNumber::Home)); return; } if (fieldname == QLatin1String("telephonenumber")) { a.insertPhoneNumber(PhoneNumber(value, PhoneNumber::Work)); return; } if (fieldname == QLatin1String("mobile") /* mozilla/Netscape 7 */ || fieldname == QLatin1String("cellphone")) { a.insertPhoneNumber(PhoneNumber(value, PhoneNumber::Cell)); return; } if (fieldname == QLatin1String("pager") // mozilla || fieldname == QLatin1String("pagerphone")) { // mozilla a.insertPhoneNumber(PhoneNumber(value, PhoneNumber::Pager)); return; } if (fieldname == QLatin1String("facsimiletelephonenumber")) { a.insertPhoneNumber(PhoneNumber(value, PhoneNumber::Fax)); return; } if (fieldname == QLatin1String("xmozillaanyphone")) { // mozilla a.insertPhoneNumber(PhoneNumber(value, PhoneNumber::Work)); return; } if (fieldname == QLatin1String("streethomeaddress") || fieldname == QLatin1String("mozillahomestreet")) { // thunderbird homeAddr.setStreet(value); return; } if (fieldname == QLatin1String("street") || fieldname == QLatin1String("postaladdress")) { // mozilla workAddr.setStreet(value); return; } if (fieldname == QLatin1String("mozillapostaladdress2") || fieldname == QLatin1String("mozillaworkstreet2")) { // mozilla workAddr.setStreet(workAddr.street() + QLatin1Char('\n') + value); return; } if (fieldname == QLatin1String("postalcode")) { workAddr.setPostalCode(value); return; } if (fieldname == QLatin1String("postofficebox")) { workAddr.setPostOfficeBox(value); return; } if (fieldname == QLatin1String("homepostaladdress")) { // Netscape 7 homeAddr.setStreet(value); return; } if (fieldname == QLatin1String("mozillahomepostaladdress2")) { // mozilla homeAddr.setStreet(homeAddr.street() + QLatin1Char('\n') + value); return; } if (fieldname == QLatin1String("mozillahomelocalityname")) { // mozilla homeAddr.setLocality(value); return; } if (fieldname == QLatin1String("mozillahomestate")) { // mozilla homeAddr.setRegion(value); return; } if (fieldname == QLatin1String("mozillahomepostalcode")) { // mozilla homeAddr.setPostalCode(value); return; } if (fieldname == QLatin1String("mozillahomecountryname")) { // mozilla if (value.length() <= 2) { value = Address::ISOtoCountry(value); } homeAddr.setCountry(value); return; } if (fieldname == QLatin1String("locality")) { workAddr.setLocality(value); return; } if (fieldname == QLatin1String("streetaddress")) { // Netscape 4.x workAddr.setStreet(value); return; } if (fieldname == QLatin1String("countryname") || fieldname == QLatin1String("c")) { // mozilla if (value.length() <= 2) { value = Address::ISOtoCountry(value); } workAddr.setCountry(value); return; } if (fieldname == QLatin1String("l")) { // mozilla workAddr.setLocality(value); return; } if (fieldname == QLatin1String("st")) { workAddr.setRegion(value); return; } if (fieldname == QLatin1String("ou")) { a.setRole(value); return; } if (fieldname == QLatin1String("department")) { a.setDepartment(value); return; } if (fieldname == QLatin1String("member")) { // this is a mozilla list member (cn=xxx, mail=yyy) const QStringList list = value.split(QLatin1Char(',')); QString name, email; QStringList::ConstIterator it; const QStringList::ConstIterator end(list.constEnd()); for (it = list.constBegin(); it != end; ++it) { if ((*it).startsWith(QLatin1String("cn="))) { name = (*it).mid(3).trimmed(); } if ((*it).startsWith(QLatin1String("mail="))) { email = (*it).mid(5).trimmed(); } } if (!name.isEmpty() && !email.isEmpty()) { email = QLatin1String(" <") + email + QLatin1Char('>'); } ContactGroup::Data data; data.setEmail(email); data.setName(name); contactGroup.append(data); return; } if (fieldname == QLatin1String("modifytimestamp")) { if (value == QLatin1String("0Z")) { // ignore return; } QDateTime dt = VCardStringToDate(value); if (dt.isValid()) { a.setRevision(dt); return; } } if (fieldname == QLatin1String("display-name")) { contactGroup.setName(value); return; } if (fieldname == QLatin1String("objectclass")) { // ignore return; } if (fieldname == QLatin1String("birthyear")) { bool ok; birthyear = value.toInt(&ok); if (!ok) { birthyear = -1; } return; } if (fieldname == QLatin1String("birthmonth")) { birthmonth = value.toInt(); return; } if (fieldname == QLatin1String("birthday")) { birthday = value.toInt(); return; } if (fieldname == QLatin1String("xbatbirthday")) { QDate dt = QDate::fromString(value, QStringLiteral("yyyyMMdd")); if (dt.isValid()) { a.setBirthday(dt); } return; } qCWarning(KCONTACTS_LOG) << QStringLiteral("LDIFConverter: Unknown field for '%1': '%2=%3'\n"). arg(a.formattedName(), fieldname, value); }