2020-09-30 23:28:47 +01:00
/*
SPDX - FileCopyrightText : 2008 - 2010 Volker Lanz < vl @ fidra . de >
SPDX - FileCopyrightText : 2014 - 2018 Andrius Š tikonas < andrius @ stikonas . eu >
SPDX - FileCopyrightText : 2016 Chantara Tith < tith . chantara @ gmail . com >
2020-10-01 00:33:19 +01:00
SPDX - FileCopyrightText : 2018 Caio Jordão Carvalho < caiojcarvalho @ gmail . com >
2020-09-30 23:28:47 +01:00
SPDX - License - Identifier : GPL - 3.0 - or - later
*/
2015-06-04 01:29:22 +01:00
# include "ops/createpartitiontableoperation.h"
# include "core/device.h"
# include "core/partitiontable.h"
# include "core/partition.h"
2018-07-15 14:31:27 +01:00
# include "core/raid/softwareraid.h"
2015-06-04 01:29:22 +01:00
# include "jobs/createpartitiontablejob.h"
# include <QString>
# include <KLocalizedString>
/** Creates a new CreatePartitionTableOperation.
2015-07-13 15:16:36 +01:00
@ param d the Device to create the new PartitionTable on
@ param t the type for the new PartitionTable
2015-06-04 01:29:22 +01:00
*/
CreatePartitionTableOperation : : CreatePartitionTableOperation ( Device & d , PartitionTable : : TableType t ) :
2015-07-13 15:16:36 +01:00
Operation ( ) ,
m_TargetDevice ( d ) ,
m_OldPartitionTable ( targetDevice ( ) . partitionTable ( ) ) ,
m_PartitionTable ( new PartitionTable ( t , PartitionTable : : defaultFirstUsable ( d , t ) , PartitionTable : : defaultLastUsable ( d , t ) ) ) ,
m_CreatePartitionTableJob ( new CreatePartitionTableJob ( targetDevice ( ) ) )
2015-06-04 01:29:22 +01:00
{
2015-07-13 15:16:36 +01:00
addJob ( createPartitionTableJob ( ) ) ;
2015-06-04 01:29:22 +01:00
}
/** Creates a new CreatePartitionTableOperation.
2015-07-13 15:16:36 +01:00
@ param d the Device to create the new PartitionTable on
@ param ptable pointer to the new partition table object . the operation takes ownership .
2015-06-04 01:29:22 +01:00
*/
CreatePartitionTableOperation : : CreatePartitionTableOperation ( Device & d , PartitionTable * ptable ) :
2015-07-13 15:16:36 +01:00
Operation ( ) ,
m_TargetDevice ( d ) ,
m_OldPartitionTable ( targetDevice ( ) . partitionTable ( ) ) ,
m_PartitionTable ( ptable ) ,
m_CreatePartitionTableJob ( new CreatePartitionTableJob ( targetDevice ( ) ) )
2015-06-04 01:29:22 +01:00
{
2015-07-13 15:16:36 +01:00
addJob ( createPartitionTableJob ( ) ) ;
2015-06-04 01:29:22 +01:00
}
CreatePartitionTableOperation : : ~ CreatePartitionTableOperation ( )
{
2015-07-13 15:16:36 +01:00
if ( status ( ) = = StatusPending )
delete m_PartitionTable ;
2015-06-04 01:29:22 +01:00
}
bool CreatePartitionTableOperation : : targets ( const Device & d ) const
{
2015-07-13 15:16:36 +01:00
return d = = targetDevice ( ) ;
2015-06-04 01:29:22 +01:00
}
void CreatePartitionTableOperation : : preview ( )
{
2015-07-13 15:16:36 +01:00
targetDevice ( ) . setPartitionTable ( partitionTable ( ) ) ;
targetDevice ( ) . partitionTable ( ) - > updateUnallocated ( targetDevice ( ) ) ;
2015-06-04 01:29:22 +01:00
}
void CreatePartitionTableOperation : : undo ( )
{
2015-07-13 15:16:36 +01:00
targetDevice ( ) . setPartitionTable ( oldPartitionTable ( ) ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
if ( targetDevice ( ) . partitionTable ( ) )
targetDevice ( ) . partitionTable ( ) - > updateUnallocated ( targetDevice ( ) ) ;
2015-06-04 01:29:22 +01:00
}
bool CreatePartitionTableOperation : : execute ( Report & parent )
{
2015-07-13 15:16:36 +01:00
targetDevice ( ) . setPartitionTable ( partitionTable ( ) ) ;
return Operation : : execute ( parent ) ;
2015-06-04 01:29:22 +01:00
}
/** Can a new partition table be created on a device?
2015-07-22 14:48:03 +01:00
@ param device pointer to the device , can be nullptr
2015-07-13 15:16:36 +01:00
@ return true if a new partition table can be created on @ p device
2015-06-04 01:29:22 +01:00
*/
bool CreatePartitionTableOperation : : canCreate ( const Device * device )
{
2018-07-15 14:31:27 +01:00
if ( device = = nullptr )
return false ;
if ( device - > type ( ) = = Device : : Type : : SoftwareRAID_Device ) {
const SoftwareRAID * raid = static_cast < const SoftwareRAID * > ( device ) ;
2018-07-15 17:11:50 +01:00
if ( raid - > status ( ) = = SoftwareRAID : : Status : : Inactive )
2018-07-15 14:31:27 +01:00
return false ;
}
return ( device - > partitionTable ( ) = = nullptr | | ! device - > partitionTable ( ) - > isChildMounted ( ) )
& & ( device - > type ( ) ! = Device : : Type : : LVM_Device ) ;
2015-06-04 01:29:22 +01:00
}
QString CreatePartitionTableOperation : : description ( ) const
{
2016-07-17 23:41:00 +01:00
return xi18nc ( " @info:status " , " Create a new partition table (type: %1) on < filename > % 2 < / filename > " , partitionTable()->typeName(), targetDevice().deviceNode()) ;
2015-06-04 01:29:22 +01:00
}