2017-12-20 14:39:09 +01:00
|
|
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
2014-06-27 17:25:39 +02:00
|
|
|
*
|
|
|
|
* Copyright 2014, Aurélien Gâteau <agateau@kde.org>
|
|
|
|
*
|
|
|
|
* Calamares 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.
|
|
|
|
*
|
|
|
|
* Calamares 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 Calamares. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2015-07-02 13:49:21 +02:00
|
|
|
#include "core/PartitionModel.h"
|
|
|
|
|
|
|
|
#include "core/ColorUtils.h"
|
|
|
|
#include "core/PartitionInfo.h"
|
2015-09-18 15:41:07 +02:00
|
|
|
#include "core/KPMHelpers.h"
|
2015-07-02 13:49:21 +02:00
|
|
|
#include "utils/Logger.h"
|
2014-06-30 15:03:29 +02:00
|
|
|
|
2014-06-27 17:25:39 +02:00
|
|
|
// CalaPM
|
2015-07-02 13:49:21 +02:00
|
|
|
#include <kpmcore/core/device.h>
|
|
|
|
#include <kpmcore/core/partition.h>
|
|
|
|
#include <kpmcore/core/partitiontable.h>
|
|
|
|
#include <kpmcore/fs/filesystem.h>
|
2014-06-27 17:25:39 +02:00
|
|
|
|
2014-07-02 17:25:59 +02:00
|
|
|
// KF5
|
|
|
|
#include <KFormat>
|
|
|
|
|
2014-07-29 13:36:48 +02:00
|
|
|
// Qt
|
|
|
|
#include <QColor>
|
|
|
|
|
2014-07-29 10:57:10 +02:00
|
|
|
//- ResetHelper --------------------------------------------
|
|
|
|
PartitionModel::ResetHelper::ResetHelper( PartitionModel* model )
|
2014-08-05 09:54:30 +02:00
|
|
|
: m_model( model )
|
2014-06-27 17:25:39 +02:00
|
|
|
{
|
2014-07-29 10:57:10 +02:00
|
|
|
m_model->beginResetModel();
|
2014-06-27 17:25:39 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 10:57:10 +02:00
|
|
|
PartitionModel::ResetHelper::~ResetHelper()
|
|
|
|
{
|
|
|
|
m_model->endResetModel();
|
|
|
|
}
|
|
|
|
|
|
|
|
//- PartitionModel -----------------------------------------
|
|
|
|
PartitionModel::PartitionModel( QObject* parent )
|
|
|
|
: QAbstractItemModel( parent )
|
2015-06-14 00:48:51 +02:00
|
|
|
, m_device( nullptr )
|
2014-06-27 17:25:39 +02:00
|
|
|
{
|
2014-06-30 16:17:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-12-15 14:00:34 +01:00
|
|
|
PartitionModel::init( Device* device , const OsproberEntryList& osproberEntries )
|
2014-06-30 16:17:28 +02:00
|
|
|
{
|
|
|
|
beginResetModel();
|
2014-07-29 10:57:10 +02:00
|
|
|
m_device = device;
|
2015-12-15 14:00:34 +01:00
|
|
|
m_osproberEntries = osproberEntries;
|
2014-06-27 17:25:39 +02:00
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
2014-07-02 17:55:53 +02:00
|
|
|
int
|
|
|
|
PartitionModel::columnCount( const QModelIndex& parent ) const
|
|
|
|
{
|
2014-07-29 10:57:10 +02:00
|
|
|
return ColumnCount;
|
2014-07-02 17:55:53 +02:00
|
|
|
}
|
|
|
|
|
2014-06-27 17:25:39 +02:00
|
|
|
int
|
|
|
|
PartitionModel::rowCount( const QModelIndex& parent ) const
|
|
|
|
{
|
2014-07-29 10:57:10 +02:00
|
|
|
Partition* parentPartition = partitionForIndex( parent );
|
|
|
|
if ( parentPartition )
|
|
|
|
return parentPartition->children().count();
|
|
|
|
PartitionTable* table = m_device->partitionTable();
|
|
|
|
return table ? table->children().count() : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex
|
|
|
|
PartitionModel::index( int row, int column, const QModelIndex& parent ) const
|
|
|
|
{
|
|
|
|
PartitionNode* parentPartition = parent.isValid()
|
2014-08-05 09:54:30 +02:00
|
|
|
? static_cast< PartitionNode* >( partitionForIndex( parent ) )
|
|
|
|
: static_cast< PartitionNode* >( m_device->partitionTable() );
|
2014-07-29 10:57:10 +02:00
|
|
|
if ( !parentPartition )
|
|
|
|
return QModelIndex();
|
|
|
|
auto lst = parentPartition->children();
|
|
|
|
if ( row < 0 || row >= lst.count() )
|
|
|
|
return QModelIndex();
|
|
|
|
if ( column < 0 || column >= ColumnCount )
|
|
|
|
return QModelIndex();
|
|
|
|
Partition* partition = parentPartition->children().at( row );
|
|
|
|
return createIndex( row, column, partition );
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex
|
|
|
|
PartitionModel::parent( const QModelIndex& child ) const
|
|
|
|
{
|
|
|
|
if ( !child.isValid() )
|
|
|
|
return QModelIndex();
|
|
|
|
Partition* partition = partitionForIndex( child );
|
2015-06-21 01:21:06 +02:00
|
|
|
if ( !partition )
|
|
|
|
return QModelIndex();
|
2014-07-29 10:57:10 +02:00
|
|
|
PartitionNode* parentNode = partition->parent();
|
|
|
|
if ( parentNode == m_device->partitionTable() )
|
|
|
|
return QModelIndex();
|
|
|
|
|
|
|
|
int row = 0;
|
|
|
|
for ( auto p : m_device->partitionTable()->children() )
|
|
|
|
{
|
|
|
|
if ( parentNode == p )
|
|
|
|
return createIndex( row, 0, parentNode );
|
|
|
|
++row;
|
|
|
|
}
|
2018-03-28 15:31:45 +02:00
|
|
|
cWarning() << "No parent found!";
|
2014-07-29 10:57:10 +02:00
|
|
|
return QModelIndex();
|
2014-06-27 17:25:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant
|
|
|
|
PartitionModel::data( const QModelIndex& index, int role ) const
|
|
|
|
{
|
2014-07-29 10:57:10 +02:00
|
|
|
Partition* partition = partitionForIndex( index );
|
|
|
|
if ( !partition )
|
2014-06-27 17:25:39 +02:00
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
switch ( role )
|
|
|
|
{
|
|
|
|
case Qt::DisplayRole:
|
2014-07-01 16:29:26 +02:00
|
|
|
{
|
2014-07-02 17:55:53 +02:00
|
|
|
int col = index.column();
|
|
|
|
if ( col == NameColumn )
|
2014-06-27 17:25:39 +02:00
|
|
|
{
|
2015-09-18 15:41:07 +02:00
|
|
|
if ( KPMHelpers::isPartitionFreeSpace( partition ) )
|
2014-07-29 10:57:10 +02:00
|
|
|
return tr( "Free Space" );
|
2014-07-02 17:55:53 +02:00
|
|
|
else
|
|
|
|
{
|
2015-09-18 15:41:07 +02:00
|
|
|
return KPMHelpers::isPartitionNew( partition )
|
2014-07-29 10:57:10 +02:00
|
|
|
? tr( "New partition" )
|
|
|
|
: partition->partitionPath();
|
2014-07-02 17:55:53 +02:00
|
|
|
}
|
2014-07-01 16:29:26 +02:00
|
|
|
}
|
2014-07-02 17:55:53 +02:00
|
|
|
if ( col == FileSystemColumn )
|
2015-12-16 15:47:11 +01:00
|
|
|
return KPMHelpers::prettyNameForFileSystemType( partition->fileSystem().type() );
|
2014-07-02 17:55:53 +02:00
|
|
|
if ( col == MountPointColumn )
|
2014-07-16 15:50:41 +02:00
|
|
|
return PartitionInfo::mountPoint( partition );
|
2014-07-02 17:55:53 +02:00
|
|
|
if ( col == SizeColumn )
|
|
|
|
{
|
2016-08-31 05:30:45 +02:00
|
|
|
qint64 size = ( partition->lastSector() - partition->firstSector() + 1 ) * m_device->logicalSize();
|
2014-07-02 17:55:53 +02:00
|
|
|
return KFormat().formatByteSize( size );
|
|
|
|
}
|
|
|
|
cDebug() << "Unknown column" << col;
|
|
|
|
return QVariant();
|
2014-07-01 16:29:26 +02:00
|
|
|
}
|
2014-07-29 13:36:48 +02:00
|
|
|
case Qt::DecorationRole:
|
|
|
|
if ( index.column() == NameColumn )
|
2014-08-07 17:24:39 +02:00
|
|
|
return ColorUtils::colorForPartition( partition );
|
2014-07-29 13:36:48 +02:00
|
|
|
else
|
|
|
|
return QVariant();
|
2015-12-28 09:08:32 +01:00
|
|
|
case Qt::ToolTipRole:
|
|
|
|
{
|
|
|
|
int col = index.column();
|
|
|
|
QString name;
|
|
|
|
if ( col == NameColumn )
|
|
|
|
{
|
|
|
|
if ( KPMHelpers::isPartitionFreeSpace( partition ) )
|
|
|
|
name = tr( "Free Space" );
|
|
|
|
else
|
|
|
|
{
|
|
|
|
name = KPMHelpers::isPartitionNew( partition )
|
|
|
|
? tr( "New partition" )
|
|
|
|
: partition->partitionPath();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
QString prettyFileSystem = KPMHelpers::prettyNameForFileSystemType( partition->fileSystem().type() );
|
2016-08-31 05:30:45 +02:00
|
|
|
qint64 size = ( partition->lastSector() - partition->firstSector() + 1 ) * m_device->logicalSize();
|
2015-12-28 09:08:32 +01:00
|
|
|
QString prettySize = KFormat().formatByteSize( size );
|
|
|
|
return QVariant(name + " " + prettyFileSystem + " " + prettySize);
|
|
|
|
}
|
2014-07-29 13:37:12 +02:00
|
|
|
case SizeRole:
|
2016-08-31 05:30:45 +02:00
|
|
|
return ( partition->lastSector() - partition->firstSector() + 1 ) * m_device->logicalSize();
|
2014-07-29 15:56:00 +02:00
|
|
|
case IsFreeSpaceRole:
|
2015-09-18 15:41:07 +02:00
|
|
|
return KPMHelpers::isPartitionFreeSpace( partition );
|
2015-12-15 14:00:34 +01:00
|
|
|
|
2015-12-15 16:07:53 +01:00
|
|
|
case IsPartitionNewRole:
|
|
|
|
return KPMHelpers::isPartitionNew( partition );
|
|
|
|
|
2015-12-15 15:41:42 +01:00
|
|
|
case FileSystemLabelRole:
|
|
|
|
if ( partition->fileSystem().supportGetLabel() != FileSystem::cmdSupportNone &&
|
|
|
|
!partition->fileSystem().label().isEmpty() )
|
|
|
|
return partition->fileSystem().label();
|
|
|
|
return QVariant();
|
|
|
|
|
2015-12-16 15:47:11 +01:00
|
|
|
case FileSystemTypeRole:
|
|
|
|
return partition->fileSystem().type();
|
|
|
|
|
2015-12-17 16:13:42 +01:00
|
|
|
case PartitionPathRole:
|
2015-12-18 15:29:31 +01:00
|
|
|
return partition->partitionPath();
|
2015-12-17 16:13:42 +01:00
|
|
|
|
2015-12-17 15:25:39 +01:00
|
|
|
case PartitionPtrRole:
|
|
|
|
return qVariantFromValue( (void*)partition );
|
|
|
|
|
2015-12-15 14:00:34 +01:00
|
|
|
// Osprober roles:
|
|
|
|
case OsproberNameRole:
|
|
|
|
foreach ( const OsproberEntry& osproberEntry, m_osproberEntries )
|
2016-07-28 17:42:45 +02:00
|
|
|
if ( partition->fileSystem().supportGetUUID() != FileSystem::cmdSupportNone &&
|
|
|
|
!partition->fileSystem().uuid().isEmpty() &&
|
|
|
|
osproberEntry.uuid == partition->fileSystem().uuid() )
|
2015-12-15 14:00:34 +01:00
|
|
|
return osproberEntry.prettyName;
|
|
|
|
return QVariant();
|
|
|
|
case OsproberPathRole:
|
|
|
|
foreach ( const OsproberEntry& osproberEntry, m_osproberEntries )
|
2016-07-28 17:42:45 +02:00
|
|
|
if ( partition->fileSystem().supportGetUUID() != FileSystem::cmdSupportNone &&
|
|
|
|
!partition->fileSystem().uuid().isEmpty() &&
|
|
|
|
osproberEntry.uuid == partition->fileSystem().uuid() )
|
2015-12-15 14:00:34 +01:00
|
|
|
return osproberEntry.path;
|
|
|
|
return QVariant();
|
|
|
|
case OsproberCanBeResizedRole:
|
|
|
|
foreach ( const OsproberEntry& osproberEntry, m_osproberEntries )
|
2016-07-28 17:42:45 +02:00
|
|
|
if ( partition->fileSystem().supportGetUUID() != FileSystem::cmdSupportNone &&
|
|
|
|
!partition->fileSystem().uuid().isEmpty() &&
|
|
|
|
osproberEntry.uuid == partition->fileSystem().uuid() )
|
2015-12-15 14:00:34 +01:00
|
|
|
return osproberEntry.canBeResized;
|
|
|
|
return QVariant();
|
|
|
|
case OsproberRawLineRole:
|
|
|
|
foreach ( const OsproberEntry& osproberEntry, m_osproberEntries )
|
2016-07-28 17:42:45 +02:00
|
|
|
if ( partition->fileSystem().supportGetUUID() != FileSystem::cmdSupportNone &&
|
|
|
|
!partition->fileSystem().uuid().isEmpty() &&
|
|
|
|
osproberEntry.uuid == partition->fileSystem().uuid() )
|
2015-12-15 14:00:34 +01:00
|
|
|
return osproberEntry.line;
|
|
|
|
return QVariant();
|
2016-07-14 17:40:58 +02:00
|
|
|
case OsproberHomePartitionPathRole:
|
|
|
|
foreach ( const OsproberEntry& osproberEntry, m_osproberEntries )
|
2016-07-28 17:42:45 +02:00
|
|
|
if ( partition->fileSystem().supportGetUUID() != FileSystem::cmdSupportNone &&
|
|
|
|
!partition->fileSystem().uuid().isEmpty() &&
|
|
|
|
osproberEntry.uuid == partition->fileSystem().uuid() )
|
2016-07-14 17:40:58 +02:00
|
|
|
return osproberEntry.homePath;
|
|
|
|
return QVariant();
|
2015-12-15 14:00:34 +01:00
|
|
|
// end Osprober roles.
|
|
|
|
|
2014-06-27 17:25:39 +02:00
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-25 13:13:04 +02:00
|
|
|
QVariant
|
|
|
|
PartitionModel::headerData( int section, Qt::Orientation orientation, int role ) const
|
|
|
|
{
|
|
|
|
if ( role != Qt::DisplayRole )
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
switch ( section )
|
|
|
|
{
|
|
|
|
case NameColumn:
|
|
|
|
return tr( "Name" );
|
|
|
|
case FileSystemColumn:
|
|
|
|
return tr( "File System" );
|
|
|
|
case MountPointColumn:
|
2014-07-28 15:00:30 +02:00
|
|
|
return tr( "Mount Point" );
|
2014-07-25 13:13:04 +02:00
|
|
|
case SizeColumn:
|
2014-07-28 15:00:30 +02:00
|
|
|
return tr( "Size" );
|
2014-07-25 13:13:04 +02:00
|
|
|
default:
|
|
|
|
cDebug() << "Unknown column" << section;
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-30 15:03:45 +02:00
|
|
|
Partition*
|
|
|
|
PartitionModel::partitionForIndex( const QModelIndex& index ) const
|
|
|
|
{
|
2014-07-29 10:57:10 +02:00
|
|
|
if ( !index.isValid() )
|
2014-06-30 15:03:45 +02:00
|
|
|
return nullptr;
|
2014-07-29 10:57:10 +02:00
|
|
|
return reinterpret_cast< Partition* >( index.internalPointer() );
|
2014-06-30 15:03:45 +02:00
|
|
|
}
|
2015-12-24 13:37:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
PartitionModel::update()
|
|
|
|
{
|
|
|
|
emit dataChanged( index( 0, 0 ), index( rowCount() - 1, columnCount() - 1 ) );
|
|
|
|
}
|