[kupfer: 5/41] clipboard: Move Selected Text object into the clipboard plugin



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

    clipboard: Move Selected Text object into the clipboard plugin
    
    This way the user can disable Selected Text if absolutely needed. It is
    also more logical for us to poll the clipboard(s) in only one place.

 kupfer/plugin/clipboard.py      |   74 +++++++++++++++++++++++++++-----------
 kupfer/plugin/core/__init__.py  |    3 +-
 kupfer/plugin/core/selection.py |   55 -----------------------------
 3 files changed, 53 insertions(+), 79 deletions(-)
---
diff --git a/kupfer/plugin/clipboard.py b/kupfer/plugin/clipboard.py
index 15d2e0b..f409de1 100644
--- a/kupfer/plugin/clipboard.py
+++ b/kupfer/plugin/clipboard.py
@@ -15,6 +15,7 @@ from kupfer.objects import FileLeaf
 from kupfer.obj.compose import MultipleLeaf
 from kupfer import plugin_support
 from kupfer.weaklib import gobject_connect_weakly
+from kupfer import kupferstring
 
 
 __kupfer_settings__ = plugin_support.PluginSettings(
@@ -40,9 +41,17 @@ __kupfer_settings__ = plugin_support.PluginSettings(
 
 URI_TARGET="text/uri-list"
 
+class SelectedText (TextLeaf):
+	qf_id = "selectedtext"
+	def __init__(self, text):
+		TextLeaf.__init__(self, text, _('Selected Text'))
+
+	def __repr__(self):
+		return "<%s %s>" % (__name__, self.qf_id)
+
 class ClipboardText (TextLeaf):
 	def get_description(self):
-		numlines = max(self.object.count("\n"), 1)
+		numlines = self.object.count("\n") + 1
 		desc = self.get_first_text_line(self.object)
 
 		return ngettext('Clipboard "%(desc)s"',
@@ -54,6 +63,9 @@ class CurrentClipboardText (ClipboardText):
 	def __init__(self, text):
 		ClipboardText.__init__(self, text, _('Clipboard Text'))
 
+	def __repr__(self):
+		return "<%s %s>" % (__name__, self.qf_id)
+
 class CurrentClipboardFile (FileLeaf):
 	"represents the *unique* current clipboard file"
 	qf_id = "clipboardfile"
@@ -107,45 +119,63 @@ class ClipboardSource (Source):
 		gobject_connect_weakly(clip, "owner-change", self._clipboard_changed)
 		self.clipboard_uris = []
 		self.clipboard_text = None
+		self.selected_text = None
 
 	def finalize(self):
 		self.clipboard_uris = []
 		self.clipboard_text = None
+		self.selected_text = None
 		self.mark_for_update()
 
 	def _clipboard_changed(self, clip, event, *args):
 		is_selection = (event.selection == gtk.gdk.SELECTION_PRIMARY)
-		if is_selection and not __kupfer_settings__["use_selection"]:
-			return
 
 		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
-
-		if newtext in self.clipboards:
-			self.clipboards.remove(newtext)
+		# receive clipboard as gtk text
+		newtext = kupferstring.tounicode(clip.wait_for_text())
+
+		is_valid = bool(newtext and newtext.strip())
+		is_sync_selection = (is_selection and
+		                     __kupfer_settings__["sync_selection"])
+
+		if not is_selection or __kupfer_settings__["use_selection"]:
+			if is_valid:
+				self._add_to_history(newtext, is_selection)
+
+		if is_sync_selection and is_valid:
+			gtk.clipboard_get(gtk.gdk.SELECTION_CLIPBOARD).set_text(newtext)
+
+		if is_selection:
+			self.selected_text = newtext
+		if not is_selection or is_sync_selection:
+			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 = []
+		self._prune_to_length(max_len)
+		self.mark_for_update()
+
+	def _add_to_history(self, cliptext, is_selection):
+		if cliptext in self.clipboards:
+			self.clipboards.remove(cliptext)
 		# if the previous text is a prefix of the new selection, supercede it
 		if (is_selection and self.clipboards
-				and (newtext.startswith(self.clipboards[-1])
-				or newtext.endswith(self.clipboards[-1]))):
+				and (cliptext.startswith(self.clipboards[-1])
+				or cliptext.endswith(self.clipboards[-1]))):
 			self.clipboards.pop()
-		self.clipboards.append(newtext)
-
-		if is_selection and __kupfer_settings__["sync_selection"]:
-			gtk.clipboard_get(gtk.gdk.SELECTION_CLIPBOARD).set_text(newtext)
+		self.clipboards.append(cliptext)
 
+	def _prune_to_length(self, max_len):
 		while len(self.clipboards) > max_len:
 			self.clipboards.popleft()
-		self.mark_for_update()
 
 	def get_items(self):
+		# selected text
+		if self.selected_text:
+			yield SelectedText(self.selected_text)
+
 		# produce the current clipboard files if any
 		paths = filter(None, 
 		        [gio.File(uri=uri).get_path() for uri in self.clipboard_uris])
diff --git a/kupfer/plugin/core/__init__.py b/kupfer/plugin/core/__init__.py
index d6895df..5b9f9d3 100644
--- a/kupfer/plugin/core/__init__.py
+++ b/kupfer/plugin/core/__init__.py
@@ -38,10 +38,9 @@ def register_subplugin(module):
 		globals()[attr] += object_names
 		globals().update((sym, getattr(module, sym)) for sym in object_names)
 
-from kupfer.plugin.core import contents, selection, text, internal, commands
+from kupfer.plugin.core import contents, text, internal, commands
 
 register_subplugin(contents)
-register_subplugin(selection)
 register_subplugin(text)
 register_subplugin(internal)
 register_subplugin(commands)



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