File indexing completed on 2024-05-12 05:11:11

0001 /*
0002   SPDX-FileCopyrightText: 2009 Constantin Berzan <exit3219@gmail.com>
0003 
0004   SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "akonadi-mime_export.h"
0010 
0011 #include <Akonadi/DispatchModeAttribute>
0012 #include <Akonadi/SentActionAttribute>
0013 #include <Akonadi/SentBehaviourAttribute>
0014 #include <Akonadi/TransportAttribute>
0015 
0016 #include <QString>
0017 #include <QStringList>
0018 
0019 #include <KCompositeJob>
0020 
0021 #include <Akonadi/AddressAttribute>
0022 #include <Akonadi/Collection>
0023 
0024 #include <KMime/KMimeMessage>
0025 
0026 #include <memory>
0027 
0028 namespace Akonadi
0029 {
0030 class MessageQueueJobPrivate;
0031 
0032 /**
0033   @short Provides an interface for sending email.
0034 
0035   This class takes a KMime::Message and some related info such as sender and
0036   recipient addresses, and places the message in the outbox.  The mail
0037   dispatcher agent will then take it from there and send it.
0038 
0039   This is the preferred way for applications to send email.
0040 
0041   This job requires some options to be set before being started.  Modify the
0042   attributes of this job to change these options.
0043 
0044   You need to set the transport of the transport attribute, the from address of
0045   the address attribute and one of the to, cc or bcc addresses of the address
0046   attribute. Also, you need to call setMessage().
0047   Optionally, you can change the dispatch mode attribute or the sent behaviour
0048   attribute.
0049 
0050   Example:
0051   @code
0052 
0053   MessageQueueJob *job = new MessageQueueJob( this );
0054   job->setMessage( msg ); // msg is a Message::Ptr
0055   job->transportAttribute().setTransportId( TransportManager::self()->defaultTransportId() );
0056   // Use the default dispatch mode.
0057   // Use the default sent-behaviour.
0058   job->addressAttribute().setFrom( from ); // from is a QString
0059   job->addressAttribute().setTo( to ); // to is a QStringList
0060   connect( job, SIGNAL(result(KJob*)), this, SLOT(jobResult(KJob*)) );
0061   job->start();
0062 
0063   @endcode
0064 
0065   @see DispatchModeAttribute
0066   @see SentActionAttribute
0067   @see SentBehaviourAttribute
0068   @see TransportAttribute
0069   @see AddressAttribute
0070 
0071   @author Constantin Berzan <exit3219@gmail.com>
0072   @since 4.4
0073 */
0074 class AKONADI_MIME_EXPORT MessageQueueJob : public KCompositeJob
0075 {
0076     Q_OBJECT
0077 
0078 public:
0079     /**
0080       Creates a new MessageQueueJob.
0081       @param parent the QObject parent
0082       This is not an autostarting job; you need to call start() yourself.
0083     */
0084     explicit MessageQueueJob(QObject *parent = nullptr);
0085 
0086     /**
0087       Destroys the MessageQueueJob.
0088       This job deletes itself after finishing.
0089     */
0090     ~MessageQueueJob() override;
0091 
0092     /**
0093       Returns the message to be sent.
0094     */
0095     [[nodiscard]] KMime::Message::Ptr message() const;
0096 
0097     /**
0098       Returns a reference to the dispatch mode attribute for this message.
0099       Modify the returned attribute to change the dispatch mode.
0100     */
0101     DispatchModeAttribute &dispatchModeAttribute();
0102 
0103     /**
0104       Returns a reference to the address attribute for this message.
0105       Modify the returned attribute to change the receivers or the from
0106       address.
0107     */
0108     Akonadi::AddressAttribute &addressAttribute();
0109 
0110     /**
0111       Returns a reference to the transport attribute for this message.
0112       Modify the returned attribute to change the transport used for
0113       sending the mail.
0114     */
0115     TransportAttribute &transportAttribute();
0116 
0117     /**
0118       Returns a reference to the sent behaviour attribute for this message.
0119       Modify the returned attribute to change the sent behaviour.
0120     */
0121     SentBehaviourAttribute &sentBehaviourAttribute();
0122 
0123     /**
0124       Returns a reference to the sent action attribute for this message.
0125       Modify the returned attribute to change the sent actions.
0126     */
0127     SentActionAttribute &sentActionAttribute();
0128 
0129     /**
0130       Sets the message to be sent.
0131     */
0132     void setMessage(const KMime::Message::Ptr &message);
0133 
0134     /**
0135       Creates the item and places it in the outbox.
0136       It is now queued for sending by the mail dispatcher agent.
0137     */
0138     void start() override;
0139 
0140 protected Q_SLOTS:
0141     /**
0142       Called when the ItemCreateJob subjob finishes.
0143 
0144       (reimplemented from KCompositeJob)
0145     */
0146     void slotResult(KJob *) override;
0147 
0148 private:
0149     friend class MessageQueueJobPrivate;
0150     std::unique_ptr<MessageQueueJobPrivate> const d;
0151 };
0152 } // namespace MailTransport