GCC Code Coverage Report


Directory: ./
File: panels/network/connection-editor/vpn-helpers.c
Date: 2024-05-04 07:58:27
Exec Total Coverage
Lines: 0 68 0.0%
Functions: 0 5 0.0%
Branches: 0 30 0.0%

Line Branch Exec Source
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* NetworkManager Connection editor -- Connection editor for NetworkManager
3 *
4 * Dan Williams <dcbw@redhat.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * (C) Copyright 2008 Red Hat, Inc.
21 */
22
23 #include "config.h"
24
25 #include <adwaita.h>
26 #include <string.h>
27 #include <glib.h>
28 #include <gmodule.h>
29 #include <adwaita.h>
30 #include <gtk/gtk.h>
31 #include <glib/gi18n.h>
32 #include <NetworkManager.h>
33
34 #include "vpn-helpers.h"
35
36 NMVpnEditorPlugin *
37 vpn_get_plugin_by_service (const char *service)
38 {
39 NMVpnPluginInfo *plugin_info;
40
41 g_return_val_if_fail (service != NULL, NULL);
42
43 plugin_info = nm_vpn_plugin_info_list_find_by_service (vpn_get_plugins (), service);
44 if (plugin_info)
45 return nm_vpn_plugin_info_get_editor_plugin (plugin_info);
46 return NULL;
47 }
48
49 static gint
50 _sort_vpn_plugins (NMVpnPluginInfo *aa, NMVpnPluginInfo *bb)
51 {
52 return strcmp (nm_vpn_plugin_info_get_name (aa), nm_vpn_plugin_info_get_name (bb));
53 }
54
55 GSList *
56 vpn_get_plugins (void)
57 {
58 static GSList *plugins = NULL;
59 GSList *p;
60
61 p = nm_vpn_plugin_info_list_load ();
62 plugins = NULL;
63 while (p) {
64 g_autoptr(NMVpnPluginInfo) plugin_info = NM_VPN_PLUGIN_INFO (p->data);
65 g_autoptr(GError) error = NULL;
66
67 /* load the editor plugin, and preserve only those NMVpnPluginInfo that can
68 * successfully load the plugin. */
69 if (nm_vpn_plugin_info_load_editor_plugin (plugin_info, &error))
70 plugins = g_slist_prepend (plugins, g_steal_pointer (&plugin_info));
71 else {
72 if ( !nm_vpn_plugin_info_get_plugin (plugin_info)
73 && nm_vpn_plugin_info_lookup_property (plugin_info, NM_VPN_PLUGIN_INFO_KF_GROUP_GNOME, "properties")) {
74 g_message ("vpn: (%s,%s) cannot load legacy-only plugin",
75 nm_vpn_plugin_info_get_name (plugin_info),
76 nm_vpn_plugin_info_get_filename (plugin_info));
77 } else if (g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT)) {
78 g_message ("vpn: (%s,%s) file \"%s\" not found. Did you install the client package?",
79 nm_vpn_plugin_info_get_name (plugin_info),
80 nm_vpn_plugin_info_get_filename (plugin_info),
81 nm_vpn_plugin_info_get_plugin (plugin_info));
82 } else {
83 g_warning ("vpn: (%s,%s) could not load plugin: %s",
84 nm_vpn_plugin_info_get_name (plugin_info),
85 nm_vpn_plugin_info_get_filename (plugin_info),
86 error->message);
87 }
88 }
89 p = g_slist_delete_link (p, p);
90 }
91
92 /* sort the list of plugins alphabetically. */
93 plugins = g_slist_sort (plugins, (GCompareFunc) _sort_vpn_plugins);
94 return plugins;
95 }
96
97 typedef struct {
98 VpnImportCallback callback;
99 gpointer user_data;
100 GtkWindow *parent;
101 } ActionInfo;
102
103 static void
104 import_vpn_from_file_cb (GObject *source_object, GAsyncResult *result, gpointer user_data)
105 {
106 g_autofree gchar *filename = NULL;
107 g_autoptr(GFile) file = NULL;
108 ActionInfo *info = (ActionInfo *) user_data;
109 GtkFileDialog *dialog;
110 NMConnection *connection = NULL;
111 g_autoptr(GError) error = NULL;
112 GSList *iter;
113
114 dialog = GTK_FILE_DIALOG (source_object);
115 file = gtk_file_dialog_open_finish (dialog, result, &error);
116 if (!file) {
117 g_warning ("%s: didn't get a filename back from the chooser!", __func__);
118
119 return;
120 }
121
122 filename = g_file_get_path (file);
123
124 #if NM_CHECK_VERSION (1,40,0)
125 connection = nm_conn_wireguard_import (filename, &error);
126 #endif
127
128 for (iter = vpn_get_plugins (); !connection && iter; iter = iter->next) {
129 NMVpnEditorPlugin *plugin;
130
131 plugin = nm_vpn_plugin_info_get_editor_plugin (iter->data);
132 g_clear_error (&error);
133 connection = nm_vpn_editor_plugin_import (plugin, filename, &error);
134 }
135
136 if (!connection) {
137 GtkWidget *err_dialog;
138 g_autofree gchar *bname;
139
140 bname = g_path_get_basename (filename);
141 err_dialog = adw_message_dialog_new (info->parent,
142 _("Cannot Import VPN Connection"),
143 NULL);
144
145 adw_message_dialog_format_body (ADW_MESSAGE_DIALOG (err_dialog),
146 _("The file ā€œ%sā€ could not be read or does not contain recognized VPN connection information\n\nError: %s."),
147 bname, error ? error->message : "unknown error");
148 adw_message_dialog_add_response (ADW_MESSAGE_DIALOG (err_dialog),
149 "close", _("_Close"));
150
151 gtk_window_present (GTK_WINDOW (err_dialog));
152 }
153
154 info->callback (connection, info->user_data);
155 g_free (info);
156 }
157
158 void
159 vpn_import (GtkWindow *parent, VpnImportCallback callback, gpointer user_data)
160 {
161 g_autoptr(GFile) home_folder = NULL;
162 GtkFileDialog *dialog;
163 ActionInfo *info;
164
165 dialog = gtk_file_dialog_new ();
166 gtk_file_dialog_set_title (dialog, _("Select file to import"));
167 gtk_file_dialog_set_modal (dialog, TRUE);
168 home_folder = g_file_new_for_path (g_get_home_dir ());
169 gtk_file_dialog_set_initial_folder (dialog, home_folder);
170
171 info = g_malloc0 (sizeof (ActionInfo));
172 info->callback = callback;
173 info->user_data = user_data;
174 info->parent = parent;
175
176 gtk_file_dialog_open (dialog, parent, NULL, import_vpn_from_file_cb, info);
177 }
178