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

0001 /*
0002    SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "emailaddressselectionldapdialog.h"
0008 #include <Akonadi/RecipientsPickerWidget>
0009 #include <KConfigGroup>
0010 #include <KLocalizedString>
0011 #include <KPluginFactory>
0012 #include <KSharedConfig>
0013 #include <KWindowConfig>
0014 #include <PimCommonAkonadi/LdapSearchDialog>
0015 #include <QDialogButtonBox>
0016 #include <QLineEdit>
0017 #include <QPushButton>
0018 #include <QTreeView>
0019 #include <QVBoxLayout>
0020 #include <QWindow>
0021 namespace
0022 {
0023 static const char myConfigEmailAddressSelectionLdapDialog[] = "EmailAddressSelectionLdapDialog";
0024 }
0025 K_PLUGIN_CLASS_WITH_JSON(EmailAddressSelectionLdapDialog, "emailaddressselectionldapdialog.json")
0026 
0027 EmailAddressSelectionLdapDialog::EmailAddressSelectionLdapDialog(QWidget *parent, const QList<QVariant> &)
0028     : Akonadi::AbstractEmailAddressSelectionDialog(parent)
0029     , mView(new Akonadi::RecipientsPickerWidget(true, nullptr, this))
0030 {
0031     auto mainLayout = new QVBoxLayout(this);
0032     mainLayout->addWidget(mView);
0033     connect(mView->emailAddressSelectionWidget()->view(), &QTreeView::doubleClicked, this, &QDialog::accept);
0034 
0035     auto searchLDAPButton = new QPushButton(i18n("Search &Directory Service"), this);
0036     connect(searchLDAPButton, &QPushButton::clicked, this, &EmailAddressSelectionLdapDialog::slotSearchLDAP);
0037     mainLayout->addWidget(searchLDAPButton);
0038 
0039     KConfig config(QStringLiteral("kabldaprc"));
0040     KConfigGroup group = config.group(QStringLiteral("LDAP"));
0041     int numHosts = group.readEntry("NumSelectedHosts", 0);
0042     if (!numHosts) {
0043         searchLDAPButton->setVisible(false);
0044     }
0045 
0046     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0047     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0048     okButton->setDefault(true);
0049     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0050     connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
0051     connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0052 
0053     mainLayout->addWidget(buttonBox);
0054     readConfig();
0055 }
0056 
0057 void EmailAddressSelectionLdapDialog::readConfig()
0058 {
0059     create(); // ensure a window is created
0060     windowHandle()->resize(QSize(500, 300));
0061     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myConfigEmailAddressSelectionLdapDialog));
0062     KWindowConfig::restoreWindowSize(windowHandle(), group);
0063     resize(windowHandle()->size()); // workaround for QTBUG-40584
0064 }
0065 
0066 void EmailAddressSelectionLdapDialog::writeConfig()
0067 {
0068     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myConfigEmailAddressSelectionLdapDialog));
0069     KWindowConfig::saveWindowSize(windowHandle(), group);
0070 }
0071 
0072 EmailAddressSelectionLdapDialog::~EmailAddressSelectionLdapDialog()
0073 {
0074     writeConfig();
0075 }
0076 
0077 Akonadi::EmailAddressSelection::List EmailAddressSelectionLdapDialog::selectedAddresses() const
0078 {
0079     return mView->emailAddressSelectionWidget()->selectedAddresses();
0080 }
0081 
0082 Akonadi::EmailAddressSelectionWidget *EmailAddressSelectionLdapDialog::view() const
0083 {
0084     return mView->emailAddressSelectionWidget();
0085 }
0086 
0087 void EmailAddressSelectionLdapDialog::slotSearchLDAP()
0088 {
0089     if (!mLdapSearchDialog) {
0090         mLdapSearchDialog = new PimCommon::LdapSearchDialog(this);
0091         connect(mLdapSearchDialog, &PimCommon::LdapSearchDialog::contactsAdded, this, &EmailAddressSelectionLdapDialog::ldapSearchResult);
0092     }
0093 
0094     mLdapSearchDialog->setSearchText(mView->emailAddressSelectionWidget()->searchLineEdit()->text());
0095     mLdapSearchDialog->show();
0096 }
0097 
0098 void EmailAddressSelectionLdapDialog::ldapSearchResult()
0099 {
0100     const KContacts::Addressee::List contacts = mLdapSearchDialog->selectedContacts();
0101     Q_EMIT insertAddresses(contacts);
0102 }
0103 
0104 #include "emailaddressselectionldapdialog.moc"
0105 
0106 #include "moc_emailaddressselectionldapdialog.cpp"