9
using TeeJee.FileSystem;
10
using TeeJee.JsonHelper;
11
using TeeJee.ProcessHelper;
12
using TeeJee.GtkHelper;
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;
30
public static Gee.HashMap<string,ThunarMenuEntry> action_list;
31
public static string user_home;
33
public static void query_actions(string _user_home){
34
log_debug("ThunarMenuEntry.query_actions()");
36
action_list = new Gee.HashMap<string,ThunarMenuEntry>();
37
user_home = _user_home;
39
var xml_path = "%s/.config/Thunar/uca.xml".printf(user_home);
41
if (!file_exists(xml_path)){
45
xml_fix_invalid_declaration(xml_path);
47
// Parse the document from path
48
Xml.Doc* doc = Xml.Parser.parse_file (xml_path);
50
log_error("File not found or permission denied: %s".printf(xml_path));
54
Xml.Node* root = doc->get_root_element ();
56
log_error("Root element missing in xml: %s".printf(xml_path));
61
if (root->name != "actions") {
62
log_error("Unexpected element '%s' in xml: %s".printf(root->name, xml_path));
67
for (Xml.Node* action = root->children; action != null; action = action->next) {
68
if (action->type != Xml.ElementType.ELEMENT_NODE) {
72
if (action->name != "action") {
76
var item = new ThunarMenuEntry();
78
for (Xml.Node* node = action->children; node != null; node = node->next) {
79
if (node->type != Xml.ElementType.ELEMENT_NODE) {
85
item.name = node->get_content();
88
item.icon = node->get_content();
91
item.id = node->get_content();
94
item.command = node->get_content();
97
item.description = node->get_content();
100
item.patterns = node->get_content();
103
item.show_for_directories = true;
106
item.show_for_audio_files = true;
109
item.show_for_video_files = true;
112
item.show_for_image_files = true;
115
item.show_for_text_files = true;
118
item.show_for_other_files = true;
123
action_list[item.id] = item;
129
public static void xml_fix_invalid_declaration(string xml_file){
130
if (!file_exists(xml_file)){
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);
145
foreach(var action in action_list.values){
146
if ((action.id == id)||(action.command == command)){
153
var xml_path = "%s/.config/Thunar/uca.xml".printf(user_home);
155
if (!file_exists(xml_path)){
159
xml_fix_invalid_declaration(xml_path);
161
// Parse the document from path
162
Xml.Doc* doc = Xml.Parser.parse_file (xml_path);
164
log_error("File not found or permission denied: %s".printf(xml_path));
168
Xml.Node* root = doc->get_root_element ();
170
log_error("Root element missing in xml: %s".printf(xml_path));
175
if (root->name != "actions") {
176
log_error("Unexpected element '%s' in xml: %s".printf(root->name, xml_path));
181
for (Xml.Node* action = root->children; action != null; action = action->next) {
182
if (action->type != Xml.ElementType.ELEMENT_NODE) {
186
if (action->name != "action") {
191
Xml.Node* action = root->new_text_child (null, "action", "");
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);
200
if (show_for_directories){
201
action->new_text_child (null, "directories", "");
203
if (show_for_audio_files){
204
action->new_text_child (null, "audio-files", "");
206
if (show_for_video_files){
207
action->new_text_child (null, "video-files", "");
209
if (show_for_image_files){
210
action->new_text_child (null, "image-files", "");
212
if (show_for_text_files){
213
action->new_text_child (null, "text-files", "");
215
if (show_for_other_files){
216
action->new_text_child (null, "other-files", "");
219
doc->save_file(xml_path);
225
public void remove(){
227
foreach(var action in action_list.values){
228
if (action.id == id){
235
var xml_path = "%s/.config/Thunar/uca.xml".printf(user_home);
237
if (!file_exists(xml_path)){
241
xml_fix_invalid_declaration(xml_path);
243
// Parse the document from path
244
Xml.Doc* doc = Xml.Parser.parse_file (xml_path);
246
log_error("File not found or permission denied: %s".printf(xml_path));
250
Xml.Node* root = doc->get_root_element ();
252
log_error("Root element missing in xml: %s".printf(xml_path));
257
if (root->name != "actions") {
258
log_error("Unexpected element '%s' in xml: %s".printf(root->name, xml_path));
263
for (Xml.Node* action = root->children; action != null; action = action->next) {
264
if (action->type != Xml.ElementType.ELEMENT_NODE) {
268
if (action->name != "action") {
272
for (Xml.Node* node = action->children; node != null; node = node->next) {
273
if (node->type != Xml.ElementType.ELEMENT_NODE) {
279
if (node->get_content().strip() == id){
287
doc->save_file(xml_path);