File indexing completed on 2024-04-28 05:45:54

0001 /*
0002     SPDX-FileCopyrightText: 2008-2010 Volker Lanz <vl@fidra.de>
0003     SPDX-FileCopyrightText: 2014-2020 Andrius Štikonas <andrius@stikonas.eu>
0004 
0005     SPDX-License-Identifier: GPL-3.0-or-later
0006 */
0007 
0008 #include "jobs/setpartflagsjob.h"
0009 
0010 #include "backend/corebackend.h"
0011 #include "backend/corebackendmanager.h"
0012 #include "backend/corebackenddevice.h"
0013 #include "backend/corebackendpartitiontable.h"
0014 
0015 #include "core/device.h"
0016 #include "core/partition.h"
0017 #include "core/partitionrole.h"
0018 #include "core/partitiontable.h"
0019 
0020 #include "util/report.h"
0021 
0022 #include <KLocalizedString>
0023 
0024 /** Creates a new SetPartFlagsJob
0025     @param d the Device the Partition whose flags are to be set is on
0026     @param p the Partition whose flags are to be set
0027     @param flags the new flags for the Partition
0028 */
0029 SetPartFlagsJob::SetPartFlagsJob(Device& d, Partition& p, PartitionTable::Flags flags) :
0030     Job(),
0031     m_Device(d),
0032     m_Partition(p),
0033     m_Flags(flags)
0034 {
0035 }
0036 
0037 qint32 SetPartFlagsJob::numSteps() const
0038 {
0039     return PartitionTable::flagList().size();
0040 }
0041 
0042 bool SetPartFlagsJob::run(Report& parent)
0043 {
0044     bool rval = true;
0045 
0046     Report* report = jobStarted(parent);
0047 
0048     std::unique_ptr<CoreBackendDevice> backendDevice = CoreBackendManager::self()->backend()->openDevice(device());
0049 
0050     if (backendDevice) {
0051         std::unique_ptr<CoreBackendPartitionTable> backendPartitionTable = backendDevice->openPartitionTable();
0052 
0053         if (backendPartitionTable) {
0054             int count = 0;
0055 
0056             for (const auto &f : PartitionTable::flagList()) {
0057                 Q_EMIT progress(++count);
0058 
0059                 const bool oldState = (partition().activeFlags() & f) ? true : false;
0060                 const bool state = (flags() & f) ? true : false;
0061                 if (oldState == state)
0062                         continue;
0063 
0064                 if (!backendPartitionTable->setFlag(*report, partition(), f, state)) {
0065                     report->line() << xi18nc("@info:progress", "There was an error setting flag %1 for partition <filename>%2</filename> to state %3.", PartitionTable::flagName(f), partition().deviceNode(), state ? xi18nc("@info:progress flag turned on, active", "on") : xi18nc("@info:progress flag turned off, inactive", "off"));
0066 
0067                     rval = false;
0068                 }
0069             }
0070 
0071             if (rval)
0072                 backendPartitionTable->commit();
0073         } else
0074             report->line() << xi18nc("@info:progress", "Could not open partition table on device <filename>%1</filename> to set partition flags for partition <filename>%2</filename>.", device().deviceNode(), partition().deviceNode());
0075     } else
0076         report->line() << xi18nc("@info:progress", "Could not open device <filename>%1</filename> to set partition flags for partition <filename>%2</filename>.", device().deviceNode(), partition().deviceNode());
0077 
0078     if (rval)
0079         partition().setFlags(flags());
0080 
0081     jobFinished(*report, rval);
0082 
0083     return rval;
0084 }
0085 
0086 QString SetPartFlagsJob::description() const
0087 {
0088     if (PartitionTable::flagNames(flags()).size() == 0)
0089         return xi18nc("@info:progress", "Clear flags for partition <filename>%1</filename>", partition().deviceNode());
0090 
0091     return xi18nc("@info:progress", "Set the flags for partition <filename>%1</filename> to \"%2\"", partition().deviceNode(), PartitionTable::flagNames(flags()).join(QStringLiteral(",")));
0092 }