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