Skip to content

Commit eadc035

Browse files
committed
Start in on VPN Connection Dialog
1 parent 95eee7d commit eadc035

File tree

3 files changed

+153
-26
lines changed

3 files changed

+153
-26
lines changed

src/Views/VPNPage.vala

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,10 @@ public class Network.VPNPage : Network.Widgets.Page {
8888
child = main_overlay;
8989

9090
add_button.clicked.connect (() => {
91-
try_connection_editor ("--create --type=vpn");
91+
var connection_dialog = new VPNConnectionDialog () {
92+
transient_for = (Gtk.Window) get_root ()
93+
};
94+
connection_dialog.present ();
9295
});
9396

9497
remove_vpn_toast.default_action.connect (() => {
@@ -291,31 +294,6 @@ public class Network.VPNPage : Network.Widgets.Page {
291294
}
292295
}
293296

294-
private void try_connection_editor (string args) {
295-
try {
296-
var appinfo = AppInfo.create_from_commandline (
297-
"nm-connection-editor %s".printf (args),
298-
null,
299-
GLib.AppInfoCreateFlags.NONE
300-
);
301-
appinfo.launch (null, null);
302-
} catch (Error error) {
303-
var dialog = new Granite.MessageDialog (
304-
_("Failed to run Connection Editor"),
305-
_("The program \"nm-connection-editor\" may not be installed."),
306-
new ThemedIcon ("network-vpn"),
307-
Gtk.ButtonsType.CLOSE
308-
) {
309-
badge_icon = new ThemedIcon ("dialog-error"),
310-
modal = true,
311-
transient_for = (Gtk.Window) get_root ()
312-
};
313-
dialog.show_error_details (error.message);
314-
dialog.present ();
315-
dialog.response.connect (dialog.destroy);
316-
}
317-
}
318-
319297
private void delete_connection (VPNMenuItem item) {
320298
if (sel_row != null && sel_row == item) {
321299
try {
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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+
// }

src/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ plug_files = files(
1616
'Widgets/WifiMenuItem.vala',
1717
'Widgets/Proxy/ProxyExceptionsPage.vala',
1818
'Widgets/Proxy/ProxyConfigurationPage.vala',
19+
'Widgets/VPN/VPNConnectionDialog.vala',
1920
'Widgets/VPN/VPNInfoDialog.vala',
2021
'Widgets/VPN/VPNMenuItem.vala'
2122
)

0 commit comments

Comments
 (0)