File indexing completed on 2025-01-19 04:46:32

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "windowscontactimportexportplugininterface.h"
0008 #include "importexportwindowscontactplugin_debug.h"
0009 #include "importwindowcontact.h"
0010 #include <KActionCollection>
0011 #include <KLocalizedString>
0012 #include <PimCommon/RenameFileDialog>
0013 #include <QAction>
0014 #include <QFile>
0015 #include <QFileDialog>
0016 
0017 #include <KAddressBookImportExport/ImportExportEngine>
0018 
0019 WindowsContactImportExportPluginInterface::WindowsContactImportExportPluginInterface(QObject *parent)
0020     : KAddressBookImportExport::PluginInterface(parent)
0021 {
0022 }
0023 
0024 WindowsContactImportExportPluginInterface::~WindowsContactImportExportPluginInterface() = default;
0025 
0026 void WindowsContactImportExportPluginInterface::createAction(KActionCollection *ac)
0027 {
0028     QAction *action = ac->addAction(QStringLiteral("file_import_windows_contact"));
0029     action->setText(i18n("Import Windows Contact file..."));
0030     action->setWhatsThis(i18n("Import contacts from windows contact file."));
0031     setImportActions(QList<QAction *>() << action);
0032     connect(action, &QAction::triggered, this, &WindowsContactImportExportPluginInterface::slotImportWindowsContact);
0033 
0034     // If a day we implement it
0035     //    action = ac->addAction(QStringLiteral("file_export_windows_contact"));
0036     //    action->setText(i18n("Export Windows Contact file..."));
0037     //    action->setWhatsThis(i18n("Export contacts to windows contact file."));
0038     //    setExportActions(QList<QAction *>() << action);
0039     //    connect(action, &QAction::triggered, this, &WindowsContactImportExportPluginInterface::slotExportWindowsContact);
0040 }
0041 
0042 void WindowsContactImportExportPluginInterface::slotExportWindowsContact()
0043 {
0044     mImportExportAction = Export;
0045     Q_EMIT emitPluginActivated(this);
0046 }
0047 
0048 void WindowsContactImportExportPluginInterface::slotImportWindowsContact()
0049 {
0050     mImportExportAction = Import;
0051     Q_EMIT emitPluginActivated(this);
0052 }
0053 
0054 void WindowsContactImportExportPluginInterface::exec()
0055 {
0056     switch (mImportExportAction) {
0057     case Import:
0058         importWindowsContact();
0059         break;
0060     case Export:
0061         exportWindowsContact();
0062         break;
0063     }
0064 }
0065 
0066 void WindowsContactImportExportPluginInterface::importWindowsContact()
0067 {
0068     const QStringList fileNames = QFileDialog::getOpenFileNames(parentWidget(), QString(), QDir::homePath(), i18n("Windows Contact file (*.contact)"));
0069 
0070     if (fileNames.isEmpty()) {
0071         return;
0072     }
0073 
0074     KContacts::Addressee::List addresseeList;
0075     ImportWindowContact importer;
0076     importer.setParentWidget(parentWidget());
0077     for (const QString &fileName : fileNames) {
0078         addresseeList.append(importer.importFile(fileName));
0079     }
0080 
0081     KAddressBookImportExport::ContactList contactList;
0082     contactList.setAddressList(addresseeList);
0083 
0084     auto engine = new KAddressBookImportExport::ImportExportEngine(this);
0085     engine->setContactList(contactList);
0086     engine->setDefaultAddressBook(defaultCollection());
0087     engine->importContacts();
0088 }
0089 
0090 void WindowsContactImportExportPluginInterface::exportWindowsContact()
0091 {
0092     qCWarning(IMPORTEXPORTWINDOWSCONTACTPLUGIN_LOG) << "Export windows contact not implement yet";
0093     // TODO
0094 }
0095 
0096 bool WindowsContactImportExportPluginInterface::canImportFileType(const QUrl &url)
0097 {
0098     return url.path().endsWith(QLatin1StringView(".contact"));
0099 }
0100 
0101 void WindowsContactImportExportPluginInterface::importFile(const QUrl &url)
0102 {
0103     Q_UNUSED(url)
0104 }
0105 
0106 #include "moc_windowscontactimportexportplugininterface.cpp"