source: calamares/trunk/fuentes/src/modules/partition/jobs/ResizePartitionJob.cpp @ 7538

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

sync with github

File size: 3.8 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 2015, Teo Mrnjavac <teo@kde.org>
5 *   Copyright 2017, Andrius Štikonas <andrius@stikonas.eu>
6 *
7 *   Calamares is free software: you can redistribute it and/or modify
8 *   it under the terms of the GNU General Public License as published by
9 *   the Free Software Foundation, either version 3 of the License, or
10 *   (at your option) any later version.
11 *
12 *   Calamares is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include "jobs/ResizePartitionJob.h"
22
23#include "utils/Units.h"
24
25// KPMcore
26#include <core/device.h>
27#include <ops/resizeoperation.h>
28#include <util/report.h>
29
30using CalamaresUtils::BytesToMiB;
31
32//- ResizePartitionJob ---------------------------------------------------------
33ResizePartitionJob::ResizePartitionJob( Device* device, Partition* partition, qint64 firstSector, qint64 lastSector )
34    : PartitionJob( partition )
35    , m_device( device )
36    , m_oldFirstSector( partition->firstSector() ) // Keep a copy of old sectors because they will be overwritten in updatePreview()
37    , m_oldLastSector( partition->lastSector() )
38    , m_newFirstSector( firstSector )
39    , m_newLastSector( lastSector )
40{
41}
42
43QString
44ResizePartitionJob::prettyName() const
45{
46    // FIXME: Copy PM ResizeOperation code which generates a description of the
47    // operation
48    return tr( "Resize partition %1." ).arg( partition()->partitionPath() );
49}
50
51
52QString
53ResizePartitionJob::prettyDescription() const
54{
55    return tr( "Resize <strong>%2MB</strong> partition <strong>%1</strong> to "
56               "<strong>%3MB</strong>." )
57            .arg( partition()->partitionPath() )
58            .arg( ( BytesToMiB( m_oldLastSector - m_oldFirstSector + 1 ) * partition()->sectorSize() ) )
59            .arg( ( BytesToMiB( m_newLastSector - m_newFirstSector + 1 ) * partition()->sectorSize() ) );
60}
61
62
63QString
64ResizePartitionJob::prettyStatusMessage() const
65{
66    return tr( "Resizing %2MB partition %1 to "
67               "%3MB." )
68            .arg( partition()->partitionPath() )
69            .arg( ( BytesToMiB( m_oldLastSector - m_oldFirstSector + 1 ) * partition()->sectorSize() ) )
70            .arg( ( BytesToMiB( m_newLastSector - m_newFirstSector + 1 ) * partition()->sectorSize() ) );
71}
72
73
74Calamares::JobResult
75ResizePartitionJob::exec()
76{
77    Report report (nullptr);
78    // Restore partition sectors that were modified for preview
79    m_partition->setFirstSector( m_oldFirstSector );
80    m_partition->setLastSector( m_oldLastSector );
81    ResizeOperation op(*m_device, *m_partition, m_newFirstSector, m_newLastSector);
82    op.setStatus(Operation::StatusRunning);
83    connect(&op, &Operation::progress, this, &ResizePartitionJob::iprogress );
84
85    QString errorMessage = tr( "The installer failed to resize partition %1 on disk '%2'." )
86                       .arg( m_partition->partitionPath() )
87                       .arg( m_device->name() );
88    if (op.execute(report))
89        return Calamares::JobResult::ok();
90
91    return Calamares::JobResult::error(errorMessage, report.toText());
92}
93
94void
95ResizePartitionJob::updatePreview()
96{
97    m_device->partitionTable()->removeUnallocated();
98    m_partition->parent()->remove( m_partition );
99    m_partition->setFirstSector( m_newFirstSector );
100    m_partition->setLastSector( m_newLastSector );
101    m_partition->parent()->insert( m_partition );
102    m_device->partitionTable()->updateUnallocated( *m_device );
103}
104
105
106Device*
107ResizePartitionJob::device() const
108{
109    return m_device;
110}
Note: See TracBrowser for help on using the repository browser.