~teejee2008/timeshift/trunk

« back to all changes in this revision

Viewing changes to src/Utility/ThunarMenuEntry.vala

  • Committer: Tony George
  • Date: 2016-08-13 04:16:47 UTC
  • Revision ID: tony.george.kol@gmail.com-20160813041647-ivf2g6rszt00xco5
Updated project structure

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
using GLib;
 
3
using Gtk;
 
4
using Gee;
 
5
using Json;
 
6
using Xml;
 
7
 
 
8
using TeeJee.Logging;
 
9
using TeeJee.FileSystem;
 
10
using TeeJee.JsonHelper;
 
11
using TeeJee.ProcessHelper;
 
12
using TeeJee.GtkHelper;
 
13
using TeeJee.System;
 
14
using TeeJee.Misc;
 
15
 
 
16
public class ThunarMenuEntry : GLib.Object {
 
17
        public string name = "";
 
18
        public string icon = "";
 
19
        public string id = "";
 
20
        public string command = "";
 
21
        public string description = "";
 
22
        public string patterns = "";
 
23
        public bool show_for_directories = false;
 
24
        public bool show_for_audio_files = false;
 
25
        public bool show_for_video_files = false;
 
26
        public bool show_for_image_files = false;
 
27
        public bool show_for_text_files = false;
 
28
        public bool show_for_other_files = false;
 
29
 
 
30
        public static Gee.HashMap<string,ThunarMenuEntry> action_list;
 
31
        public static string user_home;
 
32
        
 
33
        public static void query_actions(string _user_home){
 
34
                log_debug("ThunarMenuEntry.query_actions()");
 
35
                
 
36
                action_list = new Gee.HashMap<string,ThunarMenuEntry>();
 
37
                user_home = _user_home;
 
38
                
 
39
                var xml_path = "%s/.config/Thunar/uca.xml".printf(user_home);
 
40
 
 
41
                if (!file_exists(xml_path)){
 
42
                        return;
 
43
                }
 
44
 
 
45
                xml_fix_invalid_declaration(xml_path);
 
46
                
 
47
                // Parse the document from path
 
48
                Xml.Doc* doc = Xml.Parser.parse_file (xml_path);
 
49
                if (doc == null) {
 
50
                        log_error("File not found or permission denied: %s".printf(xml_path));
 
51
                        return;
 
52
                }
 
53
 
 
54
                Xml.Node* root = doc->get_root_element ();
 
55
                if (root == null) {
 
56
                        log_error("Root element missing in xml: %s".printf(xml_path));
 
57
                        delete doc;
 
58
                        return;
 
59
                }
 
60
        
 
61
                if (root->name != "actions") {
 
62
                        log_error("Unexpected element '%s' in xml: %s".printf(root->name, xml_path));
 
63
                        delete doc;
 
64
                        return;
 
65
                }
 
66
 
 
67
                for (Xml.Node* action = root->children; action != null; action = action->next) {
 
68
                        if (action->type != Xml.ElementType.ELEMENT_NODE) {
 
69
                                continue;
 
70
                        }
 
71
                                
 
72
                        if (action->name != "action") {
 
73
                                continue;
 
74
                        }
 
75
 
 
76
                        var item = new ThunarMenuEntry();
 
77
                        
 
78
                        for (Xml.Node* node = action->children; node != null; node = node->next) {
 
79
                                if (node->type != Xml.ElementType.ELEMENT_NODE) {
 
80
                                        continue;
 
81
                                }
 
82
 
 
83
                                switch (node->name){
 
84
                                case "name":
 
85
                                        item.name = node->get_content();
 
86
                                        break;
 
87
                                case "icon":
 
88
                                        item.icon = node->get_content();
 
89
                                        break;
 
90
                                case "unique-id":
 
91
                                        item.id = node->get_content();
 
92
                                        break;
 
93
                                case "command":
 
94
                                        item.command = node->get_content();
 
95
                                        break;
 
96
                                case "description":
 
97
                                        item.description = node->get_content();
 
98
                                        break;
 
99
                                case "patterns":
 
100
                                        item.patterns = node->get_content();
 
101
                                        break;
 
102
                                case "directories":
 
103
                                        item.show_for_directories = true;
 
104
                                        break;
 
105
                                case "audio-files":
 
106
                                        item.show_for_audio_files = true;
 
107
                                        break;
 
108
                                case "video-files":
 
109
                                        item.show_for_video_files = true;
 
110
                                        break;
 
111
                                case "image-files":
 
112
                                        item.show_for_image_files = true;
 
113
                                        break;
 
114
                                case "text-files":
 
115
                                        item.show_for_text_files = true;
 
116
                                        break;
 
117
                                case "other-files":
 
118
                                        item.show_for_other_files = true;
 
119
                                        break;
 
120
                                }
 
121
                        }
 
122
 
 
123
                        action_list[item.id] = item;
 
124
                }
 
125
 
 
126
                delete doc;
 
127
        }
 
128
 
 
129
        public static void xml_fix_invalid_declaration(string xml_file){
 
130
                if (!file_exists(xml_file)){
 
131
                        return;
 
132
                }
 
133
                
 
134
                var xml = file_read(xml_file);
 
135
                var dec = xml.split("\n")[0];
 
136
                if (dec == "<?xml encoding=\"UTF-8\" version=\"1.0\"?>"){
 
137
                        xml = xml.replace("<?xml encoding=\"UTF-8\" version=\"1.0\"?>",
 
138
                                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
 
139
                        file_write(xml_file, xml);
 
140
                }
 
141
        }
 
142
        
 
143
        public void add(){
 
144
                bool found = false;
 
145
                foreach(var action in action_list.values){
 
146
                        if ((action.id == id)||(action.command == command)){
 
147
                                found = true;
 
148
                                return;
 
149
                        }
 
150
                }
 
151
 
 
152
                if (!found){
 
153
                        var xml_path = "%s/.config/Thunar/uca.xml".printf(user_home);
 
154
 
 
155
                        if (!file_exists(xml_path)){
 
156
                                return;
 
157
                        }
 
158
                        
 
159
                        xml_fix_invalid_declaration(xml_path);
 
160
                        
 
161
                        // Parse the document from path
 
162
                        Xml.Doc* doc = Xml.Parser.parse_file (xml_path);
 
163
                        if (doc == null) {
 
164
                                log_error("File not found or permission denied: %s".printf(xml_path));
 
165
                                return;
 
166
                        }
 
167
 
 
168
                        Xml.Node* root = doc->get_root_element ();
 
169
                        if (root == null) {
 
170
                                log_error("Root element missing in xml: %s".printf(xml_path));
 
171
                                delete doc;
 
172
                                return;
 
173
                        }
 
174
                
 
175
                        if (root->name != "actions") {
 
176
                                log_error("Unexpected element '%s' in xml: %s".printf(root->name, xml_path));
 
177
                                delete doc;
 
178
                                return;
 
179
                        }
 
180
 
 
181
                        for (Xml.Node* action = root->children; action != null; action = action->next) {
 
182
                                if (action->type != Xml.ElementType.ELEMENT_NODE) {
 
183
                                        continue;
 
184
                                }
 
185
                                        
 
186
                                if (action->name != "action") {
 
187
                                        continue;
 
188
                                }
 
189
                        }
 
190
 
 
191
                        Xml.Node* action = root->new_text_child (null, "action", "");
 
192
 
 
193
                        action->new_text_child (null, "name", name);
 
194
                        action->new_text_child (null, "icon", icon);
 
195
                        action->new_text_child (null, "unique-id", id);
 
196
                        action->new_text_child (null, "command", command);
 
197
                        action->new_text_child (null, "description", description);
 
198
                        action->new_text_child (null, "patterns", patterns);
 
199
                        
 
200
                        if (show_for_directories){
 
201
                                action->new_text_child (null, "directories", "");
 
202
                        }
 
203
                        if (show_for_audio_files){
 
204
                                action->new_text_child (null, "audio-files", "");
 
205
                        }
 
206
                        if (show_for_video_files){
 
207
                                action->new_text_child (null, "video-files", "");
 
208
                        }
 
209
                        if (show_for_image_files){
 
210
                                action->new_text_child (null, "image-files", "");
 
211
                        }
 
212
                        if (show_for_text_files){
 
213
                                action->new_text_child (null, "text-files", "");
 
214
                        }
 
215
                        if (show_for_other_files){
 
216
                                action->new_text_child (null, "other-files", "");
 
217
                        }
 
218
 
 
219
                        doc->save_file(xml_path);
 
220
 
 
221
                        delete doc;
 
222
                }
 
223
        }
 
224
 
 
225
        public void remove(){
 
226
                bool found = false;
 
227
                foreach(var action in action_list.values){
 
228
                        if (action.id == id){
 
229
                                found = true;
 
230
                                break;
 
231
                        }
 
232
                }
 
233
 
 
234
                if (found){
 
235
                        var xml_path = "%s/.config/Thunar/uca.xml".printf(user_home);
 
236
 
 
237
                        if (!file_exists(xml_path)){
 
238
                                return;
 
239
                        }
 
240
                        
 
241
                        xml_fix_invalid_declaration(xml_path);
 
242
                        
 
243
                        // Parse the document from path
 
244
                        Xml.Doc* doc = Xml.Parser.parse_file (xml_path);
 
245
                        if (doc == null) {
 
246
                                log_error("File not found or permission denied: %s".printf(xml_path));
 
247
                                return;
 
248
                        }
 
249
 
 
250
                        Xml.Node* root = doc->get_root_element ();
 
251
                        if (root == null) {
 
252
                                log_error("Root element missing in xml: %s".printf(xml_path));
 
253
                                delete doc;
 
254
                                return;
 
255
                        }
 
256
                
 
257
                        if (root->name != "actions") {
 
258
                                log_error("Unexpected element '%s' in xml: %s".printf(root->name, xml_path));
 
259
                                delete doc;
 
260
                                return;
 
261
                        }
 
262
 
 
263
                        for (Xml.Node* action = root->children; action != null; action = action->next) {
 
264
                                if (action->type != Xml.ElementType.ELEMENT_NODE) {
 
265
                                        continue;
 
266
                                }
 
267
                                        
 
268
                                if (action->name != "action") {
 
269
                                        continue;
 
270
                                }
 
271
 
 
272
                                for (Xml.Node* node = action->children; node != null; node = node->next) {
 
273
                                        if (node->type != Xml.ElementType.ELEMENT_NODE) {
 
274
                                                continue;
 
275
                                        }
 
276
 
 
277
                                        switch (node->name){
 
278
                                        case "unique-id":
 
279
                                                if (node->get_content().strip() == id){
 
280
                                                        action->unlink();
 
281
                                                }
 
282
                                                break;
 
283
                                        }
 
284
                                }
 
285
                        }
 
286
 
 
287
                        doc->save_file(xml_path);
 
288
 
 
289
                        delete doc;
 
290
                }
 
291
        }
 
292
}
 
293