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