2014-06-27 17:25:39 +02:00
|
|
|
/* === This file is part of Calamares - <http://github.com/calamares> ===
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
#include <PartitionModel.h>
|
|
|
|
|
2014-07-04 17:33:26 +02:00
|
|
|
#include <PartitionInfo.h>
|
2014-06-30 15:03:29 +02:00
|
|
|
#include <PMUtils.h>
|
2014-07-02 17:55:53 +02:00
|
|
|
#include <utils/Logger.h>
|
2014-06-30 15:03:29 +02:00
|
|
|
|
2014-06-27 17:25:39 +02:00
|
|
|
// CalaPM
|
|
|
|
#include <core/device.h>
|
|
|
|
#include <core/partition.h>
|
|
|
|
#include <core/partitiontable.h>
|
|
|
|
#include <fs/filesystem.h>
|
|
|
|
|
2014-07-02 17:25:59 +02:00
|
|
|
// KF5
|
|
|
|
#include <KFormat>
|
|
|
|
|
2014-06-27 17:25:39 +02:00
|
|
|
PartitionModel::PartitionModel( QObject* parent )
|
|
|
|
: QAbstractListModel( parent )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-07-16 16:20:58 +02:00
|
|
|
PartitionModel::init( Device* device )
|
2014-06-27 17:25:39 +02:00
|
|
|
{
|
|
|
|
m_device = device;
|
2014-06-30 16:17:28 +02:00
|
|
|
reload();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PartitionModel::reload()
|
|
|
|
{
|
|
|
|
beginResetModel();
|
2014-06-27 17:25:39 +02:00
|
|
|
m_partitionList.clear();
|
2014-06-30 16:17:28 +02:00
|
|
|
if ( m_device )
|
2014-06-27 17:25:39 +02:00
|
|
|
fillPartitionList( m_device->partitionTable() );
|
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
2014-07-02 17:55:53 +02:00
|
|
|
int
|
|
|
|
PartitionModel::columnCount( const QModelIndex& parent ) const
|
|
|
|
{
|
|
|
|
return LastColumn;
|
|
|
|
}
|
|
|
|
|
2014-06-27 17:25:39 +02:00
|
|
|
int
|
|
|
|
PartitionModel::rowCount( const QModelIndex& parent ) const
|
|
|
|
{
|
|
|
|
return parent.isValid() ? 0 : m_partitionList.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant
|
|
|
|
PartitionModel::data( const QModelIndex& index, int role ) const
|
|
|
|
{
|
|
|
|
int row = index.row();
|
|
|
|
if ( row < 0 || row >= m_partitionList.count() )
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
Partition* partition = m_partitionList.at( row );
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2014-07-02 17:55:53 +02:00
|
|
|
// FIXME: Turn model into a tree model, will make implementing the
|
|
|
|
// preview easier
|
|
|
|
QString prefix = partition->roles().has( PartitionRole::Logical )
|
|
|
|
? QStringLiteral( " " ) : QStringLiteral();
|
|
|
|
if ( PMUtils::isPartitionFreeSpace( partition ) )
|
|
|
|
return prefix + tr( "Free Space" );
|
|
|
|
else
|
|
|
|
{
|
2014-07-15 17:33:16 +02:00
|
|
|
return prefix + ( PMUtils::isPartitionNew( partition )
|
2014-07-02 17:55:53 +02:00
|
|
|
? tr( "New partition" )
|
|
|
|
: partition->partitionPath() );
|
|
|
|
}
|
2014-07-01 16:29:26 +02:00
|
|
|
}
|
2014-07-02 17:55:53 +02:00
|
|
|
if ( col == FileSystemColumn )
|
|
|
|
return partition->fileSystem().name();
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
qint64 size = ( partition->lastSector() - partition->firstSector() + 1 ) * m_device->logicalSectorSize();
|
|
|
|
return KFormat().formatByteSize( size );
|
|
|
|
}
|
|
|
|
cDebug() << "Unknown column" << col;
|
|
|
|
return QVariant();
|
2014-07-01 16:29:26 +02:00
|
|
|
}
|
2014-06-27 17:25:39 +02:00
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PartitionModel::fillPartitionList( PartitionNode* parent )
|
|
|
|
{
|
2014-07-10 18:27:37 +02:00
|
|
|
if ( !parent )
|
|
|
|
return;
|
2014-06-27 17:25:39 +02:00
|
|
|
for ( auto partition : parent->children() )
|
|
|
|
{
|
|
|
|
m_partitionList << partition;
|
|
|
|
fillPartitionList( partition );
|
|
|
|
}
|
|
|
|
}
|
2014-06-30 15:03:45 +02:00
|
|
|
|
|
|
|
Partition*
|
|
|
|
PartitionModel::partitionForIndex( const QModelIndex& index ) const
|
|
|
|
{
|
|
|
|
int row = index.row();
|
|
|
|
if ( row < 0 || row >= m_partitionList.count() )
|
|
|
|
return nullptr;
|
|
|
|
return m_partitionList.at( row );
|
|
|
|
}
|