File indexing completed on 2025-01-05 04:49:30

0001 /*
0002    SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "automaticaddcontactsinterface.h"
0008 #include "automaticaddcontactsjob.h"
0009 #include <KConfigGroup>
0010 #include <KIdentityManagementCore/Identity>
0011 #include <KIdentityManagementCore/IdentityManager>
0012 #include <KSharedConfig>
0013 
0014 AutomaticAddContactsInterface::AutomaticAddContactsInterface(QObject *parent)
0015     : MessageComposer::PluginEditorCheckBeforeSendInterface(parent)
0016 {
0017 }
0018 
0019 AutomaticAddContactsInterface::~AutomaticAddContactsInterface() = default;
0020 
0021 bool AutomaticAddContactsInterface::exec(const MessageComposer::PluginEditorCheckBeforeSendParams &params)
0022 {
0023     AutomaticAddContactsSettings setting = mHashSettings.value(params.identity());
0024     if (setting.mEnabled) {
0025         if (setting.mContactCollection.isValid()) {
0026             QStringList lst;
0027             if (!params.ccAddresses().trimmed().isEmpty()) {
0028                 lst << params.ccAddresses();
0029             }
0030             if (!params.bccAddresses().trimmed().isEmpty()) {
0031                 lst << params.bccAddresses();
0032             }
0033             if (!params.toAddresses().trimmed().isEmpty()) {
0034                 lst << params.toAddresses();
0035             }
0036             if (!lst.isEmpty()) {
0037                 // Don't delete it, it's autodelete
0038                 auto job = new AutomaticAddContactsJob;
0039                 job->setCollection(setting.mContactCollection);
0040                 job->setEmails(lst);
0041                 job->start();
0042             }
0043         }
0044     }
0045     return true;
0046 }
0047 
0048 void AutomaticAddContactsInterface::reloadConfig()
0049 {
0050     mHashSettings.clear();
0051 
0052     KIdentityManagementCore::IdentityManager *im = KIdentityManagementCore::IdentityManager::self();
0053     KIdentityManagementCore::IdentityManager::ConstIterator end = im->end();
0054     KSharedConfig::Ptr config = KSharedConfig::openConfig();
0055     for (KIdentityManagementCore::IdentityManager::ConstIterator it = im->begin(); it != end; ++it) {
0056         const uint identity = (*it).uoid();
0057         KConfigGroup identityGroup = config->group(QStringLiteral("Automatic Add Contacts %1").arg(identity));
0058         AutomaticAddContactsSettings settings;
0059         settings.mEnabled = identityGroup.readEntry("Enabled", false);
0060         settings.mContactCollection = Akonadi::Collection(identityGroup.readEntry("Collection", -1));
0061         mHashSettings.insert(identity, settings);
0062     }
0063 }
0064 
0065 #include "moc_automaticaddcontactsinterface.cpp"