2015-06-04 01:29:22 +01:00
/*************************************************************************
* Copyright ( C ) 2008 , 2010 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/>.*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# include "jobs/setpartflagsjob.h"
# include "backend/corebackend.h"
# include "backend/corebackendmanager.h"
# include "backend/corebackenddevice.h"
# include "backend/corebackendpartition.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
2015-07-13 15:16:36 +01:00
CoreBackendDevice * backendDevice = CoreBackendManager : : self ( ) - > backend ( ) - > openDevice ( device ( ) . deviceNode ( ) ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
if ( backendDevice ) {
CoreBackendPartitionTable * backendPartitionTable = backendDevice - > openPartitionTable ( ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
if ( backendPartitionTable ) {
CoreBackendPartition * backendPartition = ( partition ( ) . roles ( ) . has ( PartitionRole : : Extended ) )
? backendPartitionTable - > getExtendedPartition ( )
: backendPartitionTable - > getPartitionBySector ( partition ( ) . firstSector ( ) ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
if ( backendPartition ) {
quint32 count = 0 ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
foreach ( const PartitionTable : : Flag & f , PartitionTable : : flagList ( ) ) {
emit progress ( + + count ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
const bool state = ( flags ( ) & f ) ? true : false ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
if ( ! backendPartition - > setFlag ( * report , f , state ) ) {
2016-07-17 23:41:00 +01:00
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
2015-07-13 15:16:36 +01:00
rval = false ;
}
}
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
delete backendPartition ;
} else
2016-07-17 23:41:00 +01:00
report - > line ( ) < < xi18nc ( " @info:progress " , " Could not find partition <filename>%1</filename> on device <filename>%2</filename> to set partition flags. " , partition ( ) . deviceNode ( ) , device ( ) . deviceNode ( ) ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
if ( rval )
backendPartitionTable - > commit ( ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
delete backendPartitionTable ;
} 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-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
delete backendDevice ;
} 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
}