Commit 003ce1d5 authored by Jan Grulich's avatar Jan Grulich

Ask to save modified connection when selecting another one

BUG:381158
parent 559693ce
......@@ -27,6 +27,7 @@
#include "vpnuiplugin.h"
// KDE
#include <KMessageBox>
#include <KPluginFactory>
#include <KSharedConfig>
#include <kdeclarative/kdeclarative.h>
......@@ -83,6 +84,7 @@ KCMNetworkmanagement::KCMNetworkmanagement(QWidget *parent, const QVariantList &
m_quickView->rootContext()->setContextProperty("baseColor", mainWidget->palette().color(QPalette::Active, QPalette::Base));
m_quickView->rootContext()->setContextProperty("highlightColor", mainWidget->palette().color(QPalette::Active, QPalette::Highlight));
m_quickView->rootContext()->setContextProperty("textColor", mainWidget->palette().color(QPalette::Active, QPalette::Text));
m_quickView->rootContext()->setContextProperty("connectionModified", false);
m_quickView->setResizeMode(QQuickView::SizeRootObjectToView);
m_quickView->setSource(QUrl::fromLocalFile(QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("kcm_networkmanagement/qml/main.qml"))));
......@@ -90,6 +92,7 @@ KCMNetworkmanagement::KCMNetworkmanagement(QWidget *parent, const QVariantList &
connect(rootItem, SIGNAL(selectedConnectionChanged(QString)), this, SLOT(onSelectedConnectionChanged(QString)));
connect(rootItem, SIGNAL(requestCreateConnection(int,QString,QString,bool)), this, SLOT(onRequestCreateConnection(int,QString,QString,bool)));
connect(rootItem, SIGNAL(requestExportConnection(QString)), this, SLOT(onRequestExportConnection(QString)));
connect(rootItem, SIGNAL(requestToChangeConnection(QString,QString)), this, SLOT(onRequestToChangeConnection(QString,QString)));
QVBoxLayout *l = new QVBoxLayout(this);
l->addWidget(mainWidget);
......@@ -204,6 +207,8 @@ void KCMNetworkmanagement::save()
m_handler->updateConnection(connection, m_tabWidget->setting());
}
kcmChanged(false);
KCModule::save();
}
......@@ -369,6 +374,21 @@ void KCMNetworkmanagement::onRequestExportConnection(const QString &connectionPa
}
}
void KCMNetworkmanagement::onRequestToChangeConnection( const QString &connectionName, const QString &connectionPath)
{
NetworkManager::Connection::Ptr connection = NetworkManager::findConnection(m_currentConnectionPath);
if (connection) {
if (KMessageBox::questionYesNo(this, i18n("Do you want to save changes made to the connection '%1'?", connection->name()), i18n("Save changes"),
KStandardGuiItem::save(), KStandardGuiItem::discard(), QString(), KMessageBox::Notify) == KMessageBox::Yes) {
save();
}
}
QObject *rootItem = m_quickView->rootObject();
QMetaObject::invokeMethod(rootItem, "selectConnection", Q_ARG(QVariant, connectionName), Q_ARG(QVariant, connectionPath));
}
void KCMNetworkmanagement::onSelectedConnectionChanged(const QString &connectionPath)
{
if (connectionPath.isEmpty()) {
......@@ -404,6 +424,12 @@ void KCMNetworkmanagement::addConnection(const NetworkManager::ConnectionSetting
editor->show();
}
void KCMNetworkmanagement::kcmChanged(bool kcmChanged)
{
m_quickView->rootContext()->setContextProperty("connectionModified", kcmChanged);
Q_EMIT changed(kcmChanged);
}
void KCMNetworkmanagement::loadConnectionSettings(const NetworkManager::ConnectionSettings::Ptr& connectionSettings)
{
if (m_tabWidget) {
......@@ -413,33 +439,20 @@ void KCMNetworkmanagement::loadConnectionSettings(const NetworkManager::Connecti
connect(m_tabWidget, &ConnectionEditorTabWidget::settingChanged,
[this] () {
if (m_tabWidget->isInitialized() && m_tabWidget->isValid()) {
Q_EMIT changed(true);
kcmChanged(true);
}
});
connect(m_tabWidget, &ConnectionEditorTabWidget::validityChanged,
[this] (bool valid) {
if (m_tabWidget->isInitialized()) {
Q_EMIT changed(valid);
kcmChanged(valid);
}
});
QVBoxLayout *layout = new QVBoxLayout(m_ui->connectionConfiguration);
layout->addWidget(m_tabWidget);
}
Q_EMIT changed(false);
}
void KCMNetworkmanagement::resetSelection()
{
// Reset selected connections
m_currentConnectionPath.clear();
QObject *rootItem = m_quickView->rootObject();
QMetaObject::invokeMethod(rootItem, "deselectConnections");
if (m_tabWidget) {
delete m_ui->connectionConfiguration->layout();
delete m_tabWidget;
m_tabWidget = nullptr;
}
Q_EMIT changed(false);
kcmChanged(false);
}
void KCMNetworkmanagement::importVpn()
......@@ -495,4 +508,18 @@ void KCMNetworkmanagement::importVpn()
}
}
void KCMNetworkmanagement::resetSelection()
{
// Reset selected connections
m_currentConnectionPath.clear();
QObject *rootItem = m_quickView->rootObject();
QMetaObject::invokeMethod(rootItem, "deselectConnections");
if (m_tabWidget) {
delete m_ui->connectionConfiguration->layout();
delete m_tabWidget;
m_tabWidget = nullptr;
}
Q_EMIT changed(false);
}
#include "kcm.moc"
......@@ -46,12 +46,14 @@ private Q_SLOTS:
void onSelectedConnectionChanged(const QString &connectionPath);
void onRequestCreateConnection(int connectionType, const QString &vpnType, const QString &specificType, bool shared);
void onRequestExportConnection(const QString &connectionPath);
void onRequestToChangeConnection(const QString &connectionName, const QString &connectionPath);
private:
void addConnection(const NetworkManager::ConnectionSettings::Ptr &connectionSettings);
void importVpn();
void kcmChanged(bool kcmChanged);
void loadConnectionSettings(const NetworkManager::ConnectionSettings::Ptr &connectionSettings);
void resetSelection();
void importVpn();
QString m_currentConnectionPath;
QString m_createdConnectionUuid;
......
......@@ -30,6 +30,7 @@ ListItem {
checked: mouseArea.containsMouse || ConnectionPath === connectionView.currentConnectionPath
height: connectionItemBase.height
signal aboutToChangeConnection(bool exportable, string name, string path)
signal aboutToExportConnection(string path)
signal aboutToRemoveConnection(string name, string path)
......@@ -146,9 +147,7 @@ ListItem {
onClicked: {
if (mouse.button === Qt.LeftButton) {
connectionView.currentConnectionExportable = KcmVpnConnectionExportable
connectionView.currentConnectionName = Name
connectionView.currentConnectionPath = ConnectionPath
aboutToChangeConnection(KcmVpnConnectionExportable, Name, ConnectionPath)
} else if (mouse.button == Qt.RightButton) {
connectionItemMenu.open(mouse.x, mouse.y)
}
......
......@@ -34,6 +34,7 @@ Item {
signal selectedConnectionChanged(string connection)
signal requestCreateConnection(int type, string vpnType, string specificType, bool shared)
signal requestExportConnection(string connection)
signal requestToChangeConnection(string name, string path)
Rectangle {
id: background
......@@ -98,15 +99,26 @@ Item {
section.property: "KcmConnectionType"
section.delegate: Header { text: section }
delegate: ConnectionItem {
onAboutToRemoveConnection: {
deleteConfirmationDialog.connectionName = name
deleteConfirmationDialog.connectionPath = path
deleteConfirmationDialog.open()
onAboutToChangeConnection: {
// Shouldn't be problem to set this in advance
connectionView.currentConnectionExportable = exportable
if (connectionModified) {
requestToChangeConnection(name, path)
} else {
connectionView.currentConnectionName = name
connectionView.currentConnectionPath = path
}
}
onAboutToExportConnection: {
requestExportConnection(path)
}
onAboutToRemoveConnection: {
deleteConfirmationDialog.connectionName = name
deleteConfirmationDialog.connectionPath = path
deleteConfirmationDialog.open()
}
}
onCurrentConnectionPathChanged: {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment