1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
/////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011-2017 by Simon Schneegans
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or (at
// your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
/////////////////////////////////////////////////////////////////////////
namespace GnomePie {
/////////////////////////////////////////////////////////////////////////
/// An ActionGroup which displays the user's main menu. It's a bit ugly,
/// but it supports both, an older version and libgnome-menus-3 at the
/// same time.
/////////////////////////////////////////////////////////////////////////
public class MenuGroup : ActionGroup {
/////////////////////////////////////////////////////////////////////
/// Used to register this type of ActionGroup. It sets the display
/// name for this ActionGroup, it's icon name and the string used in
/// the pies.conf file for this kind of ActionGroups.
/////////////////////////////////////////////////////////////////////
public static GroupRegistry.TypeDescription register() {
var description = new GroupRegistry.TypeDescription();
description.name = _("Group: Main menu");
description.icon = "start-here";
description.description = _("Displays your main menu structure.");
description.id = "menu";
return description;
}
/////////////////////////////////////////////////////////////////////
/// True, if this MenuGroup is the top most menu.
/////////////////////////////////////////////////////////////////////
public bool is_toplevel {get; construct set; default = true;}
/////////////////////////////////////////////////////////////////////
/// The menu tree displayed by the MenuGroup. Only set for the
/// toplevel MenuGroup.
/////////////////////////////////////////////////////////////////////
private GMenu.Tree menu = null;
/////////////////////////////////////////////////////////////////////
/// A list of all sub menus of this MenuGroup.
/////////////////////////////////////////////////////////////////////
private Gee.ArrayList<MenuGroup?> childs;
/////////////////////////////////////////////////////////////////////
/// Two members needed to avoid useless, frequent changes of the
/// stored Actions.
/////////////////////////////////////////////////////////////////////
private bool changing = false;
private bool changed_again = false;
/////////////////////////////////////////////////////////////////////
/// C'tor, initializes all members. Used for the toplevel menu.
/////////////////////////////////////////////////////////////////////
public MenuGroup(string parent_id) {
GLib.Object(parent_id : parent_id, is_toplevel : true);
}
/////////////////////////////////////////////////////////////////////
/// C'tor, initializes all members. Used for sub menus.
/////////////////////////////////////////////////////////////////////
public MenuGroup.sub_menu(string parent_id) {
GLib.Object(parent_id : parent_id, is_toplevel : false);
}
construct {
this.childs = new Gee.ArrayList<MenuGroup?>();
if (this.is_toplevel) {
#if HAVE_GMENU_3
this.menu = new GMenu.Tree("applications.menu", GMenu.TreeFlags.INCLUDE_EXCLUDED);
this.menu.changed.connect(this.reload);
#endif
this.load_toplevel();
}
}
/////////////////////////////////////////////////////////////////////
/// Starts to load the menu.
/////////////////////////////////////////////////////////////////////
private void load_toplevel() {
#if HAVE_GMENU_3
try {
if (this.menu.load_sync()) {
this.load_contents(this.menu.get_root_directory(), this.parent_id);
}
} catch (GLib.Error e) {
warning(e.message);
}
#else
this.menu = GMenu.Tree.lookup ("applications.menu", GMenu.TreeFlags.INCLUDE_EXCLUDED);
this.menu.add_monitor(this.reload);
var dir = this.menu.get_root_directory();
this.load_contents(dir, this.parent_id);
#endif
}
/////////////////////////////////////////////////////////////////////
/// Parses the main menu recursively.
/////////////////////////////////////////////////////////////////////
private void load_contents(GMenu.TreeDirectory dir, string parent_id) {
#if HAVE_GMENU_3
var item = dir.iter();
while (true) {
var type = item.next();
if (type == GMenu.TreeItemType.INVALID)
break;
if (type == GMenu.TreeItemType.DIRECTORY && !item.get_directory().get_is_nodisplay()) {
// create a MenuGroup for sub menus
// get icon
var icon = item.get_directory().get_icon();
var sub_menu = PieManager.create_dynamic_pie(item.get_directory().get_name(),
icon.to_string());
var group = new MenuGroup.sub_menu(sub_menu.id);
group.add_action(new PieAction(parent_id, true));
group.load_contents(item.get_directory(), sub_menu.id);
childs.add(group);
sub_menu.add_group(group);
this.add_action(new PieAction(sub_menu.id));
} else if (type == GMenu.TreeItemType.ENTRY ) {
// create an AppAction for entries
if (!item.get_entry().get_is_excluded()) {
this.add_action(ActionRegistry.new_for_app_info(item.get_entry().get_app_info()));
}
}
}
#else
foreach (var item in dir.get_contents()) {
switch(item.get_type()) {
case GMenu.TreeItemType.DIRECTORY:
// create a MenuGroup for sub menus
if (!((GMenu.TreeDirectory)item).get_is_nodisplay()) {
var sub_menu = PieManager.create_dynamic_pie(
((GMenu.TreeDirectory)item).get_name(),
((GMenu.TreeDirectory)item).get_icon());
var group = new MenuGroup.sub_menu(sub_menu.id);
group.add_action(new PieAction(parent_id, true));
group.load_contents((GMenu.TreeDirectory)item, sub_menu.id);
childs.add(group);
sub_menu.add_group(group);
this.add_action(new PieAction(sub_menu.id));
}
break;
case GMenu.TreeItemType.ENTRY:
// create an AppAction for entries
if (!((GMenu.TreeEntry)item).get_is_nodisplay() && !((GMenu.TreeEntry)item).get_is_excluded()) {
this.add_action(new AppAction(((GMenu.TreeEntry)item).get_name(),
((GMenu.TreeEntry)item).get_icon(),
((GMenu.TreeEntry)item).get_exec()));
}
break;
}
}
#endif
}
/////////////////////////////////////////////////////////////////////
/// Reloads the menu.
/////////////////////////////////////////////////////////////////////
private void reload() {
// avoid too frequent changes...
if (!this.changing) {
this.changing = true;
Timeout.add(500, () => {
if (this.changed_again) {
this.changed_again = false;
return true;
}
// reload
message("Main menu changed...");
#if !HAVE_GMENU_3
this.menu.remove_monitor(this.reload);
#endif
this.clear();
this.load_toplevel();
this.changing = false;
return false;
});
} else {
this.changed_again = true;
}
}
/////////////////////////////////////////////////////////////////////
/// Deletes all generated Pies, when the toplevel menu is deleted.
/////////////////////////////////////////////////////////////////////
public override void on_remove() {
if (this.is_toplevel)
this.clear();
}
/////////////////////////////////////////////////////////////////////
/// Clears this ActionGroup recursively.
/////////////////////////////////////////////////////////////////////
private void clear() {
foreach (var child in childs)
child.clear();
if (!this.is_toplevel)
PieManager.remove_pie(this.parent_id);
this.delete_all();
this.childs.clear();
#if !HAVE_GMENU_3
this.menu = null;
#endif
}
}
}
|