[evince] [shell] Pass the GDBusConnection to EvMediaPlayerKeys



commit f8b51206518dc31d290ed6400425cba7d4d3c9da
Author: Christian Persch <chpe gnome org>
Date:   Thu May 13 14:26:07 2010 +0200

    [shell] Pass the GDBusConnection to EvMediaPlayerKeys
    
    We already have a connection, so just pass it to EvMediaPlayerKeys.

 shell/ev-application.c       |    2 +-
 shell/ev-media-player-keys.c |   93 +++++++++++++++++++++++++++++++-----------
 shell/ev-media-player-keys.h |    3 +-
 3 files changed, 72 insertions(+), 26 deletions(-)
---
diff --git a/shell/ev-application.c b/shell/ev-application.c
index 0f29da8..93a5cbf 100644
--- a/shell/ev-application.c
+++ b/shell/ev-application.c
@@ -888,7 +888,7 @@ ev_application_init (EvApplication *ev_application)
                 g_error_free (error);
         }
 
-	ev_application->keys = ev_media_player_keys_new ();
+	ev_application->keys = ev_media_player_keys_new (ev_application->connection);
 }
 #endif /* ENABLE_DBUS */
 }
diff --git a/shell/ev-media-player-keys.c b/shell/ev-media-player-keys.c
index da334b6..11a3648 100644
--- a/shell/ev-media-player-keys.c
+++ b/shell/ev-media-player-keys.c
@@ -32,6 +32,11 @@
 #define SD_INTERFACE   "org.gnome.SettingsDaemon.MediaKeys"
 
 enum {
+        PROP_0,
+        PROP_CONNECTION
+};
+
+enum {
 	KEY_PRESSED,
 	LAST_SIGNAL
 };
@@ -58,6 +63,11 @@ static guint signals[LAST_SIGNAL];
 
 G_DEFINE_TYPE (EvMediaPlayerKeys, ev_media_player_keys, G_TYPE_OBJECT)
 
+static void ev_media_player_keys_set_property (GObject      *object,
+                                               guint         prop_id,
+                                               const GValue *value,
+                                               GParamSpec   *pspec);
+static void ev_media_player_keys_constructed (GObject *object);
 static void ev_media_player_keys_finalize (GObject *object);
 
 static void
@@ -65,6 +75,10 @@ ev_media_player_keys_class_init (EvMediaPlayerKeysClass *klass)
 {
 	GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
+        object_class->set_property = ev_media_player_keys_set_property;
+        object_class->constructed = ev_media_player_keys_constructed;
+	object_class->finalize = ev_media_player_keys_finalize;
+
 	signals[KEY_PRESSED] =
 		g_signal_new ("key_pressed",
 			      EV_TYPE_MEDIA_PLAYER_KEYS,
@@ -74,8 +88,14 @@ ev_media_player_keys_class_init (EvMediaPlayerKeysClass *klass)
 			      g_cclosure_marshal_VOID__STRING,
 			      G_TYPE_NONE,
 			      1, G_TYPE_STRING);
-	
-	object_class->finalize = ev_media_player_keys_finalize;
+
+	g_object_class_install_property (object_class,
+					 PROP_CONNECTION,
+					 g_param_spec_object ("connection", NULL, NULL,
+                                                              G_TYPE_DBUS_CONNECTION,
+							      G_PARAM_WRITABLE |
+                                                              G_PARAM_CONSTRUCT_ONLY |
+                                                              G_PARAM_STATIC_STRINGS));
 }
 
 static void
@@ -145,9 +165,9 @@ mediakeys_service_appeared_cb (GDBusConnection *connection,
 {
         EvMediaPlayerKeys *keys = EV_MEDIA_PLAYER_KEYS (user_data);
 
-        keys->connection = g_object_ref (connection);
+        g_assert (connection == keys->connection);
 
-        keys->subscription_id = g_dbus_connection_signal_subscribe (connection,
+        keys->subscription_id = g_dbus_connection_signal_subscribe (keys->connection,
                                                                     name_owner,
                                                                     SD_INTERFACE,
                                                                     "MediaPlayerKeyPressed",
@@ -166,27 +186,30 @@ mediakeys_service_disappeared_cb (GDBusConnection *connection,
 {
         EvMediaPlayerKeys *keys = EV_MEDIA_PLAYER_KEYS (user_data);
 
-        if (keys->connection == NULL)
-                return;
-
-        g_assert (keys->connection == connection);
-
-        g_dbus_connection_signal_unsubscribe (connection, keys->subscription_id);
-        keys->subscription_id = 0;
+        g_assert (connection == keys->connection);
 
-        g_object_unref (keys->connection);
-        keys->connection = NULL;
+        if (keys->subscription_id != 0) {
+                g_dbus_connection_signal_unsubscribe (connection, keys->subscription_id);
+                keys->subscription_id = 0;
+        }
 }
 
 static void
 ev_media_player_keys_init (EvMediaPlayerKeys *keys)
 {
-        keys->watch_id = g_bus_watch_name (G_BUS_TYPE_SESSION,
-                                           SD_NAME,
-                                           G_BUS_NAME_WATCHER_FLAGS_NONE,
-                                           mediakeys_service_appeared_cb,
-                                           mediakeys_service_disappeared_cb,
-                                           keys, NULL);
+}
+
+static void
+ev_media_player_keys_constructed (GObject *object)
+{
+        EvMediaPlayerKeys *keys = EV_MEDIA_PLAYER_KEYS (object);
+
+        keys->watch_id = g_bus_watch_name_on_connection (keys->connection,
+                                                         SD_NAME,
+                                                         G_BUS_NAME_WATCHER_FLAGS_NONE,
+                                                         mediakeys_service_appeared_cb,
+                                                         mediakeys_service_disappeared_cb,
+                                                         keys, NULL);
 }
 
 void
@@ -205,20 +228,42 @@ ev_media_player_keys_finalize (GObject *object)
 
         ev_media_player_keys_release_keys (keys);
 
+        g_bus_unwatch_name (keys->watch_id);
+
         if (keys->subscription_id != 0) {
-                g_assert (keys->connection != NULL);
                 g_dbus_connection_signal_unsubscribe (keys->connection, keys->subscription_id);
-                g_object_unref (keys->connection);
         }
 
-        g_bus_unwatch_name (keys->watch_id);
+        if (keys->connection != NULL) {
+                g_object_unref (keys->connection);
+        }
 
 	G_OBJECT_CLASS (ev_media_player_keys_parent_class)->finalize (object);
 }
 
+static void
+ev_media_player_keys_set_property (GObject      *object,
+                                   guint         prop_id,
+                                   const GValue *value,
+                                   GParamSpec   *pspec)
+{
+        EvMediaPlayerKeys *keys = EV_MEDIA_PLAYER_KEYS (object);
+
+	switch (prop_id) {
+        case PROP_CONNECTION:
+                keys->connection = g_value_dup_object (value);
+		break;
+	default:
+		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+		break;
+	}
+}
+
 EvMediaPlayerKeys *
-ev_media_player_keys_new (void)
+ev_media_player_keys_new (GDBusConnection *connection)
 {
-	return g_object_new (EV_TYPE_MEDIA_PLAYER_KEYS, NULL);
+	return g_object_new (EV_TYPE_MEDIA_PLAYER_KEYS,
+                             "connection", connection,
+                             NULL);
 }
 
diff --git a/shell/ev-media-player-keys.h b/shell/ev-media-player-keys.h
index d23c188..7e69c47 100644
--- a/shell/ev-media-player-keys.h
+++ b/shell/ev-media-player-keys.h
@@ -23,6 +23,7 @@
 #define EV_MEDIA_PLAYER_KEYS_H
 
 #include <glib-object.h>
+#include <gio/gio.h>
 
 G_BEGIN_DECLS
 
@@ -39,7 +40,7 @@ typedef struct _EvMediaPlayerKeysClass EvMediaPlayerKeysClass;
 
 GType	           ev_media_player_keys_get_type  (void) G_GNUC_CONST;
 
-EvMediaPlayerKeys *ev_media_player_keys_new	  (void);
+EvMediaPlayerKeys *ev_media_player_keys_new	  (GDBusConnection *connection);
 
 void               ev_media_player_keys_focused	  (EvMediaPlayerKeys *keys);
 



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