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