diff --git a/src/qt/qt_vmmanager_main.cpp b/src/qt/qt_vmmanager_main.cpp index bf22e367e..484696954 100644 --- a/src/qt/qt_vmmanager_main.cpp +++ b/src/qt/qt_vmmanager_main.cpp @@ -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")); @@ -433,6 +440,25 @@ 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(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; + } +} + QStringList VMManagerMain::getSearchCompletionList() const { diff --git a/src/qt/qt_vmmanager_main.hpp b/src/qt/qt_vmmanager_main.hpp index d41d57938..9b9401d0f 100644 --- a/src/qt/qt_vmmanager_main.hpp +++ b/src/qt/qt_vmmanager_main.hpp @@ -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; diff --git a/src/qt/qt_vmmanager_model.cpp b/src/qt/qt_vmmanager_model.cpp index 76fcbffd6..3f6bdc4f9 100644 --- a/src/qt/qt_vmmanager_model.cpp +++ b/src/qt/qt_vmmanager_model.cpp @@ -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; -} \ No newline at end of file +} diff --git a/src/qt/qt_vmmanager_model.hpp b/src/qt/qt_vmmanager_model.hpp index 9fed1ca8c..4205e8098 100644 --- a/src/qt/qt_vmmanager_model.hpp +++ b/src/qt/qt_vmmanager_model.hpp @@ -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);