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.py < prev    next >
Encoding:
Python Source  |  2006-08-30  |  5.8 KB  |  174 lines

  1. #!/usr/bin/env python
  2.  
  3. #
  4. # Copyright (C) 2005 Red Hat, Inc.
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. #
  20.  
  21. import os
  22. import os.path
  23. import gtk
  24. import gtk.gdk
  25. import gmenu
  26.  
  27. def lookup_system_menu_file (menu_file):
  28.     conf_dirs = None
  29.     if os.environ.has_key ("XDG_CONFIG_DIRS"):
  30.         conf_dirs = os.environ["XDG_CONFIG_DIRS"]
  31.     if not conf_dirs:
  32.         conf_dirs = "/etc/xdg"
  33.  
  34.     for conf_dir in conf_dirs.split (":"):
  35.         menu_file_path = os.path.join (conf_dir, "menus", menu_file)
  36.         if os.path.isfile (menu_file_path):
  37.             return menu_file_path
  38.     
  39.     return None
  40.  
  41. def load_icon_from_path (icon_path):
  42.     if os.path.isfile (icon_path):
  43.         try:
  44.             return gtk.gdk.pixbuf_new_from_file_at_size (icon_path, 24, 24)
  45.         except:
  46.             pass
  47.     return None
  48.  
  49. def load_icon_from_data_dirs (icon_value):
  50.     data_dirs = None
  51.     if os.environ.has_key ("XDG_DATA_DIRS"):
  52.         data_dirs = os.environ["XDG_DATA_DIRS"]
  53.     if not data_dirs:
  54.         data_dirs = "/usr/local/share/:/usr/share/"
  55.  
  56.     for data_dir in data_dirs.split (":"):
  57.         retval = load_icon_from_path (os.path.join (data_dir, "pixmaps", icon_value))
  58.         if retval:
  59.             return retval
  60.         retval = load_icon_from_path (os.path.join (data_dir, "icons", icon_value))
  61.         if retval:
  62.             return retval
  63.     
  64.     return None
  65.  
  66. def load_icon (icon_theme, icon_value):
  67.     if not icon_value:
  68.         return
  69.  
  70.     if os.path.isabs (icon_value):
  71.         icon = load_icon_from_path (icon_value)
  72.         if icon:
  73.             return icon
  74.         icon_name = os.path.basename (icon_value)
  75.     else:
  76.         icon_name = icon_value
  77.     
  78.     if icon_name.endswith (".png"):
  79.         icon_name = icon_name[:-len (".png")]
  80.     elif icon_name.endswith (".xpm"):
  81.         icon_name = icon_name[:-len (".xpm")]
  82.     elif icon_name.endswith (".svg"):
  83.         icon_name = icon_name[:-len (".svg")]
  84.     
  85.     try:
  86.         return icon_theme.load_icon (icon_name, 24, 0)
  87.     except:
  88.         return load_icon_from_data_dirs (icon_value)
  89.  
  90. class MenuTreeModel (gtk.TreeStore):
  91.     (
  92.         COLUMN_IS_ENTRY,
  93.         COLUMN_ID,
  94.         COLUMN_NAME,
  95.         COLUMN_ICON,
  96.         COLUMN_MENU_FILE,
  97.         COLUMN_SYSTEM_VISIBLE,
  98.         COLUMN_USER_VISIBLE
  99.     ) = range (7)
  100.  
  101.     def __init__ (self, menu_files):
  102.         gtk.TreeStore.__init__ (self, bool, str, str, gtk.gdk.Pixbuf, str, bool, bool)
  103.  
  104.         self.entries_list_iter = None
  105.         
  106.         self.icon_theme = gtk.icon_theme_get_default ()
  107.  
  108.         if (len (menu_files) < 1):
  109.             menu_files = ["applications.menu", "settings.menu"]
  110.  
  111.         for menu_file in menu_files:
  112.             tree = gmenu.lookup_tree (menu_file, gmenu.FLAGS_INCLUDE_EXCLUDED)
  113.             self.__append_directory (tree.root, None, False, menu_file)
  114.  
  115.             system_file = lookup_system_menu_file (menu_file)
  116.             if system_file:
  117.                 system_tree = gmenu.lookup_tree (system_file, gmenu.FLAGS_INCLUDE_EXCLUDED)
  118.                 self.__append_directory (system_tree.root, None, True, menu_file)
  119.  
  120.     def __append_directory (self, directory, parent_iter, system, menu_file):
  121.         if not directory:
  122.             return
  123.         
  124.         iter = self.iter_children (parent_iter)
  125.         while iter:
  126.             if self[iter][self.COLUMN_ID] == directory.menu_id:
  127.                 break
  128.             iter = self.iter_next (iter)
  129.  
  130.         if not iter:
  131.             iter = self.append (parent_iter)
  132.  
  133.             self[iter][self.COLUMN_IS_ENTRY] = False
  134.             self[iter][self.COLUMN_ID]       = directory.menu_id
  135.             self[iter][self.COLUMN_NAME]     = directory.name
  136.             self[iter][self.COLUMN_ICON]     = load_icon (self.icon_theme, directory.icon)
  137.  
  138.             if not menu_file is None:
  139.                 self[iter][self.COLUMN_MENU_FILE] = menu_file
  140.  
  141.         if system:
  142.             self[iter][self.COLUMN_SYSTEM_VISIBLE] = True
  143.         else:
  144.             self[iter][self.COLUMN_USER_VISIBLE]   = True
  145.         
  146.         for child_item in directory.contents:
  147.             if isinstance (child_item, gmenu.Directory):
  148.                 self.__append_directory (child_item, iter, system, None)
  149.                 
  150.             if not isinstance (child_item, gmenu.Entry):
  151.                 continue
  152.             
  153.             child_iter = self.iter_children (iter)
  154.             while child_iter:
  155.                 if child_item.type == gmenu.TYPE_ENTRY and \
  156.                    self[child_iter][self.COLUMN_IS_ENTRY] and \
  157.                    self[child_iter][self.COLUMN_ID] == child_item.desktop_file_id:
  158.                         break
  159.                 child_iter = self.iter_next (child_iter)
  160.  
  161.             if not child_iter:
  162.                 child_iter = self.append (iter)
  163.  
  164.                 self[child_iter][self.COLUMN_IS_ENTRY] = True
  165.                 self[child_iter][self.COLUMN_ID]       = child_item.desktop_file_id
  166.                 self[child_iter][self.COLUMN_NAME]     = child_item.name
  167.                 self[child_iter][self.COLUMN_ICON]     = load_icon (self.icon_theme,
  168.                                                                     child_item.icon)
  169.  
  170.             if system:
  171.                 self[child_iter][self.COLUMN_SYSTEM_VISIBLE] = not child_item.is_excluded
  172.             else:
  173.                 self[child_iter][self.COLUMN_USER_VISIBLE]   = not child_item.is_excluded
  174.