File indexing completed on 2024-12-22 04:52:48

0001 /*
0002    SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "balsaaddressbook.h"
0008 
0009 #include "importwizardutil.h"
0010 
0011 #include <KContacts/Addressee>
0012 #include <KContacts/LDIFConverter>
0013 
0014 #include <KConfig>
0015 #include <KConfigGroup>
0016 #include <KLocalizedString>
0017 #include <QUrl>
0018 
0019 #include "balsaplugin_debug.h"
0020 #include <QFile>
0021 #include <QFileInfo>
0022 #include <QRegularExpression>
0023 
0024 BalsaAddressBook::BalsaAddressBook(const QString &filename)
0025     : mFileName(filename)
0026 {
0027 }
0028 
0029 BalsaAddressBook::~BalsaAddressBook() = default;
0030 
0031 void BalsaAddressBook::importAddressBook()
0032 {
0033     KConfig config(mFileName);
0034     const QStringList addressBookList = config.groupList().filter(QRegularExpression(QStringLiteral("address-book-\\d+")));
0035     if (addressBookList.isEmpty()) {
0036         addAddressBookImportInfo(i18n("No addressbook found"));
0037     } else {
0038         for (const QString &addressbook : addressBookList) {
0039             KConfigGroup grp = config.group(addressbook);
0040             readAddressBook(grp);
0041         }
0042     }
0043 }
0044 
0045 void BalsaAddressBook::readAddressBook(const KConfigGroup &grp)
0046 {
0047     const QString type = grp.readEntry(QStringLiteral("Type"));
0048     if (type.isEmpty()) {
0049         addAddressBookImportInfo(i18n("No addressbook found"));
0050         return;
0051     }
0052     const QString name = grp.readEntry(QStringLiteral("Name"));
0053 
0054     if (type == QLatin1StringView("LibBalsaAddressBookLdap")) {
0055         ldapStruct ldap;
0056         ldap.dn = grp.readEntry(QStringLiteral("BaseDN"));
0057         ldap.useTLS = (grp.readEntry(QStringLiteral("EnableTLS")) == QLatin1StringView("true"));
0058         ldap.ldapUrl = QUrl(grp.readEntry(QStringLiteral("Host")));
0059         ldap.port = ldap.ldapUrl.port();
0060         // TODO: verify
0061         const QString bookDN = grp.readEntry(QStringLiteral("BookDN")); // TODO ?
0062         ImportWizardUtil::mergeLdap(ldap);
0063         addAddressBookImportInfo(i18n("Ldap created"));
0064     } else if (type == QLatin1StringView("LibBalsaAddressBookGpe")) {
0065         qCDebug(BALSAPLUGIN_LOG) << " Import it !";
0066     } else if (type == QLatin1StringView("LibBalsaAddressBookLdif")) {
0067         const QString path = grp.readEntry(QStringLiteral("Path"));
0068         if (!path.isEmpty()) {
0069             KContacts::Addressee::List contacts;
0070             KContacts::ContactGroup::List contactsGroup;
0071             QFile file(path);
0072             if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
0073                 QTextStream stream(&file);
0074                 stream.setEncoding(QStringConverter::Encoding::Latin1);
0075 
0076                 const QString wholeFile = stream.readAll();
0077                 const QDateTime dtDefault = QFileInfo(file).lastModified();
0078                 file.close();
0079 
0080                 KContacts::LDIFConverter::LDIFToAddressee(wholeFile, contacts, contactsGroup, dtDefault);
0081                 for (KContacts::Addressee contact : std::as_const(contacts)) {
0082                     addImportContactNote(contact, QStringLiteral("Balsa"));
0083                     createContact(contact);
0084                 }
0085             }
0086         }
0087     } else if (type == QLatin1StringView("LibBalsaAddressBookVcard")) {
0088         const QString path = grp.readEntry(QStringLiteral("Path"));
0089         if (!path.isEmpty()) {
0090             QMap<QString, QVariant> settings;
0091             settings.insert(QStringLiteral("Path"), path);
0092             settings.insert(QStringLiteral("DisplayName"), name);
0093             addAddressBookImportInfo(i18n("New addressbook created: %1", createResource(QStringLiteral("akonadi_vcard_resource"), name, settings)));
0094         }
0095     } else {
0096         qCDebug(BALSAPLUGIN_LOG) << " unknown addressbook type :" << type;
0097     }
0098 }