File: removeduplicatesjob.cpp

package info (click to toggle)
akonadi-mime 4%3A18.08.3-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,784 kB
  • sloc: cpp: 6,292; xml: 20; makefile: 5; sh: 3
file content (209 lines) | stat: -rw-r--r-- 7,014 bytes parent folder | download
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
    Copyright (c) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
    Copyright (c) 2010 Andras Mantia <andras@kdab.com>
    Copyright (c) 2012 Dan Vrátil <dvratil@redhat.com>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 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
    Lesser General Public License for more details.

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

#include "removeduplicatesjob.h"
#include "akonadi_mime_debug.h"
#include <itemfetchjob.h>
#include <itemdeletejob.h>
#include <itemfetchscope.h>

#include <kmime/kmime_message.h>

#include <KLocalizedString>

class Q_DECL_HIDDEN Akonadi::RemoveDuplicatesJob::Private
{
public:
    Private(RemoveDuplicatesJob *parent)
        : mCurrentJob(nullptr)
        , mJobCount(0)
        , mKilled(false)
        , mParent(parent)
    {
    }

    void fetchItem()
    {
        Akonadi::Collection collection = mFolders.value(mJobCount - 1);
        qCDebug(AKONADIMIME_LOG) << "Processing collection" << collection.name() << "(" << collection.id() << ")";

        Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob(collection, mParent);
        job->fetchScope().setAncestorRetrieval(Akonadi::ItemFetchScope::Parent);
        job->fetchScope().fetchFullPayload();
        job->fetchScope().setIgnoreRetrievalErrors(true);
        mParent->connect(job, &ItemFetchJob::result, mParent, [this](KJob *job) {
            slotFetchDone(job);
        });
        mCurrentJob = job;

        Q_EMIT mParent->description(mParent, i18n("Retrieving items..."));
    }

    void slotFetchDone(KJob *job)
    {
        mJobCount--;
        if (job->error()) {
            mParent->setError(job->error());
            mParent->setErrorText(job->errorText());
            mParent->emitResult();
            return;
        }

        if (mKilled) {
            mParent->emitResult();
            return;
        }

        Q_EMIT mParent->description(mParent, i18n("Searching for duplicates..."));

        Akonadi::ItemFetchJob *fjob = static_cast<Akonadi::ItemFetchJob *>(job);
        Akonadi::Item::List items = fjob->items();

        //find duplicate mails with the same messageid
        //if duplicates are found, check the content as well to be sure they are the same
        QMap<QByteArray, uint> messageIds;
        QMap<uint, QList<uint> > duplicates;
        QMap<uint, uint> bodyHashes;
        const int numberOfItems(items.size());
        for (int i = 0; i < numberOfItems; ++i) {
            Akonadi::Item item = items.at(i);
            if (item.hasPayload<KMime::Message::Ptr>()) {
                KMime::Message::Ptr message = item.payload<KMime::Message::Ptr>();
                QByteArray idStr = message->messageID()->as7BitString(false);
                //TODO: Maybe do some more check in case of idStr.isEmpty()
                //like when the first message's body is different from the 2nd,
                //but the 2nd is the same as the 3rd, etc.
                //if ( !idStr.isEmpty() )
                {
                    if (messageIds.contains(idStr)) {
                        uint mainId = messageIds.value(idStr);
                        if (!bodyHashes.contains(mainId)) {
                            bodyHashes.insert(mainId, qHash(items.value(mainId).payload<KMime::Message::Ptr>()->encodedContent()));
                        }
                        uint hash = qHash(message->encodedContent());
                        qCDebug(AKONADIMIME_LOG) << idStr << bodyHashes.value(mainId) << hash;
                        if (bodyHashes.value(mainId) == hash) {
                            duplicates[mainId].append(i);
                        }
                    } else {
                        messageIds.insert(idStr, i);
                    }
                }
            }
        }

        QMap<uint, QList<uint> >::ConstIterator end(duplicates.constEnd());
        for (QMap<uint, QList<uint> >::ConstIterator it = duplicates.constBegin(); it != end; ++it) {
            QList<uint>::ConstIterator dupEnd(it.value().constEnd());
            for (QList<uint>::ConstIterator dupIt = it.value().constBegin(); dupIt != dupEnd; ++dupIt) {
                mDuplicateItems.append(items.value(*dupIt));
            }
        }

        if (mKilled) {
            mParent->emitResult();
            return;
        }

        if (mJobCount > 0) {
            fetchItem();
        } else {
            if (mDuplicateItems.isEmpty()) {
                qCDebug(AKONADIMIME_LOG) << "No duplicates, I'm done here";
                mParent->emitResult();
                return;
            } else {
                Q_EMIT mParent->description(mParent, i18n("Removing duplicates..."));
                Akonadi::ItemDeleteJob *delCmd = new Akonadi::ItemDeleteJob(mDuplicateItems, mParent);
                mParent->connect(delCmd, &ItemDeleteJob::result, mParent, [this](KJob *job) {
                    slotDeleteDone(job);
                });
            }
        }
    }

    void slotDeleteDone(KJob *job)
    {
        qCDebug(AKONADIMIME_LOG) << "Job done";

        mParent->setError(job->error());
        mParent->setErrorText(job->errorText());
        mParent->emitResult();
    }

    Akonadi::Collection::List mFolders;
    Akonadi::Item::List mDuplicateItems;
    Akonadi::Job *mCurrentJob = nullptr;
    int mJobCount;
    bool mKilled = false;

private:
    RemoveDuplicatesJob *mParent = nullptr;
};

using namespace Akonadi;

RemoveDuplicatesJob::RemoveDuplicatesJob(const Akonadi::Collection &folder, QObject *parent)
    : Job(parent)
    , d(new Private(this))
{
    d->mJobCount = 1;
    d->mFolders << folder;
}

RemoveDuplicatesJob::RemoveDuplicatesJob(const Akonadi::Collection::List &folders, QObject *parent)
    : Job(parent)
    , d(new Private(this))
{
    d->mFolders = folders;
    d->mJobCount = d->mFolders.length();
}

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

void RemoveDuplicatesJob::doStart()
{
    qCDebug(AKONADIMIME_LOG) << " void RemoveDuplicatesJob::doStart()";

    if (d->mFolders.isEmpty()) {
        qCWarning(AKONADIMIME_LOG) << "No collections to process";
        emitResult();
        return;
    }

    d->fetchItem();
}

bool RemoveDuplicatesJob::doKill()
{
    qCDebug(AKONADIMIME_LOG) << "Killed!";

    d->mKilled = true;
    if (d->mCurrentJob) {
        d->mCurrentJob->kill(EmitResult);
    }

    return true;
}

#include "moc_removeduplicatesjob.cpp"