Mercurial > geeqie
annotate src/editors.c @ 1408:1cc4ea53c9a3
Fix editor_command_path_parse().
author | zas_ |
---|---|
date | Mon, 09 Mar 2009 19:35:53 +0000 |
parents | 57421a728682 |
children | 863ac709f6b4 |
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 { | |
1405 | 45 EditorFlags flags; |
140 | 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); |
1405 | 59 static EditorFlags editor_command_next_start(EditorData *ed); |
60 static EditorFlags editor_command_next_finish(EditorData *ed, gint status); | |
61 static EditorFlags 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 |
1407
57421a728682
Revert changeset 1502. It brokes the spec: hidden desktop files from user directory should hide files from system directory with the same name.
zas_
parents:
1406
diff
changeset
|
172 editor = g_new0(EditorDescription, 1); |
57421a728682
Revert changeset 1502. It brokes the spec: hidden desktop files from user directory should hide files from system directory with the same name.
zas_
parents:
1406
diff
changeset
|
173 |
57421a728682
Revert changeset 1502. It brokes the spec: hidden desktop files from user directory should hide files from system directory with the same name.
zas_
parents:
1406
diff
changeset
|
174 editor->key = g_strdup(key); |
57421a728682
Revert changeset 1502. It brokes the spec: hidden desktop files from user directory should hide files from system directory with the same name.
zas_
parents:
1406
diff
changeset
|
175 editor->file = g_strdup(path); |
57421a728682
Revert changeset 1502. It brokes the spec: hidden desktop files from user directory should hide files from system directory with the same name.
zas_
parents:
1406
diff
changeset
|
176 |
57421a728682
Revert changeset 1502. It brokes the spec: hidden desktop files from user directory should hide files from system directory with the same name.
zas_
parents:
1406
diff
changeset
|
177 g_hash_table_insert(editors, editor->key, editor); |
57421a728682
Revert changeset 1502. It brokes the spec: hidden desktop files from user directory should hide files from system directory with the same name.
zas_
parents:
1406
diff
changeset
|
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 { |
1407
57421a728682
Revert changeset 1502. It brokes the spec: hidden desktop files from user directory should hide files from system directory with the same name.
zas_
parents:
1406
diff
changeset
|
182 editor->hidden = TRUE; |
1278
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 } | |
1407
57421a728682
Revert changeset 1502. It brokes the spec: hidden desktop files from user directory should hide files from system directory with the same name.
zas_
parents:
1406
diff
changeset
|
198 if (!found) editor->hidden = TRUE; |
1272 | 199 g_strfreev(categories); |
200 } | |
201 else | |
202 { | |
1407
57421a728682
Revert changeset 1502. It brokes the spec: hidden desktop files from user directory should hide files from system directory with the same name.
zas_
parents:
1406
diff
changeset
|
203 editor->hidden = TRUE; |
1272 | 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 } | |
1407
57421a728682
Revert changeset 1502. It brokes the spec: hidden desktop files from user directory should hide files from system directory with the same name.
zas_
parents:
1406
diff
changeset
|
217 if (!found) editor->hidden = TRUE; |
1272 | 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 } | |
1407
57421a728682
Revert changeset 1502. It brokes the spec: hidden desktop files from user directory should hide files from system directory with the same name.
zas_
parents:
1406
diff
changeset
|
232 if (found) editor->hidden = TRUE; |
1272 | 233 g_strfreev(not_show_in); |
234 } | |
235 | |
1407
57421a728682
Revert changeset 1502. It brokes the spec: hidden desktop files from user directory should hide files from system directory with the same name.
zas_
parents:
1406
diff
changeset
|
236 |
1274 | 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); | |
1407
57421a728682
Revert changeset 1502. It brokes the spec: hidden desktop files from user directory should hide files from system directory with the same name.
zas_
parents:
1406
diff
changeset
|
241 if (!try_exec_res) editor->hidden = TRUE; |
1274 | 242 g_free(try_exec_res); |
243 g_free(try_exec); | |
244 } | |
245 | |
1407
57421a728682
Revert changeset 1502. It brokes the spec: hidden desktop files from user directory should hide files from system directory with the same name.
zas_
parents:
1406
diff
changeset
|
246 if (editor->hidden) |
1272 | 247 { |
248 /* hidden editors will be deleted, no need to parse the rest */ | |
249 g_key_file_free(key_file); | |
1407
57421a728682
Revert changeset 1502. It brokes the spec: hidden desktop files from user directory should hide files from system directory with the same name.
zas_
parents:
1406
diff
changeset
|
250 return TRUE; |
1272 | 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 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "X-Geeqie-Keep-Fullscreen", NULL)) editor->flags |= EDITOR_KEEP_FS; | |
281 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "X-Geeqie-Verbose", NULL)) editor->flags |= EDITOR_VERBOSE; | |
282 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "X-Geeqie-Verbose-Multi", NULL)) editor->flags |= EDITOR_VERBOSE_MULTI; | |
283 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "X-Geeqie-Filter", NULL)) editor->flags |= EDITOR_DEST; | |
284 if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "Terminal", NULL)) editor->flags |= EDITOR_TERMINAL; | |
285 | |
286 editor->flags |= editor_command_parse(editor, NULL, NULL); | |
287 g_key_file_free(key_file); | |
288 | |
289 return TRUE; | |
768
ff51413f098d
Use functions to set editors name and command and ensure they are
zas_
parents:
766
diff
changeset
|
290 } |
ff51413f098d
Use functions to set editors name and command and ensure they are
zas_
parents:
766
diff
changeset
|
291 |
1272 | 292 static gboolean editor_remove_desktop_file_cb(gpointer key, gpointer value, gpointer user_data) |
293 { | |
294 EditorDescription *editor = value; | |
295 return editor->hidden; | |
296 } | |
297 | |
298 static void editor_read_desktop_dir(const gchar *path) | |
9 | 299 { |
1272 | 300 DIR *dp; |
301 struct dirent *dir; | |
302 gchar *pathl; | |
303 | |
304 pathl = path_from_utf8(path); | |
305 dp = opendir(pathl); | |
306 g_free(pathl); | |
307 if (!dp) | |
308 { | |
309 /* dir not found */ | |
310 return; | |
311 } | |
312 while ((dir = readdir(dp)) != NULL) | |
313 { | |
314 gchar *namel = dir->d_name; | |
315 | |
1402 | 316 if (g_str_has_suffix(namel, ".desktop")) |
1272 | 317 { |
318 gchar *name = path_to_utf8(namel); | |
319 gchar *dpath = g_build_filename(path, name, NULL); | |
320 editor_read_desktop_file(dpath); | |
321 g_free(dpath); | |
322 g_free(name); | |
323 } | |
324 } | |
325 closedir(dp); | |
326 } | |
327 | |
328 void editor_load_descriptions(void) | |
329 { | |
330 gchar *path; | |
331 gchar *xdg_data_dirs; | |
332 gchar *all_dirs; | |
333 gchar **split_dirs; | |
9 | 334 gint i; |
1272 | 335 |
336 if (!editors) | |
337 { | |
338 editors = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify)editor_description_free); | |
339 } | |
9 | 340 |
1272 | 341 xdg_data_dirs = getenv("XDG_DATA_DIRS"); |
342 if (xdg_data_dirs && xdg_data_dirs[0]) | |
343 xdg_data_dirs = path_to_utf8(xdg_data_dirs); | |
344 else | |
345 xdg_data_dirs = g_strdup("/usr/share"); | |
346 | |
1274 | 347 all_dirs = g_strconcat(get_rc_dir(), ":", GQ_APP_DIR, ":", xdg_data_home_get(), ":", xdg_data_dirs, NULL); |
1272 | 348 |
349 g_free(xdg_data_dirs); | |
350 | |
351 split_dirs = g_strsplit(all_dirs, ":", 0); | |
352 | |
353 g_free(all_dirs); | |
354 | |
355 for (i = 0; split_dirs[i]; i++) | |
9 | 356 { |
1272 | 357 path = g_build_filename(split_dirs[i], "applications", NULL); |
358 editor_read_desktop_dir(path); | |
359 g_free(path); | |
9 | 360 } |
1272 | 361 |
362 g_strfreev(split_dirs); | |
363 | |
364 g_hash_table_foreach_remove(editors, editor_remove_desktop_file_cb, NULL); | |
9 | 365 } |
366 | |
1272 | 367 static void editor_list_add_cb(gpointer key, gpointer value, gpointer data) |
368 { | |
369 GList **listp = data; | |
370 EditorDescription *editor = value; | |
371 | |
1402 | 372 /* do not show the special commands in any list, they are called explicitly */ |
1272 | 373 if (strcmp(editor->key, CMD_COPY) == 0 || |
374 strcmp(editor->key, CMD_MOVE) == 0 || | |
375 strcmp(editor->key, CMD_RENAME) == 0 || | |
376 strcmp(editor->key, CMD_DELETE) == 0 || | |
377 strcmp(editor->key, CMD_FOLDER) == 0) return; | |
378 | |
379 *listp = g_list_prepend(*listp, editor); | |
380 } | |
381 | |
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
|
382 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
|
383 { |
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
|
384 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
|
385 const EditorDescription *eb = b; |
1402 | 386 gint ret; |
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 |
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 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
|
389 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
|
390 |
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 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
|
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 |
1272 | 394 GList *editor_list_get(void) |
395 { | |
396 GList *editors_list = NULL; | |
397 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
|
398 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
|
399 |
1272 | 400 return editors_list; |
401 } | |
402 | |
403 /* ------------------------------ */ | |
404 | |
405 | |
140 | 406 static void editor_verbose_data_free(EditorData *ed) |
407 { | |
408 if (!ed->vd) return; | |
409 g_free(ed->vd); | |
410 ed->vd = NULL; | |
411 } | |
412 | |
413 static void editor_data_free(EditorData *ed) | |
414 { | |
415 editor_verbose_data_free(ed); | |
416 g_free(ed); | |
417 } | |
418 | |
9 | 419 static void editor_verbose_window_close(GenericDialog *gd, gpointer data) |
420 { | |
140 | 421 EditorData *ed = data; |
9 | 422 |
423 generic_dialog_close(gd); | |
140 | 424 editor_verbose_data_free(ed); |
425 if (ed->pid == -1) editor_data_free(ed); /* the process has already terminated */ | |
9 | 426 } |
427 | |
428 static void editor_verbose_window_stop(GenericDialog *gd, gpointer data) | |
429 { | |
140 | 430 EditorData *ed = data; |
431 ed->stopping = TRUE; | |
432 ed->count = 0; | |
433 editor_verbose_window_progress(ed, _("stopping...")); | |
9 | 434 } |
435 | |
436 static void editor_verbose_window_enable_close(EditorVerboseData *vd) | |
437 { | |
438 vd->gd->cancel_cb = editor_verbose_window_close; | |
439 | |
440 spinner_set_interval(vd->spinner, -1); | |
441 gtk_widget_set_sensitive(vd->button_stop, FALSE); | |
442 gtk_widget_set_sensitive(vd->button_close, TRUE); | |
443 } | |
444 | |
140 | 445 static EditorVerboseData *editor_verbose_window(EditorData *ed, const gchar *text) |
9 | 446 { |
447 EditorVerboseData *vd; | |
448 GtkWidget *scrolled; | |
449 GtkWidget *hbox; | |
450 gchar *buf; | |
451 | |
452 vd = g_new0(EditorVerboseData, 1); | |
453 | |
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
|
454 vd->gd = file_util_gen_dlg(_("Edit command results"), "editor_results", |
9 | 455 NULL, FALSE, |
140 | 456 NULL, ed); |
9 | 457 buf = g_strdup_printf(_("Output of %s"), text); |
458 generic_dialog_add_message(vd->gd, NULL, buf, NULL); | |
459 g_free(buf); | |
460 vd->button_stop = generic_dialog_add_button(vd->gd, GTK_STOCK_STOP, NULL, | |
461 editor_verbose_window_stop, FALSE); | |
462 gtk_widget_set_sensitive(vd->button_stop, FALSE); | |
463 vd->button_close = generic_dialog_add_button(vd->gd, GTK_STOCK_CLOSE, NULL, | |
464 editor_verbose_window_close, TRUE); | |
465 gtk_widget_set_sensitive(vd->button_close, FALSE); | |
466 | |
467 scrolled = gtk_scrolled_window_new(NULL, NULL); | |
468 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN); | |
469 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled), | |
470 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); | |
471 gtk_box_pack_start(GTK_BOX(vd->gd->vbox), scrolled, TRUE, TRUE, 5); | |
472 gtk_widget_show(scrolled); | |
473 | |
474 vd->text = gtk_text_view_new(); | |
475 gtk_text_view_set_editable(GTK_TEXT_VIEW(vd->text), FALSE); | |
476 gtk_widget_set_size_request(vd->text, EDITOR_WINDOW_WIDTH, EDITOR_WINDOW_HEIGHT); | |
477 gtk_container_add(GTK_CONTAINER(scrolled), vd->text); | |
478 gtk_widget_show(vd->text); | |
479 | |
480 hbox = gtk_hbox_new(FALSE, 0); | |
481 gtk_box_pack_start(GTK_BOX(vd->gd->vbox), hbox, FALSE, FALSE, 0); | |
482 gtk_widget_show(hbox); | |
483 | |
484 vd->progress = gtk_progress_bar_new(); | |
485 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(vd->progress), 0.0); | |
486 gtk_box_pack_start(GTK_BOX(hbox), vd->progress, TRUE, TRUE, 0); | |
487 gtk_widget_show(vd->progress); | |
488 | |
489 vd->spinner = spinner_new(NULL, SPINNER_SPEED); | |
490 gtk_box_pack_start(GTK_BOX(hbox), vd->spinner, FALSE, FALSE, 0); | |
491 gtk_widget_show(vd->spinner); | |
442 | 492 |
9 | 493 gtk_widget_show(vd->gd->dialog); |
494 | |
140 | 495 ed->vd = vd; |
9 | 496 return vd; |
497 } | |
498 | |
499 static void editor_verbose_window_fill(EditorVerboseData *vd, gchar *text, gint len) | |
500 { | |
501 GtkTextBuffer *buffer; | |
502 GtkTextIter iter; | |
503 | |
504 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(vd->text)); | |
505 gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1); | |
506 gtk_text_buffer_insert(buffer, &iter, text, len); | |
507 } | |
508 | |
140 | 509 static void editor_verbose_window_progress(EditorData *ed, const gchar *text) |
9 | 510 { |
140 | 511 if (!ed->vd) return; |
512 | |
513 if (ed->total) | |
9 | 514 { |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
995
diff
changeset
|
515 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ed->vd->progress), (gdouble)ed->count / ed->total); |
9 | 516 } |
517 | |
140 | 518 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(ed->vd->progress), (text) ? text : ""); |
9 | 519 } |
520 | |
521 static gboolean editor_verbose_io_cb(GIOChannel *source, GIOCondition condition, gpointer data) | |
522 { | |
140 | 523 EditorData *ed = data; |
9 | 524 gchar buf[512]; |
525 gsize count; | |
526 | |
140 | 527 if (condition & G_IO_IN) |
9 | 528 { |
140 | 529 while (g_io_channel_read_chars(source, buf, sizeof(buf), &count, NULL) == G_IO_STATUS_NORMAL) |
530 { | |
531 if (!g_utf8_validate(buf, count, NULL)) | |
9 | 532 { |
140 | 533 gchar *utf8; |
444 | 534 |
140 | 535 utf8 = g_locale_to_utf8(buf, count, NULL, NULL, NULL); |
536 if (utf8) | |
9 | 537 { |
140 | 538 editor_verbose_window_fill(ed->vd, utf8, -1); |
539 g_free(utf8); | |
9 | 540 } |
541 else | |
542 { | |
288
d1f74154463e
Replace occurences of Geeqie / geeqie by constants defined in main.h.
zas_
parents:
283
diff
changeset
|
543 editor_verbose_window_fill(ed->vd, "Error converting text to valid utf8\n", -1); |
9 | 544 } |
545 } | |
140 | 546 else |
547 { | |
548 editor_verbose_window_fill(ed->vd, buf, count); | |
549 } | |
550 } | |
9 | 551 } |
552 | |
140 | 553 if (condition & (G_IO_ERR | G_IO_HUP)) |
9 | 554 { |
140 | 555 g_io_channel_shutdown(source, TRUE, NULL); |
9 | 556 return FALSE; |
557 } | |
558 | |
559 return TRUE; | |
560 } | |
561 | |
138 | 562 typedef enum { |
563 PATH_FILE, | |
1272 | 564 PATH_FILE_URL, |
140 | 565 PATH_DEST |
138 | 566 } PathType; |
567 | |
568 | |
1272 | 569 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
|
570 { |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
571 GString *string; |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
572 gchar *pathl; |
1408 | 573 const gchar *p = NULL; |
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 string = g_string_new(""); |
442 | 576 |
1272 | 577 if (type == PATH_FILE || type == PATH_FILE_URL) |
138 | 578 { |
1272 | 579 GList *work = editor->ext_list; |
442 | 580 |
147
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
581 if (!work) |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
582 p = fd->path; |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
583 else |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
584 { |
516 | 585 while (work) |
147
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
586 { |
444 | 587 GList *work2; |
147
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
588 gchar *ext = work->data; |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
589 work = work->next; |
442 | 590 |
591 if (strcmp(ext, "*") == 0 || | |
1307 | 592 g_ascii_strcasecmp(ext, fd->extension) == 0) |
147
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
593 { |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
594 p = fd->path; |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
595 break; |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
596 } |
442 | 597 |
444 | 598 work2 = fd->sidecar_files; |
516 | 599 while (work2) |
147
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
600 { |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
601 FileData *sfd = work2->data; |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
602 work2 = work2->next; |
442 | 603 |
1307 | 604 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
|
605 { |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
606 p = sfd->path; |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
607 break; |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
608 } |
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 if (p) break; |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
611 } |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
612 if (!p) return NULL; |
b2266996fa83
added possibility to specify prefered file type for external commands
nadvornik
parents:
140
diff
changeset
|
613 } |
138 | 614 } |
140 | 615 else if (type == PATH_DEST) |
138 | 616 { |
617 if (fd->change && fd->change->dest) | |
618 p = fd->change->dest; | |
619 else | |
620 p = ""; | |
621 } | |
444 | 622 |
1407
57421a728682
Revert changeset 1502. It brokes the spec: hidden desktop files from user directory should hide files from system directory with the same name.
zas_
parents:
1406
diff
changeset
|
623 g_assert(p); |
123
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
624 while (*p != '\0') |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
625 { |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
626 /* must escape \, ", `, and $ to avoid problems, |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
627 * 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
|
628 */ |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
629 if (strchr("\\\"`$", *p) != NULL) |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
630 { |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
631 string = g_string_append_c(string, '\\'); |
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 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
|
634 p++; |
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 |
1272 | 637 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
|
638 pathl = path_from_utf8(string->str); |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
639 g_string_free(string, TRUE); |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
640 |
1402 | 641 if (pathl && !pathl[0]) /* empty string case */ |
642 { | |
643 g_free(pathl); | |
644 pathl = NULL; | |
645 } | |
646 | |
123
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
647 return pathl; |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
648 } |
3602a4aa7c71
Sat Dec 2 20:15:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
60
diff
changeset
|
649 |
9 | 650 |
1405 | 651 EditorFlags editor_command_parse(const EditorDescription *editor, GList *list, gchar **output) |
9 | 652 { |
1405 | 653 EditorFlags flags = 0; |
1272 | 654 const gchar *p; |
140 | 655 GString *result = NULL; |
442 | 656 |
140 | 657 if (output) |
658 result = g_string_new(""); | |
659 | |
1272 | 660 if (editor->exec[0] == '\0') |
140 | 661 { |
662 flags |= EDITOR_ERROR_EMPTY; | |
663 goto err; | |
664 } | |
669 | 665 |
1272 | 666 p = editor->exec; |
669 | 667 /* skip leading whitespaces if any */ |
668 while (g_ascii_isspace(*p)) p++; | |
442 | 669 |
140 | 670 /* command */ |
442 | 671 |
140 | 672 while (*p) |
673 { | |
674 if (*p != '%') | |
675 { | |
676 if (output) result = g_string_append_c(result, *p); | |
677 } | |
678 else /* *p == '%' */ | |
679 { | |
680 gchar *pathl = NULL; | |
9 | 681 |
140 | 682 p++; |
442 | 683 |
684 switch (*p) | |
140 | 685 { |
1272 | 686 case 'f': /* single file */ |
687 case 'u': /* single url */ | |
140 | 688 flags |= EDITOR_FOR_EACH; |
689 if (flags & EDITOR_SINGLE_COMMAND) | |
690 { | |
691 flags |= EDITOR_ERROR_INCOMPATIBLE; | |
692 goto err; | |
693 } | |
1399 | 694 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
|
695 { |
1399 | 696 /* use the first file from the list */ |
697 if (!list->data) | |
698 { | |
699 flags |= EDITOR_ERROR_NO_FILE; | |
700 goto err; | |
701 } | |
702 pathl = editor_command_path_parse((FileData *)list->data, | |
703 (*p == 'f') ? PATH_FILE : PATH_FILE_URL, | |
704 editor); | |
705 if (!pathl) | |
706 { | |
707 flags |= EDITOR_ERROR_NO_FILE; | |
708 goto err; | |
709 } | |
710 if (output) | |
711 { | |
712 result = g_string_append_c(result, '"'); | |
713 result = g_string_append(result, pathl); | |
714 result = g_string_append_c(result, '"'); | |
715 } | |
716 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
|
717 } |
442 | 718 break; |
60
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
719 |
1272 | 720 case 'F': |
721 case 'U': | |
140 | 722 flags |= EDITOR_SINGLE_COMMAND; |
723 if (flags & (EDITOR_FOR_EACH | EDITOR_DEST)) | |
724 { | |
725 flags |= EDITOR_ERROR_INCOMPATIBLE; | |
726 goto err; | |
727 } | |
728 | |
1399 | 729 if (list) |
140 | 730 { |
731 /* use whole list */ | |
732 GList *work = list; | |
733 gboolean ok = FALSE; | |
444 | 734 |
140 | 735 while (work) |
736 { | |
737 FileData *fd = work->data; | |
1272 | 738 pathl = editor_command_path_parse(fd, (*p == 'F') ? PATH_FILE : PATH_FILE_URL, editor); |
140 | 739 if (pathl) |
740 { | |
741 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
|
742 |
a0bd58a6535f
In various Edit context menus, only display editors that match the file types in the selection.
zas_
parents:
1367
diff
changeset
|
743 if (output) |
a0bd58a6535f
In various Edit context menus, only display editors that match the file types in the selection.
zas_
parents:
1367
diff
changeset
|
744 { |
a0bd58a6535f
In various Edit context menus, only display editors that match the file types in the selection.
zas_
parents:
1367
diff
changeset
|
745 ok = TRUE; |
a0bd58a6535f
In various Edit context menus, only display editors that match the file types in the selection.
zas_
parents:
1367
diff
changeset
|
746 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
|
747 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
|
748 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
|
749 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
|
750 } |
140 | 751 g_free(pathl); |
752 } | |
753 work = work->next; | |
754 } | |
442 | 755 if (!ok) |
140 | 756 { |
757 flags |= EDITOR_ERROR_NO_FILE; | |
758 goto err; | |
759 } | |
760 } | |
442 | 761 break; |
1272 | 762 case 'i': |
763 if (output) | |
764 { | |
765 result = g_string_append(result, editor->icon); | |
766 } | |
767 break; | |
768 case 'c': | |
769 if (output) | |
770 { | |
771 result = g_string_append(result, editor->name); | |
772 } | |
773 break; | |
774 case 'k': | |
775 if (output) | |
776 { | |
777 result = g_string_append(result, editor->file); | |
778 } | |
779 break; | |
669 | 780 case '%': |
781 /* %% = % escaping */ | |
782 if (output) result = g_string_append_c(result, *p); | |
783 break; | |
1272 | 784 case 'd': |
785 case 'D': | |
786 case 'n': | |
787 case 'N': | |
788 case 'v': | |
789 case 'm': | |
790 /* deprecated according to spec, ignore */ | |
791 break; | |
140 | 792 default: |
793 flags |= EDITOR_ERROR_SYNTAX; | |
794 goto err; | |
795 } | |
796 } | |
797 p++; | |
9 | 798 } |
799 | |
140 | 800 if (output) *output = g_string_free(result, FALSE); |
801 return flags; | |
802 | |
442 | 803 |
140 | 804 err: |
442 | 805 if (output) |
9 | 806 { |
140 | 807 g_string_free(result, TRUE); |
808 *output = NULL; | |
809 } | |
810 return flags; | |
811 } | |
812 | |
1272 | 813 |
1397
a0bd58a6535f
In various Edit context menus, only display editors that match the file types in the selection.
zas_
parents:
1367
diff
changeset
|
814 static void editor_child_exit_cb(GPid pid, gint status, gpointer data) |
140 | 815 { |
816 EditorData *ed = data; | |
817 g_spawn_close_pid(pid); | |
818 ed->pid = -1; | |
442 | 819 |
140 | 820 editor_command_next_finish(ed, status); |
821 } | |
822 | |
823 | |
1405 | 824 static EditorFlags editor_command_one(const EditorDescription *editor, GList *list, EditorData *ed) |
140 | 825 { |
826 gchar *command; | |
827 FileData *fd = list->data; | |
828 GPid pid; | |
442 | 829 gint standard_output; |
830 gint standard_error; | |
140 | 831 gboolean ok; |
832 | |
833 ed->pid = -1; | |
1405 | 834 ed->flags = editor->flags; |
835 ed->flags |= editor_command_parse(editor, list, &command); | |
140 | 836 |
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
|
837 ok = !EDITOR_ERRORS(ed->flags); |
140 | 838 |
839 if (ok) | |
840 { | |
737
8a8873e7a552
Make shell command and its option rc file options instead of hardcoded strings.
zas_
parents:
731
diff
changeset
|
841 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
|
842 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
|
843 |
8a8873e7a552
Make shell command and its option rc file options instead of hardcoded strings.
zas_
parents:
731
diff
changeset
|
844 if (ok) |
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 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
|
847 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
|
848 } |
8a8873e7a552
Make shell command and its option rc file options instead of hardcoded strings.
zas_
parents:
731
diff
changeset
|
849 |
8a8873e7a552
Make shell command and its option rc file options instead of hardcoded strings.
zas_
parents:
731
diff
changeset
|
850 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
|
851 } |
8a8873e7a552
Make shell command and its option rc file options instead of hardcoded strings.
zas_
parents:
731
diff
changeset
|
852 |
8a8873e7a552
Make shell command and its option rc file options instead of hardcoded strings.
zas_
parents:
731
diff
changeset
|
853 if (ok) |
8a8873e7a552
Make shell command and its option rc file options instead of hardcoded strings.
zas_
parents:
731
diff
changeset
|
854 { |
443 | 855 gchar *working_directory; |
856 gchar *args[4]; | |
737
8a8873e7a552
Make shell command and its option rc file options instead of hardcoded strings.
zas_
parents:
731
diff
changeset
|
857 guint n = 0; |
443 | 858 |
859 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
|
860 args[n++] = options->shell.path; |
8a8873e7a552
Make shell command and its option rc file options instead of hardcoded strings.
zas_
parents:
731
diff
changeset
|
861 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
|
862 args[n++] = options->shell.options; |
8a8873e7a552
Make shell command and its option rc file options instead of hardcoded strings.
zas_
parents:
731
diff
changeset
|
863 args[n++] = command; |
8a8873e7a552
Make shell command and its option rc file options instead of hardcoded strings.
zas_
parents:
731
diff
changeset
|
864 args[n] = NULL; |
443 | 865 |
1272 | 866 if ((ed->flags & EDITOR_DEST) && fd->change && fd->change->dest) /* FIXME: error handling */ |
867 { | |
1402 | 868 g_setenv("GEEQIE_DESTINATION", fd->change->dest, TRUE); |
1272 | 869 } |
870 else | |
871 { | |
1402 | 872 g_unsetenv("GEEQIE_DESTINATION"); |
1272 | 873 } |
874 | |
442 | 875 ok = g_spawn_async_with_pipes(working_directory, args, NULL, |
140 | 876 G_SPAWN_DO_NOT_REAP_CHILD, /* GSpawnFlags */ |
442 | 877 NULL, NULL, |
878 &pid, | |
879 NULL, | |
880 ed->vd ? &standard_output : NULL, | |
881 ed->vd ? &standard_error : NULL, | |
140 | 882 NULL); |
443 | 883 |
884 g_free(working_directory); | |
442 | 885 |
140 | 886 if (!ok) ed->flags |= EDITOR_ERROR_CANT_EXEC; |
887 } | |
888 | |
442 | 889 if (ok) |
140 | 890 { |
891 g_child_watch_add(pid, editor_child_exit_cb, ed); | |
892 ed->pid = pid; | |
893 } | |
442 | 894 |
140 | 895 if (ed->vd) |
896 { | |
897 if (!ok) | |
9 | 898 { |
140 | 899 gchar *buf; |
900 | |
1272 | 901 buf = g_strdup_printf(_("Failed to run command:\n%s\n"), editor->file); |
140 | 902 editor_verbose_window_fill(ed->vd, buf, strlen(buf)); |
903 g_free(buf); | |
904 | |
905 } | |
442 | 906 else |
140 | 907 { |
908 GIOChannel *channel_output; | |
909 GIOChannel *channel_error; | |
444 | 910 |
140 | 911 channel_output = g_io_channel_unix_new(standard_output); |
912 g_io_channel_set_flags(channel_output, G_IO_FLAG_NONBLOCK, NULL); | |
926 | 913 g_io_channel_set_encoding(channel_output, NULL, NULL); |
140 | 914 |
915 g_io_add_watch_full(channel_output, G_PRIORITY_HIGH, G_IO_IN | G_IO_ERR | G_IO_HUP, | |
916 editor_verbose_io_cb, ed, NULL); | |
917 g_io_channel_unref(channel_output); | |
918 | |
919 channel_error = g_io_channel_unix_new(standard_error); | |
920 g_io_channel_set_flags(channel_error, G_IO_FLAG_NONBLOCK, NULL); | |
926 | 921 g_io_channel_set_encoding(channel_error, NULL, NULL); |
140 | 922 |
923 g_io_add_watch_full(channel_error, G_PRIORITY_HIGH, G_IO_IN | G_IO_ERR | G_IO_HUP, | |
924 editor_verbose_io_cb, ed, NULL); | |
925 g_io_channel_unref(channel_error); | |
926 } | |
927 } | |
442 | 928 |
140 | 929 g_free(command); |
930 | |
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
|
931 return EDITOR_ERRORS(ed->flags); |
140 | 932 } |
933 | |
1405 | 934 static EditorFlags editor_command_next_start(EditorData *ed) |
140 | 935 { |
936 if (ed->vd) editor_verbose_window_fill(ed->vd, "\n", 1); | |
937 | |
938 if (ed->list && ed->count < ed->total) | |
939 { | |
940 FileData *fd; | |
1405 | 941 EditorFlags error; |
140 | 942 |
943 fd = ed->list->data; | |
944 | |
945 if (ed->vd) | |
946 { | |
1402 | 947 if (ed->flags & EDITOR_FOR_EACH) |
948 editor_verbose_window_progress(ed, fd->path); | |
949 else | |
950 editor_verbose_window_progress(ed, _("running...")); | |
140 | 951 } |
952 ed->count++; | |
953 | |
1272 | 954 error = editor_command_one(ed->editor, ed->list, ed); |
140 | 955 if (!error && ed->vd) |
956 { | |
957 gtk_widget_set_sensitive(ed->vd->button_stop, (ed->list != NULL) ); | |
958 if (ed->flags & EDITOR_FOR_EACH) | |
9 | 959 { |
140 | 960 editor_verbose_window_fill(ed->vd, fd->path, strlen(fd->path)); |
961 editor_verbose_window_fill(ed->vd, "\n", 1); | |
9 | 962 } |
963 } | |
140 | 964 |
442 | 965 if (!error) |
140 | 966 return 0; |
1402 | 967 |
968 /* command was not started, call the finish immediately */ | |
969 return editor_command_next_finish(ed, 0); | |
140 | 970 } |
442 | 971 |
140 | 972 /* everything is done */ |
237
404629011caa
Add missing return at the end of editor_command_next_start().
zas_
parents:
196
diff
changeset
|
973 return editor_command_done(ed); |
140 | 974 } |
975 | |
1405 | 976 static EditorFlags editor_command_next_finish(EditorData *ed, gint status) |
140 | 977 { |
978 gint cont = ed->stopping ? EDITOR_CB_SKIP : EDITOR_CB_CONTINUE; | |
979 | |
980 if (status) | |
981 ed->flags |= EDITOR_ERROR_STATUS; | |
982 | |
983 if (ed->flags & EDITOR_FOR_EACH) | |
984 { | |
985 /* handle the first element from the list */ | |
986 GList *fd_element = ed->list; | |
444 | 987 |
140 | 988 ed->list = g_list_remove_link(ed->list, fd_element); |
989 if (ed->callback) | |
911 | 990 { |
140 | 991 cont = ed->callback(ed->list ? ed : NULL, ed->flags, fd_element, ed->data); |
911 | 992 if (ed->stopping && cont == EDITOR_CB_CONTINUE) cont = EDITOR_CB_SKIP; |
993 } | |
140 | 994 filelist_free(fd_element); |
9 | 995 } |
996 else | |
997 { | |
140 | 998 /* handle whole list */ |
999 if (ed->callback) | |
1000 cont = ed->callback(NULL, ed->flags, ed->list, ed->data); | |
1001 filelist_free(ed->list); | |
1002 ed->list = NULL; | |
1003 } | |
9 | 1004 |
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
|
1005 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
|
1006 { |
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
|
1007 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
|
1008 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
|
1009 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
|
1010 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
|
1011 } |
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
|
1012 |
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
|
1013 return editor_command_next_start(ed); |
140 | 1014 } |
9 | 1015 |
1405 | 1016 static EditorFlags editor_command_done(EditorData *ed) |
140 | 1017 { |
1405 | 1018 EditorFlags flags; |
9 | 1019 |
140 | 1020 if (ed->vd) |
1021 { | |
1022 if (ed->count == ed->total) | |
9 | 1023 { |
1402 | 1024 editor_verbose_window_progress(ed, _("done")); |
9 | 1025 } |
1026 else | |
1027 { | |
1402 | 1028 editor_verbose_window_progress(ed, _("stopped by user")); |
9 | 1029 } |
140 | 1030 editor_verbose_window_enable_close(ed->vd); |
1031 } | |
1032 | |
1033 /* free the not-handled items */ | |
1034 if (ed->list) | |
1035 { | |
1036 ed->flags |= EDITOR_ERROR_SKIPPED; | |
1037 if (ed->callback) ed->callback(NULL, ed->flags, ed->list, ed->data); | |
1038 filelist_free(ed->list); | |
1039 ed->list = NULL; | |
1040 } | |
9 | 1041 |
140 | 1042 ed->count = 0; |
1043 | |
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
|
1044 flags = EDITOR_ERRORS(ed->flags); |
140 | 1045 |
1046 if (!ed->vd) editor_data_free(ed); | |
1047 | |
1048 return flags; | |
1049 } | |
1050 | |
1051 void editor_resume(gpointer ed) | |
1052 { | |
1053 editor_command_next_start(ed); | |
1054 } | |
443 | 1055 |
140 | 1056 void editor_skip(gpointer ed) |
1057 { | |
442 | 1058 editor_command_done(ed); |
9 | 1059 } |
1060 | |
1405 | 1061 static EditorFlags editor_command_start(const EditorDescription *editor, const gchar *text, GList *list, EditorCallback cb, gpointer data) |
140 | 1062 { |
1063 EditorData *ed; | |
1405 | 1064 EditorFlags flags = editor->flags; |
442 | 1065 |
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
|
1066 if (EDITOR_ERRORS(flags)) return EDITOR_ERRORS(flags); |
140 | 1067 |
1068 ed = g_new0(EditorData, 1); | |
1069 ed->list = filelist_copy(list); | |
1070 ed->flags = flags; | |
1272 | 1071 ed->editor = editor; |
140 | 1072 ed->total = (flags & EDITOR_SINGLE_COMMAND) ? 1 : g_list_length(list); |
1073 ed->callback = cb; | |
1074 ed->data = data; | |
442 | 1075 |
140 | 1076 if ((flags & EDITOR_VERBOSE_MULTI) && list && list->next) |
1077 flags |= EDITOR_VERBOSE; | |
442 | 1078 |
140 | 1079 if (flags & EDITOR_VERBOSE) |
1080 editor_verbose_window(ed, text); | |
442 | 1081 |
1082 editor_command_next_start(ed); | |
140 | 1083 /* 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
|
1084 return EDITOR_ERRORS(flags); |
140 | 1085 } |
1086 | |
1272 | 1087 gboolean is_valid_editor_command(const gchar *key) |
444 | 1088 { |
1272 | 1089 if (!key) return FALSE; |
1090 return g_hash_table_lookup(editors, key) != NULL; | |
444 | 1091 } |
1092 | |
1405 | 1093 EditorFlags start_editor_from_filelist_full(const gchar *key, GList *list, EditorCallback cb, gpointer data) |
9 | 1094 { |
1405 | 1095 EditorFlags error; |
1272 | 1096 EditorDescription *editor; |
1097 if (!key) return FALSE; | |
1098 | |
1099 editor = g_hash_table_lookup(editors, key); | |
9 | 1100 |
444 | 1101 if (!list) return FALSE; |
1272 | 1102 if (!editor) return FALSE; |
1103 | |
1104 error = editor_command_start(editor, editor->name, list, cb, data); | |
9 | 1105 |
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
|
1106 if (EDITOR_ERRORS(error)) |
669 | 1107 { |
1272 | 1108 gchar *text = g_strdup_printf(_("%s\n\"%s\""), editor_get_error_str(error), editor->file); |
669 | 1109 |
1110 file_util_warning_dialog(_("Invalid editor command"), text, GTK_STOCK_DIALOG_ERROR, NULL); | |
1111 g_free(text); | |
1112 } | |
1113 | |
140 | 1114 return error; |
9 | 1115 } |
1116 | |
1405 | 1117 EditorFlags start_editor_from_filelist(const gchar *key, GList *list) |
140 | 1118 { |
1272 | 1119 return start_editor_from_filelist_full(key, list, NULL, NULL); |
140 | 1120 } |
1121 | |
1405 | 1122 EditorFlags start_editor_from_file_full(const gchar *key, FileData *fd, EditorCallback cb, gpointer data) |
9 | 1123 { |
1124 GList *list; | |
1405 | 1125 EditorFlags error; |
9 | 1126 |
138 | 1127 if (!fd) return FALSE; |
9 | 1128 |
138 | 1129 list = g_list_append(NULL, fd); |
1272 | 1130 error = start_editor_from_filelist_full(key, list, cb, data); |
9 | 1131 g_list_free(list); |
140 | 1132 return error; |
9 | 1133 } |
60
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
1134 |
1405 | 1135 EditorFlags start_editor_from_file(const gchar *key, FileData *fd) |
136 | 1136 { |
1272 | 1137 return start_editor_from_file_full(key, fd, NULL, NULL); |
136 | 1138 } |
1139 | |
1405 | 1140 gboolean 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
|
1141 { |
1272 | 1142 EditorDescription *editor; |
1143 if (!key) return TRUE; | |
1144 | |
1145 editor = g_hash_table_lookup(editors, key); | |
1146 if (!editor) return TRUE; | |
60
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
1147 |
1405 | 1148 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
|
1149 } |
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
1150 |
1405 | 1151 gboolean editor_is_filter(const gchar *key) |
753 | 1152 { |
1272 | 1153 EditorDescription *editor; |
1154 if (!key) return TRUE; | |
1155 | |
1156 editor = g_hash_table_lookup(editors, key); | |
1157 if (!editor) return TRUE; | |
753 | 1158 |
1405 | 1159 return !!(editor->flags & EDITOR_DEST); |
753 | 1160 } |
1161 | |
1405 | 1162 const gchar *editor_get_error_str(EditorFlags flags) |
140 | 1163 { |
1164 if (flags & EDITOR_ERROR_EMPTY) return _("Editor template is empty."); | |
1165 if (flags & EDITOR_ERROR_SYNTAX) return _("Editor template has incorrect syntax."); | |
1166 if (flags & EDITOR_ERROR_INCOMPATIBLE) return _("Editor template uses incompatible macros."); | |
1167 if (flags & EDITOR_ERROR_NO_FILE) return _("Can't find matching file type."); | |
1168 if (flags & EDITOR_ERROR_CANT_EXEC) return _("Can't execute external editor."); | |
1169 if (flags & EDITOR_ERROR_STATUS) return _("External editor returned error status."); | |
1170 if (flags & EDITOR_ERROR_SKIPPED) return _("File was skipped."); | |
1171 return _("Unknown error."); | |
1172 } | |
731
fa8f7d7396cf
Introduce an helper function that returns the name of an editor.
zas_
parents:
730
diff
changeset
|
1173 |
1272 | 1174 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
|
1175 { |
1272 | 1176 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
|
1177 |
1272 | 1178 if (!editor) return NULL; |
1179 | |
1180 return editor->name; | |
731
fa8f7d7396cf
Introduce an helper function that returns the name of an editor.
zas_
parents:
730
diff
changeset
|
1181 } |
1055
1646720364cf
Adding a vim modeline to all files - patch by Klaus Ethgen
nadvornik
parents:
1022
diff
changeset
|
1182 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */ |