2015-06-04 01:29:22 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* Copyright (C) 2008 by Volker Lanz <vl@fidra.de> *
|
2016-03-02 19:00:31 +00:00
|
|
|
* Copyright (C) 2016 by Andrius Štikonas <andrius@stikonas.eu> *
|
2015-06-04 01:29:22 +01:00
|
|
|
* *
|
|
|
|
* 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 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 General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.*
|
|
|
|
*************************************************************************/
|
|
|
|
|
2018-04-11 01:52:46 +01:00
|
|
|
#include "ops/operation_p.h"
|
2015-06-04 01:29:22 +01:00
|
|
|
|
|
|
|
#include "core/partition.h"
|
|
|
|
#include "core/device.h"
|
2018-10-19 19:55:35 +01:00
|
|
|
#include "core/lvmdevice.h"
|
2015-06-04 01:29:22 +01:00
|
|
|
|
|
|
|
#include "jobs/job.h"
|
|
|
|
|
|
|
|
#include "util/report.h"
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QIcon>
|
|
|
|
#include <QString>
|
|
|
|
|
|
|
|
#include <KLocalizedString>
|
|
|
|
|
|
|
|
Operation::Operation() :
|
2018-04-11 01:52:46 +01:00
|
|
|
d(std::make_unique<OperationPrivate>())
|
2015-06-04 01:29:22 +01:00
|
|
|
{
|
2018-04-11 01:52:46 +01:00
|
|
|
d->m_Status = StatusNone;
|
|
|
|
d->m_ProgressBase = 0;
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Operation::~Operation()
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
qDeleteAll(jobs());
|
|
|
|
jobs().clear();
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Operation::insertPreviewPartition(Device& device, Partition& p)
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
Q_ASSERT(device.partitionTable());
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
device.partitionTable()->removeUnallocated();
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2018-10-19 20:18:39 +01:00
|
|
|
if (p.parent()->insert(&p)) {
|
|
|
|
if (device.type() == Device::Type::LVM_Device) {
|
|
|
|
const LvmDevice& lvm = static_cast<const LvmDevice&>(device);
|
|
|
|
lvm.setFreePE(lvm.freePE() - p.length());
|
|
|
|
}
|
2018-10-19 19:55:35 +01:00
|
|
|
}
|
2018-10-19 20:18:39 +01:00
|
|
|
else
|
|
|
|
qWarning() << "failed to insert preview partition " << p.deviceNode() << " at " << &p << ".";
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
device.partitionTable()->updateUnallocated(device);
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Operation::removePreviewPartition(Device& device, Partition& p)
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
Q_ASSERT(device.partitionTable());
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2018-10-19 19:55:35 +01:00
|
|
|
if (p.parent()->remove(&p)) {
|
|
|
|
if (device.type() == Device::Type::LVM_Device) {
|
|
|
|
const LvmDevice& lvm = static_cast<const LvmDevice&>(device);
|
|
|
|
lvm.setFreePE(lvm.freePE() + p.length());
|
|
|
|
}
|
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
device.partitionTable()->updateUnallocated(device);
|
2018-10-19 19:55:35 +01:00
|
|
|
}
|
2015-07-13 15:16:36 +01:00
|
|
|
else
|
|
|
|
qWarning() << "failed to remove partition " << p.deviceNode() << " at " << &p << " from preview.";
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @return text describing the Operation's current status */
|
|
|
|
QString Operation::statusText() const
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
static const QString s[] = {
|
2016-07-17 23:41:00 +01:00
|
|
|
xi18nc("@info:progress operation", "None"),
|
|
|
|
xi18nc("@info:progress operation", "Pending"),
|
|
|
|
xi18nc("@info:progress operation", "Running"),
|
|
|
|
xi18nc("@info:progress operation", "Success"),
|
|
|
|
xi18nc("@info:progress operation", "Warning"),
|
|
|
|
xi18nc("@info:progress operation", "Error")
|
2015-07-13 15:16:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
Q_ASSERT(status() >= 0 && static_cast<quint32>(status()) < sizeof(s) / sizeof(s[0]));
|
|
|
|
|
|
|
|
if (status() < 0 || static_cast<quint32>(status()) >= sizeof(s) / sizeof(s[0])) {
|
|
|
|
qWarning() << "invalid status " << status();
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return s[status()];
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @return icon for the current Operation's status */
|
2017-09-08 15:05:55 +01:00
|
|
|
QString Operation::statusIcon() const
|
2015-06-04 01:29:22 +01:00
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
static const QString icons[] = {
|
|
|
|
QString(),
|
|
|
|
QStringLiteral("dialog-information"),
|
|
|
|
QStringLiteral("dialog-information"),
|
|
|
|
QStringLiteral("dialog-ok"),
|
|
|
|
QStringLiteral("dialog-warning"),
|
|
|
|
QStringLiteral("dialog-error")
|
|
|
|
};
|
|
|
|
|
|
|
|
Q_ASSERT(status() >= 0 && static_cast<quint32>(status()) < sizeof(icons) / sizeof(icons[0]));
|
|
|
|
|
|
|
|
if (status() < 0 || static_cast<quint32>(status()) >= sizeof(icons) / sizeof(icons[0])) {
|
|
|
|
qWarning() << "invalid status " << status();
|
2017-09-08 15:05:55 +01:00
|
|
|
return QString();
|
2015-07-13 15:16:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (status() == StatusNone)
|
2017-09-08 15:05:55 +01:00
|
|
|
return QString();
|
2015-07-13 15:16:36 +01:00
|
|
|
|
2017-09-08 15:05:55 +01:00
|
|
|
return icons[status()];
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Operation::addJob(Job* job)
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
if (job) {
|
|
|
|
jobs().append(job);
|
2016-05-18 19:54:36 +01:00
|
|
|
connect(job, &Job::started, this, &Operation::onJobStarted);
|
|
|
|
connect(job, &Job::progress, this, &Operation::progress);
|
|
|
|
connect(job, &Job::finished, this, &Operation::onJobFinished);
|
2015-07-13 15:16:36 +01:00
|
|
|
}
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Operation::onJobStarted()
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
Job* job = qobject_cast<Job*>(sender());
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
if (job)
|
|
|
|
emit jobStarted(job, this);
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Operation::onJobFinished()
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
Job* job = qobject_cast<Job*>(sender());
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
if (job) {
|
|
|
|
setProgressBase(progressBase() + job->numSteps());
|
|
|
|
emit jobFinished(job, this);
|
|
|
|
}
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @return total number of steps to run this Operation */
|
|
|
|
qint32 Operation::totalProgress() const
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
qint32 result = 0;
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2016-08-08 20:45:41 +01:00
|
|
|
for (const auto &job : jobs())
|
2016-06-01 21:00:31 +01:00
|
|
|
result += job->numSteps();
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
return result;
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Execute the operation
|
2015-07-13 15:16:36 +01:00
|
|
|
@param parent the parent Report to create a new child for
|
|
|
|
@return true on success
|
2015-06-04 01:29:22 +01:00
|
|
|
*/
|
|
|
|
bool Operation::execute(Report& parent)
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
bool rval = false;
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
Report* report = parent.newChild(description());
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2016-08-11 14:26:54 +01:00
|
|
|
const auto Jobs = jobs();
|
|
|
|
for (const auto &job : Jobs)
|
2016-06-01 21:00:31 +01:00
|
|
|
if (!(rval = job->run(*report)))
|
|
|
|
break;
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
setStatus(rval ? StatusFinishedSuccess : StatusError);
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2016-07-17 23:41:00 +01:00
|
|
|
report->setStatus(xi18nc("@info:status (success, error, warning...) of operation", "%1: %2", description(), statusText()));
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
return rval;
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
2018-04-11 01:52:46 +01:00
|
|
|
|
|
|
|
Operation::OperationStatus Operation::status() const
|
|
|
|
{
|
|
|
|
return d->m_Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Operation::setStatus(OperationStatus s)
|
|
|
|
{
|
|
|
|
d->m_Status = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<Job*>& Operation::jobs()
|
|
|
|
{
|
|
|
|
return d->m_Jobs;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QList<Job*>& Operation::jobs() const
|
|
|
|
{
|
|
|
|
return d->m_Jobs;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Operation::setProgressBase(qint32 i)
|
|
|
|
{
|
|
|
|
d->m_ProgressBase = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
qint32 Operation::progressBase() const
|
|
|
|
{
|
|
|
|
return d->m_ProgressBase;
|
|
|
|
}
|