/usr/share/avant-window-navigator/applets/stacks/stacks_icons.py is in awn-applet-stack 0.4.1~bzr1507-0ubuntu7.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | #!/usr/bin/env python
# Copyright (c) 2007 Timon ter Braak
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
import os
import gio
import gtk
try:
import gnome.ui
except ImportError:
pass
# Borrowed Thumbnailer from "gimmie"
class Thumbnailer:
def __init__(self, uri, mimetype):
self.uri = uri or ""
self.mimetype = mimetype or ""
self.cached_icon = None
self.cached_timestamp = None
self.cached_size = None
def get_icon(self, icon_size, timestamp = 0):
if not self.cached_icon or \
icon_size != self.cached_size or \
timestamp != self.cached_timestamp:
self.cached_icon = self._lookup_or_make_thumb(icon_size, timestamp)
self.cached_icon = self.cached_icon.add_alpha (True, '\0', '\0', '\0')
self.cached_size = icon_size
self.cached_timestamp = timestamp
return self.cached_icon
def _lookup_or_make_thumb(self, icon_size, timestamp):
icon_name = None
# FIXME replace gnome.ui usage by something else
try:
icon_theme = gtk.icon_theme_get_default()
thumb_factory = gnome.ui.ThumbnailFactory("normal")
icon_name, icon_type = \
gnome.ui.icon_lookup(icon_theme, thumb_factory, self.uri, self.mimetype, 0)
if icon_type == gnome.ui.ICON_LOOKUP_RESULT_FLAGS_THUMBNAIL or \
thumb_factory.has_valid_failed_thumbnail(self.uri, timestamp):
# Use existing thumbnail
thumb = IconFactory().load_icon(icon_name, icon_size)
elif self._is_local_uri(self.uri):
# Generate a thumbnail for local files only
thumb = thumb_factory.generate_thumbnail(self.uri, self.mimetype)
thumb_factory.save_thumbnail(thumb, self.uri, timestamp)
thumb = IconFactory().scale_to_bounded(thumb, icon_size)
if thumb:
# Fixup the thumbnail a bit
thumb = self._nicer_dimensions(thumb)
return thumb
except:
pass
if icon_name is None:
icon_name = "image-missing"
# Fallback to mime-type icon on failure
return IconFactory().load_icon(icon_name, icon_size)
def _is_local_uri(self, uri):
# NOTE: gnomevfs.URI.is_local seems to hang for some URIs (e.g. ssh
# or http). So look in a list of local schemes which comes
# directly from gnome_vfs_uri_is_local_scheme.
try:
scheme = uri.split("://")[0]
return not scheme or scheme in ("file", "help", "ghelp", "gnome-help",
"trash", "man", "info", "hardware", "search", "pipe","gnome-trash")
except:
return False
def _nicer_dimensions(self, icon):
### Constrain thumb dimensions to 1:1.2
if float(icon.get_height()) / float(icon.get_width()) > 1.2:
return icon.subpixbuf(0, 0, icon.get_width(), int(icon.get_width() * 1.2))
return icon
# Borrowed IconFactory from "gimmie"
class IconFactory:
def load_icon_from_path(self, icon_path, icon_size = None):
if os.path.isfile(icon_path):
try:
if icon_size:
# constrain height, not width
thumb = gtk.gdk.pixbuf_new_from_file_at_size(icon_path, -1, int(icon_size))
return thumb
else:
thumb = gtk.gdk.pixbuf_new_from_file(icon_path)
return thumb
except:
pass
return None
def load_icon_from_data_dirs(self, icon_value, icon_size = None):
data_dirs = None
if os.environ.has_key("XDG_DATA_DIRS"):
data_dirs = os.environ["XDG_DATA_DIRS"]
if not data_dirs:
data_dirs = "/usr/local/share/:/usr/share/"
for data_dir in data_dirs.split(":"):
retval = self.load_icon_from_path(os.path.join(data_dir, "pixmaps", icon_value),
icon_size)
if retval:
return retval
retval = self.load_icon_from_path(os.path.join(data_dir, "icons", icon_value),
icon_size)
if retval:
return retval
return None
def scale_to_bounded(self, icon, size):
if icon:
if icon.get_height() > size:
_icon = icon.scale_simple(
int(round(size * icon.get_width() / icon.get_height())),
int(round(size)),
gtk.gdk.INTERP_BILINEAR)
if _icon is not None:
icon = _icon
if icon.get_width() > size:
_icon = icon.scale_simple(
int(round(size)),
int(round(size * icon.get_height() / icon.get_width())),
gtk.gdk.INTERP_BILINEAR)
if _icon is not None:
icon = _icon
return icon
def load_icon(self, icon_value, icon_size, force_size = True):
assert icon_value, "No icon to load!"
if isinstance(icon_size, gtk.IconSize):
icon_size = gtk.icon_size_lookup(icon_size)[0]
force_size = True
if isinstance(icon_value, gtk.gdk.Pixbuf):
if force_size:
return self.scale_to_bounded(icon_value, icon_size)
return icon_value
if os.path.isabs(icon_value):
icon = self.load_icon_from_path(icon_value, icon_size)
if icon:
if force_size:
return self.scale_to_bounded(icon, icon_size)
return icon
icon_name = os.path.basename(icon_value)
else:
icon_name = icon_value
if icon_name.endswith(".png"):
icon_name = icon_name[:-len(".png")]
elif icon_name.endswith(".xpm"):
icon_name = icon_name[:-len(".xpm")]
elif icon_name.endswith(".svg"):
icon_name = icon_name[:-len(".svg")]
icon = None
icon_theme = gtk.icon_theme_get_default()
info = icon_theme.lookup_icon(icon_name, icon_size, gtk.ICON_LOOKUP_USE_BUILTIN)
if info:
if icon_name.startswith("gtk-"):
# NOTE: IconInfo/IconTheme.load_icon leaks a ref to the icon, so
# load it manually.
# NOTE: The bindings are also broken for Gtk's builtin pixbufs:
# IconInfo.get_builtin_pixbuf always returns None.
icon = info.load_icon()
elif info.get_filename():
icon = self.load_icon_from_path(info.get_filename())
else:
icon = self.load_icon_from_data_dirs(icon_value, icon_size) # Fallback
if icon and force_size:
return self.scale_to_bounded(icon, icon_size)
return icon
def load_image(self, icon_value, icon_size, force_size=True):
pixbuf = self.load_icon(icon_value, icon_size, force_size)
img = gtk.Image()
img.set_from_pixbuf(pixbuf)
img.show()
return img
|