[seahorse/wip/nielsdg/headerbar-pkcs11] pkcs11: Pkcs11Properties: Use GtkTemplate



commit 895b2c1dbe56b4ba29e0cdbd94c02fdb79710187
Author: Niels De Graef <nielsdegraef gmail com>
Date:   Tue Jan 8 19:07:35 2019 +0100

    pkcs11: Pkcs11Properties: Use GtkTemplate
    
    Also stop using deprecated stuff like `GtkUiManager`

 data/seahorse.gresource.xml          |   1 +
 pkcs11/pkcs11-properties.vala        | 356 +++++++++++++++--------------------
 pkcs11/seahorse-pkcs11-properties.ui |  57 ++++++
 po/POTFILES.in                       |   1 +
 4 files changed, 214 insertions(+), 201 deletions(-)
---
diff --git a/data/seahorse.gresource.xml b/data/seahorse.gresource.xml
index 796ef5b5..b2d3f376 100644
--- a/data/seahorse.gresource.xml
+++ b/data/seahorse.gresource.xml
@@ -42,6 +42,7 @@
 
     <!-- PKCS#11 -->
     <file alias="seahorse-pkcs11-generate.ui" 
preprocess="xml-stripblanks">../pkcs11/seahorse-pkcs11-generate.ui</file>
+    <file alias="seahorse-pkcs11-properties.ui" 
preprocess="xml-stripblanks">../pkcs11/seahorse-pkcs11-properties.ui</file>
     <file alias="seahorse-pkcs11-request.ui" 
preprocess="xml-stripblanks">../pkcs11/seahorse-pkcs11-request.ui</file>
   </gresource>
 </gresources>
diff --git a/pkcs11/pkcs11-properties.vala b/pkcs11/pkcs11-properties.vala
index f3af5797..01b6adb3 100644
--- a/pkcs11/pkcs11-properties.vala
+++ b/pkcs11/pkcs11-properties.vala
@@ -3,6 +3,7 @@
  *
  * Copyright (C) 2008 Stefan Walter
  * Copyright (C) 2013 Red Hat Inc.
+ * Copyright (C) 2020 Niels De Graef
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU Lesser General Public License as
@@ -22,205 +23,158 @@
  * Author: Stef Walter <stefw redhat com>
  */
 
-namespace Seahorse {
-namespace Pkcs11 {
-
-public class Properties : Gtk.Window {
-       public Gck.Object object { construct; get; }
-
-       private string UI_STRING =
-               """<ui>
-                       <toolbar name='Toolbar'>
-                               <toolitem action='export-object'/>
-                               <toolitem action='delete-object'/>
-                               <separator name='MiddleSeparator' expand='true'/>
-                               <toolitem action='request-certificate'/>
-                       </toolbar>
-               </ui>""";
-
-       private Gtk.Box _content;
-       private Gcr.Viewer _viewer;
-       private GLib.Cancellable _cancellable;
-       private Gck.Object _request_key;
-       private Gtk.UIManager _ui_manager;
-       private Gtk.ActionGroup _actions;
-
-       public Properties(Gck.Object object,
-                         Gtk.Window window) {
-               GLib.Object(object: object, transient_for: window);
-       }
-
-       construct {
-               this._cancellable = new GLib.Cancellable();
-               set_default_size (400, 400);
-
-               this._content = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
-               this.add(this._content);
-               this._content.show();
-
-               this._viewer = Gcr.Viewer.new_scrolled();
-               this._content.add(this._viewer);
-               this._viewer.set_hexpand(true);
-               this._viewer.set_vexpand(true);
-               this._viewer.show();
-
-               /* ... */
-
-               this._actions = new Gtk.ActionGroup("Pkcs11Actions");
-               this._actions.set_translation_domain(Config.GETTEXT_PACKAGE);
-               this._actions.add_actions(UI_ACTIONS, this);
-               var action = this._actions.get_action("delete-object");
-               this.object.bind_property("deletable", action, "sensitive",
-                                         GLib.BindingFlags.SYNC_CREATE);
-               action = this._actions.get_action("export-object");
-               this.object.bind_property("exportable", action, "sensitive",
-                                         GLib.BindingFlags.SYNC_CREATE);
-               var request = this._actions.get_action("request-certificate");
-               request.is_important = true;
-               request.visible = false;
-
-               this._ui_manager = new Gtk.UIManager();
-               this._ui_manager.insert_action_group(this._actions, 0);
-               this._ui_manager.add_widget.connect((widget) => {
-                       if (!(widget is Gtk.Toolbar))
-                               return;
-
-                       this._content.pack_start(widget, false, true, 0);
-                       this._content.reorder_child(widget, 0);
-
-                       widget.get_style_context().add_class(Gtk.STYLE_CLASS_PRIMARY_TOOLBAR);
-                       widget.reset_style();
-                       widget.show();
-               });
-               try {
-                       this._ui_manager.add_ui_from_string(UI_STRING, -1);
-               } catch (GLib.Error err) {
-                       GLib.critical ("%s", err.message);
-               }
-               this._ui_manager.ensure_update();
-
-               this.object.notify["label"].connect(() => { this.update_label(); });
-               this.update_label();
-               this.add_renderer_for_object(this.object);
-               this.check_certificate_request_capable(this.object);
-
-               GLib.Object? partner;
-               this.object.get("partner", out partner);
-               if (partner != null) {
-                       this.add_renderer_for_object(partner);
-                       this.check_certificate_request_capable(partner);
-               }
-
-               GLib.List<Exporter> exporters;
-               if (this.object is Exportable)
-                       exporters = ((Exportable)this.object).create_exporters(ExporterType.ANY);
-
-               var export = this._actions.get_action("export-object");
-               export.set_visible(exporters != null);
-
-               this._viewer.grab_focus();
-       }
-
-       public override void dispose() {
-               this._cancellable.cancel();
-               base.dispose();
-       }
-
-       private void update_label() {
-               string? label;
-               string? description;
-               this.object.get("label", out label, "description", out description);
-               if (label == null || label == "")
-                       label = _("Unnamed");
-               this.set_title("%s - %s".printf(label, description));
-       }
-
-       private void add_renderer_for_object(GLib.Object object) {
-               Gck.Attributes? attributes = null;
-               string? label = null;
-
-               object.get("label", &label, "attributes", &attributes);
-               if (attributes != null) {
-                       var renderer = Gcr.Renderer.create(label, attributes);
-                       if (renderer != null) {
-                               object.bind_property("label", renderer, "label",
-                                                    GLib.BindingFlags.DEFAULT);
-                               object.bind_property("attributes", renderer, "attributes",
-                                                    GLib.BindingFlags.DEFAULT);
-
-                               if (renderer.get_class().find_property("object") != null)
-                                       renderer.set("object", object);
-
-                               this._viewer.add_renderer(renderer);
-                       }
-               }
-       }
-
-       private void on_export_certificate(Gtk.Action action) {
-               GLib.List<GLib.Object> objects = null;
-               objects.append(this.object);
-               try {
-                       Exportable.export_to_prompt_wait(objects, this);
-               } catch (GLib.Error err) {
-                       Util.show_error(this, _("Failed to export certificate"), err.message);
-               }
-       }
-
-       private void on_delete_objects(Gtk.Action action) {
-               GLib.Object? partner;
-               this.object.get("partner", out partner);
-
-               Deleter deleter;
-               if (partner != null || this.object is PrivateKey) {
-                       deleter = new KeyDeleter((Gck.Object)this.object);
-                       if (!deleter.add_object(partner))
-                               GLib.assert_not_reached();
-               } else {
-                       deleter = new Deleter((Gck.Object)this.object);
-               }
-
-               if (deleter.prompt(this)) {
-                       deleter.delete.begin(this._cancellable, (obj, res) => {
-                               try {
-                                       if (deleter.delete.end(res))
-                                               this.destroy();
-                               } catch (GLib.Error err) {
-                                       Util.show_error(this, _("Couldn’t delete"), err.message);
-                               }
-                       });
-               }
-       }
-
-       private void on_request_certificate(Gtk.Action action) {
-               Request.prompt(this, this._request_key);
-       }
-
-       private const Gtk.ActionEntry[] UI_ACTIONS = {
-                { "export-object", "document-save-as", N_("_Export"), "",
-                 N_("Export the certificate"), on_export_certificate },
-               { "delete-object", "edit-delete", N_("_Delete"), "<Ctrl>Delete",
-                 N_("Delete this certificate or key"), on_delete_objects },
-               { "request-certificate", null, N_("Request _Certificate"), null,
-                 N_("Create a certificate request file for this key"), on_request_certificate },
-       };
-
-       private void check_certificate_request_capable(GLib.Object object) {
-               if (!(object is PrivateKey))
-                       return;
-
-               Gcr.CertificateRequest.capable_async.begin((PrivateKey)object, this._cancellable, (obj, res) 
=> {
-                       try {
-                               if (Gcr.CertificateRequest.capable_async.end(res)) {
-                                       var request = this._actions.get_action("request-certificate");
-                                       request.set_visible(true);
-                                       this._request_key = (PrivateKey)object;
-                               }
-                       } catch (GLib.Error err) {
-                               GLib.message("couldn't check capabilities of private key: %s", err.message);
-                       }
-               });
-       }
-}
-
-}
+[GtkTemplate (ui = "/org/gnome/Seahorse/seahorse-pkcs11-properties.ui")]
+public class Seahorse.Pkcs11.Properties : Gtk.Dialog {
+
+    public Gck.Object object { construct; get; }
+
+    /* [GtkChild] */
+    /* private Gtk.HeaderBar header; */
+    [GtkChild]
+    private Gtk.Button delete_button;
+    [GtkChild]
+    private Gtk.Button export_button;
+    [GtkChild]
+    private Gtk.Button request_certificate_button;
+
+    [GtkChild]
+    private Gtk.Box content;
+
+    private Gcr.Viewer _viewer;
+    private GLib.Cancellable _cancellable;
+    private Gck.Object _request_key;
+
+    public Properties(Gck.Object object, Gtk.Window window) {
+        GLib.Object(object: object, transient_for: window);
+    }
+
+    construct {
+        this._cancellable = new GLib.Cancellable();
+
+        this._viewer = Gcr.Viewer.new_scrolled();
+        this.content.pack_start(this._viewer);
+        this._viewer.set_hexpand(true);
+        this._viewer.set_vexpand(true);
+        this._viewer.show();
+
+        /* ... */
+
+        this.object.bind_property("deletable", this.delete_button, "visible",
+                                  BindingFlags.SYNC_CREATE);
+        this.object.bind_property("exportable", this.export_button, "visible",
+                                  BindingFlags.SYNC_CREATE);
+
+        object.notify["label"].connect(() => { update_label(); });
+        update_label();
+        add_renderer_for_object(this.object);
+        check_certificate_request_capable(this.object);
+
+        GLib.Object? partner;
+        this.object.get("partner", out partner);
+        if (partner != null) {
+            add_renderer_for_object(partner);
+            check_certificate_request_capable(partner);
+        }
+
+        GLib.List<Exporter> exporters = null;
+        if (this.object is Exportable)
+            exporters = ((Exportable)this.object).create_exporters(ExporterType.ANY);
+
+        this.export_button.set_visible(exporters != null);
+
+        this._viewer.grab_focus();
+    }
+
+    public override void dispose() {
+        this._cancellable.cancel();
+        base.dispose();
+    }
+
+    private void update_label() {
+        string? label;
+        string? description;
+        this.object.get("label", out label, "description", out description);
+        if (label == null || label == "")
+            label = _("Unnamed");
+        this.title = "%s - %s".printf(label, description);
+    }
+
+    private void add_renderer_for_object(GLib.Object object) {
+        Gck.Attributes? attributes = null;
+        string? label = null;
+
+        object.get("label", &label, "attributes", &attributes);
+        if (attributes != null) {
+            var renderer = Gcr.Renderer.create(label, attributes);
+            if (renderer != null) {
+                object.bind_property("label", renderer, "label",
+                                     GLib.BindingFlags.DEFAULT);
+                object.bind_property("attributes", renderer, "attributes",
+                                     GLib.BindingFlags.DEFAULT);
+
+                if (renderer.get_class().find_property("object") != null)
+                    renderer.set("object", object);
+
+                this._viewer.add_renderer(renderer);
+            }
+        }
+    }
+
+    [GtkCallback]
+    private void on_export_button_clicked(Gtk.Button export_button) {
+        GLib.List<GLib.Object> objects = null;
+        objects.append(this.object);
+        try {
+            Exportable.export_to_prompt_wait(objects, this);
+        } catch (GLib.Error err) {
+            Util.show_error(this, _("Failed to export certificate"), err.message);
+        }
+    }
+
+    [GtkCallback]
+    private void on_delete_button_clicked(Gtk.Button delete_button) {
+        GLib.Object? partner;
+        this.object.get("partner", out partner);
+
+        Deleter deleter;
+        if (partner != null || this.object is PrivateKey) {
+            deleter = new KeyDeleter((Gck.Object)this.object);
+            if (!deleter.add_object(partner))
+                GLib.assert_not_reached();
+        } else {
+            deleter = new Deleter((Gck.Object)this.object);
+        }
+
+        if (deleter.prompt(this)) {
+            deleter.delete.begin(this._cancellable, (obj, res) => {
+                try {
+                    if (deleter.delete.end(res))
+                        this.destroy();
+                } catch (GLib.Error err) {
+                    Util.show_error(this, _("Couldn’t delete"), err.message);
+                }
+            });
+        }
+    }
+
+    [GtkCallback]
+    private void on_request_certificate_button_clicked(Gtk.Button request_button) {
+        Request.prompt(this, this._request_key);
+    }
+
+    private void check_certificate_request_capable(GLib.Object object) {
+        if (!(object is PrivateKey))
+            return;
+
+        Gcr.CertificateRequest.capable_async.begin((PrivateKey)object, this._cancellable, (obj, res) => {
+            try {
+                if (Gcr.CertificateRequest.capable_async.end(res)) {
+                    this.request_certificate_button.set_visible(true);
+                    this._request_key = (PrivateKey)object;
+                }
+            } catch (GLib.Error err) {
+                GLib.message("couldn't check capabilities of private key: %s", err.message);
+            }
+        });
+    }
 }
diff --git a/pkcs11/seahorse-pkcs11-properties.ui b/pkcs11/seahorse-pkcs11-properties.ui
new file mode 100644
index 00000000..0c7b7838
--- /dev/null
+++ b/pkcs11/seahorse-pkcs11-properties.ui
@@ -0,0 +1,57 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <requires lib="gtk+" version="3.22"/>
+  <template class="SeahorsePkcs11Properties" parent="GtkDialog">
+    <property name="width_request">400</property>
+    <property name="height_request">400</property>
+    <property name="can_focus">False</property>
+    <child internal-child="vbox">
+      <object class="GtkBox" id="content">
+        <property name="visible">True</property>
+        <property name="orientation">vertical</property>
+        <property name="can_focus">False</property>
+        <property name="spacing">12</property>
+        <child>
+          <object class="GtkBox">
+            <property name="visible">True</property>
+            <property name="orientation">horizontal</property>
+            <property name="spacing">6</property>
+            <child>
+              <object class="GtkButton" id="delete_button">
+                <property name="visible">True</property>
+                <property name="label" translatable="yes">Delete</property>
+                <property name="tooltip_text" translatable="yes">Delete this certificate or key</property>
+                <signal name="clicked" handler="on_delete_button_clicked"/>
+                <style>
+                  <class name="destructive-action"/>
+                </style>
+              </object>
+            </child>
+            <child>
+              <object class="GtkButton" id="export_button">
+                <property name="visible">True</property>
+                <property name="label" translatable="yes">Export</property>
+                <property name="tooltip_text" translatable="yes">Export the certificate</property>
+                <signal name="clicked" handler="on_export_button_clicked"/>
+              </object>
+            </child>
+            <child>
+              <object class="GtkButton" id="request_certificate_button">
+                <property name="visible">False</property>
+                <property name="label" translatable="yes">Request Certificate</property>
+                <property name="tooltip_text" translatable="yes">Create a certificate request file for this 
key</property>
+                <signal name="clicked" handler="on_request_certificate_button_clicked"/>
+              </object>
+              <packing>
+                <property name="pack_type">end</property>
+              </packing>
+            </child>
+          </object>
+          <packing>
+            <property name="pack_type">end</property>
+          </packing>
+        </child>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 8e745f19..f9941535 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -89,6 +89,7 @@ pkcs11/pkcs11-request.vala
 pkcs11/pkcs11-token.vala
 pkcs11/seahorse-pkcs11-backend.c
 pkcs11/seahorse-pkcs11-generate.ui
+pkcs11/seahorse-pkcs11-properties.ui
 pkcs11/seahorse-pkcs11-request.ui
 src/application.vala
 src/import-dialog.vala


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