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 / GMenuSimpleEditor / menutreemodel.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-08-31  |  4.3 KB  |  156 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. import os
  5. import os.path as os
  6. import gtk
  7. import gtk.gdk as gtk
  8. import gmenu
  9.  
  10. def lookup_system_menu_file(menu_file):
  11.     conf_dirs = None
  12.     if os.environ.has_key('XDG_CONFIG_DIRS'):
  13.         conf_dirs = os.environ['XDG_CONFIG_DIRS']
  14.     
  15.     if not conf_dirs:
  16.         conf_dirs = '/etc/xdg'
  17.     
  18.     for conf_dir in conf_dirs.split(':'):
  19.         menu_file_path = os.path.join(conf_dir, 'menus', menu_file)
  20.         if os.path.isfile(menu_file_path):
  21.             return menu_file_path
  22.             continue
  23.     
  24.  
  25.  
  26. def load_icon_from_path(icon_path):
  27.     if os.path.isfile(icon_path):
  28.         
  29.         try:
  30.             return gtk.gdk.pixbuf_new_from_file_at_size(icon_path, 24, 24)
  31.  
  32.     
  33.  
  34.  
  35. def load_icon_from_data_dirs(icon_value):
  36.     data_dirs = None
  37.     if os.environ.has_key('XDG_DATA_DIRS'):
  38.         data_dirs = os.environ['XDG_DATA_DIRS']
  39.     
  40.     if not data_dirs:
  41.         data_dirs = '/usr/local/share/:/usr/share/'
  42.     
  43.     for data_dir in data_dirs.split(':'):
  44.         retval = load_icon_from_path(os.path.join(data_dir, 'pixmaps', icon_value))
  45.         if retval:
  46.             return retval
  47.         
  48.         retval = load_icon_from_path(os.path.join(data_dir, 'icons', icon_value))
  49.         if retval:
  50.             return retval
  51.             continue
  52.     
  53.  
  54.  
  55. def load_icon(icon_theme, icon_value):
  56.     if not icon_value:
  57.         return None
  58.     
  59.     if os.path.isabs(icon_value):
  60.         icon = load_icon_from_path(icon_value)
  61.         if icon:
  62.             return icon
  63.         
  64.         icon_name = os.path.basename(icon_value)
  65.     else:
  66.         icon_name = icon_value
  67.     if icon_name.endswith('.png'):
  68.         icon_name = icon_name[:-len('.png')]
  69.     elif icon_name.endswith('.xpm'):
  70.         icon_name = icon_name[:-len('.xpm')]
  71.     elif icon_name.endswith('.svg'):
  72.         icon_name = icon_name[:-len('.svg')]
  73.     
  74.     
  75.     try:
  76.         return icon_theme.load_icon(icon_name, 24, 0)
  77.     except:
  78.         return load_icon_from_data_dirs(icon_value)
  79.  
  80.  
  81.  
  82. class MenuTreeModel(gtk.TreeStore):
  83.     (COLUMN_IS_ENTRY, COLUMN_ID, COLUMN_NAME, COLUMN_ICON, COLUMN_MENU_FILE, COLUMN_SYSTEM_VISIBLE, COLUMN_USER_VISIBLE) = range(7)
  84.     
  85.     def __init__(self, menu_files):
  86.         gtk.TreeStore.__init__(self, bool, str, str, gtk.gdk.Pixbuf, str, bool, bool)
  87.         self.entries_list_iter = None
  88.         self.icon_theme = gtk.icon_theme_get_default()
  89.         if len(menu_files) < 1:
  90.             menu_files = [
  91.                 'applications.menu',
  92.                 'settings.menu']
  93.         
  94.         for menu_file in menu_files:
  95.             tree = gmenu.lookup_tree(menu_file, gmenu.FLAGS_INCLUDE_EXCLUDED)
  96.             self._MenuTreeModel__append_directory(tree.root, None, False, menu_file)
  97.             system_file = lookup_system_menu_file(menu_file)
  98.             if system_file:
  99.                 system_tree = gmenu.lookup_tree(system_file, gmenu.FLAGS_INCLUDE_EXCLUDED)
  100.                 self._MenuTreeModel__append_directory(system_tree.root, None, True, menu_file)
  101.                 continue
  102.         
  103.  
  104.     
  105.     def __append_directory(self, directory, parent_iter, system, menu_file):
  106.         if not directory:
  107.             return None
  108.         
  109.         iter = self.iter_children(parent_iter)
  110.         while iter:
  111.             if self[iter][self.COLUMN_ID] == directory.menu_id:
  112.                 break
  113.             
  114.             iter = self.iter_next(iter)
  115.         if not iter:
  116.             iter = self.append(parent_iter)
  117.             self[iter][self.COLUMN_IS_ENTRY] = False
  118.             self[iter][self.COLUMN_ID] = directory.menu_id
  119.             self[iter][self.COLUMN_NAME] = directory.name
  120.             self[iter][self.COLUMN_ICON] = load_icon(self.icon_theme, directory.icon)
  121.             if menu_file is not None:
  122.                 self[iter][self.COLUMN_MENU_FILE] = menu_file
  123.             
  124.         
  125.         if system:
  126.             self[iter][self.COLUMN_SYSTEM_VISIBLE] = True
  127.         else:
  128.             self[iter][self.COLUMN_USER_VISIBLE] = True
  129.         for child_item in directory.contents:
  130.             if isinstance(child_item, gmenu.Directory):
  131.                 self._MenuTreeModel__append_directory(child_item, iter, system, None)
  132.             
  133.             if not isinstance(child_item, gmenu.Entry):
  134.                 continue
  135.             
  136.             child_iter = self.iter_children(iter)
  137.             while child_iter:
  138.                 if child_item.type == gmenu.TYPE_ENTRY and self[child_iter][self.COLUMN_IS_ENTRY] and self[child_iter][self.COLUMN_ID] == child_item.desktop_file_id:
  139.                     break
  140.                 
  141.                 child_iter = self.iter_next(child_iter)
  142.             if not child_iter:
  143.                 child_iter = self.append(iter)
  144.                 self[child_iter][self.COLUMN_IS_ENTRY] = True
  145.                 self[child_iter][self.COLUMN_ID] = child_item.desktop_file_id
  146.                 self[child_iter][self.COLUMN_NAME] = child_item.name
  147.                 self[child_iter][self.COLUMN_ICON] = load_icon(self.icon_theme, child_item.icon)
  148.             
  149.             if system:
  150.                 self[child_iter][self.COLUMN_SYSTEM_VISIBLE] = not (child_item.is_excluded)
  151.                 continue
  152.             self[child_iter][self.COLUMN_USER_VISIBLE] = not (child_item.is_excluded)
  153.         
  154.  
  155.  
  156.