2 Copyright (C) 2013-2016 Montel Laurent <montel@kde.org>
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include "sendlaterjob.h"
21 #include "sendlaterinfo.h"
23 #include "MessageComposer/AkonadiSender"
24 #include <MessageComposer/Util>
25 #include "messagecore/messagehelpers.h"
26 #include <MessageCore/StringUtil>
28 #include <MailTransport/mailtransport/transportattribute.h>
29 #include <MailTransport/mailtransport/sentbehaviourattribute.h>
30 #include <MailTransport/mailtransport/transport.h>
31 #include <MailTransport/mailtransport/transportmanager.h>
33 #include <ItemFetchJob>
34 #include <ItemDeleteJob>
36 #include <KNotification>
37 #include <KLocalizedString>
39 #include <KIconLoader>
40 #include "sendlateragent_debug.h"
42 SendLaterJob::SendLaterJob(SendLaterManager
*manager
, SendLater::SendLaterInfo
*info
, QObject
*parent
)
47 qCDebug(SENDLATERAGENT_LOG
) << " SendLaterJob::SendLaterJob" << this;
50 SendLaterJob::~SendLaterJob()
52 qCDebug(SENDLATERAGENT_LOG
) << " SendLaterJob::~SendLaterJob()" << this;
55 void SendLaterJob::start()
58 if (mInfo
->itemId() > -1) {
59 const Akonadi::Item item
= Akonadi::Item(mInfo
->itemId());
60 Akonadi::ItemFetchJob
*fetch
= new Akonadi::ItemFetchJob(item
, this);
61 mFetchScope
.fetchAttribute
<MailTransport::TransportAttribute
>();
62 mFetchScope
.fetchAttribute
<MailTransport::SentBehaviourAttribute
>();
63 mFetchScope
.setAncestorRetrieval(Akonadi::ItemFetchScope::Parent
);
64 mFetchScope
.fetchFullPayload(true);
65 fetch
->setFetchScope(mFetchScope
);
66 connect(fetch
, &Akonadi::ItemFetchJob::itemsReceived
, this, &SendLaterJob::slotMessageTransfered
);
67 connect(fetch
, &Akonadi::ItemFetchJob::result
, this, &SendLaterJob::slotJobFinished
);
70 qCDebug(SENDLATERAGENT_LOG
) << " message Id is invalid";
71 sendError(i18n("Not message found."), SendLaterManager::ItemNotFound
);
74 sendError(i18n("Not message found."), SendLaterManager::UnknownError
);
75 qCDebug(SENDLATERAGENT_LOG
) << " Item is null. It's a bug!";
79 void SendLaterJob::slotMessageTransfered(const Akonadi::Item::List
&items
)
81 if (items
.isEmpty()) {
82 sendError(i18n("No message found."), SendLaterManager::ItemNotFound
);
83 qCDebug(SENDLATERAGENT_LOG
) << " slotMessageTransfered failed !";
85 } else if (items
.count() == 1) {
87 mItem
= items
.first();
90 qCDebug(SENDLATERAGENT_LOG
) << "Error during fetching message.";
91 sendError(i18n("Error during fetching message."), SendLaterManager::TooManyItemFound
);
94 void SendLaterJob::slotJobFinished(KJob
*job
)
97 sendError(i18n("Cannot fetch message. %1", job
->errorString()), SendLaterManager::CanNotFetchItem
);
100 if (!MailTransport::TransportManager::self()->showTransportCreationDialog(Q_NULLPTR
, MailTransport::TransportManager::IfNoTransportExists
)) {
101 qCDebug(SENDLATERAGENT_LOG
) << " we can't create transport ";
102 sendError(i18n("We can't create transport"), SendLaterManager::CanNotCreateTransport
);
106 if (mItem
.isValid()) {
107 const KMime::Message::Ptr msg
= MessageCore::Util::message(mItem
);
109 sendError(i18n("Message is not a real message"), SendLaterManager::CanNotFetchItem
);
112 updateAndCleanMessageBeforeSending(msg
);
114 if (!mManager
->sender()->send(msg
, MessageComposer::MessageSender::SendImmediate
)) {
115 sendError(i18n("Cannot send message."), SendLaterManager::MailDispatchDoesntWork
);
117 if (!mInfo
->isRecurrence()) {
118 Akonadi::ItemDeleteJob
*fetch
= new Akonadi::ItemDeleteJob(mItem
, this);
119 connect(fetch
, &Akonadi::ItemDeleteJob::result
, this, &SendLaterJob::slotDeleteItem
);
127 void SendLaterJob::updateAndCleanMessageBeforeSending(const KMime::Message::Ptr
&msg
)
129 msg
->date()->setDateTime(QDateTime::currentDateTime());
130 MessageComposer::Util::removeNotNecessaryHeaders(msg
);
134 void SendLaterJob::slotDeleteItem(KJob
*job
)
137 qCDebug(SENDLATERAGENT_LOG
) << " void SendLaterJob::slotDeleteItem( KJob *job ) :" << job
->errorString();
142 void SendLaterJob::sendDone()
144 const QPixmap pixmap
= QIcon::fromTheme(QStringLiteral("kmail")).pixmap(KIconLoader::SizeSmall
, KIconLoader::SizeSmall
);
146 KNotification::event(QStringLiteral("mailsend"),
147 i18n("Message sent"),
150 KNotification::CloseOnTimeout
,
151 QStringLiteral("akonadi_sendlater_agent"));
152 mManager
->sendDone(mInfo
);
156 void SendLaterJob::sendError(const QString
&error
, SendLaterManager::ErrorType type
)
158 const QPixmap pixmap
= QIcon::fromTheme(QStringLiteral("kmail")).pixmap(KIconLoader::SizeSmall
, KIconLoader::SizeSmall
);
159 KNotification::event(QStringLiteral("mailsendfailed"),
163 KNotification::CloseOnTimeout
,
164 QStringLiteral("akonadi_sendlater_agent"));
165 mManager
->sendError(mInfo
, type
);