4 * Copyright (C) 2008 The Geeqie Team
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!
18 #include "filefilter.h"
20 #include "ui_fileops.h"
21 #include "ui_spinner.h"
22 #include "ui_utildlg.h"
27 #define EDITOR_WINDOW_WIDTH 500
28 #define EDITOR_WINDOW_HEIGHT 300
32 typedef struct _EditorVerboseData EditorVerboseData;
33 struct _EditorVerboseData {
35 GtkWidget *button_close;
36 GtkWidget *button_stop;
42 typedef struct _EditorData EditorData;
46 gchar *command_template;
51 EditorVerboseData *vd;
52 EditorCallback callback;
57 static Editor editor_slot_defaults[GQ_EDITOR_SLOTS] = {
58 { N_("The Gimp"), "gimp-remote %{.cr2;.crw;.nef;.raw;*}f" },
59 { N_("XV"), "xv %f" },
60 { N_("Xpaint"), "xpaint %f" },
61 { N_("UFraw"), "ufraw %{.cr2;.crw;.nef;.raw}p" },
62 { N_("Add XMP sidecar"), "%vFILE=%{.cr2;.crw;.nef;.raw}p;XMP=`echo \"$FILE\"|sed -e 's|\\.[^.]*$|.xmp|'`; exiftool -tagsfromfile \"$FILE\" \"$XMP\"" },
66 { N_("Rotate jpeg clockwise"), "%vif jpegtran -rotate 90 -copy all -outfile %{.jpg;.jpeg}p_tmp %{.jpg;.jpeg}p; then mv %{.jpg;.jpeg}p_tmp %{.jpg;.jpeg}p;else rm %{.jpg;.jpeg}p_tmp;fi" },
67 { N_("Rotate jpeg counterclockwise"), "%vif jpegtran -rotate 270 -copy all -outfile %{.jpg;.jpeg}p_tmp %{.jpg;.jpeg}p; then mv %{.jpg;.jpeg}p_tmp %{.jpg;.jpeg}p;else rm %{.jpg;.jpeg}p_tmp;fi" },
71 { N_("External Copy command"), "%vset -x;cp %p %d" },
72 { N_("External Move command"), "%vset -x;mv %p %d" },
73 { N_("External Rename command"), "%vset -x;mv %p %d" },
74 { N_("External Delete command"), NULL },
75 { N_("External New Folder command"), NULL },
77 { N_("External Copy command"), NULL },
78 { N_("External Move command"), NULL },
79 { N_("External Rename command"), NULL },
80 { N_("External Delete command"), NULL },
81 { N_("External New Folder command"), NULL },
85 static void editor_verbose_window_progress(EditorData *ed, const gchar *text);
86 static gint editor_command_next_start(EditorData *ed);
87 static gint editor_command_next_finish(EditorData *ed, gint status);
88 static gint editor_command_done(EditorData *ed);
91 *-----------------------------------------------------------------------------
92 * external editor routines
93 *-----------------------------------------------------------------------------
96 void editor_set_name(gint n, gchar *name)
98 if (n < 0 || n >= GQ_EDITOR_SLOTS) return;
100 g_free(options->editor[n].name);
102 options->editor[n].name = name ? utf8_validate_or_convert(name) : NULL;
105 void editor_set_command(gint n, gchar *command)
107 if (n < 0 || n >= GQ_EDITOR_SLOTS) return;
109 g_free(options->editor[n].command);
110 options->editor[n].command = command ? utf8_validate_or_convert(command) : NULL;
113 void editor_reset_defaults(void)
117 for (i = 0; i < GQ_EDITOR_SLOTS; i++)
119 editor_set_name(i, _(editor_slot_defaults[i].name));
120 editor_set_command(i, _(editor_slot_defaults[i].command));
124 static void editor_verbose_data_free(EditorData *ed)
131 static void editor_data_free(EditorData *ed)
133 editor_verbose_data_free(ed);
134 g_free(ed->command_template);
138 static void editor_verbose_window_close(GenericDialog *gd, gpointer data)
140 EditorData *ed = data;
142 generic_dialog_close(gd);
143 editor_verbose_data_free(ed);
144 if (ed->pid == -1) editor_data_free(ed); /* the process has already terminated */
147 static void editor_verbose_window_stop(GenericDialog *gd, gpointer data)
149 EditorData *ed = data;
152 editor_verbose_window_progress(ed, _("stopping..."));
155 static void editor_verbose_window_enable_close(EditorVerboseData *vd)
157 vd->gd->cancel_cb = editor_verbose_window_close;
159 spinner_set_interval(vd->spinner, -1);
160 gtk_widget_set_sensitive(vd->button_stop, FALSE);
161 gtk_widget_set_sensitive(vd->button_close, TRUE);
164 static EditorVerboseData *editor_verbose_window(EditorData *ed, const gchar *text)
166 EditorVerboseData *vd;
171 vd = g_new0(EditorVerboseData, 1);
173 vd->gd = file_util_gen_dlg(_("Edit command results"), GQ_WMCLASS, "editor_results",
176 buf = g_strdup_printf(_("Output of %s"), text);
177 generic_dialog_add_message(vd->gd, NULL, buf, NULL);
179 vd->button_stop = generic_dialog_add_button(vd->gd, GTK_STOCK_STOP, NULL,
180 editor_verbose_window_stop, FALSE);
181 gtk_widget_set_sensitive(vd->button_stop, FALSE);
182 vd->button_close = generic_dialog_add_button(vd->gd, GTK_STOCK_CLOSE, NULL,
183 editor_verbose_window_close, TRUE);
184 gtk_widget_set_sensitive(vd->button_close, FALSE);
186 scrolled = gtk_scrolled_window_new(NULL, NULL);
187 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
188 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
189 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
190 gtk_box_pack_start(GTK_BOX(vd->gd->vbox), scrolled, TRUE, TRUE, 5);
191 gtk_widget_show(scrolled);
193 vd->text = gtk_text_view_new();
194 gtk_text_view_set_editable(GTK_TEXT_VIEW(vd->text), FALSE);
195 gtk_widget_set_size_request(vd->text, EDITOR_WINDOW_WIDTH, EDITOR_WINDOW_HEIGHT);
196 gtk_container_add(GTK_CONTAINER(scrolled), vd->text);
197 gtk_widget_show(vd->text);
199 hbox = gtk_hbox_new(FALSE, 0);
200 gtk_box_pack_start(GTK_BOX(vd->gd->vbox), hbox, FALSE, FALSE, 0);
201 gtk_widget_show(hbox);
203 vd->progress = gtk_progress_bar_new();
204 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(vd->progress), 0.0);
205 gtk_box_pack_start(GTK_BOX(hbox), vd->progress, TRUE, TRUE, 0);
206 gtk_widget_show(vd->progress);
208 vd->spinner = spinner_new(NULL, SPINNER_SPEED);
209 gtk_box_pack_start(GTK_BOX(hbox), vd->spinner, FALSE, FALSE, 0);
210 gtk_widget_show(vd->spinner);
212 gtk_widget_show(vd->gd->dialog);
218 static void editor_verbose_window_fill(EditorVerboseData *vd, gchar *text, gint len)
220 GtkTextBuffer *buffer;
223 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(vd->text));
224 gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
225 gtk_text_buffer_insert(buffer, &iter, text, len);
228 static void editor_verbose_window_progress(EditorData *ed, const gchar *text)
234 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ed->vd->progress), (double)ed->count / ed->total);
237 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(ed->vd->progress), (text) ? text : "");
240 static gboolean editor_verbose_io_cb(GIOChannel *source, GIOCondition condition, gpointer data)
242 EditorData *ed = data;
246 if (condition & G_IO_IN)
248 while (g_io_channel_read_chars(source, buf, sizeof(buf), &count, NULL) == G_IO_STATUS_NORMAL)
250 if (!g_utf8_validate(buf, count, NULL))
254 utf8 = g_locale_to_utf8(buf, count, NULL, NULL, NULL);
257 editor_verbose_window_fill(ed->vd, utf8, -1);
262 editor_verbose_window_fill(ed->vd, "Error converting text to valid utf8\n", -1);
267 editor_verbose_window_fill(ed->vd, buf, count);
272 if (condition & (G_IO_ERR | G_IO_HUP))
274 g_io_channel_shutdown(source, TRUE, NULL);
287 static gchar *editor_command_path_parse(const FileData *fd, PathType type, const gchar *extensions)
291 const gchar *p = NULL;
293 string = g_string_new("");
295 if (type == PATH_FILE)
297 GList *ext_list = filter_to_list(extensions);
298 GList *work = ext_list;
307 gchar *ext = work->data;
310 if (strcmp(ext, "*") == 0 ||
311 strcasecmp(ext, fd->extension) == 0)
317 work2 = fd->sidecar_files;
320 FileData *sfd = work2->data;
323 if (strcasecmp(ext, sfd->extension) == 0)
331 string_list_free(ext_list);
335 else if (type == PATH_DEST)
337 if (fd->change && fd->change->dest)
338 p = fd->change->dest;
345 /* must escape \, ", `, and $ to avoid problems,
346 * we assume system shell supports bash-like escaping
348 if (strchr("\\\"`$", *p) != NULL)
350 string = g_string_append_c(string, '\\');
352 string = g_string_append_c(string, *p);
356 pathl = path_from_utf8(string->str);
357 g_string_free(string, TRUE);
364 * The supported macros for editor commands:
366 * %f first occurence replaced by quoted sequence of filenames, command is run once.
367 * only one occurence of this macro is supported.
368 * ([ls %f] results in [ls "file1" "file2" ... "lastfile"])
369 * %p command is run for each filename in turn, each instance replaced with single filename.
370 * multiple occurences of this macro is supported for complex shell commands.
371 * This macro will BLOCK THE APPLICATION until it completes, since command is run once
372 * for every file in syncronous order. To avoid blocking add the %v macro, below.
373 * ([ls %p] results in [ls "file1"], [ls "file2"] ... [ls "lastfile"])
374 * none if no macro is supplied, the result is equivalent to "command %f"
375 * ([ls] results in [ls "file1" "file2" ... "lastfile"])
377 * Only one of the macros %f or %p may be used in a given commmand.
379 * %v must be the first two characters[1] in a command, causes a window to display
380 * showing the output of the command(s).
381 * %V same as %v except in the case of %p only displays a window for multiple files,
382 * operating on a single file is suppresses the output dialog.
384 * %w must be first two characters in a command, presence will disable full screen
385 * from exiting upon invocation.
388 * [1] Note: %v,%V may also be preceded by "%w".
392 gint editor_command_parse(const gchar *template, GList *list, gchar **output)
395 const gchar *p = template;
396 GString *result = NULL;
397 gchar *extensions = NULL;
400 result = g_string_new("");
402 if (!template || template[0] == '\0')
404 flags |= EDITOR_ERROR_EMPTY;
408 /* skip leading whitespaces if any */
409 while (g_ascii_isspace(*p)) p++;
417 flags |= EDITOR_KEEP_FS;
421 flags |= EDITOR_VERBOSE;
425 flags |= EDITOR_VERBOSE_MULTI;
429 flags |= EDITOR_ERROR_SYNTAX;
434 /* skip whitespaces if any */
435 while (g_ascii_isspace(*p)) p++;
443 if (output) result = g_string_append_c(result, *p);
452 /* for example "%f" or "%{.crw;.raw;.cr2}f" */
458 end = strchr(p, '}');
461 flags |= EDITOR_ERROR_SYNTAX;
465 extensions = g_strndup(p, end - p);
472 flags |= EDITOR_DEST;
475 flags |= EDITOR_FOR_EACH;
476 if (flags & EDITOR_SINGLE_COMMAND)
478 flags |= EDITOR_ERROR_INCOMPATIBLE;
483 /* use the first file from the list */
484 if (!list || !list->data)
486 flags |= EDITOR_ERROR_NO_FILE;
489 pathl = editor_command_path_parse((FileData *)list->data,
490 (flags & EDITOR_DEST) ? PATH_DEST : PATH_FILE,
494 flags |= EDITOR_ERROR_NO_FILE;
497 result = g_string_append_c(result, '"');
498 result = g_string_append(result, pathl);
500 result = g_string_append_c(result, '"');
505 flags |= EDITOR_SINGLE_COMMAND;
506 if (flags & (EDITOR_FOR_EACH | EDITOR_DEST))
508 flags |= EDITOR_ERROR_INCOMPATIBLE;
520 FileData *fd = work->data;
521 pathl = editor_command_path_parse(fd, PATH_FILE, extensions);
526 if (work != list) g_string_append_c(result, ' ');
527 result = g_string_append_c(result, '"');
528 result = g_string_append(result, pathl);
530 result = g_string_append_c(result, '"');
536 flags |= EDITOR_ERROR_NO_FILE;
542 /* %% = % escaping */
543 if (output) result = g_string_append_c(result, *p);
546 flags |= EDITOR_ERROR_SYNTAX;
549 if (extensions) g_free(extensions);
555 if (output) *output = g_string_free(result, FALSE);
562 g_string_free(result, TRUE);
565 if (extensions) g_free(extensions);
569 static void editor_child_exit_cb (GPid pid, gint status, gpointer data)
571 EditorData *ed = data;
572 g_spawn_close_pid(pid);
575 editor_command_next_finish(ed, status);
579 static gint editor_command_one(const gchar *template, GList *list, EditorData *ed)
582 FileData *fd = list->data;
584 gint standard_output;
589 ed->flags = editor_command_parse(template, list, &command);
591 ok = !(ed->flags & EDITOR_ERROR_MASK);
595 ok = (options->shell.path && *options->shell.path);
596 if (!ok) log_printf("ERROR: empty shell command\n");
600 ok = (access(options->shell.path, X_OK) == 0);
601 if (!ok) log_printf("ERROR: cannot execute shell command '%s'\n", options->shell.path);
604 if (!ok) ed->flags |= EDITOR_ERROR_CANT_EXEC;
609 gchar *working_directory;
613 working_directory = remove_level_from_path(fd->path);
614 args[n++] = options->shell.path;
615 if (options->shell.options && *options->shell.options)
616 args[n++] = options->shell.options;
620 ok = g_spawn_async_with_pipes(working_directory, args, NULL,
621 G_SPAWN_DO_NOT_REAP_CHILD, /* GSpawnFlags */
625 ed->vd ? &standard_output : NULL,
626 ed->vd ? &standard_error : NULL,
629 g_free(working_directory);
631 if (!ok) ed->flags |= EDITOR_ERROR_CANT_EXEC;
636 g_child_watch_add(pid, editor_child_exit_cb, ed);
646 buf = g_strdup_printf(_("Failed to run command:\n%s\n"), template);
647 editor_verbose_window_fill(ed->vd, buf, strlen(buf));
653 GIOChannel *channel_output;
654 GIOChannel *channel_error;
656 channel_output = g_io_channel_unix_new(standard_output);
657 g_io_channel_set_flags(channel_output, G_IO_FLAG_NONBLOCK, NULL);
658 g_io_channel_set_encoding(channel_output, NULL, NULL);
660 g_io_add_watch_full(channel_output, G_PRIORITY_HIGH, G_IO_IN | G_IO_ERR | G_IO_HUP,
661 editor_verbose_io_cb, ed, NULL);
662 g_io_channel_unref(channel_output);
664 channel_error = g_io_channel_unix_new(standard_error);
665 g_io_channel_set_flags(channel_error, G_IO_FLAG_NONBLOCK, NULL);
666 g_io_channel_set_encoding(channel_error, NULL, NULL);
668 g_io_add_watch_full(channel_error, G_PRIORITY_HIGH, G_IO_IN | G_IO_ERR | G_IO_HUP,
669 editor_verbose_io_cb, ed, NULL);
670 g_io_channel_unref(channel_error);
676 return ed->flags & EDITOR_ERROR_MASK;
679 static gint editor_command_next_start(EditorData *ed)
681 if (ed->vd) editor_verbose_window_fill(ed->vd, "\n", 1);
683 if (ed->list && ed->count < ed->total)
692 editor_verbose_window_progress(ed, (ed->flags & EDITOR_FOR_EACH) ? fd->path : _("running..."));
696 error = editor_command_one(ed->command_template, ed->list, ed);
697 if (!error && ed->vd)
699 gtk_widget_set_sensitive(ed->vd->button_stop, (ed->list != NULL) );
700 if (ed->flags & EDITOR_FOR_EACH)
702 editor_verbose_window_fill(ed->vd, fd->path, strlen(fd->path));
703 editor_verbose_window_fill(ed->vd, "\n", 1);
710 /* command was not started, call the finish immediately */
711 return editor_command_next_finish(ed, 0);
714 /* everything is done */
715 return editor_command_done(ed);
718 static gint editor_command_next_finish(EditorData *ed, gint status)
720 gint cont = ed->stopping ? EDITOR_CB_SKIP : EDITOR_CB_CONTINUE;
723 ed->flags |= EDITOR_ERROR_STATUS;
725 if (ed->flags & EDITOR_FOR_EACH)
727 /* handle the first element from the list */
728 GList *fd_element = ed->list;
730 ed->list = g_list_remove_link(ed->list, fd_element);
733 cont = ed->callback(ed->list ? ed : NULL, ed->flags, fd_element, ed->data);
734 if (ed->stopping && cont == EDITOR_CB_CONTINUE) cont = EDITOR_CB_SKIP;
736 filelist_free(fd_element);
740 /* handle whole list */
742 cont = ed->callback(NULL, ed->flags, ed->list, ed->data);
743 filelist_free(ed->list);
747 if (cont == EDITOR_CB_SUSPEND)
748 return ed->flags & EDITOR_ERROR_MASK;
749 else if (cont == EDITOR_CB_SKIP)
750 return editor_command_done(ed);
752 return editor_command_next_start(ed);
755 static gint editor_command_done(EditorData *ed)
763 if (ed->count == ed->total)
769 text = _("stopped by user");
771 editor_verbose_window_progress(ed, text);
772 editor_verbose_window_enable_close(ed->vd);
775 /* free the not-handled items */
778 ed->flags |= EDITOR_ERROR_SKIPPED;
779 if (ed->callback) ed->callback(NULL, ed->flags, ed->list, ed->data);
780 filelist_free(ed->list);
786 flags = ed->flags & EDITOR_ERROR_MASK;
788 if (!ed->vd) editor_data_free(ed);
793 void editor_resume(gpointer ed)
795 editor_command_next_start(ed);
798 void editor_skip(gpointer ed)
800 editor_command_done(ed);
803 static gint editor_command_start(const gchar *template, const gchar *text, GList *list, EditorCallback cb, gpointer data)
806 gint flags = editor_command_parse(template, NULL, NULL);
808 if (flags & EDITOR_ERROR_MASK) return flags & EDITOR_ERROR_MASK;
810 ed = g_new0(EditorData, 1);
811 ed->list = filelist_copy(list);
813 ed->command_template = g_strdup(template);
814 ed->total = (flags & EDITOR_SINGLE_COMMAND) ? 1 : g_list_length(list);
816 ed->stopping = FALSE;
820 if ((flags & EDITOR_VERBOSE_MULTI) && list && list->next)
821 flags |= EDITOR_VERBOSE;
823 if (flags & EDITOR_VERBOSE)
824 editor_verbose_window(ed, text);
826 editor_command_next_start(ed);
827 /* errors from editor_command_next_start will be handled via callback */
828 return flags & EDITOR_ERROR_MASK;
831 gboolean is_valid_editor_command(gint n)
833 return (n >= 0 && n < GQ_EDITOR_SLOTS
834 && options->editor[n].command
835 && strlen(options->editor[n].command) > 0);
838 gint start_editor_from_filelist_full(gint n, GList *list, EditorCallback cb, gpointer data)
843 if (!list) return FALSE;
844 if (!is_valid_editor_command(n)) return FALSE;
846 command = g_locale_from_utf8(options->editor[n].command, -1, NULL, NULL, NULL);
847 error = editor_command_start(command, options->editor[n].name, list, cb, data);
850 if (n < GQ_EDITOR_GENERIC_SLOTS && (error & EDITOR_ERROR_MASK))
852 gchar *text = g_strdup_printf(_("%s\n#%d \"%s\":\n%s"), editor_get_error_str(error), n+1,
853 options->editor[n].name, options->editor[n].command);
855 file_util_warning_dialog(_("Invalid editor command"), text, GTK_STOCK_DIALOG_ERROR, NULL);
862 gint start_editor_from_filelist(gint n, GList *list)
864 return start_editor_from_filelist_full(n, list, NULL, NULL);
867 gint start_editor_from_file_full(gint n, FileData *fd, EditorCallback cb, gpointer data)
872 if (!fd) return FALSE;
874 list = g_list_append(NULL, fd);
875 error = start_editor_from_filelist_full(n, list, cb, data);
880 gint start_editor_from_file(gint n, FileData *fd)
882 return start_editor_from_file_full(n, fd, NULL, NULL);
885 gint editor_window_flag_set(gint n)
887 if (!is_valid_editor_command(n)) return TRUE;
889 return (editor_command_parse(options->editor[n].command, NULL, NULL) & EDITOR_KEEP_FS);
892 gint editor_is_filter(gint n)
894 if (!is_valid_editor_command(n)) return FALSE;
896 return (editor_command_parse(options->editor[n].command, NULL, NULL) & EDITOR_DEST);
899 const gchar *editor_get_error_str(gint flags)
901 if (flags & EDITOR_ERROR_EMPTY) return _("Editor template is empty.");
902 if (flags & EDITOR_ERROR_SYNTAX) return _("Editor template has incorrect syntax.");
903 if (flags & EDITOR_ERROR_INCOMPATIBLE) return _("Editor template uses incompatible macros.");
904 if (flags & EDITOR_ERROR_NO_FILE) return _("Can't find matching file type.");
905 if (flags & EDITOR_ERROR_CANT_EXEC) return _("Can't execute external editor.");
906 if (flags & EDITOR_ERROR_STATUS) return _("External editor returned error status.");
907 if (flags & EDITOR_ERROR_SKIPPED) return _("File was skipped.");
908 return _("Unknown error.");
911 const gchar *editor_get_name(gint n)
913 if (!is_valid_editor_command(n)) return NULL;
915 if (options->editor[n].name && strlen(options->editor[n].name) > 0)
916 return options->editor[n].name;
918 return _("(unknown)");