~jconti/recent-notifications/trunk

« back to all changes in this revision

Viewing changes to recent_notifications/Icon.py

  • Committer: Jason Conti
  • Date: 2010-02-21 21:26:22 UTC
  • Revision ID: jason.conti@gmail.com-20100221212622-qi7mpvfc0uxvzofz
Replacing COPYING with a gpl3 license.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""
2
 
Icon.py
3
 
by Jason Conti
4
 
February 22, 2010
5
 
 
6
 
Utility functions for working with icons.
7
 
"""
8
 
 
9
 
import gc
10
 
import gtk
11
 
import logging
12
 
 
13
 
icon_cache = {}
14
 
logger = logging.getLogger("Icon")
15
 
 
16
 
def clear_icon_cache():
17
 
  """Clear the icon cache, garbage collecting all the icons."""
18
 
  global icon_cache
19
 
  for key, icon in icon_cache:
20
 
    del icon
21
 
  icon_cache = {}
22
 
  gc.collect()
23
 
 
24
 
def load_icon(name, size = 48):
25
 
  """Loads an icon from the default icon theme."""
26
 
  global icon_cache
27
 
  if (name, size) in icon_cache:
28
 
    return icon_cache[(name, size)]
29
 
  else:
30
 
    try:
31
 
      theme = gtk.icon_theme_get_default()
32
 
      icon = theme.load_icon(name, size, gtk.ICON_LOOKUP_FORCE_SVG)
33
 
      icon_cache[(name, size)] = icon
34
 
      return icon
35
 
    except:
36
 
      logger.exception("failed to load_icon")
37
 
      return None
38
 
 
39
 
def load_icon_from_file(path, size = 48):
40
 
  """Loads an icon from the given path."""
41
 
  global icon_cache
42
 
  if (path, size) in icon_cache:
43
 
    return icon_cache[(path, size)]
44
 
  else:
45
 
    try:
46
 
      icon = gtk.gdk.pixbuf_new_from_file_at_size(path, size, size)
47
 
      icon_cache[(path, size)] = icon
48
 
      return icon
49
 
    except:
50
 
      logger.exception("failed to load_icon_from_file")
51
 
      return None
52