ldapimportexportplugininterface.cpp 1.78 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1
/*
2
   SPDX-FileCopyrightText: 2016-2020 Laurent Montel <montel@kde.org>
Laurent Montel's avatar
Laurent Montel committed
3

4
   SPDX-License-Identifier: GPL-2.0-or-later
Laurent Montel's avatar
Laurent Montel committed
5 6 7 8 9 10
*/

#include "ldapimportexportplugininterface.h"
#include <KLocalizedString>
#include <KActionCollection>
#include <QAction>
Laurent Montel's avatar
Laurent Montel committed
11
#include <QPointer>
Laurent Montel's avatar
Laurent Montel committed
12
#include <PimCommonAkonadi/LdapSearchDialog>
13
#include <KAddressBookImportExport/ImportExportEngine>
Laurent Montel's avatar
Laurent Montel committed
14 15

LDapImportExportPluginInterface::LDapImportExportPluginInterface(QObject *parent)
16
    : KAddressBookImportExport::PluginInterface(parent)
Laurent Montel's avatar
Laurent Montel committed
17 18 19
{
}

20
LDapImportExportPluginInterface::~LDapImportExportPluginInterface() = default;
Laurent Montel's avatar
Laurent Montel committed
21 22 23 24

void LDapImportExportPluginInterface::createAction(KActionCollection *ac)
{
    QAction *action = ac->addAction(QStringLiteral("file_import_ldap"));
Laurent Montel's avatar
Laurent Montel committed
25 26
    action->setText(i18n("Import From LDAP server..."));
    action->setWhatsThis(i18n("Import contacts from an LDAP server."));
Laurent Montel's avatar
Laurent Montel committed
27
    setImportActions({action});
Laurent Montel's avatar
Laurent Montel committed
28
    connect(action, &QAction::triggered, this, &LDapImportExportPluginInterface::slotImportLdap);
Laurent Montel's avatar
Laurent Montel committed
29 30 31 32
}

void LDapImportExportPluginInterface::exec()
{
Laurent Montel's avatar
Laurent Montel committed
33
    switch (mImportExportAction) {
Laurent Montel's avatar
Laurent Montel committed
34
    case Import:
Laurent Montel's avatar
Laurent Montel committed
35
        importLdap();
Laurent Montel's avatar
Laurent Montel committed
36 37 38 39
        break;
    case Export:
        break;
    }
Laurent Montel's avatar
Laurent Montel committed
40
}
Laurent Montel's avatar
Laurent Montel committed
41 42 43 44 45 46

void LDapImportExportPluginInterface::slotImportLdap()
{
    mImportExportAction = Import;
    Q_EMIT emitPluginActivated(this);
}
Laurent Montel's avatar
Laurent Montel committed
47 48 49

void LDapImportExportPluginInterface::importLdap()
{
50
    KAddressBookImportExport::ContactList contactList;
Laurent Montel's avatar
Laurent Montel committed
51
    QPointer<PimCommon::LdapSearchDialog> dlg = new PimCommon::LdapSearchDialog(parentWidget());
Laurent Montel's avatar
Laurent Montel committed
52

Laurent Montel's avatar
Laurent Montel committed
53
    if (dlg->exec()) {
Laurent Montel's avatar
Laurent Montel committed
54 55 56 57
        contactList.setAddressList(dlg->selectedContacts());
    }

    delete dlg;
58
    auto *engine = new KAddressBookImportExport::ImportExportEngine(this);
Laurent Montel's avatar
Laurent Montel committed
59 60 61
    engine->setContactList(contactList);
    engine->setDefaultAddressBook(defaultCollection());
    engine->importContacts();
Laurent Montel's avatar
Laurent Montel committed
62
}