|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | + * SPDX-FileCopyrightText: 2024 elementary, Inc. (https://elementary.io) |
| 4 | + */ |
| 5 | + |
| 6 | +public class Network.VPNConnectionDialog : Gtk.Window { |
| 7 | + construct { |
| 8 | + var title_label = new Gtk.Label (_("Choose a VPN Connection type")) { |
| 9 | + hexpand = true, |
| 10 | + selectable = true, |
| 11 | + max_width_chars = 50, |
| 12 | + wrap = true, |
| 13 | + xalign = 0 |
| 14 | + }; |
| 15 | + title_label.add_css_class (Granite.STYLE_CLASS_TITLE_LABEL); |
| 16 | + |
| 17 | + var import_button = new Gtk.Button.with_label (_("Import configuration file")); |
| 18 | + |
| 19 | + var content_area = new Gtk.Box (VERTICAL, 0); |
| 20 | + content_area.append (title_label); |
| 21 | + content_area.append (import_button); |
| 22 | + content_area.add_css_class ("dialog-content-area"); |
| 23 | + |
| 24 | + var create_button = new Gtk.Button.with_label (_("Create…")) { |
| 25 | + sensitive = false |
| 26 | + }; |
| 27 | + |
| 28 | + var cancel_button = new Gtk.Button.with_label (_("Cancel")); |
| 29 | + |
| 30 | + var action_area = new Gtk.Box (HORIZONTAL, 0) { |
| 31 | + margin_top = 24, |
| 32 | + halign = END, |
| 33 | + valign = END, |
| 34 | + vexpand = true, |
| 35 | + homogeneous = true |
| 36 | + }; |
| 37 | + action_area.append (cancel_button); |
| 38 | + action_area.append (create_button); |
| 39 | + action_area.add_css_class ("dialog-action-area"); |
| 40 | + |
| 41 | + var vbox = new Gtk.Box (VERTICAL, 0); |
| 42 | + vbox.append (content_area); |
| 43 | + vbox.append (action_area); |
| 44 | + vbox.add_css_class ("dialog-vbox"); |
| 45 | + |
| 46 | + var window_handle = new Gtk.WindowHandle () { |
| 47 | + child = vbox |
| 48 | + }; |
| 49 | + |
| 50 | + add_css_class ("dialog"); |
| 51 | + add_css_class ("message"); |
| 52 | + default_height = 400; |
| 53 | + default_width = 300; |
| 54 | + modal = true; |
| 55 | + child = window_handle; |
| 56 | + titlebar = new Gtk.Grid () { visible = false }; |
| 57 | + |
| 58 | + import_button.clicked.connect (import_config_file); |
| 59 | + |
| 60 | + cancel_button.clicked.connect (() => close ()); |
| 61 | + } |
| 62 | + |
| 63 | + private async void import_config_file () { |
| 64 | + var dialog = new Gtk.FileDialog () { |
| 65 | + modal = true, |
| 66 | + title = _("Select file to import") |
| 67 | + }; |
| 68 | + |
| 69 | + |
| 70 | + GLib.File file = null; |
| 71 | + try { |
| 72 | + file = yield dialog.open (this, null); |
| 73 | + } catch (Error e) { |
| 74 | + critical (e.message); |
| 75 | + } |
| 76 | + |
| 77 | + var filename = file.get_path (); |
| 78 | + |
| 79 | + NM.Connection connection = null; |
| 80 | + |
| 81 | + try { |
| 82 | + connection = NM.conn_wireguard_import (filename); |
| 83 | + } catch (Error e) { |
| 84 | + critical (e.message); |
| 85 | + } |
| 86 | + |
| 87 | + try { |
| 88 | + var plugin_info_list = NM.VpnPluginInfo.list_load (); |
| 89 | + foreach (unowned var plugin_info in plugin_info_list) { |
| 90 | + if (connection != null) { |
| 91 | + break; |
| 92 | + } |
| 93 | + |
| 94 | + var plugin = plugin_info.get_editor_plugin (); |
| 95 | + connection = plugin.import (filename); |
| 96 | + } |
| 97 | + } catch (Error e) { |
| 98 | + critical (e.message); |
| 99 | + } |
| 100 | + |
| 101 | + // try { |
| 102 | + // yield ((NM.RemoteConnection) connection).save_async (null); |
| 103 | + // } catch (Error e) { |
| 104 | + // critical (e.message); |
| 105 | + // } |
| 106 | + |
| 107 | + close (); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | + // try_connection_editor ("--create --type=vpn"); |
| 125 | + // private void try_connection_editor (string args) { |
| 126 | + // try { |
| 127 | + // var appinfo = AppInfo.create_from_commandline ( |
| 128 | + // "nm-connection-editor %s".printf (args), |
| 129 | + // null, |
| 130 | + // GLib.AppInfoCreateFlags.NONE |
| 131 | + // ); |
| 132 | + // appinfo.launch (null, null); |
| 133 | + // } catch (Error error) { |
| 134 | + // var dialog = new Granite.MessageDialog ( |
| 135 | + // _("Failed to run Connection Editor"), |
| 136 | + // _("The program \"nm-connection-editor\" may not be installed."), |
| 137 | + // new ThemedIcon ("network-vpn"), |
| 138 | + // Gtk.ButtonsType.CLOSE |
| 139 | + // ) { |
| 140 | + // badge_icon = new ThemedIcon ("dialog-error"), |
| 141 | + // modal = true, |
| 142 | + // transient_for = (Gtk.Window) get_root () |
| 143 | + // }; |
| 144 | + // dialog.show_error_details (error.message); |
| 145 | + // dialog.present (); |
| 146 | + // dialog.response.connect (dialog.destroy); |
| 147 | + // } |
| 148 | + // } |
0 commit comments