1
/***************************************************************************
2
* Copyright (C) 2008 by Volker Lanz <vl@fidra.de> *
4
* This program is free software; you can redistribute it and/or modify *
5
* it under the terms of the GNU General Public License as published by *
6
* the Free Software Foundation; either version 2 of the License, or *
7
* (at your option) any later version. *
9
* This program is distributed in the hope that it will be useful, *
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12
* GNU General Public License for more details. *
14
* You should have received a copy of the GNU General Public License *
15
* along with this program; if not, write to the *
16
* Free Software Foundation, Inc., *
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18
***************************************************************************/
20
#include "ops/newoperation.h"
22
#include "core/partition.h"
23
#include "core/device.h"
24
#include "core/partitionnode.h"
26
#include "jobs/createpartitionjob.h"
27
#include "jobs/createfilesystemjob.h"
28
#include "jobs/setfilesystemlabeljob.h"
29
#include "jobs/checkfilesystemjob.h"
31
#include "fs/filesystem.h"
32
#include "fs/filesystemfactory.h"
34
#include "util/capacity.h"
41
/** Creates a new NewOperation.
42
@param d the Device to create a new Partition on
43
@param p pointer to the new Partition to create. May not be NULL.
45
NewOperation::NewOperation(Device& d, Partition* p) :
49
m_CreatePartitionJob(new CreatePartitionJob(targetDevice(), newPartition())),
50
m_CreateFileSystemJob(NULL),
51
m_SetFileSystemLabelJob(NULL),
52
m_CheckFileSystemJob(NULL)
54
addJob(createPartitionJob());
56
const FileSystem& fs = newPartition().fileSystem();
58
if (fs.type() != FileSystem::Extended)
60
// It would seem tempting to skip the CreateFileSystemJob or the
61
// SetFileSystemLabelJob if either has nothing to do (unformatted FS or
62
// empty label). However, the user might later on decide to change FS or
63
// label. The operation stack will merge these operations with this one here
64
// and if the jobs don't exist things will break.
66
m_CreateFileSystemJob = new CreateFileSystemJob(newPartition());
67
addJob(createFileSystemJob());
69
m_SetFileSystemLabelJob = new SetFileSystemLabelJob(newPartition(), fs.label());
70
addJob(setLabelJob());
72
m_CheckFileSystemJob = new CheckFileSystemJob(newPartition());
77
NewOperation::~NewOperation()
79
if (status() == StatusPending)
80
delete m_NewPartition;
83
void NewOperation::preview()
85
insertPreviewPartition(targetDevice(), newPartition());
88
void NewOperation::undo()
90
removePreviewPartition(targetDevice(), newPartition());
93
QString NewOperation::description() const
95
return QString(i18nc("@info/plain", "Create a new partition (%1, %2) on <filename>%3</filename>", Capacity(newPartition()).toString(), newPartition().fileSystem().name(), targetDevice().deviceNode()));
98
/** Can a Partition be created somewhere?
99
@param p the Partition where a new Partition is to be created, may be NULL
100
@return true if a new Partition can be created in @p p
102
bool NewOperation::canCreateNew(const Partition* p)
104
return p != NULL && p->roles().has(PartitionRole::Unallocated);
107
Partition* NewOperation::createNew(const Partition& cloneFrom)
109
Partition* p = new Partition(cloneFrom);
111
p->deleteFileSystem();
112
p->setFileSystem(FileSystemFactory::create(FileSystem::defaultFileSystem(), p->firstSector(), p->lastSector()));
113
p->setState(Partition::StateNew);