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

Ali Abdallah aliov at xfce.org
Mon Mar 23 20:09:13 CET 2009


Author: aliov
Date: 2009-03-23 19:09:13 +0000 (Mon, 23 Mar 2009)
New Revision: 6983

Added:
   xfce4-power-manager/trunk/libxfpm/xfpm-notify.c
   xfce4-power-manager/trunk/libxfpm/xfpm-notify.h
Removed:
   xfce4-power-manager/trunk/src/xfpm-notify.c
   xfce4-power-manager/trunk/src/xfpm-notify.h
Modified:
   xfce4-power-manager/trunk/ChangeLog
   xfce4-power-manager/trunk/libxfpm/Makefile.am
   xfce4-power-manager/trunk/panel-plugins/inhibit/Makefile.am
   xfce4-power-manager/trunk/panel-plugins/inhibit/inhibit-plugin.c
   xfce4-power-manager/trunk/src/Makefile.am
   xfce4-power-manager/trunk/src/xfpm-battery.c
   xfce4-power-manager/trunk/src/xfpm-supply.c
Log:
Moved notification to the common code+added notification to the inhibit plugin

Modified: xfce4-power-manager/trunk/ChangeLog
===================================================================
--- xfce4-power-manager/trunk/ChangeLog	2009-03-23 14:35:28 UTC (rev 6982)
+++ xfce4-power-manager/trunk/ChangeLog	2009-03-23 19:09:13 UTC (rev 6983)
@@ -1,4 +1,7 @@
 
+2009-03-23 20:09 Ali aliov at xfce.org 
+	 * : Moved notification to the common code+added notification to the inhibit plugin
+
 2009-03-22 23:09 Ali aliov at xfce.org 
 	 * : Support cookies in the inhibit interface
 

Modified: xfce4-power-manager/trunk/libxfpm/Makefile.am
===================================================================
--- xfce4-power-manager/trunk/libxfpm/Makefile.am	2009-03-23 14:35:28 UTC (rev 6982)
+++ xfce4-power-manager/trunk/libxfpm/Makefile.am	2009-03-23 19:09:13 UTC (rev 6983)
@@ -43,13 +43,16 @@
         xfpm-popups.c           	\
         xfpm-popups.h           	\
         xfpm-common.c           	\
-        xfpm-common.h
+        xfpm-common.h			\
+	xfpm-notify.c			\
+	xfpm-notify.h
 
 libxfpmcommon_la_CFLAGS =       	\
         $(GTK_CFLAGS)           	\
         $(GLIB_CFLAGS)          	\
         $(DBUS_CFLAGS)			\
-	$(LIBXFCE4UTIL_CFLAGS)
+	$(LIBXFCE4UTIL_CFLAGS)		\
+	$(LIBNOTIFY_CFLAGS)
 
 if MAINTAINER_MODE
 

Added: xfce4-power-manager/trunk/libxfpm/xfpm-notify.c
===================================================================
--- xfce4-power-manager/trunk/libxfpm/xfpm-notify.c	                        (rev 0)
+++ xfce4-power-manager/trunk/libxfpm/xfpm-notify.c	2009-03-23 19:09:13 UTC (rev 6983)
@@ -0,0 +1,210 @@
+/*
+ * * Copyright (C) 2008-2009 Ali <aliov at xfce.org>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+
+#ifdef HAVE_ERRNO_H
+#include <errno.h>
+#endif
+
+#include <gtk/gtk.h>
+#include <libxfce4util/libxfce4util.h>
+
+#include <libnotify/notify.h>
+
+#include "libxfpm/xfpm-common.h"
+
+#include "xfpm-notify.h"
+
+/* Init */
+static void xfpm_notify_class_init (XfpmNotifyClass *klass);
+static void xfpm_notify_init       (XfpmNotify *notify);
+static void xfpm_notify_finalize   (GObject *object);
+
+#define XFPM_NOTIFY_GET_PRIVATE(o) \
+(G_TYPE_INSTANCE_GET_PRIVATE((o), XFPM_TYPE_NOTIFY, XfpmNotifyPrivate))
+
+struct XfpmNotifyPrivate
+{
+    NotifyNotification *notification;
+};
+
+G_DEFINE_TYPE(XfpmNotify, xfpm_notify, G_TYPE_OBJECT)
+
+static void
+xfpm_notify_class_init(XfpmNotifyClass *klass)
+{
+    GObjectClass *object_class = G_OBJECT_CLASS(klass);
+
+    object_class->finalize = xfpm_notify_finalize;
+
+
+    g_type_class_add_private(klass,sizeof(XfpmNotifyPrivate));
+}
+
+static void
+xfpm_notify_init(XfpmNotify *notify)
+{
+    notify->priv = XFPM_NOTIFY_GET_PRIVATE(notify);
+    
+    notify->priv->notification = NULL;
+        
+    notify_init ("xfce4-power-manager");
+}
+
+static void
+xfpm_notify_finalize(GObject *object)
+{
+    XfpmNotify *notify;
+
+    notify = XFPM_NOTIFY(object);
+    
+    G_OBJECT_CLASS(xfpm_notify_parent_class)->finalize(object);
+}
+
+static void
+xfpm_notify_set_icon (XfpmNotify *notify, NotifyNotification *n, const gchar *icon_name )
+{
+    GdkPixbuf *pix = xfpm_load_icon (icon_name, 48);
+    
+    if ( pix )
+    {
+	notify_notification_set_icon_from_pixbuf (n, 
+						  pix);
+	g_object_unref ( G_OBJECT(pix));
+    }
+}
+
+static NotifyNotification *
+xfpm_notify_new_notification_internal (XfpmNotify *notify, const gchar *title, const gchar *message,
+				       const gchar *icon_name, guint timeout,
+				       XfpmNotifyUrgency urgency, GtkStatusIcon *icon)
+{
+    NotifyNotification *n;
+    
+    n = notify_notification_new (title, message, NULL, NULL);
+    
+    if ( icon_name )
+    	xfpm_notify_set_icon (notify, n, icon_name);
+	
+    if ( icon )
+    	notify_notification_attach_to_status_icon (n, icon);
+	
+    notify_notification_set_urgency (n, urgency);
+    notify_notification_set_timeout (n, timeout);
+    
+    return n;
+}
+
+static void
+xfpm_notify_closed_cb (NotifyNotification *n, XfpmNotify *notify)
+{
+    notify->priv->notification = NULL;
+    g_object_unref (G_OBJECT(n));
+}
+
+static void
+xfpm_notify_close_notification (XfpmNotify *notify )
+{
+    if ( notify->priv->notification )
+    {
+    	if (!notify_notification_close (notify->priv->notification, NULL))
+	    g_warning ("Failed to close notification\n");
+	
+	g_object_unref (G_OBJECT(notify->priv->notification) );
+	notify->priv->notification  = NULL;
+    }
+}
+
+XfpmNotify *
+xfpm_notify_new(void)
+{
+    XfpmNotify *notify = NULL;
+    notify = g_object_new (XFPM_TYPE_NOTIFY, NULL);
+    return notify;
+}
+
+void xfpm_notify_show_notification (XfpmNotify *notify, const gchar *title,
+				    const gchar *text,  const gchar *icon_name,
+				    gint timeout, gboolean simple,
+				    XfpmNotifyUrgency urgency, GtkStatusIcon *icon)
+{
+    if ( !simple )
+        xfpm_notify_close_notification (notify);
+    
+    NotifyNotification *n = xfpm_notify_new_notification_internal (notify, title, 
+							           text, icon_name, 
+								   timeout, urgency, 
+								   icon);
+    xfpm_notify_present_notification (notify, n, simple);
+}
+
+NotifyNotification *xfpm_notify_new_notification (XfpmNotify *notify,
+						  const gchar *title,
+						  const gchar *text,
+						  const gchar *icon_name,
+						  guint timeout,
+						  XfpmNotifyUrgency urgency,
+						  GtkStatusIcon *icon)
+{
+    NotifyNotification *n = xfpm_notify_new_notification_internal (notify, title, 
+							           text, icon_name, 
+								   timeout, urgency, 
+								   icon);
+    return n;
+}
+
+void xfpm_notify_add_action_to_notification (XfpmNotify *notify, NotifyNotification *n,
+					    const gchar *id, const gchar *action_label,
+					    NotifyActionCallback callback, gpointer data)
+{
+    g_return_if_fail (XFPM_IS_NOTIFY(notify));
+    
+    notify_notification_add_action (n, id, action_label,
+				   (NotifyActionCallback)callback,
+				    data, NULL);
+    
+}
+
+void xfpm_notify_present_notification (XfpmNotify *notify, NotifyNotification *n, gboolean simple)
+{
+    g_return_if_fail (XFPM_IS_NOTIFY(notify));
+    
+    if ( !simple )
+    {
+	g_signal_connect (G_OBJECT(n),"closed",
+			G_CALLBACK(xfpm_notify_closed_cb), notify);
+	notify->priv->notification = n;
+    }
+    
+    notify_notification_show (n, NULL);
+}

Added: xfce4-power-manager/trunk/libxfpm/xfpm-notify.h
===================================================================
--- xfce4-power-manager/trunk/libxfpm/xfpm-notify.h	                        (rev 0)
+++ xfce4-power-manager/trunk/libxfpm/xfpm-notify.h	2009-03-23 19:09:13 UTC (rev 6983)
@@ -0,0 +1,90 @@
+/*
+ * * Copyright (C) 2008-2009 Ali <aliov at xfce.org>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifndef __XFPM_NOTIFY_H
+#define __XFPM_NOTIFY_H
+
+#include <glib-object.h>
+#include <gtk/gtk.h>
+
+#include <libnotify/notify.h>
+
+G_BEGIN_DECLS
+
+#define XFPM_TYPE_NOTIFY        (xfpm_notify_get_type () )
+#define XFPM_NOTIFY(o)          (G_TYPE_CHECK_INSTANCE_CAST((o), XFPM_TYPE_NOTIFY, XfpmNotify))
+#define XFPM_IS_NOTIFY(o)       (G_TYPE_CHECK_INSTANCE_TYPE((o), XFPM_TYPE_NOTIFY))
+
+typedef enum 
+{
+    XFPM_NOTIFY_LOW,
+    XFPM_NOTIFY_CRITICAL,
+    XFPM_NOTIFY_NORMAL
+    
+} XfpmNotifyUrgency;
+
+typedef struct XfpmNotifyPrivate XfpmNotifyPrivate;
+
+typedef struct
+{
+    GObject		  parent;
+    XfpmNotifyPrivate	 *priv;
+    
+} XfpmNotify;
+
+typedef struct
+{
+    GObjectClass          parent_class;
+    
+} XfpmNotifyClass;
+
+GType        	 	  xfpm_notify_get_type          	    (void) G_GNUC_CONST;
+XfpmNotify      	 *xfpm_notify_new                           (void);
+
+void             	  xfpm_notify_show_notification 	    (XfpmNotify *notify,
+								     const gchar *title,
+								     const gchar *text,
+								     const gchar *icon_name,
+								     gint timeout,
+								     gboolean simple,
+								     XfpmNotifyUrgency urgency,
+								     GtkStatusIcon *icon);
+
+NotifyNotification       *xfpm_notify_new_notification  	    (XfpmNotify *notify,
+								     const gchar *title,
+								     const gchar *text,
+								     const gchar *icon_name,
+								     guint timeout,
+								     XfpmNotifyUrgency urgency,
+								     GtkStatusIcon *icon);
+
+void 			  xfpm_notify_add_action_to_notification    (XfpmNotify *notify, 
+								     NotifyNotification *n,
+								     const gchar *id, 
+								     const gchar *action_label,
+								     NotifyActionCallback callback, 
+								     gpointer data);
+								     
+void 			  xfpm_notify_present_notification 	    (XfpmNotify *notify, 
+								     NotifyNotification *n,
+								     gboolean simple);
+G_END_DECLS
+
+#endif /* __XFPM_NOTIFY_H */

Modified: xfce4-power-manager/trunk/panel-plugins/inhibit/Makefile.am
===================================================================
--- xfce4-power-manager/trunk/panel-plugins/inhibit/Makefile.am	2009-03-23 14:35:28 UTC (rev 6982)
+++ xfce4-power-manager/trunk/panel-plugins/inhibit/Makefile.am	2009-03-23 19:09:13 UTC (rev 6983)
@@ -11,6 +11,7 @@
 	-DLOCALEDIR=\"$(localedir)\"  		\
 	$(LIBXFCE4PANEL_CFLAGS)       		\
 	$(LIBXFCE4UTIL_CFLAGS)        		\
+	$(LIBNOTIFY_CFLAGS)			\
 	$(GTK_CFLAGS)			      	\
 	$(GLIB_CFLAGS)				\
 	$(DBUS_GLIB_CFLAGS)
@@ -19,6 +20,7 @@
 	$(top_builddir)/libxfpm/libxfpmcommon.la\
 	$(LIBXFCE4PANEL_LIBS)   		\
 	$(LIBXFCE4UTIL_LIBS)			\
+	$(LIBNOTIFY_LIBS)			\
 	$(GTK_LIBS)   				\
 	$(GLIB_LIBS)				\
 	$(DBUS_GLIB_LIBS)

Modified: xfce4-power-manager/trunk/panel-plugins/inhibit/inhibit-plugin.c
===================================================================
--- xfce4-power-manager/trunk/panel-plugins/inhibit/inhibit-plugin.c	2009-03-23 14:35:28 UTC (rev 6982)
+++ xfce4-power-manager/trunk/panel-plugins/inhibit/inhibit-plugin.c	2009-03-23 19:09:13 UTC (rev 6983)
@@ -38,6 +38,8 @@
 
 #include "libxfpm/xfpm-common.h"
 #include "libxfpm/xfpm-dbus.h"
+#include "libxfpm/xfpm-notify.h"
+#include "libxfpm/xfpm-string.h"
 
 #include "inhibit-client.h"
 
@@ -48,17 +50,60 @@
     DBusGConnection  *bus;
     DBusGProxy       *proxy;
     
+    XfpmNotify       *notify;
+    
     GtkWidget        *button;
     GtkWidget        *image;
     
     gboolean          connected;
     guint             cookie;
     
-    gboolean          inhibited;
+    gboolean          plugin_inhibited;
 
 } inhibit_t;
 
+/*
+ * Read a bool property
+ * Returns: bool property value, true is the fallback
+ */
 static gboolean
+inhibit_plugin_read_bool_entry (const gchar *property)
+{
+    gchar *file;
+    XfceRc *rc;
+    gboolean value;
+    
+    file = xfce_resource_save_location (XFCE_RESOURCE_CONFIG, "xfce4/panel/inhibit.rc", TRUE);
+    rc = xfce_rc_simple_open (file, FALSE);
+    g_free (file);
+    
+    value = xfce_rc_read_bool_entry (rc, property, TRUE);
+    return value;
+}
+
+/*
+ * Save a bool entry
+ */
+static void
+inhibit_plugin_save_bool_entry (const gchar *property, gboolean value)
+{
+    gchar *file;
+    XfceRc *rc;
+    
+    file = xfce_resource_save_location (XFCE_RESOURCE_CONFIG, "xfce4/panel/inhibit.rc", TRUE);
+    
+    rc = xfce_rc_simple_open (file, FALSE);
+    g_free (file);
+    
+    xfce_rc_write_bool_entry (rc, property, value);
+    xfce_rc_close (rc);
+}
+
+/*
+ *  Used to set of update the icon size
+ *  returns true if successful, false if failure.
+ */
+static gboolean
 inhibit_plugin_set_icon (inhibit_t *inhibit, gint width)
 {
     GdkPixbuf *pixbuf;
@@ -74,15 +119,9 @@
     return FALSE;
 }
 
-static void
-inhibit_plugin_set_button (inhibit_t *inhibit)
-{
-    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(inhibit->button), inhibit->inhibited);
-    gtk_widget_set_tooltip_text (inhibit->button, 
-			         inhibit->inhibited ? _("Automatic sleep inhibited") :
-						      _("Automatic sleep enabled"));
-}
-
+/*
+ * Size of the panel changed
+ */
 static gboolean
 inhibit_plugin_size_changed_cb (XfcePanelPlugin *plugin, gint size, inhibit_t *inhibit)
 {
@@ -90,88 +129,261 @@
 				  inhibit->button->style->xthickness);
 				 
     gtk_widget_set_size_request (GTK_WIDGET(plugin), size, size);
+    
     return inhibit_plugin_set_icon (inhibit, width);
 }
 
-static void 
-inhibit_plugin_free_data_cb (XfcePanelPlugin *plugin, inhibit_t *inhibit)
+/*
+ * Get the inhibition state of the running instance of the power manager
+ * returns: false is not instance running or no inhibit is set
+ *          true is the power manager is inhibited
+ */
+static gboolean
+inhibit_plugin_get_inhibit (inhibit_t *inhibit)
 {
-    g_free (inhibit);
-}
-
-static void
-inhibit_changed_cb (DBusGProxy *proxy, gboolean inhibited, inhibit_t *inhibit)
-{
-    TRACE("Inhibit changed %d", inhibited);
-    inhibit->inhibited = inhibited;
-    inhibit_plugin_set_button (inhibit);
-}
-
-static void
-inhibit_plugin_get_inhibit (inhibit_t *plugin)
-{
     GError *error = NULL;
+    gboolean inhibited;
     
-    if (!xfpm_inhibit_dbus_client_has_inhibit (plugin->proxy, &plugin->inhibited, &error) )
+    if ( !inhibit->connected )
+	return FALSE;
+    
+    if (!xfpm_inhibit_dbus_client_has_inhibit (inhibit->proxy, &inhibited, &error) )
     {
 	g_critical ("Unable to get inhibit state: %s", error->message);
 	g_error_free (error);
-	return;
+	return FALSE;
     }
-    
-    inhibit_plugin_set_button (plugin);
+    return inhibited;
 }
 
+/*
+ * Send the inhibit message and store the cookie to be used
+ * later when we want to UnInhibit
+ */
 static void
-inhibit_plugin_set_inhibit (inhibit_t *plugin)
+inhibit_plugin_set_inhibit (inhibit_t *inhibit)
 {
     GError *error = NULL;
     
     const gchar *app = "Inhibit plugin";
     const gchar *reason = "User settings";
     
-    if (!xfpm_inhibit_dbus_client_inhibit (plugin->proxy, app, reason, &plugin->cookie, &error))
+    if (!xfpm_inhibit_dbus_client_inhibit (inhibit->proxy, app, reason, &inhibit->cookie, &error))
     {
 	g_critical ("Unable to set inhibit: %s", error->message);
 	g_error_free (error);
 	return;
     }
-    
-    inhibit_plugin_set_button (plugin);
 }
 
+/*
+ * Send the unset inhibit message with the cookie already saved
+ */
 static void
-inhibit_plugin_unset_inhibit (inhibit_t *plugin)
+inhibit_plugin_unset_inhibit (inhibit_t *inhibit)
 {
     GError *error = NULL;
     
-    if (!xfpm_inhibit_dbus_client_un_inhibit (plugin->proxy, plugin->cookie, &error))
+    if (!xfpm_inhibit_dbus_client_un_inhibit (inhibit->proxy, inhibit->cookie, &error))
     {
 	g_critical ("Unable to set UnInhibit: %s", error->message);
 	g_error_free (error);
 	return;
     }
-    inhibit_plugin_set_button (plugin);
 }
 
+/*
+ * Set the tooltip of the button widget
+ */
 static void
+inhibit_plugin_set_tooltip (inhibit_t *inhibit)
+{
+    gboolean inhibited;
+    TRACE ("inhibititon =%s", xfpm_bool_to_string (inhibit->plugin_inhibited));
+    
+    if ( !inhibit->connected )
+	gtk_widget_set_tooltip_text (inhibit->button, _("No power manager instance running") );
+    else if ( inhibit->plugin_inhibited )
+    {
+	gtk_widget_set_tooltip_text (inhibit->button, _("Automatic sleep inhibited") );
+    }
+    else
+    {
+	inhibited = inhibit_plugin_get_inhibit (inhibit);
+	if ( inhibited )
+	    gtk_widget_set_tooltip_text (inhibit->button, _("Another application is disabling the automatic sleep") );
+	else
+	    gtk_widget_set_tooltip_text (inhibit->button, _("Automatic sleep enabled"));
+    }
+}
+
+/*
+ * Set the button toggled state in respect to the inhibition state
+ */
+static void
+inhibit_plugin_set_button (inhibit_t *inhibit)
+{
+    gboolean inhibited;
+    
+    if ( !inhibit->connected )
+    {
+	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(inhibit->button), FALSE);
+	return;
+    }
+	
+    if ( inhibit->plugin_inhibited )
+	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(inhibit->button), TRUE);
+    else
+    {
+	inhibited = inhibit_plugin_get_inhibit (inhibit);
+        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(inhibit->button), inhibited);
+    }
+}
+
+/*
+ * Refresh all the info (button+tooltips)
+ */
+static void
+inhibit_plugin_refresh_info (inhibit_t *inhibit)
+{
+    inhibit_plugin_set_button (inhibit);
+    inhibit_plugin_set_tooltip (inhibit);
+}
+
+static void
+inhibit_plugin_notify_callback (NotifyNotification *n, const gchar *id, inhibit_t *inhibit)
+{
+    if ( xfpm_strequal (id, "inhibit-changed-notification") )
+    {
+	inhibit_plugin_save_bool_entry ("inhibit-changed-notification", FALSE);
+    }
+    else if ( xfpm_strequal (id, "power-manager-disconnected-notification") )
+    {
+	inhibit_plugin_save_bool_entry ("power-manager-disconnected-notification", FALSE);
+    }
+}
+
+/*
+ * Standard signal sent by the power manager to inform us about the 
+ * inhibition status
+ */
+static void
+inhibit_changed_cb (DBusGProxy *proxy, gboolean inhibited, inhibit_t *inhibit)
+{
+    gboolean show_notification;
+    NotifyNotification *n;
+    const gchar *message;
+    
+    TRACE("Inhibit changed %d", inhibited);
+    inhibit_plugin_refresh_info (inhibit);
+    
+    show_notification = inhibit_plugin_read_bool_entry ("inhibit-changed-notification");
+    
+    if ( show_notification )
+    {
+	message = inhibited ? (_("Power manager automatic sleep is disabled")) :
+			      (_("Power manager automatic sleep is enabled")) ;
+			      
+	n = xfpm_notify_new_notification (inhibit->notify,
+				      (_("Inhibit plugin")),
+				      message,
+				      "gnome-inhibit-applet",
+				      5000,
+				      XFPM_NOTIFY_NORMAL,
+				      NULL);
+				      
+	xfpm_notify_add_action_to_notification (inhibit->notify,
+						n,
+						"inhibit-changed-notification",
+						(_("Don't show again")),
+						(NotifyActionCallback) inhibit_plugin_notify_callback,
+						inhibit);
+						
+	notify_notification_attach_to_widget (n, inhibit->button);
+	
+	xfpm_notify_present_notification (inhibit->notify, n, FALSE);
+    }
+}
+
+/*
+ * This should be called when the running instance of the power manager
+ * disappears from the session bus name. 
+ */
+static void
 proxy_destroy_cb (DBusGProxy *proxy, inhibit_t *inhibit)
 {
+    gboolean notify;
+    NotifyNotification *n;
     TRACE("Power manager disconnected");
     
-    gtk_widget_set_tooltip_text (inhibit->button, _("Power manager disconnected"));
-   // dbus_g_proxy_disconnect_signal (inhibit->proxy, "HasInhibitChanged",
-	//			    G_CALLBACK(inhibit_changed_cb), inhibit);
+    notify = inhibit_plugin_read_bool_entry ("power-manager-disconnected-notification");
+    if ( notify )
+    {
+    
+	n = xfpm_notify_new_notification (inhibit->notify,
+				      (_("Inhibit plugin")),
+				      (_("Power manager disconnected")),
+				      "gnome-inhibit-applet",
+				      5000,
+				      XFPM_NOTIFY_NORMAL,
+				      NULL);
+				      
+	xfpm_notify_add_action_to_notification (inhibit->notify,
+						n,
+						"power-manager-disconnected-notification",
+						(_("Don't show again")),
+						(NotifyActionCallback) inhibit_plugin_notify_callback,
+						inhibit);
+				      
+	notify_notification_attach_to_widget (n, inhibit->button);
+	xfpm_notify_present_notification (inhibit->notify, n, FALSE);
+    }
     inhibit->proxy = NULL;
     inhibit->connected = FALSE;
+    inhibit_plugin_refresh_info (inhibit);
 }
 
+/*
+ * Destroying the proxy, but we block the destroy signal before as we 
+ * want to get the destroy signal only if the running instance of 
+ * the power manager disappears from the session bus.
+ */
 static void
-inhibit_plugin_connect_more (inhibit_t *plugin)
+inhibit_plugin_disconnect_proxy (inhibit_t *inhibit)
 {
+    g_signal_handlers_block_by_func (inhibit->proxy, proxy_destroy_cb, inhibit);
+    g_object_unref (inhibit->proxy);
+    inhibit->proxy = NULL;
+}
+
+/*
+ * Free all the allocated memory, called when the plugin is removed from the panel
+ */
+static void 
+inhibit_plugin_free_data_cb (XfcePanelPlugin *plugin, inhibit_t *inhibit)
+{
+    if ( inhibit->bus )
+	dbus_g_connection_unref (inhibit->bus);
+	
+    if ( inhibit->proxy )
+	inhibit_plugin_disconnect_proxy (inhibit);
+	
+    g_object_unref (inhibit->notify);
+    
+    g_free (inhibit);
+}
+
+/*
+ * Create the proxy on the inhibit interface and then connect to the signals
+ * this function sets the boolean inhibit->connected to false if failure and to
+ * true if all goes fine.
+ */
+static void
+inhibit_plugin_connect_more (inhibit_t *inhibit)
+{
     GError *error = NULL;
     
-    plugin->proxy = dbus_g_proxy_new_for_name_owner  (plugin->bus,
+    inhibit->proxy = dbus_g_proxy_new_for_name_owner  (inhibit->bus,
 						      "org.freedesktop.PowerManagement",
 						      "/org/freedesktop/PowerManagement/Inhibit",
 						      "org.freedesktop.PowerManagement.Inhibit",
@@ -180,101 +392,128 @@
     {
 	g_warning ("Unable to get name owner: %s", error->message);
 	g_error_free (error);
-	plugin->connected = FALSE;
+	inhibit->connected = FALSE;
 	return;
     }
     
-    dbus_g_proxy_add_signal (plugin->proxy, "HasInhibitChanged", 
+    dbus_g_proxy_add_signal (inhibit->proxy, "HasInhibitChanged", 
 			     G_TYPE_BOOLEAN, G_TYPE_INVALID);
     
-    dbus_g_proxy_connect_signal (plugin->proxy, "HasInhibitChanged", 
-				 G_CALLBACK (inhibit_changed_cb), plugin, NULL);
+    dbus_g_proxy_connect_signal (inhibit->proxy, "HasInhibitChanged", 
+				 G_CALLBACK (inhibit_changed_cb), inhibit, NULL);
     
-    g_signal_connect (plugin->proxy, "destroy",
-		      G_CALLBACK(proxy_destroy_cb), plugin);
+    g_signal_connect (inhibit->proxy, "destroy",
+		      G_CALLBACK(proxy_destroy_cb), inhibit);
 		      
-    plugin->connected = TRUE;
-    inhibit_plugin_get_inhibit (plugin);
+    inhibit->connected = TRUE;
 }
 
+/*
+ * Checks if a power manager found on the session bus and has a inhibit interface
+ * The names are Freedesktop standard.
+ */
 static void
-inhibit_plugin_connect (inhibit_t *plugin)
+inhibit_plugin_connect (inhibit_t *inhibit)
 {
-    if (!plugin->bus )
-	plugin->bus = dbus_g_bus_get (DBUS_BUS_SESSION, NULL);
+    if (!inhibit->bus )
+	inhibit->bus = dbus_g_bus_get (DBUS_BUS_SESSION, NULL);
     
-    if ( !xfpm_dbus_name_has_owner (dbus_g_connection_get_connection(plugin->bus),
+    if ( !xfpm_dbus_name_has_owner (dbus_g_connection_get_connection(inhibit->bus),
 				    "org.freedesktop.PowerManagement") )
     {
-	gtk_widget_set_tooltip_text (plugin->button, _("No power manager instance running"));
-	plugin->connected = FALSE;
+	gtk_widget_set_tooltip_text (inhibit->button, _("No power manager instance running"));
+	inhibit->connected = FALSE;
 	return;
     }
     
-    if ( !xfpm_dbus_name_has_owner (dbus_g_connection_get_connection(plugin->bus),
+    if ( !xfpm_dbus_name_has_owner (dbus_g_connection_get_connection(inhibit->bus),
 				    "org.freedesktop.PowerManagement.Inhibit") )
     {
-	gtk_widget_set_tooltip_text (plugin->button, _("No power manager instance running"));
-	plugin->connected = FALSE;
+	gtk_widget_set_tooltip_text (inhibit->button, _("No power manager instance running"));
+	inhibit->connected = FALSE;
 	return;
     }
 }
 
+/*
+ * Disconnecting the proxy and reloading all information
+ */
 static void
-reload_activated (GtkWidget *widget, inhibit_t *plugin)
+reload_activated (GtkWidget *widget, inhibit_t *inhibit)
 {
-    if ( plugin->proxy )
-	proxy_destroy_cb (plugin->proxy, plugin);
+    if ( inhibit->proxy )
+    {
+	inhibit_plugin_disconnect_proxy (inhibit);
+	inhibit->connected = FALSE;
+    }
 	
-    inhibit_plugin_connect (plugin);
-    inhibit_plugin_connect_more (plugin);
-    
+    inhibit_plugin_connect (inhibit);
+    inhibit_plugin_connect_more (inhibit);
+    inhibit_plugin_refresh_info (inhibit);
 }
 
-static void
-button_toggled_cb (GtkWidget *widget, inhibit_t *plugin)
+/*
+ * Button press events, Inhibit if pressed, UnInhibit if released
+ */
+static gboolean
+button_press_event_cb (GtkWidget *button, GdkEventButton *ev, inhibit_t *inhibit)
 {
-    if ( !plugin->connected )
-	return;
-	
-    gboolean toggled = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widget));
+    if ( ev->button != 1 )
+	return FALSE;
     
-    if ( toggled == TRUE && toggled != plugin->inhibited )
-	inhibit_plugin_set_inhibit (plugin);
-    else if ( toggled == FALSE && toggled != plugin->inhibited )
-	inhibit_plugin_unset_inhibit (plugin);
+    /*User ask us to inhibit ?*/
+    //FIXME: Check if we manage to inhibit
+    if ( !inhibit->plugin_inhibited )
+    {
+	inhibit_plugin_set_inhibit (inhibit);
+	inhibit->plugin_inhibited = TRUE;
+    }
+    else
+    {
+	inhibit_plugin_unset_inhibit (inhibit);
+	inhibit->plugin_inhibited = FALSE;
+    }
+    TRACE("button press event %s", xfpm_bool_to_string (inhibit->plugin_inhibited));
+    inhibit_plugin_refresh_info (inhibit);
+    return TRUE;
 }
 
+/*
+ * Constructor of the plugin
+ */
 static void
-inhibit_plugin_construct (inhibit_t *plugin)
+inhibit_plugin_construct (inhibit_t *inhibit)
 {
     GtkWidget *mi;
     
-    plugin->image = gtk_image_new ();
-    plugin->button = gtk_toggle_button_new ();
+    inhibit->image = gtk_image_new ();
+    inhibit->button = gtk_toggle_button_new ();
+    inhibit->notify = xfpm_notify_new ();
     
-    gtk_container_add (GTK_CONTAINER(plugin->button), plugin->image);
+    gtk_container_add (GTK_CONTAINER(inhibit->button), inhibit->image);
     
-    gtk_button_set_relief (GTK_BUTTON(plugin->button), GTK_RELIEF_NONE);
+    gtk_button_set_relief (GTK_BUTTON(inhibit->button), GTK_RELIEF_NONE);
     
-    g_signal_connect (plugin->button, "toggled",
-		      G_CALLBACK(button_toggled_cb), plugin);
+    g_signal_connect (inhibit->button, "button-press-event",
+		      G_CALLBACK(button_press_event_cb), inhibit);
     
-    gtk_container_add (GTK_CONTAINER(plugin->plugin), plugin->button);
+    gtk_container_add (GTK_CONTAINER(inhibit->plugin), inhibit->button);
     
-    xfce_panel_plugin_add_action_widget (plugin->plugin, plugin->button);
+    xfce_panel_plugin_add_action_widget (inhibit->plugin, inhibit->button);
     
     mi = gtk_image_menu_item_new_from_stock (GTK_STOCK_REFRESH, NULL);
     gtk_widget_show (mi);
-    
     g_signal_connect (mi, "activate",
-		      G_CALLBACK(reload_activated), plugin);
+		      G_CALLBACK(reload_activated), inhibit);
 		      
-    xfce_panel_plugin_menu_insert_item (plugin->plugin, GTK_MENU_ITEM(mi));
+    xfce_panel_plugin_menu_insert_item (inhibit->plugin, GTK_MENU_ITEM(mi));
 
-    gtk_widget_show_all (plugin->button);
+    gtk_widget_show_all (inhibit->button);
 }
 
+/*
+ * register_inhibit_plugin: called by the panel
+ */
 static void
 register_inhibit_plugin (XfcePanelPlugin *plugin)
 {
@@ -289,6 +528,8 @@
     inhibit_plugin_connect (inhibit);
     inhibit_plugin_connect_more (inhibit);
     
+    inhibit_plugin_refresh_info (inhibit);
+    
     g_signal_connect (plugin, "free-data",
 		      G_CALLBACK(inhibit_plugin_free_data_cb), inhibit);
 		      

Modified: xfce4-power-manager/trunk/src/Makefile.am
===================================================================
--- xfce4-power-manager/trunk/src/Makefile.am	2009-03-23 14:35:28 UTC (rev 6982)
+++ xfce4-power-manager/trunk/src/Makefile.am	2009-03-23 19:09:13 UTC (rev 6983)
@@ -45,8 +45,6 @@
 	xfpm-screen-saver.h			\
 	xfpm-tray-icon.c			\
 	xfpm-tray-icon.h			\
-	xfpm-notify.c				\
-	xfpm-notify.h				\
 	xfpm-network-manager.c			\
 	xfpm-network-manager.h			\
 	xfpm-config.h				\

Modified: xfce4-power-manager/trunk/src/xfpm-battery.c
===================================================================
--- xfce4-power-manager/trunk/src/xfpm-battery.c	2009-03-23 14:35:28 UTC (rev 6982)
+++ xfce4-power-manager/trunk/src/xfpm-battery.c	2009-03-23 19:09:13 UTC (rev 6983)
@@ -43,7 +43,6 @@
 #include "xfpm-battery-info.h"
 #include "xfpm-xfconf.h"
 #include "xfpm-config.h"
-#include "xfpm-notify.h"
 #include "xfpm-adapter.h"
 
 /* Init */

Modified: xfce4-power-manager/trunk/src/xfpm-supply.c
===================================================================
--- xfce4-power-manager/trunk/src/xfpm-supply.c	2009-03-23 14:35:28 UTC (rev 6982)
+++ xfce4-power-manager/trunk/src/xfpm-supply.c	2009-03-23 19:09:13 UTC (rev 6983)
@@ -34,11 +34,11 @@
 #include "libxfpm/hal-iface.h"
 #include "libxfpm/xfpm-string.h"
 #include "libxfpm/xfpm-common.h"
+#include "libxfpm/xfpm-notify.h"
 
 #include "xfpm-supply.h"
 #include "xfpm-adapter.h"
 #include "xfpm-battery.h"
-#include "xfpm-notify.h"
 #include "xfpm-enum.h"
 #include "xfpm-enum-types.h"
 #include "xfpm-xfconf.h"
@@ -254,7 +254,7 @@
 static void
 _notify_action_callback (NotifyNotification *n, gchar *action, XfpmSupply *supply)
 {
-    if ( xfpm_strequal(action, "shutdow") )
+    if ( xfpm_strequal(action, "shutdown") )
 	g_signal_emit (G_OBJECT(supply ), signals[SHUTDOWN_REQUEST], 0, XFPM_DO_SHUTDOWN);
     else if ( xfpm_strequal(action, "hibernate") )
 	g_signal_emit (G_OBJECT(supply ), signals[SHUTDOWN_REQUEST], 0, XFPM_DO_SHUTDOWN);




More information about the Goodies-commits mailing list