summaryrefslogtreecommitdiff
path: root/migrationutility/migrateuserjob.cpp
blob: 3b894a97610e1224b40d8d471f42155e8d1db1ad (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
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
/*
 * 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 "migrateuserjob.h"
#include "migratefolderjob.h"
#include "sourceaccount.h"
#include "kolabaccount.h"
#include <errorhandler.h>

MigrateUserJob::MigrateUserJob(const QList<SourceAccount*> &sourceAccounts, KolabAccount *kolabAccount, QObject* parent)
:   KJob(parent),
    mSourceAccounts(sourceAccounts),
    mCurrentSourceAccount(0),
    mKolabAccount(kolabAccount)
{
}

void MigrateUserJob::start()
{
    if (!mKolabAccount || mSourceAccounts.isEmpty()) {
        setError(KJob::UserDefinedError);
        setErrorText("Failed to get source or target account");
        emitResult();
        return;
    }
    mKolabAccount->cleanAccount();
    mKolabAccount->setupFolders();
    migrateNextAccount();
}

void MigrateUserJob::migrateNextAccount()
{
    if (mCurrentSourceAccount) {
        mCurrentSourceAccount->deleteLater();
    }
    if (mSourceAccounts.isEmpty()) {
        mKolabAccount->logout();
        emitResult();
        return;
    }
    mCurrentSourceAccount = mSourceAccounts.takeFirst();
    KJob *loginJob = mCurrentSourceAccount->login();
    Q_ASSERT(loginJob);
    connect(loginJob, SIGNAL(result(KJob*)), this, SLOT(onLoginDone(KJob*)));
    loginJob->start();
}

void MigrateUserJob::onLoginDone(KJob *job)
{
    if (job->error()) {
        Error() << "Failed to login in source account: " << job->errorString();
        emitResult();
        return;
    }
    Debug() << "login successful";
    //Go to eventloop first, to ensure the session is fully initialized before running further jobs
    QMetaObject::invokeMethod(this, "fetchFolders", Qt::QueuedConnection);
}

void MigrateUserJob::fetchFolders()
{
    FetchFoldersJob *fetchJob = mCurrentSourceAccount->fetchFolderList();
    if (!fetchJob) {
        mFolderQueue = mCurrentSourceAccount->lookupFolderList();
        migrateNextFolder();
    } else {
       connect(fetchJob, SIGNAL(foldersReceived(QStringList)), this, SLOT(onFoldersReceived(QStringList)));
       connect(fetchJob, SIGNAL(result(KJob*)), this, SLOT(onFoldersFetched(KJob*)));
       fetchJob->start();
    }
}

void MigrateUserJob::onFoldersReceived(const QStringList &folders)
{
    mFolderQueue.append(folders);
}

void MigrateUserJob::onFoldersFetched(KJob* )
{
    migrateNextFolder();
}

void MigrateUserJob::migrateNextFolder()
{
    if (!mCurrentFolder.isEmpty()) {
        Warning() << "already migrating a folder";
        return;
    }
    if (mFolderQueue.isEmpty()) {
        Debug() << "Account migrated, logging out";
        KJob *logoutJob = mCurrentSourceAccount->logout();
        if (logoutJob) {
            connect(logoutJob, SIGNAL(result(KJob*)), this, SLOT(migrateNextAccount()));
            logoutJob->start();
        } else {
            QMetaObject::invokeMethod(this, "migrateNextAccount", Qt::QueuedConnection);
        }
        //TODO here we could delete the source account
        return;
    }
    mCurrentFolder = mFolderQueue.takeFirst();
    if (isIgnored(mCurrentFolder)) {
        Debug() << "Skipping source folder: " << mCurrentFolder;
        mCurrentFolder.clear();
        migrateNextFolder();
    }

    MigrateFolderJob *job = new MigrateFolderJob(mCurrentFolder, mCurrentSourceAccount, mKolabAccount, this);
    connect(job, SIGNAL(result(KJob*)), this, SLOT(onFolderMigrated(KJob*)));
    job->start();
}

void MigrateUserJob::onFolderMigrated(KJob *job)
{
    if (job->error()) {
        Error() << "Failed to migrate folder: " << mCurrentFolder;
        Error() << job->errorString();
    }
    mCurrentFolder.clear();
    migrateNextFolder();
}

bool MigrateUserJob::isIgnored(const QString &sourceFolder) const
{
    foreach (const QString &ignoredFolder, mCurrentSourceAccount->ignoredFolders()) {
        if (sourceFolder.contains(ignoredFolder)) {
            return true;
        }
    }
    return false;
}