File indexing completed on 2024-10-27 04:50:49
0001 /* 0002 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "sendlaterjob.h" 0008 0009 #include <MessageComposer/AkonadiSender> 0010 #include <MessageComposer/DraftStatus> 0011 #include <MessageComposer/SendLaterInfo> 0012 #include <MessageComposer/Util> 0013 #include <MessageCore/StringUtil> 0014 0015 #include <Akonadi/SentBehaviourAttribute> 0016 #include <Akonadi/TransportAttribute> 0017 #include <MailTransport/Transport> 0018 #include <MailTransport/TransportManager> 0019 0020 #include <Akonadi/ItemDeleteJob> 0021 #include <Akonadi/ItemFetchJob> 0022 0023 #include "sendlateragent_debug.h" 0024 #include <KLocalizedString> 0025 #include <KNotification> 0026 0027 SendLaterJob::SendLaterJob(SendLaterManager *manager, MessageComposer::SendLaterInfo *info, QObject *parent) 0028 : QObject(parent) 0029 , mManager(manager) 0030 , mInfo(info) 0031 { 0032 qCDebug(SENDLATERAGENT_LOG) << " SendLaterJob::SendLaterJob" << this; 0033 } 0034 0035 SendLaterJob::~SendLaterJob() 0036 { 0037 qCDebug(SENDLATERAGENT_LOG) << " SendLaterJob::~SendLaterJob()" << this; 0038 } 0039 0040 void SendLaterJob::start() 0041 { 0042 if (mInfo) { 0043 if (mInfo->itemId() > -1) { 0044 const Akonadi::Item item = Akonadi::Item(mInfo->itemId()); 0045 auto fetch = new Akonadi::ItemFetchJob(item, this); 0046 mFetchScope.fetchAttribute<Akonadi::TransportAttribute>(); 0047 mFetchScope.fetchAttribute<Akonadi::SentBehaviourAttribute>(); 0048 mFetchScope.setAncestorRetrieval(Akonadi::ItemFetchScope::Parent); 0049 mFetchScope.fetchFullPayload(true); 0050 fetch->setFetchScope(mFetchScope); 0051 connect(fetch, &Akonadi::ItemFetchJob::itemsReceived, this, &SendLaterJob::slotMessageTransfered); 0052 connect(fetch, &Akonadi::ItemFetchJob::result, this, &SendLaterJob::slotJobFinished); 0053 fetch->start(); 0054 } else { 0055 qCDebug(SENDLATERAGENT_LOG) << " message Id is invalid"; 0056 sendError(i18n("Not message found."), SendLaterManager::ItemNotFound); 0057 } 0058 } else { 0059 sendError(i18n("Not message found."), SendLaterManager::UnknownError); 0060 qCDebug(SENDLATERAGENT_LOG) << " Item is null. It's a bug!"; 0061 } 0062 } 0063 0064 void SendLaterJob::slotMessageTransfered(const Akonadi::Item::List &items) 0065 { 0066 if (items.isEmpty()) { 0067 sendError(i18n("No message found."), SendLaterManager::ItemNotFound); 0068 qCDebug(SENDLATERAGENT_LOG) << " slotMessageTransfered failed !"; 0069 return; 0070 } else if (items.count() == 1) { 0071 // Success 0072 mItem = items.first(); 0073 return; 0074 } 0075 qCDebug(SENDLATERAGENT_LOG) << "Error during fetching message."; 0076 sendError(i18n("Error during fetching message."), SendLaterManager::TooManyItemFound); 0077 } 0078 0079 void SendLaterJob::slotJobFinished(KJob *job) 0080 { 0081 if (job->error()) { 0082 sendError(i18n("Cannot fetch message. %1", job->errorString()), SendLaterManager::CanNotFetchItem); 0083 return; 0084 } 0085 if (!MailTransport::TransportManager::self()->showTransportCreationDialog(nullptr, MailTransport::TransportManager::IfNoTransportExists)) { 0086 qCDebug(SENDLATERAGENT_LOG) << " we can't create transport "; 0087 sendError(i18n("We can't create transport"), SendLaterManager::CanNotCreateTransport); 0088 return; 0089 } 0090 0091 if (mItem.isValid()) { 0092 const KMime::Message::Ptr msg = MessageComposer::Util::message(mItem); 0093 if (!msg) { 0094 sendError(i18n("Message is not a real message"), SendLaterManager::CanNotFetchItem); 0095 return; 0096 } 0097 // TODO verify encryption + signed 0098 updateAndCleanMessageBeforeSending(msg); 0099 0100 if (!mManager->sender()->send(msg, MessageComposer::MessageSender::SendImmediate)) { 0101 sendError(i18n("Cannot send message."), SendLaterManager::MailDispatchDoesntWork); 0102 } else { 0103 if (!mInfo->isRecurrence()) { 0104 auto fetch = new Akonadi::ItemDeleteJob(mItem, this); 0105 connect(fetch, &Akonadi::ItemDeleteJob::result, this, &SendLaterJob::slotDeleteItem); 0106 } else { 0107 sendDone(); 0108 } 0109 } 0110 } 0111 } 0112 0113 void SendLaterJob::updateAndCleanMessageBeforeSending(const KMime::Message::Ptr &msg) 0114 { 0115 msg->date()->setDateTime(QDateTime::currentDateTime()); 0116 MessageComposer::removeDraftCryptoHeaders(msg); 0117 msg->assemble(); 0118 } 0119 0120 void SendLaterJob::slotDeleteItem(KJob *job) 0121 { 0122 if (job->error()) { 0123 qCDebug(SENDLATERAGENT_LOG) << " void SendLaterJob::slotDeleteItem( KJob *job ) :" << job->errorString(); 0124 } 0125 sendDone(); 0126 } 0127 0128 void SendLaterJob::sendDone() 0129 { 0130 KNotification::event(QStringLiteral("mailsend"), 0131 QString(), 0132 i18n("Message sent"), 0133 QStringLiteral("kmail"), 0134 KNotification::CloseOnTimeout, 0135 QStringLiteral("akonadi_sendlater_agent")); 0136 mManager->sendDone(mInfo); 0137 deleteLater(); 0138 } 0139 0140 void SendLaterJob::sendError(const QString &error, SendLaterManager::ErrorType type) 0141 { 0142 KNotification::event(QStringLiteral("mailsendfailed"), 0143 QString(), 0144 error, 0145 QStringLiteral("kmail"), 0146 KNotification::CloseOnTimeout, 0147 QStringLiteral("akonadi_sendlater_agent")); 0148 mManager->sendError(mInfo, type); 0149 deleteLater(); 0150 } 0151 0152 #include "moc_sendlaterjob.cpp"