Mercurial > geeqie.yaz
annotate src/editors.c @ 454:57459cf0952c
Prefix editor entries in the main menu with numbers.
It allows Alt+E+number as shortcut to start an editor.
author | zas_ |
---|---|
date | Sun, 20 Apr 2008 22:29:23 +0000 |
parents | 452b7ffeb7ad |
children | 48c8e49b571c |
rev | line source |
---|---|
9 | 1 /* |
196 | 2 * Geeqie |
123
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
3 * (C) 2006 John Ellis |
9 | 4 * |
5 * Author: John Ellis | |
6 * | |
7 * This software is released under the GNU General Public License (GNU GPL). | |
8 * Please read the included file COPYING for more information. | |
9 * This software comes with no warranty of any kind, use at your own risk! | |
10 */ | |
11 | |
12 | |
281 | 13 #include "main.h" |
9 | 14 #include "editors.h" |
15 | |
16 #include "utilops.h" | |
17 #include "ui_fileops.h" | |
18 #include "ui_spinner.h" | |
19 #include "ui_utildlg.h" | |
20 | |
140 | 21 #include "filelist.h" |
22 | |
9 | 23 #include <errno.h> |
24 | |
25 | |
26 #define EDITOR_WINDOW_WIDTH 500 | |
27 #define EDITOR_WINDOW_HEIGHT 300 | |
28 | |
140 | 29 #define COMMAND_SHELL "/bin/sh" |
9 | 30 #define COMMAND_OPT "-c" |
31 | |
32 | |
33 typedef struct _EditorVerboseData EditorVerboseData; | |
34 struct _EditorVerboseData { | |
35 GenericDialog *gd; | |
36 GtkWidget *button_close; | |
37 GtkWidget *button_stop; | |
38 GtkWidget *text; | |
39 GtkWidget *progress; | |
40 GtkWidget *spinner; | |
140 | 41 }; |
42 | |
43 typedef struct _EditorData EditorData; | |
44 struct _EditorData { | |
45 gint flags; | |
46 GPid pid; | |
47 gchar *command_template; | |
48 GList *list; | |
9 | 49 gint count; |
50 gint total; | |
140 | 51 gboolean stopping; |
52 EditorVerboseData *vd; | |
53 EditorCallback callback; | |
54 gpointer data; | |
9 | 55 }; |
56 | |
57 | |
283 | 58 static gchar *editor_slot_defaults[GQ_EDITOR_SLOTS * 2] = { |
416 | 59 N_("The Gimp"), "gimp-remote %{.cr2;.crw;.nef;.raw;*}f", |
9 | 60 N_("XV"), "xv %f", |
61 N_("Xpaint"), "xpaint %f", | |
147
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
62 N_("UFraw"), "ufraw %{.cr2;.crw;.nef;.raw}p", |
190
c2923efebfdc
whitelist of files that can have an xmp sidecar, sample external command
nadvornik
parents:
147
diff
changeset
|
63 N_("Add XMP sidecar"), "%vFILE=%{.cr2;.crw;.nef;.raw}p;XMP=`echo \"$FILE\"|sed -e 's|\\.[^.]*$|.xmp|'`; exiftool -tagsfromfile \"$FILE\" \"$XMP\"", |
9 | 64 NULL, NULL, |
65 NULL, NULL, | |
66 NULL, NULL, | |
147
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
67 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", |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
68 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", |
134
9009856628f7
started implementation of external commands; external Delete should work
nadvornik
parents:
123
diff
changeset
|
69 /* special slots */ |
136 | 70 #if 1 |
71 /* for testing */ | |
261 | 72 N_("External Copy command"), "%vset -x;cp %p %d", |
73 N_("External Move command"), "%vset -x;mv %p %d", | |
74 N_("External Rename command"), "%vset -x;mv %p %d", | |
440
3d16af3b133a
rm as external delete command is too dangerous, even for testing
nadvornik
parents:
416
diff
changeset
|
75 N_("External Delete command"), NULL, |
261 | 76 N_("External New Folder command"), NULL |
136 | 77 #else |
261 | 78 N_("External Copy command"), NULL, |
79 N_("External Move command"), NULL, | |
80 N_("External Rename command"), NULL, | |
81 N_("External Delete command"), NULL, | |
82 N_("External New Folder command"), NULL | |
136 | 83 #endif |
9 | 84 }; |
85 | |
140 | 86 static void editor_verbose_window_progress(EditorData *ed, const gchar *text); |
87 static gint editor_command_next_start(EditorData *ed); | |
88 static gint editor_command_next_finish(EditorData *ed, gint status); | |
89 static gint editor_command_done(EditorData *ed); | |
9 | 90 |
91 /* | |
92 *----------------------------------------------------------------------------- | |
93 * external editor routines | |
94 *----------------------------------------------------------------------------- | |
95 */ | |
96 | |
97 void editor_reset_defaults(void) | |
98 { | |
99 gint i; | |
100 | |
283 | 101 for (i = 0; i < GQ_EDITOR_SLOTS; i++) |
9 | 102 { |
318 | 103 g_free(options->editor_name[i]); |
104 options->editor_name[i] = g_strdup(_(editor_slot_defaults[i * 2])); | |
105 g_free(options->editor_command[i]); | |
106 options->editor_command[i] = g_strdup(editor_slot_defaults[i * 2 + 1]); | |
9 | 107 } |
108 } | |
109 | |
140 | 110 static void editor_verbose_data_free(EditorData *ed) |
111 { | |
112 if (!ed->vd) return; | |
113 g_free(ed->vd); | |
114 ed->vd = NULL; | |
115 } | |
116 | |
117 static void editor_data_free(EditorData *ed) | |
118 { | |
119 editor_verbose_data_free(ed); | |
120 g_free(ed->command_template); | |
121 g_free(ed); | |
122 } | |
123 | |
9 | 124 static void editor_verbose_window_close(GenericDialog *gd, gpointer data) |
125 { | |
140 | 126 EditorData *ed = data; |
9 | 127 |
128 generic_dialog_close(gd); | |
140 | 129 editor_verbose_data_free(ed); |
130 if (ed->pid == -1) editor_data_free(ed); /* the process has already terminated */ | |
9 | 131 } |
132 | |
133 static void editor_verbose_window_stop(GenericDialog *gd, gpointer data) | |
134 { | |
140 | 135 EditorData *ed = data; |
136 ed->stopping = TRUE; | |
137 ed->count = 0; | |
138 editor_verbose_window_progress(ed, _("stopping...")); | |
9 | 139 } |
140 | |
141 static void editor_verbose_window_enable_close(EditorVerboseData *vd) | |
142 { | |
143 vd->gd->cancel_cb = editor_verbose_window_close; | |
144 | |
145 spinner_set_interval(vd->spinner, -1); | |
146 gtk_widget_set_sensitive(vd->button_stop, FALSE); | |
147 gtk_widget_set_sensitive(vd->button_close, TRUE); | |
148 } | |
149 | |
140 | 150 static EditorVerboseData *editor_verbose_window(EditorData *ed, const gchar *text) |
9 | 151 { |
152 EditorVerboseData *vd; | |
153 GtkWidget *scrolled; | |
154 GtkWidget *hbox; | |
155 gchar *buf; | |
156 | |
157 vd = g_new0(EditorVerboseData, 1); | |
158 | |
254
9faf34f047b1
Make the wmclass value unique among the code by defining
zas_
parents:
238
diff
changeset
|
159 vd->gd = file_util_gen_dlg(_("Edit command results"), GQ_WMCLASS, "editor_results", |
9 | 160 NULL, FALSE, |
140 | 161 NULL, ed); |
9 | 162 buf = g_strdup_printf(_("Output of %s"), text); |
163 generic_dialog_add_message(vd->gd, NULL, buf, NULL); | |
164 g_free(buf); | |
165 vd->button_stop = generic_dialog_add_button(vd->gd, GTK_STOCK_STOP, NULL, | |
166 editor_verbose_window_stop, FALSE); | |
167 gtk_widget_set_sensitive(vd->button_stop, FALSE); | |
168 vd->button_close = generic_dialog_add_button(vd->gd, GTK_STOCK_CLOSE, NULL, | |
169 editor_verbose_window_close, TRUE); | |
170 gtk_widget_set_sensitive(vd->button_close, FALSE); | |
171 | |
172 scrolled = gtk_scrolled_window_new(NULL, NULL); | |
173 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN); | |
174 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled), | |
175 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); | |
176 gtk_box_pack_start(GTK_BOX(vd->gd->vbox), scrolled, TRUE, TRUE, 5); | |
177 gtk_widget_show(scrolled); | |
178 | |
179 vd->text = gtk_text_view_new(); | |
180 gtk_text_view_set_editable(GTK_TEXT_VIEW(vd->text), FALSE); | |
181 gtk_widget_set_size_request(vd->text, EDITOR_WINDOW_WIDTH, EDITOR_WINDOW_HEIGHT); | |
182 gtk_container_add(GTK_CONTAINER(scrolled), vd->text); | |
183 gtk_widget_show(vd->text); | |
184 | |
185 hbox = gtk_hbox_new(FALSE, 0); | |
186 gtk_box_pack_start(GTK_BOX(vd->gd->vbox), hbox, FALSE, FALSE, 0); | |
187 gtk_widget_show(hbox); | |
188 | |
189 vd->progress = gtk_progress_bar_new(); | |
190 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(vd->progress), 0.0); | |
191 gtk_box_pack_start(GTK_BOX(hbox), vd->progress, TRUE, TRUE, 0); | |
192 gtk_widget_show(vd->progress); | |
193 | |
194 vd->spinner = spinner_new(NULL, SPINNER_SPEED); | |
195 gtk_box_pack_start(GTK_BOX(hbox), vd->spinner, FALSE, FALSE, 0); | |
196 gtk_widget_show(vd->spinner); | |
442 | 197 |
9 | 198 gtk_widget_show(vd->gd->dialog); |
199 | |
140 | 200 ed->vd = vd; |
9 | 201 return vd; |
202 } | |
203 | |
204 static void editor_verbose_window_fill(EditorVerboseData *vd, gchar *text, gint len) | |
205 { | |
206 GtkTextBuffer *buffer; | |
207 GtkTextIter iter; | |
208 | |
209 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(vd->text)); | |
210 gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1); | |
211 gtk_text_buffer_insert(buffer, &iter, text, len); | |
212 } | |
213 | |
140 | 214 static void editor_verbose_window_progress(EditorData *ed, const gchar *text) |
9 | 215 { |
140 | 216 if (!ed->vd) return; |
217 | |
218 if (ed->total) | |
9 | 219 { |
140 | 220 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ed->vd->progress), (double)ed->count / ed->total); |
9 | 221 } |
222 | |
140 | 223 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(ed->vd->progress), (text) ? text : ""); |
9 | 224 } |
225 | |
226 static gboolean editor_verbose_io_cb(GIOChannel *source, GIOCondition condition, gpointer data) | |
227 { | |
140 | 228 EditorData *ed = data; |
9 | 229 gchar buf[512]; |
230 gsize count; | |
231 | |
140 | 232 if (condition & G_IO_IN) |
9 | 233 { |
140 | 234 while (g_io_channel_read_chars(source, buf, sizeof(buf), &count, NULL) == G_IO_STATUS_NORMAL) |
235 { | |
236 if (!g_utf8_validate(buf, count, NULL)) | |
9 | 237 { |
140 | 238 gchar *utf8; |
444 | 239 |
140 | 240 utf8 = g_locale_to_utf8(buf, count, NULL, NULL, NULL); |
241 if (utf8) | |
9 | 242 { |
140 | 243 editor_verbose_window_fill(ed->vd, utf8, -1); |
244 g_free(utf8); | |
9 | 245 } |
246 else | |
247 { | |
288
d1f74154463e
Replace occurences of Geeqie / geeqie by constants defined in main.h.
zas_
parents:
283
diff
changeset
|
248 editor_verbose_window_fill(ed->vd, "Error converting text to valid utf8\n", -1); |
9 | 249 } |
250 } | |
140 | 251 else |
252 { | |
253 editor_verbose_window_fill(ed->vd, buf, count); | |
254 } | |
255 } | |
9 | 256 } |
257 | |
140 | 258 if (condition & (G_IO_ERR | G_IO_HUP)) |
9 | 259 { |
140 | 260 g_io_channel_shutdown(source, TRUE, NULL); |
9 | 261 return FALSE; |
262 } | |
263 | |
264 return TRUE; | |
265 } | |
266 | |
138 | 267 typedef enum { |
268 PATH_FILE, | |
140 | 269 PATH_DEST |
138 | 270 } PathType; |
271 | |
272 | |
140 | 273 static gchar *editor_command_path_parse(const FileData *fd, PathType type, const gchar *extensions) |
123
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
274 { |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
275 GString *string; |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
276 gchar *pathl; |
147
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
277 const gchar *p = NULL; |
123
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
278 |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
279 string = g_string_new(""); |
442 | 280 |
138 | 281 if (type == PATH_FILE) |
282 { | |
147
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
283 GList *ext_list = filter_to_list(extensions); |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
284 GList *work = ext_list; |
442 | 285 |
147
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
286 if (!work) |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
287 p = fd->path; |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
288 else |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
289 { |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
290 while(work) |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
291 { |
444 | 292 GList *work2; |
147
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
293 gchar *ext = work->data; |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
294 work = work->next; |
442 | 295 |
296 if (strcmp(ext, "*") == 0 || | |
147
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
297 strcasecmp(ext, fd->extension) == 0) |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
298 { |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
299 p = fd->path; |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
300 break; |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
301 } |
442 | 302 |
444 | 303 work2 = fd->sidecar_files; |
147
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
304 while(work2) |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
305 { |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
306 FileData *sfd = work2->data; |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
307 work2 = work2->next; |
442 | 308 |
147
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
309 if (strcasecmp(ext, sfd->extension) == 0) |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
310 { |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
311 p = sfd->path; |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
312 break; |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
313 } |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
314 } |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
315 if (p) break; |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
316 } |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
317 string_list_free(ext_list); |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
318 if (!p) return NULL; |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
319 } |
138 | 320 } |
140 | 321 else if (type == PATH_DEST) |
138 | 322 { |
323 if (fd->change && fd->change->dest) | |
324 p = fd->change->dest; | |
325 else | |
326 p = ""; | |
327 } | |
444 | 328 |
123
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
329 while (*p != '\0') |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
330 { |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
331 /* must escape \, ", `, and $ to avoid problems, |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
332 * we assume system shell supports bash-like escaping |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
333 */ |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
334 if (strchr("\\\"`$", *p) != NULL) |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
335 { |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
336 string = g_string_append_c(string, '\\'); |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
337 } |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
338 string = g_string_append_c(string, *p); |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
339 p++; |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
340 } |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
341 |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
342 pathl = path_from_utf8(string->str); |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
343 g_string_free(string, TRUE); |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
344 |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
345 return pathl; |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
346 } |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
347 |
9 | 348 |
349 /* | |
350 * The supported macros for editor commands: | |
351 * | |
352 * %f first occurence replaced by quoted sequence of filenames, command is run once. | |
353 * only one occurence of this macro is supported. | |
354 * ([ls %f] results in [ls "file1" "file2" ... "lastfile"]) | |
355 * %p command is run for each filename in turn, each instance replaced with single filename. | |
356 * multiple occurences of this macro is supported for complex shell commands. | |
357 * This macro will BLOCK THE APPLICATION until it completes, since command is run once | |
358 * for every file in syncronous order. To avoid blocking add the %v macro, below. | |
359 * ([ls %p] results in [ls "file1"], [ls "file2"] ... [ls "lastfile"]) | |
360 * none if no macro is supplied, the result is equivalent to "command %f" | |
361 * ([ls] results in [ls "file1" "file2" ... "lastfile"]) | |
362 * | |
363 * Only one of the macros %f or %p may be used in a given commmand. | |
364 * | |
60
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
365 * %v must be the first two characters[1] in a command, causes a window to display |
9 | 366 * showing the output of the command(s). |
367 * %V same as %v except in the case of %p only displays a window for multiple files, | |
368 * operating on a single file is suppresses the output dialog. | |
60
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
369 * |
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
370 * %w must be first two characters in a command, presence will disable full screen |
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
371 * from exiting upon invocation. |
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
372 * |
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
373 * |
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
374 * [1] Note: %v,%V may also be preceded by "%w". |
9 | 375 */ |
140 | 376 |
377 | |
378 gint editor_command_parse(const gchar *template, GList *list, gchar **output) | |
9 | 379 { |
140 | 380 gint flags = 0; |
381 const gchar *p = template; | |
382 GString *result = NULL; | |
383 gchar *extensions = NULL; | |
442 | 384 |
140 | 385 if (output) |
386 result = g_string_new(""); | |
387 | |
442 | 388 if (!template || template[0] == '\0') |
140 | 389 { |
390 flags |= EDITOR_ERROR_EMPTY; | |
391 goto err; | |
392 } | |
9 | 393 |
442 | 394 |
140 | 395 /* global flags */ |
396 while (*p == '%') | |
397 { | |
398 switch (*++p) | |
399 { | |
400 case 'w': | |
401 flags |= EDITOR_KEEP_FS; | |
402 p++; | |
403 break; | |
404 case 'v': | |
405 flags |= EDITOR_VERBOSE; | |
406 p++; | |
407 break; | |
408 case 'V': | |
409 flags |= EDITOR_VERBOSE_MULTI; | |
410 p++; | |
411 break; | |
412 } | |
413 } | |
442 | 414 |
140 | 415 /* command */ |
442 | 416 |
140 | 417 while (*p) |
418 { | |
419 if (*p != '%') | |
420 { | |
421 if (output) result = g_string_append_c(result, *p); | |
422 } | |
423 else /* *p == '%' */ | |
424 { | |
425 extensions = NULL; | |
426 gchar *pathl = NULL; | |
9 | 427 |
140 | 428 p++; |
442 | 429 |
147
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
430 /* for example "%f" or "%{.crw;.raw;.cr2}f" */ |
140 | 431 if (*p == '{') |
432 { | |
444 | 433 gchar *end; |
434 | |
140 | 435 p++; |
444 | 436 end = strchr(p, '}'); |
140 | 437 if (!end) |
438 { | |
439 flags |= EDITOR_ERROR_SYNTAX; | |
440 goto err; | |
441 } | |
442 | 442 |
140 | 443 extensions = g_strndup(p, end - p); |
444 p = end + 1; | |
445 } | |
442 | 446 |
447 switch (*p) | |
140 | 448 { |
449 case 'd': | |
450 flags |= EDITOR_DEST; | |
451 case 'p': | |
452 flags |= EDITOR_FOR_EACH; | |
453 if (flags & EDITOR_SINGLE_COMMAND) | |
454 { | |
455 flags |= EDITOR_ERROR_INCOMPATIBLE; | |
456 goto err; | |
457 } | |
458 if (output) | |
459 { | |
460 /* use the first file from the list */ | |
442 | 461 if (!list || !list->data) |
140 | 462 { |
463 flags |= EDITOR_ERROR_NO_FILE; | |
464 goto err; | |
465 } | |
466 pathl = editor_command_path_parse((FileData *)list->data, (*p == 'd') ? PATH_DEST : PATH_FILE, extensions); | |
442 | 467 if (!pathl) |
140 | 468 { |
469 flags |= EDITOR_ERROR_NO_FILE; | |
470 goto err; | |
471 } | |
472 result = g_string_append_c(result, '"'); | |
473 result = g_string_append(result, pathl); | |
474 g_free(pathl); | |
475 result = g_string_append_c(result, '"'); | |
476 } | |
442 | 477 break; |
60
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
478 |
140 | 479 case 'f': |
480 flags |= EDITOR_SINGLE_COMMAND; | |
481 if (flags & (EDITOR_FOR_EACH | EDITOR_DEST)) | |
482 { | |
483 flags |= EDITOR_ERROR_INCOMPATIBLE; | |
484 goto err; | |
485 } | |
486 | |
487 if (output) | |
488 { | |
489 /* use whole list */ | |
490 GList *work = list; | |
491 gboolean ok = FALSE; | |
444 | 492 |
140 | 493 while (work) |
494 { | |
495 FileData *fd = work->data; | |
496 pathl = editor_command_path_parse(fd, PATH_FILE, extensions); | |
497 | |
498 if (pathl) | |
499 { | |
500 ok = TRUE; | |
501 if (work != list) g_string_append_c(result, ' '); | |
502 result = g_string_append_c(result, '"'); | |
503 result = g_string_append(result, pathl); | |
504 g_free(pathl); | |
505 result = g_string_append_c(result, '"'); | |
506 } | |
507 work = work->next; | |
508 } | |
442 | 509 if (!ok) |
140 | 510 { |
511 flags |= EDITOR_ERROR_NO_FILE; | |
512 goto err; | |
513 } | |
514 } | |
442 | 515 break; |
140 | 516 default: |
517 flags |= EDITOR_ERROR_SYNTAX; | |
518 goto err; | |
519 } | |
520 if (extensions) g_free(extensions); | |
521 extensions = NULL; | |
522 } | |
523 p++; | |
9 | 524 } |
525 | |
140 | 526 if (output) *output = g_string_free(result, FALSE); |
527 return flags; | |
528 | |
442 | 529 |
140 | 530 err: |
442 | 531 if (output) |
9 | 532 { |
140 | 533 g_string_free(result, TRUE); |
534 *output = NULL; | |
535 } | |
536 if (extensions) g_free(extensions); | |
537 return flags; | |
538 } | |
539 | |
540 static void editor_child_exit_cb (GPid pid, gint status, gpointer data) | |
541 { | |
542 EditorData *ed = data; | |
543 g_spawn_close_pid(pid); | |
544 ed->pid = -1; | |
442 | 545 |
140 | 546 editor_command_next_finish(ed, status); |
547 } | |
548 | |
549 | |
550 static gint editor_command_one(const gchar *template, GList *list, EditorData *ed) | |
551 { | |
552 gchar *command; | |
553 FileData *fd = list->data; | |
554 GPid pid; | |
442 | 555 gint standard_output; |
556 gint standard_error; | |
140 | 557 gboolean ok; |
558 | |
559 ed->pid = -1; | |
560 ed->flags = editor_command_parse(template, list, &command); | |
561 | |
562 ok = !(ed->flags & EDITOR_ERROR_MASK); | |
563 | |
564 if (ok) | |
565 { | |
443 | 566 gchar *working_directory; |
567 gchar *args[4]; | |
568 | |
569 working_directory = remove_level_from_path(fd->path); | |
570 args[0] = COMMAND_SHELL; | |
571 args[1] = COMMAND_OPT; | |
572 args[2] = command; | |
573 args[3] = NULL; | |
574 | |
442 | 575 ok = g_spawn_async_with_pipes(working_directory, args, NULL, |
140 | 576 G_SPAWN_DO_NOT_REAP_CHILD, /* GSpawnFlags */ |
442 | 577 NULL, NULL, |
578 &pid, | |
579 NULL, | |
580 ed->vd ? &standard_output : NULL, | |
581 ed->vd ? &standard_error : NULL, | |
140 | 582 NULL); |
443 | 583 |
584 g_free(working_directory); | |
442 | 585 |
140 | 586 if (!ok) ed->flags |= EDITOR_ERROR_CANT_EXEC; |
587 } | |
588 | |
442 | 589 if (ok) |
140 | 590 { |
591 g_child_watch_add(pid, editor_child_exit_cb, ed); | |
592 ed->pid = pid; | |
593 } | |
442 | 594 |
140 | 595 if (ed->vd) |
596 { | |
597 if (!ok) | |
9 | 598 { |
140 | 599 gchar *buf; |
600 | |
147
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
601 buf = g_strdup_printf(_("Failed to run command:\n%s\n"), template); |
140 | 602 editor_verbose_window_fill(ed->vd, buf, strlen(buf)); |
603 g_free(buf); | |
604 | |
605 } | |
442 | 606 else |
140 | 607 { |
608 GIOChannel *channel_output; | |
609 GIOChannel *channel_error; | |
444 | 610 |
140 | 611 channel_output = g_io_channel_unix_new(standard_output); |
612 g_io_channel_set_flags(channel_output, G_IO_FLAG_NONBLOCK, NULL); | |
613 | |
614 g_io_add_watch_full(channel_output, G_PRIORITY_HIGH, G_IO_IN | G_IO_ERR | G_IO_HUP, | |
615 editor_verbose_io_cb, ed, NULL); | |
616 g_io_channel_unref(channel_output); | |
617 | |
618 channel_error = g_io_channel_unix_new(standard_error); | |
619 g_io_channel_set_flags(channel_error, G_IO_FLAG_NONBLOCK, NULL); | |
620 | |
621 g_io_add_watch_full(channel_error, G_PRIORITY_HIGH, G_IO_IN | G_IO_ERR | G_IO_HUP, | |
622 editor_verbose_io_cb, ed, NULL); | |
623 g_io_channel_unref(channel_error); | |
624 } | |
625 } | |
442 | 626 |
140 | 627 g_free(command); |
628 | |
629 return ed->flags & EDITOR_ERROR_MASK; | |
630 } | |
631 | |
632 static gint editor_command_next_start(EditorData *ed) | |
633 { | |
634 if (ed->vd) editor_verbose_window_fill(ed->vd, "\n", 1); | |
635 | |
636 if (ed->list && ed->count < ed->total) | |
637 { | |
638 FileData *fd; | |
639 gint error; | |
640 | |
641 fd = ed->list->data; | |
642 | |
643 if (ed->vd) | |
644 { | |
645 editor_verbose_window_progress(ed, (ed->flags & EDITOR_FOR_EACH) ? fd->path : _("running...")); | |
646 } | |
647 ed->count++; | |
648 | |
649 error = editor_command_one(ed->command_template, ed->list, ed); | |
650 if (!error && ed->vd) | |
651 { | |
652 gtk_widget_set_sensitive(ed->vd->button_stop, (ed->list != NULL) ); | |
653 if (ed->flags & EDITOR_FOR_EACH) | |
9 | 654 { |
140 | 655 editor_verbose_window_fill(ed->vd, fd->path, strlen(fd->path)); |
656 editor_verbose_window_fill(ed->vd, "\n", 1); | |
9 | 657 } |
658 } | |
140 | 659 |
442 | 660 if (!error) |
140 | 661 return 0; |
662 else | |
663 /* command was not started, call the finish immediately */ | |
664 return editor_command_next_finish(ed, 0); | |
665 } | |
442 | 666 |
140 | 667 /* everything is done */ |
237
404629011caa
Add missing return at the end of editor_command_next_start().
zas_
parents:
196
diff
changeset
|
668 return editor_command_done(ed); |
140 | 669 } |
670 | |
671 static gint editor_command_next_finish(EditorData *ed, gint status) | |
672 { | |
673 gint cont = ed->stopping ? EDITOR_CB_SKIP : EDITOR_CB_CONTINUE; | |
674 | |
675 if (status) | |
676 ed->flags |= EDITOR_ERROR_STATUS; | |
677 | |
678 if (ed->flags & EDITOR_FOR_EACH) | |
679 { | |
680 /* handle the first element from the list */ | |
681 GList *fd_element = ed->list; | |
444 | 682 |
140 | 683 ed->list = g_list_remove_link(ed->list, fd_element); |
684 if (ed->callback) | |
685 cont = ed->callback(ed->list ? ed : NULL, ed->flags, fd_element, ed->data); | |
686 filelist_free(fd_element); | |
9 | 687 } |
688 else | |
689 { | |
140 | 690 /* handle whole list */ |
691 if (ed->callback) | |
692 cont = ed->callback(NULL, ed->flags, ed->list, ed->data); | |
693 filelist_free(ed->list); | |
694 ed->list = NULL; | |
695 } | |
9 | 696 |
140 | 697 if (cont == EDITOR_CB_SUSPEND) |
698 return ed->flags & EDITOR_ERROR_MASK; | |
699 else if (cont == EDITOR_CB_SKIP) | |
700 return editor_command_done(ed); | |
701 else | |
702 return editor_command_next_start(ed); | |
703 } | |
9 | 704 |
140 | 705 static gint editor_command_done(EditorData *ed) |
706 { | |
707 gint flags; | |
9 | 708 |
140 | 709 if (ed->vd) |
710 { | |
444 | 711 const gchar *text; |
712 | |
140 | 713 if (ed->count == ed->total) |
9 | 714 { |
140 | 715 text = _("done"); |
9 | 716 } |
717 else | |
718 { | |
140 | 719 text = _("stopped by user"); |
9 | 720 } |
140 | 721 editor_verbose_window_progress(ed, text); |
722 editor_verbose_window_enable_close(ed->vd); | |
723 } | |
724 | |
725 /* free the not-handled items */ | |
726 if (ed->list) | |
727 { | |
728 ed->flags |= EDITOR_ERROR_SKIPPED; | |
729 if (ed->callback) ed->callback(NULL, ed->flags, ed->list, ed->data); | |
730 filelist_free(ed->list); | |
731 ed->list = NULL; | |
732 } | |
9 | 733 |
140 | 734 ed->count = 0; |
735 | |
736 flags = ed->flags & EDITOR_ERROR_MASK; | |
737 | |
738 if (!ed->vd) editor_data_free(ed); | |
739 | |
740 return flags; | |
741 } | |
742 | |
743 void editor_resume(gpointer ed) | |
744 { | |
745 editor_command_next_start(ed); | |
746 } | |
443 | 747 |
140 | 748 void editor_skip(gpointer ed) |
749 { | |
442 | 750 editor_command_done(ed); |
9 | 751 } |
752 | |
140 | 753 static gint editor_command_start(const gchar *template, const gchar *text, GList *list, EditorCallback cb, gpointer data) |
754 { | |
755 EditorData *ed; | |
756 gint flags = editor_command_parse(template, NULL, NULL); | |
442 | 757 |
140 | 758 if (flags & EDITOR_ERROR_MASK) return flags & EDITOR_ERROR_MASK; |
759 | |
760 ed = g_new0(EditorData, 1); | |
761 ed->list = filelist_copy(list); | |
762 ed->flags = flags; | |
763 ed->command_template = g_strdup(template); | |
764 ed->total = (flags & EDITOR_SINGLE_COMMAND) ? 1 : g_list_length(list); | |
765 ed->count = 0; | |
766 ed->stopping = FALSE; | |
767 ed->callback = cb; | |
768 ed->data = data; | |
442 | 769 |
140 | 770 if ((flags & EDITOR_VERBOSE_MULTI) && list && list->next) |
771 flags |= EDITOR_VERBOSE; | |
442 | 772 |
140 | 773 if (flags & EDITOR_VERBOSE) |
774 editor_verbose_window(ed, text); | |
442 | 775 |
776 editor_command_next_start(ed); | |
140 | 777 /* errors from editor_command_next_start will be handled via callback */ |
778 return flags & EDITOR_ERROR_MASK; | |
779 } | |
780 | |
444 | 781 static gint is_valid_editor_command(gint n) |
782 { | |
783 return (n >= 0 && n < GQ_EDITOR_SLOTS | |
784 && options->editor_command[n] | |
785 && strlen(options->editor_command[n]) > 0); | |
786 } | |
787 | |
140 | 788 gint start_editor_from_filelist_full(gint n, GList *list, EditorCallback cb, gpointer data) |
9 | 789 { |
790 gchar *command; | |
140 | 791 gint error; |
9 | 792 |
444 | 793 if (!list) return FALSE; |
794 if (!is_valid_editor_command(n)) return FALSE; | |
9 | 795 |
318 | 796 command = g_locale_from_utf8(options->editor_command[n], -1, NULL, NULL, NULL); |
797 error = editor_command_start(command, options->editor_name[n], list, cb, data); | |
9 | 798 g_free(command); |
140 | 799 return error; |
9 | 800 } |
801 | |
140 | 802 gint start_editor_from_filelist(gint n, GList *list) |
803 { | |
804 return start_editor_from_filelist_full(n, list, NULL, NULL); | |
805 } | |
806 | |
807 gint start_editor_from_file_full(gint n, FileData *fd, EditorCallback cb, gpointer data) | |
9 | 808 { |
809 GList *list; | |
140 | 810 gint error; |
9 | 811 |
138 | 812 if (!fd) return FALSE; |
9 | 813 |
138 | 814 list = g_list_append(NULL, fd); |
140 | 815 error = start_editor_from_filelist_full(n, list, cb, data); |
9 | 816 g_list_free(list); |
140 | 817 return error; |
9 | 818 } |
60
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
819 |
140 | 820 gint start_editor_from_file(gint n, FileData *fd) |
136 | 821 { |
140 | 822 return start_editor_from_file_full(n, fd, NULL, NULL); |
136 | 823 } |
824 | |
60
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
825 gint editor_window_flag_set(gint n) |
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
826 { |
444 | 827 if (!is_valid_editor_command(n)) return TRUE; |
60
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
828 |
318 | 829 return (editor_command_parse(options->editor_command[n], NULL, NULL) & EDITOR_KEEP_FS); |
60
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
830 } |
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
831 |
140 | 832 const gchar *editor_get_error_str(gint flags) |
833 { | |
834 if (flags & EDITOR_ERROR_EMPTY) return _("Editor template is empty."); | |
835 if (flags & EDITOR_ERROR_SYNTAX) return _("Editor template has incorrect syntax."); | |
836 if (flags & EDITOR_ERROR_INCOMPATIBLE) return _("Editor template uses incompatible macros."); | |
837 if (flags & EDITOR_ERROR_NO_FILE) return _("Can't find matching file type."); | |
838 if (flags & EDITOR_ERROR_CANT_EXEC) return _("Can't execute external editor."); | |
839 if (flags & EDITOR_ERROR_STATUS) return _("External editor returned error status."); | |
840 if (flags & EDITOR_ERROR_SKIPPED) return _("File was skipped."); | |
841 return _("Unknown error."); | |
842 } |