2
# Copyright (C) 2005 Red Hat, Inc.
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
from gi.repository import GObject
22
from gi.repository import Gio
23
from gi.repository import Gtk
24
from gi.repository import GdkPixbuf
25
from gi.repository import GMenu
27
def lookup_system_menu_file (menu_file):
29
if os.environ.has_key ("XDG_CONFIG_DIRS"):
30
conf_dirs = os.environ["XDG_CONFIG_DIRS"]
32
conf_dirs = "/etc/xdg"
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):
41
class MenuTreeModel (Gtk.TreeStore):
48
COLUMN_SYSTEM_VISIBLE,
52
def __init__ (self, menu_files):
53
Gtk.TreeStore.__init__ (self, bool, str, str, Gio.Icon, str, bool, bool)
55
self.entries_list_iter = None
57
if (len (menu_files) < 1):
58
menu_files = ["applications.menu"]
60
for menu_file in menu_files:
61
if menu_file == "applications.menu" and os.environ.has_key ("XDG_MENU_PREFIX"):
62
menu_file = os.environ["XDG_MENU_PREFIX"] + menu_file
64
tree = GObject.new (GMenu.Tree, menu_basename = menu_file, flags = GMenu.TreeFlags.INCLUDE_EXCLUDED|GMenu.TreeFlags.SORT_DISPLAY_NAME)
67
self.__append_directory (tree.get_root_directory (), None, False, menu_file)
69
system_file = lookup_system_menu_file (menu_file)
71
system_tree = GObject.new (GMenu.Tree, menu_path = system_file, flags = GMenu.TreeFlags.INCLUDE_EXCLUDED|GMenu.TreeFlags.SORT_DISPLAY_NAME)
72
system_tree.load_sync ()
74
self.__append_directory (system_tree.get_root_directory (), None, True, menu_file)
76
def __append_directory (self, directory, parent_iter, system, menu_file):
80
iter = self.iter_children (parent_iter)
81
while iter is not None:
82
if self.get_value(iter, self.COLUMN_ID) == directory.get_menu_id ():
84
iter = self.iter_next (iter)
87
row = (False, directory.get_menu_id (), directory.get_name (), directory.get_icon (), menu_file, False, False)
88
iter = self.append (parent_iter, row)
91
self.set_value (iter, self.COLUMN_SYSTEM_VISIBLE, True)
93
self.set_value (iter, self.COLUMN_USER_VISIBLE, True)
95
dir_iter = directory.iter ()
96
next_type = dir_iter.next ()
97
while next_type != GMenu.TreeItemType.INVALID:
98
current_type = next_type
99
next_type = dir_iter.next ()
101
if current_type == GMenu.TreeItemType.DIRECTORY:
102
child_item = dir_iter.get_directory ()
103
self.__append_directory (child_item, iter, system, None)
105
if current_type != GMenu.TreeItemType.ENTRY:
108
child_item = dir_iter.get_entry ()
110
child_iter = self.iter_children (iter)
111
while child_iter is not None:
112
if self.get_value(child_iter, self.COLUMN_IS_ENTRY) and \
113
self.get_value(child_iter, self.COLUMN_ID) == child_item.get_desktop_file_id ():
115
child_iter = self.iter_next (child_iter)
117
if child_iter is None:
118
app_info = child_item.get_app_info ()
119
row = (True, child_item.get_desktop_file_id (), app_info.get_display_name (), app_info.get_icon (), None, False, False)
120
child_iter = self.append (iter, row)
123
self.set_value (child_iter, self.COLUMN_SYSTEM_VISIBLE, not child_item.get_is_excluded (),)
125
self.set_value (child_iter, self.COLUMN_USER_VISIBLE, not child_item.get_is_excluded (),)