sendmdnhandler.cpp 4.32 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*
  Copyright (C) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.net,
     Author Tobias Koenig <tokoe@kdab.com>

  This library is free software; you can redistribute it and/or modify it
  under the terms of the GNU Library General Public License as published by
  the Free Software Foundation; either version 2 of the License, or (at your
  option) any later version.

  This library is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
  License for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; see the file COPYING.LIB.  If not, write to the
  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  02110-1301, USA.
*/

#include "sendmdnhandler.h"

#include "mailcommon/mailinterfaces.h"
#include "kernel/mailkernel.h"
#include "util/mailutil.h"
#include "filter/mdnadvicedialog.h"
#include "mailcommon_debug.h"
Laurent Montel's avatar
Laurent Montel committed
28
#include <MessageComposer/Util>
Laurent Montel's avatar
Laurent Montel committed
29
#include <MessageComposer/MessageFactoryNG>
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <MessageComposer/MessageSender>
#include <messageviewer/messageviewersettings.h>

#include <Collection>
#include <Item>
#include <Akonadi/KMime/MessageFlags>

#include <QQueue>
#include <QTimer>

using namespace MailCommon;

class Q_DECL_HIDDEN SendMdnHandler::Private
{
public:
    Private(SendMdnHandler *qq, IKernel *kernel)
Laurent Montel's avatar
Laurent Montel committed
46
47
        : q(qq)
        , mKernel(kernel)
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    {
    }

    void handleMessages();

    SendMdnHandler *q;
    IKernel *mKernel;
    QQueue<Akonadi::Item> mItemQueue;
    QTimer mTimer;
};

void SendMdnHandler::Private::handleMessages()
{
    while (!mItemQueue.isEmpty()) {
        Akonadi::Item item = mItemQueue.dequeue();

#if 0
        // should we send an MDN?
Laurent Montel's avatar
Laurent Montel committed
66
67
68
        if (MessageViewer::MessageViewerSettings::notSendWhenEncrypted()
            && message()->encryptionState() != KMMsgNotEncrypted
            && message()->encryptionState() != KMMsgEncryptionStateUnknown) {
69
70
71
72
73
74
75
            return;
        }
#else
        qCDebug(MAILCOMMON_LOG) << "AKONADI PORT: Disabled code in  " << Q_FUNC_INFO;
#endif

        const Akonadi::Collection collection = item.parentCollection();
Laurent Montel's avatar
Laurent Montel committed
76
77
78
79
80
        if (collection.isValid()
            && (CommonKernel->folderIsSentMailFolder(collection)
                || CommonKernel->folderIsTrash(collection)
                || CommonKernel->folderIsDraftOrOutbox(collection)
                || CommonKernel->folderIsTemplates(collection))) {
81
82
83
            continue;
        }

Laurent Montel's avatar
Laurent Montel committed
84
        const KMime::Message::Ptr message = MessageComposer::Util::message(item);
85
86
87
88
        if (!message) {
            continue;
        }

Laurent Montel's avatar
Laurent Montel committed
89
90
        const QPair<bool, KMime::MDN::SendingMode> mdnSend
            = MDNAdviceHelper::instance()->checkAndSetMDNInfo(item, KMime::MDN::Displayed);
91
        if (mdnSend.first) {
Laurent Montel's avatar
Laurent Montel committed
92
            const int quote = MessageViewer::MessageViewerSettings::self()->quoteMessage();
93

Laurent Montel's avatar
Laurent Montel committed
94
            MessageComposer::MessageFactoryNG factory(message, Akonadi::Item().id());
95
96
97
            factory.setIdentityManager(mKernel->identityManager());
            factory.setFolderIdentity(MailCommon::Util::folderIdentity(item));

Laurent Montel's avatar
Laurent Montel committed
98
99
            const KMime::Message::Ptr mdn
                = factory.createMDN(KMime::MDN::ManualAction, KMime::MDN::Displayed, mdnSend.second, quote);
100
101
102
103
104
105
106
107
108
109
            if (mdn) {
                if (!mKernel->msgSender()->send(mdn)) {
                    qCDebug(MAILCOMMON_LOG) << "Sending failed.";
                }
            }
        }
    }
}

SendMdnHandler::SendMdnHandler(IKernel *kernel, QObject *parent)
Laurent Montel's avatar
Laurent Montel committed
110
111
    : QObject(parent)
    , d(new Private(this, kernel))
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
{
    d->mTimer.setSingleShot(true);
    connect(&d->mTimer, SIGNAL(timeout()), this, SLOT(handleMessages()));
}

SendMdnHandler::~SendMdnHandler()
{
    delete d;
}

void SendMdnHandler::setItem(const Akonadi::Item &item)
{
    if (item.hasFlag(Akonadi::MessageFlags::Seen)) {
        return;
    }

    d->mTimer.stop();

    d->mItemQueue.enqueue(item);

Laurent Montel's avatar
Laurent Montel committed
132
133
    if (MessageViewer::MessageViewerSettings::self()->delayedMarkAsRead()
        && MessageViewer::MessageViewerSettings::self()->delayedMarkTime() != 0) {
134
135
136
137
138
139
140
141
        d->mTimer.start(MessageViewer::MessageViewerSettings::self()->delayedMarkTime() * 1000);
        return;
    }

    d->handleMessages();
}

#include "moc_sendmdnhandler.cpp"