1 /*
2 SPDX-FileCopyrightText: 2012-2021 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "balsaaddressbook.h"
8
9 #include "importwizardutil.h"
10
11 #include <KContacts/Addressee>
12 #include <KContacts/LDIFConverter>
13
14 #include <KConfig>
15 #include <KConfigGroup>
16 #include <KLocalizedString>
17 #include <QUrl>
18 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
19 #include <QTextCodec>
20 #endif
21
22 #include "balsaplugin_debug.h"
23 #include <QFile>
24 #include <QFileInfo>
25 #include <QRegularExpression>
26
BalsaAddressBook(const QString & filename)27 BalsaAddressBook::BalsaAddressBook(const QString &filename)
28 : mFileName(filename)
29 {
30 }
31
~BalsaAddressBook()32 BalsaAddressBook::~BalsaAddressBook()
33 {
34 }
35
importAddressBook()36 void BalsaAddressBook::importAddressBook()
37 {
38 KConfig config(mFileName);
39 const QStringList addressBookList = config.groupList().filter(QRegularExpression(QStringLiteral("address-book-\\d+")));
40 if (addressBookList.isEmpty()) {
41 addAddressBookImportInfo(i18n("No addressbook found"));
42 } else {
43 for (const QString &addressbook : addressBookList) {
44 KConfigGroup grp = config.group(addressbook);
45 readAddressBook(grp);
46 }
47 }
48 }
49
readAddressBook(const KConfigGroup & grp)50 void BalsaAddressBook::readAddressBook(const KConfigGroup &grp)
51 {
52 const QString type = grp.readEntry(QStringLiteral("Type"));
53 if (type.isEmpty()) {
54 addAddressBookImportInfo(i18n("No addressbook found"));
55 return;
56 }
57 const QString name = grp.readEntry(QStringLiteral("Name"));
58
59 if (type == QLatin1String("LibBalsaAddressBookLdap")) {
60 ldapStruct ldap;
61 ldap.dn = grp.readEntry(QStringLiteral("BaseDN"));
62 ldap.useTLS = (grp.readEntry(QStringLiteral("EnableTLS")) == QLatin1String("true"));
63 ldap.ldapUrl = QUrl(grp.readEntry(QStringLiteral("Host")));
64 ldap.port = ldap.ldapUrl.port();
65 // TODO: verify
66 const QString bookDN = grp.readEntry(QStringLiteral("BookDN")); // TODO ?
67 ImportWizardUtil::mergeLdap(ldap);
68 addAddressBookImportInfo(i18n("Ldap created"));
69 } else if (type == QLatin1String("LibBalsaAddressBookGpe")) {
70 qCDebug(BALSAPLUGIN_LOG) << " Import it !";
71 } else if (type == QLatin1String("LibBalsaAddressBookLdif")) {
72 const QString path = grp.readEntry(QStringLiteral("Path"));
73 if (!path.isEmpty()) {
74 KContacts::Addressee::List contacts;
75 KContacts::ContactGroup::List contactsGroup;
76 QFile file(path);
77 if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
78 QTextStream stream(&file);
79 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
80 stream.setCodec(QTextCodec::codecForName("ISO-8859-1"));
81 #else
82 stream.setEncoding(QStringConverter::Encoding::Latin1);
83 #endif
84
85 const QString wholeFile = stream.readAll();
86 const QDateTime dtDefault = QFileInfo(file).lastModified();
87 file.close();
88
89 KContacts::LDIFConverter::LDIFToAddressee(wholeFile, contacts, contactsGroup, dtDefault);
90 for (KContacts::Addressee contact : std::as_const(contacts)) {
91 addImportContactNote(contact, QStringLiteral("Balsa"));
92 createContact(contact);
93 }
94 }
95 }
96 } else if (type == QLatin1String("LibBalsaAddressBookVcard")) {
97 const QString path = grp.readEntry(QStringLiteral("Path"));
98 if (!path.isEmpty()) {
99 QMap<QString, QVariant> settings;
100 settings.insert(QStringLiteral("Path"), path);
101 settings.insert(QStringLiteral("DisplayName"), name);
102 addAddressBookImportInfo(i18n("New addressbook created: %1", createResource(QStringLiteral("akonadi_vcard_resource"), name, settings)));
103 }
104 } else {
105 qCDebug(BALSAPLUGIN_LOG) << " unknown addressbook type :" << type;
106 }
107 }
108