3
class MenuDir : Object {
4
public string id { get; set; }
5
public string name { get; set; }
6
public string summary { get; set; }
7
public string icon { get; set; }
8
public string directory { get; set; }
9
public string[] included;
10
public string[] excluded;
13
public void complete (KeyFile file) {
15
icon = "applications-other";
17
file.load_from_file ("/usr/share/desktop-directories/%s".printf (directory), 0);
18
name = file.get_string ("Desktop Entry", "Name");
19
summary = file.get_string ("Desktop Entry", "Comment");
20
icon = file.get_string ("Desktop Entry", "Icon");
21
if (summary == null) {
28
GLib.debug ("Error retrieving datas for %s: %s\n", directory, e.message);
39
private const MarkupParser parser = {
40
opening_item,// when an element opens
41
closing_item, // when an element closes
42
get_text, // when text is found
43
null, // when comments are found
44
null // when errors occur
47
private string menu_file;
48
private MarkupParseContext context;
49
private MenuDir[] dirlist;
50
private MenuDir[] dirs_level;
52
private int level = 0;
53
private string last_item;
54
private bool include = true;
57
public MenuDir[] parse () {
59
FileUtils.get_contents (menu_file, out file, null);
60
context.parse (file, file.length);
65
public MenuParser (string menu_file) {
66
context = new MarkupParseContext(
67
parser, // the structure with the callbacks
68
0, // MarkupParseFlags
69
this, // extra argument for the callbacks, methods in this case
70
null // when the parsing ends
75
this.menu_file = menu_file;
79
void opening_item (MarkupParseContext context, string name, string[] attr, string[] vals) throws MarkupError {
83
MenuDir tmp = new MenuDir();
84
dirs_level[level] = tmp;
86
dirs_level[level].level = level;
95
void closing_item (MarkupParseContext context, string name) throws MarkupError {
99
dirs_level[level].complete (file);
107
private bool check_whitespaces (string str) {
108
return (str.strip() == "" ? true : false);
111
void get_text (MarkupParseContext context, string text, size_t text_len) throws MarkupError {
112
if (check_whitespaces (text)) { return; }
115
dirs_level[level-1].name = text;
116
dirs_level[level-1].id = text;
119
dirs_level[level-1].directory = text;
123
dirs_level[level-1].included += text;
125
dirs_level[level-1].excluded += text;