[kupfer] kupfer_provider: Add SelectionChangedString signal



commit 8700b0ad570d0949f5a803cd44387420780a5e5c
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Sun Aug 30 00:32:20 2009 +0200

    kupfer_provider: Add SelectionChangedString signal
    
    Since the SelectionChanged signal is the only correct method, but
    other users of this plugin might find it easiert to work with a plain
    string representation, we add a String method as well; this will let
    the locale module guess the proper encoding and it will send along all
    filenames that can be decoded with the guessed encoding, and skip the
    rest.
    
    On a typical system, that means all proper UTF-8 filenames are passed
    along, but other invalid filenames are not (That we get and can use
    with SelectionChanged)

 extras/kupfer_provider.py |   20 ++++++++++++++++++++
 1 files changed, 20 insertions(+), 0 deletions(-)
---
diff --git a/extras/kupfer_provider.py b/extras/kupfer_provider.py
index 80ad2d7..ad1088a 100644
--- a/extras/kupfer_provider.py
+++ b/extras/kupfer_provider.py
@@ -21,6 +21,8 @@ updated with all file selections in Nautilus, and broadcast them over a D-Bus
 signal.
 """
 
+import locale
+
 import dbus
 import dbus.glib
 from dbus.gobject_service import ExportedGObject
@@ -41,6 +43,14 @@ class Object (ExportedGObject):
 		"""
 		return paths
 
+	@dbus.service.signal(interface_name, signature="as")
+	def SelectionChangedStrings(self, paths):
+		"""Nautilus selection changed. Passes an array of strings;
+		We guess the filesystem encoding, and any files that cannot be
+		decoded are skipped
+		"""
+		return paths
+
 class KupferSelectionProvider(nautilus.MenuProvider):
 	def __init__(self):
 		selfname = type(self).__name__
@@ -66,8 +76,18 @@ class KupferSelectionProvider(nautilus.MenuProvider):
 		# - All selected files are locals
 		uris = [f.get_uri() for f in files]
 		paths = filter(None, (gio.File(u).get_path() for u in uris))
+		# try to decode filesystem strings
+		ustrs = []
+		encoding = locale.getpreferredencoding()
+		for p in paths:
+			try:
+				pdec = p.decode(encoding)
+			except UnicodeDecodeError:
+				continue
+			ustrs.append(pdec)
 		if paths != self.cur_selection and self.service:
 			self.service.SelectionChanged(paths)
+			self.service.SelectionChangedStrings(ustrs)
 		self.cur_selection = paths
 		return
 



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