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