[Goodies-commits] r6980 - in xfce4-power-manager/trunk: . panel-plugins/brightness panel-plugins/inhibit src

Ali Abdallah aliov at xfce.org
Sun Mar 22 23:09:56 CET 2009


Author: aliov
Date: 2009-03-22 22:09:55 +0000 (Sun, 22 Mar 2009)
New Revision: 6980

Modified:
   xfce4-power-manager/trunk/ChangeLog
   xfce4-power-manager/trunk/panel-plugins/brightness/brightness-plugin.c
   xfce4-power-manager/trunk/panel-plugins/inhibit/inhibit-plugin.c
   xfce4-power-manager/trunk/src/xfpm-inhibit.c
   xfce4-power-manager/trunk/src/xfpm-inhibit.h
Log:
Support cookies in the inhibit interface

Modified: xfce4-power-manager/trunk/ChangeLog
===================================================================
--- xfce4-power-manager/trunk/ChangeLog	2009-03-22 19:45:43 UTC (rev 6979)
+++ xfce4-power-manager/trunk/ChangeLog	2009-03-22 22:09:55 UTC (rev 6980)
@@ -1,4 +1,7 @@
 
+2009-03-22 23:09 Ali aliov at xfce.org 
+	 * : Support cookies in the inhibit interface
+
 2009-03-22 20:45 Ali aliov at xfce.org 
 	 * : Correct the tooltips of the inhibit plugin
 

Modified: xfce4-power-manager/trunk/panel-plugins/brightness/brightness-plugin.c
===================================================================
--- xfce4-power-manager/trunk/panel-plugins/brightness/brightness-plugin.c	2009-03-22 19:45:43 UTC (rev 6979)
+++ xfce4-power-manager/trunk/panel-plugins/brightness/brightness-plugin.c	2009-03-22 22:09:55 UTC (rev 6980)
@@ -420,6 +420,12 @@
     if ( brightness->proxy )
 	g_object_unref (brightness->proxy);
 	
+    if ( brightness->xfpm_proxy )
+	g_object_unref (brightness->xfpm_proxy );
+	
+    if ( brightness->session )
+	dbus_g_connection_unref (brightness->session);
+	
     g_free (brightness);
 }
 

Modified: xfce4-power-manager/trunk/panel-plugins/inhibit/inhibit-plugin.c
===================================================================
--- xfce4-power-manager/trunk/panel-plugins/inhibit/inhibit-plugin.c	2009-03-22 19:45:43 UTC (rev 6979)
+++ xfce4-power-manager/trunk/panel-plugins/inhibit/inhibit-plugin.c	2009-03-22 22:09:55 UTC (rev 6980)
@@ -52,7 +52,7 @@
     GtkWidget        *image;
     
     gboolean          connected;
-    guint             cookies;
+    guint             cookie;
     
     gboolean          inhibited;
 
@@ -79,8 +79,8 @@
 {
     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(inhibit->button), inhibit->inhibited);
     gtk_widget_set_tooltip_text (inhibit->button, 
-			         inhibit->inhibited ? _("Automatic sleep enabled") :
-						      _("Automatic sleep inhibited"));
+			         inhibit->inhibited ? _("Automatic sleep inhibited") :
+						      _("Automatic sleep enabled"));
 }
 
 static gboolean
@@ -125,12 +125,12 @@
 static void
 inhibit_plugin_set_inhibit (inhibit_t *plugin)
 {
-    GError *error;
+    GError *error = NULL;
     
     const gchar *app = "Inhibit plugin";
     const gchar *reason = "User settings";
     
-    if (!xfpm_inhibit_dbus_client_inhibit (plugin->proxy, app, reason, &plugin->cookies, &error))
+    if (!xfpm_inhibit_dbus_client_inhibit (plugin->proxy, app, reason, &plugin->cookie, &error))
     {
 	g_critical ("Unable to set inhibit: %s", error->message);
 	g_error_free (error);
@@ -143,9 +143,9 @@
 static void
 inhibit_plugin_unset_inhibit (inhibit_t *plugin)
 {
-    GError *error;
+    GError *error = NULL;
     
-    if (!xfpm_inhibit_dbus_client_un_inhibit (plugin->proxy, plugin->cookies, &error))
+    if (!xfpm_inhibit_dbus_client_un_inhibit (plugin->proxy, plugin->cookie, &error))
     {
 	g_critical ("Unable to set UnInhibit: %s", error->message);
 	g_error_free (error);
@@ -228,6 +228,7 @@
 	
     inhibit_plugin_connect (plugin);
     inhibit_plugin_connect_more (plugin);
+    
 }
 
 static void

Modified: xfce4-power-manager/trunk/src/xfpm-inhibit.c
===================================================================
--- xfce4-power-manager/trunk/src/xfpm-inhibit.c	2009-03-22 19:45:43 UTC (rev 6979)
+++ xfce4-power-manager/trunk/src/xfpm-inhibit.c	2009-03-22 22:09:55 UTC (rev 6980)
@@ -54,9 +54,39 @@
 #define XFPM_INHIBIT_GET_PRIVATE(o) \
 (G_TYPE_INSTANCE_GET_PRIVATE((o), XFPM_TYPE_INHIBIT, XfpmInhibitPrivate))
 
+GQuark
+xfpm_inhibit_get_error_quark (void)
+{
+    static GQuark quark = 0;
+    if (!quark)
+	quark = g_quark_from_static_string ("Xfce Power Manager inhibit");
+	
+    return quark;
+}
+
+GType
+xfpm_inhibit_error_get_type (void)
+{
+    static GType type = 0;
+    
+    if (!type)
+    {
+	static const GEnumValue values[] = {
+	    { XFPM_INHIBIT_ERROR_UNKNOWN, "XFPM_INHIBIT_ERROR_UNKNOWN", "Unknown" },
+	    { XFPM_INHIBIT_ERROR_INVALID_COOKIE, "XFPM_INHIBIT_ERROR_INVALID_COOKIE", "InvalidCookie" },
+	    { 0, NULL, NULL }
+	};
+	
+	type = g_enum_register_static ("XfpmInhibitError", values);
+    }
+    
+    return type;
+}
+
 struct XfpmInhibitPrivate
 {
     XfpmScreenSaver *srv;
+    GHashTable      *hash;
     gboolean         inhibited;
 };
 
@@ -70,7 +100,7 @@
 
 static gpointer xfpm_inhibit_object = NULL;
 
-G_DEFINE_TYPE(XfpmInhibit, xfpm_inhibit, G_TYPE_OBJECT)
+G_DEFINE_TYPE (XfpmInhibit, xfpm_inhibit, G_TYPE_OBJECT)
 
 static void
 xfpm_inhibit_screen_saver_inhibited_cb (XfpmScreenSaver *srv, gboolean is_inhibited, XfpmInhibit *inhibit)
@@ -105,6 +135,8 @@
 {
     inhibit->priv = XFPM_INHIBIT_GET_PRIVATE(inhibit);
     
+    inhibit->priv->hash = g_hash_table_new_full (NULL, NULL, NULL, g_free);
+    
     inhibit->priv->srv = xfpm_screen_saver_new ();
     
     g_signal_connect (inhibit->priv->srv, "screen-saver-inhibited",
@@ -119,10 +151,55 @@
     XfpmInhibit *inhibit;
 
     inhibit = XFPM_INHIBIT(object);
+    
+    if ( inhibit->priv->srv)
+	g_object_unref (inhibit->priv->srv);
+	
+    g_hash_table_destroy (inhibit->priv->hash);
 
     G_OBJECT_CLASS(xfpm_inhibit_parent_class)->finalize(object);
 }
 
+static guint
+xfpm_inhibit_get_cookie (XfpmInhibit *inhibit)
+{
+    GList *list;
+    guint cookie;
+    guint max = 0;
+    guint hash_cookie;
+    gint i;
+    
+    list = g_hash_table_get_keys (inhibit->priv->hash);
+
+    for ( i = 0; i < g_list_length (list); i++)
+    {
+	hash_cookie = GPOINTER_TO_INT ((gpointer) g_list_nth_data(list, i));
+	max = MAX(max, hash_cookie);
+    }
+	
+    if ( list )
+	g_list_free (list);
+	
+    cookie = max + 1;
+    return cookie;
+}
+
+static guint
+xfpm_inhibit_add_application (XfpmInhibit *inhibit, const gchar *app_name)
+{
+    guint cookie = xfpm_inhibit_get_cookie (inhibit);
+    g_hash_table_insert (inhibit->priv->hash, 
+			 GINT_TO_POINTER(cookie),
+			 g_strdup (app_name));
+    return cookie;
+}
+
+static gboolean
+xfpm_inhibit_remove_application (XfpmInhibit *inhibit, guint cookie)
+{
+    return g_hash_table_remove (inhibit->priv->hash, GINT_TO_POINTER(cookie));
+}
+
 XfpmInhibit *
 xfpm_inhibit_new(void)
 {
@@ -135,7 +212,6 @@
 	xfpm_inhibit_object = g_object_new (XFPM_TYPE_INHIBIT, NULL);
 	g_object_add_weak_pointer (xfpm_inhibit_object, &xfpm_inhibit_object);
     }
-    
     return XFPM_INHIBIT (xfpm_inhibit_object);
 }
 
@@ -164,6 +240,10 @@
 {
     dbus_g_object_type_install_info(G_TYPE_FROM_CLASS(klass),
 				    &dbus_glib_xfpm_inhibit_object_info);
+				    
+    dbus_g_error_domain_register (XFPM_INHIBIT_ERROR, 
+				  "org.freedesktop.PowerManagement.Inhibit",
+				  XFPM_TYPE_INHIBIT_ERROR);
 }
 
 static void xfpm_inhibit_dbus_init	  (XfpmInhibit *inhibit)
@@ -181,12 +261,14 @@
 						 guint       *OUT_cookie,
 						 GError     **error)
 {
+    guint cookie = xfpm_inhibit_add_application (inhibit, IN_appname);
+    
     TRACE("Inhibit send application name=%s reason=%s", IN_appname, IN_reason);
     
     inhibit->priv->inhibited = TRUE;
     g_signal_emit (G_OBJECT(inhibit), signals[HAS_INHIBIT_CHANGED], 0, inhibit->priv->inhibited);
     
-    *OUT_cookie = 1;//FIXME support cookies
+    *OUT_cookie = cookie;
     
     return TRUE;
 }
@@ -196,9 +278,20 @@
 						 GError     **error)
 {
     TRACE("UnHibit message received");
-    inhibit->priv->inhibited = FALSE;
-    g_signal_emit (G_OBJECT(inhibit), signals[HAS_INHIBIT_CHANGED], 0, inhibit->priv->inhibited);
     
+    if (!xfpm_inhibit_remove_application (inhibit, IN_cookie))
+    {
+	g_set_error (error, XFPM_INHIBIT_ERROR, XFPM_INHIBIT_ERROR_INVALID_COOKIE, _("Invalid cookie"));
+	return FALSE;
+    }
+    
+    if ( g_hash_table_size (inhibit->priv->hash) == 0)
+    {
+	TRACE("Inhibit removed");
+	inhibit->priv->inhibited = FALSE;
+	g_signal_emit (G_OBJECT(inhibit), signals[HAS_INHIBIT_CHANGED], 0, inhibit->priv->inhibited);
+    }
+    
     return TRUE;
 }
 
@@ -207,5 +300,8 @@
 						 GError     **error)
 {
     TRACE("Has Inhibit message received");
+    
+    *OUT_has_inhibit = inhibit->priv->inhibited;
+    
     return TRUE;
 }

Modified: xfce4-power-manager/trunk/src/xfpm-inhibit.h
===================================================================
--- xfce4-power-manager/trunk/src/xfpm-inhibit.h	2009-03-22 19:45:43 UTC (rev 6979)
+++ xfce4-power-manager/trunk/src/xfpm-inhibit.h	2009-03-22 22:09:55 UTC (rev 6980)
@@ -29,6 +29,16 @@
 #define XFPM_INHIBIT(o)          (G_TYPE_CHECK_INSTANCE_CAST((o), XFPM_TYPE_INHIBIT, XfpmInhibit))
 #define XFPM_IS_INHIBIT(o)       (G_TYPE_CHECK_INSTANCE_TYPE((o), XFPM_TYPE_INHIBIT))
 
+#define XFPM_TYPE_INHIBIT_ERROR  (xfpm_inhibit_error_get_type ())
+#define XFPM_INHIBIT_ERROR       (xfpm_inhibit_get_error_quark ())
+
+typedef enum
+{
+    XFPM_INHIBIT_ERROR_UNKNOWN = 0,
+    XFPM_INHIBIT_ERROR_INVALID_COOKIE
+    
+} XfpmInhibitError;
+
 typedef struct XfpmInhibitPrivate XfpmInhibitPrivate;
 
 typedef struct
@@ -48,6 +58,9 @@
 } XfpmInhibitClass;
 
 GType        		  xfpm_inhibit_get_type        (void) G_GNUC_CONST;
+GType                     xfpm_inhibit_error_get_type  (void) G_GNUC_CONST;
+GQuark                    xfpm_inhibit_get_error_quark ();
+
 XfpmInhibit              *xfpm_inhibit_new             (void);
 
 G_END_DECLS




More information about the Goodies-commits mailing list