File indexing completed on 2024-11-24 04:43:05

0001 /*
0002    SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "sendmailplugininterface.h"
0008 #include "mailsenderjob.h"
0009 
0010 #include <KActionCollection>
0011 #include <KLocalizedString>
0012 #include <KMessageBox>
0013 #include <QAction>
0014 #include <QDesktopServices>
0015 
0016 SendMailPluginInterface::SendMailPluginInterface(QObject *parent)
0017     : PimCommon::GenericPluginInterface(parent)
0018 {
0019 }
0020 
0021 SendMailPluginInterface::~SendMailPluginInterface() = default;
0022 
0023 void SendMailPluginInterface::createAction(KActionCollection *ac)
0024 {
0025     mAction = ac->addAction(QStringLiteral("send_mail"));
0026     mAction->setText(i18n("Send an email..."));
0027     mAction->setIcon(QIcon::fromTheme(QStringLiteral("mail-message-new")));
0028     connect(mAction, &QAction::triggered, this, &SendMailPluginInterface::slotActivated);
0029     const PimCommon::ActionType type(mAction, PimCommon::ActionType::Action);
0030     addActionType(type);
0031 }
0032 
0033 void SendMailPluginInterface::slotActivated()
0034 {
0035     Q_EMIT emitPluginActivated(this);
0036 }
0037 
0038 void SendMailPluginInterface::setCurrentItems(const Akonadi::Item::List &items)
0039 {
0040     mListItems = items;
0041 }
0042 
0043 PimCommon::GenericPluginInterface::RequireTypes SendMailPluginInterface::requiresFeatures() const
0044 {
0045     return PimCommon::GenericPluginInterface::CurrentItems;
0046 }
0047 
0048 void SendMailPluginInterface::updateActions(int numberOfSelectedItems, int numberOfSelectedCollections)
0049 {
0050     Q_UNUSED(numberOfSelectedCollections)
0051     if (mAction) {
0052         mAction->setEnabled(numberOfSelectedItems > 0);
0053     }
0054 }
0055 
0056 void SendMailPluginInterface::exec()
0057 {
0058     if (mListItems.isEmpty()) {
0059         KMessageBox::error(parentWidget(), i18n("You have not selected any contacts."));
0060     } else {
0061         auto mailSender = new KABMailSender::MailSenderJob(mListItems, this);
0062         connect(mailSender, &KABMailSender::MailSenderJob::sendMails, this, &SendMailPluginInterface::slotSendMails);
0063         connect(mailSender, &KABMailSender::MailSenderJob::sendMailsError, this, &SendMailPluginInterface::slotSendMailError);
0064         mailSender->start();
0065     }
0066 }
0067 
0068 void SendMailPluginInterface::slotSendMailError(const QString &error)
0069 {
0070     KMessageBox::error(parentWidget(), error);
0071 }
0072 
0073 void SendMailPluginInterface::slotSendMails(const QStringList &emails)
0074 {
0075     if (!emails.isEmpty()) {
0076         QUrl url;
0077         url.setScheme(QStringLiteral("mailto"));
0078         url.setPath(emails.join(QLatin1Char(';')));
0079         QDesktopServices::openUrl(url);
0080     }
0081 }
0082 
0083 #include "moc_sendmailplugininterface.cpp"