File indexing completed on 2025-01-05 04:45:51

0001 /*
0002    SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #include "evolutionaddressbook.h"
0007 #include "abstractdisplayinfo.h"
0008 
0009 #include <KLocalizedString>
0010 #include <KMessageBox>
0011 #include <QFileDialog>
0012 #include <QProcess>
0013 
0014 EvolutionAddressBook::EvolutionAddressBook() = default;
0015 
0016 EvolutionAddressBook::~EvolutionAddressBook() = default;
0017 
0018 void EvolutionAddressBook::exportEvolutionAddressBook()
0019 {
0020     KMessageBox::information(mAbstractDisplayInfo->parentWidget(),
0021                              i18n("Evolution address book will be exported as vCard. Import vCard in KAddressBook."),
0022                              i18nc("@title:window", "Export Evolution Address Book"));
0023 
0024     const QString directory =
0025         QFileDialog::getExistingDirectory(mAbstractDisplayInfo->parentWidget(), i18n("Select the directory where vCards will be stored."));
0026     if (directory.isEmpty()) {
0027         return;
0028     }
0029     QFile evolutionFile;
0030     bool found = false;
0031     for (int i = 0; i < 9; ++i) {
0032         evolutionFile.setFileName(QStringLiteral("/usr/lib/evolution/3.%1/evolution-addressbook-export").arg(i));
0033         if (evolutionFile.exists()) {
0034             found = true;
0035             break;
0036         }
0037     }
0038     if (found) {
0039         QStringList arguments;
0040         arguments << QStringLiteral("-l");
0041         QProcess proc;
0042         proc.start(evolutionFile.fileName(), arguments);
0043         if (!proc.waitForFinished()) {
0044             return;
0045         }
0046         QByteArray result = proc.readAll();
0047         proc.close();
0048         if (!result.isEmpty()) {
0049             result.replace('\n', ',');
0050             const QString value(QString::fromLatin1(result.trimmed()));
0051             const QStringList listAddressBook = value.split(QLatin1Char(','));
0052             // qCDebug(EVOLUTIONPLUGIN_LOG)<<" listAddressBook"<<listAddressBook;
0053             int i = 0;
0054             QString name;
0055             QString displayname;
0056             for (const QString &arg : listAddressBook) {
0057                 switch (i) {
0058                 case 0:
0059                     name = arg;
0060                     name.remove(0, 1);
0061                     name.remove(name.length() - 1, 1);
0062                     ++i;
0063                     // name
0064                     break;
0065                 case 1:
0066                     displayname = arg;
0067                     displayname.remove(0, 1);
0068                     displayname.remove(displayname.length() - 1, 1);
0069                     // display name
0070                     ++i;
0071                     break;
0072                 case 2:
0073                     if (!displayname.isEmpty() && !name.isEmpty()) {
0074                         arguments.clear();
0075                         arguments << QStringLiteral("--format=vcard") << name << QStringLiteral("--output=%1/%2.vcard").arg(directory, displayname);
0076                         proc.start(evolutionFile.fileName(), arguments);
0077                         if (proc.waitForFinished()) {
0078                             addAddressBookImportInfo(i18n("Address book \"%1\" exported.", displayname));
0079                         } else {
0080                             addAddressBookImportError(i18n("Failed to export address book \"%1\".", displayname));
0081                         }
0082                     }
0083                     i = 0; // reset
0084                     break;
0085                 }
0086             }
0087         }
0088     }
0089 }
0090 
0091 #include "moc_evolutionaddressbook.cpp"