[kupfer] Add TextRepresentation interface, and make objects Copyable



commit 3b0b657757b0646660a92b50e51f653889218649
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Tue Dec 15 15:12:38 2009 +0100

    Add TextRepresentation interface, and make objects Copyable
    
    Any object adhering to TextRepresentation may be understood as text
    (so far, only copied with the Copy action).

 kupfer/interface.py            |   10 ++++++++++
 kupfer/objects.py              |   10 +++++++---
 kupfer/plugin/clipboard.py     |   13 -------------
 kupfer/plugin/core/__init__.py |   22 ++++++++++++++++++++++
 4 files changed, 39 insertions(+), 16 deletions(-)
---
diff --git a/kupfer/interface.py b/kupfer/interface.py
new file mode 100644
index 0000000..74f5a25
--- /dev/null
+++ b/kupfer/interface.py
@@ -0,0 +1,10 @@
+
+class TextRepresentation (object):
+	"""
+	Kupfer Objects that implement this interface have a plain text
+	representation that can be used for Copy & Paste etc
+	"""
+	def get_text_representation(self):
+		"""The default implementation returns the represented object"""
+		return self.object
+
diff --git a/kupfer/objects.py b/kupfer/objects.py
index 86473f1..4a641b7 100644
--- a/kupfer/objects.py
+++ b/kupfer/objects.py
@@ -20,6 +20,7 @@ from kupfer import icons, launch, utils
 from kupfer import pretty
 from kupfer.utils import locale_sort
 from kupfer.helplib import PicklingHelperMixin, FilesystemWatchMixin
+from kupfer.interface import TextRepresentation
 from kupfer.kupferstring import tounicode, toutf8, tofolded
 
 class Error (Exception):
@@ -184,7 +185,7 @@ class DummyLeaf (Leaf):
 	def __init__(self):
 		super(DummyLeaf, self).__init__(None, _("No matches"))
 
-class FileLeaf (Leaf):
+class FileLeaf (Leaf, TextRepresentation):
 	"""
 	Represents one file
 	"""
@@ -233,6 +234,9 @@ class FileLeaf (Leaf):
 	def is_dir(self):
 		return path.isdir(self.object)
 
+	def get_text_representation(self):
+		return gobject.filename_display_name(self.object)
+
 	def get_description(self):
 		"""Format the path shorter:
 		replace homedir by ~/
@@ -1016,7 +1020,7 @@ class AppLeafContentMixin (object):
 		if leaf == cls.get_leaf_repr():
 			return cls()
 
-class UrlLeaf (Leaf):
+class UrlLeaf (Leaf, TextRepresentation):
 	# slots saves memory since we have lots this Leaf
 	__slots__ = ("name", "object")
 	def __init__(self, obj, name):
@@ -1053,7 +1057,7 @@ class Do (Action):
 	def get_description(self):
 		return _("Perform action")
 
-class TextLeaf (Leaf):
+class TextLeaf (Leaf, TextRepresentation):
 	"""Represent a text query
 	represented object is the unicode string
 	"""
diff --git a/kupfer/plugin/clipboard.py b/kupfer/plugin/clipboard.py
index 434656c..9752f2b 100644
--- a/kupfer/plugin/clipboard.py
+++ b/kupfer/plugin/clipboard.py
@@ -8,7 +8,6 @@ from kupfer.helplib import gobject_connect_weakly
 
 __kupfer_name__ = _("Clipboards")
 __kupfer_sources__ = ("ClipboardSource", )
-__kupfer_actions__ = ("CopyToClipboard", )
 __description__ = _("Recent clipboards")
 __version__ = ""
 __author__ = "Ulrik Sverdrup <ulrik sverdrup gmail com>"
@@ -70,15 +69,3 @@ class ClipboardSource (Source, PicklingHelperMixin):
 	def provides(self):
 		yield TextLeaf
 
-class CopyToClipboard (Action):
-	def __init__(self):
-		Action.__init__(self, _("Copy"))
-	def activate(self, leaf):
-		clip = gtk.clipboard_get(gtk.gdk.SELECTION_CLIPBOARD)
-		clip.set_text(leaf.object)
-	def item_types(self):
-		yield TextLeaf
-	def get_description(self):
-		return _("Copy to clipboard")
-	def get_icon_name(self):
-		return "gtk-copy"
diff --git a/kupfer/plugin/core/__init__.py b/kupfer/plugin/core/__init__.py
index 3da7d25..e01c90e 100644
--- a/kupfer/plugin/core/__init__.py
+++ b/kupfer/plugin/core/__init__.py
@@ -7,6 +7,7 @@ __kupfer_name__ = u"Core"
 __kupfer_sources__ = ()   # Updated later
 __kupfer_actions__ = (    # Updated later
 	"SearchInside",
+	"CopyToClipboard",
 	)
 __description__ = u"Core actions and items"
 __version__ = ""
@@ -47,3 +48,24 @@ class SearchInside (Action):
 		return _("Search inside this catalog")
 	def get_icon_name(self):
 		return "search"
+
+class CopyToClipboard (Action):
+	# rank down since it applies everywhere
+	rank_adjust = -2
+	def __init__(self):
+		Action.__init__(self, _("Copy"))
+	def activate(self, leaf):
+		clip = gtk.clipboard_get(gtk.gdk.SELECTION_CLIPBOARD)
+		clip.set_text(leaf.get_text_representation())
+	def item_types(self):
+		yield Leaf
+	def valid_for_item(self, leaf):
+		try:
+			return bool(leaf.get_text_representation())
+		except AttributeError:
+			pass
+	def get_description(self):
+		return _("Copy to clipboard")
+	def get_icon_name(self):
+		return "gtk-copy"
+



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