File: migratorbase.cpp

package info (click to toggle)
kdepim-runtime 4%3A25.12.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 26,184 kB
  • sloc: cpp: 90,565; xml: 1,074; javascript: 60; sh: 58; makefile: 11
file content (287 lines) | stat: -rw-r--r-- 7,650 bytes parent folder | download | duplicates (2)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
 * SPDX-FileCopyrightText: 2013 Christian Mollekopf <mollekopf@kolabsys.com>
 *
 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
 *
 */

#include "migratorbase.h"
#include "migration_debug.h"
#include <Akonadi/ServerManager>
#include <KLocalizedString>
#include <QCoreApplication>
#include <QDateTime>
#include <QDir>
#include <QFileInfo>
#include <QStandardPaths>

static QString messageTypeToString(MigratorBase::MessageType type)
{
    switch (type) {
    case MigratorBase::Success:
        return QStringLiteral("Success");
    case MigratorBase::Skip:
        return QStringLiteral("Skipped");
    case MigratorBase::Info:
        return QStringLiteral("Info   ");
    case MigratorBase::Warning:
        return QStringLiteral("WARNING");
    case MigratorBase::Error:
        return QStringLiteral("ERROR  ");
    }
    Q_ASSERT(false);
    return {};
}

static QMap<QString, MigratorBase::MigrationState> fillMigrationStateMapping()
{
    QMap<QString, MigratorBase::MigrationState> map;
    map.insert(QStringLiteral("Complete"), MigratorBase::Complete);
    map.insert(QStringLiteral("Aborted"), MigratorBase::Aborted);
    map.insert(QStringLiteral("InProgress"), MigratorBase::InProgress);
    map.insert(QStringLiteral("Failed"), MigratorBase::Failed);
    return map;
}

static QMap<QString, MigratorBase::MigrationState> migrationStateMapping = fillMigrationStateMapping();

static QString stateToIdentifier(MigratorBase::MigrationState state)
{
    Q_ASSERT(migrationStateMapping.values().contains(state));
    return migrationStateMapping.key(state);
}

static MigratorBase::MigrationState identifierToState(const QString &identifier)
{
    Q_ASSERT(migrationStateMapping.contains(identifier));
    return migrationStateMapping.value(identifier);
}

MigratorBase::MigratorBase(const QString &identifier, QObject *parent)
    : QObject(parent)
    , mIdentifier(identifier)
    , mConfig(new KConfig(Akonadi::ServerManager::addNamespace(QStringLiteral("akonadi-migrationrc"))))
{
    const QString logFileName = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') + QCoreApplication::applicationName()
        + QLatin1Char('/') + identifier + QStringLiteral("migration.log");
    QFileInfo fileInfo(logFileName);
    QDir().mkpath(fileInfo.absolutePath());
    setLogfile(logFileName);
    connect(this, &MigratorBase::message, this, &MigratorBase::logMessage);
    loadState();
}

MigratorBase::MigratorBase(const QString &identifier, const QString &configFile, const QString &logFile, QObject *parent)
    : QObject(parent)
    , mIdentifier(identifier)
    , mMigrationState(None)
{
    if (!configFile.isEmpty()) {
        mConfig.reset(new KConfig(configFile));
    }
    setLogfile(logFile);
    connect(this, &MigratorBase::message, this, &MigratorBase::logMessage);
    loadState();
}

MigratorBase::~MigratorBase() = default;

void MigratorBase::setLogfile(const QString &logfile)
{
    if (!logfile.isEmpty()) {
        mLogFile.reset(new QFile(logfile));
        if (!mLogFile->open(QFile::Append)) {
            mLogFile.reset();
            qCWarning(MIGRATION_LOG) << "Unable to open log file: " << logfile;
        }
    } else {
        mLogFile.reset();
    }
}

QString MigratorBase::identifier() const
{
    return mIdentifier;
}

QString MigratorBase::displayName() const
{
    return {};
}

QString MigratorBase::description() const
{
    return {};
}

QString MigratorBase::logfile() const
{
    if (mLogFile) {
        return mLogFile->fileName();
    }
    return {};
}

bool MigratorBase::canStart()
{
    if (mIdentifier.isEmpty()) {
        Q_EMIT message(Error, i18n("Missing Identifier"));
        return false;
    }
    return true;
}

void MigratorBase::start()
{
    if (mMigrationState == InProgress) {
        qCWarning(MIGRATION_LOG) << "already running";
        return;
    }
    if (!canStart()) {
        Q_EMIT message(Error, i18n("Failed to start migration because migrator is not ready"));
        Q_EMIT stoppedProcessing();
        return;
    }
    // TODO acquire dbus lock
    logMessage(Info, displayName());
    Q_EMIT message(Info, i18n("Starting migration…"));
    setMigrationState(InProgress);
    setProgress(0);
    startWork();
}

void MigratorBase::pause()
{
    qCWarning(MIGRATION_LOG) << "pause is not implemented";
}

void MigratorBase::resume()
{
    qCWarning(MIGRATION_LOG) << "resume is not implemented";
}

void MigratorBase::abort()
{
    qCWarning(MIGRATION_LOG) << "abort is not implemented";
}

void MigratorBase::logMessage(MigratorBase::MessageType type, const QString &msg)
{
    if (mLogFile) {
        mLogFile->write(QString(QLatin1Char('[') + QDateTime::currentDateTime().toString() + QStringLiteral("] ") + messageTypeToString(type)
                                + QStringLiteral(": ") + msg + QLatin1Char('\n'))
                            .toUtf8());
        mLogFile->flush();
    }
}

bool MigratorBase::shouldAutostart() const
{
    return false;
}

void MigratorBase::setMigrationState(MigratorBase::MigrationState state)
{
    mMigrationState = state;
    switch (state) {
    case Complete:
        setProgress(100);
        Q_EMIT message(Success, i18n("Migration complete"));
        Q_EMIT stoppedProcessing();
        break;
    case Aborted:
        Q_EMIT message(Skip, i18n("Migration aborted"));
        Q_EMIT stoppedProcessing();
        break;
    case InProgress:
        break;
    case Failed:
        Q_EMIT message(Error, i18n("Migration failed"));
        Q_EMIT stoppedProcessing();
        break;
    case Paused:
        Q_EMIT message(Info, i18n("Migration paused"));
        Q_EMIT stateChanged(mMigrationState);
        return;
    default:
        qCWarning(MIGRATION_LOG) << "invalid state " << state;
        Q_ASSERT(false);
        return;
    }
    saveState();
    Q_EMIT stateChanged(mMigrationState);
}

MigratorBase::MigrationState MigratorBase::migrationState() const
{
    return mMigrationState;
}

void MigratorBase::saveState()
{
    config().writeEntry(QStringLiteral("MigrationState"), stateToIdentifier(mMigrationState));
}

void MigratorBase::loadState()
{
    const QString state = config().readEntry(QStringLiteral("MigrationState"), QString());
    if (!state.isEmpty()) {
        mMigrationState = identifierToState(state);
    }

    if (mMigrationState == InProgress) {
        Q_EMIT message(Warning, i18n("This migration has already been started once but was aborted"));
        mMigrationState = NeedsUpdate;
    }
    switch (mMigrationState) {
    case Complete:
        mProgress = 100;
        break;
    default:
        mProgress = 0;
    }
}

NullableConfigGroup MigratorBase::config()
{
    if (mConfig) {
        return NullableConfigGroup(mConfig->group(mIdentifier));
    }
    return {};
}

int MigratorBase::progress() const
{
    return mProgress;
}

void MigratorBase::setProgress(int prog)
{
    if (mProgress != prog) {
        mProgress = prog;
        Q_EMIT progress(prog);
    }
}

QString MigratorBase::status() const
{
    switch (mMigrationState) {
    case None:
        return i18nc("@info:status", "Not started");
    case InProgress:
        return i18nc("@info:status", "Running…");
    case Complete:
        return i18nc("@info:status", "Complete");
    case Aborted:
        return i18nc("@info:status", "Aborted");
    case Paused:
        return i18nc("@info:status", "Paused");
    case NeedsUpdate:
        return i18nc("@info:status", "Needs Update");
    case Failed:
        return i18nc("@info:status", "Failed");
    }
    return {};
}

#include "moc_migratorbase.cpp"