Commit d1d8db50 authored by Jan Grulich's avatar Jan Grulich
Browse files

Add vpn import/export

parent fb1ae185
......@@ -22,10 +22,18 @@
#include "connectioneditor.h"
#include "connectiondetaileditor.h"
#include "mobileconnectionwizard.h"
#include "vpnuiplugin.h"
#include <KLocale>
#include <KService>
#include <KServiceTypeTrader>
#include <QFileDialog>
#include <NetworkManagerQt/Connection>
#include <NetworkManagerQt/Manager>
#include <NetworkManagerQt/Settings>
ConnectionEditor::ConnectionEditor(QObject* parent)
: QObject(parent)
{
......@@ -149,98 +157,95 @@ QStringList ConnectionEditor::availableVpnPlugins() const
// }
// }
// void ConnectionEditor::importVpn()
// {
// // get the list of supported extensions
// const KService::List services = KServiceTypeTrader::self()->query("PlasmaNetworkManagement/VpnUiPlugin");
// QString extensions;
// foreach (const KService::Ptr &service, services) {
// VpnUiPlugin * vpnPlugin = service->createInstance<VpnUiPlugin>(this);
// if (vpnPlugin) {
// extensions += vpnPlugin->supportedFileExtensions() % QLatin1Literal(" ");
// delete vpnPlugin;
// }
// }
//
// const QString filename = QFileDialog::getOpenFileName(this, i18n("Import VPN Connection"), QDir::homePath(), extensions.simplified());
// if (!filename.isEmpty()) {
// QFileInfo fi(filename);
// const QString ext = QLatin1Literal("*.") % fi.suffix();
// qDebug() << "Importing VPN connection" << filename << "extension:" << ext;
//
// foreach (const KService::Ptr &service, services) {
// VpnUiPlugin * vpnPlugin = service->createInstance<VpnUiPlugin>(this);
// if (vpnPlugin && vpnPlugin->supportedFileExtensions().contains(ext)) {
// qDebug() << "Found VPN plugin" << service->name() << ", type:" << service->property("X-NetworkManager-Services", QVariant::String).toString();
//
// NMVariantMapMap connection = vpnPlugin->importConnectionSettings(filename);
//
// //qDebug() << "Raw connection:" << connection;
//
// NetworkManager::ConnectionSettings connectionSettings;
// connectionSettings.fromMap(connection);
// connectionSettings.setUuid(NetworkManager::ConnectionSettings::createNewUuid());
//
// //qDebug() << "Converted connection:" << connectionSettings;
//
// const QString conId = NetworkManager::addConnection(connectionSettings.toMap());
// qDebug() << "Adding imported connection under id:" << conId;
//
// if (connection.isEmpty()) { // the "positive" part will arrive with connectionAdded
void ConnectionEditor::importVpn()
{
// get the list of supported extensions
const KService::List services = KServiceTypeTrader::self()->query("PlasmaNetworkManagement/VpnUiPlugin");
QString extensions;
foreach (const KService::Ptr &service, services) {
VpnUiPlugin * vpnPlugin = service->createInstance<VpnUiPlugin>(this);
if (vpnPlugin) {
extensions += vpnPlugin->supportedFileExtensions() % QLatin1Literal(" ");
delete vpnPlugin;
}
}
const QString filename = QFileDialog::getOpenFileName(0, i18n("Import VPN Connection"), QDir::homePath(), extensions.simplified());
if (!filename.isEmpty()) {
QFileInfo fi(filename);
const QString ext = QLatin1Literal("*.") % fi.suffix();
qDebug() << "Importing VPN connection" << filename << "extension:" << ext;
foreach (const KService::Ptr &service, services) {
VpnUiPlugin * vpnPlugin = service->createInstance<VpnUiPlugin>(this);
if (vpnPlugin && vpnPlugin->supportedFileExtensions().contains(ext)) {
qDebug() << "Found VPN plugin" << service->name() << ", type:" << service->property("X-NetworkManager-Services", QVariant::String).toString();
NMVariantMapMap connection = vpnPlugin->importConnectionSettings(filename);
//qDebug() << "Raw connection:" << connection;
NetworkManager::ConnectionSettings connectionSettings;
connectionSettings.fromMap(connection);
connectionSettings.setUuid(NetworkManager::ConnectionSettings::createNewUuid());
//qDebug() << "Converted connection:" << connectionSettings;
const QString conId = NetworkManager::addConnection(connectionSettings.toMap());
qDebug() << "Adding imported connection under id:" << conId;
if (connection.isEmpty()) { // the "positive" part will arrive with connectionAdded
// TODO display error
// m_editor->messageWidget->animatedShow();
// m_editor->messageWidget->setMessageType(KMessageWidget::Error);
// m_editor->messageWidget->setText(i18n("Importing VPN connection %1 failed\n%2", fi.fileName(), vpnPlugin->lastErrorMessage()));
// QTimer::singleShot(5000, m_editor->messageWidget, SLOT(animatedHide()));
// } else {
// delete vpnPlugin;
// break; // stop iterating over the plugins if the import produced at least some output
// }
//
// delete vpnPlugin;
// }
// }
// }
// }
} else {
delete vpnPlugin;
break; // stop iterating over the plugins if the import produced at least some output
}
// void ConnectionEditor::exportVpn()
// {
// const QModelIndex currentIndex = m_editor->connectionsWidget->currentIndex();
//
// if (!currentIndex.isValid() || currentIndex.parent().isValid()) {
// return;
// }
//
// Connection::Ptr connection = NetworkManager::findConnectionByUuid(currentIndex.data(NetworkModel::UuidRole).toString());
// if (!connection) {
// return;
// }
//
// NetworkManager::ConnectionSettings::Ptr connSettings = connection->settings();
//
// if (connSettings->connectionType() != NetworkManager::ConnectionSettings::Vpn)
// return;
//
// NetworkManager::VpnSetting::Ptr vpnSetting = connSettings->setting(NetworkManager::Setting::Vpn).dynamicCast<NetworkManager::VpnSetting>();
//
// qDebug() << "Exporting VPN connection" << connection->name() << "type:" << vpnSetting->serviceType();
//
// QString error;
// VpnUiPlugin * vpnPlugin = KServiceTypeTrader::createInstanceFromQuery<VpnUiPlugin>(QString::fromLatin1("PlasmaNetworkManagement/VpnUiPlugin"),
// QString::fromLatin1("[X-NetworkManager-Services]=='%1'").arg(vpnSetting->serviceType()),
// this, QVariantList(), &error);
//
// if (vpnPlugin) {
// if (vpnPlugin->suggestedFileName(connSettings).isEmpty()) { // this VPN doesn't support export
delete vpnPlugin;
}
}
}
}
void ConnectionEditor::exportVpn(const QString& connectionUuid)
{
NetworkManager::Connection::Ptr connection = NetworkManager::findConnectionByUuid(connectionUuid);
if (!connection) {
return;
}
NetworkManager::ConnectionSettings::Ptr connSettings = connection->settings();
if (connSettings->connectionType() != NetworkManager::ConnectionSettings::Vpn)
return;
NetworkManager::VpnSetting::Ptr vpnSetting = connSettings->setting(NetworkManager::Setting::Vpn).dynamicCast<NetworkManager::VpnSetting>();
qDebug() << "Exporting VPN connection" << connection->name() << "type:" << vpnSetting->serviceType();
QString error;
VpnUiPlugin * vpnPlugin = KServiceTypeTrader::createInstanceFromQuery<VpnUiPlugin>(QString::fromLatin1("PlasmaNetworkManagement/VpnUiPlugin"),
QString::fromLatin1("[X-NetworkManager-Services]=='%1'").arg(vpnSetting->serviceType()),
this, QVariantList(), &error);
if (vpnPlugin) {
if (vpnPlugin->suggestedFileName(connSettings).isEmpty()) { // this VPN doesn't support export
// TODO display error
// m_editor->messageWidget->animatedShow();
// m_editor->messageWidget->setMessageType(KMessageWidget::Error);
// m_editor->messageWidget->setText(i18n("Export is not supported by this VPN type"));
// QTimer::singleShot(5000, m_editor->messageWidget, SLOT(animatedHide()));
// return;
// }
//
// const QString url = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation) + QDir::separator() + vpnPlugin->suggestedFileName(connSettings);
// const QString filename = QFileDialog::getSaveFileName(this, i18n("Export VPN Connection"), url, vpnPlugin->supportedFileExtensions());
// if (!filename.isEmpty()) {
return;
}
const QString url = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation) + QDir::separator() + vpnPlugin->suggestedFileName(connSettings);
const QString filename = QFileDialog::getSaveFileName(0, i18n("Export VPN Connection"), url, vpnPlugin->supportedFileExtensions());
if (!filename.isEmpty()) {
// TODO display error/success
// if (!vpnPlugin->exportConnectionSettings(connSettings, filename)) {
// m_editor->messageWidget->animatedShow();
// m_editor->messageWidget->setMessageType(KMessageWidget::Error);
......@@ -252,9 +257,9 @@ QStringList ConnectionEditor::availableVpnPlugins() const
// m_editor->messageWidget->setText(i18n("VPN connection %1 exported successfully", connection->name()));
// QTimer::singleShot(5000, m_editor->messageWidget, SLOT(animatedHide()));
// }
// }
// delete vpnPlugin;
// } else {
// qWarning() << "Error getting VpnUiPlugin for export:" << error;
// }
// }
}
delete vpnPlugin;
} else {
qWarning() << "Error getting VpnUiPlugin for export:" << error;
}
}
......@@ -37,8 +37,8 @@ public:
Q_INVOKABLE void addConnection(uint connectionType, bool shared);
Q_INVOKABLE void addVpnConnection(const QString& plugin);
// Q_INVOKABLE void exportVpn();
// Q_INVOKABLE void importVpn();
Q_INVOKABLE void exportVpn(const QString& connectionUuid);
Q_INVOKABLE void importVpn();
QStringList availableVpnPlugins() const;
private:
......
......@@ -28,7 +28,6 @@ import QtQuick.Layouts 1.1
import org.kde.plasma.core 2.0 as PlasmaCore
// import org.kde.plasma.extras 2.0 as PlasmaExtras
import org.kde.plasma.networkmanagement 0.1 as PlasmaNM
import ConnectionEditor 0.1
Window {
id: addConnectionDialog;
......@@ -40,10 +39,6 @@ Window {
flags: Qt.Dialog;
title: i18n("Add a new connection");
ConnectionEditor {
id: connectionEditor;
}
Text {
id: dialogHeadline;
......
......@@ -27,6 +27,7 @@ import QtQuick.Layouts 1.1
import org.kde.plasma.core 2.0 as PlasmaCore
// import org.kde.plasma.extras 2.0 as PlasmaExtras
import org.kde.plasma.networkmanagement 0.1 as PlasmaNM
import ConnectionEditor 0.1
ApplicationWindow {
id: mainWindow;
......@@ -77,11 +78,40 @@ ApplicationWindow {
}
}
Action {
id: importVpnAction;
iconName: "document-import";
text: i18n("Import VPN");
onTriggered: {
connectionEditor.importVpn();
}
}
Action {
id: exportVpnAction;
enabled: connectionView.selectedConnectionType == PlasmaNM.Enums.Vpn;
iconName: "document-export";
text: i18n("Export VPN");
onTriggered: {
connectionEditor.exportVpn(connectionView.selectedConnectionUuid);
}
}
menuBar: MenuBar {
Menu {
title: i18n("File");
// TODO import/export VPN
MenuItem {
action: importVpnAction;
}
MenuItem {
action: exportVpnAction;
}
MenuItem {
text: i18n("Close");
......@@ -115,6 +145,10 @@ ApplicationWindow {
}
}
ConnectionEditor {
id: connectionEditor;
}
PlasmaNM.Handler {
id: handler;
}
......@@ -126,10 +160,10 @@ ApplicationWindow {
PlasmaNM.EditorProxyModel {
id: editorProxyModel;
filterString: searchBar.text;
sourceModel: connectionModel;
sortRole: connectionView.getColumn(connectionView.sortIndicatorColumn).role;
sortOrder: connectionView.sortIndicatorOrder;
filterString: searchBar.text;
}
Dialog {
......@@ -139,7 +173,6 @@ ApplicationWindow {
SystemPalette { id: myPalette; colorGroup: SystemPalette.Active }
GroupBox {
anchors.fill: parent;
TextField {
......@@ -156,10 +189,9 @@ ApplicationWindow {
TableView {
id: connectionView;
property string selectedConnectionDevicePath;
property string selectedConnectionPath;
property string selectedConnectionSpecificPath;
property string selectedConnectionUuid;
property int selectedConnectionType;
anchors {
bottom: parent.bottom;
......@@ -170,43 +202,7 @@ ApplicationWindow {
model: editorProxyModel;
TableViewColumn {
role: "Name";
title: i18n("Connection name");
width: 300
// delegate: Item {
// PlasmaCore.IconItem {
// id: stateIcon;
//
// anchors {
// left: parent.left;
// leftMargin: 5;
// verticalCenter: parent.verticalCenter;
// }
// width: units.iconSizes.small;
// height: units.iconSizes.small;
//
// source: model.get(styleData.row, "ConnectionState") == PlasmaNM.Enums.Activated ? "user-online" : "user-offline";
// active: true;
// smooth: true;
// }
//
// Text {
// anchors {
// left: stateIcon.right;
// leftMargin: 8;
// verticalCenter: parent.verticalCenter;
// }
//
//
// color: styleData.textColor
// elide: styleData.elideMode
// text: styleData.value
// }
// }
}
TableViewColumn { role: "Name"; title: i18n("Connection name"); width: 300 }
TableViewColumn { role: "LastUsedDateOnly"; title: i18n("Last used") }
section.property: "TypeString";
......@@ -268,12 +264,12 @@ ApplicationWindow {
color: styleData.selected ? myPalette.highlight : (styleData.alternate ? myPalette.midlight : myPalette.light);
}
backgroundVisible: false;
sortIndicatorVisible: true;
onClicked: {
selectedConnectionDevicePath = model.get(row, "DevicePath");
selectedConnectionPath = model.get(row, "ConnectionPath");
selectedConnectionSpecificPath = model.get(row, "SpecificPath");
selectedConnectionType = model.get(row, "Type");
selectedConnectionUuid = model.get(row, "Uuid");
console.log("Connection " + model.get(row, "Name") + " selected");
}
......
......@@ -121,16 +121,8 @@ QVariant EditorProxyModel::get(int row, const QString& role)
return data(index, NetworkModel::ConnectionPathRole);
} else if (role == QLatin1String("ConnectionState")) {
return data(index, NetworkModel::ConnectionStateRole);
} else if (role == QLatin1String("DevicePath")) {
return data(index, NetworkModel::DevicePathRole);
} else if (role == QLatin1String("ItemType")) {
return data(index, NetworkModel::ItemTypeRole);
} else if (role == QLatin1String("Name")) {
return data(index, NetworkModel::NameRole);
} else if (role == QLatin1String("TypeIcon")) {
return data(index, NetworkModel::TypeIconRole);
} else if (role == QLatin1String("SpecificPath")) {
return data(index, NetworkModel::SpecificPathRole);
} else if (role == QLatin1String("Type")) {
return data(index, NetworkModel::TypeRole);
} else if (role == QLatin1String("Uuid")) {
return data(index, NetworkModel::UuidRole);
}
......
......@@ -49,8 +49,6 @@ QVariant NetworkModel::data(const QModelIndex& index, int role) const
if (row >= 0 && row < m_list.count()) {
NetworkModelItem * item = m_list.itemAt(row);
QStringList editorData;
QStringList editorSection;
switch (role) {
case AppletSectionRole:
if (item->connectionState() == NetworkManager::ActiveConnection::Activated) {
......
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