source: calamares/trunk/fuentes/src/modules/partition/gui/CreatePartitionDialog.cpp @ 7538

Last change on this file since 7538 was 7538, checked in by kbut, 13 months ago

sync with github

File size: 10.0 KB
Line 
1/* === This file is part of Calamares - <https://github.com/calamares> ===
2 *
3 *   Copyright 2014, Aurélien Gâteau <agateau@kde.org>
4 *   Copyright 2016, Teo Mrnjavac <teo@kde.org>
5 *
6 *   Calamares is free software: you can redistribute it and/or modify
7 *   it under the terms of the GNU General Public License as published by
8 *   the Free Software Foundation, either version 3 of the License, or
9 *   (at your option) any later version.
10 *
11 *   Calamares is distributed in the hope that it will be useful,
12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *   GNU General Public License for more details.
15 *
16 *   You should have received a copy of the GNU General Public License
17 *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "gui/CreatePartitionDialog.h"
21
22#include "core/ColorUtils.h"
23#include "core/PartitionInfo.h"
24#include "core/PartUtils.h"
25#include "core/KPMHelpers.h"
26#include "gui/PartitionDialogHelpers.h"
27#include "gui/PartitionSizeController.h"
28
29#include "ui_CreatePartitionDialog.h"
30
31#include "utils/Logger.h"
32#include "GlobalStorage.h"
33#include "JobQueue.h"
34
35// KPMcore
36#include <kpmcore/core/device.h>
37#include <kpmcore/core/partition.h>
38#include <kpmcore/fs/filesystem.h>
39#include <kpmcore/fs/filesystemfactory.h>
40#include <kpmcore/fs/luks.h>
41
42// Qt
43#include <QComboBox>
44#include <QDir>
45#include <QListWidgetItem>
46#include <QPushButton>
47#include <QRegularExpression>
48#include <QRegularExpressionValidator>
49#include <QSet>
50
51static QSet< FileSystem::Type > s_unmountableFS(
52{
53    FileSystem::Unformatted,
54    FileSystem::LinuxSwap,
55    FileSystem::Extended,
56    FileSystem::Unknown,
57    FileSystem::Lvm2_PV
58} );
59
60CreatePartitionDialog::CreatePartitionDialog( Device* device, PartitionNode* parentPartition, Partition* partition, const QStringList& usedMountPoints, QWidget* parentWidget )
61    : QDialog( parentWidget )
62    , m_ui( new Ui_CreatePartitionDialog )
63    , m_partitionSizeController( new PartitionSizeController( this ) )
64    , m_device( device )
65    , m_parent( parentPartition )
66    , m_usedMountPoints( usedMountPoints )
67{
68    m_ui->setupUi( this );
69    m_ui->encryptWidget->setText( tr( "En&crypt" ) );
70    m_ui->encryptWidget->hide();
71
72    if (m_device->type() == Device::Type::Disk_Device) {
73        m_ui->lvNameLabel->hide();
74        m_ui->lvNameLineEdit->hide();
75    }
76    if (m_device->type() == Device::Type::LVM_Device) {
77        /* LVM logical volume name can consist of: letters numbers _ . - +
78         * It cannot start with underscore _ and must not be equal to . or .. or any entry in /dev/
79         * QLineEdit accepts QValidator::Intermediate, so we just disable . at the beginning */
80        QRegularExpression re(QStringLiteral(R"(^(?!_|\.)[\w\-.+]+)"));
81        QRegularExpressionValidator *validator = new QRegularExpressionValidator(re, this);
82        m_ui->lvNameLineEdit->setValidator(validator);
83    }
84
85    standardMountPoints( *(m_ui->mountPointComboBox), partition ? PartitionInfo::mountPoint( partition ) : QString() );
86
87    if ( device->partitionTable()->type() == PartitionTable::msdos ||
88         device->partitionTable()->type() == PartitionTable::msdos_sectorbased )
89        initMbrPartitionTypeUi();
90    else
91        initGptPartitionTypeUi();
92
93    // File system
94    FileSystem::Type defaultFsType = FileSystem::typeForName(
95                                         Calamares::JobQueue::instance()->
96                                         globalStorage()->
97                                         value( "defaultFileSystemType" ).toString() );
98    int defaultFsIndex = -1;
99    int fsCounter = 0;
100    QStringList fsNames;
101    for ( auto fs : FileSystemFactory::map() )
102    {
103        if ( fs->supportCreate() != FileSystem::cmdSupportNone &&
104             fs->type() != FileSystem::Extended )
105        {
106            fsNames << fs->name();
107            if ( fs->type() == defaultFsType )
108                defaultFsIndex = fsCounter;
109            fsCounter++;
110        }
111    }
112    m_ui->fsComboBox->addItems( fsNames );
113
114    // Connections
115    connect( m_ui->fsComboBox, SIGNAL( activated( int ) ), SLOT( updateMountPointUi() ) );
116    connect( m_ui->extendedRadioButton, SIGNAL( toggled( bool ) ), SLOT( updateMountPointUi() ) );
117
118    connect( m_ui->mountPointComboBox, &QComboBox::currentTextChanged, this, &CreatePartitionDialog::checkMountPointSelection );
119
120    // Select a default
121    m_ui->fsComboBox->setCurrentIndex( defaultFsIndex );
122    updateMountPointUi();
123
124    setFlagList( *(m_ui->m_listFlags), static_cast< PartitionTable::Flags >( ~PartitionTable::Flags::Int(0) ), partition ? PartitionInfo::flags( partition ) : PartitionTable::Flags() );
125
126    // Checks the initial selection.
127    checkMountPointSelection();
128}
129
130CreatePartitionDialog::~CreatePartitionDialog()
131{}
132
133
134PartitionTable::Flags
135CreatePartitionDialog::newFlags() const
136{
137    return flagsFromList( *(m_ui->m_listFlags) );
138}
139
140void
141CreatePartitionDialog::initMbrPartitionTypeUi()
142{
143    QString fixedPartitionString;
144    bool parentIsPartitionTable = m_parent->isRoot();
145    if ( !parentIsPartitionTable )
146    {
147        m_role = PartitionRole( PartitionRole::Logical );
148        fixedPartitionString = tr( "Logical" );
149    }
150    else if ( m_device->partitionTable()->hasExtended() )
151    {
152        m_role = PartitionRole( PartitionRole::Primary );
153        fixedPartitionString = tr( "Primary" );
154    }
155
156    if ( fixedPartitionString.isEmpty() )
157        m_ui->fixedPartitionLabel->hide();
158    else
159    {
160        m_ui->fixedPartitionLabel->setText( fixedPartitionString );
161        m_ui->primaryRadioButton->hide();
162        m_ui->extendedRadioButton->hide();
163    }
164}
165
166void
167CreatePartitionDialog::initGptPartitionTypeUi()
168{
169    m_role = PartitionRole( PartitionRole::Primary );
170    m_ui->fixedPartitionLabel->setText( tr( "GPT" ) );
171    m_ui->primaryRadioButton->hide();
172    m_ui->extendedRadioButton->hide();
173}
174
175Partition*
176CreatePartitionDialog::createPartition()
177{
178    if ( m_role.roles() == PartitionRole::None )
179    {
180        m_role = PartitionRole(
181                     m_ui->extendedRadioButton->isChecked()
182                     ? PartitionRole::Extended
183                     : PartitionRole::Primary
184                 );
185    }
186
187    qint64 first = m_partitionSizeController->firstSector();
188    qint64 last = m_partitionSizeController->lastSector();
189
190    FileSystem::Type fsType = m_role.has( PartitionRole::Extended )
191                              ? FileSystem::Extended
192                              : FileSystem::typeForName( m_ui->fsComboBox->currentText() );
193
194    Partition* partition = nullptr;
195    QString luksPassphrase = m_ui->encryptWidget->passphrase();
196    if ( m_ui->encryptWidget->state() == EncryptWidget::EncryptionConfirmed &&
197         !luksPassphrase.isEmpty() )
198    {
199        partition = KPMHelpers::createNewEncryptedPartition(
200            m_parent,
201            *m_device,
202            m_role,
203            fsType, first, last, luksPassphrase, newFlags()
204        );
205    }
206    else
207    {
208        partition = KPMHelpers::createNewPartition(
209            m_parent,
210            *m_device,
211            m_role,
212            fsType, first, last, newFlags()
213        );
214    }
215
216    if (m_device->type() == Device::Type::LVM_Device) {
217        partition->setPartitionPath(m_device->deviceNode() + QStringLiteral("/") + m_ui->lvNameLineEdit->text().trimmed());
218    }
219
220    PartitionInfo::setMountPoint( partition, selectedMountPoint( m_ui->mountPointComboBox ) );
221    PartitionInfo::setFormat( partition, true );
222
223    return partition;
224}
225
226void
227CreatePartitionDialog::updateMountPointUi()
228{
229    bool enabled = m_ui->primaryRadioButton->isChecked();
230    if ( enabled )
231    {
232        FileSystem::Type type = FileSystem::typeForName( m_ui->fsComboBox->currentText() );
233        enabled = !s_unmountableFS.contains( type );
234
235        if ( FileSystemFactory::map()[FileSystem::Type::Luks]->supportCreate() &&
236             FS::luks::canEncryptType( type ) &&
237             !m_role.has( PartitionRole::Extended ) )
238        {
239            m_ui->encryptWidget->show();
240            m_ui->encryptWidget->reset();
241        }
242        else
243        {
244            m_ui->encryptWidget->reset();
245            m_ui->encryptWidget->hide();
246        }
247    }
248    m_ui->mountPointLabel->setEnabled( enabled );
249    m_ui->mountPointComboBox->setEnabled( enabled );
250    if ( !enabled )
251        m_ui->mountPointComboBox->setCurrentText( QString() );
252}
253
254void
255CreatePartitionDialog::checkMountPointSelection()
256{
257    if ( m_usedMountPoints.contains( selectedMountPoint( m_ui->mountPointComboBox ) ) )
258    {
259        m_ui->labelMountPoint->setText( tr( "Mountpoint already in use. Please select another one." ) );
260        m_ui->buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
261    }
262    else
263    {
264        m_ui->labelMountPoint->setText( QString() );
265        m_ui->buttonBox->button( QDialogButtonBox::Ok )->setEnabled( true );
266    }
267}
268
269void
270CreatePartitionDialog::initPartResizerWidget( Partition* partition )
271{
272    QColor color = KPMHelpers::isPartitionFreeSpace( partition )
273                   ? ColorUtils::colorForPartitionInFreeSpace( partition )
274                   : ColorUtils::colorForPartition( partition );
275    m_partitionSizeController->init( m_device, partition, color );
276    m_partitionSizeController->setPartResizerWidget( m_ui->partResizerWidget );
277    m_partitionSizeController->setSpinBox( m_ui->sizeSpinBox );
278}
279
280void
281CreatePartitionDialog::initFromFreeSpace( Partition* freeSpacePartition )
282{
283    initPartResizerWidget( freeSpacePartition );
284}
285
286void
287CreatePartitionDialog::initFromPartitionToCreate( Partition* partition )
288{
289    Q_ASSERT( partition );
290
291    bool isExtended = partition->roles().has( PartitionRole::Extended );
292    Q_ASSERT( !isExtended );
293    if ( isExtended )
294    {
295        cDebug() << "Editing extended partitions is not supported for now";
296        return;
297    }
298
299    initPartResizerWidget( partition );
300
301    // File System
302    FileSystem::Type fsType = partition->fileSystem().type();
303    m_ui->fsComboBox->setCurrentText( FileSystem::nameForType( fsType ) );
304
305    // Mount point
306    setSelectedMountPoint( m_ui->mountPointComboBox, PartitionInfo::mountPoint( partition ) );
307
308    updateMountPointUi();
309}
Note: See TracBrowser for help on using the repository browser.