akabeioperation.cpp 6.4 KB
Newer Older
Dario Freddi's avatar
Dario Freddi committed
1 2 3 4 5 6 7 8 9 10
/* This file is part of the Chakra project

   Copyright (C) 2010 Dario Freddi <drf@chakra-project.org>

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

11
#include <akabeioperation_p.h>
12

13 14
#include <akabeioperationrunner_p.h>
#include <akabeibackend.h>
Dario Freddi's avatar
Dario Freddi committed
15

16
#include <QEventLoop>
Lukas Appelhans's avatar
Lukas Appelhans committed
17 18
#include <QDebug>
#include <QCoreApplication>
19

Dario Freddi's avatar
Dario Freddi committed
20 21
namespace Akabei {

22 23 24
OperationPrivate::OperationPrivate(Operation *op, const QString &tN)
    : q_ptr(op)
    , targetName(tN)
Dario Freddi's avatar
Dario Freddi committed
25
    , uuid(QUuid::createUuid())
Lukas Appelhans's avatar
Lukas Appelhans committed
26
    , processingOptions(NoProcessingOption)
Lukas Appelhans's avatar
Lukas Appelhans committed
27
    , eta(0)
Dario Freddi's avatar
Dario Freddi committed
28
{
29
    runner = Backend::instance()->operationRunner();
Dario Freddi's avatar
Dario Freddi committed
30 31
}

32 33 34 35
void OperationPrivate::concurrentValidate()
{
    Q_Q(Operation);
    validateLoop = new QEventLoop;
36
    status = Operation::StatusValidating;
37
    q->validate();
38
    if (status == Operation::StatusValidating) {
Dario Freddi's avatar
Dario Freddi committed
39
        validateLoop.data()->exec();
40
    }
41 42 43
    validateLoop.data()->deleteLater();
}

Dario Freddi's avatar
Dario Freddi committed
44 45 46 47
void OperationPrivate::concurrentRun()
{
    Q_Q(Operation);
    runLoop = new QEventLoop;
48
    setStatus(Operation::StatusRunning);
Lukas Appelhans's avatar
Lukas Appelhans committed
49
    q->run();
50
    if (status == Operation::StatusRunning) {
Dario Freddi's avatar
Dario Freddi committed
51
        runLoop.data()->exec();
52
    }
Dario Freddi's avatar
Dario Freddi committed
53 54 55
    runLoop.data()->deleteLater();
}

Dario Freddi's avatar
Dario Freddi committed
56 57 58 59 60
void OperationPrivate::setStatus(Operation::Status newstatus)
{
    if (status != newstatus) {
        Q_Q(Operation);
        status = newstatus;
61
        runner->d_func()->emitStatus(q, status);
Dario Freddi's avatar
Dario Freddi committed
62 63 64
    }
}

Lukas Appelhans's avatar
Lukas Appelhans committed
65 66 67 68 69
void OperationPrivate::setProcessingOptions(ProcessingOptions options)
{
    processingOptions = options;
}

70
Operation::Operation(const QString &targetName)
71
    : QObject(nullptr), d_ptr(new OperationPrivate(this, targetName))
Dario Freddi's avatar
Dario Freddi committed
72 73 74 75 76 77 78 79
{
}

Operation::~Operation()
{
    delete d_ptr;
}

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
QUuid Operation::uuid() const
{
    Q_D(const Operation);
    return d->uuid;
}

QString Operation::targetName() const
{
    Q_D(const Operation);
    return d->targetName;
}

QStringList Operation::conflictingTargets() const
{
    Q_D(const Operation);
    return d->conflictingTargets;
}

Operation::Phase Operation::phase() const
{
    Q_D(const Operation);
    return d->phase;
}

int Operation::priority() const
{
    Q_D(const Operation);
    return d->priority;
}

Operation::Status Operation::status() const
{
    Q_D(const Operation);
    return d->status;
}

116 117
QStringList Operation::targetAdditions() const
{
Dario Freddi's avatar
Dario Freddi committed
118 119
    Q_D(const Operation);
    return d->targetAdditions;
120 121 122 123
}

QStringList Operation::targetRemovals() const
{
Dario Freddi's avatar
Dario Freddi committed
124 125
    Q_D(const Operation);
    return d->targetRemovals;
126 127
}

128 129 130 131 132 133 134 135 136 137 138 139
QStringList Operation::fileSystemAdditions() const
{
    Q_D(const Operation);
    return d->fileSystemAdditions;
}

QStringList Operation::fileSystemRemovals() const
{
    Q_D(const Operation);
    return d->fileSystemRemovals;
}

Dario Freddi's avatar
Dario Freddi committed
140
bool Operation::canBeConcurrent() const
141
{
142
    Q_D(const Operation);
Dario Freddi's avatar
Dario Freddi committed
143 144 145
    return d->canBeConcurrent;
}

Dario Freddi's avatar
Dario Freddi committed
146 147 148 149 150 151
QString Operation::description() const
{
    Q_D(const Operation);
    return d->description;
}

152
Operation::List Operation::postOperations() const
153 154
{
    Q_D(const Operation);
155 156 157 158 159 160 161
    return d->postOps;
}

Operation::List Operation::preOperations() const
{
    Q_D(const Operation);
    return d->preOps;
162 163
}

164 165 166 167 168 169
QStringList Operation::targetDependencies() const
{
    Q_D(const Operation);
    return d->deps;
}

Dario Freddi's avatar
Dario Freddi committed
170
QByteArray Operation::targetVersion() const
171 172
{
    Q_D(const Operation);
173
    return d->targetVersion;
174 175
}

176
Error::List Operation::errors() const
Dario Freddi's avatar
Dario Freddi committed
177 178 179 180 181
{
    Q_D(const Operation);
    return d->errors;
}

182
ProcessingOptions Operation::processingOptions() const
Lukas Appelhans's avatar
Lukas Appelhans committed
183 184 185 186 187
{
    Q_D(const Operation);
    return d->processingOptions;
}

Lukas Appelhans's avatar
Lukas Appelhans committed
188 189 190 191 192 193
int Operation::eta() const
{
    Q_D(const Operation);
    return d->eta;
}

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
void Operation::setCanBeConcurrent(bool concurrent)
{
    Q_D(Operation);
    d->canBeConcurrent = concurrent;
}

void Operation::setPhase(Phase phase)
{
    Q_D(Operation);
    d->phase = phase;
}

void Operation::setPriority(int priority)
{
    Q_D(Operation);
    d->priority = priority;
}

void Operation::setFileSystemAdditions(const QStringList &targets)
{
    Q_D(Operation);
    d->fileSystemAdditions = targets;
}

void Operation::setFileSystemRemovals(const QStringList &targets)
{
    Q_D(Operation);
    d->fileSystemRemovals = targets;
}

void Operation::setConflictingTargets(const QStringList &targets)
{
    Q_D(Operation);
    d->conflictingTargets = targets;
}

230 231
void Operation::setTargetDependencies(const QStringList& targets)
{
Dario Freddi's avatar
Dario Freddi committed
232 233
    Q_D(Operation);
    d->deps = targets;
234 235 236 237
}

void Operation::setTargetAdditions(const QStringList& targets)
{
Dario Freddi's avatar
Dario Freddi committed
238 239
    Q_D(Operation);
    d->targetAdditions = targets;
240 241 242 243
}

void Operation::setTargetRemovals(const QStringList& targets)
{
Dario Freddi's avatar
Dario Freddi committed
244 245
    Q_D(Operation);
    d->targetRemovals = targets;
246 247
}

248
void Operation::setErrors(Error::List const& errors)
249 250
{
    Q_D(Operation);
Dario Freddi's avatar
Dario Freddi committed
251
    d->errors = errors;
252 253
}

254 255 256 257 258 259 260
void Operation::setPostOperations(const Akabei::Operation::List& operations)
{
    Q_D(Operation);
    d->postOps = operations;
}

void Operation::setPreOperations(const Akabei::Operation::List& operations)
261 262
{
    Q_D(Operation);
263
    d->preOps = operations;
264 265
}

Dario Freddi's avatar
Dario Freddi committed
266 267
void Operation::setProgress(int percentage)
{
Dario Freddi's avatar
Dario Freddi committed
268
    Q_D(Operation);
269
    if (d->runner) {
Lukas Appelhans's avatar
Lukas Appelhans committed
270
        d->runner->d_func()->emitProgress(this, qBound(0, percentage, 100));
271
    }
Dario Freddi's avatar
Dario Freddi committed
272 273 274 275
}

void Operation::setFinished(bool result)
{
Dario Freddi's avatar
Dario Freddi committed
276
    Q_D(Operation);
Lukas Appelhans's avatar
Lukas Appelhans committed
277 278
    if ((result && d->status == StatusPerformed) || (!result && d->status == StatusError))
        return;
Dario Freddi's avatar
Dario Freddi committed
279
    if (result) {
280
        d->setStatus(StatusPerformed);
Dario Freddi's avatar
Dario Freddi committed
281
    } else {
282
        d->setStatus(StatusError);
Dario Freddi's avatar
Dario Freddi committed
283
    }
Lukas Appelhans's avatar
Lukas Appelhans committed
284
    setEta(0);
285
    if (!d->runLoop.isNull()) {
Dario Freddi's avatar
Dario Freddi committed
286
        d->runLoop.data()->quit();
287
    }
Dario Freddi's avatar
Dario Freddi committed
288 289 290 291
}

void Operation::setValidationFinished(bool result)
{
Dario Freddi's avatar
Dario Freddi committed
292 293
    Q_D(Operation);
    if (result) {
294
        d->setStatus(StatusReady);
Dario Freddi's avatar
Dario Freddi committed
295
    } else {
296
        d->setStatus(StatusNotReady);
Dario Freddi's avatar
Dario Freddi committed
297
    }
298
    if (!d->validateLoop.isNull()) {
Dario Freddi's avatar
Dario Freddi committed
299
        d->validateLoop.data()->quit();
300
    }
Dario Freddi's avatar
Dario Freddi committed
301 302
}

Dario Freddi's avatar
Dario Freddi committed
303
void Operation::setTargetVersion(const QByteArray& version)
304 305
{
    Q_D(Operation);
306
    d->targetVersion = version;
307 308
}

Dario Freddi's avatar
Dario Freddi committed
309 310 311 312 313 314
void Operation::setDescription(const QString& string)
{
    Q_D(Operation);
    d->description = string;
}

Dario Freddi's avatar
Dario Freddi committed
315 316 317
void Operation::addMessage(const QString& message)
{
    Q_D(Operation);
318
    if (d->runner) {
319
        d->runner->d_func()->emitMessage(this, message);
320
    }
Dario Freddi's avatar
Dario Freddi committed
321 322
}

Lukas Appelhans's avatar
Lukas Appelhans committed
323 324 325 326 327 328 329 330 331
void Operation::setEta(int eta)
{
    Q_D(Operation);
    d->eta = eta;
    if (d->runner) {
        d->runner->d_func()->emitEtaChanged(this, eta);
    }
}

332
}
Lukas Appelhans's avatar
Lukas Appelhans committed
333

AlmAck's avatar
AlmAck committed
334
#include "moc_akabeioperation.cpp"