[epiphany/wip/exalm/gtk4: 2/4] Port away from GcrCertificateWidget




commit 633fd8ee24236c4fdd385e9ccf3409a8ac56950e
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Sun Sep 4 20:28:55 2022 +0400

    Port away from GcrCertificateWidget

 lib/widgets/ephy-certificate-dialog.c | 186 ++++++++++++++++++++++++++++++++--
 meson.build                           |   2 +-
 org.gnome.Epiphany.Canary.json.in     |   1 -
 org.gnome.Epiphany.json               |  18 ----
 4 files changed, 181 insertions(+), 26 deletions(-)
---
diff --git a/lib/widgets/ephy-certificate-dialog.c b/lib/widgets/ephy-certificate-dialog.c
index dc6c0920a..f832346ca 100644
--- a/lib/widgets/ephy-certificate-dialog.c
+++ b/lib/widgets/ephy-certificate-dialog.c
@@ -1,6 +1,8 @@
 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /*
  *  Copyright © 2012 Igalia S.L.
+ *  Copyright © 2021 Collabora Ltd.
+ *  Copyright © Corentin Noël <corentin noel collabora com>
  *
  *  This file is part of Epiphany.
  *
@@ -23,9 +25,10 @@
 
 #include "ephy-lib-type-builtins.h"
 
+#include <adwaita.h>
 #define GCK_API_SUBJECT_TO_CHANGE
 #define GCR_API_SUBJECT_TO_CHANGE
-#include <gcr-4/gcr-gtk4/gcr-certificate-widget.h>
+#include <gcr-4/gcr/gcr.h>
 #include <glib/gi18n.h>
 
 /**
@@ -60,6 +63,172 @@ struct _EphyCertificateDialog {
 
 G_DEFINE_TYPE (EphyCertificateDialog, ephy_certificate_dialog, GTK_TYPE_DIALOG)
 
+static char *
+bytes_to_display (GBytes *bytes)
+{
+  const char *hexc = "0123456789ABCDEF";
+  GString *result;
+  const char *input;
+  gsize read_bytes;
+  gsize remaining_bytes;
+  guchar j;
+
+  g_return_val_if_fail (bytes != NULL, NULL);
+
+  input = g_bytes_get_data (bytes, &remaining_bytes);
+  result = g_string_sized_new (remaining_bytes * 2 + 1);
+
+  for (read_bytes = 0; read_bytes < remaining_bytes; read_bytes++) {
+    if (read_bytes && (read_bytes % 1) == 0)
+      g_string_append_c (result, ' ');
+
+    j = *(input) >> 4 & 0xf;
+    g_string_append_c (result, hexc[j]);
+
+    j = *(input++) & 0xf;
+    g_string_append_c (result, hexc[j]);
+  }
+
+  return g_string_free (result, FALSE);
+}
+
+static GtkWidget *
+create_section_row (GcrCertificateField *field,
+                    GtkSizeGroup        *size_group)
+{
+  GtkWidget *row, *box, *label, *value;
+  GValue val = G_VALUE_INIT;
+  GType value_type;
+
+  g_return_val_if_fail (GCR_IS_CERTIFICATE_FIELD (field), NULL);
+
+  row = gtk_list_box_row_new ();
+  gtk_widget_set_focusable (row, FALSE);
+  gtk_list_box_row_set_activatable (GTK_LIST_BOX_ROW (row), FALSE);
+
+  box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
+  gtk_widget_set_margin_start (box, 12);
+  gtk_widget_set_margin_end (box, 12);
+  gtk_widget_set_margin_top (box, 12);
+  gtk_widget_set_margin_bottom (box, 12);
+  gtk_list_box_row_set_child (GTK_LIST_BOX_ROW (row), box);
+
+  label = gtk_label_new (gcr_certificate_field_get_label (field));
+  gtk_label_set_xalign (GTK_LABEL (label), 0.0);
+  gtk_label_set_yalign (GTK_LABEL (label), 0.0);
+  gtk_box_append (GTK_BOX (box), label);
+
+  value = gtk_label_new (NULL);
+  gtk_label_set_xalign (GTK_LABEL (value), 1.0);
+  gtk_label_set_yalign (GTK_LABEL (value), 0.0);
+  gtk_label_set_selectable (GTK_LABEL (value), TRUE);
+  gtk_label_set_wrap (GTK_LABEL (value), TRUE);
+  gtk_label_set_wrap_mode (GTK_LABEL (value), PANGO_WRAP_WORD_CHAR);
+  gtk_widget_set_hexpand (value, TRUE);
+  gtk_box_append (GTK_BOX (box), value);
+
+  value_type = gcr_certificate_field_get_value_type (field);
+  g_value_init (&val, value_type);
+  gcr_certificate_field_get_value (field, &val);
+
+  if (g_type_is_a (value_type, G_TYPE_STRING)) {
+    gtk_label_set_label (GTK_LABEL (value), g_value_get_string (&val));
+  } else if (g_type_is_a (value_type, G_TYPE_STRV)) {
+    g_autofree char *lbl = g_strjoinv ("\n", (GStrv)g_value_get_boxed (&val));
+
+    gtk_label_set_label (GTK_LABEL (value), lbl);
+    gtk_label_set_justify (GTK_LABEL (value), GTK_JUSTIFY_RIGHT);
+  } else if (g_type_is_a (value_type, G_TYPE_BYTES)) {
+    GBytes *bytes = g_value_get_boxed (&val);
+    g_autofree char *lbl = bytes_to_display (bytes);
+
+    gtk_label_set_label (GTK_LABEL (value), lbl);
+    gtk_widget_add_css_class (value, "monospace");
+  }
+
+  g_value_unset (&val);
+
+  gtk_size_group_add_widget (size_group, label);
+
+  return row;
+}
+
+static GtkWidget *
+create_section (GcrCertificateSection *section)
+{
+  GtkWidget *group, *listbox;
+  GtkSizeGroup *size_group;
+
+  group = adw_preferences_group_new ();
+  g_object_bind_property (section, "label", group, "title", G_BINDING_SYNC_CREATE);
+
+  listbox = gtk_list_box_new ();
+  gtk_list_box_set_selection_mode (GTK_LIST_BOX (listbox), GTK_SELECTION_NONE);
+  gtk_widget_add_css_class (listbox, "boxed-list");
+  adw_preferences_group_add (ADW_PREFERENCES_GROUP (group), listbox);
+
+  size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
+
+  gtk_list_box_bind_model (GTK_LIST_BOX (listbox),
+                           gcr_certificate_section_get_fields (section),
+                           (GtkListBoxCreateWidgetFunc)create_section_row,
+                           size_group, g_object_unref);
+
+  return group;
+}
+
+static void
+reveal_clicked_cb (GtkWidget *button,
+                   GtkWidget *secondary_info)
+{
+  gtk_widget_hide (button);
+  gtk_widget_show (secondary_info);
+}
+
+static GtkWidget *
+create_page (GcrCertificate *certificate)
+{
+  GtkWidget *box, *primary_info, *secondary_info, *reveal_button;
+  GList *elements, *l;
+  GtkSizeGroup *size_group;
+
+  box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
+
+  primary_info = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
+  gtk_box_append (GTK_BOX (box), primary_info);
+
+  reveal_button = gtk_button_new_from_icon_name ("view-more-horizontal-symbolic");
+  gtk_widget_set_halign (reveal_button, GTK_ALIGN_CENTER);
+  gtk_box_append (GTK_BOX (box), reveal_button);
+
+  secondary_info = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
+  gtk_widget_hide (secondary_info);
+  gtk_box_append (GTK_BOX (box), secondary_info);
+
+  size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
+  gtk_size_group_add_widget (size_group, primary_info);
+  gtk_size_group_add_widget (size_group, secondary_info);
+
+  elements = gcr_certificate_get_interface_elements (certificate);
+
+  for (l = elements; l != NULL; l = l->next) {
+    GcrCertificateSection *section = l->data;
+    GtkWidget *widget = create_section (section);
+
+    if (gcr_certificate_section_get_flags (section) & GCR_CERTIFICATE_SECTION_IMPORTANT)
+      gtk_box_append (GTK_BOX (primary_info), widget);
+    else
+      gtk_box_append (GTK_BOX (secondary_info), widget);
+  }
+
+  g_list_free_full (elements, (GDestroyNotify)g_object_unref);
+
+  g_signal_connect (reveal_button, "clicked",
+                    G_CALLBACK (reveal_clicked_cb), secondary_info);
+
+  return box;
+}
+
 static void
 ephy_certificate_dialog_set_address (EphyCertificateDialog *dialog,
                                      const char            *address)
@@ -76,18 +245,18 @@ ephy_certificate_dialog_set_certificate (EphyCertificateDialog *dialog,
 {
   GcrCertificate *simple_certificate;
   GByteArray *certificate_data;
-  GtkWidget *certificate_widget;
+  GtkWidget *page;
 
   g_object_get (certificate, "certificate", &certificate_data, NULL);
   simple_certificate = gcr_simple_certificate_new ((const guchar *)certificate_data->data,
                                                    certificate_data->len);
   g_byte_array_unref (certificate_data);
 
-  certificate_widget = GTK_WIDGET (gcr_certificate_widget_new (simple_certificate));
-  gtk_widget_set_vexpand (certificate_widget, TRUE);
+  page = create_page (simple_certificate);
+  gtk_widget_set_vexpand (page, TRUE);
   g_object_unref (simple_certificate);
 
-  gtk_box_append (GTK_BOX (dialog->box), certificate_widget);
+  gtk_box_append (GTK_BOX (dialog->box), page);
 }
 
 static char *
@@ -279,6 +448,7 @@ ephy_certificate_dialog_init (EphyCertificateDialog *dialog)
   GtkWidget *grid;
   GtkWidget *scrolled_window;
   GtkWidget *content_area;
+  GtkWidget *clamp;
 
   gtk_window_set_default_size (GTK_WINDOW (dialog), -1, 500);
 
@@ -316,11 +486,14 @@ ephy_certificate_dialog_init (EphyCertificateDialog *dialog)
   gtk_widget_set_margin_end (dialog->box, 12);
   gtk_box_append (GTK_BOX (dialog->box), grid);
 
+  clamp = adw_clamp_new ();
+  adw_clamp_set_child (ADW_CLAMP (clamp), dialog->box);
+
   scrolled_window = gtk_scrolled_window_new ();
   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
                                   GTK_POLICY_NEVER,
                                   GTK_POLICY_AUTOMATIC);
-  gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scrolled_window), dialog->box);
+  gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scrolled_window), clamp);
 
   content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
   gtk_box_append (GTK_BOX (content_area), scrolled_window);
@@ -345,6 +518,7 @@ ephy_certificate_dialog_new (GtkWindow            *parent,
                                      "tls-errors", tls_errors,
                                      "modal", TRUE,
                                      "use-header-bar", TRUE,
+                                     "width-request", 500,
                                      NULL));
   if (parent)
     gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
diff --git a/meson.build b/meson.build
index fe49f38a9..029cfb564 100644
--- a/meson.build
+++ b/meson.build
@@ -82,7 +82,7 @@ conf.set('GLIB_VERSION_MIN_REQUIRED', 'GLIB_VERSION_2_70')
 conf.set('GLIB_VERSION_MAX_ALLOWED', 'GLIB_VERSION_2_70')
 
 cairo_dep = dependency('cairo', version: '>= 1.2')
-gcr_dep = dependency('gcr-4-gtk4', version: '>= 3.9.0')
+gcr_dep = dependency('gcr-4', version: '>= 3.9.0')
 gdk_pixbuf_dep = dependency('gdk-pixbuf-2.0', version: '>= 2.36.5')
 gio_dep = dependency('gio-2.0', version: glib_requirement)
 gio_unix_dep = dependency('gio-unix-2.0', version: glib_requirement)
diff --git a/org.gnome.Epiphany.Canary.json.in b/org.gnome.Epiphany.Canary.json.in
index 9a81cf746..5dfb7fd9c 100644
--- a/org.gnome.Epiphany.Canary.json.in
+++ b/org.gnome.Epiphany.Canary.json.in
@@ -42,7 +42,6 @@
             "config-opts" : [
                 "-Dintrospection=false",
                 "-Dvapi=false",
-                "-Dgtk3=false",
                 "-Dgtk_doc=false",
                 "-Dssh_agent=false",
                 "-Dsystemd=disabled"
diff --git a/org.gnome.Epiphany.json b/org.gnome.Epiphany.json
index 8ac8d797b..b5e0849cf 100644
--- a/org.gnome.Epiphany.json
+++ b/org.gnome.Epiphany.json
@@ -105,24 +105,6 @@
                 }
             ]
         },
-        {
-            "name" : "gcr",
-            "buildsystem" : "meson",
-            "config-opts" : [
-                "-Dintrospection=false",
-                "-Dvapi=false",
-                "-Dgtk3=false",
-                "-Dgtk_doc=false",
-                "-Dssh_agent=false",
-                "-Dsystemd=disabled"
-            ],
-            "sources" : [
-                {
-                    "type" : "git",
-                    "url" : "https://gitlab.gnome.org/GNOME/gcr.git";
-                }
-            ]
-        },
         {
             "name" : "epiphany",
             "buildsystem" : "meson",


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