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 "ui_fileops.h"
19 #include "ui_spinner.h"
20 #include "ui_utildlg.h"
23 #include "filefilter.h"
28 #define EDITOR_WINDOW_WIDTH 500
29 #define EDITOR_WINDOW_HEIGHT 300
31 #define COMMAND_SHELL "/bin/sh"
32 #define COMMAND_OPT "-c"
35 typedef struct _EditorVerboseData EditorVerboseData;
36 struct _EditorVerboseData {
38 GtkWidget *button_close;
39 GtkWidget *button_stop;
45 typedef struct _EditorData EditorData;
49 gchar *command_template;
54 EditorVerboseData *vd;
55 EditorCallback callback;
60 static gchar *editor_slot_defaults[GQ_EDITOR_SLOTS * 2] = {
61 N_("The Gimp"), "gimp-remote %{.cr2;.crw;.nef;.raw;*}f",
63 N_("Xpaint"), "xpaint %f",
64 N_("UFraw"), "ufraw %{.cr2;.crw;.nef;.raw}p",
65 N_("Add XMP sidecar"), "%vFILE=%{.cr2;.crw;.nef;.raw}p;XMP=`echo \"$FILE\"|sed -e 's|\\.[^.]*$|.xmp|'`; exiftool -tagsfromfile \"$FILE\" \"$XMP\"",
69 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",
70 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",
74 N_("External Copy command"), "%vset -x;cp %p %d",
75 N_("External Move command"), "%vset -x;mv %p %d",
76 N_("External Rename command"), "%vset -x;mv %p %d",
77 N_("External Delete command"), NULL,
78 N_("External New Folder command"), NULL
80 N_("External Copy command"), NULL,
81 N_("External Move command"), NULL,
82 N_("External Rename command"), NULL,
83 N_("External Delete command"), NULL,
84 N_("External New Folder command"), NULL
88 static void editor_verbose_window_progress(EditorData *ed, const gchar *text);
89 static gint editor_command_next_start(EditorData *ed);
90 static gint editor_command_next_finish(EditorData *ed, gint status);
91 static gint editor_command_done(EditorData *ed);
94 *-----------------------------------------------------------------------------
95 * external editor routines
96 *-----------------------------------------------------------------------------
99 void editor_reset_defaults(void)
103 for (i = 0; i < GQ_EDITOR_SLOTS; i++)
105 g_free(options->editor_name[i]);
106 options->editor_name[i] = g_strdup(_(editor_slot_defaults[i * 2]));
107 g_free(options->editor_command[i]);
108 options->editor_command[i] = g_strdup(editor_slot_defaults[i * 2 + 1]);
112 static void editor_verbose_data_free(EditorData *ed)
119 static void editor_data_free(EditorData *ed)
121 editor_verbose_data_free(ed);
122 g_free(ed->command_template);
126 static void editor_verbose_window_close(GenericDialog *gd, gpointer data)
128 EditorData *ed = data;
130 generic_dialog_close(gd);
131 editor_verbose_data_free(ed);
132 if (ed->pid == -1) editor_data_free(ed); /* the process has already terminated */
135 static void editor_verbose_window_stop(GenericDialog *gd, gpointer data)
137 EditorData *ed = data;
140 editor_verbose_window_progress(ed, _("stopping..."));
143 static void editor_verbose_window_enable_close(EditorVerboseData *vd)
145 vd->gd->cancel_cb = editor_verbose_window_close;
147 spinner_set_interval(vd->spinner, -1);
148 gtk_widget_set_sensitive(vd->button_stop, FALSE);
149 gtk_widget_set_sensitive(vd->button_close, TRUE);
152 static EditorVerboseData *editor_verbose_window(EditorData *ed, const gchar *text)
154 EditorVerboseData *vd;
159 vd = g_new0(EditorVerboseData, 1);
161 vd->gd = file_util_gen_dlg(_("Edit command results"), GQ_WMCLASS, "editor_results",
164 buf = g_strdup_printf(_("Output of %s"), text);
165 generic_dialog_add_message(vd->gd, NULL, buf, NULL);
167 vd->button_stop = generic_dialog_add_button(vd->gd, GTK_STOCK_STOP, NULL,
168 editor_verbose_window_stop, FALSE);
169 gtk_widget_set_sensitive(vd->button_stop, FALSE);
170 vd->button_close = generic_dialog_add_button(vd->gd, GTK_STOCK_CLOSE, NULL,
171 editor_verbose_window_close, TRUE);
172 gtk_widget_set_sensitive(vd->button_close, FALSE);
174 scrolled = gtk_scrolled_window_new(NULL, NULL);
175 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
176 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
177 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
178 gtk_box_pack_start(GTK_BOX(vd->gd->vbox), scrolled, TRUE, TRUE, 5);
179 gtk_widget_show(scrolled);
181 vd->text = gtk_text_view_new();
182 gtk_text_view_set_editable(GTK_TEXT_VIEW(vd->text), FALSE);
183 gtk_widget_set_size_request(vd->text, EDITOR_WINDOW_WIDTH, EDITOR_WINDOW_HEIGHT);
184 gtk_container_add(GTK_CONTAINER(scrolled), vd->text);
185 gtk_widget_show(vd->text);
187 hbox = gtk_hbox_new(FALSE, 0);
188 gtk_box_pack_start(GTK_BOX(vd->gd->vbox), hbox, FALSE, FALSE, 0);
189 gtk_widget_show(hbox);
191 vd->progress = gtk_progress_bar_new();
192 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(vd->progress), 0.0);
193 gtk_box_pack_start(GTK_BOX(hbox), vd->progress, TRUE, TRUE, 0);
194 gtk_widget_show(vd->progress);
196 vd->spinner = spinner_new(NULL, SPINNER_SPEED);
197 gtk_box_pack_start(GTK_BOX(hbox), vd->spinner, FALSE, FALSE, 0);
198 gtk_widget_show(vd->spinner);
200 gtk_widget_show(vd->gd->dialog);
206 static void editor_verbose_window_fill(EditorVerboseData *vd, gchar *text, gint len)
208 GtkTextBuffer *buffer;
211 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(vd->text));
212 gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
213 gtk_text_buffer_insert(buffer, &iter, text, len);
216 static void editor_verbose_window_progress(EditorData *ed, const gchar *text)
222 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ed->vd->progress), (double)ed->count / ed->total);
225 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(ed->vd->progress), (text) ? text : "");
228 static gboolean editor_verbose_io_cb(GIOChannel *source, GIOCondition condition, gpointer data)
230 EditorData *ed = data;
234 if (condition & G_IO_IN)
236 while (g_io_channel_read_chars(source, buf, sizeof(buf), &count, NULL) == G_IO_STATUS_NORMAL)
238 if (!g_utf8_validate(buf, count, NULL))
242 utf8 = g_locale_to_utf8(buf, count, NULL, NULL, NULL);
245 editor_verbose_window_fill(ed->vd, utf8, -1);
250 editor_verbose_window_fill(ed->vd, "Error converting text to valid utf8\n", -1);
255 editor_verbose_window_fill(ed->vd, buf, count);
260 if (condition & (G_IO_ERR | G_IO_HUP))
262 g_io_channel_shutdown(source, TRUE, NULL);
275 static gchar *editor_command_path_parse(const FileData *fd, PathType type, const gchar *extensions)
279 const gchar *p = NULL;
281 string = g_string_new("");
283 if (type == PATH_FILE)
285 GList *ext_list = filter_to_list(extensions);
286 GList *work = ext_list;
295 gchar *ext = work->data;
298 if (strcmp(ext, "*") == 0 ||
299 strcasecmp(ext, fd->extension) == 0)
305 work2 = fd->sidecar_files;
308 FileData *sfd = work2->data;
311 if (strcasecmp(ext, sfd->extension) == 0)
319 string_list_free(ext_list);
323 else if (type == PATH_DEST)
325 if (fd->change && fd->change->dest)
326 p = fd->change->dest;
333 /* must escape \, ", `, and $ to avoid problems,
334 * we assume system shell supports bash-like escaping
336 if (strchr("\\\"`$", *p) != NULL)
338 string = g_string_append_c(string, '\\');
340 string = g_string_append_c(string, *p);
344 pathl = path_from_utf8(string->str);
345 g_string_free(string, TRUE);
352 * The supported macros for editor commands:
354 * %f first occurence replaced by quoted sequence of filenames, command is run once.
355 * only one occurence of this macro is supported.
356 * ([ls %f] results in [ls "file1" "file2" ... "lastfile"])
357 * %p command is run for each filename in turn, each instance replaced with single filename.
358 * multiple occurences of this macro is supported for complex shell commands.
359 * This macro will BLOCK THE APPLICATION until it completes, since command is run once
360 * for every file in syncronous order. To avoid blocking add the %v macro, below.
361 * ([ls %p] results in [ls "file1"], [ls "file2"] ... [ls "lastfile"])
362 * none if no macro is supplied, the result is equivalent to "command %f"
363 * ([ls] results in [ls "file1" "file2" ... "lastfile"])
365 * Only one of the macros %f or %p may be used in a given commmand.
367 * %v must be the first two characters[1] in a command, causes a window to display
368 * showing the output of the command(s).
369 * %V same as %v except in the case of %p only displays a window for multiple files,
370 * operating on a single file is suppresses the output dialog.
372 * %w must be first two characters in a command, presence will disable full screen
373 * from exiting upon invocation.
376 * [1] Note: %v,%V may also be preceded by "%w".
380 gint editor_command_parse(const gchar *template, GList *list, gchar **output)
383 const gchar *p = template;
384 GString *result = NULL;
385 gchar *extensions = NULL;
388 result = g_string_new("");
390 if (!template || template[0] == '\0')
392 flags |= EDITOR_ERROR_EMPTY;
403 flags |= EDITOR_KEEP_FS;
407 flags |= EDITOR_VERBOSE;
411 flags |= EDITOR_VERBOSE_MULTI;
423 if (output) result = g_string_append_c(result, *p);
432 /* for example "%f" or "%{.crw;.raw;.cr2}f" */
438 end = strchr(p, '}');
441 flags |= EDITOR_ERROR_SYNTAX;
445 extensions = g_strndup(p, end - p);
452 flags |= EDITOR_DEST;
454 flags |= EDITOR_FOR_EACH;
455 if (flags & EDITOR_SINGLE_COMMAND)
457 flags |= EDITOR_ERROR_INCOMPATIBLE;
462 /* use the first file from the list */
463 if (!list || !list->data)
465 flags |= EDITOR_ERROR_NO_FILE;
468 pathl = editor_command_path_parse((FileData *)list->data, (*p == 'd') ? PATH_DEST : PATH_FILE, extensions);
471 flags |= EDITOR_ERROR_NO_FILE;
474 result = g_string_append_c(result, '"');
475 result = g_string_append(result, pathl);
477 result = g_string_append_c(result, '"');
482 flags |= EDITOR_SINGLE_COMMAND;
483 if (flags & (EDITOR_FOR_EACH | EDITOR_DEST))
485 flags |= EDITOR_ERROR_INCOMPATIBLE;
497 FileData *fd = work->data;
498 pathl = editor_command_path_parse(fd, PATH_FILE, extensions);
503 if (work != list) g_string_append_c(result, ' ');
504 result = g_string_append_c(result, '"');
505 result = g_string_append(result, pathl);
507 result = g_string_append_c(result, '"');
513 flags |= EDITOR_ERROR_NO_FILE;
519 flags |= EDITOR_ERROR_SYNTAX;
522 if (extensions) g_free(extensions);
528 if (output) *output = g_string_free(result, FALSE);
535 g_string_free(result, TRUE);
538 if (extensions) g_free(extensions);
542 static void editor_child_exit_cb (GPid pid, gint status, gpointer data)
544 EditorData *ed = data;
545 g_spawn_close_pid(pid);
548 editor_command_next_finish(ed, status);
552 static gint editor_command_one(const gchar *template, GList *list, EditorData *ed)
555 FileData *fd = list->data;
557 gint standard_output;
562 ed->flags = editor_command_parse(template, list, &command);
564 ok = !(ed->flags & EDITOR_ERROR_MASK);
568 gchar *working_directory;
571 working_directory = remove_level_from_path(fd->path);
572 args[0] = COMMAND_SHELL;
573 args[1] = COMMAND_OPT;
577 ok = g_spawn_async_with_pipes(working_directory, args, NULL,
578 G_SPAWN_DO_NOT_REAP_CHILD, /* GSpawnFlags */
582 ed->vd ? &standard_output : NULL,
583 ed->vd ? &standard_error : NULL,
586 g_free(working_directory);
588 if (!ok) ed->flags |= EDITOR_ERROR_CANT_EXEC;
593 g_child_watch_add(pid, editor_child_exit_cb, ed);
603 buf = g_strdup_printf(_("Failed to run command:\n%s\n"), template);
604 editor_verbose_window_fill(ed->vd, buf, strlen(buf));
610 GIOChannel *channel_output;
611 GIOChannel *channel_error;
613 channel_output = g_io_channel_unix_new(standard_output);
614 g_io_channel_set_flags(channel_output, G_IO_FLAG_NONBLOCK, NULL);
616 g_io_add_watch_full(channel_output, G_PRIORITY_HIGH, G_IO_IN | G_IO_ERR | G_IO_HUP,
617 editor_verbose_io_cb, ed, NULL);
618 g_io_channel_unref(channel_output);
620 channel_error = g_io_channel_unix_new(standard_error);
621 g_io_channel_set_flags(channel_error, G_IO_FLAG_NONBLOCK, NULL);
623 g_io_add_watch_full(channel_error, G_PRIORITY_HIGH, G_IO_IN | G_IO_ERR | G_IO_HUP,
624 editor_verbose_io_cb, ed, NULL);
625 g_io_channel_unref(channel_error);
631 return ed->flags & EDITOR_ERROR_MASK;
634 static gint editor_command_next_start(EditorData *ed)
636 if (ed->vd) editor_verbose_window_fill(ed->vd, "\n", 1);
638 if (ed->list && ed->count < ed->total)
647 editor_verbose_window_progress(ed, (ed->flags & EDITOR_FOR_EACH) ? fd->path : _("running..."));
651 error = editor_command_one(ed->command_template, ed->list, ed);
652 if (!error && ed->vd)
654 gtk_widget_set_sensitive(ed->vd->button_stop, (ed->list != NULL) );
655 if (ed->flags & EDITOR_FOR_EACH)
657 editor_verbose_window_fill(ed->vd, fd->path, strlen(fd->path));
658 editor_verbose_window_fill(ed->vd, "\n", 1);
665 /* command was not started, call the finish immediately */
666 return editor_command_next_finish(ed, 0);
669 /* everything is done */
670 return editor_command_done(ed);
673 static gint editor_command_next_finish(EditorData *ed, gint status)
675 gint cont = ed->stopping ? EDITOR_CB_SKIP : EDITOR_CB_CONTINUE;
678 ed->flags |= EDITOR_ERROR_STATUS;
680 if (ed->flags & EDITOR_FOR_EACH)
682 /* handle the first element from the list */
683 GList *fd_element = ed->list;
685 ed->list = g_list_remove_link(ed->list, fd_element);
687 cont = ed->callback(ed->list ? ed : NULL, ed->flags, fd_element, ed->data);
688 filelist_free(fd_element);
692 /* handle whole list */
694 cont = ed->callback(NULL, ed->flags, ed->list, ed->data);
695 filelist_free(ed->list);
699 if (cont == EDITOR_CB_SUSPEND)
700 return ed->flags & EDITOR_ERROR_MASK;
701 else if (cont == EDITOR_CB_SKIP)
702 return editor_command_done(ed);
704 return editor_command_next_start(ed);
707 static gint editor_command_done(EditorData *ed)
715 if (ed->count == ed->total)
721 text = _("stopped by user");
723 editor_verbose_window_progress(ed, text);
724 editor_verbose_window_enable_close(ed->vd);
727 /* free the not-handled items */
730 ed->flags |= EDITOR_ERROR_SKIPPED;
731 if (ed->callback) ed->callback(NULL, ed->flags, ed->list, ed->data);
732 filelist_free(ed->list);
738 flags = ed->flags & EDITOR_ERROR_MASK;
740 if (!ed->vd) editor_data_free(ed);
745 void editor_resume(gpointer ed)
747 editor_command_next_start(ed);
750 void editor_skip(gpointer ed)
752 editor_command_done(ed);
755 static gint editor_command_start(const gchar *template, const gchar *text, GList *list, EditorCallback cb, gpointer data)
758 gint flags = editor_command_parse(template, NULL, NULL);
760 if (flags & EDITOR_ERROR_MASK) return flags & EDITOR_ERROR_MASK;
762 ed = g_new0(EditorData, 1);
763 ed->list = filelist_copy(list);
765 ed->command_template = g_strdup(template);
766 ed->total = (flags & EDITOR_SINGLE_COMMAND) ? 1 : g_list_length(list);
768 ed->stopping = FALSE;
772 if ((flags & EDITOR_VERBOSE_MULTI) && list && list->next)
773 flags |= EDITOR_VERBOSE;
775 if (flags & EDITOR_VERBOSE)
776 editor_verbose_window(ed, text);
778 editor_command_next_start(ed);
779 /* errors from editor_command_next_start will be handled via callback */
780 return flags & EDITOR_ERROR_MASK;
783 static gint is_valid_editor_command(gint n)
785 return (n >= 0 && n < GQ_EDITOR_SLOTS
786 && options->editor_command[n]
787 && strlen(options->editor_command[n]) > 0);
790 gint start_editor_from_filelist_full(gint n, GList *list, EditorCallback cb, gpointer data)
795 if (!list) return FALSE;
796 if (!is_valid_editor_command(n)) return FALSE;
798 command = g_locale_from_utf8(options->editor_command[n], -1, NULL, NULL, NULL);
799 error = editor_command_start(command, options->editor_name[n], list, cb, data);
804 gint start_editor_from_filelist(gint n, GList *list)
806 return start_editor_from_filelist_full(n, list, NULL, NULL);
809 gint start_editor_from_file_full(gint n, FileData *fd, EditorCallback cb, gpointer data)
814 if (!fd) return FALSE;
816 list = g_list_append(NULL, fd);
817 error = start_editor_from_filelist_full(n, list, cb, data);
822 gint start_editor_from_file(gint n, FileData *fd)
824 return start_editor_from_file_full(n, fd, NULL, NULL);
827 gint editor_window_flag_set(gint n)
829 if (!is_valid_editor_command(n)) return TRUE;
831 return (editor_command_parse(options->editor_command[n], NULL, NULL) & EDITOR_KEEP_FS);
834 const gchar *editor_get_error_str(gint flags)
836 if (flags & EDITOR_ERROR_EMPTY) return _("Editor template is empty.");
837 if (flags & EDITOR_ERROR_SYNTAX) return _("Editor template has incorrect syntax.");
838 if (flags & EDITOR_ERROR_INCOMPATIBLE) return _("Editor template uses incompatible macros.");
839 if (flags & EDITOR_ERROR_NO_FILE) return _("Can't find matching file type.");
840 if (flags & EDITOR_ERROR_CANT_EXEC) return _("Can't execute external editor.");
841 if (flags & EDITOR_ERROR_STATUS) return _("External editor returned error status.");
842 if (flags & EDITOR_ERROR_SKIPPED) return _("File was skipped.");
843 return _("Unknown error.");