[kupfer] plugin.common: Allow browsing and restoring from Trash



commit 6dc626f7d25e252334a3c2479de4491a6cd019f6
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Sat Aug 22 19:39:08 2009 +0200

    plugin.common: Allow browsing and restoring from Trash

 kupfer/plugin/common.py |   83 +++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 80 insertions(+), 3 deletions(-)
---
diff --git a/kupfer/plugin/common.py b/kupfer/plugin/common.py
index d035642..64c6edc 100644
--- a/kupfer/plugin/common.py
+++ b/kupfer/plugin/common.py
@@ -1,5 +1,6 @@
 import gtk
 import gio
+import gobject
 
 from kupfer.objects import Leaf, Action, Source, RunnableLeaf
 from kupfer import objects, utils, icons, pretty
@@ -86,13 +87,89 @@ class SpecialLocation (objects.Leaf):
 	def get_icon_name(self):
 		return "folder"
 
+TRASH_URI="trash://"
+
+class RestoreTrashedFile (Action):
+	def __init__(self):
+		Action.__init__(self, _("Restore"))
+
+	def activate(self, leaf):
+		orig_path = leaf.get_orig_path()
+		if not orig_path:
+			return
+		orig_gfile = gio.File(orig_path)
+		cur_gfile = leaf.get_gfile()
+		if orig_gfile.query_exists():
+			raise IOError("Target file exists at %s" % orig_gfile.get_path())
+		pretty.print_debug(__name__, "Move %s to %s" % (cur_gfile, orig_gfile))
+		ret = cur_gfile.move(orig_gfile)
+		pretty.print_debug(__name__, "Move ret=%s" % (ret, ))
+
+	def get_description(self):
+		return _("Move file back to original location")
+	def get_icon_name(self):
+		return "gtk-undo-ltr"
+
+class TrashFile (Leaf):
+	"""A file in the trash. Represented object is a file info object"""
+	def __init__(self, info):
+		name = info.get_display_name()
+		Leaf.__init__(self, info, name)
+	def get_actions(self):
+		if self.get_orig_path():
+			yield RestoreTrashedFile()
+	def get_gfile(self):
+		cur_gfile = gio.File(TRASH_URI).get_child(self.object.get_name())
+		return cur_gfile
+	def get_orig_path(self):
+		try:
+			orig_path = self.object.get_attribute_byte_string("trash::orig-path")
+			return orig_path
+		except AttributeError:
+			pass
+		return None
+
+	def is_valid(self):
+		return self.get_gfile().query_exists()
+
+	def get_description(self):
+		orig_path = self.get_orig_path()
+		return utils.get_display_path_for_bytestring(orig_path) if orig_path \
+				else None
+	def get_gicon(self):
+		return self.object.get_icon()
+	def get_icon_name(self):
+		return "gtk-file"
+
+class TrashContentSource (Source):
+	def is_dynamic(self):
+		return True
+	def get_items(self):
+		gfile = gio.File(TRASH_URI)
+		enumerator = gfile.enumerate_children("standard::*,trash::*")
+		for info in enumerator:
+			yield TrashFile(info)
+	def should_sort_lexically(self):
+		return True
+	def get_gicon(self):
+		return icons.get_gicon_for_file(TRASH_URI)
+
 class Trash (SpecialLocation):
 	def __init__(self, name=None):
-		SpecialLocation.__init__(self, "trash://", name=name)
-	def get_description(self):
+		SpecialLocation.__init__(self, TRASH_URI, name=name)
+
+	def has_content(self):
+		return self.get_item_count()
+	def content_source(self, alternate=False):
+		return TrashContentSource(name=unicode(self))
+
+	def get_item_count(self):
 		gfile = gio.File(self.object)
 		info = gfile.query_info(gio.FILE_ATTRIBUTE_TRASH_ITEM_COUNT)
-		item_count = info.get_attribute_uint32(gio.FILE_ATTRIBUTE_TRASH_ITEM_COUNT)
+		return info.get_attribute_uint32(gio.FILE_ATTRIBUTE_TRASH_ITEM_COUNT)
+
+	def get_description(self):
+		item_count = self.get_item_count()
 		if not item_count:
 			return _("Trash is empty")
 		# proper translation of plural



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