Merge pull request #5909 from lemondrops/manager-4

Manager: Add machine deletion
This commit is contained in:
Miran Grča
2025-08-07 23:19:34 +02:00
committed by GitHub
5 changed files with 60 additions and 3 deletions

View File

@@ -107,6 +107,13 @@ VMManagerMain::VMManagerMain(QWidget *parent) :
});
killIcon.setEnabled(selected_sysconfig->process->state() == QProcess::Running);
QAction deleteAction(tr("&Delete"));
contextMenu.addAction(&deleteAction);
connect(&deleteAction, &QAction::triggered, [this, parent] {
deleteSystem(selected_sysconfig);
});
deleteAction.setEnabled(selected_sysconfig->process->state() == QProcess::NotRunning);
contextMenu.addSeparator();
QAction showRawConfigFile(tr("Show &config file"));
@@ -181,11 +188,19 @@ VMManagerMain::currentSelectionChanged(const QModelIndex &current,
return;
}
disconnect(selected_sysconfig, &VMManagerSystem::configurationChanged, this, &VMManagerMain::onConfigUpdated);
/* hack to prevent strange segfaults when adding a machine after
removing all machines previously */
if (selected_sysconfig->config_signal_connected == true) {
disconnect(selected_sysconfig, &VMManagerSystem::configurationChanged, this, &VMManagerMain::onConfigUpdated);
selected_sysconfig->config_signal_connected = false;
}
const auto mapped_index = proxy_model->mapToSource(current);
selected_sysconfig = vm_model->getConfigObjectForIndex(mapped_index);
vm_details->updateData(selected_sysconfig);
connect(selected_sysconfig, &VMManagerSystem::configurationChanged, this, &VMManagerMain::onConfigUpdated);
if (selected_sysconfig->config_signal_connected == false) {
connect(selected_sysconfig, &VMManagerSystem::configurationChanged, this, &VMManagerMain::onConfigUpdated);
selected_sysconfig->config_signal_connected = true;
}
// Emit that the selection changed, include with the process state
emit selectionChanged(current, selected_sysconfig->process->state());
@@ -433,6 +448,33 @@ VMManagerMain::addNewSystem(const QString &name, const QString &dir, const QStri
});
}
void
VMManagerMain::deleteSystem(VMManagerSystem *sysconfig)
{
QMessageBox msgbox(QMessageBox::Icon::Warning, tr("Warning"), tr("Do you really want to delete the virtual machine \"%1\" and all its files? This action cannot be undone!").arg(sysconfig->displayName), QMessageBox::StandardButton::Yes | QMessageBox::StandardButton::No, qobject_cast<QWidget *>(this->parent()));
msgbox.exec();
if (msgbox.result() == QMessageBox::Yes) {
auto qrmdir = new QDir(sysconfig->config_dir);
if (const bool rmdirResult = qrmdir->removeRecursively(); !rmdirResult) {
QMessageBox::critical(this, tr("Remove directory failed"), tr("Some files in the machine's directory were unable to be deleted. Please delete them manually."));
return;
}
auto config = new VMManagerConfig(VMManagerConfig::ConfigType::General);
config->remove(sysconfig->uuid);
vm_model->removeConfigFromModel(sysconfig);
delete sysconfig;
if (vm_model->rowCount(QModelIndex()) <= 0) {
/* no machines left - get rid of the last machine's leftovers */
ui->detailsArea->layout()->removeWidget(vm_details);
delete vm_details;
vm_details = new VMManagerDetails();
ui->detailsArea->layout()->addWidget(vm_details);
}
}
}
QStringList
VMManagerMain::getSearchCompletionList() const
{

View File

@@ -70,6 +70,7 @@ public slots:
void shutdownForceButtonPressed() const;
void searchSystems(const QString &text) const;
void newMachineWizard();
void deleteSystem(VMManagerSystem *sysconfig);
void addNewSystem(const QString &name, const QString &dir, const QString &displayName = QString(), const QString &configFile = {});
#if __GNUC__ >= 11
[[nodiscard]] QStringList getSearchCompletionList() const;

View File

@@ -140,6 +140,18 @@ VMManagerModel::addConfigToModel(VMManagerSystem *system_config)
connect(system_config, &VMManagerSystem::itemDataChanged, this, &VMManagerModel::modelDataChanged);
endInsertRows();
}
void
VMManagerModel::removeConfigFromModel(VMManagerSystem *system_config)
{
const QModelIndex index = getIndexForConfigFile(system_config->config_file);
disconnect(system_config, &VMManagerSystem::itemDataChanged, this, &VMManagerModel::modelDataChanged);
beginRemoveRows(QModelIndex(), index.row(), index.row());
machines.remove(index.row());
endRemoveRows();
emit systemDataChanged();
}
void
VMManagerModel::modelDataChanged()
{
@@ -177,4 +189,4 @@ VMManagerModel::getActiveMachineCount()
running++;
}
return running;
}
}

View File

@@ -51,6 +51,7 @@ public:
[[nodiscard]] QVariant headerData(int section, Qt::Orientation orientation,
int role) const override;
void addConfigToModel(VMManagerSystem *system_config);
void removeConfigFromModel(VMManagerSystem *system_config);
[[nodiscard]] VMManagerSystem * getConfigObjectForIndex(const QModelIndex &index) const;
QModelIndex getIndexForConfigFile(const QFileInfo& config_file);

View File

@@ -136,6 +136,7 @@ public:
QProcess *process = new QProcess();
bool window_obscured;
bool config_signal_connected = false;
QString getDisplayValue(VMManager::Display::Name key);
QFileInfoList getScreenshots();