[partition] Look up bootloader by name, method

The bootloader model knows about both rows and
devices, so we can look up both at once. The
existing implementation as a non-member was rather
sketchy and wasn't used except as support for
restoreSelectedBootLoader().
This commit is contained in:
Adriaan de Groot 2021-05-25 13:05:45 +02:00
parent dabd895755
commit d0276fd25f
2 changed files with 40 additions and 23 deletions

View File

@ -18,6 +18,7 @@
// KPMcore // KPMcore
#include <kpmcore/core/device.h> #include <kpmcore/core/device.h>
#include <kpmcore/core/partition.h>
#include <QComboBox> #include <QComboBox>
@ -148,28 +149,39 @@ BootLoaderModel::data( const QModelIndex& index, int role ) const
return QStandardItemModel::data( index, role ); return QStandardItemModel::data( index, role );
} }
std::pair< int, Device* >
BootLoaderModel::findBootLoader( const QString& path ) const
{
int r = 0;
for ( Device* d : m_devices )
{
if ( d && d->deviceNode() == path )
{
return std::make_pair( r, d );
}
r++;
}
Partition* partition = KPMHelpers::findPartitionByMountPoint( m_devices, path );
if ( partition )
{
const QString partition_device_path = partition->deviceNode();
r = 0;
for ( Device* d : m_devices )
{
if ( d && d->deviceNode() == partition_device_path )
{
return std::make_pair( r, d );
}
r++;
}
}
return std::make_pair( -1, nullptr );
}
namespace Calamares namespace Calamares
{ {
int
findBootloader( const QAbstractItemModel* model, const QString& path )
{
for ( int i = 0; i < model->rowCount(); ++i )
{
const auto index = model->index( i, 0, QModelIndex() );
if ( !index.isValid() )
{
continue;
}
QVariant var = model->data( index, BootLoaderModel::BootLoaderPathRole );
if ( var.isValid() && var.toString() == path )
{
return i;
}
}
return -1;
}
void void
restoreSelectedBootLoader( QComboBox& combo, const QString& path ) restoreSelectedBootLoader( QComboBox& combo, const QString& path )
{ {
@ -180,12 +192,16 @@ restoreSelectedBootLoader( QComboBox& combo, const QString& path )
return; return;
} }
int r = -1;
if ( path.isEmpty() ) if ( path.isEmpty() )
{ {
cDebug() << "No path to restore, choosing default";
combo.setCurrentIndex( 0 ); combo.setCurrentIndex( 0 );
return;
} }
else if ( ( r = findBootloader( model, path ) ) >= 0 )
const BootLoaderModel* bmodel = qobject_cast< const BootLoaderModel* >( model );
int r = bmodel ? bmodel->findBootLoader( path ).first : -1;
if ( r >= 0 )
{ {
combo.setCurrentIndex( r ); combo.setCurrentIndex( r );
} }

View File

@ -47,6 +47,14 @@ public:
QVariant data( const QModelIndex& index, int role = Qt::DisplayRole ) const override; QVariant data( const QModelIndex& index, int role = Qt::DisplayRole ) const override;
/** @brief Looks up a boot-loader by device-name @p path (e.g. /dev/sda)
*
* Returns a row number (index) in the model and a Device*: if there **is** a
* device for the given @p path, index will be in range of the model and
* Device* non-null. Returns (-1, nullptr) otherwise.
*/
std::pair< int, Device* > findBootLoader( const QString& path ) const;
private: private:
DeviceList m_devices; DeviceList m_devices;
mutable QMutex m_lock; mutable QMutex m_lock;
@ -57,13 +65,6 @@ private:
namespace Calamares namespace Calamares
{ {
/** @brief Returns the row number of boot-loader @p path (e.g. /dev/sda)
*
* Assuming the @p model is a BootLoaderModel, will return a row number
* in the model. Returns -1 otherwise.
*/
int findBootloader( const QAbstractItemModel* model, const QString& path );
/** @brief Tries to set @p path as selected item in @p combo /** @brief Tries to set @p path as selected item in @p combo
* *
* Matches a boot-loader install path (e.g. /dev/sda) with a model * Matches a boot-loader install path (e.g. /dev/sda) with a model