[krb5-auth-dialog] Make preferences a proper child of GtkDialog



commit 373ae2c5d3eab708e9245deb93f65c3068f351e1
Author: Guido Günther <agx sigxcpu org>
Date:   Fri Oct 24 13:56:16 2014 +0200

    Make preferences a proper child of GtkDialog
    
    This gives us variants with and without header bar for free.

 src/ka-applet.c                    |    9 +-
 src/ka-preferences.c               |  501 ++++++++++++++++++++++--------------
 src/ka-preferences.h               |   25 ++-
 src/resources/ui/ka-preferences.ui |    6 +-
 4 files changed, 335 insertions(+), 206 deletions(-)
---
diff --git a/src/ka-applet.c b/src/ka-applet.c
index 4bf363e..bc14f81 100644
--- a/src/ka-applet.c
+++ b/src/ka-applet.c
@@ -82,6 +82,7 @@ struct _KaAppletPrivate {
     gboolean ns_persistence;    /* does the notification server support persistence */
 
     KaPwDialog *pwdialog;       /* the password dialog */
+    KaPreferences *prefs;       /* the prefs dialog */
     int pw_prompt_secs;         /* when to start sending notifications */
     KaPluginLoader *loader;     /* Plugin loader */
 
@@ -202,7 +203,7 @@ action_preferences (GSimpleAction *action G_GNUC_UNUSED,
 {
     KaApplet *self = userdata;
 
-    ka_preferences_window_show (self);
+    ka_preferences_run (self->priv->prefs);
 }
 
 static void
@@ -283,7 +284,7 @@ ka_applet_startup (GApplication *application)
 
     self->priv->startup_ccache = ka_kerberos_init (self);
     ka_main_window_create (self);
-    ka_preferences_window_create (self);
+    self->priv->prefs = ka_preferences_new (self);
 
     ka_applet_app_menu_create(self);
 }
@@ -1164,8 +1165,10 @@ ka_applet_destroy (KaApplet* self)
                                       GTK_WINDOW (first->data));
     }
 
+    gtk_widget_destroy (GTK_WIDGET(self->priv->prefs));
+    self->priv->prefs = NULL;
+
     ka_kerberos_destroy ();
-    ka_preferences_window_destroy ();
 }
 
 
diff --git a/src/ka-preferences.c b/src/ka-preferences.c
index d5bf92c..95d850f 100644
--- a/src/ka-preferences.c
+++ b/src/ka-preferences.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2011 Guido Guenther <agx sigxcpu org>
+ * Copyright (C) 2011,2014 Guido Guenther <agx sigxcpu org>
  *
  * 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
@@ -24,18 +24,22 @@
 
 #include <glib/gi18n.h>
 
-#define WID(b, w) (GtkWidget *) gtk_builder_get_object (b, w)
-
 #define PKINIT_SMARTCARD "PKCS11:" SC_PKCS11
 #define PKINIT_FILE "FILE:"
 
 #define N_BINDINGS 3
 
-struct _KaPreferencesPrivate {
-    GtkBuilder *builder;
+struct _KaPreferences {
+    GtkDialog parent;
 
-    GSettings *settings;
+    KaPreferencesPrivate *priv;
+};
 
+struct _KaPreferencesClass {
+    GtkDialogClass parent;
+};
+
+struct _KaPreferencesPrivate {
     GtkWidget *dialog;
     GtkWidget *notebook;
     GtkWidget *principal_entry;
@@ -45,29 +49,112 @@ struct _KaPreferencesPrivate {
     GtkWidget *pkanchors_entry;
     GtkWidget *pkanchors_button;
     GtkWidget *prompt_mins_entry;
+    GtkWidget *forwardable_toggle;
+    GtkWidget *proxiable_toggle;
+    GtkWidget *renewable_toggle;
 
+    GSettings *settings;
     GBinding* bindings[N_BINDINGS];
     int       n_bindings;
-} prefs;
+
+    KaApplet *applet;
+};
+
+G_DEFINE_TYPE_WITH_PRIVATE (KaPreferences, ka_preferences, GTK_TYPE_DIALOG);
+
+enum {
+    PROP_0,
+    PROP_APPLET,
+};
 
 
 static void
-ka_preferences_principal_notify (GSettings *settings,
-                                 gchar *key,
-                                 gpointer userdata G_GNUC_UNUSED)
+ka_preferences_set_property (GObject      *object,
+                             guint         prop_id,
+                             const GValue *value,
+                             GParamSpec   *pspec)
+{
+  KaPreferences *self = KA_PREFERENCES (object);
+
+  switch (prop_id) {
+  case PROP_APPLET:
+      self->priv->applet = g_value_get_object (value);
+      break;
+  default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+  }
+}
+
+
+static void
+ka_preferences_get_property (GObject    *object,
+                             guint       prop_id,
+                             GValue     *value,
+                             GParamSpec *pspec)
+{
+  KaPreferences *self = KA_PREFERENCES (object);
+
+  switch (prop_id) {
+  case PROP_APPLET:
+      g_value_set_object (value, self->priv->applet);
+      break;
+
+  default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+  }
+}
+
+
+static GObject *
+ka_preferences_constructor (GType type,
+                         guint n_construct_properties,
+                         GObjectConstructParam *construct_params)
+{
+    GObject *object;
+    guint idx;
+    GParamSpec *pspec;
+    GValue *value;
+
+    for (idx = 0; idx < n_construct_properties; idx++)
+    {
+        pspec = construct_params[idx].pspec;
+        if (g_strcmp0 (pspec->name, "use-header-bar") != 0)
+            continue;
+
+        /* GtkDialog uses "-1" to imply an unset value for this property */
+        value = construct_params[idx].value;
+        if (g_value_get_int (value) == -1)
+            g_value_set_int (value, 1);
+
+        break;
+    }
+
+    object = G_OBJECT_CLASS (ka_preferences_parent_class)->constructor (type,
+                                                                     n_construct_properties,
+                                                                     construct_params);
+
+    return object;
+}
+
+
+static void
+ka_preferences_principal_notify (KaPreferences *self,
+                                 gchar *key)
 {
     const char *principal;
 
-    principal = g_settings_get_string (settings, key);
+    principal = g_settings_get_string (self->priv->settings, key);
 
     if (!principal || !strlen(principal))
-        gtk_entry_set_text (GTK_ENTRY (prefs.principal_entry), "");
+        gtk_entry_set_text (GTK_ENTRY (self->priv->principal_entry), "");
     else {
         const char *old_principal;
 
-        old_principal = gtk_entry_get_text (GTK_ENTRY (prefs.principal_entry));
+        old_principal = gtk_entry_get_text (GTK_ENTRY (self->priv->principal_entry));
         if (!old_principal || (old_principal && strcmp (old_principal, principal)))
-            gtk_entry_set_text (GTK_ENTRY (prefs.principal_entry), principal);
+            gtk_entry_set_text (GTK_ENTRY (self->priv->principal_entry), principal);
     }
 }
 
@@ -76,54 +163,50 @@ ka_preferences_principal_changed (GtkEntry *entry,
                                   gpointer userdata)
 {
     const char *principal;
-    KaApplet *applet = KA_APPLET(userdata);
+    KaPreferences *self = KA_PREFERENCES (userdata);
 
     principal = gtk_entry_get_text (entry);
 
-    if (principal || strlen(principal))
-        g_object_set (applet, KA_PROP_NAME_PRINCIPAL, principal, NULL);
+    if (principal && strlen(principal))
+        g_object_set (self->priv->applet, KA_PROP_NAME_PRINCIPAL, principal, NULL);
     else
-        g_object_set (applet, KA_PROP_NAME_PRINCIPAL, "", NULL);
+        g_object_set (self->priv->applet, KA_PROP_NAME_PRINCIPAL, "", NULL);
 }
 
 
 static void
-ka_preferences_setup_principal_entry (KaApplet *applet)
+ka_preferences_setup_principal_entry (KaPreferences *self)
 {
     char *principal = NULL;
 
-    prefs.principal_entry = WID (prefs.builder, "principal_entry");
-    g_assert (prefs.principal_entry != NULL);
-
-    g_object_get (applet, KA_PROP_NAME_PRINCIPAL, &principal, NULL);
+    g_object_get (self->priv->applet, KA_PROP_NAME_PRINCIPAL, &principal, NULL);
     if (!principal)
         g_warning ("Getting principal failed");
     if (principal && strlen(principal))
-        gtk_entry_set_text (GTK_ENTRY (prefs.principal_entry), principal);
+        gtk_entry_set_text (GTK_ENTRY (self->priv->principal_entry), principal);
     g_free (principal);
 
-    g_signal_connect (prefs.principal_entry, "changed",
-                      G_CALLBACK (ka_preferences_principal_changed), applet);
+    g_signal_connect (self->priv->principal_entry, "changed",
+                      G_CALLBACK (ka_preferences_principal_changed), self);
 }
 
 
 static void
-ka_preferences_pkuserid_notify (GSettings *settings,
-                                gchar *key,
-                                gpointer userdata G_GNUC_UNUSED)
+ka_preferences_pkuserid_notify (KaPreferences *self,
+                                gchar *key)
 {
     const char *pkuserid;
 
-    pkuserid = g_settings_get_string (settings, key);
+    pkuserid = g_settings_get_string (self->priv->settings, key);
 
-    if (!pkuserid || !strlen(pkuserid))
-        gtk_entry_set_text (GTK_ENTRY (prefs.pkuserid_entry), "");
-    else {
+    if (pkuserid && strlen(pkuserid)) {
         const char *old_pkuserid;
 
-        old_pkuserid = gtk_entry_get_text (GTK_ENTRY (prefs.pkuserid_entry));
+        old_pkuserid = gtk_entry_get_text (GTK_ENTRY (self->priv->pkuserid_entry));
         if (!old_pkuserid || (old_pkuserid && strcmp (old_pkuserid, pkuserid)))
-            gtk_entry_set_text (GTK_ENTRY (prefs.pkuserid_entry), pkuserid);
+            gtk_entry_set_text (GTK_ENTRY (self->priv->pkuserid_entry), pkuserid);
+    } else {
+        gtk_entry_set_text (GTK_ENTRY (self->priv->pkuserid_entry), "");
     }
 }
 
@@ -133,54 +216,51 @@ ka_preferences_pkuserid_changed (GtkEntry *entry,
                                  gpointer *userdata)
 {
     const char *pkuserid;
-    KaApplet *applet = KA_APPLET(userdata);
+    KaPreferences *self = KA_PREFERENCES (userdata);
 
     pkuserid = gtk_entry_get_text (entry);
 
-    if (!pkuserid || !strlen(pkuserid))
-        g_object_set (applet, KA_PROP_NAME_PK_USERID, "", NULL);
+    if (pkuserid && strlen(pkuserid))
+        g_object_set (self->priv->applet, KA_PROP_NAME_PK_USERID, pkuserid, NULL);
     else
-        g_object_set (applet, KA_PROP_NAME_PK_USERID, pkuserid, NULL);
+        g_object_set (self->priv->applet, KA_PROP_NAME_PK_USERID, "", NULL);
 }
 
+
 static void
-ka_preferences_setup_pkuserid_entry (KaApplet *applet)
+ka_preferences_setup_pkuserid_entry (KaPreferences *self)
 {
-    char     *pkuserid = NULL;
-
-    prefs.pkuserid_entry = WID(prefs.builder, "pkuserid_entry");
-    g_assert (prefs.pkuserid_entry != NULL);
+    char *pkuserid = NULL;
 
-    g_object_get (applet, KA_PROP_NAME_PK_USERID, &pkuserid, NULL);
+    g_object_get (self->priv->applet, KA_PROP_NAME_PK_USERID, &pkuserid, NULL);
     if (!pkuserid)
         g_warning ("Getting pkuserid failed");
     if (pkuserid && strlen(pkuserid))
-        gtk_entry_set_text (GTK_ENTRY (prefs.pkuserid_entry), pkuserid);
+        gtk_entry_set_text (GTK_ENTRY (self->priv->pkuserid_entry), pkuserid);
     if (pkuserid)
         g_free (pkuserid);
 
-    g_signal_connect (prefs.pkuserid_entry, "changed",
-                      G_CALLBACK (ka_preferences_pkuserid_changed), applet);
+    g_signal_connect (self->priv->pkuserid_entry, "changed",
+                      G_CALLBACK (ka_preferences_pkuserid_changed), self);
 }
 
 static void
-ka_preferences_pkanchors_notify (GSettings *settings,
-                                 gchar *key,
-                                 gpointer userdata G_GNUC_UNUSED)
+ka_preferences_pkanchors_notify (KaPreferences *self,
+                                 gchar *key)
 {
     const char *pkanchors;
 
-    pkanchors = g_settings_get_string (settings, key);
+    pkanchors = g_settings_get_string (self->priv->settings, key);
 
-    if (!pkanchors || !strlen(pkanchors))
-        gtk_entry_set_text (GTK_ENTRY (prefs.pkanchors_entry), "");
-    else {
+    if (pkanchors && strlen(pkanchors)) {
         const char *old_pkanchors;
 
-        old_pkanchors = gtk_entry_get_text (GTK_ENTRY (prefs.pkanchors_entry));
+        old_pkanchors = gtk_entry_get_text (GTK_ENTRY (self->priv->pkanchors_entry));
         if (!old_pkanchors || (old_pkanchors && strcmp (old_pkanchors,
                                                         pkanchors)))
-            gtk_entry_set_text (GTK_ENTRY (prefs.pkanchors_entry), pkanchors);
+            gtk_entry_set_text (GTK_ENTRY (self->priv->pkanchors_entry), pkanchors);
+    } else {
+        gtk_entry_set_text (GTK_ENTRY (self->priv->pkanchors_entry), "");
     }
 }
 
@@ -189,44 +269,41 @@ ka_preferences_pkanchors_changed (GtkEntry *entry,
                                   gpointer userdata)
 {
     const char *pkanchors;
-    KaApplet *applet = KA_APPLET(userdata);
+    KaPreferences *self = KA_PREFERENCES (userdata);
 
     pkanchors = gtk_entry_get_text (entry);
 
-    if (!pkanchors || !strlen(pkanchors))
-        g_object_set (applet, KA_PROP_NAME_PK_ANCHORS, "", NULL);
+    if (pkanchors && strlen(pkanchors))
+        g_object_set (self->priv->applet, KA_PROP_NAME_PK_ANCHORS, pkanchors, NULL);
     else
-        g_object_set (applet, KA_PROP_NAME_PK_ANCHORS, pkanchors, NULL);
+        g_object_set (self->priv->applet, KA_PROP_NAME_PK_ANCHORS, "", NULL);
 }
 
 
 static void
-ka_preferences_setup_pkanchors_entry (KaApplet *applet)
+ka_preferences_setup_pkanchors_entry (KaPreferences *self)
 {
     char *pkanchors = NULL;
 
-    prefs.pkanchors_entry = WID(prefs.builder, "pkanchors_entry");
-    g_assert (prefs.pkanchors_entry != NULL);
-
-    g_object_get (applet, KA_PROP_NAME_PK_ANCHORS, &pkanchors, NULL);
+    g_object_get (self->priv->applet, KA_PROP_NAME_PK_ANCHORS, &pkanchors, NULL);
     if (!pkanchors)
         g_warning ("Getting pkanchors failed");
 
     if (pkanchors && strlen(pkanchors))
-        gtk_entry_set_text (GTK_ENTRY (prefs.pkanchors_entry), pkanchors);
+        gtk_entry_set_text (GTK_ENTRY (self->priv->pkanchors_entry), pkanchors);
     if (pkanchors)
         g_free (pkanchors);
 
-    g_signal_connect (prefs.pkanchors_entry, "changed",
-                      G_CALLBACK (ka_preferences_pkanchors_changed), applet);
+    g_signal_connect (self->priv->pkanchors_entry, "changed",
+                      G_CALLBACK (ka_preferences_pkanchors_changed), self);
 }
 
 
 static void
-ka_preferences_toggle_pkuserid_entry (gboolean state, gpointer userdata G_GNUC_UNUSED)
+ka_preferences_toggle_pkuserid_entry (KaPreferences *self, gboolean state)
 {
-    gtk_widget_set_sensitive (prefs.pkuserid_entry, state);
-    gtk_widget_set_sensitive (prefs.pkuserid_button, state);
+    gtk_widget_set_sensitive (self->priv->pkuserid_entry, state);
+    gtk_widget_set_sensitive (self->priv->pkuserid_button, state);
 }
 
 
@@ -236,56 +313,52 @@ ka_preferences_smartcard_toggled (GtkToggleButton *toggle,
 {
     gboolean smartcard = gtk_toggle_button_get_active (toggle);
     static gchar *old_path = NULL;
-    KaApplet *applet = KA_APPLET(userdata);
+    KaPreferences *self = KA_PREFERENCES(userdata);
 
     if (smartcard) {
         const char *path;
 
-        path = gtk_entry_get_text (GTK_ENTRY(prefs.pkuserid_entry));
+        path = gtk_entry_get_text (GTK_ENTRY(self->priv->pkuserid_entry));
         if (g_strcmp0 (path, PKINIT_SMARTCARD)) {
             g_free (old_path);
             old_path = g_strdup (path);
         }
 
-        ka_preferences_toggle_pkuserid_entry (FALSE, NULL);
-        g_object_set (applet, KA_SETTING_KEY_PK_USERID, PKINIT_SMARTCARD, NULL);
+        ka_preferences_toggle_pkuserid_entry (self, FALSE);
+        g_object_set (self->priv->applet, KA_SETTING_KEY_PK_USERID, PKINIT_SMARTCARD, NULL);
     } else {
-        ka_preferences_toggle_pkuserid_entry (TRUE, NULL);
-        if (old_path)
-            g_object_set (applet, KA_SETTING_KEY_PK_USERID, old_path, NULL);
+        ka_preferences_toggle_pkuserid_entry (self, TRUE);
+        if (old_path && strlen(old_path))
+            g_object_set (self->priv->applet, KA_SETTING_KEY_PK_USERID, old_path, NULL);
         else
-            g_object_set (applet, KA_SETTING_KEY_PK_USERID, old_path, "", NULL);
+            g_object_set (self->priv->applet, KA_SETTING_KEY_PK_USERID, old_path, "", NULL);
     }
 }
 
 
 static void
-ka_preferences_setup_smartcard_toggle (KaApplet *applet)
+ka_preferences_setup_smartcard_toggle (KaPreferences *self)
 {
     char *pkuserid = NULL;
 
-    prefs.smartcard_toggle = WID (prefs.builder, "smartcard_toggle");
-    g_assert (prefs.smartcard_toggle != NULL);
-
-    g_object_get(applet, KA_PROP_NAME_PK_USERID, &pkuserid, NULL);
+    g_object_get(self->priv->applet, KA_PROP_NAME_PK_USERID, &pkuserid, NULL);
     if (!pkuserid)
         g_warning ("Getting pk userid failed");
 
-    g_signal_connect (prefs.smartcard_toggle, "toggled",
-                      G_CALLBACK (ka_preferences_smartcard_toggled), applet);
+    g_signal_connect (self->priv->smartcard_toggle, "toggled",
+                      G_CALLBACK (ka_preferences_smartcard_toggled), self);
 
     if (!g_strcmp0 (pkuserid, PKINIT_SMARTCARD))
-        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (prefs.smartcard_toggle), TRUE);
+        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->priv->smartcard_toggle), TRUE);
     else
-        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (prefs.smartcard_toggle), FALSE);
+        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->priv->smartcard_toggle), FALSE);
 
-    if (pkuserid)
-        g_free (pkuserid);
+    g_free (pkuserid);
 }
 
 
 static void
-ka_preferences_browse_certs (GtkEntry *entry)
+ka_preferences_browse_certs (KaPreferences *self, GtkEntry *entry)
 {
     GtkWidget *filechooser;
     GtkFileFilter *cert_filter, *all_filter;
@@ -294,7 +367,7 @@ ka_preferences_browse_certs (GtkEntry *entry)
     gint ret;
 
     filechooser = gtk_file_chooser_dialog_new(_("Choose Certificate"),
-                                              GTK_WINDOW (prefs.dialog),
+                                              GTK_WINDOW (self),
                                               GTK_FILE_CHOOSER_ACTION_OPEN,
                                               _("_Cancel"), GTK_RESPONSE_CANCEL,
                                               _("_Open"), GTK_RESPONSE_ACCEPT,
@@ -322,103 +395,87 @@ ka_preferences_browse_certs (GtkEntry *entry)
     gtk_widget_destroy (GTK_WIDGET(filechooser));
 
     if (filename) {
-        gchar *cert = g_strconcat( PKINIT_FILE, filename, NULL);
+        gchar *cert = g_strconcat (PKINIT_FILE, filename, NULL);
         gtk_entry_set_text (entry, cert);
-        g_free (filename);
         g_free (cert);
-  }
+    }
     g_object_unref (cert_filter);
     g_object_unref (all_filter);
+    g_free (filename);
 }
 
 static void
 ka_preferences_browse_pkuserids (GtkButton *button G_GNUC_UNUSED,
-                                 gpointer userdata G_GNUC_UNUSED)
+                                 gpointer userdata)
 {
-    ka_preferences_browse_certs (GTK_ENTRY(prefs.pkuserid_entry));
+    KaPreferences *self = KA_PREFERENCES (userdata);
+    ka_preferences_browse_certs (self, GTK_ENTRY(self->priv->pkuserid_entry));
 }
 
 static void
 ka_preferences_browse_pkanchors(GtkButton *button G_GNUC_UNUSED,
-                                gpointer userdata G_GNUC_UNUSED)
+                                gpointer userdata)
 {
-    ka_preferences_browse_certs (GTK_ENTRY(prefs.pkanchors_entry));
+    KaPreferences *self = KA_PREFERENCES (userdata);
+    ka_preferences_browse_certs (self, GTK_ENTRY(self->priv->pkanchors_entry));
 }
 
 static void
-ka_preferences_setup_pkuserid_button ()
+ka_preferences_setup_pkuserid_button (KaPreferences *self)
 {
-    prefs.pkuserid_button = WID (prefs.builder, "pkuserid_button");
-    g_assert (prefs.pkuserid_button != NULL);
-
-    g_signal_connect (prefs.pkuserid_button, "clicked",
-                      G_CALLBACK (ka_preferences_browse_pkuserids), NULL);
+    g_signal_connect (self->priv->pkuserid_button, "clicked",
+                      G_CALLBACK (ka_preferences_browse_pkuserids), self);
 
 }
 
 static void
-ka_preferences_setup_pkanchors_button ()
+ka_preferences_setup_pkanchors_button (KaPreferences *self)
 {
-    prefs.pkanchors_button = WID (prefs.builder, "pkanchors_button");
-    g_assert (prefs.pkanchors_button != NULL);
-
-    g_signal_connect (prefs.pkanchors_button, "clicked",
-                      G_CALLBACK (ka_preferences_browse_pkanchors), NULL);
+    g_signal_connect (self->priv->pkanchors_button, "clicked",
+                      G_CALLBACK (ka_preferences_browse_pkanchors), self);
 
 }
 
 static void
-ka_preferences_setup_forwardable_toggle (KaApplet *applet)
+ka_preferences_setup_forwardable_toggle (KaPreferences *self)
 {
     GBinding *binding;
-    GtkWidget *toggle;
-
-    toggle = WID (prefs.builder, "forwardable_toggle");
-    g_assert (toggle != NULL);
 
-    binding = g_object_bind_property (applet,
+    binding = g_object_bind_property (self->priv->applet,
                                       KA_PROP_NAME_TGT_FORWARDABLE,
-                                      toggle,
+                                      self->priv->forwardable_toggle,
                                       "active",
                                       G_BINDING_BIDIRECTIONAL);
-    prefs.bindings[prefs.n_bindings] = binding;
-    prefs.n_bindings++;
+    self->priv->bindings[self->priv->n_bindings] = binding;
+    self->priv->n_bindings++;
 }
 
 static void
-ka_preferences_setup_proxiable_toggle (KaApplet *applet)
+ka_preferences_setup_proxiable_toggle (KaPreferences *self)
 {
     GBinding *binding;
-    GtkWidget *toggle;
 
-    toggle = WID (prefs.builder, "proxiable_toggle");
-    g_assert (toggle != NULL);
-
-    binding = g_object_bind_property (applet,
+    binding = g_object_bind_property (self->priv->applet,
                                       KA_PROP_NAME_TGT_PROXIABLE,
-                                      toggle,
+                                      self->priv->proxiable_toggle,
                                       "active",
                                       G_BINDING_BIDIRECTIONAL);
-    prefs.bindings[prefs.n_bindings] = binding;
-    prefs.n_bindings++;
+    self->priv->bindings[self->priv->n_bindings] = binding;
+    self->priv->n_bindings++;
 }
 
 static void
-ka_preferences_setup_renewable_toggle (KaApplet *applet)
+ka_preferences_setup_renewable_toggle (KaPreferences *self)
 {
     GBinding *binding;
-    GtkWidget *toggle;
-
-    toggle = WID (prefs.builder, "renewable_toggle");
-    g_assert (toggle != NULL);
 
-    binding = g_object_bind_property (applet,
+    binding = g_object_bind_property (self->priv->applet,
                                       KA_PROP_NAME_TGT_RENEWABLE,
-                                      toggle,
+                                      self->priv->renewable_toggle,
                                       "active",
                                       G_BINDING_BIDIRECTIONAL);
-    prefs.bindings[prefs.n_bindings] = binding;
-    prefs.n_bindings++;
+    self->priv->bindings[self->priv->n_bindings] = binding;
+    self->priv->n_bindings++;
 }
 
 
@@ -427,111 +484,161 @@ ka_preferences_prompt_mins_changed (GtkSpinButton *button,
                                     gpointer userdata)
 {
     gint prompt_mins;
-    KaApplet *applet = KA_APPLET(userdata);
+    KaPreferences *self = KA_PREFERENCES (userdata);
 
     prompt_mins = gtk_spin_button_get_value_as_int (button);
-    g_object_set (applet, KA_PROP_NAME_PW_PROMPT_MINS, prompt_mins, NULL);
+    g_object_set (self->priv->applet, KA_PROP_NAME_PW_PROMPT_MINS, prompt_mins, NULL);
 }
 
 
 static void
-ka_preferences_prompt_mins_notify (GSettings *settings,
-                                   gchar *key,
-                                   gpointer userdata G_GNUC_UNUSED)
+ka_preferences_prompt_mins_notify (KaPreferences *self,
+                                   gchar *key)
 {
     gint prompt_mins;
 
-    prompt_mins = g_settings_get_int (settings, key);
+    prompt_mins = g_settings_get_int (self->priv->settings, key);
     if (prompt_mins != gtk_spin_button_get_value_as_int (
-            GTK_SPIN_BUTTON (prefs.prompt_mins_entry)))
-        gtk_spin_button_set_value (GTK_SPIN_BUTTON (prefs.prompt_mins_entry),
+            GTK_SPIN_BUTTON (self->priv->prompt_mins_entry)))
+        gtk_spin_button_set_value (GTK_SPIN_BUTTON (self->priv->prompt_mins_entry),
                                    prompt_mins);
 }
 
 static void
-ka_preferences_setup_prompt_mins_entry (KaApplet *applet)
+ka_preferences_setup_prompt_mins_entry (KaPreferences *self)
 {
     gint prompt_mins;
 
-    prefs.prompt_mins_entry = WID (prefs.builder, "prompt_mins_entry");
-    g_assert (prefs.prompt_mins_entry != NULL);
+    g_object_get (self->priv->applet, KA_PROP_NAME_PW_PROMPT_MINS, &prompt_mins, NULL);
 
-    g_object_get (applet, KA_PROP_NAME_PW_PROMPT_MINS, &prompt_mins, NULL);
-
-    gtk_spin_button_set_value (GTK_SPIN_BUTTON (prefs.prompt_mins_entry),
+    gtk_spin_button_set_value (GTK_SPIN_BUTTON (self->priv->prompt_mins_entry),
                                prompt_mins);
 
-    g_signal_connect (prefs.prompt_mins_entry, "value-changed",
-                      G_CALLBACK (ka_preferences_prompt_mins_changed), applet);
+    g_signal_connect (self->priv->prompt_mins_entry, "value-changed",
+                      G_CALLBACK (ka_preferences_prompt_mins_changed), self);
 }
 
 
+/* GSettings changed, update the prefs dialog */
 static void
-ka_preferences_settings_changed (GSettings *settings,
+ka_preferences_settings_changed (GSettings *settings G_GNUC_UNUSED,
                                  gchar *key,
                                  gpointer userdata)
 {
-    KaApplet *applet = KA_APPLET (userdata);
+    KaPreferences *self = KA_PREFERENCES (userdata);
 
     if (!g_strcmp0 (key, KA_SETTING_KEY_PRINCIPAL))
-        ka_preferences_principal_notify (settings, key, applet);
+        ka_preferences_principal_notify (self, key);
     else if (!g_strcmp0(key, KA_SETTING_KEY_PK_USERID))
-        ka_preferences_pkuserid_notify (settings, key, applet);
+        ka_preferences_pkuserid_notify (self, key);
     else if (!g_strcmp0(key, KA_SETTING_KEY_PK_ANCHORS))
-        ka_preferences_pkanchors_notify (settings, key, applet);
+        ka_preferences_pkanchors_notify (self, key);
     else if (!g_strcmp0(key, KA_SETTING_KEY_PW_PROMPT_MINS))
-        ka_preferences_prompt_mins_notify (settings, key, applet);
+        ka_preferences_prompt_mins_notify (self, key);
 }
 
-void
-ka_preferences_window_create (KaApplet *applet)
-{
-    prefs.builder = gtk_builder_new_from_resource ("/org/gnome/krb5-auth-dialog/ui/ka-preferences.ui");
-    ka_preferences_setup_principal_entry (applet);
-    ka_preferences_setup_pkuserid_entry (applet);
-    ka_preferences_setup_pkuserid_button (applet);
-    ka_preferences_setup_smartcard_toggle (applet);
-    ka_preferences_setup_pkanchors_entry (applet);
-    ka_preferences_setup_pkanchors_button (applet);
 
-    ka_preferences_setup_forwardable_toggle (applet);
-    ka_preferences_setup_proxiable_toggle (applet);
-    ka_preferences_setup_renewable_toggle (applet);
-    ka_preferences_setup_prompt_mins_entry (applet);
+static void
+ka_preferences_constructed (GObject *object)
+{
+  KaPreferences *self = KA_PREFERENCES (object);
+
+  if (G_OBJECT_CLASS (ka_preferences_parent_class)->constructed != NULL)
+    G_OBJECT_CLASS (ka_preferences_parent_class)->constructed (object);
+
+  g_assert_nonnull (self->priv->applet);
+  self->priv->settings = ka_applet_get_settings(self->priv->applet);
+  ka_preferences_setup_principal_entry (self);
+  ka_preferences_setup_pkuserid_entry (self);
+  ka_preferences_setup_pkuserid_button (self);
+  ka_preferences_setup_smartcard_toggle (self);
+  ka_preferences_setup_pkanchors_entry (self);
+  ka_preferences_setup_pkanchors_button (self);
+
+  ka_preferences_setup_forwardable_toggle (self);
+  ka_preferences_setup_proxiable_toggle (self);
+  ka_preferences_setup_renewable_toggle (self);
+  ka_preferences_setup_prompt_mins_entry (self);
+
+  g_signal_connect (ka_applet_get_settings(self->priv->applet),
+                    "changed",
+                    G_CALLBACK (ka_preferences_settings_changed),
+                    self);
+
+  g_assert (self->priv->n_bindings == N_BINDINGS);
+}
 
-    g_signal_connect (ka_applet_get_settings(applet),
-                      "changed",
-                      G_CALLBACK (ka_preferences_settings_changed),
-                      applet);
 
-    g_assert (prefs.n_bindings == N_BINDINGS);
+static void
+ka_preferences_init (KaPreferences *self)
+{
+    self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
+                                              KA_TYPE_PREFERENCES,
+                                              KaPreferencesPrivate);
 
-    prefs.notebook = WID (prefs.builder, "ka_notebook");
-    prefs.dialog = WID (prefs.builder, "krb5_preferences_dialog");
+    gtk_widget_init_template (GTK_WIDGET (self));
 }
 
-void
-ka_preferences_window_show (KaApplet *applet)
+
+static void
+ka_preferences_class_init (KaPreferencesClass *klass)
 {
-    GtkWindow *parent = ka_applet_last_focused_window (applet);
+    GObjectClass *object_class = G_OBJECT_CLASS (klass);
+    GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+    object_class->constructor = ka_preferences_constructor;
+    object_class->set_property = ka_preferences_set_property;
+    object_class->get_property = ka_preferences_get_property;
+    object_class->constructed = ka_preferences_constructed;
+
+    g_object_class_install_property (object_class,
+                                     PROP_APPLET,
+                                     g_param_spec_object ("applet",
+                                                          "Applet",
+                                                          "The applet we configure",
+                                                          KA_TYPE_APPLET,
+                                                          G_PARAM_CONSTRUCT_ONLY |
+                                                          G_PARAM_READWRITE |
+                                                          G_PARAM_STATIC_STRINGS));
+    /* Bind class to template
+     */
+    gtk_widget_class_set_template_from_resource (widget_class,
+                                                 "/org/gnome/krb5-auth-dialog/ui/ka-preferences.ui");
+
+    gtk_widget_class_bind_template_child_private (widget_class, KaPreferences, principal_entry);
+    gtk_widget_class_bind_template_child_private (widget_class, KaPreferences, pkuserid_button);
+    gtk_widget_class_bind_template_child_private (widget_class, KaPreferences, pkuserid_entry);
+    gtk_widget_class_bind_template_child_private (widget_class, KaPreferences, smartcard_toggle);
+    gtk_widget_class_bind_template_child_private (widget_class, KaPreferences, pkanchors_entry);
+    gtk_widget_class_bind_template_child_private (widget_class, KaPreferences, pkanchors_button);
+    gtk_widget_class_bind_template_child_private (widget_class, KaPreferences, prompt_mins_entry);
+    gtk_widget_class_bind_template_child_private (widget_class, KaPreferences, forwardable_toggle);
+    gtk_widget_class_bind_template_child_private (widget_class, KaPreferences, renewable_toggle);
+    gtk_widget_class_bind_template_child_private (widget_class, KaPreferences, proxiable_toggle);
+    gtk_widget_class_bind_template_child_private (widget_class, KaPreferences, notebook);
+}
 
-    if (parent)
-        gtk_window_set_transient_for (GTK_WINDOW(prefs.dialog),
-                                      GTK_WINDOW(parent));
-    gtk_window_present (GTK_WINDOW(prefs.dialog));
-    gtk_dialog_run (GTK_DIALOG (prefs.dialog));
-    gtk_widget_hide (prefs.dialog);
+
+KaPreferences*
+ka_preferences_new (KaApplet *applet)
+{
+    KaPreferences *self = g_object_new (KA_TYPE_PREFERENCES,
+                                        "applet", applet, NULL);
+    return self;
 }
 
+
 void
-ka_preferences_window_destroy ()
+ka_preferences_run (KaPreferences *self)
 {
-    int i;
-
-    for (i = 0; i < prefs.n_bindings; i++)
-        g_object_unref (prefs.bindings[i]);
+    GtkWindow *parent = ka_applet_last_focused_window (self->priv->applet);
 
-    g_object_unref (prefs.builder);
+    if (parent)
+        gtk_window_set_transient_for (GTK_WINDOW(self),
+                                      GTK_WINDOW(parent));
+    gtk_window_present (GTK_WINDOW(self));
+    gtk_dialog_run (GTK_DIALOG (self));
+    gtk_widget_hide (GTK_WIDGET (self));
 }
 
 /*
diff --git a/src/ka-preferences.h b/src/ka-preferences.h
index 6249a10..4b6a732 100644
--- a/src/ka-preferences.h
+++ b/src/ka-preferences.h
@@ -3,15 +3,34 @@
 #ifndef KA_PREFERENCES_H
 #define KA_PREFERENCES_H
 
+#include <glib.h>
+#include <glib-object.h>
 #include <gtk/gtk.h>
 
+#include "config.h"
 #include "ka-applet.h"
 
 G_BEGIN_DECLS
 
-void ka_preferences_window_create (KaApplet *applet);
-void ka_preferences_window_destroy (void);
-void ka_preferences_window_show (KaApplet *applet);
+#define KA_TYPE_PREFERENCES            (ka_preferences_get_type ())
+#define KA_PREFERENCES(obj)            \
+    (G_TYPE_CHECK_INSTANCE_CAST ((obj), KA_TYPE_PREFERENCES, KaPreferences))
+#define KA_PREFERENCES_CLASS(klass)    \
+    (G_TYPE_CHECK_CLASS_CAST ((klass), KA_TYPE_PREFERENCES, KaPreferencesClass))
+#define KA_IS_PREFERENCES(obj)         \
+    (G_TYPE_CHECK_INSTANCE_TYPE ((obj), KA_TYPE_PREFERENCES))
+#define KA_IS_PREFERENCES_CLASS(klass) \
+    (G_TYPE_CHECK_CLASS_TYPE ((klass), KA_TYPE_PREFERENCES))
+#define KA_PREFERENCES_GET_CLASS(obj)  \
+    (G_TYPE_INSTANCE_GET_CLASS ((obj), KA_TYPE_PREFERENCES, KaPreferencesClass))
+    typedef struct _KaPreferences KaPreferences;
+typedef struct _KaPreferencesClass KaPreferencesClass;
+typedef struct _KaPreferencesPrivate KaPreferencesPrivate;
+
+GType ka_preferences_get_type (void);
+
+KaPreferences* ka_preferences_new (KaApplet *applet);
+void ka_preferences_run (KaPreferences *self);
 
 
 G_END_DECLS
diff --git a/src/resources/ui/ka-preferences.ui b/src/resources/ui/ka-preferences.ui
index 9194871..8b880eb 100644
--- a/src/resources/ui/ka-preferences.ui
+++ b/src/resources/ui/ka-preferences.ui
@@ -6,7 +6,7 @@
     <property name="step_increment">1</property>
     <property name="page_increment">10</property>
   </object>
-  <object class="GtkDialog" id="krb5_preferences_dialog">
+  <template class="KaPreferences">
     <property name="can_focus">False</property>
     <property name="border_width">5</property>
     <property name="type_hint">dialog</property>
@@ -45,7 +45,7 @@
           </packing>
         </child>
         <child>
-          <object class="GtkNotebook" id="ka_notebook">
+          <object class="GtkNotebook" id="notebook">
             <property name="visible">True</property>
             <property name="can_focus">True</property>
             <child>
@@ -787,5 +787,5 @@
     <action-widgets>
       <action-widget response="0">button1</action-widget>
     </action-widgets>
-  </object>
+  </template>
 </interface>


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