Only store editors if they match criterias to not be hidden from start, it makes...
[geeqie.git] / src / editors.c
1 /*
2  * Geeqie
3  * (C) 2006 John Ellis
4  * Copyright (C) 2008 - 2009 The Geeqie Team
5  *
6  * Author: John Ellis
7  *
8  * This software is released under the GNU General Public License (GNU GPL).
9  * Please read the included file COPYING for more information.
10  * This software comes with no warranty of any kind, use at your own risk!
11  */
12
13
14 #include "main.h"
15 #include "editors.h"
16
17 #include "filedata.h"
18 #include "filefilter.h"
19 #include "misc.h"
20 #include "ui_fileops.h"
21 #include "ui_spinner.h"
22 #include "ui_utildlg.h"
23 #include "utilops.h"
24
25 #include <errno.h>
26
27
28 #define EDITOR_WINDOW_WIDTH 500
29 #define EDITOR_WINDOW_HEIGHT 300
30
31
32
33 typedef struct _EditorVerboseData EditorVerboseData;
34 struct _EditorVerboseData {
35         GenericDialog *gd;
36         GtkWidget *button_close;
37         GtkWidget *button_stop;
38         GtkWidget *text;
39         GtkWidget *progress;
40         GtkWidget *spinner;
41 };
42
43 typedef struct _EditorData EditorData;
44 struct _EditorData {
45         EditorFlags flags;
46         GPid pid;
47         GList *list;
48         gint count;
49         gint total;
50         gboolean stopping;
51         EditorVerboseData *vd;
52         EditorCallback callback;
53         gpointer data;
54         const EditorDescription *editor;
55 };
56
57
58 static void editor_verbose_window_progress(EditorData *ed, const gchar *text);
59 static EditorFlags editor_command_next_start(EditorData *ed);
60 static EditorFlags editor_command_next_finish(EditorData *ed, gint status);
61 static EditorFlags editor_command_done(EditorData *ed);
62
63 /*
64  *-----------------------------------------------------------------------------
65  * external editor routines
66  *-----------------------------------------------------------------------------
67  */
68
69 GHashTable *editors = NULL;
70
71 #ifdef G_KEY_FILE_DESKTOP_GROUP
72 #define DESKTOP_GROUP G_KEY_FILE_DESKTOP_GROUP
73 #else
74 #define DESKTOP_GROUP "Desktop Entry"
75 #endif
76
77 void editor_description_free(EditorDescription *editor)
78 {
79         if (!editor) return;
80         
81         g_free(editor->key);
82         g_free(editor->name);
83         g_free(editor->icon);
84         g_free(editor->exec);
85         g_free(editor->menu_path);
86         g_free(editor->hotkey);
87         string_list_free(editor->ext_list);
88         g_free(editor->file);
89         g_free(editor);
90 }
91
92 static GList *editor_mime_types_to_extensions(gchar **mime_types)
93 {
94         /* FIXME: this should be rewritten to use the shared mime database, as soon as we switch to gio */
95         
96         static const gchar *conv_table[][2] = {
97                 {"application/x-ufraw", "%raw"},
98                 {"image/*",             "*"},
99                 {"image/bmp",           ".bmp"},
100                 {"image/gif",           ".gif"},
101                 {"image/jpeg",          ".jpeg;.jpg"},
102                 {"image/jpg",           ".jpg;.jpeg"},
103                 {"image/pcx",           ".pcx"},
104                 {"image/png",           ".png"},
105                 {"image/svg",           ".svg"},
106                 {"image/svg+xml",       ".svg"},
107                 {"image/svg+xml-compressed",    ".svg"},        
108                 {"image/tiff",          ".tiff;.tif"},
109                 {"image/x-bmp",         ".bmp"},
110                 {"image/x-canon-crw",   ".crw"},
111                 {"image/x-cr2",         ".cr2"},
112                 {"image/x-dcraw",       "%raw"},
113                 {"image/x-ico",         ".ico"},
114                 {"image/x-mrw",         ".mrw"},
115                 {"image/x-MS-bmp",      ".bmp"},
116                 {"image/x-nef",         ".nef"},
117                 {"image/x-orf",         ".orf"},
118                 {"image/x-pcx",         ".pcx"},
119                 {"image/xpm",           ".xpm"},
120                 {"image/x-png",         ".png"},
121                 {"image/x-portable-anymap",     ".pam"},        
122                 {"image/x-portable-bitmap",     ".pbm"},
123                 {"image/x-portable-graymap",    ".pgm"},
124                 {"image/x-portable-pixmap",     ".ppm"},
125                 {"image/x-psd",         ".psd"},
126                 {"image/x-raf",         ".raf"},
127                 {"image/x-sgi",         ".sgi"},
128                 {"image/x-tga",         ".tga"},
129                 {"image/x-xbitmap",     ".xbm"},
130                 {"image/x-xcf",         ".xcf"},
131                 {"image/x-xpixmap",     ".xpm"},
132                 {"image/x-x3f",         ".x3f"},
133                 {NULL, NULL}};
134         
135         gint i, j;
136         GList *list = NULL;
137         
138         for (i = 0; mime_types[i]; i++) 
139                 for (j = 0; conv_table[j][0]; j++)
140                         if (strcmp(mime_types[i], conv_table[j][0]) == 0)
141                                 list = g_list_concat(list, filter_to_list(conv_table[j][1]));
142         
143         return list;
144 }
145
146 static gboolean editor_read_desktop_file(const gchar *path)
147 {
148         GKeyFile *key_file;
149         EditorDescription *editor;
150         gchar *extensions;
151         gchar *type;
152         const gchar *key = filename_from_path(path);
153         gchar **categories, **only_show_in, **not_show_in;
154         gchar *try_exec;
155         gboolean hidden = FALSE;
156
157         if (g_hash_table_lookup(editors, key)) return FALSE; /* the file found earlier wins */
158         
159         key_file = g_key_file_new();
160         if (!g_key_file_load_from_file(key_file, path, 0, NULL))
161                 {
162                 g_key_file_free(key_file);
163                 return FALSE;
164                 }
165
166         type = g_key_file_get_string(key_file, DESKTOP_GROUP, "Type", NULL);
167         if (!type || strcmp(type, "Application") != 0)
168                 {
169                 /* We only consider desktop entries of Application type */
170                 return FALSE;
171                 }
172         
173         if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "Hidden", NULL)
174             || g_key_file_get_boolean(key_file, DESKTOP_GROUP, "NoDisplay", NULL))
175                 {
176                 hidden = TRUE;
177                 }
178
179         categories = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "Categories", NULL, NULL);
180         if (categories)
181                 {
182                 gboolean found = FALSE;
183                 gint i;
184                 for (i = 0; categories[i]; i++) 
185                         /* IMHO "Graphics" is exactly the category that we are interested in, so this does not have to be configurable */
186                         if (strcmp(categories[i], "Graphics") == 0 ||
187                             strcmp(categories[i], "X-Geeqie") == 0) 
188                                 {
189                                 found = TRUE;
190                                 break;
191                                 }
192                 if (!found) hidden = TRUE;
193                 g_strfreev(categories);
194                 }
195         else
196                 {
197                 hidden = TRUE;
198                 }
199
200         only_show_in = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "OnlyShowIn", NULL, NULL);
201         if (only_show_in)
202                 {
203                 gboolean found = FALSE;
204                 gint i;
205                 for (i = 0; only_show_in[i]; i++) 
206                         if (strcmp(only_show_in[i], "X-Geeqie") == 0)
207                                 {
208                                 found = TRUE;
209                                 break;
210                                 }
211                 if (!found) hidden = TRUE;
212                 g_strfreev(only_show_in);
213                 }
214
215         not_show_in = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "NotShowIn", NULL, NULL);
216         if (not_show_in)
217                 {
218                 gboolean found = FALSE;
219                 gint i;
220                 for (i = 0; not_show_in[i]; i++) 
221                         if (strcmp(not_show_in[i], "X-Geeqie") == 0)
222                                 {
223                                 found = TRUE;
224                                 break;
225                                 }
226                 if (found) hidden = TRUE;
227                 g_strfreev(not_show_in);
228                 }
229                 
230         try_exec = g_key_file_get_string(key_file, DESKTOP_GROUP, "TryExec", NULL);
231         if (try_exec && !editor->hidden)
232                 {
233                 gchar *try_exec_res = g_find_program_in_path(try_exec);
234                 if (!try_exec_res) hidden = TRUE;
235                 g_free(try_exec_res);
236                 g_free(try_exec);
237                 }
238                 
239         if (hidden) 
240                 {
241                 /* hidden editors will be deleted, no need to parse the rest */
242                 g_key_file_free(key_file);
243                 return FALSE;
244                 }
245
246         editor = g_new0(EditorDescription, 1);
247         
248         editor->key = g_strdup(key);
249         editor->file = g_strdup(path);
250         editor->hidden = hidden;
251
252         g_hash_table_insert(editors, editor->key, editor);
253
254         editor->name = g_key_file_get_locale_string(key_file, DESKTOP_GROUP, "Name", NULL, NULL);
255         editor->icon = g_key_file_get_string(key_file, DESKTOP_GROUP, "Icon", NULL);
256
257         editor->exec = g_key_file_get_string(key_file, DESKTOP_GROUP, "Exec", NULL);
258         
259         /* we take only editors that accept parameters, FIXME: the test can be improved */
260         if (!strchr(editor->exec, '%')) editor->hidden = TRUE; 
261         
262         editor->menu_path = g_key_file_get_string(key_file, DESKTOP_GROUP, "X-Geeqie-Menu-Path", NULL);
263         if (!editor->menu_path) editor->menu_path = g_strdup("EditMenu/ExternalMenu");
264         
265         editor->hotkey = g_key_file_get_string(key_file, DESKTOP_GROUP, "X-Geeqie-Hotkey", NULL);
266
267         extensions = g_key_file_get_string(key_file, DESKTOP_GROUP, "X-Geeqie-File-Extensions", NULL);
268         if (extensions)
269                 editor->ext_list = filter_to_list(extensions);
270         else
271                 {
272                 gchar **mime_types = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "MimeType", NULL, NULL);
273                 if (mime_types)
274                         {
275                         editor->ext_list = editor_mime_types_to_extensions(mime_types);
276                         g_strfreev(mime_types);
277                         if (!editor->ext_list) editor->hidden = TRUE; 
278                         }
279                 }
280                 
281         if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "X-Geeqie-Keep-Fullscreen", NULL)) editor->flags |= EDITOR_KEEP_FS;
282         if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "X-Geeqie-Verbose", NULL)) editor->flags |= EDITOR_VERBOSE;
283         if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "X-Geeqie-Verbose-Multi", NULL)) editor->flags |= EDITOR_VERBOSE_MULTI;
284         if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "X-Geeqie-Filter", NULL)) editor->flags |= EDITOR_DEST;
285         if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "Terminal", NULL)) editor->flags |= EDITOR_TERMINAL;
286         
287         editor->flags |= editor_command_parse(editor, NULL, NULL);
288         g_key_file_free(key_file);
289         
290         return TRUE;    
291 }
292
293 static gboolean editor_remove_desktop_file_cb(gpointer key, gpointer value, gpointer user_data)
294 {
295         EditorDescription *editor = value;
296         return editor->hidden;
297 }
298
299 static void editor_read_desktop_dir(const gchar *path)
300 {
301         DIR *dp;
302         struct dirent *dir;
303         gchar *pathl;
304
305         pathl = path_from_utf8(path);
306         dp = opendir(pathl);
307         g_free(pathl);
308         if (!dp)
309                 {
310                 /* dir not found */
311                 return;
312                 }
313         while ((dir = readdir(dp)) != NULL)
314                 {
315                 gchar *namel = dir->d_name;
316                 
317                 if (g_str_has_suffix(namel, ".desktop"))
318                         {
319                         gchar *name = path_to_utf8(namel);
320                         gchar *dpath = g_build_filename(path, name, NULL);
321                         editor_read_desktop_file(dpath);
322                         g_free(dpath);
323                         g_free(name);
324                         }       
325                 }
326         closedir(dp);
327 }
328
329 void editor_load_descriptions(void)
330 {
331         gchar *path;
332         gchar *xdg_data_dirs;
333         gchar *all_dirs;
334         gchar **split_dirs;
335         gint i;
336         
337         if (!editors)
338                 {
339                 editors = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify)editor_description_free);
340                 }
341
342         xdg_data_dirs = getenv("XDG_DATA_DIRS");
343         if (xdg_data_dirs && xdg_data_dirs[0])
344                 xdg_data_dirs = path_to_utf8(xdg_data_dirs);
345         else
346                 xdg_data_dirs = g_strdup("/usr/share");
347         
348         all_dirs = g_strconcat(get_rc_dir(), ":", GQ_APP_DIR, ":", xdg_data_home_get(), ":", xdg_data_dirs, NULL);
349         
350         g_free(xdg_data_dirs);
351
352         split_dirs = g_strsplit(all_dirs, ":", 0);
353         
354         g_free(all_dirs);
355
356         for (i = 0; split_dirs[i]; i++)
357                 {
358                 path = g_build_filename(split_dirs[i], "applications", NULL);
359                 editor_read_desktop_dir(path);
360                 g_free(path);
361                 }
362                 
363         g_strfreev(split_dirs);
364         
365         g_hash_table_foreach_remove(editors, editor_remove_desktop_file_cb, NULL);
366 }
367
368 static void editor_list_add_cb(gpointer key, gpointer value, gpointer data)
369 {
370         GList **listp = data;
371         EditorDescription *editor = value;
372         
373         /* do not show the special commands in any list, they are called explicitly */ 
374         if (strcmp(editor->key, CMD_COPY) == 0 ||
375             strcmp(editor->key, CMD_MOVE) == 0 ||  
376             strcmp(editor->key, CMD_RENAME) == 0 ||
377             strcmp(editor->key, CMD_DELETE) == 0 ||
378             strcmp(editor->key, CMD_FOLDER) == 0) return;
379
380         *listp = g_list_prepend(*listp, editor);
381 }
382
383 static gint editor_sort(gconstpointer a, gconstpointer b)
384 {
385         const EditorDescription *ea = a;
386         const EditorDescription *eb = b;
387         gint ret;
388         
389         ret = strcmp(ea->menu_path, eb->menu_path);
390         if (ret != 0) return ret;
391         
392         return g_utf8_collate(ea->name, eb->name);
393 }
394
395 GList *editor_list_get(void)
396 {
397         GList *editors_list = NULL;
398         g_hash_table_foreach(editors, editor_list_add_cb, &editors_list);
399         editors_list = g_list_sort(editors_list, editor_sort);
400
401         return editors_list;
402 }
403
404 /* ------------------------------ */
405
406
407 static void editor_verbose_data_free(EditorData *ed)
408 {
409         if (!ed->vd) return;
410         g_free(ed->vd);
411         ed->vd = NULL;
412 }
413
414 static void editor_data_free(EditorData *ed)
415 {
416         editor_verbose_data_free(ed);
417         g_free(ed);
418 }
419
420 static void editor_verbose_window_close(GenericDialog *gd, gpointer data)
421 {
422         EditorData *ed = data;
423
424         generic_dialog_close(gd);
425         editor_verbose_data_free(ed);
426         if (ed->pid == -1) editor_data_free(ed); /* the process has already terminated */
427 }
428
429 static void editor_verbose_window_stop(GenericDialog *gd, gpointer data)
430 {
431         EditorData *ed = data;
432         ed->stopping = TRUE;
433         ed->count = 0;
434         editor_verbose_window_progress(ed, _("stopping..."));
435 }
436
437 static void editor_verbose_window_enable_close(EditorVerboseData *vd)
438 {
439         vd->gd->cancel_cb = editor_verbose_window_close;
440
441         spinner_set_interval(vd->spinner, -1);
442         gtk_widget_set_sensitive(vd->button_stop, FALSE);
443         gtk_widget_set_sensitive(vd->button_close, TRUE);
444 }
445
446 static EditorVerboseData *editor_verbose_window(EditorData *ed, const gchar *text)
447 {
448         EditorVerboseData *vd;
449         GtkWidget *scrolled;
450         GtkWidget *hbox;
451         gchar *buf;
452
453         vd = g_new0(EditorVerboseData, 1);
454
455         vd->gd = file_util_gen_dlg(_("Edit command results"), "editor_results",
456                                    NULL, FALSE,
457                                    NULL, ed);
458         buf = g_strdup_printf(_("Output of %s"), text);
459         generic_dialog_add_message(vd->gd, NULL, buf, NULL);
460         g_free(buf);
461         vd->button_stop = generic_dialog_add_button(vd->gd, GTK_STOCK_STOP, NULL,
462                                                    editor_verbose_window_stop, FALSE);
463         gtk_widget_set_sensitive(vd->button_stop, FALSE);
464         vd->button_close = generic_dialog_add_button(vd->gd, GTK_STOCK_CLOSE, NULL,
465                                                     editor_verbose_window_close, TRUE);
466         gtk_widget_set_sensitive(vd->button_close, FALSE);
467
468         scrolled = gtk_scrolled_window_new(NULL, NULL);
469         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
470         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
471                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
472         gtk_box_pack_start(GTK_BOX(vd->gd->vbox), scrolled, TRUE, TRUE, 5);
473         gtk_widget_show(scrolled);
474
475         vd->text = gtk_text_view_new();
476         gtk_text_view_set_editable(GTK_TEXT_VIEW(vd->text), FALSE);
477         gtk_widget_set_size_request(vd->text, EDITOR_WINDOW_WIDTH, EDITOR_WINDOW_HEIGHT);
478         gtk_container_add(GTK_CONTAINER(scrolled), vd->text);
479         gtk_widget_show(vd->text);
480
481         hbox = gtk_hbox_new(FALSE, 0);
482         gtk_box_pack_start(GTK_BOX(vd->gd->vbox), hbox, FALSE, FALSE, 0);
483         gtk_widget_show(hbox);
484
485         vd->progress = gtk_progress_bar_new();
486         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(vd->progress), 0.0);
487         gtk_box_pack_start(GTK_BOX(hbox), vd->progress, TRUE, TRUE, 0);
488         gtk_widget_show(vd->progress);
489
490         vd->spinner = spinner_new(NULL, SPINNER_SPEED);
491         gtk_box_pack_start(GTK_BOX(hbox), vd->spinner, FALSE, FALSE, 0);
492         gtk_widget_show(vd->spinner);
493
494         gtk_widget_show(vd->gd->dialog);
495
496         ed->vd = vd;
497         return vd;
498 }
499
500 static void editor_verbose_window_fill(EditorVerboseData *vd, gchar *text, gint len)
501 {
502         GtkTextBuffer *buffer;
503         GtkTextIter iter;
504
505         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(vd->text));
506         gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
507         gtk_text_buffer_insert(buffer, &iter, text, len);
508 }
509
510 static void editor_verbose_window_progress(EditorData *ed, const gchar *text)
511 {
512         if (!ed->vd) return;
513
514         if (ed->total)
515                 {
516                 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ed->vd->progress), (gdouble)ed->count / ed->total);
517                 }
518
519         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(ed->vd->progress), (text) ? text : "");
520 }
521
522 static gboolean editor_verbose_io_cb(GIOChannel *source, GIOCondition condition, gpointer data)
523 {
524         EditorData *ed = data;
525         gchar buf[512];
526         gsize count;
527
528         if (condition & G_IO_IN)
529                 {
530                 while (g_io_channel_read_chars(source, buf, sizeof(buf), &count, NULL) == G_IO_STATUS_NORMAL)
531                         {
532                         if (!g_utf8_validate(buf, count, NULL))
533                                 {
534                                 gchar *utf8;
535
536                                 utf8 = g_locale_to_utf8(buf, count, NULL, NULL, NULL);
537                                 if (utf8)
538                                         {
539                                         editor_verbose_window_fill(ed->vd, utf8, -1);
540                                         g_free(utf8);
541                                         }
542                                 else
543                                         {
544                                         editor_verbose_window_fill(ed->vd, "Error converting text to valid utf8\n", -1);
545                                         }
546                                 }
547                         else
548                                 {
549                                 editor_verbose_window_fill(ed->vd, buf, count);
550                                 }
551                         }
552                 }
553
554         if (condition & (G_IO_ERR | G_IO_HUP))
555                 {
556                 g_io_channel_shutdown(source, TRUE, NULL);
557                 return FALSE;
558                 }
559
560         return TRUE;
561 }
562
563 typedef enum {
564         PATH_FILE,
565         PATH_FILE_URL,
566         PATH_DEST
567 } PathType;
568
569
570 static gchar *editor_command_path_parse(const FileData *fd, PathType type, const EditorDescription *editor)
571 {
572         GString *string;
573         gchar *pathl;
574         const gchar *p = NULL;
575
576         string = g_string_new("");
577
578         if (type == PATH_FILE || type == PATH_FILE_URL)
579                 {
580                 GList *work = editor->ext_list;
581
582                 if (!work)
583                         p = fd->path;
584                 else
585                         {
586                         while (work)
587                                 {
588                                 GList *work2;
589                                 gchar *ext = work->data;
590                                 work = work->next;
591
592                                 if (strcmp(ext, "*") == 0 ||
593                                     g_ascii_strcasecmp(ext, fd->extension) == 0)
594                                         {
595                                         p = fd->path;
596                                         break;
597                                         }
598
599                                 work2 = fd->sidecar_files;
600                                 while (work2)
601                                         {
602                                         FileData *sfd = work2->data;
603                                         work2 = work2->next;
604
605                                         if (g_ascii_strcasecmp(ext, sfd->extension) == 0)
606                                                 {
607                                                 p = sfd->path;
608                                                 break;
609                                                 }
610                                         }
611                                 if (p) break;
612                                 }
613                         if (!p) return NULL;
614                         }
615                 }
616         else if (type == PATH_DEST)
617                 {
618                 if (fd->change && fd->change->dest)
619                         p = fd->change->dest;
620                 else
621                         p = "";
622                 }
623
624         while (*p != '\0')
625                 {
626                 /* must escape \, ", `, and $ to avoid problems,
627                  * we assume system shell supports bash-like escaping
628                  */
629                 if (strchr("\\\"`$", *p) != NULL)
630                         {
631                         string = g_string_append_c(string, '\\');
632                         }
633                 string = g_string_append_c(string, *p);
634                 p++;
635                 }
636
637         if (type == PATH_FILE_URL) g_string_prepend(string, "file://");
638         pathl = path_from_utf8(string->str);
639         g_string_free(string, TRUE);
640
641         if (pathl && !pathl[0]) /* empty string case */
642                 {
643                 g_free(pathl);
644                 pathl = NULL;
645                 }
646
647         return pathl;
648 }
649
650
651 EditorFlags editor_command_parse(const EditorDescription *editor, GList *list, gchar **output)
652 {
653         EditorFlags flags = 0;
654         const gchar *p;
655         GString *result = NULL;
656
657         if (output)
658                 result = g_string_new("");
659
660         if (editor->exec[0] == '\0')
661                 {
662                 flags |= EDITOR_ERROR_EMPTY;
663                 goto err;
664                 }
665         
666         p = editor->exec;
667         /* skip leading whitespaces if any */
668         while (g_ascii_isspace(*p)) p++;
669
670         /* command */
671
672         while (*p)
673                 {
674                 if (*p != '%')
675                         {
676                         if (output) result = g_string_append_c(result, *p);
677                         }
678                 else /* *p == '%' */
679                         {
680                         gchar *pathl = NULL;
681
682                         p++;
683
684                         switch (*p)
685                                 {
686                                 case 'f': /* single file */
687                                 case 'u': /* single url */
688                                         flags |= EDITOR_FOR_EACH;
689                                         if (flags & EDITOR_SINGLE_COMMAND)
690                                                 {
691                                                 flags |= EDITOR_ERROR_INCOMPATIBLE;
692                                                 goto err;
693                                                 }
694                                         if (list)
695                                                 {
696                                                 /* use the first file from the list */
697                                                 if (!list->data)
698                                                         {
699                                                         flags |= EDITOR_ERROR_NO_FILE;
700                                                         goto err;
701                                                         }
702                                                 pathl = editor_command_path_parse((FileData *)list->data,
703                                                                                   (*p == 'f') ? PATH_FILE : PATH_FILE_URL,
704                                                                                   editor);
705                                                 if (!pathl)
706                                                         {
707                                                         flags |= EDITOR_ERROR_NO_FILE;
708                                                         goto err;
709                                                         }
710                                                 if (output)
711                                                         {
712                                                         result = g_string_append_c(result, '"');
713                                                         result = g_string_append(result, pathl);
714                                                         result = g_string_append_c(result, '"');
715                                                         }
716                                                 g_free(pathl);
717                                                 }
718                                         break;
719
720                                 case 'F':
721                                 case 'U':
722                                         flags |= EDITOR_SINGLE_COMMAND;
723                                         if (flags & (EDITOR_FOR_EACH | EDITOR_DEST))
724                                                 {
725                                                 flags |= EDITOR_ERROR_INCOMPATIBLE;
726                                                 goto err;
727                                                 }
728
729                                         if (list)
730                                                 {
731                                                 /* use whole list */
732                                                 GList *work = list;
733                                                 gboolean ok = FALSE;
734
735                                                 while (work)
736                                                         {
737                                                         FileData *fd = work->data;
738                                                         pathl = editor_command_path_parse(fd, (*p == 'F') ? PATH_FILE : PATH_FILE_URL, editor);
739                                                         if (pathl)
740                                                                 {
741                                                                 ok = TRUE;
742
743                                                                 if (output)
744                                                                         {
745                                                                         ok = TRUE;
746                                                                         if (work != list) g_string_append_c(result, ' ');
747                                                                         result = g_string_append_c(result, '"');
748                                                                         result = g_string_append(result, pathl);
749                                                                         result = g_string_append_c(result, '"');
750                                                                         }
751                                                                 g_free(pathl);
752                                                                 }
753                                                         work = work->next;
754                                                         }
755                                                 if (!ok)
756                                                         {
757                                                         flags |= EDITOR_ERROR_NO_FILE;
758                                                         goto err;
759                                                         }
760                                                 }
761                                         break;
762                                 case 'i':
763                                         if (output)
764                                                 {
765                                                 result = g_string_append(result, editor->icon);
766                                                 }
767                                         break;
768                                 case 'c':
769                                         if (output)
770                                                 {
771                                                 result = g_string_append(result, editor->name);
772                                                 }
773                                         break;
774                                 case 'k':
775                                         if (output)
776                                                 {
777                                                 result = g_string_append(result, editor->file);
778                                                 }
779                                         break;
780                                 case '%':
781                                         /* %% = % escaping */
782                                         if (output) result = g_string_append_c(result, *p);
783                                         break;
784                                 case 'd':
785                                 case 'D':
786                                 case 'n':
787                                 case 'N':
788                                 case 'v':
789                                 case 'm':
790                                         /* deprecated according to spec, ignore */
791                                         break;
792                                 default:
793                                         flags |= EDITOR_ERROR_SYNTAX;
794                                         goto err;
795                                 }
796                         }
797                 p++;
798                 }
799
800         if (output) *output = g_string_free(result, FALSE);
801         return flags;
802
803
804 err:
805         if (output)
806                 {
807                 g_string_free(result, TRUE);
808                 *output = NULL;
809                 }
810         return flags;
811 }
812
813
814 static void editor_child_exit_cb(GPid pid, gint status, gpointer data)
815 {
816         EditorData *ed = data;
817         g_spawn_close_pid(pid);
818         ed->pid = -1;
819
820         editor_command_next_finish(ed, status);
821 }
822
823
824 static EditorFlags editor_command_one(const EditorDescription *editor, GList *list, EditorData *ed)
825 {
826         gchar *command;
827         FileData *fd = list->data;
828         GPid pid;
829         gint standard_output;
830         gint standard_error;
831         gboolean ok;
832
833         ed->pid = -1;
834         ed->flags = editor->flags;
835         ed->flags |= editor_command_parse(editor, list, &command);
836
837         ok = !EDITOR_ERRORS(ed->flags);
838
839         if (ok)
840                 {
841                 ok = (options->shell.path && *options->shell.path);
842                 if (!ok) log_printf("ERROR: empty shell command\n");
843                         
844                 if (ok)
845                         {
846                         ok = (access(options->shell.path, X_OK) == 0);
847                         if (!ok) log_printf("ERROR: cannot execute shell command '%s'\n", options->shell.path);
848                         }
849
850                 if (!ok) ed->flags |= EDITOR_ERROR_CANT_EXEC;
851                 }
852
853         if (ok)
854                 {
855                 gchar *working_directory;
856                 gchar *args[4];
857                 guint n = 0;
858
859                 working_directory = remove_level_from_path(fd->path);
860                 args[n++] = options->shell.path;
861                 if (options->shell.options && *options->shell.options)
862                         args[n++] = options->shell.options;
863                 args[n++] = command;
864                 args[n] = NULL;
865
866                 if ((ed->flags & EDITOR_DEST) && fd->change && fd->change->dest) /* FIXME: error handling */
867                         {
868                         g_setenv("GEEQIE_DESTINATION", fd->change->dest, TRUE);
869                         }
870                 else
871                         {
872                         g_unsetenv("GEEQIE_DESTINATION");
873                         }
874
875                 ok = g_spawn_async_with_pipes(working_directory, args, NULL,
876                                       G_SPAWN_DO_NOT_REAP_CHILD, /* GSpawnFlags */
877                                       NULL, NULL,
878                                       &pid,
879                                       NULL,
880                                       ed->vd ? &standard_output : NULL,
881                                       ed->vd ? &standard_error : NULL,
882                                       NULL);
883                 
884                 g_free(working_directory);
885
886                 if (!ok) ed->flags |= EDITOR_ERROR_CANT_EXEC;
887                 }
888
889         if (ok)
890                 {
891                 g_child_watch_add(pid, editor_child_exit_cb, ed);
892                 ed->pid = pid;
893                 }
894
895         if (ed->vd)
896                 {
897                 if (!ok)
898                         {
899                         gchar *buf;
900
901                         buf = g_strdup_printf(_("Failed to run command:\n%s\n"), editor->file);
902                         editor_verbose_window_fill(ed->vd, buf, strlen(buf));
903                         g_free(buf);
904
905                         }
906                 else
907                         {
908                         GIOChannel *channel_output;
909                         GIOChannel *channel_error;
910
911                         channel_output = g_io_channel_unix_new(standard_output);
912                         g_io_channel_set_flags(channel_output, G_IO_FLAG_NONBLOCK, NULL);
913                         g_io_channel_set_encoding(channel_output, NULL, NULL);
914
915                         g_io_add_watch_full(channel_output, G_PRIORITY_HIGH, G_IO_IN | G_IO_ERR | G_IO_HUP,
916                                             editor_verbose_io_cb, ed, NULL);
917                         g_io_channel_unref(channel_output);
918
919                         channel_error = g_io_channel_unix_new(standard_error);
920                         g_io_channel_set_flags(channel_error, G_IO_FLAG_NONBLOCK, NULL);
921                         g_io_channel_set_encoding(channel_error, NULL, NULL);
922
923                         g_io_add_watch_full(channel_error, G_PRIORITY_HIGH, G_IO_IN | G_IO_ERR | G_IO_HUP,
924                                             editor_verbose_io_cb, ed, NULL);
925                         g_io_channel_unref(channel_error);
926                         }
927                 }
928
929         g_free(command);
930
931         return EDITOR_ERRORS(ed->flags);
932 }
933
934 static EditorFlags editor_command_next_start(EditorData *ed)
935 {
936         if (ed->vd) editor_verbose_window_fill(ed->vd, "\n", 1);
937
938         if (ed->list && ed->count < ed->total)
939                 {
940                 FileData *fd;
941                 EditorFlags error;
942
943                 fd = ed->list->data;
944
945                 if (ed->vd)
946                         {
947                         if (ed->flags & EDITOR_FOR_EACH)
948                                 editor_verbose_window_progress(ed, fd->path);
949                         else
950                                 editor_verbose_window_progress(ed, _("running..."));
951                         }
952                 ed->count++;
953
954                 error = editor_command_one(ed->editor, ed->list, ed);
955                 if (!error && ed->vd)
956                         {
957                         gtk_widget_set_sensitive(ed->vd->button_stop, (ed->list != NULL) );
958                         if (ed->flags & EDITOR_FOR_EACH)
959                                 {
960                                 editor_verbose_window_fill(ed->vd, fd->path, strlen(fd->path));
961                                 editor_verbose_window_fill(ed->vd, "\n", 1);
962                                 }
963                         }
964
965                 if (!error)
966                         return 0;
967                 
968                 /* command was not started, call the finish immediately */
969                 return editor_command_next_finish(ed, 0);
970                 }
971
972         /* everything is done */
973         return editor_command_done(ed);
974 }
975
976 static EditorFlags editor_command_next_finish(EditorData *ed, gint status)
977 {
978         gint cont = ed->stopping ? EDITOR_CB_SKIP : EDITOR_CB_CONTINUE;
979
980         if (status)
981                 ed->flags |= EDITOR_ERROR_STATUS;
982
983         if (ed->flags & EDITOR_FOR_EACH)
984                 {
985                 /* handle the first element from the list */
986                 GList *fd_element = ed->list;
987
988                 ed->list = g_list_remove_link(ed->list, fd_element);
989                 if (ed->callback)
990                         {
991                         cont = ed->callback(ed->list ? ed : NULL, ed->flags, fd_element, ed->data);
992                         if (ed->stopping && cont == EDITOR_CB_CONTINUE) cont = EDITOR_CB_SKIP;
993                         }
994                 filelist_free(fd_element);
995                 }
996         else
997                 {
998                 /* handle whole list */
999                 if (ed->callback)
1000                         cont = ed->callback(NULL, ed->flags, ed->list, ed->data);
1001                 filelist_free(ed->list);
1002                 ed->list = NULL;
1003                 }
1004
1005         switch (cont)
1006                 {
1007                 case EDITOR_CB_SUSPEND:
1008                         return EDITOR_ERRORS(ed->flags);
1009                 case EDITOR_CB_SKIP:
1010                         return editor_command_done(ed);
1011                 }
1012         
1013         return editor_command_next_start(ed);
1014 }
1015
1016 static EditorFlags editor_command_done(EditorData *ed)
1017 {
1018         EditorFlags flags;
1019
1020         if (ed->vd)
1021                 {
1022                 if (ed->count == ed->total)
1023                         {
1024                         editor_verbose_window_progress(ed, _("done"));
1025                         }
1026                 else
1027                         {
1028                         editor_verbose_window_progress(ed, _("stopped by user"));
1029                         }
1030                 editor_verbose_window_enable_close(ed->vd);
1031                 }
1032
1033         /* free the not-handled items */
1034         if (ed->list)
1035                 {
1036                 ed->flags |= EDITOR_ERROR_SKIPPED;
1037                 if (ed->callback) ed->callback(NULL, ed->flags, ed->list, ed->data);
1038                 filelist_free(ed->list);
1039                 ed->list = NULL;
1040                 }
1041
1042         ed->count = 0;
1043
1044         flags = EDITOR_ERRORS(ed->flags);
1045
1046         if (!ed->vd) editor_data_free(ed);
1047
1048         return flags;
1049 }
1050
1051 void editor_resume(gpointer ed)
1052 {
1053         editor_command_next_start(ed);
1054 }
1055
1056 void editor_skip(gpointer ed)
1057 {
1058         editor_command_done(ed);
1059 }
1060
1061 static EditorFlags editor_command_start(const EditorDescription *editor, const gchar *text, GList *list, EditorCallback cb, gpointer data)
1062 {
1063         EditorData *ed;
1064         EditorFlags flags = editor->flags;
1065
1066         if (EDITOR_ERRORS(flags)) return EDITOR_ERRORS(flags);
1067
1068         ed = g_new0(EditorData, 1);
1069         ed->list = filelist_copy(list);
1070         ed->flags = flags;
1071         ed->editor = editor;
1072         ed->total = (flags & EDITOR_SINGLE_COMMAND) ? 1 : g_list_length(list);
1073         ed->callback = cb;
1074         ed->data =  data;
1075
1076         if ((flags & EDITOR_VERBOSE_MULTI) && list && list->next)
1077                 flags |= EDITOR_VERBOSE;
1078
1079         if (flags & EDITOR_VERBOSE)
1080                 editor_verbose_window(ed, text);
1081
1082         editor_command_next_start(ed);
1083         /* errors from editor_command_next_start will be handled via callback */
1084         return EDITOR_ERRORS(flags);
1085 }
1086
1087 gboolean is_valid_editor_command(const gchar *key)
1088 {
1089         if (!key) return FALSE;
1090         return g_hash_table_lookup(editors, key) != NULL;
1091 }
1092
1093 EditorFlags start_editor_from_filelist_full(const gchar *key, GList *list, EditorCallback cb, gpointer data)
1094 {
1095         EditorFlags error;
1096         EditorDescription *editor;
1097         if (!key) return FALSE;
1098         
1099         editor = g_hash_table_lookup(editors, key);
1100
1101         if (!list) return FALSE;
1102         if (!editor) return FALSE;
1103
1104         error = editor_command_start(editor, editor->name, list, cb, data);
1105
1106         if (EDITOR_ERRORS(error))
1107                 {
1108                 gchar *text = g_strdup_printf(_("%s\n\"%s\""), editor_get_error_str(error), editor->file);
1109                 
1110                 file_util_warning_dialog(_("Invalid editor command"), text, GTK_STOCK_DIALOG_ERROR, NULL);
1111                 g_free(text);
1112                 }
1113
1114         return error;
1115 }
1116
1117 EditorFlags start_editor_from_filelist(const gchar *key, GList *list)
1118 {
1119         return start_editor_from_filelist_full(key, list,  NULL, NULL);
1120 }
1121
1122 EditorFlags start_editor_from_file_full(const gchar *key, FileData *fd, EditorCallback cb, gpointer data)
1123 {
1124         GList *list;
1125         EditorFlags error;
1126
1127         if (!fd) return FALSE;
1128
1129         list = g_list_append(NULL, fd);
1130         error = start_editor_from_filelist_full(key, list, cb, data);
1131         g_list_free(list);
1132         return error;
1133 }
1134
1135 EditorFlags start_editor_from_file(const gchar *key, FileData *fd)
1136 {
1137         return start_editor_from_file_full(key, fd, NULL, NULL);
1138 }
1139
1140 gboolean editor_window_flag_set(const gchar *key)
1141 {
1142         EditorDescription *editor;
1143         if (!key) return TRUE;
1144         
1145         editor = g_hash_table_lookup(editors, key);
1146         if (!editor) return TRUE;
1147
1148         return !!(editor->flags & EDITOR_KEEP_FS);
1149 }
1150
1151 gboolean editor_is_filter(const gchar *key)
1152 {
1153         EditorDescription *editor;
1154         if (!key) return TRUE;
1155         
1156         editor = g_hash_table_lookup(editors, key);
1157         if (!editor) return TRUE;
1158
1159         return !!(editor->flags & EDITOR_DEST);
1160 }
1161
1162 const gchar *editor_get_error_str(EditorFlags flags)
1163 {
1164         if (flags & EDITOR_ERROR_EMPTY) return _("Editor template is empty.");
1165         if (flags & EDITOR_ERROR_SYNTAX) return _("Editor template has incorrect syntax.");
1166         if (flags & EDITOR_ERROR_INCOMPATIBLE) return _("Editor template uses incompatible macros.");
1167         if (flags & EDITOR_ERROR_NO_FILE) return _("Can't find matching file type.");
1168         if (flags & EDITOR_ERROR_CANT_EXEC) return _("Can't execute external editor.");
1169         if (flags & EDITOR_ERROR_STATUS) return _("External editor returned error status.");
1170         if (flags & EDITOR_ERROR_SKIPPED) return _("File was skipped.");
1171         return _("Unknown error.");
1172 }
1173
1174 const gchar *editor_get_name(const gchar *key)
1175 {
1176         EditorDescription *editor = g_hash_table_lookup(editors, key);
1177
1178         if (!editor) return NULL;
1179
1180         return editor->name;
1181 }
1182 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */