[seahorse/wip/nielsgd/expires-gtktemplate] pgp: Port expiry dialog to GtkTemplate



commit e10c26fbb868b7415065097ac811731ef2dab630
Author: Niels De Graef <nielsdegraef gmail com>
Date:   Sun Feb 16 11:26:50 2020 +0100

    pgp: Port expiry dialog to GtkTemplate

 data/seahorse.gresource.xml          |   2 +-
 pgp/meson.build                      |   2 +-
 pgp/seahorse-expires.ui              | 141 ----------------------
 pgp/seahorse-gpgme-expires-dialog.c  | 223 +++++++++++++++++++++++++++++++++++
 pgp/seahorse-gpgme-expires-dialog.h  |  33 ++++++
 pgp/seahorse-gpgme-expires-dialog.ui |  57 +++++++++
 pgp/seahorse-gpgme-expires.c         | 140 ----------------------
 pgp/seahorse-pgp-key-properties.c    |  19 ++-
 po/POTFILES.in                       |   4 +-
 9 files changed, 331 insertions(+), 290 deletions(-)
---
diff --git a/data/seahorse.gresource.xml b/data/seahorse.gresource.xml
index 576f1026..7c9ac10d 100644
--- a/data/seahorse.gresource.xml
+++ b/data/seahorse.gresource.xml
@@ -27,9 +27,9 @@
     <file alias="seahorse-ssh-upload.ui" preprocess="xml-stripblanks">../ssh/seahorse-ssh-upload.ui</file>
 
     <!-- PGP -->
-    <file alias="seahorse-expires.ui" preprocess="xml-stripblanks">../pgp/seahorse-expires.ui</file>
     <file alias="seahorse-gpgme-add-subkey.ui" 
preprocess="xml-stripblanks">../pgp/seahorse-gpgme-add-subkey.ui</file>
     <file alias="seahorse-gpgme-add-uid.ui" 
preprocess="xml-stripblanks">../pgp/seahorse-gpgme-add-uid.ui</file>
+    <file alias="seahorse-gpgme-expires-dialog.ui" 
preprocess="xml-stripblanks">../pgp/seahorse-gpgme-expires-dialog.ui</file>
     <file alias="seahorse-gpgme-generate-dialog.ui" 
preprocess="xml-stripblanks">../pgp/seahorse-gpgme-generate-dialog.ui</file>
     <file alias="seahorse-gpgme-sign-dialog.ui" 
preprocess="xml-stripblanks">../pgp/seahorse-gpgme-sign-dialog.ui</file>
     <file alias="seahorse-keyserver-results.ui" 
preprocess="xml-stripblanks">../pgp/seahorse-keyserver-results.ui</file>
diff --git a/pgp/meson.build b/pgp/meson.build
index d5515860..cc9a5bdd 100644
--- a/pgp/meson.build
+++ b/pgp/meson.build
@@ -5,7 +5,7 @@ pgp_sources = files(
   'seahorse-gpgme-add-subkey.c',
   'seahorse-gpgme-add-uid.c',
   'seahorse-gpgme-data.c',
-  'seahorse-gpgme-expires.c',
+  'seahorse-gpgme-expires-dialog.c',
   'seahorse-gpgme-exporter.c',
   'seahorse-gpgme-generate-dialog.c',
   'seahorse-gpgme-key.c',
diff --git a/pgp/seahorse-gpgme-expires-dialog.c b/pgp/seahorse-gpgme-expires-dialog.c
new file mode 100644
index 00000000..4a5b5ea4
--- /dev/null
+++ b/pgp/seahorse-gpgme-expires-dialog.c
@@ -0,0 +1,223 @@
+/*
+ * Seahorse
+ *
+ * Copyright (C) 2005 Stefan Walter
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU General Public License for more details.
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include "seahorse-gpgme-expires-dialog.h"
+#include "seahorse-gpgme-key-op.h"
+#include "seahorse-gpgme-subkey.h"
+
+#include "libseahorse/seahorse-util.h"
+
+#include <glib/gi18n.h>
+
+#include <string.h>
+
+struct _SeahorseGpgmeExpiresDialog {
+    GtkDialog parent_instance;
+
+    SeahorseGpgmeSubkey *subkey;
+
+    GtkWidget *calendar;
+    GtkWidget *never_expires_check;
+};
+
+enum {
+    PROP_0,
+    PROP_SUBKEY,
+    N_PROPS
+};
+static GParamSpec *obj_props[N_PROPS] = { NULL, };
+
+G_DEFINE_TYPE (SeahorseGpgmeExpiresDialog, seahorse_gpgme_expires_dialog, GTK_TYPE_DIALOG)
+
+static void
+seahorse_gpgme_expires_dialog_response (GtkDialog *dialog, int response)
+{
+    SeahorseGpgmeExpiresDialog *self = SEAHORSE_GPGME_EXPIRES_DIALOG (dialog);
+    gpgme_error_t err;
+    time_t expiry = 0;
+
+    if (response != GTK_RESPONSE_OK)
+        return;
+
+    if (!gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (self->never_expires_check))) {
+        struct tm when;
+
+        memset (&when, 0, sizeof (when));
+        gtk_calendar_get_date (GTK_CALENDAR (self->calendar),
+                               (guint*) &(when.tm_year),
+                               (guint*) &(when.tm_mon),
+                               (guint*) &(when.tm_mday));
+        when.tm_year -= 1900;
+        expiry = mktime (&when);
+
+        if (expiry <= time (NULL)) {
+            seahorse_util_show_error (self->calendar, _("Invalid expiry date"),
+                                      _("The expiry date must be in the future"));
+            return;
+        }
+    }
+
+    gtk_widget_set_sensitive (gtk_dialog_get_content_area (GTK_DIALOG (self)), FALSE);
+
+    if (expiry != (time_t)seahorse_pgp_subkey_get_expires (SEAHORSE_PGP_SUBKEY (self->subkey))) {
+        err = seahorse_gpgme_key_op_set_expires (self->subkey, expiry);
+        if (!GPG_IS_OK (err))
+            seahorse_gpgme_handle_error (err, _("Couldn’t change expiry date"));
+    }
+}
+
+static void
+on_gpgme_expire_toggled (GtkWidget *widget,
+                         gpointer user_data)
+{
+    SeahorseGpgmeExpiresDialog *self = SEAHORSE_GPGME_EXPIRES_DIALOG (user_data);
+
+    gtk_widget_set_sensitive (self->calendar,
+                              !gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (self->never_expires_check)));
+}
+
+static void
+seahorse_gpgme_expires_dialog_get_property (GObject *object,
+                                            guint prop_id,
+                                            GValue *value,
+                                            GParamSpec *pspec)
+{
+    SeahorseGpgmeExpiresDialog *self = SEAHORSE_GPGME_EXPIRES_DIALOG (object);
+
+    switch (prop_id) {
+    case PROP_SUBKEY:
+        g_value_set_object (value, self->subkey);
+        break;
+    default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+        break;
+    }
+}
+
+static void
+seahorse_gpgme_expires_dialog_set_property (GObject *object,
+                                            guint prop_id,
+                                            const GValue *value,
+                                            GParamSpec *pspec)
+{
+    SeahorseGpgmeExpiresDialog *self = SEAHORSE_GPGME_EXPIRES_DIALOG (object);
+
+    switch (prop_id) {
+    case PROP_SUBKEY:
+        g_clear_object (&self->subkey);
+        self->subkey = g_value_dup_object (value);
+        break;
+    default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+        break;
+    }
+}
+
+static void
+seahorse_gpgme_expires_dialog_finalize (GObject *obj)
+{
+    SeahorseGpgmeExpiresDialog *self = SEAHORSE_GPGME_EXPIRES_DIALOG (obj);
+
+    g_clear_object (&self->subkey);
+
+    G_OBJECT_CLASS (seahorse_gpgme_expires_dialog_parent_class)->finalize (obj);
+}
+
+static void
+seahorse_gpgme_expires_dialog_constructed (GObject *obj)
+{
+    SeahorseGpgmeExpiresDialog *self = SEAHORSE_GPGME_EXPIRES_DIALOG (obj);
+    g_autofree char *title = NULL;
+    const char *label;
+    gulong expires;
+
+    G_OBJECT_CLASS (seahorse_gpgme_expires_dialog_parent_class)->constructed (obj);
+
+    label = seahorse_pgp_subkey_get_description (SEAHORSE_PGP_SUBKEY (self->subkey));
+    title = g_strdup_printf (_("Expiry: %s"), label);
+    gtk_window_set_title (GTK_WINDOW (self), title);
+
+    expires = seahorse_pgp_subkey_get_expires (SEAHORSE_PGP_SUBKEY (self->subkey));
+    if (expires == 0) {
+        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->never_expires_check), TRUE);
+        gtk_widget_set_sensitive (self->calendar, FALSE);
+    } else {
+        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->never_expires_check), FALSE);
+        gtk_widget_set_sensitive (self->calendar, TRUE);
+    }
+
+    if (expires) {
+        struct tm t;
+        time_t time = (time_t)expires;
+        if (gmtime_r (&time, &t)) {
+            gtk_calendar_select_month (GTK_CALENDAR (self->calendar), t.tm_mon, t.tm_year + 1900);
+            gtk_calendar_select_day (GTK_CALENDAR (self->calendar), t.tm_mday);
+        }
+    }
+}
+
+static void
+seahorse_gpgme_expires_dialog_init (SeahorseGpgmeExpiresDialog *self)
+{
+    gtk_widget_init_template (GTK_WIDGET (self));
+}
+
+static void
+seahorse_gpgme_expires_dialog_class_init (SeahorseGpgmeExpiresDialogClass *klass)
+{
+    GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+    GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+    GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (klass);
+
+    gobject_class->constructed = seahorse_gpgme_expires_dialog_constructed;
+    gobject_class->get_property = seahorse_gpgme_expires_dialog_get_property;
+    gobject_class->set_property = seahorse_gpgme_expires_dialog_set_property;
+    gobject_class->finalize = seahorse_gpgme_expires_dialog_finalize;
+
+    obj_props[PROP_SUBKEY] =
+        g_param_spec_object ("subkey", "Subkey",
+                             "Subkey",
+                             SEAHORSE_GPGME_TYPE_SUBKEY,
+                             G_PARAM_READWRITE |
+                             G_PARAM_CONSTRUCT_ONLY |
+                             G_PARAM_STATIC_STRINGS);
+
+    g_object_class_install_properties (gobject_class, N_PROPS, obj_props);
+
+    gtk_widget_class_set_template_from_resource (widget_class, 
"/org/gnome/Seahorse/seahorse-gpgme-expires-dialog.ui");
+    gtk_widget_class_bind_template_child (widget_class, SeahorseGpgmeExpiresDialog, calendar);
+    gtk_widget_class_bind_template_child (widget_class, SeahorseGpgmeExpiresDialog, never_expires_check);
+    gtk_widget_class_bind_template_callback (widget_class, on_gpgme_expire_toggled);
+
+    dialog_class->response = seahorse_gpgme_expires_dialog_response;
+}
+
+GtkDialog *
+seahorse_gpgme_expires_dialog_new (SeahorseGpgmeSubkey *subkey,
+                                   GtkWindow *parent)
+{
+    g_return_val_if_fail (SEAHORSE_GPGME_IS_SUBKEY (subkey), NULL);
+
+    return g_object_new (SEAHORSE_GPGME_TYPE_EXPIRES_DIALOG,
+                         "subkey", subkey,
+                         "use-header-bar", 1,
+                         NULL);
+}
diff --git a/pgp/seahorse-gpgme-expires-dialog.h b/pgp/seahorse-gpgme-expires-dialog.h
new file mode 100644
index 00000000..3ac8a22d
--- /dev/null
+++ b/pgp/seahorse-gpgme-expires-dialog.h
@@ -0,0 +1,33 @@
+/*
+ * Seahorse
+ *
+ * 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 General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU General Public License for more details.
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include <gtk/gtk.h>
+
+#include "pgp/seahorse-gpgme-key.h"
+#include "pgp/seahorse-gpgme-subkey.h"
+
+#define SEAHORSE_GPGME_TYPE_EXPIRES_DIALOG (seahorse_gpgme_expires_dialog_get_type ())
+G_DECLARE_FINAL_TYPE (SeahorseGpgmeExpiresDialog, seahorse_gpgme_expires_dialog,
+                      SEAHORSE_GPGME, EXPIRES_DIALOG,
+                      GtkDialog)
+
+GtkDialog*   seahorse_gpgme_expires_dialog_new    (SeahorseGpgmeSubkey *subkey,
+                                                   GtkWindow *parent);
diff --git a/pgp/seahorse-gpgme-expires-dialog.ui b/pgp/seahorse-gpgme-expires-dialog.ui
new file mode 100644
index 00000000..2d73d897
--- /dev/null
+++ b/pgp/seahorse-gpgme-expires-dialog.ui
@@ -0,0 +1,57 @@
+<?xml version="1.0"?>
+<interface>
+  <requires lib="gtk+" version="3.22"/>
+  <template class="SeahorseGpgmeExpiresDialog" parent="GtkDialog">
+    <property name="visible">True</property>
+    <property name="border-width">12</property>
+    <property name="width-request">400</property>
+    <property name="modal">True</property>
+    <child internal-child="vbox">
+      <object class="GtkBox" id="all-controls">
+        <property name="visible">True</property>
+        <property name="orientation">vertical</property>
+        <property name="spacing">6</property>
+        <child>
+          <object class="GtkCheckButton" id="never_expires_check">
+            <property name="label" translatable="yes">_Never expires</property>
+            <property name="visible">True</property>
+            <property name="can_focus">True</property>
+            <property name="receives_default">False</property>
+            <property name="use_underline">True</property>
+            <property name="draw_indicator">True</property>
+            <signal name="toggled" handler="on_gpgme_expire_toggled"/>
+          </object>
+        </child>
+        <child>
+          <object class="GtkCalendar" id="calendar">
+            <property name="visible">True</property>
+            <property name="can_focus">True</property>
+          </object>
+        </child>
+      </object>
+    </child>
+    <child type="action">
+      <object class="GtkButton" id="cancel_button">
+        <property name="label">gtk-cancel</property>
+        <property name="visible">True</property>
+        <property name="can_default">True</property>
+        <property name="receives_default">False</property>
+        <property name="use_stock">True</property>
+      </object>
+    </child>
+    <child type="action">
+      <object class="GtkButton" id="ok_button">
+        <property name="visible">True</property>
+        <property name="can_default">True</property>
+        <property name="has_default">True</property>
+        <property name="receives_default">False</property>
+        <property name="label" translatable="yes">C_hange</property>
+        <property name="use_underline">True</property>
+      </object>
+    </child>
+    <action-widgets>
+      <action-widget response="cancel">cancel_button</action-widget>
+      <action-widget response="ok" default="true">ok_button</action-widget>
+    </action-widgets>
+  </template>
+</interface>
diff --git a/pgp/seahorse-pgp-key-properties.c b/pgp/seahorse-pgp-key-properties.c
index 3cf566e4..9f48905d 100644
--- a/pgp/seahorse-pgp-key-properties.c
+++ b/pgp/seahorse-pgp-key-properties.c
@@ -27,6 +27,7 @@
 #include "seahorse-gpgme-add-subkey.h"
 #include "seahorse-gpgme-add-uid.h"
 #include "seahorse-gpgme-dialogs.h"
+#include "seahorse-gpgme-expires-dialog.h"
 #include "seahorse-gpgme-exporter.h"
 #include "seahorse-gpgme-key.h"
 #include "seahorse-gpgme-key-op.h"
@@ -1031,6 +1032,7 @@ on_subkeys_change_expires (GSimpleAction *action, GVariant *param, gpointer user
     SeahorsePgpKeyProperties *self = SEAHORSE_PGP_KEY_PROPERTIES (user_data);
     SeahorsePgpSubkey *subkey;
     GList *subkeys;
+    GtkDialog *dialog;
 
     subkey = get_selected_subkey (self);
     if (subkey == NULL) {
@@ -1041,9 +1043,13 @@ on_subkeys_change_expires (GSimpleAction *action, GVariant *param, gpointer user
 
     g_return_if_fail (SEAHORSE_GPGME_IS_SUBKEY (subkey));
 
-    if (subkey != NULL)
-        seahorse_gpgme_expires_new (SEAHORSE_GPGME_SUBKEY (subkey),
-                                    GTK_WINDOW (self));
+    if (subkey == NULL)
+        return;
+
+    dialog = seahorse_gpgme_expires_dialog_new (SEAHORSE_GPGME_SUBKEY (subkey),
+                                                GTK_WINDOW (self));
+    gtk_dialog_run (dialog);
+    gtk_widget_destroy (GTK_WIDGET (dialog));
 }
 
 static void
@@ -1126,12 +1132,15 @@ on_change_expires (GSimpleAction *action, GVariant *param, gpointer user_data)
 {
     SeahorsePgpKeyProperties *self = SEAHORSE_PGP_KEY_PROPERTIES (user_data);
     GList *subkeys;
+    GtkDialog *dialog;
 
     subkeys = seahorse_pgp_key_get_subkeys (self->key);
     g_return_if_fail (subkeys);
 
-    seahorse_gpgme_expires_new (SEAHORSE_GPGME_SUBKEY (subkeys->data),
-                                GTK_WINDOW (self));
+    dialog = seahorse_gpgme_expires_dialog_new (SEAHORSE_GPGME_SUBKEY (subkeys->data),
+                                                GTK_WINDOW (self));
+    gtk_dialog_run (dialog);
+    gtk_widget_destroy (GTK_WIDGET (dialog));
 }
 
 static void
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 70d58324..6f448c92 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -37,13 +37,13 @@ libegg/egg-datetime.c
 libseahorse/seahorse-progress.ui
 libseahorse/seahorse-util.c
 libseahorse/seahorse-widget.c
-pgp/seahorse-expires.ui
 pgp/seahorse-gpgme-add-subkey.c
 pgp/seahorse-gpgme-add-subkey.ui
 pgp/seahorse-gpgme-add-uid.c
 pgp/seahorse-gpgme-add-uid.ui
 pgp/seahorse-gpgme.c
-pgp/seahorse-gpgme-expires.c
+pgp/seahorse-gpgme-expires-dialog.c
+pgp/seahorse-gpgme-expires-dialog.ui
 pgp/seahorse-gpgme-exporter.c
 pgp/seahorse-gpgme-generate-dialog.c
 pgp/seahorse-gpgme-generate-dialog.ui


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