[gnome-panel/wip/user-menu: 2/11] panel: Add presence API to PanelSessionManager



commit ee892f61a87c12e98e700f6d0c77428ef2279ad0
Author: Vincent Untz <vuntz gnome org>
Date:   Thu Mar 31 13:45:53 2011 +0530

    panel: Add presence API to PanelSessionManager
    
    We'll be able to easily set (and watch) the presence this way.

 gnome-panel/libpanel-util/Makefile.am             |   39 ++++++
 gnome-panel/libpanel-util/panel-session-manager.c |  134 +++++++++++++++++++--
 gnome-panel/libpanel-util/panel-session-manager.h |   30 ++++-
 3 files changed, 188 insertions(+), 15 deletions(-)
---
diff --git a/gnome-panel/libpanel-util/Makefile.am b/gnome-panel/libpanel-util/Makefile.am
index 8af97e0..5590d2c 100644
--- a/gnome-panel/libpanel-util/Makefile.am
+++ b/gnome-panel/libpanel-util/Makefile.am
@@ -10,7 +10,43 @@ AM_CPPFLAGS =							\
 
 AM_CFLAGS = $(WARN_CFLAGS)
 
+panel_util_enum_headers =	\
+	panel-session-manager.h
+
+BUILT_SOURCES =			\
+	panel-util-types.c	\
+	panel-util-types.h
+
+panel-util-types.c: @REBUILD@ $(panel_util_enum_headers)
+	$(AM_V_GEN)glib-mkenums \
+			--fhead "#include <glib-object.h>\n" \
+			--fhead "#include \"panel-util-types.h\"\n\n" \
+			--fprod "\n/* enumerations from \"@filename \" */" \
+			--fprod "\n#include \"@filename \"\n" \
+			--vhead "static const GEnumValue _ enum_name@_values[] = {" \
+			--vprod "  { @VALUENAME@, \"@VALUENAME \", \"@valuenick \" }," \
+			--vtail "  { 0, NULL, NULL }\n};\n\n" \
+			--vtail "GType\n enum_name@_get_type (void)\n{\n" \
+			--vtail "  static GType type = 0;\n\n" \
+			--vtail "  if (!type)\n" \
+			--vtail "    type = g_enum_register_static (\"@EnumName \", _ enum_name@_values);\n\n" \
+			--vtail "  return type;\n}\n\n" \
+		$(panel_util_enum_headers) > $@
+
+panel-util-types.h: @REBUILD@ $(panel_util_enum_headers)
+	$(AM_V_GEN)glib-mkenums \
+			--fhead "#ifndef __PANEL_UTIL_TYPES_H__\n" \
+			--fhead "#define __PANEL_UTIL_TYPES_H__ 1\n\n" \
+			--fhead "G_BEGIN_DECLS\n\n" \
+			--ftail "G_END_DECLS\n\n" \
+			--ftail "#endif /* __PANEL_UTIL_TYPES_H__ */\n" \
+			--fprod "\n/* --- @filename@ --- */" \
+			--eprod "#define PANEL_TYPE_ ENUMSHORT@ @enum_name _get_type()\n" \
+			--eprod "GType @enum_name _get_type (void);\n" \
+		$(panel_util_enum_headers) > $@
+
 libpanel_util_la_SOURCES =		\
+	$(BUILT_SOURCES)		\
 	panel-cleanup.c			\
 	panel-cleanup.h			\
 	panel-dconf.c			\
@@ -40,4 +76,7 @@ libpanel_util_la_SOURCES =		\
 	panel-xdg.c			\
 	panel-xdg.h
 
+CLEANFILES = \
+	$(BUILT_SOURCES)
+
 -include $(top_srcdir)/git.mk
diff --git a/gnome-panel/libpanel-util/panel-session-manager.c b/gnome-panel/libpanel-util/panel-session-manager.c
index 0a2e6ff..a35a73b 100644
--- a/gnome-panel/libpanel-util/panel-session-manager.c
+++ b/gnome-panel/libpanel-util/panel-session-manager.c
@@ -25,20 +25,60 @@
 #include <gio/gio.h>
 
 #include "panel-cleanup.h"
+#include "panel-util-types.h"
 
 #include "panel-session-manager.h"
 
 struct _PanelSessionManagerPrivate {
-	GDBusProxy *proxy;
+	GDBusProxy *session_proxy;
+	GDBusProxy *presence_proxy;
 };
 
+enum {
+        PRESENCE_CHANGED,
+        LAST_SIGNAL
+};
+
+static guint panel_session_manager_signals[LAST_SIGNAL] = { 0 };
+
 G_DEFINE_TYPE (PanelSessionManager, panel_session_manager, G_TYPE_OBJECT);
 
 static void
+panel_session_manager_presence_on_signal (GDBusProxy          *proxy,
+                                          gchar               *sender_name,
+                                          gchar               *signal_name,
+                                          GVariant            *parameters,
+                                          PanelSessionManager *manager)
+{
+        if (g_strcmp0 (signal_name, "StatusChanged") == 0) {
+                unsigned int status;
+                g_variant_get (parameters, "(u)", &status);
+
+                g_signal_emit (G_OBJECT (manager),
+                               panel_session_manager_signals[PRESENCE_CHANGED],
+                               0, status);
+        }
+}
+
+static void
 panel_session_manager_class_init (PanelSessionManagerClass *klass)
 {
+        GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+
 	g_type_class_add_private (klass,
 				  sizeof (PanelSessionManagerPrivate));
+
+        panel_session_manager_signals[PRESENCE_CHANGED] =
+                g_signal_new ("presence-changed",
+                              G_TYPE_FROM_CLASS (gobject_class),
+                              G_SIGNAL_RUN_LAST,
+                              G_STRUCT_OFFSET (PanelSessionManagerClass,
+                                               presence_changed),
+                              NULL,
+                              NULL,
+                              g_cclosure_marshal_VOID__ENUM,
+                              G_TYPE_NONE, 1,
+                              PANEL_TYPE_SESSION_MANAGER_PRESENCE_TYPE);
 }
 
 static void
@@ -51,7 +91,7 @@ panel_session_manager_init (PanelSessionManager *manager)
 						     PanelSessionManagerPrivate);
 
 	error = NULL;
-	manager->priv->proxy = g_dbus_proxy_new_for_bus_sync (
+	manager->priv->session_proxy = g_dbus_proxy_new_for_bus_sync (
 						G_BUS_TYPE_SESSION,
 						G_DBUS_PROXY_FLAGS_NONE,
 						NULL,
@@ -64,7 +104,85 @@ panel_session_manager_init (PanelSessionManager *manager)
 		g_warning ("Could not connect to session manager: %s",
 			   error->message);
 		g_error_free (error);
+                return;
+	}
+
+	error = NULL;
+	manager->priv->presence_proxy = g_dbus_proxy_new_for_bus_sync (
+						G_BUS_TYPE_SESSION,
+						G_DBUS_PROXY_FLAGS_NONE,
+						NULL,
+						"org.gnome.SessionManager",
+						"/org/gnome/SessionManager/Presence",
+						"org.gnome.SessionManager.Presence",
+						NULL, &error);
+
+	if (error) {
+		g_warning ("Could not connect to session manager: %s",
+			   error->message);
+		g_error_free (error);
+                return;
+	}
+
+        g_signal_connect (manager->priv->presence_proxy,
+                          "g-signal",
+                          G_CALLBACK (panel_session_manager_presence_on_signal),
+                          manager);
+}
+
+void
+panel_session_manager_set_presence (PanelSessionManager             *manager,
+                                    PanelSessionManagerPresenceType  presence)
+{
+	GVariant *ret;
+	GError   *error;
+
+	g_return_if_fail (PANEL_IS_SESSION_MANAGER (manager));
+
+	if (!manager->priv->presence_proxy) {
+		g_warning ("Session manager service not available.");
+		return;
+	}
+
+	error = NULL;
+	ret = g_dbus_proxy_call_sync (manager->priv->presence_proxy,
+				      "SetStatus",
+				      g_variant_new ("(u)", presence),
+				      G_DBUS_CALL_FLAGS_NONE,
+				      -1,
+				      NULL,
+				      &error);
+
+	if (ret)
+		g_variant_unref (ret);
+
+	if (error) {
+		g_warning ("Could not ask session manager to change presence: %s",
+			   error->message);
+		g_error_free (error);
+	}
+}
+
+PanelSessionManagerPresenceType
+panel_session_manager_get_presence (PanelSessionManager *manager)
+{
+        GVariant *variant;
+        PanelSessionManagerPresenceType ret;
+
+	g_return_val_if_fail (PANEL_IS_SESSION_MANAGER (manager),
+                              PANEL_SESSION_MANAGER_PRESENCE_AVAILABLE);
+
+	if (!manager->priv->presence_proxy) {
+		g_warning ("Session manager service not available.");
+		return PANEL_SESSION_MANAGER_PRESENCE_AVAILABLE;
 	}
+
+        variant = g_dbus_proxy_get_cached_property (manager->priv->presence_proxy,
+                                                    "status");
+        g_variant_get (variant, "(u)", &ret);
+        g_variant_unref (variant);
+
+        return ret;
 }
 
 void
@@ -76,13 +194,13 @@ panel_session_manager_request_logout (PanelSessionManager           *manager,
 
 	g_return_if_fail (PANEL_IS_SESSION_MANAGER (manager));
 
-	if (!manager->priv->proxy) {
+	if (!manager->priv->session_proxy) {
 		g_warning ("Session manager service not available.");
 		return;
 	}
 
 	error = NULL;
-	ret = g_dbus_proxy_call_sync (manager->priv->proxy,
+	ret = g_dbus_proxy_call_sync (manager->priv->session_proxy,
 				      "Logout",
 				      g_variant_new ("(u)", mode),
 				      G_DBUS_CALL_FLAGS_NONE,
@@ -108,13 +226,13 @@ panel_session_manager_request_shutdown (PanelSessionManager *manager)
 
 	g_return_if_fail (PANEL_IS_SESSION_MANAGER (manager));
 
-	if (!manager->priv->proxy) {
+	if (!manager->priv->session_proxy) {
 		g_warning ("Session manager service not available.");
 		return;
 	}
 
 	error = NULL;
-	ret = g_dbus_proxy_call_sync (manager->priv->proxy,
+	ret = g_dbus_proxy_call_sync (manager->priv->session_proxy,
 				      "Shutdown",
 				      NULL,
 				      G_DBUS_CALL_FLAGS_NONE,
@@ -141,13 +259,13 @@ panel_session_manager_is_shutdown_available (PanelSessionManager *manager)
 
 	g_return_val_if_fail (PANEL_IS_SESSION_MANAGER (manager), FALSE);
 
-	if (!manager->priv->proxy) {
+	if (!manager->priv->session_proxy) {
 		g_warning ("Session manager service not available.");
 		return FALSE;
 	}
 
 	error = NULL;
-	ret = g_dbus_proxy_call_sync (manager->priv->proxy,
+	ret = g_dbus_proxy_call_sync (manager->priv->session_proxy,
 				      "CanShutdown",
 				      NULL,
 				      G_DBUS_CALL_FLAGS_NONE,
diff --git a/gnome-panel/libpanel-util/panel-session-manager.h b/gnome-panel/libpanel-util/panel-session-manager.h
index 0561b52..49b1715 100644
--- a/gnome-panel/libpanel-util/panel-session-manager.h
+++ b/gnome-panel/libpanel-util/panel-session-manager.h
@@ -40,6 +40,21 @@ typedef struct _PanelSessionManager		PanelSessionManager;
 typedef struct _PanelSessionManagerClass	PanelSessionManagerClass;
 typedef struct _PanelSessionManagerPrivate	PanelSessionManagerPrivate;
 
+/* Keep in sync with the values defined in gnome-session/session.h */
+typedef enum {
+        PANEL_SESSION_MANAGER_LOGOUT_MODE_NORMAL = 0,
+        PANEL_SESSION_MANAGER_LOGOUT_MODE_NO_CONFIRMATION,
+        PANEL_SESSION_MANAGER_LOGOUT_MODE_FORCE
+} PanelSessionManagerLogoutType;
+
+/* Keep in sync with the values defined in gnome-session/gsm-presence.h */
+typedef enum {
+        PANEL_SESSION_MANAGER_PRESENCE_AVAILABLE = 0,
+        PANEL_SESSION_MANAGER_PRESENCE_INVISIBLE,
+        PANEL_SESSION_MANAGER_PRESENCE_BUSY,
+        PANEL_SESSION_MANAGER_PRESENCE_IDLE
+} PanelSessionManagerPresenceType;
+
 struct _PanelSessionManager {
 	GObject parent;
 
@@ -49,19 +64,20 @@ struct _PanelSessionManager {
 
 struct _PanelSessionManagerClass {
 	GObjectClass parent_class;
+
+        void (* presence_changed)  (PanelSessionManager             *manager,
+                                    PanelSessionManagerPresenceType  presence);
 };
 
 GType panel_session_manager_get_type (void);
 
-/* Keep in sync with the values defined in gnome-session/session.h */
-typedef enum {
-	PANEL_SESSION_MANAGER_LOGOUT_MODE_NORMAL = 0,
-	PANEL_SESSION_MANAGER_LOGOUT_MODE_NO_CONFIRMATION,
-	PANEL_SESSION_MANAGER_LOGOUT_MODE_FORCE
-} PanelSessionManagerLogoutType;
-
 PanelSessionManager *panel_session_manager_get (void);
 
+void panel_session_manager_set_presence     (PanelSessionManager             *session,
+                                             PanelSessionManagerPresenceType  presence);
+PanelSessionManagerPresenceType panel_session_manager_get_presence (PanelSessionManager *session);
+
+
 void panel_session_manager_request_logout   (PanelSessionManager           *session,
 					     PanelSessionManagerLogoutType  mode);
 void panel_session_manager_request_shutdown (PanelSessionManager *session);



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