[kupfer: 3/41] clipfiles: Allow copying single files to the clipboard



commit 6dfb01a37b6ad42ad24dd017d09f5ec513a4bb43
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Tue Apr 26 18:40:30 2011 +0200

    clipfiles: Allow copying single files to the clipboard
    
    Allow the Copy action (And Ctrl+C or Ctrl+X) to put files on the
    clipboard. This does not yet handle multiple objects.

 kupfer/interface.py   |   59 ++++++++++++++++++++++++++++++++++++++++++++++--
 kupfer/obj/objects.py |    4 +++
 2 files changed, 60 insertions(+), 3 deletions(-)
---
diff --git a/kupfer/interface.py b/kupfer/interface.py
index 5db72ef..382e073 100644
--- a/kupfer/interface.py
+++ b/kupfer/interface.py
@@ -1,3 +1,4 @@
+import gtk
 
 class TextRepresentation (object):
 	"""
@@ -8,6 +9,18 @@ class TextRepresentation (object):
 		"""The default implementation returns the represented object"""
 		return self.object
 
+class UriListRepresentation (object):
+	"""
+	Kupfer Objects that implement this interface have a uri-list
+	representation that can be used for Copy & Paste etc
+
+	get_urilist_representation should return a sequence of bytestring
+	URIs.
+	"""
+	def get_urilist_representation(self):
+		"""The default implementation raises notimplementederror """
+		raise NotImplementedError
+
 def get_text_representation(obj):
 	try:
 		return obj.get_text_representation()
@@ -20,11 +33,51 @@ def copy_to_clipboard(obj, clipboard):
 
 	Return True if successful
 	"""
+	## support copying text to clipboard
+	## as well as files in both the general uri-list representation
+	## and in nautilus' file copy clipboard type
+	target_ids = (uri_id, text_id, nautilus_id) = (80, 81, 82)
+	nautilus_target = 'x-special/gnome-copied-files'
+
+	# udata is the data dict
+	def store(clipboard, sdata, info, udata):
+		if info == uri_id:
+			sdata.set_uris(udata[uri_id])
+		if info == text_id:
+			sdata.set_text(udata[text_id])
+		if info == nautilus_id:
+			str_data_format = 8
+			sdata.set(nautilus_target, str_data_format, udata[nautilus_id])
+	def clear(clipboard, udata):
+		pass
+
+	targets = []
+	data = {}
 	try:
-		clipboard.set_text(obj.get_text_representation())
-		return True
+		urilist = obj.get_urilist_representation()
+	except AttributeError:
+		pass
+	else:
+		if urilist:
+			targets = gtk.target_list_add_uri_targets(targets, uri_id)
+			targets.append((nautilus_target, 0, nautilus_id))
+			data[uri_id] = urilist
+			data[nautilus_id] = 'copy\n' + '\n'.join(urilist)
+
+	try:
+		text = obj.get_text_representation()
 	except AttributeError:
-		return False
+		pass
+	else:
+		targets = gtk.target_list_add_text_targets(targets, text_id)
+		data[text_id] = text
+	if data:
+		clipboard.set_with_data(targets, store, clear, data)
+		# store all targets
+		clipboard.set_can_store(None)
+		clipboard.store()
+		return True
+	return False
 
 def get_fileleaf_for_path(pth):
 	import kupfer.objects
diff --git a/kupfer/obj/objects.py b/kupfer/obj/objects.py
index fe78033..cb0df9d 100644
--- a/kupfer/obj/objects.py
+++ b/kupfer/obj/objects.py
@@ -11,6 +11,7 @@ see the main program file, and COPYING for details.
 import os
 from os import path
 
+import gio
 import gobject
 
 from kupfer import icons, launch, utils
@@ -96,6 +97,9 @@ class FileLeaf (Leaf, TextRepresentation):
 	def get_text_representation(self):
 		return gobject.filename_display_name(self.object)
 
+	def get_urilist_representation(self):
+		return [gio.File(path=self.object).get_uri()]
+
 	def get_description(self):
 		return utils.get_display_path_for_bytestring(self.canonical_path())
 



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