[NetworkManager-vpnc/lr/gtk-4.0: 14/15] properties: use NmaCertChooser




commit 4f0079122dd1d56197a41f65e18f98dbb5c183d1
Author: Lubomir Rintel <lkundrak v3 sk>
Date:   Wed Nov 3 18:16:19 2021 +0100

    properties: use NmaCertChooser
    
    The GtkFileChooserButton is gone from Gtk4 and this is almost a good
    replacement.

 properties/nm-vpnc-dialog.ui |  24 ++--------
 properties/nm-vpnc-editor.c  | 108 +++++--------------------------------------
 2 files changed, 17 insertions(+), 115 deletions(-)
---
diff --git a/properties/nm-vpnc-dialog.ui b/properties/nm-vpnc-dialog.ui
index 2cfa39f..6c83e8a 100644
--- a/properties/nm-vpnc-dialog.ui
+++ b/properties/nm-vpnc-dialog.ui
@@ -200,29 +200,16 @@ config: IKE Authmode hybrid</property>
       </packing>
     </child>
     <child>
-      <object class="GtkLabel" id="cafile_label">
+      <object class="NmaCertChooser" id="ca_chooser">
+        <property name="flags">13</property>
+        <property name="title">CA</property>
         <property name="visible">True</property>
-        <property name="can_focus">False</property>
-        <property name="xalign">1</property>
-        <property name="label" translatable="yes">CA _File</property>
-        <property name="use_underline">True</property>
-        <property name="mnemonic_widget">ca_file_chooser</property>
+        <property name="can-focus">False</property>
       </object>
       <packing>
         <property name="left_attach">0</property>
         <property name="top_attach">8</property>
-      </packing>
-    </child>
-    <child>
-      <object class="GtkFileChooserButton" id="ca_file_chooser">
-        <property name="visible">True</property>
-        <property name="can_focus">False</property>
-        <property name="tooltip_text" translatable="yes">CA certificate in PEM format
-config: CA-File</property>
-      </object>
-      <packing>
-        <property name="left_attach">1</property>
-        <property name="top_attach">8</property>
+        <property name="width">2</property>
       </packing>
     </child>
     <child>
@@ -282,7 +269,6 @@ config: CA-File</property>
       <widget name="label3"/>
       <widget name="label24"/>
       <widget name="label4"/>
-      <widget name="cafile_label"/>
     </widgets>
   </object>
   <object class="GtkSizeGroup" id="advanced_labels">
diff --git a/properties/nm-vpnc-editor.c b/properties/nm-vpnc-editor.c
index 0069513..bfed98a 100644
--- a/properties/nm-vpnc-editor.c
+++ b/properties/nm-vpnc-editor.c
@@ -28,6 +28,7 @@
 
 #include "nm-vpnc-editor.h"
 
+#include <nma-cert-chooser.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <errno.h>
@@ -103,17 +104,14 @@ hybrid_toggled_cb (GtkWidget *widget, gpointer user_data)
 {
        VpncEditorPrivate *priv = VPNC_EDITOR_GET_PRIVATE (user_data);
        gboolean enabled = FALSE;
-       GtkWidget *cafile_label, *ca_file_chooser;
+       GtkWidget *ca_chooser;
 
-       cafile_label = GTK_WIDGET (gtk_builder_get_object (priv->builder, "cafile_label"));
-       g_return_if_fail (cafile_label);
-       ca_file_chooser = GTK_WIDGET (gtk_builder_get_object (priv->builder, "ca_file_chooser"));
-       g_return_if_fail (ca_file_chooser);
+       ca_chooser = GTK_WIDGET (gtk_builder_get_object (priv->builder, "ca_chooser"));
+       g_return_if_fail (ca_chooser);
 
        enabled = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
 
-       gtk_widget_set_sensitive (cafile_label, enabled);
-       gtk_widget_set_sensitive (ca_file_chooser, enabled);
+       gtk_widget_set_sensitive (ca_chooser, enabled);
 
        stuff_changed_cb (widget, user_data);
 }
@@ -533,71 +531,6 @@ advanced_button_clicked_cb (GtkWidget *button, gpointer user_data)
        gtk_widget_show (priv->advanced_dialog);
 }
 
-static const char *
-find_tag (const char *tag, const char *buf, gsize len)
-{
-       gsize i, taglen;
-
-       taglen = strlen (tag);
-       if (len < taglen)
-               return NULL;
-
-       for (i = 0; i < len - taglen + 1; i++) {
-               if (memcmp (buf + i, tag, taglen) == 0)
-                       return buf + i;
-       }
-       return NULL;
-}
-
-static const char *pem_cert_begin = "-----BEGIN CERTIFICATE-----";
-
-static gboolean
-cert_filter (const GtkFileFilterInfo *filter_info, gpointer data)
-{
-       char *contents = NULL, *p, *ext;
-       gsize bytes_read = 0;
-       gboolean show = FALSE;
-       struct stat statbuf;
-
-       if (!filter_info->filename)
-               return FALSE;
-
-       p = strrchr (filter_info->filename, '.');
-       if (!p)
-               return FALSE;
-
-       ext = g_ascii_strdown (p, -1);
-       if (!ext)
-               return FALSE;
-
-       if (strcmp (ext, ".pem") && strcmp (ext, ".crt") && strcmp (ext, ".cer")) {
-               g_free (ext);
-               return FALSE;
-       }
-       g_free (ext);
-
-       /* Ignore files that are really large */
-       if (!stat (filter_info->filename, &statbuf)) {
-               if (statbuf.st_size > 500000)
-                       return FALSE;
-       }
-
-       if (!g_file_get_contents (filter_info->filename, &contents, &bytes_read, NULL))
-               return FALSE;
-
-       if (bytes_read < 400)  /* needs to be lower? */
-               goto out;
-
-       if (find_tag (pem_cert_begin, (const char *) contents, bytes_read)) {
-               show = TRUE;
-               goto out;
-       }
-
-out:
-       g_free (contents);
-       return show;
-}
-
 static gboolean
 init_plugin_ui (VpncEditor *self,
                 NMConnection *connection,
@@ -612,7 +545,6 @@ init_plugin_ui (VpncEditor *self,
        GtkTreeIter iter;
        const char *value = NULL;
        gboolean enabled = FALSE;
-       GtkFileFilter *filter;
 
        if (connection) {
                s_con = nm_connection_get_setting_connection (connection);
@@ -792,27 +724,11 @@ init_plugin_ui (VpncEditor *self,
        g_signal_connect (G_OBJECT (widget), "toggled", G_CALLBACK (hybrid_toggled_cb), self);
 
        /* CA Certificate */
-       widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "ca_file_chooser"));
-       g_return_val_if_fail (widget != NULL, FALSE);
-       gtk_widget_set_sensitive (widget, enabled);
-       gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (widget), TRUE);
-       gtk_file_chooser_button_set_title (GTK_FILE_CHOOSER_BUTTON (widget),
-                                          _("Choose a Certificate Authority (CA) certificate…"));
-
-       filter = gtk_file_filter_new ();
-       gtk_file_filter_add_custom (filter, GTK_FILE_FILTER_FILENAME, cert_filter, NULL, NULL);
-       gtk_file_filter_set_name (filter, _("PEM certificates (*.pem, *.crt, *.cer)"));
-       gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (widget), filter);
-
-       if (s_vpn) {
-               value = nm_setting_vpn_get_data_item (s_vpn, NM_VPNC_KEY_CA_FILE);
-               if (value && strlen (value))
-                       gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (widget), value);
-       }
-       g_signal_connect (G_OBJECT (widget), "file-set", G_CALLBACK (stuff_changed_cb), self);
-
-       widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "cafile_label"));
-       g_return_val_if_fail (widget != NULL, FALSE);
+       widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "ca_chooser"));
+       g_return_val_if_fail (widget, FALSE);
+       nma_cert_chooser_add_to_size_group (NMA_CERT_CHOOSER (widget),
+               GTK_SIZE_GROUP (gtk_builder_get_object (priv->builder, "labels")));
+       g_signal_connect (G_OBJECT (widget), "changed", G_CALLBACK (stuff_changed_cb), self);
        gtk_widget_set_sensitive (widget, enabled);
 
        /* Local port */
@@ -958,8 +874,8 @@ update_connection (NMVpnEditor *editor,
        if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget))) {
                nm_setting_vpn_add_data_item (s_vpn, NM_VPNC_KEY_AUTHMODE, "hybrid");
 
-               widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "ca_file_chooser"));
-               str = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (widget));
+               widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "ca_chooser"));
+               str = nma_cert_chooser_get_cert (NMA_CERT_CHOOSER (widget), NULL);
                if (str && str[0])
                        nm_setting_vpn_add_data_item (s_vpn, NM_VPNC_KEY_CA_FILE, str);
        }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]