You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.6 KiB

#include "networkdeleter.h"
#include "ui_networkdeleter.h"
#include <NetworkManagerQt/Connection>
#include <NetworkManagerQt/ConnectionSettings>
#include <NetworkManagerQt/Settings>
#include <QDBusPendingCallWatcher>
#include <QDBusPendingReply>
#include <QIcon>
#include <QLayout>
#include <QMessageBox>
NetworkDeleter::NetworkDeleter(QString networkName, QString networkUuidStr, QWidget *parent) :
QDialog(parent),
ui(new Ui::NetworkDeleter)
{
targetNetworkName = networkName;
targetNetworkUuidStr = networkUuidStr;
ui->setupUi(this);
this->setWindowTitle(tr("Delete connection %1?").arg(networkName));
ui->connectionDeleteWarningLabel->setText(tr("Are you sure you want to delete connection %1?").arg(networkName));
this->layout()->setSizeConstraint(QLayout::SetFixedSize);
connect(ui->cancelButton, &QPushButton::clicked, this, &NetworkDeleter::onCancelClicked);
connect(ui->deleteButton, &QPushButton::clicked, this, &NetworkDeleter::onDeleteClicked);
}
NetworkDeleter::~NetworkDeleter()
{
delete ui;
}
void NetworkDeleter::onCancelClicked()
{
this->done(0);
}
void NetworkDeleter::onDeleteClicked()
{
NetworkManager::Connection::Ptr conn = NetworkManager::findConnectionByUuid(targetNetworkUuidStr);
if (!conn || conn->uuid().isEmpty()) {
QMessageBox errorMsg(QMessageBox::Critical, tr("Connection removal failed"), tr("Could not remove connection %1.").arg(targetNetworkName));
errorMsg.exec();
} else {
// Copied and adapted from plasma-nm
// Remove slave connections
for (const NetworkManager::Connection::Ptr &connection : NetworkManager::listConnections()) {
NetworkManager::ConnectionSettings::Ptr settings = connection->settings();
if (settings->master() == conn->uuid()) {
connection->remove();
}
}
QDBusPendingReply<> reply = conn->remove();
auto watcher = new QDBusPendingCallWatcher(reply, this);
connect(watcher, &QDBusPendingCallWatcher::finished, this, &NetworkDeleter::deleteReplyFinished);
ui->connectionDeleteWarningLabel->setText(tr("Deleting connection %1...").arg(targetNetworkName));
ui->deleteButton->setEnabled(false);
}
}
void NetworkDeleter::deleteReplyFinished(QDBusPendingCallWatcher *watcher)
{
// Inspired by plasma-nm
QDBusPendingReply<> reply = *watcher;
if (reply.isError() || !reply.isValid()) {
QMessageBox errorMsg(QMessageBox::Critical, tr("Connection removal failed"), tr("Could not remove connection %1.").arg(targetNetworkName));
errorMsg.exec();
}
this->done(0);
}