summaryrefslogtreecommitdiff
path: root/migrationutility/migratefolderjob.cpp
blob: 13137327c6069908542b202af00e73753438288e (plain)
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
/*
 * Copyright (C) 2012  Christian Mollekopf <mollekopf@kolabsys.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "migratefolderjob.h"
#include "sourceaccount.h"
#include "kolabaccount.h"
#include <errorhandler.h>
#include <qdebug.h>

MigrateFolderJob::MigrateFolderJob(const QString &folder, SourceAccount* sourceAccount, KolabAccount *kolabAccount, QObject* parent)
:   KJob(parent),
    mSourceAccount(sourceAccount),
    mKolabAccount(kolabAccount),
    mFolder(folder),
    mRunningAppendJobs(0),
    mFetchDone(false)
{

}

void MigrateFolderJob::start()
{
    QMetaObject::invokeMethod(this, "doStart", Qt::QueuedConnection);
}

void MigrateFolderJob::doStart()
{
    Debug() << "migrating " << mFolder;
    const QPair<Kolab::FolderType, QString> &targetFolder = mSourceAccount->translateFolder(mFolder);
    mTargetFolder = mKolabAccount->applyTargetFolderTransformations(targetFolder.second);
    mKolabAccount->createFolder(mTargetFolder, targetFolder.first);

    FetchObjectsJob *fetchJob = mSourceAccount->fetchObjects(mFolder);
    if (!fetchJob) {
        setErrorText("Invalid FetchMessagesJob");
        setError(KJob::UserDefinedError);
        emitResult();
        return;
    }
    connect(fetchJob, SIGNAL(objectsReceived(QString, QList<Object>)), this, SLOT(processMessages(QString, QList<Object>)));
    connect(fetchJob, SIGNAL(result(KJob*)), this, SLOT(fetchJobFinished(KJob*)));
    fetchJob->start();
}

void MigrateFolderJob::fetchJobFinished(KJob *job)
{
    if (job->error()) {
        setErrorText("Fetch job failed");
        setError(KJob::UserDefinedError);
        Warning() << job->errorString();
    }
    mFetchDone = true;
    checkDone();
}

void MigrateFolderJob::processMessages(const QString &folder, const QList<Object> &messages)
{
    foreach (const Object &message, messages) {
        KJob *appendJob = mKolabAccount->appendObject(mSourceAccount->convertObject(message, folder), mTargetFolder);
        if (!appendJob) {
            continue;
        }
        appendJob->setProperty("appendedObject", QVariant::fromValue(message));
        connect(appendJob, SIGNAL(result(KJob*)), this, SLOT(appendJobFinished(KJob*)));
        mRunningAppendJobs++;
        appendJob->start();
    }
}

void MigrateFolderJob::appendJobFinished(KJob *job)
{
    mRunningAppendJobs--;
    if (job->error()) {
        setErrorText("Append job failed");
        setError(KJob::UserDefinedError);
        Warning() << job->errorString();
    } else {
        mSourceAccount->recordSuccessfulMigration(job->property("appendedObject").value<Object>(), mFolder);
    }
    checkDone();
}

void MigrateFolderJob::checkDone()
{
    if (mRunningAppendJobs <= 0 && mFetchDone) {
        emitResult();
    }
}