[gnome-panel/wip/gnome-3.10+] panel-shell: convert to a GObject



commit b29afea72a4813968844f1f4c0ff6c89746053f4
Author: Alberts Muktupāvels <alberts muktupavels gmail com>
Date:   Mon Oct 13 03:07:34 2014 +0300

    panel-shell: convert to a GObject

 gnome-panel/main.c          |   17 ++-
 gnome-panel/panel-session.c |    8 +-
 gnome-panel/panel-shell.c   |  213 ++++++++++++++++++++++++-------------------
 gnome-panel/panel-shell.h   |   35 ++++++-
 4 files changed, 162 insertions(+), 111 deletions(-)
---
diff --git a/gnome-panel/main.c b/gnome-panel/main.c
index 962cff8..158a8cf 100644
--- a/gnome-panel/main.c
+++ b/gnome-panel/main.c
@@ -79,6 +79,7 @@ main (int argc, char **argv)
        GError         *error;
        GtkSettings    *settings;
        PanelSession   *session;
+       PanelShell     *shell;
 
        bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
        bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
@@ -105,8 +106,10 @@ main (int argc, char **argv)
 
        g_option_context_free (context);
 
-       if (!panel_shell_register (replace)) {
-               panel_cleanup_do ();
+       shell = panel_shell_new ();
+
+       if (!panel_shell_register (shell, replace)) {
+               g_object_unref (shell);
                return 1;
        }
 
@@ -114,6 +117,7 @@ main (int argc, char **argv)
        panel_multiscreen_init ();
 
        if (!panel_layout_load ()) {
+               g_object_unref (shell);
                panel_cleanup_do ();
                return 1;
        }
@@ -124,17 +128,18 @@ main (int argc, char **argv)
         * immediate after (eg, the nautilus desktop). */
        gdk_flush ();
 
-       /* Do this at the end, to be sure that we're really ready when
-        * connecting to the session manager */
-       session = panel_session_new ();
-
        settings = gtk_settings_get_default ();
        g_signal_connect (settings, "notify::gtk-theme-name", G_CALLBACK (theme_changed), NULL);
        theme_changed (settings);
 
+       /* Do this at the end, to be sure that we're really ready when
+        * connecting to the session manager */
+       session = panel_session_new ();
+
        gtk_main ();
 
        g_object_unref (session);
+       g_object_unref (shell);
        panel_cleanup_do ();
 
        return 0;
diff --git a/gnome-panel/panel-session.c b/gnome-panel/panel-session.c
index a8e6f47..381e154 100644
--- a/gnome-panel/panel-session.c
+++ b/gnome-panel/panel-session.c
@@ -24,8 +24,8 @@
 #include <config.h>
 #include <stdlib.h>
 #include <gio/gio.h>
+#include <gtk/gtk.h>
 
-#include "panel-shell.h"
 #include "panel-session.h"
 
 struct _PanelSessionPrivate {
@@ -35,7 +35,7 @@ struct _PanelSessionPrivate {
        gchar           *client_path;
 };
 
-G_DEFINE_TYPE_WITH_PRIVATE(PanelSession, panel_session, G_TYPE_OBJECT);
+G_DEFINE_TYPE_WITH_PRIVATE (PanelSession, panel_session, G_TYPE_OBJECT);
 
 static void
 free_client_path (PanelSessionPrivate *priv)
@@ -98,10 +98,10 @@ client_proxy_signal (GDBusProxy  *proxy,
        } else if (g_str_equal (signal_name, "EndSession")) {
                send_end_session_response (priv);
                unregister_client (priv);
-               panel_shell_quit ();
+               gtk_main_quit ();
        } else if (g_str_equal (signal_name, "Stop")) {
                unregister_client (priv);
-               panel_shell_quit ();
+               gtk_main_quit ();
        }
 }
 
diff --git a/gnome-panel/panel-shell.c b/gnome-panel/panel-shell.c
index 0171668..8d71603 100644
--- a/gnome-panel/panel-shell.c
+++ b/gnome-panel/panel-shell.c
@@ -23,139 +23,162 @@
  *      Jacob Berkman <jacob ximian com>
  *      Colin Walters <walters verbum org>
  *      Vincent Untz <vuntz gnome org>
+ *      Alberts Muktupāvels <alberts muktupavels gmail com>
  */
 
 #include <config.h>
 #include <glib/gi18n.h>
 #include <gio/gio.h>
 
-#include <libpanel-util/panel-cleanup.h>
-
-#include "panel-session.h"
 #include "panel-toplevel.h"
-
 #include "panel-shell.h"
 
 #define PANEL_DBUS_SERVICE "org.gnome.Panel"
 
-static GDBusConnection *dbus_connection = NULL;
+struct _PanelShellPrivate {
+       GDBusConnection *session;
+};
+
+G_DEFINE_TYPE_WITH_PRIVATE (PanelShell, panel_shell, G_TYPE_OBJECT);
 
 static void
-panel_shell_on_name_lost (GDBusConnection *connection,
-                         const gchar     *sender_name,
-                         const gchar     *object_path,
-                         const gchar     *interface_name,
-                         const gchar     *signal_name,
-                         GVariant        *parameters,
-                         gpointer         user_data)
+destroy_toplevels (void)
 {
-       /* We lost our DBus name, and there is something replacing us.
-        * Tell the SM not to restart us automatically, then exit. */
-       g_printerr ("Panel leaving: a new panel shell is starting.\n");
+       GSList *toplevels_to_destroy, *l;
 
-       panel_shell_quit ();
+       toplevels_to_destroy = g_slist_copy (panel_toplevel_list_toplevels ());
+       for (l = toplevels_to_destroy; l; l = l->next)
+               gtk_widget_destroy (l->data);
+       g_slist_free (toplevels_to_destroy);
 }
 
 static void
-panel_shell_cleanup (gpointer data)
+on_name_lost (GDBusConnection *connection,
+              const gchar     *sender_name,
+              const gchar     *object_path,
+              const gchar     *interface_name,
+              const gchar     *signal_name,
+              GVariant        *parameters,
+              gpointer         user_data)
 {
-       if (dbus_connection != NULL) {
-               g_object_unref (dbus_connection);
-               dbus_connection = NULL;
+       gtk_main_quit ();
+}
+
+static void
+panel_shell_finalize (GObject *object)
+{
+       PanelShell *shell;
+       PanelShellPrivate *priv;
+
+       shell = PANEL_SHELL (object);
+       priv = shell->priv;
+
+       destroy_toplevels ();
+
+       g_clear_object (&priv->session);
+
+       G_OBJECT_CLASS (panel_shell_parent_class)->finalize (object);
+}
+
+static void
+panel_shell_class_init (PanelShellClass *shell_class)
+{
+       GObjectClass *object_class;
+
+       object_class = G_OBJECT_CLASS (shell_class);
+
+       object_class->finalize = panel_shell_finalize;
+}
+
+static void
+panel_shell_init (PanelShell *shell)
+{
+       PanelShellPrivate *priv;
+       GError *error;
+
+       shell->priv = panel_shell_get_instance_private (shell);
+
+       priv = shell->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;
        }
 }
 
+PanelShell *
+panel_shell_new (void)
+{
+       return g_object_new (PANEL_TYPE_SHELL, NULL);
+}
+
 gboolean
-panel_shell_register (gboolean replace)
+panel_shell_register (PanelShell *shell,
+                      gboolean    replace)
 {
+       PanelShellPrivate *priv;
        GBusNameOwnerFlags flags;
-       guint32            request_name_reply;
-       GVariant          *result;
-       gboolean           retval = FALSE;
-       GError            *error = NULL;
+       GVariant *parameters;
+       GError *error;
+       GVariant *result;
+       guint32 reply;
 
-       if (dbus_connection != NULL)
-               return TRUE;
+       priv = shell->priv;
 
-       panel_cleanup_register (PANEL_CLEAN_FUNC (panel_shell_cleanup), NULL);
-
-       /* There isn't a sync version of g_bus_own_name, so we have to requeste
-        * the name manually here. There's no ui yet so it's safe to use this
-        * sync api */
-       dbus_connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
-       if (dbus_connection == NULL) {
-               g_warning ("Cannot register the panel shell: %s", error->message);
-               goto register_out;
-       }
+       if (!priv->session)
+               return FALSE;
 
        flags = G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT;
        if (replace)
                flags |= G_BUS_NAME_OWNER_FLAGS_REPLACE;
 
-       result = g_dbus_connection_call_sync (dbus_connection,
-                                             "org.freedesktop.DBus",
-                                             "/org/freedesktop/DBus",
-                                             "org.freedesktop.DBus",
-                                             "RequestName",
-                                             g_variant_new ("(su)",
-                                                            PANEL_DBUS_SERVICE,
-                                                            flags),
-                                             G_VARIANT_TYPE ("(u)"),
-                                             G_DBUS_CALL_FLAGS_NONE,
-                                             -1, NULL, &error);
+       parameters = g_variant_new ("(su)", PANEL_DBUS_SERVICE, flags);
+       error = NULL;
+
+       result = g_dbus_connection_call_sync (priv->session,
+                                             "org.freedesktop.DBus",
+                                             "/org/freedesktop/DBus",
+                                             "org.freedesktop.DBus",
+                                             "RequestName",
+                                             parameters,
+                                             G_VARIANT_TYPE ("(u)"),
+                                             G_DBUS_CALL_FLAGS_NONE,
+                                             -1, NULL, &error);
+
        if (!result) {
-               g_warning ("Cannot register the panel shell: %s",
-                          error->message);
+               g_warning ("Cannot register the panel shell: %s", error->message);
                g_error_free (error);
-
-               goto register_out;
+               return FALSE;
        }
 
-       g_variant_get (result, "(u)", &request_name_reply);
+       g_variant_get (result, "(u)", &reply);
        g_variant_unref (result);
 
-       switch (request_name_reply) {
-       case 1: /* DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER */
-       case 4: /* DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER */
-               retval = TRUE;
-               g_dbus_connection_signal_subscribe (dbus_connection,
-                                                   "org.freedesktop.DBus",
-                                                   "org.freedesktop.DBus",
-                                                   "NameLost",
-                                                   "/org/freedesktop/DBus",
-                                                   PANEL_DBUS_SERVICE,
-                                                   G_DBUS_SIGNAL_FLAGS_NONE,
-                                                   (GDBusSignalCallback)panel_shell_on_name_lost,
-                                                   NULL, NULL);
-               break;
-       case 2: /* DBUS_REQUEST_NAME_REPLY_IN_QUEUE */
-       case 3: /* DBUS_REQUEST_NAME_REPLY_EXISTS */
-               g_printerr ("Cannot register the panel shell: there is "
-                           "already one running.\n");
-               break;
-       default:
-               g_warning ("Cannot register the panel shell: unhandled "
-                          "reply %u from RequestName", request_name_reply);
-       }
-
-register_out:
-
-       if (!retval) {
-               panel_shell_cleanup (NULL);
+       switch (reply) {
+               case 1: /* DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER */
+               case 4: /* DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER */
+                       g_dbus_connection_signal_subscribe (priv->session,
+                                                           "org.freedesktop.DBus",
+                                                           "org.freedesktop.DBus",
+                                                           "NameLost",
+                                                           "/org/freedesktop/DBus",
+                                                           PANEL_DBUS_SERVICE,
+                                                           G_DBUS_SIGNAL_FLAGS_NONE,
+                                                           (GDBusSignalCallback) on_name_lost,
+                                                           NULL, NULL);
+                       break;
+               case 2: /* DBUS_REQUEST_NAME_REPLY_IN_QUEUE */
+               case 3: /* DBUS_REQUEST_NAME_REPLY_EXISTS */
+                       g_warning ("Cannot register the panel shell: there is already one running.");
+                       return FALSE;
+               default:
+                       g_warning ("Cannot register the panel shell: unhandled reply %u from RequestName", 
reply);
+                       return FALSE;
        }
 
-       return retval;
-}
-
-void
-panel_shell_quit (void)
-{
-       GSList *toplevels_to_destroy, *l;
-
-        toplevels_to_destroy = g_slist_copy (panel_toplevel_list_toplevels ());
-        for (l = toplevels_to_destroy; l; l = l->next)
-               gtk_widget_destroy (l->data);
-        g_slist_free (toplevels_to_destroy);
-
-       gtk_main_quit ();
+       return TRUE;
 }
diff --git a/gnome-panel/panel-shell.h b/gnome-panel/panel-shell.h
index 1d17e4a..36c2281 100644
--- a/gnome-panel/panel-shell.h
+++ b/gnome-panel/panel-shell.h
@@ -22,14 +22,37 @@
  *      Jacob Berkman <jacob ximian com>
  *      Colin Walters <walters verbum org>
  *      Vincent Untz <vuntz gnome org>
+ *      Alberts Muktupāvels <alberts muktupavels gmail com>
  */
 
-#ifndef __PANEL_SHELL_H__
-#define __PANEL_SHELL_H__
+#ifndef PANEL_SHELL_H
+#define PANEL_SHELL_H
 
-#include <glib.h>
+#include <glib-object.h>
 
-gboolean panel_shell_register (gboolean replace);
-void     panel_shell_quit     (void);
+#define PANEL_TYPE_SHELL         (panel_shell_get_type ())
+#define PANEL_SHELL(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), PANEL_TYPE_SHELL, PanelShell))
+#define PANEL_SHELL_CLASS(c)     (G_TYPE_CHECK_CLASS_CAST ((c),    PANEL_TYPE_SHELL, PanelShellClass))
+#define PANEL_IS_SHELL(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), PANEL_TYPE_SHELL))
+#define PANEL_IS_SHELL_CLASS(c)  (G_TYPE_CHECK_CLASS_TYPE ((c),    PANEL_TYPE_SHELL))
+#define PANEL_SHELL_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o),  PANEL_TYPE_SHELL, PanelShellClass))
 
-#endif /* __PANEL_SHELL_H__ */
+typedef struct _PanelShell        PanelShell;
+typedef struct _PanelShellClass   PanelShellClass;
+typedef struct _PanelShellPrivate PanelShellPrivate;
+
+struct _PanelShell {
+       GObject            parent;
+       PanelShellPrivate *priv;
+};
+
+struct _PanelShellClass {
+    GObjectClass parent_class;
+};
+
+GType       panel_shell_get_type (void) G_GNUC_CONST;
+PanelShell *panel_shell_new      (void);
+gboolean    panel_shell_register (PanelShell *shell,
+                                  gboolean    replace);
+
+#endif


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