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;
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 */
169 g_key_file_free(key_file);
173 editor = g_new0(EditorDescription, 1);
175 editor->key = g_strdup(key);
176 editor->file = g_strdup(path);
178 g_hash_table_insert(editors, editor->key, editor);
180 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "Hidden", NULL)
181 || g_key_file_get_boolean(key_file, DESKTOP_GROUP, "NoDisplay", NULL))
183 editor->hidden = TRUE;
186 categories = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "Categories", NULL, NULL);
189 gboolean found = FALSE;
191 for (i = 0; categories[i]; i++)
192 /* IMHO "Graphics" is exactly the category that we are interested in, so this does not have to be configurable */
193 if (strcmp(categories[i], "Graphics") == 0 ||
194 strcmp(categories[i], "X-Geeqie") == 0)
199 if (!found) editor->hidden = TRUE;
200 g_strfreev(categories);
204 editor->hidden = TRUE;
207 only_show_in = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "OnlyShowIn", NULL, NULL);
210 gboolean found = FALSE;
212 for (i = 0; only_show_in[i]; i++)
213 if (strcmp(only_show_in[i], "X-Geeqie") == 0)
218 if (!found) editor->hidden = TRUE;
219 g_strfreev(only_show_in);
222 not_show_in = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "NotShowIn", NULL, NULL);
225 gboolean found = FALSE;
227 for (i = 0; not_show_in[i]; i++)
228 if (strcmp(not_show_in[i], "X-Geeqie") == 0)
233 if (found) editor->hidden = TRUE;
234 g_strfreev(not_show_in);
238 try_exec = g_key_file_get_string(key_file, DESKTOP_GROUP, "TryExec", NULL);
239 if (try_exec && !editor->hidden)
241 gchar *try_exec_res = g_find_program_in_path(try_exec);
242 if (!try_exec_res) editor->hidden = TRUE;
243 g_free(try_exec_res);
249 /* hidden editors will be deleted, no need to parse the rest */
250 g_key_file_free(key_file);
254 editor->name = g_key_file_get_locale_string(key_file, DESKTOP_GROUP, "Name", NULL, NULL);
255 editor->icon = g_key_file_get_string(key_file, DESKTOP_GROUP, "Icon", NULL);
257 editor->exec = g_key_file_get_string(key_file, DESKTOP_GROUP, "Exec", NULL);
259 /* we take only editors that accept parameters, FIXME: the test can be improved */
260 if (!strchr(editor->exec, '%')) editor->hidden = TRUE;
262 editor->menu_path = g_key_file_get_string(key_file, DESKTOP_GROUP, "X-Geeqie-Menu-Path", NULL);
263 if (!editor->menu_path) editor->menu_path = g_strdup("EditMenu/ExternalMenu");
265 editor->hotkey = g_key_file_get_string(key_file, DESKTOP_GROUP, "X-Geeqie-Hotkey", NULL);
267 extensions = g_key_file_get_string(key_file, DESKTOP_GROUP, "X-Geeqie-File-Extensions", NULL);
269 editor->ext_list = filter_to_list(extensions);
272 gchar **mime_types = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "MimeType", NULL, NULL);
275 editor->ext_list = editor_mime_types_to_extensions(mime_types);
276 g_strfreev(mime_types);
277 if (!editor->ext_list) editor->hidden = TRUE;
281 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "X-Geeqie-Keep-Fullscreen", NULL)) editor->flags |= EDITOR_KEEP_FS;
282 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "X-Geeqie-Verbose", NULL)) editor->flags |= EDITOR_VERBOSE;
283 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "X-Geeqie-Verbose-Multi", NULL)) editor->flags |= EDITOR_VERBOSE_MULTI;
284 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "X-Geeqie-Filter", NULL)) editor->flags |= EDITOR_DEST;
285 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "Terminal", NULL)) editor->flags |= EDITOR_TERMINAL;
287 editor->flags |= editor_command_parse(editor, NULL, NULL);
288 g_key_file_free(key_file);
293 static gboolean editor_remove_desktop_file_cb(gpointer key, gpointer value, gpointer user_data)
295 EditorDescription *editor = value;
296 return editor->hidden;
299 static void editor_read_desktop_dir(const gchar *path)
305 pathl = path_from_utf8(path);
313 while ((dir = readdir(dp)) != NULL)
315 gchar *namel = dir->d_name;
317 if (g_str_has_suffix(namel, ".desktop"))
319 gchar *name = path_to_utf8(namel);
320 gchar *dpath = g_build_filename(path, name, NULL);
321 editor_read_desktop_file(dpath);
329 void editor_load_descriptions(void)
332 gchar *xdg_data_dirs;
339 editors = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify)editor_description_free);
342 xdg_data_dirs = getenv("XDG_DATA_DIRS");
343 if (xdg_data_dirs && xdg_data_dirs[0])
344 xdg_data_dirs = path_to_utf8(xdg_data_dirs);
346 xdg_data_dirs = g_strdup("/usr/share");
348 all_dirs = g_strconcat(get_rc_dir(), ":", GQ_APP_DIR, ":", xdg_data_home_get(), ":", xdg_data_dirs, NULL);
350 g_free(xdg_data_dirs);
352 split_dirs = g_strsplit(all_dirs, ":", 0);
356 for (i = 0; split_dirs[i]; i++)
358 path = g_build_filename(split_dirs[i], "applications", NULL);
359 editor_read_desktop_dir(path);
363 g_strfreev(split_dirs);
365 g_hash_table_foreach_remove(editors, editor_remove_desktop_file_cb, NULL);
368 static void editor_list_add_cb(gpointer key, gpointer value, gpointer data)
370 GList **listp = data;
371 EditorDescription *editor = value;
373 /* do not show the special commands in any list, they are called explicitly */
374 if (strcmp(editor->key, CMD_COPY) == 0 ||
375 strcmp(editor->key, CMD_MOVE) == 0 ||
376 strcmp(editor->key, CMD_RENAME) == 0 ||
377 strcmp(editor->key, CMD_DELETE) == 0 ||
378 strcmp(editor->key, CMD_FOLDER) == 0) return;
380 *listp = g_list_prepend(*listp, editor);
383 static gint editor_sort(gconstpointer a, gconstpointer b)
385 const EditorDescription *ea = a;
386 const EditorDescription *eb = b;
389 ret = strcmp(ea->menu_path, eb->menu_path);
390 if (ret != 0) return ret;
392 return g_utf8_collate(ea->name, eb->name);
395 GList *editor_list_get(void)
397 GList *editors_list = NULL;
398 g_hash_table_foreach(editors, editor_list_add_cb, &editors_list);
399 editors_list = g_list_sort(editors_list, editor_sort);
404 /* ------------------------------ */
407 static void editor_verbose_data_free(EditorData *ed)
414 static void editor_data_free(EditorData *ed)
416 editor_verbose_data_free(ed);
420 static void editor_verbose_window_close(GenericDialog *gd, gpointer data)
422 EditorData *ed = data;
424 generic_dialog_close(gd);
425 editor_verbose_data_free(ed);
426 if (ed->pid == -1) editor_data_free(ed); /* the process has already terminated */
429 static void editor_verbose_window_stop(GenericDialog *gd, gpointer data)
431 EditorData *ed = data;
434 editor_verbose_window_progress(ed, _("stopping..."));
437 static void editor_verbose_window_enable_close(EditorVerboseData *vd)
439 vd->gd->cancel_cb = editor_verbose_window_close;
441 spinner_set_interval(vd->spinner, -1);
442 gtk_widget_set_sensitive(vd->button_stop, FALSE);
443 gtk_widget_set_sensitive(vd->button_close, TRUE);
446 static EditorVerboseData *editor_verbose_window(EditorData *ed, const gchar *text)
448 EditorVerboseData *vd;
453 vd = g_new0(EditorVerboseData, 1);
455 vd->gd = file_util_gen_dlg(_("Edit command results"), "editor_results",
458 buf = g_strdup_printf(_("Output of %s"), text);
459 generic_dialog_add_message(vd->gd, NULL, buf, NULL);
461 vd->button_stop = generic_dialog_add_button(vd->gd, GTK_STOCK_STOP, NULL,
462 editor_verbose_window_stop, FALSE);
463 gtk_widget_set_sensitive(vd->button_stop, FALSE);
464 vd->button_close = generic_dialog_add_button(vd->gd, GTK_STOCK_CLOSE, NULL,
465 editor_verbose_window_close, TRUE);
466 gtk_widget_set_sensitive(vd->button_close, FALSE);
468 scrolled = gtk_scrolled_window_new(NULL, NULL);
469 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
470 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
471 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
472 gtk_box_pack_start(GTK_BOX(vd->gd->vbox), scrolled, TRUE, TRUE, 5);
473 gtk_widget_show(scrolled);
475 vd->text = gtk_text_view_new();
476 gtk_text_view_set_editable(GTK_TEXT_VIEW(vd->text), FALSE);
477 gtk_widget_set_size_request(vd->text, EDITOR_WINDOW_WIDTH, EDITOR_WINDOW_HEIGHT);
478 gtk_container_add(GTK_CONTAINER(scrolled), vd->text);
479 gtk_widget_show(vd->text);
481 hbox = gtk_hbox_new(FALSE, 0);
482 gtk_box_pack_start(GTK_BOX(vd->gd->vbox), hbox, FALSE, FALSE, 0);
483 gtk_widget_show(hbox);
485 vd->progress = gtk_progress_bar_new();
486 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(vd->progress), 0.0);
487 gtk_box_pack_start(GTK_BOX(hbox), vd->progress, TRUE, TRUE, 0);
488 gtk_widget_show(vd->progress);
490 vd->spinner = spinner_new(NULL, SPINNER_SPEED);
491 gtk_box_pack_start(GTK_BOX(hbox), vd->spinner, FALSE, FALSE, 0);
492 gtk_widget_show(vd->spinner);
494 gtk_widget_show(vd->gd->dialog);
500 static void editor_verbose_window_fill(EditorVerboseData *vd, gchar *text, gint len)
502 GtkTextBuffer *buffer;
505 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(vd->text));
506 gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
507 gtk_text_buffer_insert(buffer, &iter, text, len);
510 static void editor_verbose_window_progress(EditorData *ed, const gchar *text)
516 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ed->vd->progress), (gdouble)ed->count / ed->total);
519 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(ed->vd->progress), (text) ? text : "");
522 static gboolean editor_verbose_io_cb(GIOChannel *source, GIOCondition condition, gpointer data)
524 EditorData *ed = data;
528 if (condition & G_IO_IN)
530 while (g_io_channel_read_chars(source, buf, sizeof(buf), &count, NULL) == G_IO_STATUS_NORMAL)
532 if (!g_utf8_validate(buf, count, NULL))
536 utf8 = g_locale_to_utf8(buf, count, NULL, NULL, NULL);
539 editor_verbose_window_fill(ed->vd, utf8, -1);
544 editor_verbose_window_fill(ed->vd, "Error converting text to valid utf8\n", -1);
549 editor_verbose_window_fill(ed->vd, buf, count);
554 if (condition & (G_IO_ERR | G_IO_HUP))
556 g_io_channel_shutdown(source, TRUE, NULL);
570 static gchar *editor_command_path_parse(const FileData *fd, PathType type, const EditorDescription *editor)
574 const gchar *p = NULL;
576 string = g_string_new("");
578 if (type == PATH_FILE || type == PATH_FILE_URL)
580 GList *work = editor->ext_list;
589 gchar *ext = work->data;
592 if (strcmp(ext, "*") == 0 ||
593 g_ascii_strcasecmp(ext, fd->extension) == 0)
599 work2 = fd->sidecar_files;
602 FileData *sfd = work2->data;
605 if (g_ascii_strcasecmp(ext, sfd->extension) == 0)
616 else if (type == PATH_DEST)
618 if (fd->change && fd->change->dest)
619 p = fd->change->dest;
627 /* must escape \, ", `, and $ to avoid problems,
628 * we assume system shell supports bash-like escaping
630 if (strchr("\\\"`$", *p) != NULL)
632 string = g_string_append_c(string, '\\');
634 string = g_string_append_c(string, *p);
638 if (type == PATH_FILE_URL) g_string_prepend(string, "file://");
639 pathl = path_from_utf8(string->str);
640 g_string_free(string, TRUE);
642 if (pathl && !pathl[0]) /* empty string case */
652 EditorFlags editor_command_parse(const EditorDescription *editor, GList *list, gchar **output)
654 EditorFlags flags = 0;
656 GString *result = NULL;
659 result = g_string_new("");
661 if (editor->exec[0] == '\0')
663 flags |= EDITOR_ERROR_EMPTY;
668 /* skip leading whitespaces if any */
669 while (g_ascii_isspace(*p)) p++;
677 if (output) result = g_string_append_c(result, *p);
687 case 'f': /* single file */
688 case 'u': /* single url */
689 flags |= EDITOR_FOR_EACH;
690 if (flags & EDITOR_SINGLE_COMMAND)
692 flags |= EDITOR_ERROR_INCOMPATIBLE;
697 /* use the first file from the list */
700 flags |= EDITOR_ERROR_NO_FILE;
703 pathl = editor_command_path_parse((FileData *)list->data,
704 (*p == 'f') ? PATH_FILE : PATH_FILE_URL,
708 flags |= EDITOR_ERROR_NO_FILE;
713 result = g_string_append_c(result, '"');
714 result = g_string_append(result, pathl);
715 result = g_string_append_c(result, '"');
723 flags |= EDITOR_SINGLE_COMMAND;
724 if (flags & (EDITOR_FOR_EACH | EDITOR_DEST))
726 flags |= EDITOR_ERROR_INCOMPATIBLE;
738 FileData *fd = work->data;
739 pathl = editor_command_path_parse(fd, (*p == 'F') ? PATH_FILE : PATH_FILE_URL, editor);
747 if (work != list) g_string_append_c(result, ' ');
748 result = g_string_append_c(result, '"');
749 result = g_string_append(result, pathl);
750 result = g_string_append_c(result, '"');
758 flags |= EDITOR_ERROR_NO_FILE;
766 result = g_string_append(result, editor->icon);
772 result = g_string_append(result, editor->name);
778 result = g_string_append(result, editor->file);
782 /* %% = % escaping */
783 if (output) result = g_string_append_c(result, *p);
791 /* deprecated according to spec, ignore */
794 flags |= EDITOR_ERROR_SYNTAX;
801 if (output) *output = g_string_free(result, FALSE);
808 g_string_free(result, TRUE);
815 static void editor_child_exit_cb(GPid pid, gint status, gpointer data)
817 EditorData *ed = data;
818 g_spawn_close_pid(pid);
821 editor_command_next_finish(ed, status);
825 static EditorFlags editor_command_one(const EditorDescription *editor, GList *list, EditorData *ed)
828 FileData *fd = list->data;
830 gint standard_output;
835 ed->flags = editor->flags;
836 ed->flags |= editor_command_parse(editor, list, &command);
838 ok = !EDITOR_ERRORS(ed->flags);
842 ok = (options->shell.path && *options->shell.path);
843 if (!ok) log_printf("ERROR: empty shell command\n");
847 ok = (access(options->shell.path, X_OK) == 0);
848 if (!ok) log_printf("ERROR: cannot execute shell command '%s'\n", options->shell.path);
851 if (!ok) ed->flags |= EDITOR_ERROR_CANT_EXEC;
856 gchar *working_directory;
860 working_directory = remove_level_from_path(fd->path);
861 args[n++] = options->shell.path;
862 if (options->shell.options && *options->shell.options)
863 args[n++] = options->shell.options;
867 if ((ed->flags & EDITOR_DEST) && fd->change && fd->change->dest) /* FIXME: error handling */
869 g_setenv("GEEQIE_DESTINATION", fd->change->dest, TRUE);
873 g_unsetenv("GEEQIE_DESTINATION");
876 ok = g_spawn_async_with_pipes(working_directory, args, NULL,
877 G_SPAWN_DO_NOT_REAP_CHILD, /* GSpawnFlags */
881 ed->vd ? &standard_output : NULL,
882 ed->vd ? &standard_error : NULL,
885 g_free(working_directory);
887 if (!ok) ed->flags |= EDITOR_ERROR_CANT_EXEC;
892 g_child_watch_add(pid, editor_child_exit_cb, ed);
902 buf = g_strdup_printf(_("Failed to run command:\n%s\n"), editor->file);
903 editor_verbose_window_fill(ed->vd, buf, strlen(buf));
909 GIOChannel *channel_output;
910 GIOChannel *channel_error;
912 channel_output = g_io_channel_unix_new(standard_output);
913 g_io_channel_set_flags(channel_output, G_IO_FLAG_NONBLOCK, NULL);
914 g_io_channel_set_encoding(channel_output, NULL, NULL);
916 g_io_add_watch_full(channel_output, G_PRIORITY_HIGH, G_IO_IN | G_IO_ERR | G_IO_HUP,
917 editor_verbose_io_cb, ed, NULL);
918 g_io_channel_unref(channel_output);
920 channel_error = g_io_channel_unix_new(standard_error);
921 g_io_channel_set_flags(channel_error, G_IO_FLAG_NONBLOCK, NULL);
922 g_io_channel_set_encoding(channel_error, NULL, NULL);
924 g_io_add_watch_full(channel_error, G_PRIORITY_HIGH, G_IO_IN | G_IO_ERR | G_IO_HUP,
925 editor_verbose_io_cb, ed, NULL);
926 g_io_channel_unref(channel_error);
932 return EDITOR_ERRORS(ed->flags);
935 static EditorFlags editor_command_next_start(EditorData *ed)
937 if (ed->vd) editor_verbose_window_fill(ed->vd, "\n", 1);
939 if (ed->list && ed->count < ed->total)
948 if (ed->flags & EDITOR_FOR_EACH)
949 editor_verbose_window_progress(ed, fd->path);
951 editor_verbose_window_progress(ed, _("running..."));
955 error = editor_command_one(ed->editor, ed->list, ed);
956 if (!error && ed->vd)
958 gtk_widget_set_sensitive(ed->vd->button_stop, (ed->list != NULL) );
959 if (ed->flags & EDITOR_FOR_EACH)
961 editor_verbose_window_fill(ed->vd, fd->path, strlen(fd->path));
962 editor_verbose_window_fill(ed->vd, "\n", 1);
969 /* command was not started, call the finish immediately */
970 return editor_command_next_finish(ed, 0);
973 /* everything is done */
974 return editor_command_done(ed);
977 static EditorFlags editor_command_next_finish(EditorData *ed, gint status)
979 gint cont = ed->stopping ? EDITOR_CB_SKIP : EDITOR_CB_CONTINUE;
982 ed->flags |= EDITOR_ERROR_STATUS;
984 if (ed->flags & EDITOR_FOR_EACH)
986 /* handle the first element from the list */
987 GList *fd_element = ed->list;
989 ed->list = g_list_remove_link(ed->list, fd_element);
992 cont = ed->callback(ed->list ? ed : NULL, ed->flags, fd_element, ed->data);
993 if (ed->stopping && cont == EDITOR_CB_CONTINUE) cont = EDITOR_CB_SKIP;
995 filelist_free(fd_element);
999 /* handle whole list */
1001 cont = ed->callback(NULL, ed->flags, ed->list, ed->data);
1002 filelist_free(ed->list);
1008 case EDITOR_CB_SUSPEND:
1009 return EDITOR_ERRORS(ed->flags);
1010 case EDITOR_CB_SKIP:
1011 return editor_command_done(ed);
1014 return editor_command_next_start(ed);
1017 static EditorFlags editor_command_done(EditorData *ed)
1023 if (ed->count == ed->total)
1025 editor_verbose_window_progress(ed, _("done"));
1029 editor_verbose_window_progress(ed, _("stopped by user"));
1031 editor_verbose_window_enable_close(ed->vd);
1034 /* free the not-handled items */
1037 ed->flags |= EDITOR_ERROR_SKIPPED;
1038 if (ed->callback) ed->callback(NULL, ed->flags, ed->list, ed->data);
1039 filelist_free(ed->list);
1045 flags = EDITOR_ERRORS(ed->flags);
1047 if (!ed->vd) editor_data_free(ed);
1052 void editor_resume(gpointer ed)
1054 editor_command_next_start(ed);
1057 void editor_skip(gpointer ed)
1059 editor_command_done(ed);
1062 static EditorFlags editor_command_start(const EditorDescription *editor, const gchar *text, GList *list, EditorCallback cb, gpointer data)
1065 EditorFlags flags = editor->flags;
1067 if (EDITOR_ERRORS(flags)) return EDITOR_ERRORS(flags);
1069 ed = g_new0(EditorData, 1);
1070 ed->list = filelist_copy(list);
1072 ed->editor = editor;
1073 ed->total = (flags & EDITOR_SINGLE_COMMAND) ? 1 : g_list_length(list);
1077 if ((flags & EDITOR_VERBOSE_MULTI) && list && list->next)
1078 flags |= EDITOR_VERBOSE;
1080 if (flags & EDITOR_VERBOSE)
1081 editor_verbose_window(ed, text);
1083 editor_command_next_start(ed);
1084 /* errors from editor_command_next_start will be handled via callback */
1085 return EDITOR_ERRORS(flags);
1088 gboolean is_valid_editor_command(const gchar *key)
1090 if (!key) return FALSE;
1091 return g_hash_table_lookup(editors, key) != NULL;
1094 EditorFlags start_editor_from_filelist_full(const gchar *key, GList *list, EditorCallback cb, gpointer data)
1097 EditorDescription *editor;
1098 if (!key) return FALSE;
1100 editor = g_hash_table_lookup(editors, key);
1102 if (!list) return FALSE;
1103 if (!editor) return FALSE;
1105 error = editor_command_start(editor, editor->name, list, cb, data);
1107 if (EDITOR_ERRORS(error))
1109 gchar *text = g_strdup_printf(_("%s\n\"%s\""), editor_get_error_str(error), editor->file);
1111 file_util_warning_dialog(_("Invalid editor command"), text, GTK_STOCK_DIALOG_ERROR, NULL);
1118 EditorFlags start_editor_from_filelist(const gchar *key, GList *list)
1120 return start_editor_from_filelist_full(key, list, NULL, NULL);
1123 EditorFlags start_editor_from_file_full(const gchar *key, FileData *fd, EditorCallback cb, gpointer data)
1128 if (!fd) return FALSE;
1130 list = g_list_append(NULL, fd);
1131 error = start_editor_from_filelist_full(key, list, cb, data);
1136 EditorFlags start_editor_from_file(const gchar *key, FileData *fd)
1138 return start_editor_from_file_full(key, fd, NULL, NULL);
1141 gboolean editor_window_flag_set(const gchar *key)
1143 EditorDescription *editor;
1144 if (!key) return TRUE;
1146 editor = g_hash_table_lookup(editors, key);
1147 if (!editor) return TRUE;
1149 return !!(editor->flags & EDITOR_KEEP_FS);
1152 gboolean editor_is_filter(const gchar *key)
1154 EditorDescription *editor;
1155 if (!key) return TRUE;
1157 editor = g_hash_table_lookup(editors, key);
1158 if (!editor) return TRUE;
1160 return !!(editor->flags & EDITOR_DEST);
1163 const gchar *editor_get_error_str(EditorFlags flags)
1165 if (flags & EDITOR_ERROR_EMPTY) return _("Editor template is empty.");
1166 if (flags & EDITOR_ERROR_SYNTAX) return _("Editor template has incorrect syntax.");
1167 if (flags & EDITOR_ERROR_INCOMPATIBLE) return _("Editor template uses incompatible macros.");
1168 if (flags & EDITOR_ERROR_NO_FILE) return _("Can't find matching file type.");
1169 if (flags & EDITOR_ERROR_CANT_EXEC) return _("Can't execute external editor.");
1170 if (flags & EDITOR_ERROR_STATUS) return _("External editor returned error status.");
1171 if (flags & EDITOR_ERROR_SKIPPED) return _("File was skipped.");
1172 return _("Unknown error.");
1175 const gchar *editor_get_name(const gchar *key)
1177 EditorDescription *editor = g_hash_table_lookup(editors, key);
1179 if (!editor) return NULL;
1181 return editor->name;
1183 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */