[Xfce4-commits] <thunar-volman:jannis/port-to-udev> Display detection and mount notifications if libnotify is available.

Jannis Pohlmann noreply at xfce.org
Thu Jul 22 13:50:01 CEST 2010


Updating branch refs/heads/jannis/port-to-udev
         to 2f75a7e81a1802866c1e381221ee36994f411499 (commit)
       from 45b7e85f817ec5472ff82ff1293a97542929b045 (commit)

commit 2f75a7e81a1802866c1e381221ee36994f411499
Author: Jannis Pohlmann <jannis at xfce.org>
Date:   Thu Jul 22 13:47:49 2010 +0200

    Display detection and mount notifications if libnotify is available.

 configure.in.in                                    |   13 +++-
 thunar-volman/Makefile.am                          |   11 +++-
 thunar-volman/main.c                               |   23 ++++++
 thunar-volman/tvm-block-device.c                   |   62 ++++++++++++++-
 thunar-volman/tvm-input-device.c                   |   23 ++++++
 thunar-volman/tvm-notify.c                         |   83 ++++++++++++++++++++
 thunar-volman/{tvm-input-device.h => tvm-notify.h} |   16 ++--
 thunar-volman/tvm-usb-device.c                     |   15 ++++
 8 files changed, 234 insertions(+), 12 deletions(-)

diff --git a/configure.in.in b/configure.in.in
index 89ba231..2189714 100644
--- a/configure.in.in
+++ b/configure.in.in
@@ -91,6 +91,12 @@ XDT_CHECK_PACKAGE([LIBXFCE4UI], [libxfce4ui-1], [4.7.0])
 XDT_CHECK_PACKAGE([LIBXFCE4UTIL], [libxfce4util-1.0], [4.7.0])
 XDT_CHECK_PACKAGE([XFCONF], [libxfconf-0], [4.7.0])
 
+dnl *******************************************************
+dnl *** Optional mount notification support (libnotify) ***
+dnl *******************************************************
+XDT_CHECK_OPTIONAL_PACKAGE([LIBNOTIFY], [libnotify], [0.4.0], [notifications], 
+                           [Notifications support], [yes])
+
 dnl ***********************************
 dnl *** Check for debugging support ***
 dnl ***********************************
@@ -130,5 +136,10 @@ dnl ***************************
 echo
 echo "Build Configuration:"
 echo
-echo "* Debug Support:             $enable_debug"
+if test x"$LIBNOTIFY_FOUND" = x"yes"; then
+echo "* Mount notifications: yes"
+else
+echo "* Mount notifications: no"
+fi
+echo "* Debug Support:       $enable_debug"
 echo
diff --git a/thunar-volman/Makefile.am b/thunar-volman/Makefile.am
index e64b656..4e9b00b 100644
--- a/thunar-volman/Makefile.am
+++ b/thunar-volman/Makefile.am
@@ -28,6 +28,12 @@ INCLUDES =								\
 bin_PROGRAMS =								\
 	thunar-volman
 
+if HAVE_LIBNOTIFY
+tvm_notify_sources =							\
+	tvm-notify.c							\
+	tvm-notify.h
+endif
+
 thunar_volman_SOURCES =							\
 	main.c								\
 	tvm-block-device.c						\
@@ -47,7 +53,8 @@ thunar_volman_SOURCES =							\
 	tvm-run.c							\
 	tvm-run.h							\
 	tvm-usb-device.c						\
-	tvm-usb-device.h
+	tvm-usb-device.h						\
+	$(tvm_notify_sources)
 
 thunar_volman_CFLAGS =							\
 	$(DBUS_CFLAGS)							\
@@ -56,6 +63,7 @@ thunar_volman_CFLAGS =							\
 	$(GTHREAD_CFLAGS)						\
 	$(GTK_CFLAGS)							\
 	$(GUDEV_CFLAGS)							\
+	$(LIBNOTIFY_CFLAGS)						\
 	$(LIBXFCE4UI_CFLAGS)						\
 	$(LIBXFCE4UTIL_CFLAGS)						\
 	$(XFCONF_CFLAGS)						\
@@ -72,6 +80,7 @@ thunar_volman_LDADD =							\
 	$(GTHREAD_LIBS)							\
 	$(GTK_LIBS)							\
 	$(GUDEV_LIBS)							\
+	$(LIBNOTIFY_LIBS)						\
 	$(LIBXFCE4UI_LIBS)						\
 	$(LIBXFCE4UTIL_LIBS)						\
 	$(XFCONF_LIBS)
diff --git a/thunar-volman/main.c b/thunar-volman/main.c
index 2943c88..b502059 100644
--- a/thunar-volman/main.c
+++ b/thunar-volman/main.c
@@ -33,6 +33,10 @@
 
 #include <gudev/gudev.h>
 
+#ifdef HAVE_LIBNOTIFY
+#include <libnotify/notify.h>
+#endif
+
 #include <libxfce4util/libxfce4util.h>
 
 #include <xfconf/xfconf.h>
@@ -97,6 +101,20 @@ main (int    argc,
   if (!g_thread_supported ())
     g_thread_init (NULL);
 
+#ifdef HAVE_LIBNOTIFY
+  if (notify_init (PACKAGE_NAME))
+    {
+      /* we do this to work around bugs in libnotify < 0.6.0. Older
+       * versions crash in notify_uninit() when no notifications are
+       * displayed before. These versions also segfault when the 
+       * ret_spec_version parameter of notify_get_server_info is 
+       * NULL... */
+      gchar *spec_version = NULL;
+      notify_get_server_info (NULL, NULL, NULL, &spec_version);
+      g_free (spec_version);
+    }
+#endif
+
   /* initialize GTK+ */
   if (!gtk_init_with_args (&argc, &argv, NULL, option_entries, GETTEXT_PACKAGE, &error))
     {
@@ -196,6 +214,11 @@ main (int    argc,
       exit_code = EXIT_FAILURE;
     }
 
+#ifdef HAVE_LIBNOTIFY
+  if (notify_is_initted ())
+    notify_uninit ();
+#endif
+
   /* release the device context */
   if (context != NULL)
     tvm_context_free (context);
diff --git a/thunar-volman/tvm-block-device.c b/thunar-volman/tvm-block-device.c
index b586df1..77fd287 100644
--- a/thunar-volman/tvm-block-device.c
+++ b/thunar-volman/tvm-block-device.c
@@ -33,6 +33,11 @@
 
 #include <gudev/gudev.h>
 
+#ifdef HAVE_LIBNOTIFY
+#include <libnotify/notify.h>
+#include <thunar-volman/tvm-notify.h>
+#endif
+
 #include <libxfce4util/libxfce4util.h>
 
 #include <thunar-volman/tvm-block-device.h>
@@ -584,14 +589,65 @@ tvm_block_device_mounted (TvmContext *context,
                           GMount     *mount,
                           GError    **error)
 {
-  gboolean success = FALSE;
-  GError  *err = NULL;
-  guint    n;
+  const gchar *summary;
+  const gchar *icon;
+  const gchar *volume_name;
+  gboolean     is_cdrom;
+  gboolean     is_dvd;
+  gboolean     success = FALSE;
+  GError      *err = NULL;
+  gchar       *decoded_name;
+  gchar       *message;
+  guint        n;
 
   g_return_if_fail (context != NULL);
   g_return_if_fail (G_IS_MOUNT (mount));
   g_return_if_fail (error == NULL || *error == NULL);
 
+#ifdef HAVE_LIBNOTIFY
+  /* distinguish between CDs and DVDs */
+  is_cdrom = g_udev_device_get_property_as_boolean (context->device, "ID_CDROM_MEDIA_CD");
+  is_dvd = g_udev_device_get_property_as_boolean (context->device, "ID_CDROM_MEDIA_DVD");
+  
+  if (is_cdrom || is_dvd)
+    {
+      /* generate notification info */
+      icon = "drive-optical";
+      summary = is_cdrom ? _("CD mounted") : _("DVD mounted");
+      message = g_strdup (is_cdrom 
+                          ? _("The CD was mounted automatically") 
+                          : _("The DVD was mounted automatically"));
+    }
+  else
+    {
+      /* fetch the volume name */
+      volume_name = g_udev_device_get_property (context->device, "ID_FS_LABEL_ENC");
+      decoded_name = tvm_notify_decode (volume_name);
+  
+      /* generate notification info */
+      icon = "drive-removable-media";
+      summary = _("Volume mounted");
+      if (decoded_name != NULL)
+        {
+          message = g_strdup_printf (_("The volume \"%s\" was mounted\n"
+                                       "automatically"), decoded_name);
+        }
+      else
+        {
+          message = g_strdup_printf (_("The inserted volume was mounted\n"
+                                       "automatically"));
+        }
+
+      g_free (decoded_name);
+    }
+
+  /* display the notification */
+  tvm_notify (icon, summary, message);
+
+  /* clean up */
+  g_free (message);
+#endif
+
   /* try block device handlers (iPod, cameras etc.) until one succeeds */
   for (n = 0; !success && err == NULL && n < G_N_ELEMENTS (block_device_handlers); ++n)
     success = (block_device_handlers[n]) (context, mount, &err);
diff --git a/thunar-volman/tvm-input-device.c b/thunar-volman/tvm-input-device.c
index 2747be1..ef79b15 100644
--- a/thunar-volman/tvm-input-device.c
+++ b/thunar-volman/tvm-input-device.c
@@ -29,6 +29,7 @@
 #include <thunar-volman/tvm-input-device.h>
 #include <thunar-volman/tvm-context.h>
 #include <thunar-volman/tvm-device.h>
+#include <thunar-volman/tvm-notify.h>
 #include <thunar-volman/tvm-run.h>
 
 
@@ -36,6 +37,9 @@
 void
 tvm_input_device_added (TvmContext *context)
 {
+  const gchar *icon;
+  const gchar *summary;
+  const gchar *message;
   const gchar *id_class;
   const gchar *id_model;
   const gchar *id_usb_driver;
@@ -58,6 +62,10 @@ tvm_input_device_added (TvmContext *context)
       /* we have a keyboard */
       enabled_property = "/autokeyboard/enabled";
       command_property = "/autokeyboard/command";
+
+      icon = _("input-keyboard");
+      summary = _("Keyboard detected");
+      message = _("A keyboard was detected");
     }
   else if (g_strcmp0 (driver, "wacom") == 0 
            || g_strcmp0 (id_usb_driver, "wacom") == 0)
@@ -65,6 +73,10 @@ tvm_input_device_added (TvmContext *context)
       /* we have a wacom tablet */
       enabled_property = "/autotablet/enabled";
       command_property = "/autotablet/command";
+
+      icon = _("input-tablet");
+      summary = _("Tablet detected");
+      message = _("A graphics tablet was detected");
     }
   else if (g_strcmp0 (id_class, "mouse") == 0)
     {
@@ -75,12 +87,20 @@ tvm_input_device_added (TvmContext *context)
           /* we have a tablet that can be used as a mouse */
           enabled_property = "/autotablet/enabled";
           command_property = "/autotablet/command";
+    
+          icon = _("input-tablet");
+          summary = _("Tablet detected");
+          message = _("A graphics tablet was detected");
         }
       else
         {
           /* we have a normal mouse */
           enabled_property = "/automouse/enabled";
           command_property = "/automouse/command";
+    
+          icon = _("input-mouse");
+          summary = _("Mouse detected");
+          message = _("A mouse was detected");
         }
     }
 
@@ -91,6 +111,9 @@ tvm_input_device_added (TvmContext *context)
       enabled = xfconf_channel_get_bool (context->channel, enabled_property, FALSE);
       if (enabled)
         {
+          /* display a detection notification */
+          tvm_notify (icon, summary, message);
+
           /* fetch the command for the input device type and try to run it */
           command = xfconf_channel_get_string (context->channel, command_property, NULL);
           if (command != NULL && *command != '\0')
diff --git a/thunar-volman/tvm-notify.c b/thunar-volman/tvm-notify.c
new file mode 100644
index 0000000..1b30505
--- /dev/null
+++ b/thunar-volman/tvm-notify.c
@@ -0,0 +1,83 @@
+/* vi:set et ai sw=2 sts=2 ts=2: */
+/*-
+ * Copyright (c) 2010 Jannis Pohlmann <jannis at xfce.org>
+ *
+ * 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 <glib.h>
+
+#include <libnotify/notify.h>
+
+#include <libxfce4util/libxfce4util.h>
+
+#include <thunar-volman/tvm-notify.h>
+
+
+
+void
+tvm_notify (const gchar *icon,
+            const gchar *summary,
+            const gchar *message)
+{
+  NotifyNotification *notification;
+
+  notification = notify_notification_new (summary, message, icon, NULL);
+  notify_notification_set_urgency (notification, NOTIFY_URGENCY_NORMAL);
+  notify_notification_set_timeout (notification, NOTIFY_EXPIRES_DEFAULT);
+  notify_notification_show (notification, NULL);
+  g_object_unref (notification);
+}
+
+
+
+gchar *
+tvm_notify_decode (const gchar *str)
+{
+  GString     *string;
+  const gchar *p;
+  gchar       *result;
+  gchar        decoded_c;
+
+  if (str == NULL)
+    return NULL;
+
+  if (!g_utf8_validate (str, -1, NULL))
+    return NULL;
+
+  string = g_string_new (NULL);
+
+  for (p = str; p != NULL && *p != '\0'; ++p)
+    {
+      if (*p == '\\' && p[1] == 'x' && g_ascii_isalnum (p[2]) && g_ascii_isalnum (p[3]))
+        {
+          decoded_c = (g_ascii_xdigit_value (p[2]) << 4) | g_ascii_xdigit_value (p[3]);
+          g_string_append_c (string, decoded_c);
+          p = p + 3;
+        }
+      else
+        g_string_append_c (string, *p);
+    }
+
+  result = string->str;
+  g_string_free (string, FALSE);
+
+  return result;
+}
diff --git a/thunar-volman/tvm-input-device.h b/thunar-volman/tvm-notify.h
similarity index 76%
copy from thunar-volman/tvm-input-device.h
copy to thunar-volman/tvm-notify.h
index 8778deb..9d1c578 100644
--- a/thunar-volman/tvm-input-device.h
+++ b/thunar-volman/tvm-notify.h
@@ -18,18 +18,20 @@
  * Boston, MA 02110-1301, USA.
  */
 
-#ifndef __TVM_INPUT_DEVICE_H__
-#define __TVM_INPUT_DEVICE_H__
+#ifndef __TVM_NOTIFY_H__
+#define __TVM_NOTIFY_H__
 
 #include <glib.h>
 
-#include <thunar-volman/tvm-context.h>
-#include <thunar-volman/tvm-device.h>
-
 G_BEGIN_DECLS
 
-void tvm_input_device_added (TvmContext *context);
+void   tvm_notify        (const gchar *icon,
+                          const gchar *summary,
+                          const gchar *message);
+
+gchar *tvm_notify_decode (const gchar *str);
 
 G_END_DECLS
 
-#endif /* !__TVM_INPUT_DEVICE_H__ */
+#endif /* !__TVM_NOTIFY_H__ */
+
diff --git a/thunar-volman/tvm-usb-device.c b/thunar-volman/tvm-usb-device.c
index 02b1529..3e99f27 100644
--- a/thunar-volman/tvm-usb-device.c
+++ b/thunar-volman/tvm-usb-device.c
@@ -30,12 +30,16 @@
 #include <thunar-volman/tvm-device.h>
 #include <thunar-volman/tvm-run.h>
 #include <thunar-volman/tvm-usb-device.h>
+#include <thunar-volman/tvm-notify.h>
 
 
 
 void
 tvm_usb_device_added (TvmContext *context)
 {
+  const gchar *icon;
+  const gchar *summary;
+  const gchar *message;
   const gchar *driver;
   const gchar *enabled_property = NULL;
   const gchar *command_property = NULL;
@@ -57,6 +61,10 @@ tvm_usb_device_added (TvmContext *context)
 
       enabled_property = "/autophoto/enabled";
       command_property = "/autophoto/command";
+
+      icon = "camera-photo";
+      summary = _("Camera detected");
+      message = _("A photo camera was detected");
     }
   else if (g_strcmp0 (driver, "usblp") == 0)
     {
@@ -64,6 +72,10 @@ tvm_usb_device_added (TvmContext *context)
 
       enabled_property = "/autoprinter/enabled";
       command_property = "/autoprinter/command";
+
+      icon = "printer";
+      summary = _("Printer detected");
+      message = _("A USB printer was detected");
     }
 
   /* check if we have a device that we support */
@@ -73,6 +85,9 @@ tvm_usb_device_added (TvmContext *context)
       enabled = xfconf_channel_get_bool (context->channel, enabled_property, FALSE);
       if (enabled)
         {
+          /* display a detection notification */
+          tvm_notify (icon, summary, message);
+
           /* fetch the command for the input device type and try to run it */
           command = xfconf_channel_get_string (context->channel, command_property, NULL);
           if (command != NULL && *command != '\0')



More information about the Xfce4-commits mailing list