Unity Lens: display thumbnail when available, fix small icons issues

This commit is contained in:
Jean-Francois Dockes 2012-05-15 13:23:17 +02:00
parent e5beb91238
commit dc7f5469fe

View File

@ -2,6 +2,10 @@
import sys import sys
import subprocess import subprocess
import time import time
import urllib
import hashlib
import os
import locale
from gi.repository import GLib, GObject, Gio from gi.repository import GLib, GObject, Gio
from gi.repository import Dee from gi.repository import Dee
@ -11,6 +15,18 @@ import recoll
BUS_PATH = "/org/recoll/unitylensrecoll/scope/main" BUS_PATH = "/org/recoll/unitylensrecoll/scope/main"
# Thumbnails standard
# 256x256
THMBDIRLARGE = "~/.thumbnails/large"
# 128x128
THMBDIRNORMAL = "~/.thumbnails/normal"
# Icon names for some recoll mime types which don't have standard icon by the
# normal method
SPEC_MIME_ICONS = {'application/x-fsdirectory' : 'gnome-fs-directory.svg',
'message/rfc822' : 'mail-read',
'application/x-recoll' : 'recoll'}
# These category ids must match the order in which we add them to the lens # These category ids must match the order in which we add them to the lens
CATEGORY_ALL = 0 CATEGORY_ALL = 0
@ -99,9 +115,7 @@ class Scope (Unity.Scope):
def _on_global_search_changed (self, scope, param_spec): def _on_global_search_changed (self, scope, param_spec):
search = self.get_global_search_string() search = self.get_global_search_string()
results = scope.props.global_results_model results = scope.props.global_results_model
#print "Global search changed to: '%s'" % search #print "Global search changed to: '%s'" % search
self._update_results_model (search, results) self._update_results_model (search, results)
def _update_results_model (self, search_string, model): def _update_results_model (self, search_string, model):
@ -141,6 +155,31 @@ class Scope (Unity.Scope):
GObject.timeout_add(TYPING_TIMEOUT, self._on_timeout, GObject.timeout_add(TYPING_TIMEOUT, self._on_timeout,
search_string, model) search_string, model)
def _get_thumbnail_path(self, url):
"""Look for a thumbnail for the input url, according to the
freedesktop thumbnail storage standard. The input 'url' always
begins with file:// and is unencoded. We encode it properly
and compute the path inside the thumbnail storage
directory. We return the path only if the thumbnail does exist
(no generation performed)"""
path = url
path = path.replace("file://", "", 1)
try:
path = "file://" + urllib.quote(path)
except:
#print "_get_thumbnail_path: quote failed"
return None
#print "_get_thumbnail: encoded path: [%s]" % (path,)
thumbname = hashlib.md5(path).hexdigest() + ".png"
#print "_get_thumbnail: thumbname: [%s]" % (thumbname,)
tpath = os.path.join(os.path.expanduser(THMBDIRNORMAL), thumbname)
if os.path.exists(tpath):
return tpath
tpath = os.path.join(os.path.expanduser(THMBDIRLARGE), thumbname)
if os.path.exists(tpath):
return tpath
return None
def _really_do_search(self, search_string, model): def _really_do_search(self, search_string, model):
#print "really_do_search:", "[" + search_string + "]" #print "really_do_search:", "[" + search_string + "]"
@ -183,12 +222,25 @@ class Scope (Unity.Scope):
# Results with an ipath get a special mime type so that they # Results with an ipath get a special mime type so that they
# get opened by starting a recoll instance. # get opened by starting a recoll instance.
thumbnail = None
if doc.ipath != "": if doc.ipath != "":
mimetype = "application/x-recoll" mimetype = "application/x-recoll"
url = doc.url + "#" + doc.ipath url = doc.url + "#" + doc.ipath
else: else:
mimetype = doc.mimetype mimetype = doc.mimetype
url = doc.url url = doc.url
# doc.url is a unicode string which is badly wrong. A
# future version of the pyrecoll module will have a
# separate method to retrieve the binary
# version. Until this happens, try to encode
# back. This won't work every time (ie: if the
# original path could not be translated to unicode by
# pyrecoll, or if the unicode can't be translated back
# in the current locale)
encoding = locale.nl_langinfo(locale.CODESET)
thumbnail = \
self._get_thumbnail_path(url.encode(encoding,
errors='replace'))
#print "Recoll Lens: Using MIMETYPE", mimetype, " URL", url #print "Recoll Lens: Using MIMETYPE", mimetype, " URL", url
@ -196,9 +248,19 @@ class Scope (Unity.Scope):
if titleorfilename == "": if titleorfilename == "":
titleorfilename = doc.filename titleorfilename = doc.filename
icon = Gio.content_type_get_icon(doc.mimetype) iconname = None
if icon: if thumbnail:
iconname = icon.get_names()[0] iconname = thumbnail
else:
if SPEC_MIME_ICONS.has_key(doc.mimetype):
iconname = SPEC_MIME_ICONS[doc.mimetype]
else:
icon = Gio.content_type_get_icon(doc.mimetype)
if icon:
iconname = icon.get_names()[0]
#print "iconname:", iconname
try: try:
abstract = self.db.makeDocAbstract(doc, query).encode('utf-8') abstract = self.db.makeDocAbstract(doc, query).encode('utf-8')