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/>.*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# include "ops/newoperation.h"
# include "core/partition.h"
# include "core/device.h"
# include "core/partitionnode.h"
# include "jobs/createpartitionjob.h"
# include "jobs/createfilesystemjob.h"
# include "jobs/setfilesystemlabeljob.h"
# include "jobs/setpartflagsjob.h"
# include "jobs/checkfilesystemjob.h"
# include "fs/filesystem.h"
# include "fs/filesystemfactory.h"
# include "util/capacity.h"
# include <QString>
# include <KLocalizedString>
/** Creates a new NewOperation.
2015-07-13 15:16:36 +01:00
@ param d the Device to create a new Partition on
2015-07-22 14:48:03 +01:00
@ param p pointer to the new Partition to create . May not be nullptr .
2015-06-04 01:29:22 +01:00
*/
NewOperation : : NewOperation ( Device & d , Partition * p ) :
2015-07-13 15:16:36 +01:00
Operation ( ) ,
m_TargetDevice ( d ) ,
m_NewPartition ( p ) ,
m_CreatePartitionJob ( new CreatePartitionJob ( targetDevice ( ) , newPartition ( ) ) ) ,
2015-07-22 14:48:03 +01:00
m_CreateFileSystemJob ( nullptr ) ,
m_SetPartFlagsJob ( nullptr ) ,
m_SetFileSystemLabelJob ( nullptr ) ,
m_CheckFileSystemJob ( nullptr )
2015-06-04 01:29:22 +01:00
{
2015-07-13 15:16:36 +01:00
addJob ( createPartitionJob ( ) ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
const FileSystem & fs = newPartition ( ) . fileSystem ( ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
if ( fs . type ( ) ! = FileSystem : : Extended ) {
// It would seem tempting to skip the CreateFileSystemJob or the
// SetFileSystemLabelJob if either has nothing to do (unformatted FS or
// empty label). However, the user might later on decide to change FS or
// label. The operation stack will merge these operations with this one here
// and if the jobs don't exist things will break.
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
m_CreateFileSystemJob = new CreateFileSystemJob ( targetDevice ( ) , newPartition ( ) ) ;
addJob ( createFileSystemJob ( ) ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
if ( fs . type ( ) = = FileSystem : : Lvm2_PV ) {
m_SetPartFlagsJob = new SetPartFlagsJob ( targetDevice ( ) , newPartition ( ) , PartitionTable : : FlagLvm ) ;
addJob ( setPartFlagsJob ( ) ) ;
}
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
m_SetFileSystemLabelJob = new SetFileSystemLabelJob ( newPartition ( ) , fs . label ( ) ) ;
addJob ( setLabelJob ( ) ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
m_CheckFileSystemJob = new CheckFileSystemJob ( newPartition ( ) ) ;
addJob ( checkJob ( ) ) ;
}
2015-06-04 01:29:22 +01:00
}
NewOperation : : ~ NewOperation ( )
{
2015-07-13 15:16:36 +01:00
if ( status ( ) = = StatusPending )
delete m_NewPartition ;
2015-06-04 01:29:22 +01:00
}
bool NewOperation : : targets ( const Device & d ) const
{
2015-07-13 15:16:36 +01:00
return d = = targetDevice ( ) ;
2015-06-04 01:29:22 +01:00
}
bool NewOperation : : targets ( const Partition & p ) const
{
2015-07-13 15:16:36 +01:00
return p = = newPartition ( ) ;
2015-06-04 01:29:22 +01:00
}
void NewOperation : : preview ( )
{
2015-07-13 15:16:36 +01:00
insertPreviewPartition ( targetDevice ( ) , newPartition ( ) ) ;
2015-06-04 01:29:22 +01:00
}
void NewOperation : : undo ( )
{
2015-07-13 15:16:36 +01:00
removePreviewPartition ( targetDevice ( ) , newPartition ( ) ) ;
2015-06-04 01:29:22 +01:00
}
QString NewOperation : : description ( ) const
{
2016-07-17 23:41:00 +01:00
return xi18nc ( " @info:status " , " Create a new partition (%1, %2) on < filename > % 3 < / filename > " , Capacity::formatByteSize(newPartition().capacity()), newPartition().fileSystem().name(), targetDevice().deviceNode()) ;
2015-06-04 01:29:22 +01:00
}
/** Can a Partition be created somewhere?
2015-07-22 14:48:03 +01:00
@ param p the Partition where a new Partition is to be created , may be nullptr
2015-07-13 15:16:36 +01:00
@ return true if a new Partition can be created in @ p p
2015-06-04 01:29:22 +01:00
*/
bool NewOperation : : canCreateNew ( const Partition * p )
{
2015-07-22 14:48:03 +01:00
return p ! = nullptr & & p - > roles ( ) . has ( PartitionRole : : Unallocated ) ;
2015-06-04 01:29:22 +01:00
}
2015-07-13 14:28:56 +01:00
Partition * NewOperation : : createNew ( const Partition & cloneFrom ,
2015-07-13 15:16:36 +01:00
FileSystem : : Type type )
2015-06-04 01:29:22 +01:00
{
2015-07-13 15:16:36 +01:00
Partition * p = new Partition ( cloneFrom ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
p - > deleteFileSystem ( ) ;
p - > setFileSystem ( FileSystemFactory : : create ( type ,
p - > firstSector ( ) ,
p - > lastSector ( ) ) ) ;
p - > setState ( Partition : : StateNew ) ;
p - > setPartitionPath ( QString ( ) ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
return p ;
2015-06-04 01:29:22 +01:00
}