home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / python2.4 / site-packages / AppInstall / common.py < prev    next >
Encoding:
Python Source  |  2006-08-28  |  3.8 KB  |  103 lines

  1. # ToughIconTheme
  2. # Try to load an icon from the current icon theme. if this fails try to load
  3. # from the default gnome theme. if this fails to try to use the buildin
  4. # MISSING_IMAGE stock icon. and as a last fallback set the icon to None
  5.  
  6. import gtk
  7. import gobject
  8. import pygtk
  9. from warnings import warn
  10.  
  11. class ToughIconTheme:
  12.     def __init__(self):
  13.         self.fallback = gtk.IconTheme()
  14.         self.fallback.set_custom_theme("hicolor")
  15.         self.crystal = gtk.IconTheme()
  16.         self.crystal.set_custom_theme("crystal")
  17.         self.gnome = gtk.IconTheme()
  18.         self.gnome.set_custom_theme("gnome")
  19.         self.default = gtk.icon_theme_get_default()
  20.         self.themes = [self.default, self.gnome, self.fallback, self.crystal]
  21.         self._internal_cache = {}
  22.  
  23.     def load_icon(self, icon, size=48, flags=0):
  24.         for theme in self.themes:
  25.             try:
  26.                 return theme.load_icon(icon, size, flags)
  27.             except:
  28.                 continue
  29.         return self.fallback.load_icon(gtk.STOCK_MISSING_IMAGE, size, flags)
  30.  
  31.     def prepend_search_path(self, path):
  32.         for theme in self.themes:
  33.             try:
  34.                 theme.prepend_search_path(path)
  35.             except:
  36.                 continue
  37.         return 
  38.  
  39.     def lookup_icon(self,  name, size, flags=0):
  40.         for theme in self.themes:
  41.             info = theme.lookup_icon(name, size, flags)
  42.             if info != None:
  43.                 return info
  44.         return None
  45.  
  46.     def get_icon_sizes(self, icon_name):
  47.         for theme in self.themes:
  48.             try:
  49.                 return theme.get_icon_sizes(icon_name)
  50.             except:
  51.                 continue
  52.         return None
  53.  
  54.     def has_icon(self, icon_name):
  55.         for theme in self.themes:
  56.             if theme.has_icon(icon_name) == True:
  57.                 return True
  58.         return False
  59.  
  60.     def _getIcon(self, name, size):
  61.         cache_name = "%s-%s" % (name, size)
  62.         if self._internal_cache.has_key(cache_name):
  63.             return self._internal_cache[cache_name]
  64.         if name is None or name == "":
  65.             warn("ICON: Using dummy icon")
  66.             name = "gnome-other"
  67.         if name.startswith("/"):
  68.             warn("ICON: Doesn't handle absolute paths: '%s'" % name)
  69.             name = "gnome-other"
  70.         if name.find(".") != -1:
  71.             import os.path
  72.             # splitting off extensions in all cases broke evolution's icon
  73.             # hopefully no common image extensions will ever have len != 3
  74.             if len(os.path.splitext(name)[1]) == (3 + 1): # extension includes '.'
  75.                 name = os.path.splitext(name)[0]
  76.         if not self.has_icon(name):
  77.             warn("ICON: Icon '%s' is not in theme" % name)
  78.             name = "gnome-other"
  79.         # FIXME: mvo: this try: except is a hack to work around 
  80.         #             ubuntu #6858 (icon is no longer in cache after removal)
  81.         #             of a pkg. correct is probably to reload the icontheme
  82.         #             (or something)
  83.         try:
  84.             icon = self.load_icon(name, size, 0)
  85.         except gobject.GError:
  86.             icon = self.load_icon("gnome-other", size, 0)
  87.             name = "gnome-other"
  88.         if icon.get_width() != size:
  89.             warn("ICON: Got badly sized icon for %s" % name)
  90.             icon = icon.scale_simple(size, size, gtk.gdk.INTERP_BILINEAR)
  91.         
  92.         info = self.lookup_icon(name, size, gtk.ICON_LOOKUP_NO_SVG | gtk.ICON_LOOKUP_USE_BUILTIN)
  93.         if info is None:
  94.             info = self.lookup_icon("gnome-other", size, gtk.ICON_LOOKUP_NO_SVG | gtk.ICON_LOOKUP_USE_BUILTIN)
  95.             if info is None:
  96.                 filename = None
  97.             else:
  98.                 filename = info.get_filename()
  99.         else:
  100.             filename = info.get_filename()
  101.         self._internal_cache[cache_name] = icon
  102.         return icon
  103.