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 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);
64 *-----------------------------------------------------------------------------
65 * external editor routines
66 *-----------------------------------------------------------------------------
69 GHashTable *editors = NULL;
71 #ifdef G_KEY_FILE_DESKTOP_GROUP
72 #define DESKTOP_GROUP G_KEY_FILE_DESKTOP_GROUP
74 #define DESKTOP_GROUP "Desktop Entry"
77 void editor_description_free(EditorDescription *editor)
85 g_free(editor->menu_path);
86 g_free(editor->hotkey);
87 string_list_free(editor->ext_list);
92 static GList *editor_mime_types_to_extensions(gchar **mime_types)
94 /* FIXME: this should be rewritten to use the shared mime database, as soon as we switch to gio */
96 static const gchar *conv_table[][2] = {
97 {"application/x-ufraw", "%raw"},
99 {"image/bmp", ".bmp"},
100 {"image/gif", ".gif"},
101 {"image/jpeg", ".jpeg;.jpg"},
102 {"image/jpg", ".jpg;.jpeg"},
103 {"image/pcx", ".pcx"},
104 {"image/png", ".png"},
105 {"image/svg", ".svg"},
106 {"image/svg+xml", ".svg"},
107 {"image/svg+xml-compressed", ".svg"},
108 {"image/tiff", ".tiff;.tif"},
109 {"image/x-bmp", ".bmp"},
110 {"image/x-canon-crw", ".crw"},
111 {"image/x-cr2", ".cr2"},
112 {"image/x-dcraw", "%raw"},
113 {"image/x-ico", ".ico"},
114 {"image/x-mrw", ".mrw"},
115 {"image/x-MS-bmp", ".bmp"},
116 {"image/x-nef", ".nef"},
117 {"image/x-orf", ".orf"},
118 {"image/x-pcx", ".pcx"},
119 {"image/xpm", ".xpm"},
120 {"image/x-png", ".png"},
121 {"image/x-portable-anymap", ".pam"},
122 {"image/x-portable-bitmap", ".pbm"},
123 {"image/x-portable-graymap", ".pgm"},
124 {"image/x-portable-pixmap", ".ppm"},
125 {"image/x-psd", ".psd"},
126 {"image/x-raf", ".raf"},
127 {"image/x-sgi", ".sgi"},
128 {"image/x-tga", ".tga"},
129 {"image/x-xbitmap", ".xbm"},
130 {"image/x-xcf", ".xcf"},
131 {"image/x-xpixmap", ".xpm"},
132 {"image/x-x3f", ".x3f"},
138 for (i = 0; mime_types[i]; i++)
139 for (j = 0; conv_table[j][0]; j++)
140 if (strcmp(mime_types[i], conv_table[j][0]) == 0)
141 list = g_list_concat(list, filter_to_list(conv_table[j][1]));
146 static gboolean editor_read_desktop_file(const gchar *path)
149 EditorDescription *editor;
152 const gchar *key = filename_from_path(path);
153 gchar **categories, **only_show_in, **not_show_in;
156 if (g_hash_table_lookup(editors, key)) return FALSE; /* the file found earlier wins */
158 key_file = g_key_file_new();
159 if (!g_key_file_load_from_file(key_file, path, 0, NULL))
161 g_key_file_free(key_file);
165 type = g_key_file_get_string(key_file, DESKTOP_GROUP, "Type", NULL);
166 if (!type || strcmp(type, "Application") != 0)
168 /* We only consider desktop entries of Application type */
172 editor = g_new0(EditorDescription, 1);
174 editor->key = g_strdup(key);
175 editor->file = g_strdup(path);
177 g_hash_table_insert(editors, editor->key, editor);
179 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "Hidden", NULL)
180 || g_key_file_get_boolean(key_file, DESKTOP_GROUP, "NoDisplay", NULL))
182 editor->hidden = TRUE;
185 categories = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "Categories", NULL, NULL);
188 gboolean found = FALSE;
190 for (i = 0; categories[i]; i++)
191 /* IMHO "Graphics" is exactly the category that we are interested in, so this does not have to be configurable */
192 if (strcmp(categories[i], "Graphics") == 0 ||
193 strcmp(categories[i], "X-Geeqie") == 0)
198 if (!found) editor->hidden = TRUE;
199 g_strfreev(categories);
203 editor->hidden = TRUE;
206 only_show_in = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "OnlyShowIn", NULL, NULL);
209 gboolean found = FALSE;
211 for (i = 0; only_show_in[i]; i++)
212 if (strcmp(only_show_in[i], "X-Geeqie") == 0)
217 if (!found) editor->hidden = TRUE;
218 g_strfreev(only_show_in);
221 not_show_in = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "NotShowIn", NULL, NULL);
224 gboolean found = FALSE;
226 for (i = 0; not_show_in[i]; i++)
227 if (strcmp(not_show_in[i], "X-Geeqie") == 0)
232 if (found) editor->hidden = TRUE;
233 g_strfreev(not_show_in);
237 try_exec = g_key_file_get_string(key_file, DESKTOP_GROUP, "TryExec", NULL);
238 if (try_exec && !editor->hidden)
240 gchar *try_exec_res = g_find_program_in_path(try_exec);
241 if (!try_exec_res) editor->hidden = TRUE;
242 g_free(try_exec_res);
248 /* hidden editors will be deleted, no need to parse the rest */
249 g_key_file_free(key_file);
253 editor->name = g_key_file_get_locale_string(key_file, DESKTOP_GROUP, "Name", NULL, NULL);
254 editor->icon = g_key_file_get_string(key_file, DESKTOP_GROUP, "Icon", NULL);
256 editor->exec = g_key_file_get_string(key_file, DESKTOP_GROUP, "Exec", NULL);
258 /* we take only editors that accept parameters, FIXME: the test can be improved */
259 if (!strchr(editor->exec, '%')) editor->hidden = TRUE;
261 editor->menu_path = g_key_file_get_string(key_file, DESKTOP_GROUP, "X-Geeqie-Menu-Path", NULL);
262 if (!editor->menu_path) editor->menu_path = g_strdup("EditMenu/ExternalMenu");
264 editor->hotkey = g_key_file_get_string(key_file, DESKTOP_GROUP, "X-Geeqie-Hotkey", NULL);
266 extensions = g_key_file_get_string(key_file, DESKTOP_GROUP, "X-Geeqie-File-Extensions", NULL);
268 editor->ext_list = filter_to_list(extensions);
271 gchar **mime_types = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "MimeType", NULL, NULL);
274 editor->ext_list = editor_mime_types_to_extensions(mime_types);
275 g_strfreev(mime_types);
276 if (!editor->ext_list) editor->hidden = TRUE;
282 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "X-Geeqie-Keep-Fullscreen", NULL)) editor->flags |= EDITOR_KEEP_FS;
283 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "X-Geeqie-Verbose", NULL)) editor->flags |= EDITOR_VERBOSE;
284 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "X-Geeqie-Verbose-Multi", NULL)) editor->flags |= EDITOR_VERBOSE_MULTI;
285 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "X-Geeqie-Filter", NULL)) editor->flags |= EDITOR_DEST;
286 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "Terminal", NULL)) editor->flags |= EDITOR_TERMINAL;
289 editor->flags |= editor_command_parse(editor, NULL, NULL);
290 g_key_file_free(key_file);
295 static gboolean editor_remove_desktop_file_cb(gpointer key, gpointer value, gpointer user_data)
297 EditorDescription *editor = value;
298 return editor->hidden;
301 static void editor_read_desktop_dir(const gchar *path)
307 pathl = path_from_utf8(path);
315 while ((dir = readdir(dp)) != NULL)
317 gchar *namel = dir->d_name;
318 size_t len = strlen(namel);
320 if (len > 8 && g_ascii_strncasecmp(namel + len - 8, ".desktop", 8) == 0)
322 gchar *name = path_to_utf8(namel);
323 gchar *dpath = g_build_filename(path, name, NULL);
324 editor_read_desktop_file(dpath);
332 void editor_load_descriptions(void)
335 gchar *xdg_data_dirs;
342 editors = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify)editor_description_free);
345 xdg_data_dirs = getenv("XDG_DATA_DIRS");
346 if (xdg_data_dirs && xdg_data_dirs[0])
347 xdg_data_dirs = path_to_utf8(xdg_data_dirs);
349 xdg_data_dirs = g_strdup("/usr/share");
351 all_dirs = g_strconcat(get_rc_dir(), ":", GQ_APP_DIR, ":", xdg_data_home_get(), ":", xdg_data_dirs, NULL);
353 g_free(xdg_data_dirs);
355 split_dirs = g_strsplit(all_dirs, ":", 0);
359 for (i = 0; split_dirs[i]; i++)
361 path = g_build_filename(split_dirs[i], "applications", NULL);
362 editor_read_desktop_dir(path);
366 g_strfreev(split_dirs);
368 g_hash_table_foreach_remove(editors, editor_remove_desktop_file_cb, NULL);
371 static void editor_list_add_cb(gpointer key, gpointer value, gpointer data)
373 GList **listp = data;
374 EditorDescription *editor = value;
376 /* do not show the special commands in any list, they are called explicitelly */
377 if (strcmp(editor->key, CMD_COPY) == 0 ||
378 strcmp(editor->key, CMD_MOVE) == 0 ||
379 strcmp(editor->key, CMD_RENAME) == 0 ||
380 strcmp(editor->key, CMD_DELETE) == 0 ||
381 strcmp(editor->key, CMD_FOLDER) == 0) return;
383 *listp = g_list_prepend(*listp, editor);
386 static gint editor_sort(gconstpointer a, gconstpointer b)
388 const EditorDescription *ea = a;
389 const EditorDescription *eb = b;
392 ret = strcmp(ea->menu_path, eb->menu_path);
393 if (ret != 0) return ret;
395 return g_utf8_collate(ea->name, eb->name);
398 GList *editor_list_get(void)
400 GList *editors_list = NULL;
401 g_hash_table_foreach(editors, editor_list_add_cb, &editors_list);
402 editors_list = g_list_sort(editors_list, editor_sort);
407 /* ------------------------------ */
410 static void editor_verbose_data_free(EditorData *ed)
417 static void editor_data_free(EditorData *ed)
419 editor_verbose_data_free(ed);
423 static void editor_verbose_window_close(GenericDialog *gd, gpointer data)
425 EditorData *ed = data;
427 generic_dialog_close(gd);
428 editor_verbose_data_free(ed);
429 if (ed->pid == -1) editor_data_free(ed); /* the process has already terminated */
432 static void editor_verbose_window_stop(GenericDialog *gd, gpointer data)
434 EditorData *ed = data;
437 editor_verbose_window_progress(ed, _("stopping..."));
440 static void editor_verbose_window_enable_close(EditorVerboseData *vd)
442 vd->gd->cancel_cb = editor_verbose_window_close;
444 spinner_set_interval(vd->spinner, -1);
445 gtk_widget_set_sensitive(vd->button_stop, FALSE);
446 gtk_widget_set_sensitive(vd->button_close, TRUE);
449 static EditorVerboseData *editor_verbose_window(EditorData *ed, const gchar *text)
451 EditorVerboseData *vd;
456 vd = g_new0(EditorVerboseData, 1);
458 vd->gd = file_util_gen_dlg(_("Edit command results"), "editor_results",
461 buf = g_strdup_printf(_("Output of %s"), text);
462 generic_dialog_add_message(vd->gd, NULL, buf, NULL);
464 vd->button_stop = generic_dialog_add_button(vd->gd, GTK_STOCK_STOP, NULL,
465 editor_verbose_window_stop, FALSE);
466 gtk_widget_set_sensitive(vd->button_stop, FALSE);
467 vd->button_close = generic_dialog_add_button(vd->gd, GTK_STOCK_CLOSE, NULL,
468 editor_verbose_window_close, TRUE);
469 gtk_widget_set_sensitive(vd->button_close, FALSE);
471 scrolled = gtk_scrolled_window_new(NULL, NULL);
472 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
473 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
474 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
475 gtk_box_pack_start(GTK_BOX(vd->gd->vbox), scrolled, TRUE, TRUE, 5);
476 gtk_widget_show(scrolled);
478 vd->text = gtk_text_view_new();
479 gtk_text_view_set_editable(GTK_TEXT_VIEW(vd->text), FALSE);
480 gtk_widget_set_size_request(vd->text, EDITOR_WINDOW_WIDTH, EDITOR_WINDOW_HEIGHT);
481 gtk_container_add(GTK_CONTAINER(scrolled), vd->text);
482 gtk_widget_show(vd->text);
484 hbox = gtk_hbox_new(FALSE, 0);
485 gtk_box_pack_start(GTK_BOX(vd->gd->vbox), hbox, FALSE, FALSE, 0);
486 gtk_widget_show(hbox);
488 vd->progress = gtk_progress_bar_new();
489 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(vd->progress), 0.0);
490 gtk_box_pack_start(GTK_BOX(hbox), vd->progress, TRUE, TRUE, 0);
491 gtk_widget_show(vd->progress);
493 vd->spinner = spinner_new(NULL, SPINNER_SPEED);
494 gtk_box_pack_start(GTK_BOX(hbox), vd->spinner, FALSE, FALSE, 0);
495 gtk_widget_show(vd->spinner);
497 gtk_widget_show(vd->gd->dialog);
503 static void editor_verbose_window_fill(EditorVerboseData *vd, gchar *text, gint len)
505 GtkTextBuffer *buffer;
508 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(vd->text));
509 gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
510 gtk_text_buffer_insert(buffer, &iter, text, len);
513 static void editor_verbose_window_progress(EditorData *ed, const gchar *text)
519 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ed->vd->progress), (gdouble)ed->count / ed->total);
522 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(ed->vd->progress), (text) ? text : "");
525 static gboolean editor_verbose_io_cb(GIOChannel *source, GIOCondition condition, gpointer data)
527 EditorData *ed = data;
531 if (condition & G_IO_IN)
533 while (g_io_channel_read_chars(source, buf, sizeof(buf), &count, NULL) == G_IO_STATUS_NORMAL)
535 if (!g_utf8_validate(buf, count, NULL))
539 utf8 = g_locale_to_utf8(buf, count, NULL, NULL, NULL);
542 editor_verbose_window_fill(ed->vd, utf8, -1);
547 editor_verbose_window_fill(ed->vd, "Error converting text to valid utf8\n", -1);
552 editor_verbose_window_fill(ed->vd, buf, count);
557 if (condition & (G_IO_ERR | G_IO_HUP))
559 g_io_channel_shutdown(source, TRUE, NULL);
573 static gchar *editor_command_path_parse(const FileData *fd, PathType type, const EditorDescription *editor)
577 const gchar *p = NULL;
579 string = g_string_new("");
581 if (type == PATH_FILE || type == PATH_FILE_URL)
583 GList *work = editor->ext_list;
592 gchar *ext = work->data;
595 if (strcmp(ext, "*") == 0 ||
596 g_ascii_strcasecmp(ext, fd->extension) == 0)
602 work2 = fd->sidecar_files;
605 FileData *sfd = work2->data;
608 if (g_ascii_strcasecmp(ext, sfd->extension) == 0)
619 else if (type == PATH_DEST)
621 if (fd->change && fd->change->dest)
622 p = fd->change->dest;
629 /* must escape \, ", `, and $ to avoid problems,
630 * we assume system shell supports bash-like escaping
632 if (strchr("\\\"`$", *p) != NULL)
634 string = g_string_append_c(string, '\\');
636 string = g_string_append_c(string, *p);
640 if (type == PATH_FILE_URL) g_string_prepend(string, "file://");
641 pathl = path_from_utf8(string->str);
642 g_string_free(string, TRUE);
648 gint editor_command_parse(const EditorDescription *editor, GList *list, gchar **output)
652 GString *result = NULL;
655 result = g_string_new("");
657 if (editor->exec[0] == '\0')
659 flags |= EDITOR_ERROR_EMPTY;
664 /* skip leading whitespaces if any */
665 while (g_ascii_isspace(*p)) p++;
673 if (output) result = g_string_append_c(result, *p);
683 case 'f': /* single file */
684 case 'u': /* single url */
685 flags |= EDITOR_FOR_EACH;
686 if (flags & EDITOR_SINGLE_COMMAND)
688 flags |= EDITOR_ERROR_INCOMPATIBLE;
693 /* use the first file from the list */
696 flags |= EDITOR_ERROR_NO_FILE;
699 pathl = editor_command_path_parse((FileData *)list->data,
700 (*p == 'f') ? PATH_FILE : PATH_FILE_URL,
704 flags |= EDITOR_ERROR_NO_FILE;
709 result = g_string_append_c(result, '"');
710 result = g_string_append(result, pathl);
711 result = g_string_append_c(result, '"');
719 flags |= EDITOR_SINGLE_COMMAND;
720 if (flags & (EDITOR_FOR_EACH | EDITOR_DEST))
722 flags |= EDITOR_ERROR_INCOMPATIBLE;
734 FileData *fd = work->data;
735 pathl = editor_command_path_parse(fd, (*p == 'F') ? PATH_FILE : PATH_FILE_URL, editor);
743 if (work != list) g_string_append_c(result, ' ');
744 result = g_string_append_c(result, '"');
745 result = g_string_append(result, pathl);
746 result = g_string_append_c(result, '"');
754 flags |= EDITOR_ERROR_NO_FILE;
762 result = g_string_append(result, editor->icon);
768 result = g_string_append(result, editor->name);
774 result = g_string_append(result, editor->file);
778 /* %% = % escaping */
779 if (output) result = g_string_append_c(result, *p);
787 /* deprecated according to spec, ignore */
790 flags |= EDITOR_ERROR_SYNTAX;
797 if (output) *output = g_string_free(result, FALSE);
804 g_string_free(result, TRUE);
811 static void editor_child_exit_cb(GPid pid, gint status, gpointer data)
813 EditorData *ed = data;
814 g_spawn_close_pid(pid);
817 editor_command_next_finish(ed, status);
821 static gint editor_command_one(const EditorDescription *editor, GList *list, EditorData *ed)
824 FileData *fd = list->data;
826 gint standard_output;
831 ed->flags = editor->flags | editor_command_parse(editor, list, &command);
833 ok = !(ed->flags & EDITOR_ERROR_MASK);
837 ok = (options->shell.path && *options->shell.path);
838 if (!ok) log_printf("ERROR: empty shell command\n");
842 ok = (access(options->shell.path, X_OK) == 0);
843 if (!ok) log_printf("ERROR: cannot execute shell command '%s'\n", options->shell.path);
846 if (!ok) ed->flags |= EDITOR_ERROR_CANT_EXEC;
851 gchar *working_directory;
855 working_directory = remove_level_from_path(fd->path);
856 args[n++] = options->shell.path;
857 if (options->shell.options && *options->shell.options)
858 args[n++] = options->shell.options;
862 if ((ed->flags & EDITOR_DEST) && fd->change && fd->change->dest) /* FIXME: error handling */
864 setenv("GEEQIE_DESTINATION", fd->change->dest, TRUE);
868 unsetenv("GEEQIE_DESTINATION");
871 ok = g_spawn_async_with_pipes(working_directory, args, NULL,
872 G_SPAWN_DO_NOT_REAP_CHILD, /* GSpawnFlags */
876 ed->vd ? &standard_output : NULL,
877 ed->vd ? &standard_error : NULL,
880 g_free(working_directory);
882 if (!ok) ed->flags |= EDITOR_ERROR_CANT_EXEC;
887 g_child_watch_add(pid, editor_child_exit_cb, ed);
897 buf = g_strdup_printf(_("Failed to run command:\n%s\n"), editor->file);
898 editor_verbose_window_fill(ed->vd, buf, strlen(buf));
904 GIOChannel *channel_output;
905 GIOChannel *channel_error;
907 channel_output = g_io_channel_unix_new(standard_output);
908 g_io_channel_set_flags(channel_output, G_IO_FLAG_NONBLOCK, NULL);
909 g_io_channel_set_encoding(channel_output, NULL, NULL);
911 g_io_add_watch_full(channel_output, G_PRIORITY_HIGH, G_IO_IN | G_IO_ERR | G_IO_HUP,
912 editor_verbose_io_cb, ed, NULL);
913 g_io_channel_unref(channel_output);
915 channel_error = g_io_channel_unix_new(standard_error);
916 g_io_channel_set_flags(channel_error, G_IO_FLAG_NONBLOCK, NULL);
917 g_io_channel_set_encoding(channel_error, NULL, NULL);
919 g_io_add_watch_full(channel_error, G_PRIORITY_HIGH, G_IO_IN | G_IO_ERR | G_IO_HUP,
920 editor_verbose_io_cb, ed, NULL);
921 g_io_channel_unref(channel_error);
927 return ed->flags & EDITOR_ERROR_MASK;
930 static gint editor_command_next_start(EditorData *ed)
932 if (ed->vd) editor_verbose_window_fill(ed->vd, "\n", 1);
934 if (ed->list && ed->count < ed->total)
943 editor_verbose_window_progress(ed, (ed->flags & EDITOR_FOR_EACH) ? fd->path : _("running..."));
947 error = editor_command_one(ed->editor, ed->list, ed);
948 if (!error && ed->vd)
950 gtk_widget_set_sensitive(ed->vd->button_stop, (ed->list != NULL) );
951 if (ed->flags & EDITOR_FOR_EACH)
953 editor_verbose_window_fill(ed->vd, fd->path, strlen(fd->path));
954 editor_verbose_window_fill(ed->vd, "\n", 1);
961 /* command was not started, call the finish immediately */
962 return editor_command_next_finish(ed, 0);
965 /* everything is done */
966 return editor_command_done(ed);
969 static gint editor_command_next_finish(EditorData *ed, gint status)
971 gint cont = ed->stopping ? EDITOR_CB_SKIP : EDITOR_CB_CONTINUE;
974 ed->flags |= EDITOR_ERROR_STATUS;
976 if (ed->flags & EDITOR_FOR_EACH)
978 /* handle the first element from the list */
979 GList *fd_element = ed->list;
981 ed->list = g_list_remove_link(ed->list, fd_element);
984 cont = ed->callback(ed->list ? ed : NULL, ed->flags, fd_element, ed->data);
985 if (ed->stopping && cont == EDITOR_CB_CONTINUE) cont = EDITOR_CB_SKIP;
987 filelist_free(fd_element);
991 /* handle whole list */
993 cont = ed->callback(NULL, ed->flags, ed->list, ed->data);
994 filelist_free(ed->list);
998 if (cont == EDITOR_CB_SUSPEND)
999 return ed->flags & EDITOR_ERROR_MASK;
1000 else if (cont == EDITOR_CB_SKIP)
1001 return editor_command_done(ed);
1003 return editor_command_next_start(ed);
1006 static gint editor_command_done(EditorData *ed)
1014 if (ed->count == ed->total)
1020 text = _("stopped by user");
1022 editor_verbose_window_progress(ed, text);
1023 editor_verbose_window_enable_close(ed->vd);
1026 /* free the not-handled items */
1029 ed->flags |= EDITOR_ERROR_SKIPPED;
1030 if (ed->callback) ed->callback(NULL, ed->flags, ed->list, ed->data);
1031 filelist_free(ed->list);
1037 flags = ed->flags & EDITOR_ERROR_MASK;
1039 if (!ed->vd) editor_data_free(ed);
1044 void editor_resume(gpointer ed)
1046 editor_command_next_start(ed);
1049 void editor_skip(gpointer ed)
1051 editor_command_done(ed);
1054 static gint editor_command_start(const EditorDescription *editor, const gchar *text, GList *list, EditorCallback cb, gpointer data)
1057 gint flags = editor->flags;
1059 if (flags & EDITOR_ERROR_MASK) return flags & EDITOR_ERROR_MASK;
1061 ed = g_new0(EditorData, 1);
1062 ed->list = filelist_copy(list);
1064 ed->editor = editor;
1065 ed->total = (flags & EDITOR_SINGLE_COMMAND) ? 1 : g_list_length(list);
1069 if ((flags & EDITOR_VERBOSE_MULTI) && list && list->next)
1070 flags |= EDITOR_VERBOSE;
1072 if (flags & EDITOR_VERBOSE)
1073 editor_verbose_window(ed, text);
1075 editor_command_next_start(ed);
1076 /* errors from editor_command_next_start will be handled via callback */
1077 return flags & EDITOR_ERROR_MASK;
1080 gboolean is_valid_editor_command(const gchar *key)
1082 if (!key) return FALSE;
1083 return g_hash_table_lookup(editors, key) != NULL;
1086 gint start_editor_from_filelist_full(const gchar *key, GList *list, EditorCallback cb, gpointer data)
1089 EditorDescription *editor;
1090 if (!key) return FALSE;
1092 editor = g_hash_table_lookup(editors, key);
1094 if (!list) return FALSE;
1095 if (!editor) return FALSE;
1097 error = editor_command_start(editor, editor->name, list, cb, data);
1099 if (error & EDITOR_ERROR_MASK)
1101 gchar *text = g_strdup_printf(_("%s\n\"%s\""), editor_get_error_str(error), editor->file);
1103 file_util_warning_dialog(_("Invalid editor command"), text, GTK_STOCK_DIALOG_ERROR, NULL);
1110 gint start_editor_from_filelist(const gchar *key, GList *list)
1112 return start_editor_from_filelist_full(key, list, NULL, NULL);
1115 gint start_editor_from_file_full(const gchar *key, FileData *fd, EditorCallback cb, gpointer data)
1120 if (!fd) return FALSE;
1122 list = g_list_append(NULL, fd);
1123 error = start_editor_from_filelist_full(key, list, cb, data);
1128 gint start_editor_from_file(const gchar *key, FileData *fd)
1130 return start_editor_from_file_full(key, fd, NULL, NULL);
1133 gint editor_window_flag_set(const gchar *key)
1135 EditorDescription *editor;
1136 if (!key) return TRUE;
1138 editor = g_hash_table_lookup(editors, key);
1139 if (!editor) return TRUE;
1141 return (editor->flags & EDITOR_KEEP_FS);
1144 gint editor_is_filter(const gchar *key)
1146 EditorDescription *editor;
1147 if (!key) return TRUE;
1149 editor = g_hash_table_lookup(editors, key);
1150 if (!editor) return TRUE;
1152 return (editor->flags & EDITOR_DEST);
1155 const gchar *editor_get_error_str(gint flags)
1157 if (flags & EDITOR_ERROR_EMPTY) return _("Editor template is empty.");
1158 if (flags & EDITOR_ERROR_SYNTAX) return _("Editor template has incorrect syntax.");
1159 if (flags & EDITOR_ERROR_INCOMPATIBLE) return _("Editor template uses incompatible macros.");
1160 if (flags & EDITOR_ERROR_NO_FILE) return _("Can't find matching file type.");
1161 if (flags & EDITOR_ERROR_CANT_EXEC) return _("Can't execute external editor.");
1162 if (flags & EDITOR_ERROR_STATUS) return _("External editor returned error status.");
1163 if (flags & EDITOR_ERROR_SKIPPED) return _("File was skipped.");
1164 return _("Unknown error.");
1167 const gchar *editor_get_name(const gchar *key)
1169 EditorDescription *editor = g_hash_table_lookup(editors, key);
1171 if (!editor) return NULL;
1173 return editor->name;
1175 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */