4 * Copyright (C) 2008 - 2009 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"
28 #define EDITOR_WINDOW_WIDTH 500
29 #define EDITOR_WINDOW_HEIGHT 300
33 typedef struct _EditorVerboseData EditorVerboseData;
34 struct _EditorVerboseData {
36 GtkWidget *button_close;
37 GtkWidget *button_stop;
43 typedef struct _EditorData EditorData;
51 EditorVerboseData *vd;
52 EditorCallback callback;
54 const EditorDescription *editor;
58 static void editor_verbose_window_progress(EditorData *ed, const gchar *text);
59 static EditorFlags editor_command_next_start(EditorData *ed);
60 static EditorFlags editor_command_next_finish(EditorData *ed, gint status);
61 static EditorFlags editor_command_done(EditorData *ed);
64 *-----------------------------------------------------------------------------
65 * external editor routines
66 *-----------------------------------------------------------------------------
69 GHashTable *editors = NULL;
70 GtkListStore *desktop_file_list;
73 #ifdef G_KEY_FILE_DESKTOP_GROUP
74 #define DESKTOP_GROUP G_KEY_FILE_DESKTOP_GROUP
76 #define DESKTOP_GROUP "Desktop Entry"
79 void editor_description_free(EditorDescription *editor)
87 g_free(editor->menu_path);
88 g_free(editor->hotkey);
89 g_free(editor->comment);
90 string_list_free(editor->ext_list);
95 static GList *editor_mime_types_to_extensions(gchar **mime_types)
97 /* FIXME: this should be rewritten to use the shared mime database, as soon as we switch to gio */
99 static const gchar *conv_table[][2] = {
100 {"application/x-ufraw", "%raw"},
102 {"image/bmp", ".bmp"},
103 {"image/gif", ".gif"},
104 {"image/jpeg", ".jpeg;.jpg"},
105 {"image/jpg", ".jpg;.jpeg"},
106 {"image/pcx", ".pcx"},
107 {"image/png", ".png"},
108 {"image/svg", ".svg"},
109 {"image/svg+xml", ".svg"},
110 {"image/svg+xml-compressed", ".svg"},
111 {"image/tiff", ".tiff;.tif"},
112 {"image/x-bmp", ".bmp"},
113 {"image/x-canon-crw", ".crw"},
114 {"image/x-cr2", ".cr2"},
115 {"image/x-dcraw", "%raw"},
116 {"image/x-ico", ".ico"},
117 {"image/x-mrw", ".mrw"},
118 {"image/x-MS-bmp", ".bmp"},
119 {"image/x-nef", ".nef"},
120 {"image/x-orf", ".orf"},
121 {"image/x-pcx", ".pcx"},
122 {"image/xpm", ".xpm"},
123 {"image/x-png", ".png"},
124 {"image/x-portable-anymap", ".pam"},
125 {"image/x-portable-bitmap", ".pbm"},
126 {"image/x-portable-graymap", ".pgm"},
127 {"image/x-portable-pixmap", ".ppm"},
128 {"image/x-psd", ".psd"},
129 {"image/x-raf", ".raf"},
130 {"image/x-sgi", ".sgi"},
131 {"image/x-tga", ".tga"},
132 {"image/x-xbitmap", ".xbm"},
133 {"image/x-xcf", ".xcf"},
134 {"image/x-xpixmap", ".xpm"},
135 {"image/x-x3f", ".x3f"},
141 for (i = 0; mime_types[i]; i++)
142 for (j = 0; conv_table[j][0]; j++)
143 if (strcmp(mime_types[i], conv_table[j][0]) == 0)
144 list = g_list_concat(list, filter_to_list(conv_table[j][1]));
149 static gboolean editor_accepts_parameters(EditorDescription *editor)
151 const gchar *p = editor->exec;
153 if (!p) return FALSE;
157 if (*p == '%' && p[1])
179 static gboolean editor_read_desktop_file(const gchar *path)
182 EditorDescription *editor;
185 const gchar *key = filename_from_path(path);
186 gchar **categories, **only_show_in, **not_show_in;
190 if (g_hash_table_lookup(editors, key)) return FALSE; /* the file found earlier wins */
192 key_file = g_key_file_new();
193 if (!g_key_file_load_from_file(key_file, path, 0, NULL))
195 g_key_file_free(key_file);
199 type = g_key_file_get_string(key_file, DESKTOP_GROUP, "Type", NULL);
200 if (!type || strcmp(type, "Application") != 0)
202 /* We only consider desktop entries of Application type */
203 g_key_file_free(key_file);
209 editor = g_new0(EditorDescription, 1);
211 editor->key = g_strdup(key);
212 editor->file = g_strdup(path);
214 g_hash_table_insert(editors, editor->key, editor);
216 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "Hidden", NULL)
217 || g_key_file_get_boolean(key_file, DESKTOP_GROUP, "NoDisplay", NULL))
219 editor->hidden = TRUE;
222 categories = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "Categories", NULL, NULL);
225 gboolean found = FALSE;
227 for (i = 0; categories[i]; i++)
228 /* IMHO "Graphics" is exactly the category that we are interested in, so this does not have to be configurable */
229 if (strcmp(categories[i], "Graphics") == 0 ||
230 strcmp(categories[i], "X-Geeqie") == 0)
235 if (!found) editor->ignored = TRUE;
236 g_strfreev(categories);
240 editor->ignored = TRUE;
243 only_show_in = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "OnlyShowIn", NULL, NULL);
246 gboolean found = FALSE;
248 for (i = 0; only_show_in[i]; i++)
249 if (strcmp(only_show_in[i], "X-Geeqie") == 0)
254 if (!found) editor->ignored = TRUE;
255 g_strfreev(only_show_in);
258 not_show_in = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "NotShowIn", NULL, NULL);
261 gboolean found = FALSE;
263 for (i = 0; not_show_in[i]; i++)
264 if (strcmp(not_show_in[i], "X-Geeqie") == 0)
269 if (found) editor->ignored = TRUE;
270 g_strfreev(not_show_in);
274 try_exec = g_key_file_get_string(key_file, DESKTOP_GROUP, "TryExec", NULL);
275 if (try_exec && !editor->hidden && !editor->ignored)
277 gchar *try_exec_res = g_find_program_in_path(try_exec);
278 if (!try_exec_res) editor->hidden = TRUE;
279 g_free(try_exec_res);
285 /* ignored editors will be deleted, no need to parse the rest */
286 g_key_file_free(key_file);
290 editor->name = g_key_file_get_locale_string(key_file, DESKTOP_GROUP, "Name", NULL, NULL);
291 editor->icon = g_key_file_get_string(key_file, DESKTOP_GROUP, "Icon", NULL);
293 /* Icon key can be either a full path (absolute with file name extension) or an icon name (without extension) */
294 if (editor->icon && !g_path_is_absolute(editor->icon))
296 gchar *ext = strrchr(editor->icon, '.');
298 if (ext && strlen(ext) == 4 &&
299 (!strcmp(ext, ".png") || !strcmp(ext, ".xpm") || !strcmp(ext, ".svg")))
301 log_printf(_("Desktop file '%s' should not include extension in Icon key: '%s'\n"),
302 editor->file, editor->icon);
309 editor->exec = g_key_file_get_string(key_file, DESKTOP_GROUP, "Exec", NULL);
311 /* we take only editors that accept parameters */
312 if (!editor_accepts_parameters(editor)) editor->hidden = TRUE;
314 editor->menu_path = g_key_file_get_string(key_file, DESKTOP_GROUP, "X-Geeqie-Menu-Path", NULL);
315 if (!editor->menu_path) editor->menu_path = g_strdup("EditMenu/ExternalMenu");
317 editor->hotkey = g_key_file_get_string(key_file, DESKTOP_GROUP, "X-Geeqie-Hotkey", NULL);
319 editor->comment = g_key_file_get_string(key_file, DESKTOP_GROUP, "Comment", NULL);
321 extensions = g_key_file_get_string(key_file, DESKTOP_GROUP, "X-Geeqie-File-Extensions", NULL);
323 editor->ext_list = filter_to_list(extensions);
326 gchar **mime_types = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "MimeType", NULL, NULL);
329 editor->ext_list = editor_mime_types_to_extensions(mime_types);
330 g_strfreev(mime_types);
331 if (!editor->ext_list) editor->hidden = TRUE;
335 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "X-Geeqie-Keep-Fullscreen", NULL)) editor->flags |= EDITOR_KEEP_FS;
336 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "X-Geeqie-Verbose", NULL)) editor->flags |= EDITOR_VERBOSE;
337 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "X-Geeqie-Verbose-Multi", NULL)) editor->flags |= EDITOR_VERBOSE_MULTI;
338 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "X-Geeqie-Filter", NULL)) editor->flags |= EDITOR_DEST;
339 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "Terminal", NULL)) editor->flags |= EDITOR_TERMINAL;
341 editor->flags |= editor_command_parse(editor, NULL, NULL);
342 g_key_file_free(key_file);
344 if (editor->ignored) return TRUE;
346 gtk_list_store_append(desktop_file_list, &iter);
347 gtk_list_store_set(desktop_file_list, &iter,
348 DESKTOP_FILE_COLUMN_KEY, key,
349 DESKTOP_FILE_COLUMN_NAME, editor->name,
350 DESKTOP_FILE_COLUMN_HIDDEN, editor->hidden,
351 DESKTOP_FILE_COLUMN_WRITABLE, access_file(path, W_OK),
352 DESKTOP_FILE_COLUMN_PATH, path, -1);
357 static gboolean editor_remove_desktop_file_cb(gpointer key, gpointer value, gpointer user_data)
359 EditorDescription *editor = value;
360 return editor->hidden || editor->ignored;
363 static void editor_read_desktop_dir(const gchar *path)
369 pathl = path_from_utf8(path);
377 while ((dir = readdir(dp)) != NULL)
379 gchar *namel = dir->d_name;
381 if (g_str_has_suffix(namel, ".desktop"))
383 gchar *name = path_to_utf8(namel);
384 gchar *dpath = g_build_filename(path, name, NULL);
385 editor_read_desktop_file(dpath);
393 void editor_load_descriptions(void)
396 gchar *xdg_data_dirs;
401 if (desktop_file_list)
403 gtk_list_store_clear(desktop_file_list);
407 desktop_file_list = gtk_list_store_new(DESKTOP_FILE_COLUMN_COUNT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_BOOLEAN, G_TYPE_STRING);
411 g_hash_table_destroy(editors);
413 editors = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify)editor_description_free);
415 xdg_data_dirs = getenv("XDG_DATA_DIRS");
416 if (xdg_data_dirs && xdg_data_dirs[0])
417 xdg_data_dirs = path_to_utf8(xdg_data_dirs);
419 xdg_data_dirs = g_strdup("/usr/share");
421 all_dirs = g_strconcat(get_rc_dir(), ":", GQ_APP_DIR, ":", xdg_data_home_get(), ":", xdg_data_dirs, NULL);
423 g_free(xdg_data_dirs);
425 split_dirs = g_strsplit(all_dirs, ":", 0);
429 for (i = 0; split_dirs[i]; i++)
431 path = g_build_filename(split_dirs[i], "applications", NULL);
432 editor_read_desktop_dir(path);
436 g_strfreev(split_dirs);
438 g_hash_table_foreach_remove(editors, editor_remove_desktop_file_cb, NULL);
441 static void editor_list_add_cb(gpointer key, gpointer value, gpointer data)
443 GList **listp = data;
444 EditorDescription *editor = value;
446 /* do not show the special commands in any list, they are called explicitly */
447 if (strcmp(editor->key, CMD_COPY) == 0 ||
448 strcmp(editor->key, CMD_MOVE) == 0 ||
449 strcmp(editor->key, CMD_RENAME) == 0 ||
450 strcmp(editor->key, CMD_DELETE) == 0 ||
451 strcmp(editor->key, CMD_FOLDER) == 0) return;
453 *listp = g_list_prepend(*listp, editor);
456 static gint editor_sort(gconstpointer a, gconstpointer b)
458 const EditorDescription *ea = a;
459 const EditorDescription *eb = b;
462 ret = strcmp(ea->menu_path, eb->menu_path);
463 if (ret != 0) return ret;
465 return g_utf8_collate(ea->name, eb->name);
468 GList *editor_list_get(void)
470 GList *editors_list = NULL;
471 g_hash_table_foreach(editors, editor_list_add_cb, &editors_list);
472 editors_list = g_list_sort(editors_list, editor_sort);
477 /* ------------------------------ */
480 static void editor_verbose_data_free(EditorData *ed)
487 static void editor_data_free(EditorData *ed)
489 editor_verbose_data_free(ed);
493 static void editor_verbose_window_close(GenericDialog *gd, gpointer data)
495 EditorData *ed = data;
497 generic_dialog_close(gd);
498 editor_verbose_data_free(ed);
499 if (ed->pid == -1) editor_data_free(ed); /* the process has already terminated */
502 static void editor_verbose_window_stop(GenericDialog *gd, gpointer data)
504 EditorData *ed = data;
507 editor_verbose_window_progress(ed, _("stopping..."));
510 static void editor_verbose_window_enable_close(EditorVerboseData *vd)
512 vd->gd->cancel_cb = editor_verbose_window_close;
514 spinner_set_interval(vd->spinner, -1);
515 gtk_widget_set_sensitive(vd->button_stop, FALSE);
516 gtk_widget_set_sensitive(vd->button_close, TRUE);
519 static EditorVerboseData *editor_verbose_window(EditorData *ed, const gchar *text)
521 EditorVerboseData *vd;
526 vd = g_new0(EditorVerboseData, 1);
528 vd->gd = file_util_gen_dlg(_("Edit command results"), "editor_results",
531 buf = g_strdup_printf(_("Output of %s"), text);
532 generic_dialog_add_message(vd->gd, NULL, buf, NULL);
534 vd->button_stop = generic_dialog_add_button(vd->gd, GTK_STOCK_STOP, NULL,
535 editor_verbose_window_stop, FALSE);
536 gtk_widget_set_sensitive(vd->button_stop, FALSE);
537 vd->button_close = generic_dialog_add_button(vd->gd, GTK_STOCK_CLOSE, NULL,
538 editor_verbose_window_close, TRUE);
539 gtk_widget_set_sensitive(vd->button_close, FALSE);
541 scrolled = gtk_scrolled_window_new(NULL, NULL);
542 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
543 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
544 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
545 gtk_box_pack_start(GTK_BOX(vd->gd->vbox), scrolled, TRUE, TRUE, 5);
546 gtk_widget_show(scrolled);
548 vd->text = gtk_text_view_new();
549 gtk_text_view_set_editable(GTK_TEXT_VIEW(vd->text), FALSE);
550 gtk_widget_set_size_request(vd->text, EDITOR_WINDOW_WIDTH, EDITOR_WINDOW_HEIGHT);
551 gtk_container_add(GTK_CONTAINER(scrolled), vd->text);
552 gtk_widget_show(vd->text);
554 hbox = gtk_hbox_new(FALSE, 0);
555 gtk_box_pack_start(GTK_BOX(vd->gd->vbox), hbox, FALSE, FALSE, 0);
556 gtk_widget_show(hbox);
558 vd->progress = gtk_progress_bar_new();
559 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(vd->progress), 0.0);
560 gtk_box_pack_start(GTK_BOX(hbox), vd->progress, TRUE, TRUE, 0);
561 gtk_widget_show(vd->progress);
563 vd->spinner = spinner_new(NULL, SPINNER_SPEED);
564 gtk_box_pack_start(GTK_BOX(hbox), vd->spinner, FALSE, FALSE, 0);
565 gtk_widget_show(vd->spinner);
567 gtk_widget_show(vd->gd->dialog);
573 static void editor_verbose_window_fill(EditorVerboseData *vd, gchar *text, gint len)
575 GtkTextBuffer *buffer;
578 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(vd->text));
579 gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
580 gtk_text_buffer_insert(buffer, &iter, text, len);
583 static void editor_verbose_window_progress(EditorData *ed, const gchar *text)
589 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ed->vd->progress), (gdouble)ed->count / ed->total);
592 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(ed->vd->progress), (text) ? text : "");
595 static gboolean editor_verbose_io_cb(GIOChannel *source, GIOCondition condition, gpointer data)
597 EditorData *ed = data;
601 if (condition & G_IO_IN)
603 while (g_io_channel_read_chars(source, buf, sizeof(buf), &count, NULL) == G_IO_STATUS_NORMAL)
605 if (!g_utf8_validate(buf, count, NULL))
609 utf8 = g_locale_to_utf8(buf, count, NULL, NULL, NULL);
612 editor_verbose_window_fill(ed->vd, utf8, -1);
617 editor_verbose_window_fill(ed->vd, "Error converting text to valid utf8\n", -1);
622 editor_verbose_window_fill(ed->vd, buf, count);
627 if (condition & (G_IO_ERR | G_IO_HUP))
629 g_io_channel_shutdown(source, TRUE, NULL);
643 static gchar *editor_command_path_parse(const FileData *fd, PathType type, const EditorDescription *editor)
647 const gchar *p = NULL;
649 string = g_string_new("");
651 if (type == PATH_FILE || type == PATH_FILE_URL)
653 GList *work = editor->ext_list;
662 gchar *ext = work->data;
665 if (strcmp(ext, "*") == 0 ||
666 g_ascii_strcasecmp(ext, fd->extension) == 0)
672 work2 = fd->sidecar_files;
675 FileData *sfd = work2->data;
678 if (g_ascii_strcasecmp(ext, sfd->extension) == 0)
689 else if (type == PATH_DEST)
691 if (fd->change && fd->change->dest)
692 p = fd->change->dest;
698 string = g_string_append(string, p);
700 if (type == PATH_FILE_URL) g_string_prepend(string, "file://");
701 pathl = path_from_utf8(string->str);
702 g_string_free(string, TRUE);
704 if (pathl && !pathl[0]) /* empty string case */
713 static GString *append_quoted(GString *str, const char *s, gboolean single_quotes, gboolean double_quotes)
720 g_string_append_c(str, '\'');
722 g_string_append(str, "\"'");
725 for (p = s; *p != '\0'; p++)
728 g_string_append(str, "'\\''");
730 g_string_append_c(str, *p);
736 g_string_append_c(str, '\'');
738 g_string_append(str, "'\"");
745 EditorFlags editor_command_parse(const EditorDescription *editor, GList *list, gchar **output)
747 EditorFlags flags = 0;
749 GString *result = NULL;
750 gboolean escape = FALSE;
751 gboolean single_quotes = FALSE;
752 gboolean double_quotes = FALSE;
755 result = g_string_new("");
757 if (editor->exec[0] == '\0')
759 flags |= EDITOR_ERROR_EMPTY;
764 /* skip leading whitespaces if any */
765 while (g_ascii_isspace(*p)) p++;
774 if (output) result = g_string_append_c(result, *p);
778 if (!single_quotes) escape = TRUE;
779 if (output) result = g_string_append_c(result, *p);
783 if (output) result = g_string_append_c(result, *p);
784 if (!single_quotes && !double_quotes)
785 single_quotes = TRUE;
786 else if (single_quotes)
787 single_quotes = FALSE;
791 if (output) result = g_string_append_c(result, *p);
792 if (!single_quotes && !double_quotes)
793 double_quotes = TRUE;
794 else if (double_quotes)
795 double_quotes = FALSE;
797 else if (*p == '%' && p[1])
805 case 'f': /* single file */
806 case 'u': /* single url */
807 flags |= EDITOR_FOR_EACH;
808 if (flags & EDITOR_SINGLE_COMMAND)
810 flags |= EDITOR_ERROR_INCOMPATIBLE;
815 /* use the first file from the list */
818 flags |= EDITOR_ERROR_NO_FILE;
821 pathl = editor_command_path_parse((FileData *)list->data,
822 (*p == 'f') ? PATH_FILE : PATH_FILE_URL,
826 flags |= EDITOR_ERROR_NO_FILE;
831 result = append_quoted(result, pathl, single_quotes, double_quotes);
839 flags |= EDITOR_SINGLE_COMMAND;
840 if (flags & (EDITOR_FOR_EACH | EDITOR_DEST))
842 flags |= EDITOR_ERROR_INCOMPATIBLE;
854 FileData *fd = work->data;
855 pathl = editor_command_path_parse(fd, (*p == 'F') ? PATH_FILE : PATH_FILE_URL, editor);
863 if (work != list) g_string_append_c(result, ' ');
864 result = append_quoted(result, pathl, single_quotes, double_quotes);
872 flags |= EDITOR_ERROR_NO_FILE;
878 if (editor->icon && *editor->icon)
882 result = g_string_append(result, "--icon ");
883 result = append_quoted(result, editor->icon, single_quotes, double_quotes);
890 result = append_quoted(result, editor->name, single_quotes, double_quotes);
896 result = append_quoted(result, editor->file, single_quotes, double_quotes);
900 /* %% = % escaping */
901 if (output) result = g_string_append_c(result, *p);
909 /* deprecated according to spec, ignore */
912 flags |= EDITOR_ERROR_SYNTAX;
918 if (output) result = g_string_append_c(result, *p);
925 *output = g_string_free(result, FALSE);
926 DEBUG_3("Editor cmd: %s", *output);
935 g_string_free(result, TRUE);
942 static void editor_child_exit_cb(GPid pid, gint status, gpointer data)
944 EditorData *ed = data;
945 g_spawn_close_pid(pid);
948 editor_command_next_finish(ed, status);
952 static EditorFlags editor_command_one(const EditorDescription *editor, GList *list, EditorData *ed)
955 FileData *fd = list->data;
957 gint standard_output;
962 ed->flags = editor->flags;
963 ed->flags |= editor_command_parse(editor, list, &command);
965 ok = !EDITOR_ERRORS(ed->flags);
969 ok = (options->shell.path && *options->shell.path);
970 if (!ok) log_printf("ERROR: empty shell command\n");
974 ok = (access(options->shell.path, X_OK) == 0);
975 if (!ok) log_printf("ERROR: cannot execute shell command '%s'\n", options->shell.path);
978 if (!ok) ed->flags |= EDITOR_ERROR_CANT_EXEC;
983 gchar *working_directory;
987 working_directory = remove_level_from_path(fd->path);
988 args[n++] = options->shell.path;
989 if (options->shell.options && *options->shell.options)
990 args[n++] = options->shell.options;
994 if ((ed->flags & EDITOR_DEST) && fd->change && fd->change->dest) /* FIXME: error handling */
996 g_setenv("GEEQIE_DESTINATION", fd->change->dest, TRUE);
1000 g_unsetenv("GEEQIE_DESTINATION");
1003 ok = g_spawn_async_with_pipes(working_directory, args, NULL,
1004 G_SPAWN_DO_NOT_REAP_CHILD, /* GSpawnFlags */
1008 ed->vd ? &standard_output : NULL,
1009 ed->vd ? &standard_error : NULL,
1012 g_free(working_directory);
1014 if (!ok) ed->flags |= EDITOR_ERROR_CANT_EXEC;
1019 g_child_watch_add(pid, editor_child_exit_cb, ed);
1029 buf = g_strdup_printf(_("Failed to run command:\n%s\n"), editor->file);
1030 editor_verbose_window_fill(ed->vd, buf, strlen(buf));
1036 GIOChannel *channel_output;
1037 GIOChannel *channel_error;
1039 channel_output = g_io_channel_unix_new(standard_output);
1040 g_io_channel_set_flags(channel_output, G_IO_FLAG_NONBLOCK, NULL);
1041 g_io_channel_set_encoding(channel_output, NULL, NULL);
1043 g_io_add_watch_full(channel_output, G_PRIORITY_HIGH, G_IO_IN | G_IO_ERR | G_IO_HUP,
1044 editor_verbose_io_cb, ed, NULL);
1045 g_io_channel_unref(channel_output);
1047 channel_error = g_io_channel_unix_new(standard_error);
1048 g_io_channel_set_flags(channel_error, G_IO_FLAG_NONBLOCK, NULL);
1049 g_io_channel_set_encoding(channel_error, NULL, NULL);
1051 g_io_add_watch_full(channel_error, G_PRIORITY_HIGH, G_IO_IN | G_IO_ERR | G_IO_HUP,
1052 editor_verbose_io_cb, ed, NULL);
1053 g_io_channel_unref(channel_error);
1059 return EDITOR_ERRORS(ed->flags);
1062 static EditorFlags editor_command_next_start(EditorData *ed)
1064 if (ed->vd) editor_verbose_window_fill(ed->vd, "\n", 1);
1066 if (ed->list && ed->count < ed->total)
1071 fd = ed->list->data;
1075 if (ed->flags & EDITOR_FOR_EACH)
1076 editor_verbose_window_progress(ed, fd->path);
1078 editor_verbose_window_progress(ed, _("running..."));
1082 error = editor_command_one(ed->editor, ed->list, ed);
1083 if (!error && ed->vd)
1085 gtk_widget_set_sensitive(ed->vd->button_stop, (ed->list != NULL) );
1086 if (ed->flags & EDITOR_FOR_EACH)
1088 editor_verbose_window_fill(ed->vd, fd->path, strlen(fd->path));
1089 editor_verbose_window_fill(ed->vd, "\n", 1);
1096 /* command was not started, call the finish immediately */
1097 return editor_command_next_finish(ed, 0);
1100 /* everything is done */
1101 return editor_command_done(ed);
1104 static EditorFlags editor_command_next_finish(EditorData *ed, gint status)
1106 gint cont = ed->stopping ? EDITOR_CB_SKIP : EDITOR_CB_CONTINUE;
1109 ed->flags |= EDITOR_ERROR_STATUS;
1111 if (ed->flags & EDITOR_FOR_EACH)
1113 /* handle the first element from the list */
1114 GList *fd_element = ed->list;
1116 ed->list = g_list_remove_link(ed->list, fd_element);
1119 cont = ed->callback(ed->list ? ed : NULL, ed->flags, fd_element, ed->data);
1120 if (ed->stopping && cont == EDITOR_CB_CONTINUE) cont = EDITOR_CB_SKIP;
1122 filelist_free(fd_element);
1126 /* handle whole list */
1128 cont = ed->callback(NULL, ed->flags, ed->list, ed->data);
1129 filelist_free(ed->list);
1135 case EDITOR_CB_SUSPEND:
1136 return EDITOR_ERRORS(ed->flags);
1137 case EDITOR_CB_SKIP:
1138 return editor_command_done(ed);
1141 return editor_command_next_start(ed);
1144 static EditorFlags editor_command_done(EditorData *ed)
1150 if (ed->count == ed->total)
1152 editor_verbose_window_progress(ed, _("done"));
1156 editor_verbose_window_progress(ed, _("stopped by user"));
1158 editor_verbose_window_enable_close(ed->vd);
1161 /* free the not-handled items */
1164 ed->flags |= EDITOR_ERROR_SKIPPED;
1165 if (ed->callback) ed->callback(NULL, ed->flags, ed->list, ed->data);
1166 filelist_free(ed->list);
1172 flags = EDITOR_ERRORS(ed->flags);
1174 if (!ed->vd) editor_data_free(ed);
1179 void editor_resume(gpointer ed)
1181 editor_command_next_start(ed);
1184 void editor_skip(gpointer ed)
1186 editor_command_done(ed);
1189 static EditorFlags editor_command_start(const EditorDescription *editor, const gchar *text, GList *list, EditorCallback cb, gpointer data)
1192 EditorFlags flags = editor->flags;
1194 if (EDITOR_ERRORS(flags)) return EDITOR_ERRORS(flags);
1196 ed = g_new0(EditorData, 1);
1197 ed->list = filelist_copy(list);
1199 ed->editor = editor;
1200 ed->total = (flags & EDITOR_SINGLE_COMMAND) ? 1 : g_list_length(list);
1204 if ((flags & EDITOR_VERBOSE_MULTI) && list && list->next)
1205 flags |= EDITOR_VERBOSE;
1207 if (flags & EDITOR_VERBOSE)
1208 editor_verbose_window(ed, text);
1210 editor_command_next_start(ed);
1211 /* errors from editor_command_next_start will be handled via callback */
1212 return EDITOR_ERRORS(flags);
1215 gboolean is_valid_editor_command(const gchar *key)
1217 if (!key) return FALSE;
1218 return g_hash_table_lookup(editors, key) != NULL;
1221 EditorFlags start_editor_from_filelist_full(const gchar *key, GList *list, EditorCallback cb, gpointer data)
1224 EditorDescription *editor;
1225 if (!key) return FALSE;
1227 editor = g_hash_table_lookup(editors, key);
1229 if (!list) return FALSE;
1230 if (!editor) return FALSE;
1232 error = editor_command_start(editor, editor->name, list, cb, data);
1234 if (EDITOR_ERRORS(error))
1236 gchar *text = g_strdup_printf(_("%s\n\"%s\""), editor_get_error_str(error), editor->file);
1238 file_util_warning_dialog(_("Invalid editor command"), text, GTK_STOCK_DIALOG_ERROR, NULL);
1245 EditorFlags start_editor_from_filelist(const gchar *key, GList *list)
1247 return start_editor_from_filelist_full(key, list, NULL, NULL);
1250 EditorFlags start_editor_from_file_full(const gchar *key, FileData *fd, EditorCallback cb, gpointer data)
1255 if (!fd) return FALSE;
1257 list = g_list_append(NULL, fd);
1258 error = start_editor_from_filelist_full(key, list, cb, data);
1263 EditorFlags start_editor_from_file(const gchar *key, FileData *fd)
1265 return start_editor_from_file_full(key, fd, NULL, NULL);
1268 gboolean editor_window_flag_set(const gchar *key)
1270 EditorDescription *editor;
1271 if (!key) return TRUE;
1273 editor = g_hash_table_lookup(editors, key);
1274 if (!editor) return TRUE;
1276 return !!(editor->flags & EDITOR_KEEP_FS);
1279 gboolean editor_is_filter(const gchar *key)
1281 EditorDescription *editor;
1282 if (!key) return TRUE;
1284 editor = g_hash_table_lookup(editors, key);
1285 if (!editor) return TRUE;
1287 return !!(editor->flags & EDITOR_DEST);
1290 const gchar *editor_get_error_str(EditorFlags flags)
1292 if (flags & EDITOR_ERROR_EMPTY) return _("Editor template is empty.");
1293 if (flags & EDITOR_ERROR_SYNTAX) return _("Editor template has incorrect syntax.");
1294 if (flags & EDITOR_ERROR_INCOMPATIBLE) return _("Editor template uses incompatible macros.");
1295 if (flags & EDITOR_ERROR_NO_FILE) return _("Can't find matching file type.");
1296 if (flags & EDITOR_ERROR_CANT_EXEC) return _("Can't execute external editor.");
1297 if (flags & EDITOR_ERROR_STATUS) return _("External editor returned error status.");
1298 if (flags & EDITOR_ERROR_SKIPPED) return _("File was skipped.");
1299 return _("Unknown error.");
1302 const gchar *editor_get_name(const gchar *key)
1304 EditorDescription *editor = g_hash_table_lookup(editors, key);
1306 if (!editor) return NULL;
1308 return editor->name;
1310 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */