my homework was late :-)
[midnight-commander.git] / gnome / gaction.c
blobdf62e036665c0b3559cc1a7ead205a717ff69a72
1 #include <config.h>
2 #include <string.h>
3 #include "x.h"
4 #include "global.h"
5 #include "dir.h"
6 #include "command.h"
7 #include "panel.h"
8 #define WANT_WIDGETS /* bleah */
9 #include "main.h"
10 #include "color.h"
11 #include "mouse.h"
12 #include "layout.h" /* get_panel_widget */
13 #include "ext.h" /* regex_command */
14 #include "cmd.h" /* copy_cmd, ren_cmd, delete_cmd, ... */
15 #include "gscreen.h"
16 #include "gmain.h"
17 #include "dir.h"
18 #include "dialog.h"
19 #include "view.h"
20 #include "gcmd.h"
21 #include "../vfs/vfs.h"
22 #include "gnome-open-dialog.h"
24 static void
25 gmc_unable_to_execute_dlg (gchar *fname, const gchar *command, gchar *action, const gchar *mime)
27 GtkWidget *msg_dialog = NULL;
28 GtkWidget *hack_widget;
29 gchar *msg;
30 gchar *fix = NULL;
31 if (!strcmp (action, "x-gnome-app-info")) {
32 msg = g_strdup_printf (
33 _("Unable to execute\n\"%s\".\n\n"
34 "Please check it to see if it points to a valid command."),
35 fname);
37 } else {
38 if (mime)
39 fix = g_strdup_printf (
40 _("\".\n\nTo fix this, bring up the mime-properties editor "
41 "in the GNOME Control Center, and edit the default %s"
42 "-action for \"%s\"."),
43 action, mime);
44 else
45 fix = g_strdup_printf (
46 _("\".\n\nTo fix this error, bring up this file's properties "
47 "and change the default %s-action."),
48 action);
50 msg = g_strdup_printf (
51 _("Unable to %s\n\"%s\"\nwith the command:\n\"%s\"%s"),
52 action, fname, command, fix);
54 g_free (fix);
56 msg_dialog = gnome_message_box_new (msg,
57 GNOME_MESSAGE_BOX_ERROR,
58 GNOME_STOCK_BUTTON_OK,
59 NULL);
60 /* Kids, don't try this at home */
61 /* this is pretty evil... <-: */
62 hack_widget = GNOME_DIALOG (msg_dialog)->vbox;
63 hack_widget = ((GtkBoxChild *) GTK_BOX (hack_widget)->children->data)->widget;
64 hack_widget = ((GtkBoxChild *) GTK_BOX (hack_widget)->children->next->data)->widget;
65 gtk_label_set_line_wrap (GTK_LABEL (hack_widget), TRUE);
67 gnome_dialog_run_and_close (GNOME_DIALOG (msg_dialog));
68 g_free (msg);
71 static void
72 gmc_execute (const char *fname, const char *buf, int needs_terminal)
74 exec_extension (fname, buf, NULL, NULL, 0, needs_terminal);
77 static gboolean
78 gmc_check_exec_string (const char *buf)
80 gint argc;
81 gint retval, i;
82 gchar *prog;
83 gchar **argv;
85 gnome_config_make_vector (buf, &argc, &argv);
86 if (argc < 1)
87 return FALSE;
89 prog = gnome_is_program_in_path (argv[0]);
90 if (prog) {
91 retval = TRUE;
92 g_free (prog);
93 } else
94 retval = FALSE;
95 /* check to see if it's a GMC directive. */
96 if ((strlen (argv[0]) > 1) && (argv[0][0] == '%') && (argv[0][1] != '%'))
97 retval = TRUE;
98 for (i = 0; i < argc; i++)
99 g_free (argv[i]);
100 g_free (argv);
101 return retval;
105 gmc_open_filename (char *fname, GList *args)
107 /* fname is a full name */
109 const char *mime_type;
110 const char *cmd;
111 char *buf = NULL;
112 int size;
113 int needs_terminal = 0;
115 if (use_magic)
116 mime_type = gnome_mime_type_or_default_of_file (fname, NULL);
117 else
118 mime_type = gnome_mime_type_or_default (fname, NULL);
121 * We accept needs_terminal as -1, which means our caller
122 * did not want to do the work
124 if (gnome_metadata_get (fname, "flags", &size, &buf) == 0){
125 needs_terminal = (strstr (buf, "needsterminal") != 0);
126 g_free (buf);
127 buf = NULL;
128 } else if (mime_type)
129 needs_terminal = gnome_mime_needsterminal (mime_type, "open.flags");
131 if (gnome_metadata_get (fname, "fm-open", &size, &buf) != 0)
132 gnome_metadata_get (fname, "open", &size, &buf);
134 if (buf) {
135 if (gmc_check_exec_string (buf))
136 gmc_execute (fname, buf, needs_terminal);
137 else
138 gmc_unable_to_execute_dlg (fname, buf, _("open"), NULL);
139 g_free (buf);
140 return 1;
143 if (!mime_type)
144 return 0;
146 cmd = gnome_mime_get_value (mime_type, "fm-open");
147 if (cmd == NULL) {
148 cmd = gnome_mime_get_value (mime_type, "open");
151 if (cmd){
152 if (gmc_check_exec_string (cmd))
153 gmc_execute (fname, cmd, needs_terminal);
154 else
155 gmc_unable_to_execute_dlg (fname, cmd, _("open"), mime_type);
156 return 1;
159 if ((strcmp (mime_type, "application/x-gnome-app-info") == 0) ||
160 (strcmp (mime_type, "application/x-kde-app-info") == 0)){
161 GnomeDesktopEntry *entry;
163 entry = gnome_desktop_entry_load (fname);
165 if (entry){
166 gnome_desktop_entry_launch (entry);
167 gnome_desktop_entry_free (entry);
168 return 1;
169 } else {
170 gmc_unable_to_execute_dlg (fname, NULL, "x-gnome-app-info", NULL);
171 return 1;
175 /* We just struck out. Prompt the user. */
176 return 0;
180 gmc_edit (char *fname)
182 /* fname is a full path */
184 const char *mime_type;
185 const char *cmd;
186 char *buf;
187 int size;
188 char *editor, *type;
189 int on_terminal;
191 if (gnome_metadata_get (fname, "edit", &size, &buf) == 0){
192 if (gmc_check_exec_string (buf))
193 gmc_execute (fname, buf, 0);
194 else
195 gmc_unable_to_execute_dlg (fname, buf, _("edit"), NULL);
196 g_free (buf);
197 return 1;
200 if (use_magic)
201 mime_type = gnome_mime_type_or_default_of_file (fname, NULL);
202 else
203 mime_type = gnome_mime_type_or_default (fname, NULL);
205 if (mime_type){
206 cmd = gnome_mime_get_value (mime_type, "edit");
208 if (cmd){
209 if (gmc_check_exec_string (cmd))
210 gmc_execute (fname, cmd, 0);
211 else
212 gmc_unable_to_execute_dlg (fname, cmd, _("edit"), mime_type);
213 return 1;
217 gnome_config_push_prefix( "/editor/Editor/");
218 type = gnome_config_get_string ("EDITOR_TYPE=executable");
220 if (strcmp (type, "mc-internal") == 0){
221 g_free (type);
222 do_edit (fname);
223 return 1;
225 g_free (type);
227 editor = gnome_config_get_string ("EDITOR=emacs");
228 on_terminal = gnome_config_get_bool ("NEEDS_TERM=false");
230 if (on_terminal){
231 char *quoted = name_quote (fname, 0);
232 char *editor_cmd = g_strconcat (editor, " ", quoted, NULL);
234 gnome_open_terminal_with_cmd (editor_cmd);
235 g_free (quoted);
236 g_free (editor_cmd);
237 } else {
238 char *cmd = g_strconcat (editor, " %s", NULL);
240 gmc_execute (fname, cmd, 0);
241 g_free (cmd);
244 g_free (editor);
245 return 0;
249 gmc_open (file_entry *fe)
251 gint retval;
252 gchar *fullname;
253 fullname = g_concat_dir_and_file (cpanel->cwd, fe->fname);
254 retval = gmc_open_filename (fullname, NULL);
255 g_free (fullname);
256 return retval;
260 gmc_open_with (gchar *filename)
262 GtkWidget *dialog;
263 gchar *command = NULL;
264 gchar *real_command;
265 WPanel *panel = cpanel;
267 dialog = gnome_open_dialog_new (filename);
268 if (!is_a_desktop_panel (panel))
269 gnome_dialog_set_parent (GNOME_DIALOG (dialog), GTK_WINDOW (panel->xwindow));
271 switch (gnome_dialog_run (GNOME_DIALOG (dialog))) {
272 case 0:
273 command = gtk_entry_get_text (GTK_ENTRY (gnome_entry_gtk_entry (GNOME_ENTRY (
274 GNOME_FILE_ENTRY (GNOME_OPEN_DIALOG (dialog)->entry)->gentry))));
275 /* Assume COMMAND %f */
276 if (command) {
277 /* FIXME: these need the needs_terminal argument to be set correctly */
278 if (strchr (command, '%'))
279 exec_extension (filename, command, NULL, NULL, 0, FALSE);
280 else {
281 /* This prolly isn't perfect, but it will do. */
282 real_command = g_strconcat (command, " %f", NULL);
283 exec_extension (filename, real_command, NULL, NULL, 0, FALSE);
284 g_free (real_command);
287 if (command) {
288 gtk_widget_destroy (dialog);
289 return 1;
291 gtk_widget_destroy (dialog);
292 return 0;
293 case 1:
294 gtk_widget_destroy (dialog);
295 default:
296 return 0;
300 static void
301 gmc_run_view (const char *filename, const char *buf)
303 exec_extension (filename, buf, NULL, NULL, 0, 0);
306 static gchar *
307 gmc_view_command (gchar *filename)
309 const char *mime_type, *cmd;
310 char *buf;
311 int size;
313 if (gnome_metadata_get (filename, "fm-view", &size, &buf) == 0)
314 return buf;
316 if (gnome_metadata_get (filename, "view", &size, &buf) == 0)
317 return buf;
319 if (use_magic)
320 mime_type = gnome_mime_type_or_default_of_file (filename, NULL);
321 else
322 mime_type = gnome_mime_type_or_default (filename, NULL);
324 if (!mime_type)
325 return NULL;
328 cmd = gnome_mime_get_value (mime_type, "fm-view");
330 if (cmd)
331 return g_strdup (cmd);
333 cmd = gnome_mime_get_value (mime_type, "view");
334 if (cmd){
335 return g_strdup (cmd);
337 return NULL;
341 gmc_view (char *filename, int start_line)
343 gchar *cmd;
344 const gchar *mime_type;
345 if (use_magic)
346 mime_type = gnome_mime_type_or_default_of_file (filename, NULL);
347 else
348 mime_type = gnome_mime_type_or_default (filename, NULL);
349 cmd = gmc_view_command (filename);
350 if (cmd) {
351 if (gmc_check_exec_string (cmd))
352 gmc_run_view (filename, cmd);
353 else
354 gmc_unable_to_execute_dlg (filename, cmd, _("view"), mime_type);
355 g_free (cmd);
356 return 1;
357 } else
358 view (NULL, filename, 0, 0);
359 return 0;