File indexing completed on 2024-12-15 04:47:01

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 "operaaddressbook.h"
0008 
0009 #include "operaplugin_debug.h"
0010 #include <KContacts/Addressee>
0011 #include <QFile>
0012 #include <QUrl>
0013 
0014 OperaAddressBook::OperaAddressBook(const QString &filename)
0015     : mFileName(filename)
0016 {
0017 }
0018 
0019 void OperaAddressBook::importAddressBook()
0020 {
0021     QFile file(mFileName);
0022     if (!file.open(QIODevice::ReadOnly)) {
0023         qCDebug(OPERAPLUGIN_LOG) << " We can't open file" << mFileName;
0024         return;
0025     }
0026 
0027     QTextStream stream(&file);
0028     bool foundContact = false;
0029     KContacts::Addressee *contact = nullptr;
0030     while (!stream.atEnd()) {
0031         QString line = stream.readLine();
0032         if (line == QLatin1StringView("#CONTACT")) {
0033             appendContact(contact);
0034             foundContact = true;
0035         } else if (line == QLatin1StringView("#FOLDER")) {
0036             appendContact(contact);
0037             foundContact = false;
0038             // TODO
0039         } else if (foundContact) {
0040             line = line.trimmed();
0041             if (!contact) {
0042                 contact = new KContacts::Addressee;
0043             }
0044             if (line.startsWith(QLatin1StringView("ID"))) {
0045                 // Nothing
0046             } else if (line.startsWith(QLatin1StringView("NAME"))) {
0047                 contact->setName(line.remove(QStringLiteral("NAME=")));
0048             } else if (line.startsWith(QLatin1StringView("URL"))) {
0049                 KContacts::ResourceLocatorUrl url;
0050                 url.setUrl(QUrl(line.remove(QStringLiteral("URL="))));
0051                 contact->setUrl(url);
0052             } else if (line.startsWith(QLatin1StringView("DESCRIPTION"))) {
0053                 contact->setNote(line.remove(QStringLiteral("DESCRIPTION=")));
0054             } else if (line.startsWith(QLatin1StringView("PHONE"))) {
0055                 contact->insertPhoneNumber(KContacts::PhoneNumber(line.remove(QStringLiteral("PHONE=")), KContacts::PhoneNumber::Home));
0056             } else if (line.startsWith(QLatin1StringView("FAX"))) {
0057                 contact->insertPhoneNumber(KContacts::PhoneNumber(line.remove(QStringLiteral("FAX=")), KContacts::PhoneNumber::Fax));
0058             } else if (line.startsWith(QLatin1StringView("POSTALADDRESS"))) {
0059                 // TODO
0060             } else if (line.startsWith(QLatin1StringView("PICTUREURL"))) {
0061                 // TODO
0062             } else if (line.startsWith(QLatin1StringView("ICON"))) {
0063                 // TODO
0064             } else if (line.startsWith(QLatin1StringView("SHORT NAME"))) {
0065                 contact->setNickName(line.remove(QStringLiteral("SHORT NAME=")));
0066             }
0067         }
0068     }
0069     appendContact(contact);
0070 }
0071 
0072 OperaAddressBook::~OperaAddressBook() = default;
0073 
0074 void OperaAddressBook::appendContact(KContacts::Addressee *contact)
0075 {
0076     if (contact) {
0077         addImportContactNote(*contact, QStringLiteral("Opera"));
0078         createContact(*contact);
0079         delete contact;
0080     }
0081 }