[krb5-auth-dialog] Port to GDBus



commit ce202b6975ff0390ac2ef56d2f24f5f15b56c384
Author: Guido Günther <agx sigxcpu org>
Date:   Fri Feb 25 23:09:22 2011 +0100

    Port to GDBus
    
    https://bugzilla.gnome.org/show_bug.cgi?id=622885

 configure.ac            |   12 +--
 etpo/Makefile.am        |    2 -
 plugins/ka-plugin-afs.c |    6 +-
 src/.gitignore          |    1 -
 src/Makefile.am         |   14 ---
 src/ka-applet-priv.h    |    5 +
 src/ka-applet.c         |   28 ++++--
 src/ka-dbus.c           |  249 +++++++++++++++++++++++++++++++++++-----------
 src/ka-dbus.h           |   12 +--
 src/ka-dbus.xml         |   30 ------
 src/ka-dialog.c         |   15 ++-
 11 files changed, 234 insertions(+), 140 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 80509e2..7c588c6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -40,22 +40,19 @@ AC_MSG_RESULT([$with_gtk])
 case "$with_gtk" in
   2.0) GTK_API_VERSION=2.0
        GTK_REQUIRED=2.18.0
-       GLIB_REQUIRED=2.16.0
-       DBUS_REQUIRED=0.60
+       GLIB_REQUIRED=2.26.0
        GCONF_REQUIRED=2.8
        LIBNOTIFY_REQUIRED=0.4
        ;;
   3.0) GTK_API_VERSION=3.0
        GTK_REQUIRED=2.90.4
-       GLIB_REQUIRED="2.25.10"
-       DBUS_REQUIRED="0.82"
-       GCONF_REQUIRED="2.31"
-       LIBNOTIFY_REQUIRED="0.5.1"
+       GLIB_REQUIRED=2.26.0
+       GCONF_REQUIRED=2.31
+       LIBNOTIFY_REQUIRED=0.5.1
        ;;
 esac
 
 PKG_CHECK_MODULES(GTK, [gtk+-$GTK_API_VERSION >= $GTK_REQUIRED, glib-2.0 >= $GLIB_REQUIRED])
-PKG_CHECK_MODULES(DBUS, dbus-glib-1 >= $DBUS_REQUIRED)
 PKG_CHECK_MODULES(GCONF, gconf-2.0 >= $GCONF_REQUIRED)
 PKG_CHECK_MODULES(GIO, [gio-2.0, gio-unix-2.0])
 PKG_CHECK_MODULES(GMODULE, gmodule-2.0)
@@ -293,7 +290,6 @@ AC_MSG_NOTICE([Libraries])
 AC_MSG_NOTICE([])
 AC_MSG_NOTICE([         Kerberos: $KRB5_CFLAGS $KRB5_LIBS])
 AC_MSG_NOTICE([              GTK: $GTK_CFLAGS $GTK_LIBS])
-AC_MSG_NOTICE([             DBus: $DBUS_CFLAGS $DBUS_LIBS])
 AC_MSG_NOTICE([            gconf: $GCONF_CFLAGS $GCONF_LIBS])
 AC_MSG_NOTICE([         gio-unix: $GIO_UNIX_CFLAGS $GIO_UNIX_LIBS $GIO_CFLAGS $GIO_LIBS])
 if test "$with_libnotify" = "yes" ; then
diff --git a/etpo/Makefile.am b/etpo/Makefile.am
index 2a92ecd..e7fa933 100644
--- a/etpo/Makefile.am
+++ b/etpo/Makefile.am
@@ -4,7 +4,6 @@ lexer.c: grammar.h
 AM_YFLAGS=-d
 AM_CFLAGS=\
 	@GTK_CFLAGS@ 		 \
-	@DBUS_CFLAGS@		 \
 	@GCONF_CFLAGS@		 \
 	@KRB5_CFLAGS@		 \
 	@NETWORK_MANAGER_CFLAGS@ \
@@ -14,7 +13,6 @@ AM_LDFLAGS=\
 	@NETWORK_MANAGER_LIBS@ \
 	@KRB5_LIBS@ \
 	@LIBNOTIFY_LIBS@ \
-	@DBUS_LIBS@ \
 	@GCONF_LIBS@ \
 	@GTK_LIBS@
 
diff --git a/plugins/ka-plugin-afs.c b/plugins/ka-plugin-afs.c
index 57daed0..831e016 100644
--- a/plugins/ka-plugin-afs.c
+++ b/plugins/ka-plugin-afs.c
@@ -39,7 +39,7 @@ struct _KaPluginAfsPrivate {
 };
 
 static void
-event_cb (gpointer *applet, gchar *princ, guint when, gpointer user_data)
+event_cb (gpointer *applet, gchar *princ, guint when, gpointer user_data G_GNUC_UNUSED)
 {
     GError *err = NULL;
     gboolean ret;
@@ -73,10 +73,10 @@ ka_plugin_afs_activate (KaPlugin *self, KaApplet *applet)
 
     priv->handlers[0] = g_signal_connect (applet,
                                           "krb-tgt-acquired",
-                                          G_CALLBACK (event_cb), "Acquired");
+                                          G_CALLBACK (event_cb), NULL);
     priv->handlers[1] = g_signal_connect (applet,
                                           "krb-tgt-renewed",
-                                          G_CALLBACK (event_cb), "Renewed");
+                                          G_CALLBACK (event_cb), NULL);
 }
 
 static void
diff --git a/src/.gitignore b/src/.gitignore
index ce314bc..6d9018c 100644
--- a/src/.gitignore
+++ b/src/.gitignore
@@ -1,4 +1,3 @@
-ka-dbus-glue.h
 krb5-auth-dialog
 krb5-auth-dialog.1
 krb5-auth-dialog.desktop
diff --git a/src/Makefile.am b/src/Makefile.am
index fb64219..65d2a92 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -66,7 +66,6 @@ krb5_auth_dialog_CPPFLAGS = \
 krb5_auth_dialog_CFLAGS = \
 	$(GMODULE_CFLAGS) \
 	$(GTK_CFLAGS) \
-	$(DBUS_CFLAGS) \
 	$(GCONF_CFLAGS) \
 	$(KRB5_CFLAGS) \
 	$(NETWORK_MANAGER_CFLAGS) \
@@ -81,23 +80,12 @@ krb5_auth_dialog_LDADD = \
 	$(LIBCAP) \
 	$(KRB5_LIBS) \
 	$(LIBNOTIFY_LIBS) \
-	$(DBUS_LIBS) \
 	$(GCONF_LIBS) \
 	$(GIO_LIBS) \
 	$(GMODULE_LIBS) \
 	$(GTK_LIBS) \
 	$(NULL)
 
-BUILT_SOURCES = \
-	ka-dbus-glue.h \
-	$(NULL)
-
-ka-dbus-glue.h: $(srcdir)/ka-dbus.xml
-	dbus-binding-tool                 \
-		--mode=glib-server        \
-		--prefix=krb5_auth_dialog \
-		$< > $@
-
 AM_CPPFLAGS = \
 	-I $(top_srcdir)/secmem/ \
 	$(DISABLE_DEPRECATED) \
@@ -113,14 +101,12 @@ EXTRA_DIST = \
 	$(schema_in_files) \
 	$(autostart_in_files) \
 	$(service_in_files) \
-	$(srcdir)/ka-dbus.xml \
 	krb5-auth-dialog.1.in \
 	$(NULL)
 
 CLEANFILES = $(schema_DATA) $(service_DATA)
 DISTCLEANFILES = \
 	krb5-auth-dialog.desktop \
-	ka-dbus-glue.h \
 	$(NULL)
 
 install-data-local: $(schema_DATA)
diff --git a/src/ka-applet-priv.h b/src/ka-applet-priv.h
index a64848c..bd55996 100644
--- a/src/ka-applet-priv.h
+++ b/src/ka-applet-priv.h
@@ -42,6 +42,8 @@ G_BEGIN_DECLS
     KA_SIGNAL_COUNT
 } KaAppletSignalNumber;
 
+extern const gchar *ka_signal_names[];
+
 /* public functions */
 gboolean ka_applet_get_show_trayicon (const KaApplet *self);
 void ka_applet_set_tgt_renewable (KaApplet *self, gboolean renewable);
@@ -53,8 +55,11 @@ void ka_applet_signal_emit (KaApplet *self, KaAppletSignalNumber signum,
                             krb5_timestamp expiry);
 void ka_applet_set_msg (KaApplet *self, const char *msg);
 
+
 /* create the applet */
 KaApplet *ka_applet_create (void);
+/* destroy the applet */
+void ka_applet_destroy (KaApplet *self);
 
 /* update tooltip and icon */
 int ka_applet_update_status (KaApplet *self, krb5_timestamp expiry);
diff --git a/src/ka-applet.c b/src/ka-applet.c
index 7de9204..2d4981e 100644
--- a/src/ka-applet.c
+++ b/src/ka-applet.c
@@ -51,6 +51,14 @@ enum {
     KA_PROP_TGT_RENEWABLE,
 };
 
+
+const gchar *ka_signal_names[KA_SIGNAL_COUNT] = {
+    "krb-tgt-acquired",
+    "krb-tgt-renewed",
+    "krb-tgt-expired",
+};
+
+
 struct _KaApplet {
     GObject parent;
 
@@ -252,12 +260,6 @@ ka_applet_class_init (KaAppletClass *klass)
 {
     GObjectClass *object_class = G_OBJECT_CLASS (klass);
     GParamSpec *pspec;
-
-    const gchar *signalNames[KA_SIGNAL_COUNT] = {
-        "krb-tgt-acquired",
-        "krb-tgt-renewed",
-        "krb-tgt-expired"
-    };
     int i;
 
     object_class->dispose = ka_applet_dispose;
@@ -320,7 +322,7 @@ ka_applet_class_init (KaAppletClass *klass)
     for (i = 0; i < KA_SIGNAL_COUNT; i++) {
         guint signalId;
 
-        signalId = g_signal_new (signalNames[i], G_OBJECT_CLASS_TYPE (klass),
+        signalId = g_signal_new (ka_signal_names[i], G_OBJECT_CLASS_TYPE (klass),
                                  G_SIGNAL_RUN_LAST, 0, NULL, NULL,
                                  ka_closure_VOID__STRING_UINT,
                                  G_TYPE_NONE, 2,   /* number of parameters */
@@ -707,8 +709,7 @@ ka_applet_cb_quit (GtkMenuItem *menuitem G_GNUC_UNUSED, gpointer user_data)
 {
     KaApplet *applet = KA_APPLET (user_data);
 
-    g_object_unref (applet);
-    gtk_main_quit ();
+    ka_applet_destroy (applet);
 }
 
 
@@ -978,6 +979,15 @@ ka_ns_check_persistence (KaApplet *self)
 }
 
 
+/* destroy the applet and quit */
+void
+ka_applet_destroy (KaApplet* applet)
+{
+    g_object_unref (applet);
+    gtk_main_quit ();
+}
+
+
 /* create the tray icon applet */
 KaApplet *
 ka_applet_create ()
diff --git a/src/ka-dbus.c b/src/ka-dbus.c
index 7b50a06..4a7be2c 100644
--- a/src/ka-dbus.c
+++ b/src/ka-dbus.c
@@ -1,6 +1,6 @@
 /* Krb5 Auth Applet -- Acquire and release kerberos tickets
  *
- * (C) 2008,2009 Guido Guenther <agx sigxcpu org>
+ * (C) 2008,2009,2011 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
@@ -20,98 +20,227 @@
 
 #include "config.h"
 
-#include <dbus/dbus-glib.h>
 #include "ka-applet-priv.h"
 #include "ka-dialog.h"
 #include "ka-dbus.h"
-#include "ka-dbus-glue.h"
 
-static DBusGConnection *session;
+static guint dbus_owner_id;
+static GDBusConnection *dbus_connection;
+static const char *dbus_object_path = "/org/gnome/KrbAuthDialog";
+static const char *dbus_interface_name = "org.gnome.KrbAuthDialog";
+static GDBusNodeInfo *introspection_data;
+
 
 gboolean
 ka_dbus_acquire_tgt (KaApplet *applet,
-                     const gchar *principal, DBusGMethodInvocation *context)
+                     const gchar *principal)
 {
     gboolean success;
 
     KA_DEBUG ("Getting TGT for '%s'", principal);
     success = ka_check_credentials (applet, principal);
-    dbus_g_method_return (context, success);
-    return TRUE;
+    return success;
 }
 
 
 gboolean
-ka_dbus_destroy_ccache (KaApplet *applet, DBusGMethodInvocation *context)
+ka_dbus_destroy_ccache (KaApplet *applet)
 {
     gboolean success;
 
     KA_DEBUG ("Destroying ticket cache");
     success = ka_destroy_ccache (applet);
-    dbus_g_method_return (context, success);
-    return TRUE;
+    return success;
 }
 
 
-gboolean
-ka_dbus_service (KaApplet *applet)
+static const gchar ka_dbus_introspection_xml[] =
+    "<node>"
+    "  <interface name='org.gnome.KrbAuthDialog'>"
+    "    <method name='acquireTgt'>"
+    "      <arg type='s' name='principal' direction='in' />"
+    "      <arg type='b' name='success' direction='out'/>"
+    "    </method>"
+    "    <method name='destroyCCache'>"
+    "      <arg type='b' name='success' direction='out'/>"
+    "    </method>"
+    "    <signal name='krb_tgt_acquired'>"
+    "       <arg type='s' name='principal' direction ='out'/>"
+    "       <arg type='u' name='expiry' direction ='out'/>"
+    "    </signal>"
+    "    <signal name='krb_tgt_renewed'>"
+    "       <arg type='s' name='principal' direction ='out'/>"
+    "       <arg type='u' name='expiry' direction ='out'/>"
+    "    </signal>"
+    "    <signal name='krb_tgt_expired'>"
+    "       <arg type='s' name='principal' direction ='out'/>"
+    "       <arg type='u' name='expiry' direction ='out'/>"
+    "    </signal>"
+    "  </interface>"
+    "</node>";
+
+
+static void
+ka_dbus_handle_method_call (GDBusConnection       *connection G_GNUC_UNUSED,
+                            const gchar           *sender G_GNUC_UNUSED,
+                            const gchar           *object_path G_GNUC_UNUSED,
+                            const gchar           *interface_name G_GNUC_UNUSED,
+                            const gchar           *method_name,
+                            GVariant              *parameters,
+                            GDBusMethodInvocation *invocation,
+                            gpointer               user_data)
 {
-    dbus_g_connection_register_g_object (session,
-                                         "/org/gnome/KrbAuthDialog",
-                                         G_OBJECT (applet));
-    return TRUE;
+    KaApplet *applet = user_data;
+    gboolean ret;
+
+    g_warn_if_fail (applet != NULL);
+
+    if (g_strcmp0 (method_name, "acquireTgt") == 0) {
+            const char *principal;
+
+            g_variant_get (parameters, "(s)", &principal);
+            ret = ka_dbus_acquire_tgt (applet, principal);
+            g_dbus_method_invocation_return_value (invocation,
+                                                   g_variant_new("(b)", ret));
+    } else if (g_strcmp0 (method_name, "destroyCCache") == 0) {
+            ret = ka_dbus_destroy_ccache (applet);
+            g_dbus_method_invocation_return_value (invocation,
+                                                   g_variant_new("(b)", ret));
+    }
 }
 
+static gchar* ka_dbus_signal_name (const gchar *name)
+{
+    gchar *c;
+    gchar *signal_name = g_strdup(name);
 
-gboolean
-ka_dbus_connect (unsigned int *status)
+   /* The DBus signal names use underscores */
+   for (c = signal_name; *c != '\0'; c++ ) {
+       if (*c == '-')
+           *c = '_';
+   }
+
+   return signal_name;
+}
+
+/* Emit DBus signals */
+static void
+ka_dbus_signal_cb (gpointer *applet G_GNUC_UNUSED,
+                   gchar *princ,
+                   guint when, gpointer user_data)
 {
-    guint request_name_reply;
-    unsigned int flags;
-    DBusGProxy *bus_proxy;
     GError *error = NULL;
-
-    /* Connect to the session bus so we get exit-on-disconnect semantics. */
-    session = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
-    if (session == NULL) {
-        g_error ("couldn't connect to session bus: %s",
-                 (error) ? error->message : "(null)");
-        *status = 1;
+    gchar *signal_name;
+
+    signal_name = ka_dbus_signal_name(user_data);
+    if (!g_dbus_connection_emit_signal (dbus_connection,
+                                        NULL,
+                                        dbus_object_path,
+                                        dbus_interface_name,
+                                        signal_name,
+                                        g_variant_new ("(su)",
+                                                       princ,
+                                                       when),
+                                        &error)) {
+        g_warning ("Failed to emit DBus signal %s: %s",
+                   signal_name,
+                   error->message);
         g_clear_error (&error);
-        return FALSE;
     }
-    flags = DBUS_NAME_FLAG_DO_NOT_QUEUE;
-    bus_proxy = dbus_g_proxy_new_for_name (session,
-                                           "org.freedesktop.DBus",
-                                           "/org/freedesktop/DBus",
-                                           "org.freedesktop.DBus");
-
-    dbus_g_object_type_install_info (KA_TYPE_APPLET,
-                                     &dbus_glib_krb5_auth_dialog_object_info);
-
-    if (!dbus_g_proxy_call (bus_proxy,
-                            "RequestName",
-                            &error,
-                            G_TYPE_STRING,
-                            "org.gnome.KrbAuthDialog",
-                            G_TYPE_UINT,
-                            flags,
-                            G_TYPE_INVALID,
-                            G_TYPE_UINT,
-                            &request_name_reply, G_TYPE_INVALID)) {
-        g_warning ("Failed to invoke RequestName: %s", error->message);
+    g_free (signal_name);
+}
+
+
+static void
+ka_dbus_connect_signals(KaApplet *applet)
+{
+    int i;
+
+    for (i = 0; i < KA_SIGNAL_COUNT; i++) {
+        g_signal_connect (applet, ka_signal_names[i],
+                          G_CALLBACK (ka_dbus_signal_cb),
+                          (gpointer)ka_signal_names[i]);
+    }
+}
+
+
+static const GDBusInterfaceVTable interface_vtable =
+{
+  .method_call = ka_dbus_handle_method_call,
+};
+
+
+static void
+ka_dbus_on_name_acquired (GDBusConnection *connection,
+                          const gchar *name G_GNUC_UNUSED,
+                          gpointer user_data)
+{
+    KaApplet *applet = user_data;
+    guint id;
+
+    introspection_data = g_dbus_node_info_new_for_xml (
+        ka_dbus_introspection_xml,
+        NULL);
+
+    id = g_dbus_connection_register_object (connection,
+                                        "/org/gnome/KrbAuthDialog",
+                                        introspection_data->interfaces[0],
+                                        &interface_vtable,
+                                        applet,
+                                        NULL,  /* user_data_free_func */
+                                        NULL); /* GError** */
+    if (!id)
+        g_error ("Failed to regiester DBus object");
+    ka_dbus_connect_signals (applet);
+
+    dbus_connection = connection;
+}
+
+
+static void
+ka_dbus_on_name_lost (GDBusConnection *connection G_GNUC_UNUSED,
+                      const gchar *name G_GNUC_UNUSED,
+                      gpointer user_data)
+{
+    KaApplet *applet = user_data;
+
+    g_warning ("Cannot acquire DBUS name");
+    ka_applet_destroy (applet);
+}
+
+
+void
+ka_dbus_disconnect ()
+{
+    if (introspection_data) {
+        g_dbus_node_info_unref (introspection_data);
+        introspection_data = NULL;
     }
-    g_clear_error (&error);
-    g_object_unref (bus_proxy);
-
-    if (request_name_reply == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER
-        || request_name_reply == DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER);
-    else if (request_name_reply == DBUS_REQUEST_NAME_REPLY_EXISTS
-             || request_name_reply == DBUS_REQUEST_NAME_REPLY_IN_QUEUE) {
-        *status = 0;
-        return FALSE;
-    } else {
-        g_assert_not_reached ();
+
+    if (dbus_owner_id) {
+        g_bus_unown_name (dbus_owner_id);
+        dbus_owner_id = 0;
     }
+    dbus_connection = NULL;
+}
+
+
+gboolean
+ka_dbus_connect (KaApplet *applet)
+{
+    g_return_val_if_fail (applet != 0, FALSE);
+
+    dbus_owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
+                                    "org.gnome.KrbAuthDialog",
+                                    G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT,
+                                    NULL,
+                                    ka_dbus_on_name_acquired,
+                                    ka_dbus_on_name_lost,
+                                    applet,
+                                    NULL);
     return TRUE;
 }
+
+/*
+ * vim:ts=4:sts=4:sw=4:et:
+ */
diff --git a/src/ka-dbus.h b/src/ka-dbus.h
index eda0974..41b30d7 100644
--- a/src/ka-dbus.h
+++ b/src/ka-dbus.h
@@ -22,16 +22,12 @@
 #define KA_DBUS_H
 
 #include <glib.h>
-#include <dbus/dbus-glib.h>
-#include <dbus/dbus-glib-lowlevel.h>
 #include "ka-applet-priv.h"
 
-gboolean ka_dbus_connect (unsigned int *status);
-gboolean ka_dbus_service (KaApplet *applet);
+gboolean ka_dbus_connect (KaApplet *applet);
+void ka_dbus_disconnect (void);
 gboolean ka_dbus_acquire_tgt (KaApplet *applet,
-                              const gchar *principal,
-                              DBusGMethodInvocation *context);
-gboolean ka_dbus_destroy_ccache (KaApplet *applet,
-                                 DBusGMethodInvocation *context);
+                              const gchar *principal);
+gboolean ka_dbus_destroy_ccache (KaApplet *applet);
 
 #endif /* KA_DBUS_H */
diff --git a/src/ka-dialog.c b/src/ka-dialog.c
index e6446c7..5b11621 100644
--- a/src/ka-dialog.c
+++ b/src/ka-dialog.c
@@ -1055,7 +1055,6 @@ main (int argc, char *argv[])
     GOptionContext *context;
     GError *error = NULL;
 
-    guint status = 0;
     gboolean run_auto = FALSE, run_always = FALSE;
 
     const char *help_msg =
@@ -1088,9 +1087,6 @@ main (int argc, char *argv[])
     bindtextdomain (PACKAGE, LOCALE_DIR);
     ka_secmem_init ();
 
-    if (!ka_dbus_connect (&status))
-        exit (status);
-
     always_run = !run_auto;
     if (using_krb5 () || always_run) {
         g_set_application_name (KA_NAME);
@@ -1098,6 +1094,11 @@ main (int argc, char *argv[])
         applet = ka_applet_create ();
         if (!applet)
             return 1;
+
+        if (!ka_dbus_connect (applet)) {
+            ka_applet_destroy (applet);
+            return 1;
+        }
         ka_nm_init ();
 
         g_timeout_add_seconds (CREDENTIAL_CHECK_INTERVAL,
@@ -1105,11 +1106,15 @@ main (int argc, char *argv[])
         g_idle_add ((GSourceFunc) credentials_expiring_once, applet);
         monitor = monitor_ccache (applet);
 
-        ka_dbus_service (applet);
         gtk_main ();
     }
+    ka_dbus_disconnect ();
     ka_nm_shutdown ();
     if (monitor)
         g_object_unref (monitor);
     return 0;
 }
+
+/*
+ * vim:ts=4:sts=4:sw=4:et:
+ */



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