2020-09-29 23:55:11 +01:00
/*
SPDX - FileCopyrightText : 2008 - 2010 Volker Lanz < vl @ fidra . de >
SPDX - FileCopyrightText : 2014 - 2020 Andrius Š tikonas < andrius @ stikonas . eu >
SPDX - License - Identifier : GPL - 3.0 - or - later
*/
2015-06-04 01:29:22 +01:00
# include "jobs/setpartflagsjob.h"
# include "backend/corebackend.h"
# include "backend/corebackendmanager.h"
# include "backend/corebackenddevice.h"
# include "backend/corebackendpartitiontable.h"
# include "core/device.h"
# include "core/partition.h"
# include "core/partitionrole.h"
# include "core/partitiontable.h"
# include "util/report.h"
# include <KLocalizedString>
/** Creates a new SetPartFlagsJob
2015-07-13 15:16:36 +01:00
@ param d the Device the Partition whose flags are to be set is on
@ param p the Partition whose flags are to be set
@ param flags the new flags for the Partition
2015-06-04 01:29:22 +01:00
*/
SetPartFlagsJob : : SetPartFlagsJob ( Device & d , Partition & p , PartitionTable : : Flags flags ) :
2015-07-13 15:16:36 +01:00
Job ( ) ,
m_Device ( d ) ,
m_Partition ( p ) ,
m_Flags ( flags )
2015-06-04 01:29:22 +01:00
{
}
qint32 SetPartFlagsJob : : numSteps ( ) const
{
2015-07-13 15:16:36 +01:00
return PartitionTable : : flagList ( ) . size ( ) ;
2015-06-04 01:29:22 +01:00
}
bool SetPartFlagsJob : : run ( Report & parent )
{
2015-07-13 15:16:36 +01:00
bool rval = true ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
Report * report = jobStarted ( parent ) ;
2015-06-04 01:29:22 +01:00
2018-03-31 14:44:40 +01:00
std : : unique_ptr < CoreBackendDevice > backendDevice = CoreBackendManager : : self ( ) - > backend ( ) - > openDevice ( device ( ) ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
if ( backendDevice ) {
2018-03-31 21:49:20 +01:00
std : : unique_ptr < CoreBackendPartitionTable > backendPartitionTable = backendDevice - > openPartitionTable ( ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
if ( backendPartitionTable ) {
2017-11-07 22:55:28 +00:00
int count = 0 ;
2015-06-04 01:29:22 +01:00
2017-11-07 22:55:28 +00:00
for ( const auto & f : PartitionTable : : flagList ( ) ) {
2020-09-15 02:36:46 +01:00
Q_EMIT progress ( + + count ) ;
2015-06-04 01:29:22 +01:00
2017-12-06 00:41:19 +00:00
const bool oldState = ( partition ( ) . activeFlags ( ) & f ) ? true : false ;
2017-11-07 22:55:28 +00:00
const bool state = ( flags ( ) & f ) ? true : false ;
2017-12-06 00:41:19 +00:00
if ( oldState = = state )
2017-12-06 00:27:02 +00:00
continue ;
2015-06-04 01:29:22 +01:00
2017-11-07 22:55:28 +00:00
if ( ! backendPartitionTable - > setFlag ( * report , partition ( ) , f , state ) ) {
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 " ) ) ;
2015-06-04 01:29:22 +01:00
2017-11-07 22:55:28 +00:00
rval = false ;
2015-07-13 15:16:36 +01:00
}
2017-11-07 03:07:51 +00:00
}
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
if ( rval )
backendPartitionTable - > commit ( ) ;
} else
2016-07-17 23:41:00 +01:00
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 ( ) ) ;
2015-07-13 15:16:36 +01:00
} else
2016-07-17 23:41:00 +01:00
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 ( ) ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
if ( rval )
partition ( ) . setFlags ( flags ( ) ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
jobFinished ( * report , rval ) ;
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
}
QString SetPartFlagsJob : : description ( ) const
{
2015-07-13 15:16:36 +01:00
if ( PartitionTable : : flagNames ( flags ( ) ) . size ( ) = = 0 )
2016-07-17 23:41:00 +01:00
return xi18nc ( " @info:progress " , " Clear flags for partition <filename>%1</filename> " , partition ( ) . deviceNode ( ) ) ;
2015-06-04 01:29:22 +01:00
2016-07-17 23:41:00 +01:00
return xi18nc ( " @info:progress " , " Set the flags for partition <filename>%1</filename> to \" %2 \" " , partition ( ) . deviceNode ( ) , PartitionTable : : flagNames ( flags ( ) ) . join ( QStringLiteral ( " , " ) ) ) ;
2015-06-04 01:29:22 +01:00
}