Select the partition number if `--device` is passed with it

such as `sda1` or `hda2` will now select the specific partition
This commit is contained in:
Tomaz Canabrava 2022-01-07 17:42:55 +00:00 committed by Andrius Štikonas
parent afcc0c6c47
commit a8da9ef6c1
5 changed files with 42 additions and 3 deletions

View File

@ -1432,3 +1432,12 @@ void MainWindow::setCurrentDeviceByName(const QString& name)
xi18nc("@title:window", "Error While Importing Partition Table"));
}
}
void MainWindow::setCurrentPartitionByName(const QString& partitionName)
{
if (!pmWidget().setCurrentPartitionByName(partitionName)) {
KMessageBox::error(this,
xi18nc("@info device should be inside of /dev", "Unrecognized partition \"%1\" ", partitionName),
xi18nc("@title:window", "Error opening partition"));
}
}

View File

@ -48,6 +48,8 @@ public:
// for instance `/dev/sda`
void setCurrentDeviceByName(const QString& name);
void setCurrentPartitionByName(const QString& partitionNumber);
Q_SIGNALS:
void settingsChanged();
void scanFinished();

View File

@ -282,6 +282,25 @@ void PartitionManagerWidget::on_m_TreePartitions_currentItemChanged(QTreeWidgetI
partTableWidget().setActiveWidget(nullptr);
}
bool PartitionManagerWidget::setCurrentPartitionByName(const QString& name)
{
auto rootNode = treePartitions().invisibleRootItem();
for (int i = 0; i < rootNode->childCount(); i++) {
auto driveNode = rootNode->child(i);
for (int e = 0; e < driveNode->childCount(); e++) {
auto partitionNode = driveNode->child(e);
const QString text = partitionNode->data(0, Qt::DisplayRole).toString();
if (text.endsWith(name)) {
partitionNode->setSelected(true);
treePartitions().setCurrentItem(partitionNode);
return true;
}
}
}
return false;
}
void PartitionManagerWidget::on_m_TreePartitions_itemDoubleClicked(QTreeWidgetItem* item, int)
{
if (item == treePartitions().topLevelItem(0)) {

View File

@ -94,6 +94,9 @@ public:
void updatePartitions();
// here we expect the full "sda1" or "nvme1n1p1"
bool setCurrentPartitionByName(const QString& partitionName);
protected:
OperationStack& operationStack() {
return *m_OperationStack;
@ -125,6 +128,7 @@ protected:
return *m_TreePartitions;
}
void onHeaderContextMenu(const QPoint& p);
protected Q_SLOTS:

View File

@ -113,13 +113,18 @@ int Q_DECL_IMPORT main(int argc, char* argv[])
if (!loadBackend())
return 0;
// device is the selected device minus the partition number.
// we need all of them to select properly the things on screen.
const QString selectedDevice = parser.value(deviceOption);
auto [device, partitionNr] = parseDevice(selectedDevice);
MainWindow* mainWindow = new MainWindow();
QObject::connect(mainWindow, &MainWindow::scanFinished, mainWindow, [mainWindow, selectedDevice] {
if (selectedDevice.length()) {
mainWindow->setCurrentDeviceByName(selectedDevice);
QObject::connect(mainWindow, &MainWindow::scanFinished, mainWindow, [mainWindow, device, selectedDevice, partitionNr] {
if (device.length()) {
mainWindow->setCurrentDeviceByName(device);
if (partitionNr.length()) {
mainWindow->setCurrentPartitionByName(selectedDevice);
}
}
});