[gnome-panel/wip/gnome-3.10+] use dbus to register with session manager



commit 2ba0cdf4a98e76165434e4654ed88e9e77809dd6
Author: Alberts Muktupāvels <alberts muktupavels gmail com>
Date:   Sun Oct 12 20:59:52 2014 +0300

    use dbus to register with session manager

 gnome-panel/main.c          |    9 +--
 gnome-panel/panel-session.c |  208 ++++++++++++++++++++++++++++++++++++-------
 gnome-panel/panel-session.h |   33 ++++++--
 gnome-panel/panel-shell.c   |    2 -
 4 files changed, 204 insertions(+), 48 deletions(-)
---
diff --git a/gnome-panel/main.c b/gnome-panel/main.c
index dbeb19c..2d06418 100644
--- a/gnome-panel/main.c
+++ b/gnome-panel/main.c
@@ -82,14 +82,12 @@ main (int argc, char **argv)
        GOptionContext *context;
        GError         *error;
        GtkSettings    *settings;
+       PanelSession   *session;
 
        bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
        bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
        textdomain (GETTEXT_PACKAGE);
 
-       /* We will register explicitly when we're ready -- see panel-session.c */
-       egg_sm_client_set_mode (EGG_SM_CLIENT_MODE_DISABLED);
-
        g_set_prgname ("gnome-panel");
 
        desktopfile = panel_g_lookup_in_applications_dirs ("gnome-panel.desktop");
@@ -99,8 +97,6 @@ main (int argc, char **argv)
        }
 
        context = g_option_context_new ("");
-       g_option_context_add_group (context,
-                                   egg_sm_client_get_option_group ());
        g_option_context_add_group (context, gtk_get_option_group (TRUE));
        g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE);
 
@@ -143,7 +139,7 @@ main (int argc, char **argv)
 
        /* Do this at the end, to be sure that we're really ready when
         * connecting to the session manager */
-       panel_session_init ();
+       session = panel_session_new ();
 
        settings = gtk_settings_get_default ();
        g_signal_connect (settings, "notify::gtk-theme-name", G_CALLBACK (theme_changed), NULL);
@@ -151,6 +147,7 @@ main (int argc, char **argv)
 
        gtk_main ();
 
+       g_object_unref (session);
        panel_cleanup_do ();
 
        return 0;
diff --git a/gnome-panel/panel-session.c b/gnome-panel/panel-session.c
index e9b4d37..a8e6f47 100644
--- a/gnome-panel/panel-session.c
+++ b/gnome-panel/panel-session.c
@@ -18,61 +18,203 @@
  *
  * Authors:
  *     Mark McLoughlin <mark skynet ie>
+ *     Alberts Muktupāvels <alberts muktupavels gmail com>
  */
 
 #include <config.h>
-
 #include <stdlib.h>
-
-#include <gdk/gdkx.h>
-
-#include <libegg/eggsmclient.h>
+#include <gio/gio.h>
 
 #include "panel-shell.h"
-
 #include "panel-session.h"
 
-static gboolean do_not_restart = FALSE;
+struct _PanelSessionPrivate {
+       GDBusConnection *session;
+       GDBusProxy      *sm_proxy;
+       GDBusProxy      *client_proxy;
+       gchar           *client_path;
+};
+
+G_DEFINE_TYPE_WITH_PRIVATE(PanelSession, panel_session, G_TYPE_OBJECT);
+
+static void
+free_client_path (PanelSessionPrivate *priv)
+{
+       if (priv->client_path) {
+               g_free (priv->client_path);
+               priv->client_path = NULL;
+       }
+}
 
 static void
-panel_session_handle_quit (EggSMClient *client,
-                          gpointer     data)
+unregister_client (PanelSessionPrivate *priv)
 {
-       panel_shell_quit ();
+       GError *error;
+
+       error = NULL;
+
+       g_dbus_proxy_call_sync (priv->sm_proxy,
+                               "UnregisterClient",
+                               g_variant_new ("(o)", priv->client_path),
+                               G_DBUS_CALL_FLAGS_NONE,
+                               G_MAXINT,
+                               NULL,
+                               &error);
+
+       if (error) {
+               g_warning ("Failed to unregister client: %s", error->message);
+               g_error_free (error);
+       }
+
+       g_clear_object (&priv->client_proxy);
+       free_client_path (priv);
 }
 
-void
-panel_session_do_not_restart (void)
+static void
+send_end_session_response (PanelSessionPrivate *priv)
 {
-       do_not_restart = TRUE;
+       g_dbus_proxy_call (priv->client_proxy,
+                          "EndSessionResponse",
+                          g_variant_new ("(bs)", TRUE, ""),
+                          G_DBUS_CALL_FLAGS_NONE,
+                          G_MAXINT,
+                          NULL, NULL, NULL);
+}
 
-       if (egg_sm_client_get_mode () != EGG_SM_CLIENT_MODE_DISABLED)
-               egg_sm_client_set_mode (EGG_SM_CLIENT_MODE_NO_RESTART);
+static void
+client_proxy_signal (GDBusProxy  *proxy,
+                     const gchar *sender_name,
+                     const gchar *signal_name,
+                     GVariant    *parameters,
+                     gpointer     user_data)
+{
+       PanelSessionPrivate *priv;
+
+       priv = user_data;
+
+       if (g_str_equal (signal_name, "QueryEndSession")) {
+               send_end_session_response (priv);
+       } else if (g_str_equal (signal_name, "CancelEndSession")) {
+       } else if (g_str_equal (signal_name, "EndSession")) {
+               send_end_session_response (priv);
+               unregister_client (priv);
+               panel_shell_quit ();
+       } else if (g_str_equal (signal_name, "Stop")) {
+               unregister_client (priv);
+               panel_shell_quit ();
+       }
 }
 
-void
-panel_session_init (void)
+static void
+panel_session_finalize (GObject *object)
 {
-       EggSMClientMode  mode;
-       EggSMClient     *client;
+       PanelSession *session;
+       PanelSessionPrivate *priv;
 
-       /* Explicitly tell the session manager we're ready -- we don't do it
-        * before. Note: this depends on setting the mode to DISABLED early
-        * during startup. */
+       session = PANEL_SESSION (object);
+       priv = session->priv;
+
+       free_client_path (priv);
+       g_clear_object (&priv->client_proxy);
+       g_clear_object (&priv->sm_proxy);
+       g_clear_object (&priv->session);
+
+       G_OBJECT_CLASS (panel_session_parent_class)->finalize (object);
+}
 
-        if (do_not_restart || getenv ("GNOME_PANEL_DEBUG"))
-               mode = EGG_SM_CLIENT_MODE_NO_RESTART;
-       else
-               mode = EGG_SM_CLIENT_MODE_NORMAL;
+static void
+panel_session_class_init (PanelSessionClass *session_class)
+{
+       GObjectClass *object_class;
 
-       egg_sm_client_set_mode (mode);
+       object_class = G_OBJECT_CLASS (session_class);
 
-       client = egg_sm_client_get ();
+       object_class->finalize = panel_session_finalize;
+}
 
-       g_signal_connect (client, "quit",
-                         G_CALLBACK (panel_session_handle_quit), NULL);
+static void
+panel_session_init (PanelSession *session)
+{
+       PanelSessionPrivate *priv;
+       GError *error;
+       const gchar *app_id;
+       const gchar *desktop_autostart_id;
+       const gchar *client_startup_id;
+       GVariant *res;
+
+       session->priv = panel_session_get_instance_private (session);
+
+       priv = session->priv;
+       error = NULL;
+
+       priv->session = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
+
+       if (!priv->session) {
+               g_warning ("Failed to get a session bus: %s", error->message);
+               g_error_free (error);
+               return;
+       }
+
+       priv->sm_proxy = g_dbus_proxy_new_sync (priv->session,
+                                               G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
+                                               G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
+                                               NULL,
+                                               "org.gnome.SessionManager",
+                                               "/org/gnome/SessionManager",
+                                               "org.gnome.SessionManager",
+                                               NULL,
+                                               &error);
+
+       if (error) {
+               g_warning ("Failed to get a session proxy: %s", error->message);
+               g_error_free (error);
+               return;
+       }
+
+       app_id = g_get_prgname ();
+       desktop_autostart_id = g_getenv ("DESKTOP_AUTOSTART_ID");
+       client_startup_id = desktop_autostart_id ? desktop_autostart_id : "";
+
+       res = g_dbus_proxy_call_sync (priv->sm_proxy,
+                                     "RegisterClient",
+                                     g_variant_new ("(ss)", app_id, client_startup_id),
+                                     G_DBUS_CALL_FLAGS_NONE,
+                                     G_MAXINT,
+                                     NULL,
+                                     &error);
+
+       if (error) {
+               g_warning ("Failed to register client: %s", error->message);
+               g_error_free (error);
+               g_clear_object (&priv->sm_proxy);
+               return;
+       }
+
+       g_variant_get (res, "(o)", &priv->client_path);
+       g_variant_unref (res);
+
+       priv->client_proxy = g_dbus_proxy_new_sync (priv->session, 0,
+                                                   NULL,
+                                                   "org.gnome.SessionManager",
+                                                   priv->client_path,
+                                                   "org.gnome.SessionManager.ClientPrivate",
+                                                   NULL,
+                                                   &error);
+
+       if (error) {
+               g_warning ("Failed to get client proxy: %s", error->message);
+               g_error_free (error);
+               g_clear_object (&priv->sm_proxy);
+               free_client_path (priv);
+               return;
+       }
+
+       g_signal_connect (priv->client_proxy, "g-signal",
+                         G_CALLBACK (client_proxy_signal), priv);
+}
 
-       /* We don't want the WM to try and save/restore our
-        * window position */
-       gdk_x11_set_sm_client_id (NULL);
+PanelSession *
+panel_session_new (void)
+{
+       return g_object_new (PANEL_TYPE_SESSION, NULL);
 }
diff --git a/gnome-panel/panel-session.h b/gnome-panel/panel-session.h
index 7d35c9f..4ce56d3 100644
--- a/gnome-panel/panel-session.h
+++ b/gnome-panel/panel-session.h
@@ -18,16 +18,35 @@
  *
  * Authors:
  *     Mark McLoughlin <mark skynet ie>
+ *     Alberts Muktupāvels <alberts muktupavels gmail com>
  */
 
-#ifndef __PANEL_SESSION_H__
-#define __PANEL_SESSION_H__
+#ifndef PANEL_SESSION_H
+#define PANEL_SESSION_H
 
-G_BEGIN_DECLS
+#include <glib-object.h>
 
-void panel_session_init           (void);
-void panel_session_do_not_restart (void);
+#define PANEL_TYPE_SESSION         (panel_session_get_type ())
+#define PANEL_SESSION(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), PANEL_TYPE_SESSION, PanelSession))
+#define PANEL_SESSION_CLASS(c)     (G_TYPE_CHECK_CLASS_CAST ((c),    PANEL_TYPE_SESSION, PanelSessionClass))
+#define PANEL_IS_SESSION(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), PANEL_TYPE_SESSION))
+#define PANEL_IS_SESSION_CLASS(c)  (G_TYPE_CHECK_CLASS_TYPE ((c),    PANEL_TYPE_SESSION))
+#define PANEL_SESSION_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o),  PANEL_TYPE_SESSION, PanelSessionClass))
 
-G_END_DECLS
+typedef struct _PanelSession        PanelSession;
+typedef struct _PanelSessionClass   PanelSessionClass;
+typedef struct _PanelSessionPrivate PanelSessionPrivate;
 
-#endif /* __PANEL_SESSION_H__ */
+struct _PanelSession {
+       GObject              parent;
+       PanelSessionPrivate *priv;
+};
+
+struct _PanelSessionClass {
+    GObjectClass parent_class;
+};
+
+GType         panel_session_get_type (void) G_GNUC_CONST;
+PanelSession *panel_session_new      (void);
+
+#endif
diff --git a/gnome-panel/panel-shell.c b/gnome-panel/panel-shell.c
index 2abdc50..0171668 100644
--- a/gnome-panel/panel-shell.c
+++ b/gnome-panel/panel-shell.c
@@ -53,7 +53,6 @@ panel_shell_on_name_lost (GDBusConnection *connection,
         * Tell the SM not to restart us automatically, then exit. */
        g_printerr ("Panel leaving: a new panel shell is starting.\n");
 
-       panel_session_do_not_restart ();
        panel_shell_quit ();
 }
 
@@ -142,7 +141,6 @@ panel_shell_register (gboolean replace)
 register_out:
 
        if (!retval) {
-               panel_session_do_not_restart ();
                panel_shell_cleanup (NULL);
        }
 


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