Mercurial > geeqie
annotate src/editors.c @ 1316:732083928afb
Enclose macro parameters names with _ to differentiate them from variable names.
author | zas_ |
---|---|
date | Tue, 24 Feb 2009 18:00:45 +0000 |
parents | e8f21b91885d |
children | fe4da037be21 |
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 |
1284 | 4 * Copyright (C) 2008 - 2009 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" | |
1022
9962b24b6b43
Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
1000
diff
changeset
|
19 #include "misc.h" |
9 | 20 #include "ui_fileops.h" |
21 #include "ui_spinner.h" | |
22 #include "ui_utildlg.h" | |
1022
9962b24b6b43
Move miscellaneous functions to their own files (new misc.[ch]).
zas_
parents:
1000
diff
changeset
|
23 #include "utilops.h" |
9 | 24 |
25 #include <errno.h> | |
26 | |
27 | |
28 #define EDITOR_WINDOW_WIDTH 500 | |
29 #define EDITOR_WINDOW_HEIGHT 300 | |
30 | |
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 GList *list; | |
9 | 48 gint count; |
49 gint total; | |
140 | 50 gboolean stopping; |
51 EditorVerboseData *vd; | |
52 EditorCallback callback; | |
53 gpointer data; | |
1272 | 54 const EditorDescription *editor; |
9 | 55 }; |
56 | |
57 | |
140 | 58 static void editor_verbose_window_progress(EditorData *ed, const gchar *text); |
59 static gint editor_command_next_start(EditorData *ed); | |
60 static gint editor_command_next_finish(EditorData *ed, gint status); | |
61 static gint editor_command_done(EditorData *ed); | |
1272 | 62 static gint editor_command_parse(const EditorDescription *editor, GList *list, gchar **output); |
9 | 63 |
64 /* | |
65 *----------------------------------------------------------------------------- | |
66 * external editor routines | |
67 *----------------------------------------------------------------------------- | |
68 */ | |
69 | |
1272 | 70 GHashTable *editors = NULL; |
71 | |
72 #ifdef G_KEY_FILE_DESKTOP_GROUP | |
73 #define DESKTOP_GROUP G_KEY_FILE_DESKTOP_GROUP | |
74 #else | |
75 #define DESKTOP_GROUP "Desktop Entry" | |
76 #endif | |
77 | |
78 void editor_description_free(EditorDescription *editor) | |
768
ff51413f098d
Use functions to set editors name and command and ensure they are
zas_
parents:
766
diff
changeset
|
79 { |
1272 | 80 if (!editor) return; |
81 | |
82 g_free(editor->key); | |
83 g_free(editor->name); | |
1278
c5852c543775
Add support for NoDisplay (entry is then hidden) and Type (only Application type is valid here) keys.
zas_
parents:
1276
diff
changeset
|
84 g_free(editor->icon); |
1272 | 85 g_free(editor->exec); |
86 g_free(editor->menu_path); | |
87 g_free(editor->hotkey); | |
88 string_list_free(editor->ext_list); | |
89 g_free(editor->file); | |
90 g_free(editor); | |
91 } | |
768
ff51413f098d
Use functions to set editors name and command and ensure they are
zas_
parents:
766
diff
changeset
|
92 |
1272 | 93 static GList *editor_mime_types_to_extensions(gchar **mime_types) |
94 { | |
95 /* FIXME: this should be rewritten to use the shared mime database, as soon as we switch to gio */ | |
768
ff51413f098d
Use functions to set editors name and command and ensure they are
zas_
parents:
766
diff
changeset
|
96 |
1272 | 97 static const gchar *conv_table[][2] = { |
98 {"application/x-ufraw", "%raw"}, | |
99 {"image/*", "*"}, | |
100 {"image/bmp", ".bmp"}, | |
101 {"image/gif", ".gif"}, | |
102 {"image/jpeg", ".jpeg;.jpg"}, | |
103 {"image/jpg", ".jpg;.jpeg"}, | |
104 {"image/pcx", ".pcx"}, | |
105 {"image/png", ".png"}, | |
106 {"image/svg", ".svg"}, | |
107 {"image/svg+xml", ".svg"}, | |
108 {"image/svg+xml-compressed", ".svg"}, | |
109 {"image/tiff", ".tiff;.tif"}, | |
110 {"image/x-bmp", ".bmp"}, | |
111 {"image/x-canon-crw", ".crw"}, | |
112 {"image/x-cr2", ".cr2"}, | |
113 {"image/x-dcraw", "%raw"}, | |
114 {"image/x-ico", ".ico"}, | |
115 {"image/x-mrw", ".mrw"}, | |
116 {"image/x-MS-bmp", ".bmp"}, | |
117 {"image/x-nef", ".nef"}, | |
118 {"image/x-orf", ".orf"}, | |
119 {"image/x-pcx", ".pcx"}, | |
120 {"image/xpm", ".xpm"}, | |
121 {"image/x-png", ".png"}, | |
122 {"image/x-portable-anymap", ".pam"}, | |
123 {"image/x-portable-bitmap", ".pbm"}, | |
124 {"image/x-portable-graymap", ".pgm"}, | |
125 {"image/x-portable-pixmap", ".ppm"}, | |
126 {"image/x-psd", ".psd"}, | |
127 {"image/x-raf", ".raf"}, | |
128 {"image/x-sgi", ".sgi"}, | |
129 {"image/x-tga", ".tga"}, | |
130 {"image/x-xbitmap", ".xbm"}, | |
131 {"image/x-xcf", ".xcf"}, | |
132 {"image/x-xpixmap", ".xpm"}, | |
133 {"image/x-x3f", ".x3f"}, | |
134 {NULL, NULL}}; | |
135 | |
136 gint i, j; | |
137 GList *list = NULL; | |
138 | |
139 for (i = 0; mime_types[i]; i++) | |
140 for (j = 0; conv_table[j][0]; j++) | |
141 if (strcmp(mime_types[i], conv_table[j][0]) == 0) | |
142 list = g_list_concat(list, filter_to_list(conv_table[j][1])); | |
143 | |
144 return list; | |
768
ff51413f098d
Use functions to set editors name and command and ensure they are
zas_
parents:
766
diff
changeset
|
145 } |
ff51413f098d
Use functions to set editors name and command and ensure they are
zas_
parents:
766
diff
changeset
|
146 |
1272 | 147 static gboolean editor_read_desktop_file(const gchar *path) |
768
ff51413f098d
Use functions to set editors name and command and ensure they are
zas_
parents:
766
diff
changeset
|
148 { |
1272 | 149 GKeyFile *key_file; |
150 EditorDescription *editor; | |
151 gchar *extensions; | |
1278
c5852c543775
Add support for NoDisplay (entry is then hidden) and Type (only Application type is valid here) keys.
zas_
parents:
1276
diff
changeset
|
152 gchar *type; |
1272 | 153 const gchar *key = filename_from_path(path); |
154 gchar **categories, **only_show_in, **not_show_in; | |
1274 | 155 gchar *try_exec; |
1272 | 156 |
157 if (g_hash_table_lookup(editors, key)) return FALSE; /* the file found earlier wins */ | |
158 | |
159 key_file = g_key_file_new(); | |
160 if (!g_key_file_load_from_file(key_file, path, 0, NULL)) | |
161 { | |
162 g_key_file_free(key_file); | |
163 return FALSE; | |
164 } | |
1278
c5852c543775
Add support for NoDisplay (entry is then hidden) and Type (only Application type is valid here) keys.
zas_
parents:
1276
diff
changeset
|
165 |
c5852c543775
Add support for NoDisplay (entry is then hidden) and Type (only Application type is valid here) keys.
zas_
parents:
1276
diff
changeset
|
166 type = g_key_file_get_string(key_file, DESKTOP_GROUP, "Type", NULL); |
c5852c543775
Add support for NoDisplay (entry is then hidden) and Type (only Application type is valid here) keys.
zas_
parents:
1276
diff
changeset
|
167 if (!type || strcmp(type, "Application") != 0) |
c5852c543775
Add support for NoDisplay (entry is then hidden) and Type (only Application type is valid here) keys.
zas_
parents:
1276
diff
changeset
|
168 { |
c5852c543775
Add support for NoDisplay (entry is then hidden) and Type (only Application type is valid here) keys.
zas_
parents:
1276
diff
changeset
|
169 /* We only consider desktop entries of Application type */ |
c5852c543775
Add support for NoDisplay (entry is then hidden) and Type (only Application type is valid here) keys.
zas_
parents:
1276
diff
changeset
|
170 return FALSE; |
c5852c543775
Add support for NoDisplay (entry is then hidden) and Type (only Application type is valid here) keys.
zas_
parents:
1276
diff
changeset
|
171 } |
1272 | 172 |
173 editor = g_new0(EditorDescription, 1); | |
174 | |
175 editor->key = g_strdup(key); | |
176 editor->file = g_strdup(path); | |
177 | |
178 g_hash_table_insert(editors, editor->key, editor); | |
179 | |
1278
c5852c543775
Add support for NoDisplay (entry is then hidden) and Type (only Application type is valid here) keys.
zas_
parents:
1276
diff
changeset
|
180 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "Hidden", NULL) |
c5852c543775
Add support for NoDisplay (entry is then hidden) and Type (only Application type is valid here) keys.
zas_
parents:
1276
diff
changeset
|
181 || g_key_file_get_boolean(key_file, DESKTOP_GROUP, "NoDisplay", NULL)) |
c5852c543775
Add support for NoDisplay (entry is then hidden) and Type (only Application type is valid here) keys.
zas_
parents:
1276
diff
changeset
|
182 { |
c5852c543775
Add support for NoDisplay (entry is then hidden) and Type (only Application type is valid here) keys.
zas_
parents:
1276
diff
changeset
|
183 editor->hidden = TRUE; |
c5852c543775
Add support for NoDisplay (entry is then hidden) and Type (only Application type is valid here) keys.
zas_
parents:
1276
diff
changeset
|
184 } |
1272 | 185 |
186 categories = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "Categories", NULL, NULL); | |
187 if (categories) | |
188 { | |
189 gboolean found = FALSE; | |
190 gint i; | |
191 for (i = 0; categories[i]; i++) | |
192 /* IMHO "Graphics" is exactly the category that we are interested in, so this does not have to be configurable */ | |
193 if (strcmp(categories[i], "Graphics") == 0 || | |
194 strcmp(categories[i], "X-Geeqie") == 0) | |
195 { | |
196 found = TRUE; | |
197 break; | |
198 } | |
199 if (!found) editor->hidden = TRUE; | |
200 g_strfreev(categories); | |
201 } | |
202 else | |
203 { | |
204 editor->hidden = TRUE; | |
205 } | |
206 | |
207 only_show_in = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "OnlyShowIn", NULL, NULL); | |
208 if (only_show_in) | |
209 { | |
210 gboolean found = FALSE; | |
211 gint i; | |
212 for (i = 0; only_show_in[i]; i++) | |
213 if (strcmp(only_show_in[i], "X-Geeqie") == 0) | |
214 { | |
215 found = TRUE; | |
216 break; | |
217 } | |
218 if (!found) editor->hidden = TRUE; | |
219 g_strfreev(only_show_in); | |
220 } | |
768
ff51413f098d
Use functions to set editors name and command and ensure they are
zas_
parents:
766
diff
changeset
|
221 |
1272 | 222 not_show_in = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "NotShowIn", NULL, NULL); |
223 if (not_show_in) | |
224 { | |
225 gboolean found = FALSE; | |
226 gint i; | |
227 for (i = 0; not_show_in[i]; i++) | |
228 if (strcmp(not_show_in[i], "X-Geeqie") == 0) | |
229 { | |
230 found = TRUE; | |
231 break; | |
232 } | |
233 if (found) editor->hidden = TRUE; | |
234 g_strfreev(not_show_in); | |
235 } | |
236 | |
1274 | 237 |
238 try_exec = g_key_file_get_string(key_file, DESKTOP_GROUP, "TryExec", NULL); | |
239 if (try_exec && !editor->hidden) | |
240 { | |
241 gchar *try_exec_res = g_find_program_in_path(try_exec); | |
242 if (!try_exec_res) editor->hidden = TRUE; | |
243 g_free(try_exec_res); | |
244 g_free(try_exec); | |
245 } | |
246 | |
1272 | 247 if (editor->hidden) |
248 { | |
249 /* hidden editors will be deleted, no need to parse the rest */ | |
250 g_key_file_free(key_file); | |
251 return TRUE; | |
252 } | |
253 | |
254 editor->name = g_key_file_get_locale_string(key_file, DESKTOP_GROUP, "Name", NULL, NULL); | |
255 editor->icon = g_key_file_get_string(key_file, DESKTOP_GROUP, "Icon", NULL); | |
256 | |
257 editor->exec = g_key_file_get_string(key_file, DESKTOP_GROUP, "Exec", NULL); | |
258 | |
259 /* we take only editors that accept parameters, FIXME: the test can be improved */ | |
260 if (!strchr(editor->exec, '%')) editor->hidden = TRUE; | |
261 | |
262 editor->menu_path = g_key_file_get_string(key_file, DESKTOP_GROUP, "X-Geeqie-Menu-Path", NULL); | |
263 if (!editor->menu_path) editor->menu_path = g_strdup("EditMenu/ExternalMenu"); | |
264 | |
265 editor->hotkey = g_key_file_get_string(key_file, DESKTOP_GROUP, "X-Geeqie-Hotkey", NULL); | |
266 | |
267 extensions = g_key_file_get_string(key_file, DESKTOP_GROUP, "X-Geeqie-File-Extensions", NULL); | |
268 if (extensions) | |
269 editor->ext_list = filter_to_list(extensions); | |
270 else | |
271 { | |
272 gchar **mime_types = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "MimeType", NULL, NULL); | |
273 if (mime_types) | |
274 { | |
275 editor->ext_list = editor_mime_types_to_extensions(mime_types); | |
276 g_strfreev(mime_types); | |
277 if (!editor->ext_list) editor->hidden = TRUE; | |
278 } | |
279 | |
280 } | |
281 | |
282 | |
283 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "X-Geeqie-Keep-Fullscreen", NULL)) editor->flags |= EDITOR_KEEP_FS; | |
284 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "X-Geeqie-Verbose", NULL)) editor->flags |= EDITOR_VERBOSE; | |
285 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "X-Geeqie-Verbose-Multi", NULL)) editor->flags |= EDITOR_VERBOSE_MULTI; | |
286 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "X-Geeqie-Filter", NULL)) editor->flags |= EDITOR_DEST; | |
287 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "Terminal", NULL)) editor->flags |= EDITOR_TERMINAL; | |
288 | |
289 | |
290 editor->flags |= editor_command_parse(editor, NULL, NULL); | |
291 g_key_file_free(key_file); | |
292 | |
293 return TRUE; | |
768
ff51413f098d
Use functions to set editors name and command and ensure they are
zas_
parents:
766
diff
changeset
|
294 } |
ff51413f098d
Use functions to set editors name and command and ensure they are
zas_
parents:
766
diff
changeset
|
295 |
1272 | 296 static gboolean editor_remove_desktop_file_cb(gpointer key, gpointer value, gpointer user_data) |
297 { | |
298 EditorDescription *editor = value; | |
299 return editor->hidden; | |
300 } | |
301 | |
302 static void editor_read_desktop_dir(const gchar *path) | |
9 | 303 { |
1272 | 304 DIR *dp; |
305 struct dirent *dir; | |
306 gchar *pathl; | |
307 | |
308 pathl = path_from_utf8(path); | |
309 dp = opendir(pathl); | |
310 g_free(pathl); | |
311 if (!dp) | |
312 { | |
313 /* dir not found */ | |
314 return; | |
315 } | |
316 while ((dir = readdir(dp)) != NULL) | |
317 { | |
318 gchar *namel = dir->d_name; | |
319 size_t len = strlen(namel); | |
320 | |
1307 | 321 if (len > 8 && g_ascii_strncasecmp(namel + len - 8, ".desktop", 8) == 0) |
1272 | 322 { |
323 gchar *name = path_to_utf8(namel); | |
324 gchar *dpath = g_build_filename(path, name, NULL); | |
325 editor_read_desktop_file(dpath); | |
326 g_free(dpath); | |
327 g_free(name); | |
328 } | |
329 } | |
330 closedir(dp); | |
331 } | |
332 | |
333 void editor_load_descriptions(void) | |
334 { | |
335 gchar *path; | |
336 gchar *xdg_data_dirs; | |
337 gchar *all_dirs; | |
338 gchar **split_dirs; | |
9 | 339 gint i; |
1272 | 340 |
341 if (!editors) | |
342 { | |
343 editors = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify)editor_description_free); | |
344 } | |
9 | 345 |
1272 | 346 xdg_data_dirs = getenv("XDG_DATA_DIRS"); |
347 if (xdg_data_dirs && xdg_data_dirs[0]) | |
348 xdg_data_dirs = path_to_utf8(xdg_data_dirs); | |
349 else | |
350 xdg_data_dirs = g_strdup("/usr/share"); | |
351 | |
1274 | 352 all_dirs = g_strconcat(get_rc_dir(), ":", GQ_APP_DIR, ":", xdg_data_home_get(), ":", xdg_data_dirs, NULL); |
1272 | 353 |
354 g_free(xdg_data_dirs); | |
355 | |
356 split_dirs = g_strsplit(all_dirs, ":", 0); | |
357 | |
358 g_free(all_dirs); | |
359 | |
360 for (i = 0; split_dirs[i]; i++) | |
9 | 361 { |
1272 | 362 path = g_build_filename(split_dirs[i], "applications", NULL); |
363 editor_read_desktop_dir(path); | |
364 g_free(path); | |
9 | 365 } |
1272 | 366 |
367 g_strfreev(split_dirs); | |
368 | |
369 g_hash_table_foreach_remove(editors, editor_remove_desktop_file_cb, NULL); | |
9 | 370 } |
371 | |
1272 | 372 static void editor_list_add_cb(gpointer key, gpointer value, gpointer data) |
373 { | |
374 GList **listp = data; | |
375 EditorDescription *editor = value; | |
376 | |
377 /* do not show the special commands in any list, they are called explicitelly */ | |
378 if (strcmp(editor->key, CMD_COPY) == 0 || | |
379 strcmp(editor->key, CMD_MOVE) == 0 || | |
380 strcmp(editor->key, CMD_RENAME) == 0 || | |
381 strcmp(editor->key, CMD_DELETE) == 0 || | |
382 strcmp(editor->key, CMD_FOLDER) == 0) return; | |
383 | |
384 *listp = g_list_prepend(*listp, editor); | |
385 } | |
386 | |
1276
4177057ca11b
editor_list_get() now returns a sorted list, this way items appear in the same order in all menus.
zas_
parents:
1274
diff
changeset
|
387 static gint editor_sort(gconstpointer a, gconstpointer b) |
4177057ca11b
editor_list_get() now returns a sorted list, this way items appear in the same order in all menus.
zas_
parents:
1274
diff
changeset
|
388 { |
4177057ca11b
editor_list_get() now returns a sorted list, this way items appear in the same order in all menus.
zas_
parents:
1274
diff
changeset
|
389 const EditorDescription *ea = a; |
4177057ca11b
editor_list_get() now returns a sorted list, this way items appear in the same order in all menus.
zas_
parents:
1274
diff
changeset
|
390 const EditorDescription *eb = b; |
4177057ca11b
editor_list_get() now returns a sorted list, this way items appear in the same order in all menus.
zas_
parents:
1274
diff
changeset
|
391 int ret; |
4177057ca11b
editor_list_get() now returns a sorted list, this way items appear in the same order in all menus.
zas_
parents:
1274
diff
changeset
|
392 |
4177057ca11b
editor_list_get() now returns a sorted list, this way items appear in the same order in all menus.
zas_
parents:
1274
diff
changeset
|
393 ret = strcmp(ea->menu_path, eb->menu_path); |
4177057ca11b
editor_list_get() now returns a sorted list, this way items appear in the same order in all menus.
zas_
parents:
1274
diff
changeset
|
394 if (ret != 0) return ret; |
4177057ca11b
editor_list_get() now returns a sorted list, this way items appear in the same order in all menus.
zas_
parents:
1274
diff
changeset
|
395 |
4177057ca11b
editor_list_get() now returns a sorted list, this way items appear in the same order in all menus.
zas_
parents:
1274
diff
changeset
|
396 return g_utf8_collate(ea->name, eb->name); |
4177057ca11b
editor_list_get() now returns a sorted list, this way items appear in the same order in all menus.
zas_
parents:
1274
diff
changeset
|
397 } |
4177057ca11b
editor_list_get() now returns a sorted list, this way items appear in the same order in all menus.
zas_
parents:
1274
diff
changeset
|
398 |
1272 | 399 GList *editor_list_get(void) |
400 { | |
401 GList *editors_list = NULL; | |
402 g_hash_table_foreach(editors, editor_list_add_cb, &editors_list); | |
1276
4177057ca11b
editor_list_get() now returns a sorted list, this way items appear in the same order in all menus.
zas_
parents:
1274
diff
changeset
|
403 editors_list = g_list_sort(editors_list, editor_sort); |
4177057ca11b
editor_list_get() now returns a sorted list, this way items appear in the same order in all menus.
zas_
parents:
1274
diff
changeset
|
404 |
1272 | 405 return editors_list; |
406 } | |
407 | |
408 /* ------------------------------ */ | |
409 | |
410 | |
140 | 411 static void editor_verbose_data_free(EditorData *ed) |
412 { | |
413 if (!ed->vd) return; | |
414 g_free(ed->vd); | |
415 ed->vd = NULL; | |
416 } | |
417 | |
418 static void editor_data_free(EditorData *ed) | |
419 { | |
420 editor_verbose_data_free(ed); | |
421 g_free(ed); | |
422 } | |
423 | |
9 | 424 static void editor_verbose_window_close(GenericDialog *gd, gpointer data) |
425 { | |
140 | 426 EditorData *ed = data; |
9 | 427 |
428 generic_dialog_close(gd); | |
140 | 429 editor_verbose_data_free(ed); |
430 if (ed->pid == -1) editor_data_free(ed); /* the process has already terminated */ | |
9 | 431 } |
432 | |
433 static void editor_verbose_window_stop(GenericDialog *gd, gpointer data) | |
434 { | |
140 | 435 EditorData *ed = data; |
436 ed->stopping = TRUE; | |
437 ed->count = 0; | |
438 editor_verbose_window_progress(ed, _("stopping...")); | |
9 | 439 } |
440 | |
441 static void editor_verbose_window_enable_close(EditorVerboseData *vd) | |
442 { | |
443 vd->gd->cancel_cb = editor_verbose_window_close; | |
444 | |
445 spinner_set_interval(vd->spinner, -1); | |
446 gtk_widget_set_sensitive(vd->button_stop, FALSE); | |
447 gtk_widget_set_sensitive(vd->button_close, TRUE); | |
448 } | |
449 | |
140 | 450 static EditorVerboseData *editor_verbose_window(EditorData *ed, const gchar *text) |
9 | 451 { |
452 EditorVerboseData *vd; | |
453 GtkWidget *scrolled; | |
454 GtkWidget *hbox; | |
455 gchar *buf; | |
456 | |
457 vd = g_new0(EditorVerboseData, 1); | |
458 | |
1174
0bea79d87065
Drop useless wmclass stuff. Gtk will take care of it and as said in the documentation using gtk_window_set_wmclass() is sort of pointless.
zas_
parents:
1055
diff
changeset
|
459 vd->gd = file_util_gen_dlg(_("Edit command results"), "editor_results", |
9 | 460 NULL, FALSE, |
140 | 461 NULL, ed); |
9 | 462 buf = g_strdup_printf(_("Output of %s"), text); |
463 generic_dialog_add_message(vd->gd, NULL, buf, NULL); | |
464 g_free(buf); | |
465 vd->button_stop = generic_dialog_add_button(vd->gd, GTK_STOCK_STOP, NULL, | |
466 editor_verbose_window_stop, FALSE); | |
467 gtk_widget_set_sensitive(vd->button_stop, FALSE); | |
468 vd->button_close = generic_dialog_add_button(vd->gd, GTK_STOCK_CLOSE, NULL, | |
469 editor_verbose_window_close, TRUE); | |
470 gtk_widget_set_sensitive(vd->button_close, FALSE); | |
471 | |
472 scrolled = gtk_scrolled_window_new(NULL, NULL); | |
473 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN); | |
474 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled), | |
475 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); | |
476 gtk_box_pack_start(GTK_BOX(vd->gd->vbox), scrolled, TRUE, TRUE, 5); | |
477 gtk_widget_show(scrolled); | |
478 | |
479 vd->text = gtk_text_view_new(); | |
480 gtk_text_view_set_editable(GTK_TEXT_VIEW(vd->text), FALSE); | |
481 gtk_widget_set_size_request(vd->text, EDITOR_WINDOW_WIDTH, EDITOR_WINDOW_HEIGHT); | |
482 gtk_container_add(GTK_CONTAINER(scrolled), vd->text); | |
483 gtk_widget_show(vd->text); | |
484 | |
485 hbox = gtk_hbox_new(FALSE, 0); | |
486 gtk_box_pack_start(GTK_BOX(vd->gd->vbox), hbox, FALSE, FALSE, 0); | |
487 gtk_widget_show(hbox); | |
488 | |
489 vd->progress = gtk_progress_bar_new(); | |
490 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(vd->progress), 0.0); | |
491 gtk_box_pack_start(GTK_BOX(hbox), vd->progress, TRUE, TRUE, 0); | |
492 gtk_widget_show(vd->progress); | |
493 | |
494 vd->spinner = spinner_new(NULL, SPINNER_SPEED); | |
495 gtk_box_pack_start(GTK_BOX(hbox), vd->spinner, FALSE, FALSE, 0); | |
496 gtk_widget_show(vd->spinner); | |
442 | 497 |
9 | 498 gtk_widget_show(vd->gd->dialog); |
499 | |
140 | 500 ed->vd = vd; |
9 | 501 return vd; |
502 } | |
503 | |
504 static void editor_verbose_window_fill(EditorVerboseData *vd, gchar *text, gint len) | |
505 { | |
506 GtkTextBuffer *buffer; | |
507 GtkTextIter iter; | |
508 | |
509 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(vd->text)); | |
510 gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1); | |
511 gtk_text_buffer_insert(buffer, &iter, text, len); | |
512 } | |
513 | |
140 | 514 static void editor_verbose_window_progress(EditorData *ed, const gchar *text) |
9 | 515 { |
140 | 516 if (!ed->vd) return; |
517 | |
518 if (ed->total) | |
9 | 519 { |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
995
diff
changeset
|
520 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ed->vd->progress), (gdouble)ed->count / ed->total); |
9 | 521 } |
522 | |
140 | 523 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(ed->vd->progress), (text) ? text : ""); |
9 | 524 } |
525 | |
526 static gboolean editor_verbose_io_cb(GIOChannel *source, GIOCondition condition, gpointer data) | |
527 { | |
140 | 528 EditorData *ed = data; |
9 | 529 gchar buf[512]; |
530 gsize count; | |
531 | |
140 | 532 if (condition & G_IO_IN) |
9 | 533 { |
140 | 534 while (g_io_channel_read_chars(source, buf, sizeof(buf), &count, NULL) == G_IO_STATUS_NORMAL) |
535 { | |
536 if (!g_utf8_validate(buf, count, NULL)) | |
9 | 537 { |
140 | 538 gchar *utf8; |
444 | 539 |
140 | 540 utf8 = g_locale_to_utf8(buf, count, NULL, NULL, NULL); |
541 if (utf8) | |
9 | 542 { |
140 | 543 editor_verbose_window_fill(ed->vd, utf8, -1); |
544 g_free(utf8); | |
9 | 545 } |
546 else | |
547 { | |
288
d1f74154463e
Replace occurences of Geeqie / geeqie by constants defined in main.h.
zas_
parents:
283
diff
changeset
|
548 editor_verbose_window_fill(ed->vd, "Error converting text to valid utf8\n", -1); |
9 | 549 } |
550 } | |
140 | 551 else |
552 { | |
553 editor_verbose_window_fill(ed->vd, buf, count); | |
554 } | |
555 } | |
9 | 556 } |
557 | |
140 | 558 if (condition & (G_IO_ERR | G_IO_HUP)) |
9 | 559 { |
140 | 560 g_io_channel_shutdown(source, TRUE, NULL); |
9 | 561 return FALSE; |
562 } | |
563 | |
564 return TRUE; | |
565 } | |
566 | |
138 | 567 typedef enum { |
568 PATH_FILE, | |
1272 | 569 PATH_FILE_URL, |
140 | 570 PATH_DEST |
138 | 571 } PathType; |
572 | |
573 | |
1272 | 574 static gchar *editor_command_path_parse(const FileData *fd, PathType type, const EditorDescription *editor) |
123
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
575 { |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
576 GString *string; |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
577 gchar *pathl; |
147
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
578 const gchar *p = NULL; |
123
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
579 |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
580 string = g_string_new(""); |
442 | 581 |
1272 | 582 if (type == PATH_FILE || type == PATH_FILE_URL) |
138 | 583 { |
1272 | 584 GList *work = editor->ext_list; |
442 | 585 |
147
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
586 if (!work) |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
587 p = fd->path; |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
588 else |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
589 { |
516 | 590 while (work) |
147
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
591 { |
444 | 592 GList *work2; |
147
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
593 gchar *ext = work->data; |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
594 work = work->next; |
442 | 595 |
596 if (strcmp(ext, "*") == 0 || | |
1307 | 597 g_ascii_strcasecmp(ext, fd->extension) == 0) |
147
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
598 { |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
599 p = fd->path; |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
600 break; |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
601 } |
442 | 602 |
444 | 603 work2 = fd->sidecar_files; |
516 | 604 while (work2) |
147
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
605 { |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
606 FileData *sfd = work2->data; |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
607 work2 = work2->next; |
442 | 608 |
1307 | 609 if (g_ascii_strcasecmp(ext, sfd->extension) == 0) |
147
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
610 { |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
611 p = sfd->path; |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
612 break; |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
613 } |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
614 } |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
615 if (p) break; |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
616 } |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
617 if (!p) return NULL; |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
618 } |
138 | 619 } |
140 | 620 else if (type == PATH_DEST) |
138 | 621 { |
622 if (fd->change && fd->change->dest) | |
623 p = fd->change->dest; | |
624 else | |
625 p = ""; | |
626 } | |
444 | 627 |
123
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
628 while (*p != '\0') |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
629 { |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
630 /* must escape \, ", `, and $ to avoid problems, |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
631 * 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
|
632 */ |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
633 if (strchr("\\\"`$", *p) != NULL) |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
634 { |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
635 string = g_string_append_c(string, '\\'); |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
636 } |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
637 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
|
638 p++; |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
639 } |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
640 |
1272 | 641 if (type == PATH_FILE_URL) g_string_prepend(string, "file://"); |
123
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
642 pathl = path_from_utf8(string->str); |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
643 g_string_free(string, TRUE); |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
644 |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
645 return pathl; |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
646 } |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
647 |
9 | 648 |
1272 | 649 static gint editor_command_parse(const EditorDescription *editor, GList *list, gchar **output) |
9 | 650 { |
140 | 651 gint flags = 0; |
1272 | 652 const gchar *p; |
140 | 653 GString *result = NULL; |
442 | 654 |
140 | 655 if (output) |
656 result = g_string_new(""); | |
657 | |
1272 | 658 if (editor->exec[0] == '\0') |
140 | 659 { |
660 flags |= EDITOR_ERROR_EMPTY; | |
661 goto err; | |
662 } | |
669 | 663 |
1272 | 664 p = editor->exec; |
669 | 665 /* skip leading whitespaces if any */ |
666 while (g_ascii_isspace(*p)) p++; | |
442 | 667 |
140 | 668 /* command */ |
442 | 669 |
140 | 670 while (*p) |
671 { | |
672 if (*p != '%') | |
673 { | |
674 if (output) result = g_string_append_c(result, *p); | |
675 } | |
676 else /* *p == '%' */ | |
677 { | |
678 gchar *pathl = NULL; | |
9 | 679 |
140 | 680 p++; |
442 | 681 |
682 switch (*p) | |
140 | 683 { |
1272 | 684 case 'f': /* single file */ |
685 case 'u': /* single url */ | |
140 | 686 flags |= EDITOR_FOR_EACH; |
687 if (flags & EDITOR_SINGLE_COMMAND) | |
688 { | |
689 flags |= EDITOR_ERROR_INCOMPATIBLE; | |
690 goto err; | |
691 } | |
692 if (output) | |
693 { | |
694 /* use the first file from the list */ | |
442 | 695 if (!list || !list->data) |
140 | 696 { |
697 flags |= EDITOR_ERROR_NO_FILE; | |
698 goto err; | |
699 } | |
669 | 700 pathl = editor_command_path_parse((FileData *)list->data, |
1272 | 701 (*p == 'f') ? PATH_FILE : PATH_FILE_URL, |
702 editor); | |
442 | 703 if (!pathl) |
140 | 704 { |
705 flags |= EDITOR_ERROR_NO_FILE; | |
706 goto err; | |
707 } | |
708 result = g_string_append_c(result, '"'); | |
709 result = g_string_append(result, pathl); | |
710 g_free(pathl); | |
711 result = g_string_append_c(result, '"'); | |
712 } | |
442 | 713 break; |
60
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
714 |
1272 | 715 case 'F': |
716 case 'U': | |
140 | 717 flags |= EDITOR_SINGLE_COMMAND; |
718 if (flags & (EDITOR_FOR_EACH | EDITOR_DEST)) | |
719 { | |
720 flags |= EDITOR_ERROR_INCOMPATIBLE; | |
721 goto err; | |
722 } | |
723 | |
724 if (output) | |
725 { | |
726 /* use whole list */ | |
727 GList *work = list; | |
728 gboolean ok = FALSE; | |
444 | 729 |
140 | 730 while (work) |
731 { | |
732 FileData *fd = work->data; | |
1272 | 733 pathl = editor_command_path_parse(fd, (*p == 'F') ? PATH_FILE : PATH_FILE_URL, editor); |
140 | 734 |
735 if (pathl) | |
736 { | |
737 ok = TRUE; | |
738 if (work != list) g_string_append_c(result, ' '); | |
739 result = g_string_append_c(result, '"'); | |
740 result = g_string_append(result, pathl); | |
741 g_free(pathl); | |
742 result = g_string_append_c(result, '"'); | |
743 } | |
744 work = work->next; | |
745 } | |
442 | 746 if (!ok) |
140 | 747 { |
748 flags |= EDITOR_ERROR_NO_FILE; | |
749 goto err; | |
750 } | |
751 } | |
442 | 752 break; |
1272 | 753 case 'i': |
754 if (output) | |
755 { | |
756 result = g_string_append(result, editor->icon); | |
757 } | |
758 break; | |
759 case 'c': | |
760 if (output) | |
761 { | |
762 result = g_string_append(result, editor->name); | |
763 } | |
764 break; | |
765 case 'k': | |
766 if (output) | |
767 { | |
768 result = g_string_append(result, editor->file); | |
769 } | |
770 break; | |
669 | 771 case '%': |
772 /* %% = % escaping */ | |
773 if (output) result = g_string_append_c(result, *p); | |
774 break; | |
1272 | 775 case 'd': |
776 case 'D': | |
777 case 'n': | |
778 case 'N': | |
779 case 'v': | |
780 case 'm': | |
781 /* deprecated according to spec, ignore */ | |
782 break; | |
140 | 783 default: |
784 flags |= EDITOR_ERROR_SYNTAX; | |
785 goto err; | |
786 } | |
787 } | |
788 p++; | |
9 | 789 } |
790 | |
140 | 791 if (output) *output = g_string_free(result, FALSE); |
792 return flags; | |
793 | |
442 | 794 |
140 | 795 err: |
442 | 796 if (output) |
9 | 797 { |
140 | 798 g_string_free(result, TRUE); |
799 *output = NULL; | |
800 } | |
801 return flags; | |
802 } | |
803 | |
1272 | 804 |
140 | 805 static void editor_child_exit_cb (GPid pid, gint status, gpointer data) |
806 { | |
807 EditorData *ed = data; | |
808 g_spawn_close_pid(pid); | |
809 ed->pid = -1; | |
442 | 810 |
140 | 811 editor_command_next_finish(ed, status); |
812 } | |
813 | |
814 | |
1272 | 815 static gint editor_command_one(const EditorDescription *editor, GList *list, EditorData *ed) |
140 | 816 { |
817 gchar *command; | |
818 FileData *fd = list->data; | |
819 GPid pid; | |
442 | 820 gint standard_output; |
821 gint standard_error; | |
140 | 822 gboolean ok; |
823 | |
824 ed->pid = -1; | |
1272 | 825 ed->flags = editor->flags | editor_command_parse(editor, list, &command); |
140 | 826 |
827 ok = !(ed->flags & EDITOR_ERROR_MASK); | |
828 | |
829 if (ok) | |
830 { | |
737
8a8873e7a552
Make shell command and its option rc file options instead of hardcoded strings.
zas_
parents:
731
diff
changeset
|
831 ok = (options->shell.path && *options->shell.path); |
8a8873e7a552
Make shell command and its option rc file options instead of hardcoded strings.
zas_
parents:
731
diff
changeset
|
832 if (!ok) log_printf("ERROR: empty shell command\n"); |
8a8873e7a552
Make shell command and its option rc file options instead of hardcoded strings.
zas_
parents:
731
diff
changeset
|
833 |
8a8873e7a552
Make shell command and its option rc file options instead of hardcoded strings.
zas_
parents:
731
diff
changeset
|
834 if (ok) |
8a8873e7a552
Make shell command and its option rc file options instead of hardcoded strings.
zas_
parents:
731
diff
changeset
|
835 { |
8a8873e7a552
Make shell command and its option rc file options instead of hardcoded strings.
zas_
parents:
731
diff
changeset
|
836 ok = (access(options->shell.path, X_OK) == 0); |
8a8873e7a552
Make shell command and its option rc file options instead of hardcoded strings.
zas_
parents:
731
diff
changeset
|
837 if (!ok) log_printf("ERROR: cannot execute shell command '%s'\n", options->shell.path); |
8a8873e7a552
Make shell command and its option rc file options instead of hardcoded strings.
zas_
parents:
731
diff
changeset
|
838 } |
8a8873e7a552
Make shell command and its option rc file options instead of hardcoded strings.
zas_
parents:
731
diff
changeset
|
839 |
8a8873e7a552
Make shell command and its option rc file options instead of hardcoded strings.
zas_
parents:
731
diff
changeset
|
840 if (!ok) ed->flags |= EDITOR_ERROR_CANT_EXEC; |
8a8873e7a552
Make shell command and its option rc file options instead of hardcoded strings.
zas_
parents:
731
diff
changeset
|
841 } |
8a8873e7a552
Make shell command and its option rc file options instead of hardcoded strings.
zas_
parents:
731
diff
changeset
|
842 |
8a8873e7a552
Make shell command and its option rc file options instead of hardcoded strings.
zas_
parents:
731
diff
changeset
|
843 if (ok) |
8a8873e7a552
Make shell command and its option rc file options instead of hardcoded strings.
zas_
parents:
731
diff
changeset
|
844 { |
443 | 845 gchar *working_directory; |
846 gchar *args[4]; | |
737
8a8873e7a552
Make shell command and its option rc file options instead of hardcoded strings.
zas_
parents:
731
diff
changeset
|
847 guint n = 0; |
443 | 848 |
849 working_directory = remove_level_from_path(fd->path); | |
737
8a8873e7a552
Make shell command and its option rc file options instead of hardcoded strings.
zas_
parents:
731
diff
changeset
|
850 args[n++] = options->shell.path; |
8a8873e7a552
Make shell command and its option rc file options instead of hardcoded strings.
zas_
parents:
731
diff
changeset
|
851 if (options->shell.options && *options->shell.options) |
8a8873e7a552
Make shell command and its option rc file options instead of hardcoded strings.
zas_
parents:
731
diff
changeset
|
852 args[n++] = options->shell.options; |
8a8873e7a552
Make shell command and its option rc file options instead of hardcoded strings.
zas_
parents:
731
diff
changeset
|
853 args[n++] = command; |
8a8873e7a552
Make shell command and its option rc file options instead of hardcoded strings.
zas_
parents:
731
diff
changeset
|
854 args[n] = NULL; |
443 | 855 |
1272 | 856 if ((ed->flags & EDITOR_DEST) && fd->change && fd->change->dest) /* FIXME: error handling */ |
857 { | |
858 setenv("GEEQIE_DESTINATION", fd->change->dest, TRUE); | |
859 } | |
860 else | |
861 { | |
862 unsetenv("GEEQIE_DESTINATION"); | |
863 } | |
864 | |
442 | 865 ok = g_spawn_async_with_pipes(working_directory, args, NULL, |
140 | 866 G_SPAWN_DO_NOT_REAP_CHILD, /* GSpawnFlags */ |
442 | 867 NULL, NULL, |
868 &pid, | |
869 NULL, | |
870 ed->vd ? &standard_output : NULL, | |
871 ed->vd ? &standard_error : NULL, | |
140 | 872 NULL); |
443 | 873 |
874 g_free(working_directory); | |
442 | 875 |
140 | 876 if (!ok) ed->flags |= EDITOR_ERROR_CANT_EXEC; |
877 } | |
878 | |
442 | 879 if (ok) |
140 | 880 { |
881 g_child_watch_add(pid, editor_child_exit_cb, ed); | |
882 ed->pid = pid; | |
883 } | |
442 | 884 |
140 | 885 if (ed->vd) |
886 { | |
887 if (!ok) | |
9 | 888 { |
140 | 889 gchar *buf; |
890 | |
1272 | 891 buf = g_strdup_printf(_("Failed to run command:\n%s\n"), editor->file); |
140 | 892 editor_verbose_window_fill(ed->vd, buf, strlen(buf)); |
893 g_free(buf); | |
894 | |
895 } | |
442 | 896 else |
140 | 897 { |
898 GIOChannel *channel_output; | |
899 GIOChannel *channel_error; | |
444 | 900 |
140 | 901 channel_output = g_io_channel_unix_new(standard_output); |
902 g_io_channel_set_flags(channel_output, G_IO_FLAG_NONBLOCK, NULL); | |
926 | 903 g_io_channel_set_encoding(channel_output, NULL, NULL); |
140 | 904 |
905 g_io_add_watch_full(channel_output, G_PRIORITY_HIGH, G_IO_IN | G_IO_ERR | G_IO_HUP, | |
906 editor_verbose_io_cb, ed, NULL); | |
907 g_io_channel_unref(channel_output); | |
908 | |
909 channel_error = g_io_channel_unix_new(standard_error); | |
910 g_io_channel_set_flags(channel_error, G_IO_FLAG_NONBLOCK, NULL); | |
926 | 911 g_io_channel_set_encoding(channel_error, NULL, NULL); |
140 | 912 |
913 g_io_add_watch_full(channel_error, G_PRIORITY_HIGH, G_IO_IN | G_IO_ERR | G_IO_HUP, | |
914 editor_verbose_io_cb, ed, NULL); | |
915 g_io_channel_unref(channel_error); | |
916 } | |
917 } | |
442 | 918 |
140 | 919 g_free(command); |
920 | |
921 return ed->flags & EDITOR_ERROR_MASK; | |
922 } | |
923 | |
924 static gint editor_command_next_start(EditorData *ed) | |
925 { | |
926 if (ed->vd) editor_verbose_window_fill(ed->vd, "\n", 1); | |
927 | |
928 if (ed->list && ed->count < ed->total) | |
929 { | |
930 FileData *fd; | |
931 gint error; | |
932 | |
933 fd = ed->list->data; | |
934 | |
935 if (ed->vd) | |
936 { | |
937 editor_verbose_window_progress(ed, (ed->flags & EDITOR_FOR_EACH) ? fd->path : _("running...")); | |
938 } | |
939 ed->count++; | |
940 | |
1272 | 941 error = editor_command_one(ed->editor, ed->list, ed); |
140 | 942 if (!error && ed->vd) |
943 { | |
944 gtk_widget_set_sensitive(ed->vd->button_stop, (ed->list != NULL) ); | |
945 if (ed->flags & EDITOR_FOR_EACH) | |
9 | 946 { |
140 | 947 editor_verbose_window_fill(ed->vd, fd->path, strlen(fd->path)); |
948 editor_verbose_window_fill(ed->vd, "\n", 1); | |
9 | 949 } |
950 } | |
140 | 951 |
442 | 952 if (!error) |
140 | 953 return 0; |
954 else | |
955 /* command was not started, call the finish immediately */ | |
956 return editor_command_next_finish(ed, 0); | |
957 } | |
442 | 958 |
140 | 959 /* everything is done */ |
237
404629011caa
Add missing return at the end of editor_command_next_start().
zas_
parents:
196
diff
changeset
|
960 return editor_command_done(ed); |
140 | 961 } |
962 | |
963 static gint editor_command_next_finish(EditorData *ed, gint status) | |
964 { | |
965 gint cont = ed->stopping ? EDITOR_CB_SKIP : EDITOR_CB_CONTINUE; | |
966 | |
967 if (status) | |
968 ed->flags |= EDITOR_ERROR_STATUS; | |
969 | |
970 if (ed->flags & EDITOR_FOR_EACH) | |
971 { | |
972 /* handle the first element from the list */ | |
973 GList *fd_element = ed->list; | |
444 | 974 |
140 | 975 ed->list = g_list_remove_link(ed->list, fd_element); |
976 if (ed->callback) | |
911 | 977 { |
140 | 978 cont = ed->callback(ed->list ? ed : NULL, ed->flags, fd_element, ed->data); |
911 | 979 if (ed->stopping && cont == EDITOR_CB_CONTINUE) cont = EDITOR_CB_SKIP; |
980 } | |
140 | 981 filelist_free(fd_element); |
9 | 982 } |
983 else | |
984 { | |
140 | 985 /* handle whole list */ |
986 if (ed->callback) | |
987 cont = ed->callback(NULL, ed->flags, ed->list, ed->data); | |
988 filelist_free(ed->list); | |
989 ed->list = NULL; | |
990 } | |
9 | 991 |
140 | 992 if (cont == EDITOR_CB_SUSPEND) |
993 return ed->flags & EDITOR_ERROR_MASK; | |
994 else if (cont == EDITOR_CB_SKIP) | |
995 return editor_command_done(ed); | |
996 else | |
997 return editor_command_next_start(ed); | |
998 } | |
9 | 999 |
140 | 1000 static gint editor_command_done(EditorData *ed) |
1001 { | |
1002 gint flags; | |
9 | 1003 |
140 | 1004 if (ed->vd) |
1005 { | |
444 | 1006 const gchar *text; |
1007 | |
140 | 1008 if (ed->count == ed->total) |
9 | 1009 { |
140 | 1010 text = _("done"); |
9 | 1011 } |
1012 else | |
1013 { | |
140 | 1014 text = _("stopped by user"); |
9 | 1015 } |
140 | 1016 editor_verbose_window_progress(ed, text); |
1017 editor_verbose_window_enable_close(ed->vd); | |
1018 } | |
1019 | |
1020 /* free the not-handled items */ | |
1021 if (ed->list) | |
1022 { | |
1023 ed->flags |= EDITOR_ERROR_SKIPPED; | |
1024 if (ed->callback) ed->callback(NULL, ed->flags, ed->list, ed->data); | |
1025 filelist_free(ed->list); | |
1026 ed->list = NULL; | |
1027 } | |
9 | 1028 |
140 | 1029 ed->count = 0; |
1030 | |
1031 flags = ed->flags & EDITOR_ERROR_MASK; | |
1032 | |
1033 if (!ed->vd) editor_data_free(ed); | |
1034 | |
1035 return flags; | |
1036 } | |
1037 | |
1038 void editor_resume(gpointer ed) | |
1039 { | |
1040 editor_command_next_start(ed); | |
1041 } | |
443 | 1042 |
140 | 1043 void editor_skip(gpointer ed) |
1044 { | |
442 | 1045 editor_command_done(ed); |
9 | 1046 } |
1047 | |
1272 | 1048 static gint editor_command_start(const EditorDescription *editor, const gchar *text, GList *list, EditorCallback cb, gpointer data) |
140 | 1049 { |
1050 EditorData *ed; | |
1272 | 1051 gint flags = editor->flags; |
442 | 1052 |
140 | 1053 if (flags & EDITOR_ERROR_MASK) return flags & EDITOR_ERROR_MASK; |
1054 | |
1055 ed = g_new0(EditorData, 1); | |
1056 ed->list = filelist_copy(list); | |
1057 ed->flags = flags; | |
1272 | 1058 ed->editor = editor; |
140 | 1059 ed->total = (flags & EDITOR_SINGLE_COMMAND) ? 1 : g_list_length(list); |
1060 ed->count = 0; | |
1061 ed->stopping = FALSE; | |
1062 ed->callback = cb; | |
1063 ed->data = data; | |
442 | 1064 |
140 | 1065 if ((flags & EDITOR_VERBOSE_MULTI) && list && list->next) |
1066 flags |= EDITOR_VERBOSE; | |
442 | 1067 |
140 | 1068 if (flags & EDITOR_VERBOSE) |
1069 editor_verbose_window(ed, text); | |
442 | 1070 |
1071 editor_command_next_start(ed); | |
140 | 1072 /* errors from editor_command_next_start will be handled via callback */ |
1073 return flags & EDITOR_ERROR_MASK; | |
1074 } | |
1075 | |
1272 | 1076 gboolean is_valid_editor_command(const gchar *key) |
444 | 1077 { |
1272 | 1078 if (!key) return FALSE; |
1079 return g_hash_table_lookup(editors, key) != NULL; | |
444 | 1080 } |
1081 | |
1272 | 1082 gint start_editor_from_filelist_full(const gchar *key, GList *list, EditorCallback cb, gpointer data) |
9 | 1083 { |
140 | 1084 gint error; |
1272 | 1085 EditorDescription *editor; |
1086 if (!key) return FALSE; | |
1087 | |
1088 editor = g_hash_table_lookup(editors, key); | |
9 | 1089 |
444 | 1090 if (!list) return FALSE; |
1272 | 1091 if (!editor) return FALSE; |
1092 | |
1093 error = editor_command_start(editor, editor->name, list, cb, data); | |
9 | 1094 |
1272 | 1095 if (error & EDITOR_ERROR_MASK) |
669 | 1096 { |
1272 | 1097 gchar *text = g_strdup_printf(_("%s\n\"%s\""), editor_get_error_str(error), editor->file); |
669 | 1098 |
1099 file_util_warning_dialog(_("Invalid editor command"), text, GTK_STOCK_DIALOG_ERROR, NULL); | |
1100 g_free(text); | |
1101 } | |
1102 | |
140 | 1103 return error; |
9 | 1104 } |
1105 | |
1272 | 1106 gint start_editor_from_filelist(const gchar *key, GList *list) |
140 | 1107 { |
1272 | 1108 return start_editor_from_filelist_full(key, list, NULL, NULL); |
140 | 1109 } |
1110 | |
1272 | 1111 gint start_editor_from_file_full(const gchar *key, FileData *fd, EditorCallback cb, gpointer data) |
9 | 1112 { |
1113 GList *list; | |
140 | 1114 gint error; |
9 | 1115 |
138 | 1116 if (!fd) return FALSE; |
9 | 1117 |
138 | 1118 list = g_list_append(NULL, fd); |
1272 | 1119 error = start_editor_from_filelist_full(key, list, cb, data); |
9 | 1120 g_list_free(list); |
140 | 1121 return error; |
9 | 1122 } |
60
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
1123 |
1272 | 1124 gint start_editor_from_file(const gchar *key, FileData *fd) |
136 | 1125 { |
1272 | 1126 return start_editor_from_file_full(key, fd, NULL, NULL); |
136 | 1127 } |
1128 | |
1272 | 1129 gint editor_window_flag_set(const gchar *key) |
60
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
1130 { |
1272 | 1131 EditorDescription *editor; |
1132 if (!key) return TRUE; | |
1133 | |
1134 editor = g_hash_table_lookup(editors, key); | |
1135 if (!editor) return TRUE; | |
60
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
1136 |
1272 | 1137 return (editor->flags & EDITOR_KEEP_FS); |
60
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
1138 } |
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
1139 |
1272 | 1140 gint editor_is_filter(const gchar *key) |
753 | 1141 { |
1272 | 1142 EditorDescription *editor; |
1143 if (!key) return TRUE; | |
1144 | |
1145 editor = g_hash_table_lookup(editors, key); | |
1146 if (!editor) return TRUE; | |
753 | 1147 |
1272 | 1148 return (editor->flags & EDITOR_DEST); |
753 | 1149 } |
1150 | |
140 | 1151 const gchar *editor_get_error_str(gint flags) |
1152 { | |
1153 if (flags & EDITOR_ERROR_EMPTY) return _("Editor template is empty."); | |
1154 if (flags & EDITOR_ERROR_SYNTAX) return _("Editor template has incorrect syntax."); | |
1155 if (flags & EDITOR_ERROR_INCOMPATIBLE) return _("Editor template uses incompatible macros."); | |
1156 if (flags & EDITOR_ERROR_NO_FILE) return _("Can't find matching file type."); | |
1157 if (flags & EDITOR_ERROR_CANT_EXEC) return _("Can't execute external editor."); | |
1158 if (flags & EDITOR_ERROR_STATUS) return _("External editor returned error status."); | |
1159 if (flags & EDITOR_ERROR_SKIPPED) return _("File was skipped."); | |
1160 return _("Unknown error."); | |
1161 } | |
731
fa8f7d7396cf
Introduce an helper function that returns the name of an editor.
zas_
parents:
730
diff
changeset
|
1162 |
1272 | 1163 const gchar *editor_get_name(const gchar *key) |
731
fa8f7d7396cf
Introduce an helper function that returns the name of an editor.
zas_
parents:
730
diff
changeset
|
1164 { |
1272 | 1165 EditorDescription *editor = g_hash_table_lookup(editors, key); |
731
fa8f7d7396cf
Introduce an helper function that returns the name of an editor.
zas_
parents:
730
diff
changeset
|
1166 |
1272 | 1167 if (!editor) return NULL; |
1168 | |
1169 return editor->name; | |
731
fa8f7d7396cf
Introduce an helper function that returns the name of an editor.
zas_
parents:
730
diff
changeset
|
1170 } |
1055
1646720364cf
Adding a vim modeline to all files - patch by Klaus Ethgen
nadvornik
parents:
1022
diff
changeset
|
1171 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */ |