~jconti/recent-notifications/trunk

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
"""
Icon.py
by Jason Conti
February 22, 2010
updated: March 26, 2011

Utility functions for working with icons.
"""

import gc
from gi.repository import GdkPixbuf, Gtk
import logging

icon_cache = {}
logger = logging.getLogger("Icon")

def clear_cache():
  """Clear the icon cache, garbage collecting all the icons."""
  global icon_cache
  for key, icon in icon_cache:
    del icon
  icon_cache = {}
  gc.collect()

def load(name, size = 48):
  """Loads an icon from the default icon theme."""
  global icon_cache
  if (name, size) in icon_cache:
    return icon_cache[(name, size)]
  else:
    try:
      theme = Gtk.IconTheme.get_default()
      icon = theme.load_icon(name, size, Gtk.IconLookupFlags.FORCE_SVG)
      icon_cache[(name, size)] = icon
      return icon
    except:
      logger.exception("failed to load_icon")
      return None

def load_from_file(path, size = 48):
  """Loads an icon from the given path."""
  global icon_cache
  if (path, size) in icon_cache:
    return icon_cache[(path, size)]
  else:
    try:
      icon = GdkPixbuf.Pixbuf.new_from_file_at_size(path, size, size)
      icon_cache[(path, size)] = icon
      return icon
    except:
      logger.exception("failed to load_icon_from_file")
      return None