[Xfce4-commits] <xfdesktop:master> Add support for mount/unmount/eject notifications like in Thunar.

Jannis Pohlmann noreply at xfce.org
Tue Nov 2 01:14:56 CET 2010


Updating branch refs/heads/master
         to 7c698f0f72de273f6cbc707b9cff4bf0f15a256d (commit)
       from 9553e7af647dbf84da20bca5bf63a6d640575c91 (commit)

commit 7c698f0f72de273f6cbc707b9cff4bf0f15a256d
Author: Jannis Pohlmann <jannis at xfce.org>
Date:   Tue Nov 2 01:00:09 2010 +0100

    Add support for mount/unmount/eject notifications like in Thunar.
    
    I'd prefer to have this in a central location due to the duplicate
    strings in Thunar and xfdesktop. But the current way is much cleaner
    than the previous spwan-wait-and-kill mechanism combined with
    exo-eject.

 configure.ac.in             |   15 ++-
 src/Makefile.am             |    9 ++
 src/xfdesktop-notify.c      |  257 +++++++++++++++++++++++++++++++++++++++++++
 src/xfdesktop-notify.h      |   38 +++++++
 src/xfdesktop-volume-icon.c |   16 +--
 5 files changed, 322 insertions(+), 13 deletions(-)

diff --git a/configure.ac.in b/configure.ac.in
index 8b42948..f45361a 100644
--- a/configure.ac.in
+++ b/configure.ac.in
@@ -177,6 +177,12 @@ AC_ARG_WITH([file-manager-fallback],
 AC_DEFINE_UNQUOTED([FILE_MANAGER_FALLBACK], ["$ac_cv_file_manager_fallback"],
     [Set to the file manager to use as a fallback])
 
+dnl **************************************
+dnl *** Optional support for libnotify ***
+dnl **************************************
+XDT_CHECK_OPTIONAL_PACKAGE([LIBNOTIFY], [libnotify], [0.4.0], [notifications],
+                           [Mount notification support], [yes])
+
 dnl ***************************************************
 dnl *** Check if we need to build the documentation ***
 dnl ***************************************************
@@ -233,7 +239,12 @@ echo "* Documentation:                                yes (in tarball)"
 else
 echo "* Documentation:                                no"
 fi
-echo "  Build desktop menu module:                    $build_desktop_menu"
-echo "  Build support for desktop icons:              $enable_desktop_icons"
+echo "* Build desktop menu module:                    $build_desktop_menu"
+echo "* Build support for desktop icons:              $enable_desktop_icons"
 echo "      Include support for file/launcher icons:  $enable_file_icons"
+if test x"$LIBNOTIFY_FOUND" = x"yes"; then
+echo "* Mount notification support:                   yes"
+else
+echo "* Mount notification support:                   no"
+fi
 echo
diff --git a/src/Makefile.am b/src/Makefile.am
index be5ffce..f7c992e 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -8,8 +8,15 @@ xfdesktop_built_sources = \
 	xfdesktop-marshal.c \
 	xfdesktop-marshal.h
 
+if HAVE_LIBNOTIFY
+xfdesktop_notify_sources = \
+	xfdesktop-notify.c \
+	xfdesktop-notify.h
+endif
+
 xfdesktop_SOURCES = \
 	$(xfdesktop_built_sources) \
+	$(xfdesktop_notify_sources) \
 	main.c \
 	menu.c \
 	menu.h \
@@ -67,6 +74,7 @@ xfdesktop_CFLAGS = \
         $(GLIB_CFLAGS) \
 	$(GTHREAD_CFLAGS) \
         $(GTK_CFLAGS) \
+	$(LIBNOTIFY_CFLAGS) \
 	$(LIBX11_CFLAGS) \
 	$(LIBXFCE4UTIL_CFLAGS) \
 	$(LIBXFCE4UI_CFLAGS) \
@@ -84,6 +92,7 @@ xfdesktop_LDADD += \
         $(GLIB_LIBS) \
 	$(GTHREAD_LIBS) \
         $(GTK_LIBS) \
+	$(LIBNOTIFY_LIBS) \
 	$(LIBX11_LDFLAGS) \
 	$(LIBX11_LIBS) \
 	$(LIBXFCE4UTIL_LIBS) \
diff --git a/src/xfdesktop-notify.c b/src/xfdesktop-notify.c
new file mode 100644
index 0000000..d1386e1
--- /dev/null
+++ b/src/xfdesktop-notify.c
@@ -0,0 +1,257 @@
+/* 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.
+ *
+ * NOTE: THIS FILE WAS COPIED FROM THUNAR. FUNCTION PREFIXES WERE
+ * ALIGNED TO XFDESKTOP AND A FEW TRANSLATOR HINTS WERE ADDED.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <glib.h>
+#include <gio/gio.h>
+
+#include <libnotify/notify.h>
+
+#include <libxfce4util/libxfce4util.h>
+
+#include "xfdesktop-notify.h"
+
+
+
+void
+xfdesktop_notify_unmount (GMount *mount)
+{
+  const gchar * const *icon_names;
+  NotifyNotification  *notification = NULL;
+  const gchar         *summary;
+  GFileInfo           *info;
+  gboolean             read_only = FALSE;
+  GFile               *icon_file;
+  GFile               *mount_point;
+  GIcon               *icon;
+  gchar               *icon_name = NULL;
+  gchar               *message;
+  gchar               *name;
+
+  g_return_if_fail (G_IS_MOUNT (mount));
+
+  mount_point = g_mount_get_root (mount);
+  
+  info = g_file_query_info (mount_point, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE, 
+                            G_FILE_QUERY_INFO_NONE, NULL, NULL);
+
+  if (info != NULL)
+    {
+      read_only = !g_file_info_get_attribute_boolean (info, 
+                                                      G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE);
+
+      g_object_unref (info);
+    }
+
+  g_object_unref (mount_point);
+
+  name = g_mount_get_name (mount);
+
+  icon = g_mount_get_icon (mount);
+  if (G_IS_THEMED_ICON (icon))
+    {
+      icon_names = g_themed_icon_get_names (G_THEMED_ICON (icon));
+      if (icon_names != NULL)
+        icon_name = g_strdup (icon_names[0]);
+    }
+  else if (G_IS_FILE_ICON (icon))
+    {
+      icon_file = g_file_icon_get_file (G_FILE_ICON (icon));
+      if (icon_file != NULL)
+        {
+          icon_name = g_file_get_path (icon_file);
+          g_object_unref (icon_file);
+        }
+    }
+  g_object_unref (icon);
+
+  if (icon_name == NULL)
+    icon_name = g_strdup ("drive-removable-media");
+
+  if (read_only)
+    {
+      /* TRANSLATORS: Please use the same translation here as in Thunar */
+      summary = _("Unmounting device");
+
+      /* TRANSLATORS: Please use the same translation here as in Thunar */
+      message = g_strdup_printf (_("The device \"%s\" is being unmounted by the system. "
+                                   "Please do not remove the media or disconnect the "
+                                   "drive"), name);
+    }
+  else
+    {
+      /* TRANSLATORS: Please use the same translation here as in Thunar */
+      summary = _("Writing data to device");
+
+      /* TRANSLATORS: Please use the same translation here as in Thunar */
+      message = g_strdup_printf (_("There is data that needs to be written to the "
+                                   "device \"%s\" before it can be removed. Please "
+                                   "do not remove the media or disconnect the drive"),
+                                   name);
+    }
+
+  notification = notify_notification_new (summary, message, icon_name, NULL);
+  notify_notification_set_urgency (notification, NOTIFY_URGENCY_CRITICAL);
+  notify_notification_set_timeout (notification, NOTIFY_EXPIRES_NEVER);
+  notify_notification_show (notification, NULL);
+
+  g_object_set_data_full (G_OBJECT (mount), "xfdesktop-notification", notification, 
+                          g_object_unref);
+
+  g_free (message);
+  g_free (icon_name);
+  g_free (name);
+}
+
+
+
+void
+xfdesktop_notify_unmount_finish (GMount *mount)
+{
+  NotifyNotification *notification;
+
+  g_return_if_fail (G_IS_MOUNT (mount));
+
+  notification = g_object_get_data (G_OBJECT (mount), "xfdesktop-notification");
+  if (notification != NULL)
+    {
+      notify_notification_close (notification, NULL);
+      g_object_set_data (G_OBJECT (mount), "xfdesktop-notification", NULL);
+    }
+}
+
+
+
+void
+xfdesktop_notify_eject (GVolume *volume)
+{
+  const gchar * const *icon_names;
+  NotifyNotification  *notification = NULL;
+  const gchar         *summary;
+  GFileInfo           *info;
+  gboolean             read_only = FALSE;
+  GMount              *mount;
+  GFile               *icon_file;
+  GFile               *mount_point;
+  GIcon               *icon;
+  gchar               *icon_name = NULL;
+  gchar               *message;
+  gchar               *name;
+
+  g_return_if_fail (G_IS_VOLUME (volume));
+
+  mount = g_volume_get_mount (volume);
+  if (mount != NULL)
+    {
+      mount_point = g_mount_get_root (mount);
+      
+      info = g_file_query_info (mount_point, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE, 
+                                G_FILE_QUERY_INFO_NONE, NULL, NULL);
+
+      if (info != NULL)
+        {
+          read_only =
+            !g_file_info_get_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE);
+
+          g_object_unref (info);
+        }
+
+      g_object_unref (mount_point);
+    }
+
+  name = g_volume_get_name (volume);
+
+  icon = g_volume_get_icon (volume);
+  if (G_IS_THEMED_ICON (icon))
+    {
+      icon_names = g_themed_icon_get_names (G_THEMED_ICON (icon));
+      if (icon_names != NULL)
+        icon_name = g_strdup (icon_names[0]);
+    }
+  else if (G_IS_FILE_ICON (icon))
+    {
+      icon_file = g_file_icon_get_file (G_FILE_ICON (icon));
+      if (icon_file != NULL)
+        {
+          icon_name = g_file_get_path (icon_file);
+          g_object_unref (icon_file);
+        }
+    }
+  g_object_unref (icon);
+
+  if (icon_name == NULL)
+    icon_name = g_strdup ("drive-removable-media");
+
+  if (read_only)
+    {
+      /* TRANSLATORS: Please use the same translation here as in Thunar */
+      summary = _("Ejecting device");
+
+      /* TRANSLATORS: Please use the same translation here as in Thunar */
+      message = g_strdup_printf (_("The device \"%s\" is being ejected. "
+                                   "This may take some time"), name);
+    }
+  else
+    {
+      /* TRANSLATORS: Please use the same translation here as in Thunar */
+      summary = _("Writing data to device");
+
+      /* TRANSLATORS: Please use the same translation here as in Thunar */
+      message = g_strdup_printf (_("There is data that needs to be written to the "
+                                   "device \"%s\" before it can be removed. Please "
+                                   "do not remove the media or disconnect the drive"),
+                                   name);
+    }
+
+  notification = notify_notification_new (summary, message, icon_name, NULL);
+  notify_notification_set_urgency (notification, NOTIFY_URGENCY_CRITICAL);
+  notify_notification_set_timeout (notification, NOTIFY_EXPIRES_NEVER);
+  notify_notification_show (notification, NULL);
+
+  g_object_set_data_full (G_OBJECT (volume), "xfdesktop-notification", notification, 
+                          g_object_unref);
+
+  g_free (message);
+  g_free (icon_name);
+  g_free (name);
+}
+
+
+
+void
+xfdesktop_notify_eject_finish (GVolume *volume)
+{
+  NotifyNotification *notification;
+
+  g_return_if_fail (G_IS_VOLUME (volume));
+
+  notification = g_object_get_data (G_OBJECT (volume), "xfdesktop-notification");
+  if (notification != NULL)
+    {
+      notify_notification_close (notification, NULL);
+      g_object_set_data (G_OBJECT (volume), "xfdesktop-notification", NULL);
+    }
+}
diff --git a/src/xfdesktop-notify.h b/src/xfdesktop-notify.h
new file mode 100644
index 0000000..5361290
--- /dev/null
+++ b/src/xfdesktop-notify.h
@@ -0,0 +1,38 @@
+/* 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.
+ */
+
+#ifndef __XFDESKTOP_NOTIFY_H__
+#define __XFDESKTOP_NOTIFY_H__
+
+#include <glib.h>
+#include <gio/gio.h>
+
+#include <libnotify/notify.h>
+
+G_BEGIN_DECLS
+
+void xfdesktop_notify_unmount        (GMount  *mount);
+void xfdesktop_notify_unmount_finish (GMount  *mount);
+void xfdesktop_notify_eject          (GVolume *volume);
+void xfdesktop_notify_eject_finish   (GVolume *volume);
+
+G_END_DECLS
+
+#endif /* !__XFDESKTOP_NOTIFY_H__ */
diff --git a/src/xfdesktop-volume-icon.c b/src/xfdesktop-volume-icon.c
index 6f0d147..85a44dc 100644
--- a/src/xfdesktop-volume-icon.c
+++ b/src/xfdesktop-volume-icon.c
@@ -48,6 +48,10 @@
 #include <thunarx/thunarx.h>
 #endif
 
+#ifdef HAVE_LIBNOTIFY
+#include "xfdesktop-notify.h"
+#endif
+
 #include "xfdesktop-common.h"
 #include "xfdesktop-file-utils.h"
 #include "xfdesktop-volume-icon.h"
@@ -456,9 +460,7 @@ xfdesktop_volume_icon_eject_finish(GObject *object,
     }
 
 #ifdef HAVE_LIBNOTIFY
-#if 0
-    xfdesktop_notify_eject_finish(mount);
-#endif
+    xfdesktop_notify_eject_finish(volume);
 #endif
 
     g_object_unref(icon);
@@ -500,10 +502,8 @@ xfdesktop_volume_icon_unmount_finish(GObject *object,
     }
 
 #ifdef HAVE_LIBNOTIFY
-#if 0
     xfdesktop_notify_unmount_finish(mount);
 #endif
-#endif
 
     g_object_unref(icon);
 }
@@ -595,22 +595,16 @@ xfdesktop_volume_icon_menu_toggle_mount(GtkWidget *widget,
     if(mount) {
         if(g_volume_can_eject(volume)) {
 #ifdef HAVE_LIBNOTIFY
-#if 0
-            /* TODO */
             xfdesktop_notify_eject(volume);
 #endif
-#endif
 
             g_volume_eject(volume, G_MOUNT_UNMOUNT_NONE, NULL,
                            xfdesktop_volume_icon_eject_finish,
                            g_object_ref(icon));
         } else {
 #ifdef HAVE_LIBNOTIFY
-#if 0
-            /* TODO */
             xfdesktop_notify_unmount(mount);
 #endif
-#endif
 
             g_mount_unmount(mount, G_MOUNT_UNMOUNT_NONE, NULL,
                             xfdesktop_volume_icon_unmount_finish, 



More information about the Xfce4-commits mailing list