diff --git a/src/Views/VPNPage.vala b/src/Views/VPNPage.vala index c2f38784..5a5cbd15 100644 --- a/src/Views/VPNPage.vala +++ b/src/Views/VPNPage.vala @@ -88,7 +88,10 @@ public class Network.VPNPage : Network.Widgets.Page { child = main_overlay; add_button.clicked.connect (() => { - try_connection_editor ("--create --type=vpn"); + var connection_dialog = new VPNConnectionDialog () { + transient_for = (Gtk.Window) get_root () + }; + connection_dialog.present (); }); remove_vpn_toast.default_action.connect (() => { @@ -291,31 +294,6 @@ public class Network.VPNPage : Network.Widgets.Page { } } - private void try_connection_editor (string args) { - try { - var appinfo = AppInfo.create_from_commandline ( - "nm-connection-editor %s".printf (args), - null, - GLib.AppInfoCreateFlags.NONE - ); - appinfo.launch (null, null); - } catch (Error error) { - var dialog = new Granite.MessageDialog ( - _("Failed to run Connection Editor"), - _("The program \"nm-connection-editor\" may not be installed."), - new ThemedIcon ("network-vpn"), - Gtk.ButtonsType.CLOSE - ) { - badge_icon = new ThemedIcon ("dialog-error"), - modal = true, - transient_for = (Gtk.Window) get_root () - }; - dialog.show_error_details (error.message); - dialog.present (); - dialog.response.connect (dialog.destroy); - } - } - private void delete_connection (VPNMenuItem item) { if (sel_row != null && sel_row == item) { try { diff --git a/src/Widgets/VPN/VPNConnectionDialog.vala b/src/Widgets/VPN/VPNConnectionDialog.vala new file mode 100644 index 00000000..4fa5db2f --- /dev/null +++ b/src/Widgets/VPN/VPNConnectionDialog.vala @@ -0,0 +1,148 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * SPDX-FileCopyrightText: 2024 elementary, Inc. (https://elementary.io) + */ + +public class Network.VPNConnectionDialog : Gtk.Window { + construct { + var title_label = new Gtk.Label (_("Choose a VPN Connection type")) { + hexpand = true, + selectable = true, + max_width_chars = 50, + wrap = true, + xalign = 0 + }; + title_label.add_css_class (Granite.STYLE_CLASS_TITLE_LABEL); + + var import_button = new Gtk.Button.with_label (_("Import configuration file")); + + var content_area = new Gtk.Box (VERTICAL, 0); + content_area.append (title_label); + content_area.append (import_button); + content_area.add_css_class ("dialog-content-area"); + + var create_button = new Gtk.Button.with_label (_("Create…")) { + sensitive = false + }; + + var cancel_button = new Gtk.Button.with_label (_("Cancel")); + + var action_area = new Gtk.Box (HORIZONTAL, 0) { + margin_top = 24, + halign = END, + valign = END, + vexpand = true, + homogeneous = true + }; + action_area.append (cancel_button); + action_area.append (create_button); + action_area.add_css_class ("dialog-action-area"); + + var vbox = new Gtk.Box (VERTICAL, 0); + vbox.append (content_area); + vbox.append (action_area); + vbox.add_css_class ("dialog-vbox"); + + var window_handle = new Gtk.WindowHandle () { + child = vbox + }; + + add_css_class ("dialog"); + add_css_class ("message"); + default_height = 400; + default_width = 300; + modal = true; + child = window_handle; + titlebar = new Gtk.Grid () { visible = false }; + + import_button.clicked.connect (import_config_file); + + cancel_button.clicked.connect (() => close ()); + } + + private async void import_config_file () { + var dialog = new Gtk.FileDialog () { + modal = true, + title = _("Select file to import") + }; + + + GLib.File file = null; + try { + file = yield dialog.open (this, null); + } catch (Error e) { + critical (e.message); + } + + var filename = file.get_path (); + + NM.Connection connection = null; + + try { + connection = NM.conn_wireguard_import (filename); + } catch (Error e) { + critical (e.message); + } + + try { + var plugin_info_list = NM.VpnPluginInfo.list_load (); + foreach (unowned var plugin_info in plugin_info_list) { + if (connection != null) { + break; + } + + var plugin = plugin_info.get_editor_plugin (); + connection = plugin.import (filename); + } + } catch (Error e) { + critical (e.message); + } + + // try { + // yield ((NM.RemoteConnection) connection).save_async (null); + // } catch (Error e) { + // critical (e.message); + // } + + close (); + } +} + + + + + + + + + + + + + + + // try_connection_editor ("--create --type=vpn"); + // private void try_connection_editor (string args) { + // try { + // var appinfo = AppInfo.create_from_commandline ( + // "nm-connection-editor %s".printf (args), + // null, + // GLib.AppInfoCreateFlags.NONE + // ); + // appinfo.launch (null, null); + // } catch (Error error) { + // var dialog = new Granite.MessageDialog ( + // _("Failed to run Connection Editor"), + // _("The program \"nm-connection-editor\" may not be installed."), + // new ThemedIcon ("network-vpn"), + // Gtk.ButtonsType.CLOSE + // ) { + // badge_icon = new ThemedIcon ("dialog-error"), + // modal = true, + // transient_for = (Gtk.Window) get_root () + // }; + // dialog.show_error_details (error.message); + // dialog.present (); + // dialog.response.connect (dialog.destroy); + // } + // } diff --git a/src/meson.build b/src/meson.build index 3de2d2a5..afffe7e2 100644 --- a/src/meson.build +++ b/src/meson.build @@ -16,6 +16,7 @@ plug_files = files( 'Widgets/WifiMenuItem.vala', 'Widgets/Proxy/ProxyExceptionsPage.vala', 'Widgets/Proxy/ProxyConfigurationPage.vala', + 'Widgets/VPN/VPNConnectionDialog.vala', 'Widgets/VPN/VPNInfoDialog.vala', 'Widgets/VPN/VPNMenuItem.vala' )