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