[kupfer: 1/41] clipfiles: Implement Clipboard File proxy object (And Clipboard Text)



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

    clipfiles: Implement Clipboard File proxy object (And Clipboard Text)
    
    Provide a proxy object for the files that are (possibly) on the current
    clipboard.
    
    We want integration with file managers everywhere, and they all support
    copying files to the clipboard--this method lets you focus them easily
    in kupfer.
    
    We also implement the current clipboard text as a proxy object as the
    same time.

 kupfer/plugin/clipboard.py |   59 +++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 58 insertions(+), 1 deletions(-)
---
diff --git a/kupfer/plugin/clipboard.py b/kupfer/plugin/clipboard.py
index 8943b7c..5f15a2a 100644
--- a/kupfer/plugin/clipboard.py
+++ b/kupfer/plugin/clipboard.py
@@ -7,9 +7,12 @@ __author__ = "Ulrik Sverdrup <ulrik sverdrup gmail com>"
 
 from collections import deque
 
+import gio
 import gtk
 
 from kupfer.objects import Source, TextLeaf, Action, SourceLeaf
+from kupfer.objects import FileLeaf
+from kupfer.obj.compose import MultipleLeaf
 from kupfer import plugin_support
 from kupfer.weaklib import gobject_connect_weakly
 
@@ -35,15 +38,42 @@ __kupfer_settings__ = plugin_support.PluginSettings(
 	},
 )
 
+URI_TARGET="text/uri-list"
+
 class ClipboardText (TextLeaf):
 	def get_description(self):
 		numlines = max(self.object.count("\n"), 1)
-		desc = unicode(self)
+		desc = self.get_first_text_line(self.object)
 
 		return ngettext('Clipboard "%(desc)s"',
 			'Clipboard with %(num)d lines "%(desc)s"',
 			numlines) % {"num": numlines, "desc": desc }
 
+class CurrentClipboardText (ClipboardText):
+	qf_id = "clipboardtext"
+	def __init__(self, text):
+		ClipboardText.__init__(self, text, _('Clipboard Text'))
+
+class CurrentClipboardFile (FileLeaf):
+	"represents the *unique* current clipboard file"
+	qf_id = "clipboardfile"
+	def __init__(self, filepath):
+		"""@filepath is a filesystem byte string `str`"""
+		FileLeaf.__init__(self, filepath, _('Clipboard File'))
+
+	def __repr__(self):
+		return "<%s %s>" % (__name__, self.qf_id)
+
+class CurrentClipboardFiles (MultipleLeaf):
+	"represents the *unique* current clipboard if there are many files"
+	qf_id = "clipboardfile"
+	def __init__(self, paths):
+		files = [FileLeaf(path) for path in paths]
+		MultipleLeaf.__init__(self, files, _("Clipboard Files"))
+
+	def __repr__(self):
+		return "<%s %s>" % (__name__, self.qf_id)
+
 
 class ClearClipboards(Action):
 	def __init__(self):
@@ -75,6 +105,13 @@ class ClipboardSource (Source):
 		gobject_connect_weakly(clip, "owner-change", self._clipboard_changed)
 		clip = gtk.clipboard_get(gtk.gdk.SELECTION_PRIMARY)
 		gobject_connect_weakly(clip, "owner-change", self._clipboard_changed)
+		self.clipboard_uris = []
+		self.clipboard_text = None
+
+	def finalize(self):
+		self.clipboard_uris = []
+		self.clipboard_text = None
+		self.mark_for_update()
 
 	def _clipboard_changed(self, clip, event, *args):
 		is_selection = (event.selection == gtk.gdk.SELECTION_PRIMARY)
@@ -83,6 +120,12 @@ class ClipboardSource (Source):
 
 		max_len = __kupfer_settings__["max"]
 		newtext = clip.wait_for_text()
+		self.clipboard_text = newtext
+		if clip.wait_is_target_available(URI_TARGET):
+			sdata = clip.wait_for_contents(URI_TARGET)
+			self.clipboard_uris = list(sdata.get_uris())
+		else:
+			self.clipboard_uris = []
 		if not (newtext and newtext.strip()):
 			return
 
@@ -103,6 +146,18 @@ class ClipboardSource (Source):
 		self.mark_for_update()
 
 	def get_items(self):
+		# produce the current clipboard files if any
+		paths = filter(None, 
+		        [gio.File(uri=uri).get_path() for uri in self.clipboard_uris])
+		if len(paths) == 1:
+			yield CurrentClipboardFile(paths[0])
+		if len(paths) > 1:
+			yield CurrentClipboardFiles(paths)
+
+		# put out the current clipboard text
+		if self.clipboard_text:
+			yield CurrentClipboardText(self.clipboard_text)
+		# put out the clipboard history
 		for t in reversed(self.clipboards):
 			yield ClipboardText(t)
 
@@ -114,6 +169,8 @@ class ClipboardSource (Source):
 
 	def provides(self):
 		yield TextLeaf
+		yield FileLeaf
+		yield MultipleLeaf
 
 	def clear(self):
 		self.clipboards.clear()



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