~lubuntu-software-center-team/lubuntu-software-center/vala-port

« back to all changes in this revision

Viewing changes to src/db-build/libmenu.vala

  • Committer: Stephen Smally
  • Date: 2012-07-04 11:46:04 UTC
  • Revision ID: eco.stefi@fastwebnet.it-20120704114604-3esw3fj1oo1j7eed
Implemented Database categories within GUI

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
namespace Menu {
3
 
    class MenuDir : Object {
4
 
                public string id { get; set; }
5
 
        public string name { get; set; }
6
 
        public string icon { get; set; }
7
 
        public string directory { get; set; }
8
 
        public string[] included;
9
 
        public string[] excluded;
10
 
        public int level;
11
 
        
12
 
        public void complete (KeyFile file) {
13
 
                        try {
14
 
                                file.load_from_file ("/usr/share/desktop-directories/%s".printf (directory), 0);
15
 
                                name = file.get_string ("Desktop Entry", "Name");
16
 
                                icon = file.get_string ("Desktop Entry", "Icon");
17
 
                        } catch (Error e) {
18
 
                                GLib.debug ("Error retrieving datas for %s: %s\n", directory, e.message);
19
 
                        }
20
 
                }
21
 
        
22
 
        public MenuDir () {
23
 
            included = {};
24
 
            excluded = {};
25
 
        }
26
 
    }
27
 
    
28
 
    class MenuParser {
29
 
        private const MarkupParser parser = {
30
 
            opening_item,// when an element opens
31
 
            closing_item,  // when an element closes
32
 
            get_text, // when text is found
33
 
            null, // when comments are found
34
 
            null  // when errors occur
35
 
        };
36
 
        
37
 
        private string menu_file;
38
 
        private MarkupParseContext context;
39
 
        private MenuDir[] dirlist;
40
 
        private MenuDir dir;
41
 
        private int level = 0;
42
 
        private string last_item;
43
 
        private bool include = true;
44
 
        private KeyFile file;
45
 
        
46
 
        public MenuDir[] parse () {
47
 
            string file;
48
 
            FileUtils.get_contents (menu_file, out file, null);
49
 
            context.parse (file, file.length);
50
 
            
51
 
            return dirlist;
52
 
        }
53
 
        
54
 
        public MenuParser (string menu_file) {
55
 
            context = new MarkupParseContext(
56
 
                parser, // the structure with the callbacks
57
 
                0,      // MarkupParseFlags
58
 
                this,   // extra argument for the callbacks, methods in this case
59
 
                null // when the parsing ends
60
 
            );
61
 
            
62
 
            dirlist = {};
63
 
            this.menu_file = menu_file;
64
 
            file = new KeyFile();
65
 
        }
66
 
        
67
 
        void opening_item (MarkupParseContext context, string name, string[] attr, string[] vals) throws MarkupError {
68
 
            last_item = name;
69
 
            switch (name) {
70
 
                case "Menu":
71
 
                    dir = new MenuDir();
72
 
                    dirlist += dir;
73
 
                    dir.level = level;
74
 
                    level ++;
75
 
                    break;
76
 
                case "Not":
77
 
                    include = false;
78
 
                    break;
79
 
            }
80
 
        }
81
 
        
82
 
        void closing_item (MarkupParseContext context, string name) throws MarkupError {
83
 
            switch (name) {
84
 
                case "Menu":
85
 
                    level --;
86
 
                    dir.complete (file);
87
 
                    break;
88
 
                case "Not":
89
 
                    include = true;
90
 
                    break;
91
 
            }
92
 
        }
93
 
        
94
 
        private bool check_whitespaces (string str) {
95
 
            return (str.strip() == "" ? true : false);
96
 
        }
97
 
        
98
 
        void get_text (MarkupParseContext context, string text, size_t text_len) throws MarkupError {
99
 
            if (check_whitespaces (text)) { return; }
100
 
            switch (last_item) {
101
 
                case "Name":
102
 
                    dir.name = text;
103
 
                    dir.id = text;
104
 
                    break;
105
 
                case "Directory":
106
 
                    dir.directory = text;
107
 
                    break;
108
 
                case "Category":
109
 
                    if (include) {
110
 
                        dir.included += text;
111
 
                    } else {
112
 
                        dir.excluded += text;
113
 
                    }
114
 
                    break;
115
 
            }
116
 
        }
117
 
    }
118
 
}