[kupfer] Assign icons to saved kupfer command files



commit 8170b50a5cef96361310fdfbf52d67aafc5ac171
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Thu Feb 11 12:22:34 2010 +0100

    Assign icons to saved kupfer command files
    
    Assign icons by taking the Kupfer icon pixbuf and writing as a
    thumbnail for the file. Then we set the metadata property
    metadata::custom-icon to point at the thumbnail file, which makes sure
    the icon stays with the file as it moves around.
    
    To make this work in the long run, we also always check on an executed
    command file if we can refresh its icon.

 kupfer/execfile.py             |   31 +++++++++++++++++++++++++++++++
 kupfer/plugin/core/commands.py |    1 +
 2 files changed, 32 insertions(+), 0 deletions(-)
---
diff --git a/kupfer/execfile.py b/kupfer/execfile.py
index ace546e..2daacf8 100644
--- a/kupfer/execfile.py
+++ b/kupfer/execfile.py
@@ -1,6 +1,10 @@
+import hashlib
 import pickle
 import os
 
+import gio
+import glib
+
 from kupfer import pretty
 from kupfer import puid
 from kupfer import conspickle
@@ -32,6 +36,7 @@ def execute_file(filepath):
 		pretty.print_error(__name__, "Could not read", filepath, err)
 		return
 	command_object.run()
+	glib.idle_add(update_icon, command_object, filepath)
 
 def save_to_file(command_leaf, filename):
 	fd = os.open(filename, os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o777)
@@ -42,6 +47,32 @@ def save_to_file(command_leaf, filename):
 	finally:
 		wfile.close()
 
+def _write_thumbnail(gfile, pixbuf):
+	uri = gfile.get_uri()
+	hashname = hashlib.md5(uri).hexdigest()
+	thumb_dir = os.path.expanduser("~/.thumbnails/normal")
+	if not os.path.exists(thumb_dir):
+		os.makedirs(thumb_dir, 0700)
+	thumb_filename = os.path.join(thumb_dir, hashname + ".png")
+	pixbuf.save(thumb_filename, "png")
+	return thumb_filename
+
+def update_icon(kobj, filepath):
+	"Give @filepath a custom icon taken from @kobj"
+	icon_key = "metadata::custom-icon"
+
+	gfile = gio.File(filepath)
+	finfo = gfile.query_info(icon_key)
+	custom_icon_uri = finfo.get_attribute_string(icon_key)
+	if custom_icon_uri and gio.File(custom_icon_uri).query_exists():
+		return
+	pretty.print_debug(__name__, "Updating icon for", filepath)
+	thumb_filename = _write_thumbnail(gfile, kobj.get_pixbuf(128))
+	if any(N.name == "metadata" for N in gfile.query_writable_namespaces()):
+		gfile.set_attribute_string("metadata::custom-icon",
+				gio.File(thumb_filename).get_uri())
+
+
 if __name__ == '__main__':
 	import doctest
 	doctest.testmod()
diff --git a/kupfer/plugin/core/commands.py b/kupfer/plugin/core/commands.py
index 54990c5..eb0c6cf 100644
--- a/kupfer/plugin/core/commands.py
+++ b/kupfer/plugin/core/commands.py
@@ -16,6 +16,7 @@ class SaveToFile (Action):
 
 	def activate(self, obj, iobj):
 		execfile.save_to_file(obj, iobj.object)
+		execfile.update_icon(obj, iobj.object)
 		return FileLeaf(os.path.abspath(iobj.object))
 
 	def item_types(self):



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