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
30 #define COMMAND_SHELL "/bin/sh"
31 #define COMMAND_OPT "-c"
34 typedef struct _EditorVerboseData EditorVerboseData;
35 struct _EditorVerboseData {
37 GtkWidget *button_close;
38 GtkWidget *button_stop;
44 typedef struct _EditorData EditorData;
48 gchar *command_template;
53 EditorVerboseData *vd;
54 EditorCallback callback;
59 static gchar *editor_slot_defaults[GQ_EDITOR_SLOTS * 2] = {
60 N_("The Gimp"), "gimp-remote %{.cr2;.crw;.nef;.raw;*}f",
62 N_("Xpaint"), "xpaint %f",
63 N_("UFraw"), "ufraw %{.cr2;.crw;.nef;.raw}p",
64 N_("Add XMP sidecar"), "%vFILE=%{.cr2;.crw;.nef;.raw}p;XMP=`echo \"$FILE\"|sed -e 's|\\.[^.]*$|.xmp|'`; exiftool -tagsfromfile \"$FILE\" \"$XMP\"",
68 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",
69 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",
73 N_("External Copy command"), "%vset -x;cp %p %d",
74 N_("External Move command"), "%vset -x;mv %p %d",
75 N_("External Rename command"), "%vset -x;mv %p %d",
76 N_("External Delete command"), NULL,
77 N_("External New Folder command"), NULL
79 N_("External Copy command"), NULL,
80 N_("External Move command"), NULL,
81 N_("External Rename command"), NULL,
82 N_("External Delete command"), NULL,
83 N_("External New Folder command"), NULL
87 static void editor_verbose_window_progress(EditorData *ed, const gchar *text);
88 static gint editor_command_next_start(EditorData *ed);
89 static gint editor_command_next_finish(EditorData *ed, gint status);
90 static gint editor_command_done(EditorData *ed);
93 *-----------------------------------------------------------------------------
94 * external editor routines
95 *-----------------------------------------------------------------------------
98 void editor_reset_defaults(void)
102 for (i = 0; i < GQ_EDITOR_SLOTS; i++)
104 g_free(options->editor_name[i]);
105 options->editor_name[i] = g_strdup(_(editor_slot_defaults[i * 2]));
106 g_free(options->editor_command[i]);
107 options->editor_command[i] = g_strdup(editor_slot_defaults[i * 2 + 1]);
111 static void editor_verbose_data_free(EditorData *ed)
118 static void editor_data_free(EditorData *ed)
120 editor_verbose_data_free(ed);
121 g_free(ed->command_template);
125 static void editor_verbose_window_close(GenericDialog *gd, gpointer data)
127 EditorData *ed = data;
129 generic_dialog_close(gd);
130 editor_verbose_data_free(ed);
131 if (ed->pid == -1) editor_data_free(ed); /* the process has already terminated */
134 static void editor_verbose_window_stop(GenericDialog *gd, gpointer data)
136 EditorData *ed = data;
139 editor_verbose_window_progress(ed, _("stopping..."));
142 static void editor_verbose_window_enable_close(EditorVerboseData *vd)
144 vd->gd->cancel_cb = editor_verbose_window_close;
146 spinner_set_interval(vd->spinner, -1);
147 gtk_widget_set_sensitive(vd->button_stop, FALSE);
148 gtk_widget_set_sensitive(vd->button_close, TRUE);
151 static EditorVerboseData *editor_verbose_window(EditorData *ed, const gchar *text)
153 EditorVerboseData *vd;
158 vd = g_new0(EditorVerboseData, 1);
160 vd->gd = file_util_gen_dlg(_("Edit command results"), GQ_WMCLASS, "editor_results",
163 buf = g_strdup_printf(_("Output of %s"), text);
164 generic_dialog_add_message(vd->gd, NULL, buf, NULL);
166 vd->button_stop = generic_dialog_add_button(vd->gd, GTK_STOCK_STOP, NULL,
167 editor_verbose_window_stop, FALSE);
168 gtk_widget_set_sensitive(vd->button_stop, FALSE);
169 vd->button_close = generic_dialog_add_button(vd->gd, GTK_STOCK_CLOSE, NULL,
170 editor_verbose_window_close, TRUE);
171 gtk_widget_set_sensitive(vd->button_close, FALSE);
173 scrolled = gtk_scrolled_window_new(NULL, NULL);
174 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
175 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
176 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
177 gtk_box_pack_start(GTK_BOX(vd->gd->vbox), scrolled, TRUE, TRUE, 5);
178 gtk_widget_show(scrolled);
180 vd->text = gtk_text_view_new();
181 gtk_text_view_set_editable(GTK_TEXT_VIEW(vd->text), FALSE);
182 gtk_widget_set_size_request(vd->text, EDITOR_WINDOW_WIDTH, EDITOR_WINDOW_HEIGHT);
183 gtk_container_add(GTK_CONTAINER(scrolled), vd->text);
184 gtk_widget_show(vd->text);
186 hbox = gtk_hbox_new(FALSE, 0);
187 gtk_box_pack_start(GTK_BOX(vd->gd->vbox), hbox, FALSE, FALSE, 0);
188 gtk_widget_show(hbox);
190 vd->progress = gtk_progress_bar_new();
191 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(vd->progress), 0.0);
192 gtk_box_pack_start(GTK_BOX(hbox), vd->progress, TRUE, TRUE, 0);
193 gtk_widget_show(vd->progress);
195 vd->spinner = spinner_new(NULL, SPINNER_SPEED);
196 gtk_box_pack_start(GTK_BOX(hbox), vd->spinner, FALSE, FALSE, 0);
197 gtk_widget_show(vd->spinner);
199 gtk_widget_show(vd->gd->dialog);
205 static void editor_verbose_window_fill(EditorVerboseData *vd, gchar *text, gint len)
207 GtkTextBuffer *buffer;
210 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(vd->text));
211 gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
212 gtk_text_buffer_insert(buffer, &iter, text, len);
215 static void editor_verbose_window_progress(EditorData *ed, const gchar *text)
221 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ed->vd->progress), (double)ed->count / ed->total);
224 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(ed->vd->progress), (text) ? text : "");
227 static gboolean editor_verbose_io_cb(GIOChannel *source, GIOCondition condition, gpointer data)
229 EditorData *ed = data;
233 if (condition & G_IO_IN)
235 while (g_io_channel_read_chars(source, buf, sizeof(buf), &count, NULL) == G_IO_STATUS_NORMAL)
237 if (!g_utf8_validate(buf, count, NULL))
241 utf8 = g_locale_to_utf8(buf, count, NULL, NULL, NULL);
244 editor_verbose_window_fill(ed->vd, utf8, -1);
249 editor_verbose_window_fill(ed->vd, "Error converting text to valid utf8\n", -1);
254 editor_verbose_window_fill(ed->vd, buf, count);
259 if (condition & (G_IO_ERR | G_IO_HUP))
261 g_io_channel_shutdown(source, TRUE, NULL);
274 static gchar *editor_command_path_parse(const FileData *fd, PathType type, const gchar *extensions)
278 const gchar *p = NULL;
280 string = g_string_new("");
282 if (type == PATH_FILE)
284 GList *ext_list = filter_to_list(extensions);
285 GList *work = ext_list;
294 gchar *ext = work->data;
297 if (strcmp(ext, "*") == 0 ||
298 strcasecmp(ext, fd->extension) == 0)
304 work2 = fd->sidecar_files;
307 FileData *sfd = work2->data;
310 if (strcasecmp(ext, sfd->extension) == 0)
318 string_list_free(ext_list);
322 else if (type == PATH_DEST)
324 if (fd->change && fd->change->dest)
325 p = fd->change->dest;
332 /* must escape \, ", `, and $ to avoid problems,
333 * we assume system shell supports bash-like escaping
335 if (strchr("\\\"`$", *p) != NULL)
337 string = g_string_append_c(string, '\\');
339 string = g_string_append_c(string, *p);
343 pathl = path_from_utf8(string->str);
344 g_string_free(string, TRUE);
351 * The supported macros for editor commands:
353 * %f first occurence replaced by quoted sequence of filenames, command is run once.
354 * only one occurence of this macro is supported.
355 * ([ls %f] results in [ls "file1" "file2" ... "lastfile"])
356 * %p command is run for each filename in turn, each instance replaced with single filename.
357 * multiple occurences of this macro is supported for complex shell commands.
358 * This macro will BLOCK THE APPLICATION until it completes, since command is run once
359 * for every file in syncronous order. To avoid blocking add the %v macro, below.
360 * ([ls %p] results in [ls "file1"], [ls "file2"] ... [ls "lastfile"])
361 * none if no macro is supplied, the result is equivalent to "command %f"
362 * ([ls] results in [ls "file1" "file2" ... "lastfile"])
364 * Only one of the macros %f or %p may be used in a given commmand.
366 * %v must be the first two characters[1] in a command, causes a window to display
367 * showing the output of the command(s).
368 * %V same as %v except in the case of %p only displays a window for multiple files,
369 * operating on a single file is suppresses the output dialog.
371 * %w must be first two characters in a command, presence will disable full screen
372 * from exiting upon invocation.
375 * [1] Note: %v,%V may also be preceded by "%w".
379 gint editor_command_parse(const gchar *template, GList *list, gchar **output)
382 const gchar *p = template;
383 GString *result = NULL;
384 gchar *extensions = NULL;
387 result = g_string_new("");
389 if (!template || template[0] == '\0')
391 flags |= EDITOR_ERROR_EMPTY;
395 /* skip leading whitespaces if any */
396 while (g_ascii_isspace(*p)) p++;
404 flags |= EDITOR_KEEP_FS;
408 flags |= EDITOR_VERBOSE;
412 flags |= EDITOR_VERBOSE_MULTI;
416 flags |= EDITOR_ERROR_SYNTAX;
421 /* skip whitespaces if any */
422 while (g_ascii_isspace(*p)) p++;
430 if (output) result = g_string_append_c(result, *p);
439 /* for example "%f" or "%{.crw;.raw;.cr2}f" */
445 end = strchr(p, '}');
448 flags |= EDITOR_ERROR_SYNTAX;
452 extensions = g_strndup(p, end - p);
459 flags |= EDITOR_DEST;
462 flags |= EDITOR_FOR_EACH;
463 if (flags & EDITOR_SINGLE_COMMAND)
465 flags |= EDITOR_ERROR_INCOMPATIBLE;
470 /* use the first file from the list */
471 if (!list || !list->data)
473 flags |= EDITOR_ERROR_NO_FILE;
476 pathl = editor_command_path_parse((FileData *)list->data,
477 (flags & EDITOR_DEST) ? PATH_DEST : PATH_FILE,
481 flags |= EDITOR_ERROR_NO_FILE;
484 result = g_string_append_c(result, '"');
485 result = g_string_append(result, pathl);
487 result = g_string_append_c(result, '"');
492 flags |= EDITOR_SINGLE_COMMAND;
493 if (flags & (EDITOR_FOR_EACH | EDITOR_DEST))
495 flags |= EDITOR_ERROR_INCOMPATIBLE;
507 FileData *fd = work->data;
508 pathl = editor_command_path_parse(fd, PATH_FILE, extensions);
513 if (work != list) g_string_append_c(result, ' ');
514 result = g_string_append_c(result, '"');
515 result = g_string_append(result, pathl);
517 result = g_string_append_c(result, '"');
523 flags |= EDITOR_ERROR_NO_FILE;
529 /* %% = % escaping */
530 if (output) result = g_string_append_c(result, *p);
533 flags |= EDITOR_ERROR_SYNTAX;
536 if (extensions) g_free(extensions);
542 if (output) *output = g_string_free(result, FALSE);
549 g_string_free(result, TRUE);
552 if (extensions) g_free(extensions);
556 static void editor_child_exit_cb (GPid pid, gint status, gpointer data)
558 EditorData *ed = data;
559 g_spawn_close_pid(pid);
562 editor_command_next_finish(ed, status);
566 static gint editor_command_one(const gchar *template, GList *list, EditorData *ed)
569 FileData *fd = list->data;
571 gint standard_output;
576 ed->flags = editor_command_parse(template, list, &command);
578 ok = !(ed->flags & EDITOR_ERROR_MASK);
582 gchar *working_directory;
585 working_directory = remove_level_from_path(fd->path);
586 args[0] = COMMAND_SHELL;
587 args[1] = COMMAND_OPT;
591 ok = g_spawn_async_with_pipes(working_directory, args, NULL,
592 G_SPAWN_DO_NOT_REAP_CHILD, /* GSpawnFlags */
596 ed->vd ? &standard_output : NULL,
597 ed->vd ? &standard_error : NULL,
600 g_free(working_directory);
602 if (!ok) ed->flags |= EDITOR_ERROR_CANT_EXEC;
607 g_child_watch_add(pid, editor_child_exit_cb, ed);
617 buf = g_strdup_printf(_("Failed to run command:\n%s\n"), template);
618 editor_verbose_window_fill(ed->vd, buf, strlen(buf));
624 GIOChannel *channel_output;
625 GIOChannel *channel_error;
627 channel_output = g_io_channel_unix_new(standard_output);
628 g_io_channel_set_flags(channel_output, G_IO_FLAG_NONBLOCK, NULL);
630 g_io_add_watch_full(channel_output, G_PRIORITY_HIGH, G_IO_IN | G_IO_ERR | G_IO_HUP,
631 editor_verbose_io_cb, ed, NULL);
632 g_io_channel_unref(channel_output);
634 channel_error = g_io_channel_unix_new(standard_error);
635 g_io_channel_set_flags(channel_error, G_IO_FLAG_NONBLOCK, NULL);
637 g_io_add_watch_full(channel_error, G_PRIORITY_HIGH, G_IO_IN | G_IO_ERR | G_IO_HUP,
638 editor_verbose_io_cb, ed, NULL);
639 g_io_channel_unref(channel_error);
645 return ed->flags & EDITOR_ERROR_MASK;
648 static gint editor_command_next_start(EditorData *ed)
650 if (ed->vd) editor_verbose_window_fill(ed->vd, "\n", 1);
652 if (ed->list && ed->count < ed->total)
661 editor_verbose_window_progress(ed, (ed->flags & EDITOR_FOR_EACH) ? fd->path : _("running..."));
665 error = editor_command_one(ed->command_template, ed->list, ed);
666 if (!error && ed->vd)
668 gtk_widget_set_sensitive(ed->vd->button_stop, (ed->list != NULL) );
669 if (ed->flags & EDITOR_FOR_EACH)
671 editor_verbose_window_fill(ed->vd, fd->path, strlen(fd->path));
672 editor_verbose_window_fill(ed->vd, "\n", 1);
679 /* command was not started, call the finish immediately */
680 return editor_command_next_finish(ed, 0);
683 /* everything is done */
684 return editor_command_done(ed);
687 static gint editor_command_next_finish(EditorData *ed, gint status)
689 gint cont = ed->stopping ? EDITOR_CB_SKIP : EDITOR_CB_CONTINUE;
692 ed->flags |= EDITOR_ERROR_STATUS;
694 if (ed->flags & EDITOR_FOR_EACH)
696 /* handle the first element from the list */
697 GList *fd_element = ed->list;
699 ed->list = g_list_remove_link(ed->list, fd_element);
701 cont = ed->callback(ed->list ? ed : NULL, ed->flags, fd_element, ed->data);
702 filelist_free(fd_element);
706 /* handle whole list */
708 cont = ed->callback(NULL, ed->flags, ed->list, ed->data);
709 filelist_free(ed->list);
713 if (cont == EDITOR_CB_SUSPEND)
714 return ed->flags & EDITOR_ERROR_MASK;
715 else if (cont == EDITOR_CB_SKIP)
716 return editor_command_done(ed);
718 return editor_command_next_start(ed);
721 static gint editor_command_done(EditorData *ed)
729 if (ed->count == ed->total)
735 text = _("stopped by user");
737 editor_verbose_window_progress(ed, text);
738 editor_verbose_window_enable_close(ed->vd);
741 /* free the not-handled items */
744 ed->flags |= EDITOR_ERROR_SKIPPED;
745 if (ed->callback) ed->callback(NULL, ed->flags, ed->list, ed->data);
746 filelist_free(ed->list);
752 flags = ed->flags & EDITOR_ERROR_MASK;
754 if (!ed->vd) editor_data_free(ed);
759 void editor_resume(gpointer ed)
761 editor_command_next_start(ed);
764 void editor_skip(gpointer ed)
766 editor_command_done(ed);
769 static gint editor_command_start(const gchar *template, const gchar *text, GList *list, EditorCallback cb, gpointer data)
772 gint flags = editor_command_parse(template, NULL, NULL);
774 if (flags & EDITOR_ERROR_MASK) return flags & EDITOR_ERROR_MASK;
776 ed = g_new0(EditorData, 1);
777 ed->list = filelist_copy(list);
779 ed->command_template = g_strdup(template);
780 ed->total = (flags & EDITOR_SINGLE_COMMAND) ? 1 : g_list_length(list);
782 ed->stopping = FALSE;
786 if ((flags & EDITOR_VERBOSE_MULTI) && list && list->next)
787 flags |= EDITOR_VERBOSE;
789 if (flags & EDITOR_VERBOSE)
790 editor_verbose_window(ed, text);
792 editor_command_next_start(ed);
793 /* errors from editor_command_next_start will be handled via callback */
794 return flags & EDITOR_ERROR_MASK;
797 static gint is_valid_editor_command(gint n)
799 return (n >= 0 && n < GQ_EDITOR_SLOTS
800 && options->editor_command[n]
801 && strlen(options->editor_command[n]) > 0);
804 gint start_editor_from_filelist_full(gint n, GList *list, EditorCallback cb, gpointer data)
809 if (!list) return FALSE;
810 if (!is_valid_editor_command(n)) return FALSE;
812 command = g_locale_from_utf8(options->editor_command[n], -1, NULL, NULL, NULL);
813 error = editor_command_start(command, options->editor_name[n], list, cb, data);
816 if (n < GQ_EDITOR_GENERIC_SLOTS && (error & EDITOR_ERROR_MASK))
818 gchar *text = g_strdup_printf(_("%s\n#%d \"%s\":\n%s"), editor_get_error_str(error), n+1,
819 options->editor_name[n], options->editor_command[n]);
821 file_util_warning_dialog(_("Invalid editor command"), text, GTK_STOCK_DIALOG_ERROR, NULL);
828 gint start_editor_from_filelist(gint n, GList *list)
830 return start_editor_from_filelist_full(n, list, NULL, NULL);
833 gint start_editor_from_file_full(gint n, FileData *fd, EditorCallback cb, gpointer data)
838 if (!fd) return FALSE;
840 list = g_list_append(NULL, fd);
841 error = start_editor_from_filelist_full(n, list, cb, data);
846 gint start_editor_from_file(gint n, FileData *fd)
848 return start_editor_from_file_full(n, fd, NULL, NULL);
851 gint editor_window_flag_set(gint n)
853 if (!is_valid_editor_command(n)) return TRUE;
855 return (editor_command_parse(options->editor_command[n], NULL, NULL) & EDITOR_KEEP_FS);
858 const gchar *editor_get_error_str(gint flags)
860 if (flags & EDITOR_ERROR_EMPTY) return _("Editor template is empty.");
861 if (flags & EDITOR_ERROR_SYNTAX) return _("Editor template has incorrect syntax.");
862 if (flags & EDITOR_ERROR_INCOMPATIBLE) return _("Editor template uses incompatible macros.");
863 if (flags & EDITOR_ERROR_NO_FILE) return _("Can't find matching file type.");
864 if (flags & EDITOR_ERROR_CANT_EXEC) return _("Can't execute external editor.");
865 if (flags & EDITOR_ERROR_STATUS) return _("External editor returned error status.");
866 if (flags & EDITOR_ERROR_SKIPPED) return _("File was skipped.");
867 return _("Unknown error.");