nautilus-program-choosing.c 26.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/* nautilus-program-choosing.c - functions for selecting and activating
 				 programs for opening/viewing particular files.

   Copyright (C) 2000 Eazel, Inc.

   The Gnome Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Author: John Sullivan <sullivan@eazel.com>
*/

#include <config.h>
#include "nautilus-program-choosing.h"
28 29

#include "nautilus-mime-actions.h"
30
#include "nautilus-global-preferences.h"
31
#include "nautilus-icon-factory.h"
32 33
#include <eel/eel-glib-extensions.h>
#include <eel/eel-gnome-extensions.h>
34
#include <eel/eel-vfs-extensions.h>
Ramiro Estrugo's avatar
Ramiro Estrugo committed
35
#include <eel/eel-stock-dialogs.h>
36
#include <eel/eel-preferences.h>
Ramiro Estrugo's avatar
Ramiro Estrugo committed
37
#include <eel/eel-string.h>
38
#include <gtk/gtk.h>
39
#include <libgnome/gnome-config.h>
40
#include <libgnome/gnome-i18n.h>
41
#include <libgnome/gnome-util.h>
42
#include <libgnome/gnome-desktop-item.h>
43
#include <libgnome/gnome-url.h>
44
#include <libgnomeui/gnome-uidefs.h>
45
#include <libgnomevfs/gnome-vfs-mime-handlers.h>
46
#include <libgnomevfs/gnome-vfs-utils.h>
47
#include <stdlib.h>
48

49 50 51 52 53 54 55 56
/* This number controls a maximum character count for a URL that is
 * displayed as part of a dialog. It's fairly arbitrary -- big enough
 * to allow most "normal" URIs to display in full, but small enough to
 * prevent the dialog from getting insanely wide.
 */
#define MAX_URI_IN_DIALOG_LENGTH 60


57 58 59 60 61 62 63
#ifdef HAVE_STARTUP_NOTIFICATION
#define SN_API_NOT_YET_FROZEN
#include <libsn/sn.h>
#include <gdk/gdk.h>
#include <gdk/gdkx.h>
#endif

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
extern char **environ;

/* Cut and paste from gdkspawn-x11.c */
static gchar **
my_gdk_spawn_make_environment_for_screen (GdkScreen  *screen,
					  gchar     **envp)
{
  gchar **retval = NULL;
  gchar  *display_name;
  gint    display_index = -1;
  gint    i, env_len;

  g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);

  if (envp == NULL)
    envp = environ;

  for (env_len = 0; envp[env_len]; env_len++)
    if (strncmp (envp[env_len], "DISPLAY", strlen ("DISPLAY")) == 0)
      display_index = env_len;

  retval = g_new (char *, env_len + 1);
  retval[env_len] = NULL;

  display_name = gdk_screen_make_display_name (screen);

  for (i = 0; i < env_len; i++)
    if (i == display_index)
      retval[i] = g_strconcat ("DISPLAY=", display_name, NULL);
    else
      retval[i] = g_strdup (envp[i]);

  g_assert (i == env_len);

  g_free (display_name);

  return retval;
}


104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
/**
 * application_cannot_open_location
 * 
 * Handle the case where an application has been selected to be launched,
 * and it cannot handle the current uri scheme.  This can happen
 * because the default application for a file type may not be able
 * to handle some kinds of locations.   We want to tell users that their
 * default application doesn't work here, rather than switching off to
 * a different one without them noticing.
 * 
 * @application: The application that was to be launched.
 * @file: The file whose location was passed as a parameter to the application
 * @parent_window: A window to use as the parent for any error dialogs.
 *  */
static void
application_cannot_open_location (GnomeVFSMimeApplication *application,
				  NautilusFile *file,
				  const char *uri_scheme,
				  GtkWindow *parent_window)
{
124
#if NEW_MIME_COMPLETE
125
	GtkDialog *message_dialog;
126
	LaunchParameters *launch_parameters;
127
	char *prompt;
128 129
	char *message;
	char *file_name;
130
	int response;
131

132
	file_name = nautilus_file_get_display_name (file);
133

134
	if (nautilus_mime_has_any_applications_for_file (file)) {
135
		if (application != NULL) {
136
			prompt = _("Open Failed, would you like to choose another application?");
137
			message = g_strdup_printf (_("\"%s\" can't open \"%s\" because \"%s\" can't access files at \"%s\" "
138
						     "locations."),
139 140 141
						   application->name, file_name, 
						   application->name, uri_scheme);
		} else {
142
			prompt = _("Open Failed, would you like to choose another action?");
143
			message = g_strdup_printf (_("The default action can't open \"%s\" because it can't access files at \"%s\" "
144
						     "locations."),
145 146 147
						   file_name, uri_scheme);
		}
		
148 149
		message_dialog = eel_show_yes_no_dialog (prompt, 
		                                         message,
150
							 _("Can't Open Location"), 
151 152
							 GTK_STOCK_OK,
							 GTK_STOCK_CANCEL,
153
							 parent_window);
154 155
		response = gtk_dialog_run (message_dialog);
		gtk_object_destroy (GTK_OBJECT (message_dialog));
156
		
157
		if (response == GTK_RESPONSE_YES) {
158 159 160 161 162 163 164 165
			launch_parameters = launch_parameters_new (file, parent_window);
			nautilus_choose_application_for_file 
				(file,
				 parent_window,
				 launch_application_callback,
				 launch_parameters);
				 
		}
166
		g_free (message);
167 168
	} else {
		if (application != NULL) {
169 170 171 172 173 174
			prompt = g_strdup_printf (_("\"%s\" can't open \"%s\" because \"%s\" can't access files at \"%s\"."
						    "locations."), application->name, file_name, 
						    application->name, uri_scheme);
			message = _("No other applications are available to view this file.  "
				    "If you copy this file onto your computer, you may be able to open "
				    "it.");
175
		} else {
176 177 178 179 180
			prompt = g_strdup_printf (_("The default action can't open \"%s\" because it can't access files at \"%s\"."
						    "locations."), file_name, uri_scheme);
     			message = _("No other actions are available to view this file.  "
				    "If you copy this file onto your computer, you may be able to open "
				    "it.");
181 182
		}
				
183 184
		eel_show_info_dialog (prompt, message, _("Can't Open Location"), parent_window);
		g_free (prompt);
185
	}	
186 187

	g_free (file_name);
188
#endif
189 190
}

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
#ifdef HAVE_STARTUP_NOTIFICATION
static void
sn_error_trap_push (SnDisplay *display,
		    Display   *xdisplay)
{
	gdk_error_trap_push ();
}

static void
sn_error_trap_pop (SnDisplay *display,
		   Display   *xdisplay)
{
	gdk_error_trap_pop ();
}

extern char **environ;

static char **
make_spawn_environment_for_sn_context (SnLauncherContext *sn_context,
				       char             **envp)
{
	char **retval;
	int    i, j;

	retval = NULL;
	
	if (envp == NULL) {
		envp = environ;
	}
	
	for (i = 0; envp[i]; i++) {
		/* Count length */
	}

	retval = g_new (char *, i + 2);

	for (i = 0, j = 0; envp[i]; i++) {
		if (!g_str_has_prefix (envp[i], "DESKTOP_STARTUP_ID=")) {
			retval[j] = g_strdup (envp[i]);
			++j;
	        }
	}

	retval[j] = g_strdup_printf ("DESKTOP_STARTUP_ID=%s",
				     sn_launcher_context_get_startup_id (sn_context));
	++j;
	retval[j] = NULL;

	return retval;
}

/* This should be fairly long, as it's confusing to users if a startup
 * ends when it shouldn't (it appears that the startup failed, and
 * they have to relaunch the app). Also the timeout only matters when
 * there are bugs and apps don't end their own startup sequence.
 *
 * This timeout is a "last resort" timeout that ignores whether the
 * startup sequence has shown activity or not.  Metacity and the
 * tasklist have smarter, and correspondingly able-to-be-shorter
 * timeouts. The reason our timeout is dumb is that we don't monitor
 * the sequence (don't use an SnMonitorContext)
 */
#define STARTUP_TIMEOUT_LENGTH (30 /* seconds */ * 1000)

typedef struct
{
	GdkScreen *screen;
	GSList *contexts;
	guint timeout_id;
} StartupTimeoutData;

static void
free_startup_timeout (void *data)
{
	StartupTimeoutData *std;

	std = data;

	g_slist_foreach (std->contexts,
			 (GFunc) sn_launcher_context_unref,
			 NULL);
	g_slist_free (std->contexts);

	if (std->timeout_id != 0) {
		g_source_remove (std->timeout_id);
		std->timeout_id = 0;
	}

	g_free (std);
}

static gboolean
startup_timeout (void *data)
{
	StartupTimeoutData *std;
	GSList *tmp;
	GTimeVal now;
	int min_timeout;

	std = data;

	min_timeout = STARTUP_TIMEOUT_LENGTH;
	
	g_get_current_time (&now);
	
	tmp = std->contexts;
	while (tmp != NULL) {
		SnLauncherContext *sn_context;
		GSList *next;
		long tv_sec, tv_usec;
		double elapsed;
		
		sn_context = tmp->data;
		next = tmp->next;
		
		sn_launcher_context_get_last_active_time (sn_context,
							  &tv_sec, &tv_usec);

		elapsed =
			((((double)now.tv_sec - tv_sec) * G_USEC_PER_SEC +
			  (now.tv_usec - tv_usec))) / 1000.0;

		if (elapsed >= STARTUP_TIMEOUT_LENGTH) {
			std->contexts = g_slist_remove (std->contexts,
							sn_context);
			sn_launcher_context_complete (sn_context);
			sn_launcher_context_unref (sn_context);
		} else {
			min_timeout = MIN (min_timeout, (STARTUP_TIMEOUT_LENGTH - elapsed));
		}
		
		tmp = next;
	}

	if (std->contexts == NULL) {
		std->timeout_id = 0;
	} else {
		std->timeout_id = g_timeout_add (min_timeout,
						 startup_timeout,
						 std);
	}

	/* always remove this one, but we may have reinstalled another one. */
	return FALSE;
}

static void
add_startup_timeout (GdkScreen         *screen,
		     SnLauncherContext *sn_context)
{
	StartupTimeoutData *data;

	data = g_object_get_data (G_OBJECT (screen), "nautilus-startup-data");
	if (data == NULL) {
		data = g_new (StartupTimeoutData, 1);
		data->screen = screen;
		data->contexts = NULL;
		data->timeout_id = 0;
		
		g_object_set_data_full (G_OBJECT (screen), "nautilus-startup-data",
					data, free_startup_timeout);		
	}

	sn_launcher_context_ref (sn_context);
	data->contexts = g_slist_prepend (data->contexts, sn_context);
	
	if (data->timeout_id == 0) {
		data->timeout_id = g_timeout_add (STARTUP_TIMEOUT_LENGTH,
						  startup_timeout,
						  data);		
	}
}
363 364 365 366 367 368

/* FIXME: This is the wrong way to do this; there should be some event
 * (e.g. button press) available with a good time.  A function like
 * this should not be needed.
 */
static Time
369
slowly_and_stupidly_obtain_timestamp (Display *xdisplay)
370
{
Alexander Larsson's avatar
Alexander Larsson committed
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
	Window xwindow;
	XEvent event;
	
	{
		XSetWindowAttributes attrs;
		Atom atom_name;
		Atom atom_type;
		char* name;
		
		attrs.override_redirect = True;
		attrs.event_mask = PropertyChangeMask | StructureNotifyMask;
		
		xwindow =
			XCreateWindow (xdisplay,
				       RootWindow (xdisplay, 0),
				       -100, -100, 1, 1,
				       0,
				       CopyFromParent,
				       CopyFromParent,
390
				       (Visual *)CopyFromParent,
Alexander Larsson's avatar
Alexander Larsson committed
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
				       CWOverrideRedirect | CWEventMask,
				       &attrs);
		
		atom_name = XInternAtom (xdisplay, "WM_NAME", TRUE);
		g_assert (atom_name != None);
		atom_type = XInternAtom (xdisplay, "STRING", TRUE);
		g_assert (atom_type != None);
		
		name = "Fake Window";
		XChangeProperty (xdisplay, 
				 xwindow, atom_name,
				 atom_type,
				 8, PropModeReplace, name, strlen (name));
	}
	
	XWindowEvent (xdisplay,
		      xwindow,
		      PropertyChangeMask,
		      &event);
	
	XDestroyWindow(xdisplay, xwindow);
	
	return event.xproperty.time;
414
}
415 416 417 418 419 420
#endif /* HAVE_STARTUP_NOTIFICATION */





421 422 423 424 425 426 427 428 429 430 431 432
/**
 * nautilus_launch_show_file:
 *
 * Shows a file using gnome_url_show.
 *
 * @file: the file whose uri will be shown.
 * @parent_window: window to use as parent for error dialog.
 */
void nautilus_launch_show_file (NautilusFile *file,
                                GtkWindow    *parent_window)
{
	GnomeVFSResult result;
433
	GnomeVFSMimeApplication *application;
434 435 436
	GdkScreen *screen;
	char **envp;
	char *uri, *uri_scheme;
437 438 439 440
	char *error_message, *detail_message;
        char *full_uri_for_display;
        char *uri_for_display;
	GnomeVFSURI *vfs_uri;
441 442 443 444 445 446 447
#ifdef HAVE_STARTUP_NOTIFICATION
	SnLauncherContext *sn_context;
	SnDisplay *sn_display;
	gboolean startup_notify;

	startup_notify = FALSE;
#endif
448

449 450
	g_return_if_fail (!nautilus_file_needs_slow_mime_type (file));

451 452 453 454 455 456 457 458 459
	uri = NULL;
	if (nautilus_file_is_nautilus_link (file)) {
		uri = nautilus_file_get_activation_uri (file);
	}
	
	if (uri == NULL) {
		uri = nautilus_file_get_uri (file);
	}
		
460
	application = nautilus_mime_get_default_application_for_file (file);
461
	
462
	screen = gtk_window_get_screen (parent_window);
463
	envp = my_gdk_spawn_make_environment_for_screen (screen, NULL);
464
	
465 466 467 468 469 470
#ifdef HAVE_STARTUP_NOTIFICATION
	sn_display = sn_display_new (gdk_display,
				     sn_error_trap_push,
				     sn_error_trap_pop);
	
	/* Only initiate notification if application supports it. */
471
	if (application) {
472
		startup_notify = gnome_vfs_mime_application_supports_startup_notification (application);
473 474
	} else {
		startup_notify = FALSE;
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
	}
	
	if (startup_notify == TRUE) {
		char *name;
		char *icon;

		sn_context = sn_launcher_context_new (sn_display,
						      screen ? gdk_screen_get_number (screen) :
						      DefaultScreen (gdk_display));
		
		name = nautilus_file_get_display_name (file);

		if (name != NULL) {
			char *description;
			
			sn_launcher_context_set_name (sn_context, name);
			
			description = g_strdup_printf (_("Opening %s"), name);
			
			sn_launcher_context_set_description (sn_context, description);

			g_free (name);
			g_free (description);
		}

		icon = nautilus_icon_factory_get_icon_for_file (file, FALSE);
		if (icon != NULL) {
			sn_launcher_context_set_icon_name (sn_context, icon);
			g_free (icon);
		}
		
		if (!sn_launcher_context_get_initiated (sn_context)) {
			const char *binary_name;
			char **old_envp;
509 510
			Time timestamp;

511
			timestamp = slowly_and_stupidly_obtain_timestamp (GDK_WINDOW_XDISPLAY (GTK_WIDGET (parent_window)->window));
512

513
			binary_name = gnome_vfs_mime_application_get_binary_name (application);
514 515 516 517 518 519 520
		
			sn_launcher_context_set_binary_name (sn_context,
							     binary_name);
			
			sn_launcher_context_initiate (sn_context,
						      g_get_prgname () ? g_get_prgname () : "unknown",
						      binary_name,
521
						      timestamp);
522 523 524 525 526 527 528 529 530 531

			old_envp = envp;
			envp = make_spawn_environment_for_sn_context (sn_context, envp);
			g_strfreev (old_envp);
		}
	} else {
		sn_context = NULL;
	}
#endif /* HAVE_STARTUP_NOTIFICATION */
	
532 533
	result = gnome_vfs_url_show_with_env (uri, envp);

534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
#ifdef HAVE_STARTUP_NOTIFICATION
	if (sn_context != NULL) {
		if (result != GNOME_VFS_OK) {
			sn_launcher_context_complete (sn_context); /* end sequence */
		} else {
			add_startup_timeout (screen ? screen :
					     gdk_display_get_default_screen (gdk_display_get_default ()),
					     sn_context);
		}
		sn_launcher_context_unref (sn_context);
	}
	
	sn_display_unref (sn_display);
#endif /* HAVE_STARTUP_NOTIFICATION */
	
549 550 551 552 553 554 555 556 557 558 559
	full_uri_for_display = eel_format_uri_for_display (uri);
	/* Truncate the URI so it doesn't get insanely wide. Note that even
	 * though the dialog uses wrapped text, if the URI doesn't contain
	 * white space then the text-wrapping code is too stupid to wrap it.
	 */
	uri_for_display = eel_str_middle_truncate
		(full_uri_for_display, MAX_URI_IN_DIALOG_LENGTH);
	g_free (full_uri_for_display);
	
	error_message = detail_message = NULL;
	
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
	switch (result) {
	case GNOME_VFS_OK:
		break;
		
	case GNOME_VFS_ERROR_NOT_SUPPORTED:
		uri_scheme = nautilus_file_get_uri_scheme (file);
		application_cannot_open_location (NULL,
						  file,
						  uri_scheme,
						  parent_window);
		g_free (uri_scheme);
		break;
		
	case GNOME_VFS_ERROR_NO_DEFAULT:
	case GNOME_VFS_ERROR_NO_HANDLER:
575
#if NEW_MIME_COMPLETE
576
		nautilus_program_chooser_show_no_choices_message
577
					(action_type, file, parent_window);
578
		break;
579
#endif
580 581 582 583 584 585
		error_message = g_strdup_printf (_("Couldn't display \"%s\"."),
						 uri_for_display);
		/* TODO: This really needs to be something saying "no app
		 * handling this file type", but there is a string freeze. */
		detail_message = g_strdup ("");
		break;
586
		
587 588 589 590 591 592 593 594
	case GNOME_VFS_ERROR_LAUNCH:
		/* TODO: These strings suck pretty badly, but we're in string-freeze,
		 * and I found these in other places to reuse. We should make them
		 * better later. */
		error_message = g_strdup_printf (_("Couldn't display \"%s\"."),
						 uri_for_display);
		detail_message = g_strdup (_("There was an error launching the application."));		
		break;
595
	default:
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632

		switch (nautilus_file_get_file_info_result (file)) {
		case GNOME_VFS_ERROR_ACCESS_DENIED:
			error_message = g_strdup_printf (_("Couldn't display \"%s\"."),
							 uri_for_display);
			detail_message = g_strdup (_("The attempt to log in failed."));		
			break;
		case GNOME_VFS_ERROR_NOT_PERMITTED:
			error_message = g_strdup_printf (_("Couldn't display \"%s\"."),
							 uri_for_display);
			detail_message = g_strdup (_("Access was denied."));		
			break;
		case GNOME_VFS_ERROR_INVALID_HOST_NAME:
		case GNOME_VFS_ERROR_HOST_NOT_FOUND:
			vfs_uri = gnome_vfs_uri_new (uri);
			error_message = g_strdup_printf (_("Couldn't display \"%s\", because no host \"%s\" could be found."),
							 uri_for_display,
							 gnome_vfs_uri_get_host_name (vfs_uri));
			detail_message = g_strdup (_("Check that the spelling is correct and that your proxy settings are correct."));
			gnome_vfs_uri_unref (vfs_uri);
			break;
		case GNOME_VFS_ERROR_INVALID_URI:
			error_message = g_strdup_printf
				(_("\"%s\" is not a valid location."),
				 uri_for_display);
			detail_message = g_strdup 
				(_("Please check the spelling and try again."));
			break;
		case GNOME_VFS_ERROR_NOT_FOUND:
			error_message = g_strdup_printf
				(_("Couldn't find \"%s\"."), 
				 uri_for_display);
			detail_message = g_strdup 
				(_("Please check the spelling and try again."));
			break;
		case GNOME_VFS_OK:
		default:
633
#if NEW_MIME_COMPLETE
634 635
			nautilus_program_chooser_show_invalid_message
				(action_type, file, parent_window);
636 637
#endif
			break;
638 639 640
		}

		
641
	}
642 643 644 645 646 647 648 649 650

	if (error_message != NULL) {
		eel_show_error_dialog (error_message, detail_message, _("Can't Display Location"), parent_window);
		
		g_free (error_message);
		g_free (detail_message);
	}
	
	g_free (uri_for_display);
651
	
652 653
	if (application != NULL) 
		gnome_vfs_mime_application_free (application);
654

655 656 657 658
	g_strfreev (envp);
	g_free (uri);
}

659
/**
660
 * nautilus_launch_application:
661
 * 
662 663
 * Fork off a process to launch an application with a given file as a
 * parameter. Provide a parent window for error dialogs. 
664
 * 
665
 * @application: The application to be launched.
666
 * @file: The file whose location should be passed as a parameter to the application
667
 * @parent_window: A window to use as the parent for any error dialogs.
668 669
 */
void
670
nautilus_launch_application (GnomeVFSMimeApplication *application, 
671
			     NautilusFile *file,
672
			     GtkWindow *parent_window)
673
{
674 675 676 677 678 679
	GdkScreen       *screen;
	char		*uri;
	char            *uri_scheme;
	GList            uris;
	char           **envp;
	GnomeVFSResult   result;
680 681 682 683
#ifdef HAVE_STARTUP_NOTIFICATION
	SnLauncherContext *sn_context;
	SnDisplay *sn_display;
#endif
684 685 686 687 688 689 690

	uri = NULL;
	if (nautilus_file_is_nautilus_link (file)) {
		uri = nautilus_file_get_activation_uri (file);
	}
	
	if (uri == NULL) {
691
		uri = nautilus_file_get_uri (file);
692
	}
693

694 695 696 697
	uris.next = NULL;
	uris.prev = NULL;
	uris.data = uri;
	
698
	screen = gtk_window_get_screen (parent_window);
699
	envp = my_gdk_spawn_make_environment_for_screen (screen, NULL);
700
	
701 702 703 704 705 706 707
#ifdef HAVE_STARTUP_NOTIFICATION
	sn_display = sn_display_new (gdk_display,
				     sn_error_trap_push,
				     sn_error_trap_pop);

	
	/* Only initiate notification if application supports it. */
708 709
	if (gnome_vfs_mime_application_supports_startup_notification (application))
	{ 
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
		char *name;
		char *icon;

		sn_context = sn_launcher_context_new (sn_display,
						      screen ? gdk_screen_get_number (screen) :
						      DefaultScreen (gdk_display));
		
		name = nautilus_file_get_display_name (file);
		if (name != NULL) {
			char *description;
			
			sn_launcher_context_set_name (sn_context, name);
			
			description = g_strdup_printf (_("Opening %s"), name);
			
			sn_launcher_context_set_description (sn_context, description);

			g_free (name);
			g_free (description);
		}

		icon = nautilus_icon_factory_get_icon_for_file (file, FALSE);
		if (icon != NULL) {
			sn_launcher_context_set_icon_name (sn_context, icon);
			g_free (icon);
		}
		
		if (!sn_launcher_context_get_initiated (sn_context)) {
			const char *binary_name;
			char **old_envp;
740 741
			Time timestamp;

742
			timestamp = slowly_and_stupidly_obtain_timestamp (GDK_WINDOW_XDISPLAY (GTK_WIDGET (parent_window)->window));
743
			
744
			binary_name = gnome_vfs_mime_application_get_binary_name (application);
745 746 747 748 749 750 751
		
			sn_launcher_context_set_binary_name (sn_context,
							     binary_name);
			
			sn_launcher_context_initiate (sn_context,
						      g_get_prgname () ? g_get_prgname () : "unknown",
						      binary_name,
752
						      timestamp);
753 754 755 756 757 758 759 760 761 762

			old_envp = envp;
			envp = make_spawn_environment_for_sn_context (sn_context, envp);
			g_strfreev (old_envp);
		}
	} else {
		sn_context = NULL;
	}
#endif /* HAVE_STARTUP_NOTIFICATION */
	
763 764
	result = gnome_vfs_mime_application_launch_with_env (application, &uris, envp);

765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
#ifdef HAVE_STARTUP_NOTIFICATION
	if (sn_context != NULL) {
		if (result != GNOME_VFS_OK) {
			sn_launcher_context_complete (sn_context); /* end sequence */
		} else {
			add_startup_timeout (screen ? screen :
					     gdk_display_get_default_screen (gdk_display_get_default ()),
					     sn_context);
		}
		sn_launcher_context_unref (sn_context);
	}
	
	sn_display_unref (sn_display);
#endif /* HAVE_STARTUP_NOTIFICATION */

780 781 782 783 784 785 786 787 788 789 790 791 792
	switch (result) {
	case GNOME_VFS_OK:
		break;

	case GNOME_VFS_ERROR_NOT_SUPPORTED:
		uri_scheme = nautilus_file_get_uri_scheme (file);
		application_cannot_open_location (application,
						  file,
						  uri_scheme,
						  parent_window);
		g_free (uri_scheme);
		
		break;
793

794
	default:
795
#if NEW_MIME_COMPLETE
796 797 798
		nautilus_program_chooser_show_invalid_message
			(GNOME_VFS_MIME_ACTION_TYPE_APPLICATION, file, parent_window);
			 
799
#endif
800 801 802 803 804
		break;
	}
	
	g_free (uri);
	g_strfreev (envp);
805 806 807
}

/**
808
 * nautilus_launch_application_from_command:
809 810 811 812
 * 
 * Fork off a process to launch an application with a given uri as
 * a parameter.
 * 
813 814
 * @command_string: The application to be launched, with any desired
 * command-line options.
815
 * @parameter: Passed as a parameter to the application as is.
816 817
 */
void
818 819
nautilus_launch_application_from_command (GdkScreen  *screen,
					  const char *name,
820
					  const char *command_string, 
821
					  const char *parameter, 
822
					  gboolean use_terminal)
823
{
824 825
	char *full_command;
	char *quoted_parameter; 
826

827
	if (parameter != NULL) {
828
		quoted_parameter = g_shell_quote (parameter);
829
		full_command = g_strconcat (command_string, " ", quoted_parameter, NULL);
830
		g_free (quoted_parameter);
831
	} else {
832
		full_command = g_strdup (command_string);
833 834
	}

835
	if (use_terminal) {
836
		eel_gnome_open_terminal_on_screen (full_command, screen);
837
	} else {
838
	    	eel_gnome_shell_execute_on_screen (full_command, screen);
839 840
	}

841
	g_free (full_command);
842
}
843 844

void
845 846 847 848
nautilus_launch_desktop_file (GdkScreen   *screen,
			      const char  *desktop_file_uri,
			      const GList *parameter_uris,
			      GtkWindow   *parent_window)
849 850 851
{
	GError *error;
	GnomeDesktopItem *ditem;
852
	GnomeDesktopItemLaunchFlags flags;
853 854 855 856
	const char *command_string;
	char *local_path, *message;
	const GList *p;
	int total, count;
857
	char **envp;
858
#ifdef HAVE_STARTUP_NOTIFICATION
859
	Time timestamp;
860
#endif
861 862 863 864 865 866 867 868 869 870 871

	/* strip the leading command specifier */
	if (eel_str_has_prefix (desktop_file_uri, NAUTILUS_DESKTOP_COMMAND_SPECIFIER)) {
		desktop_file_uri += strlen (NAUTILUS_DESKTOP_COMMAND_SPECIFIER);
	}

	/* Don't allow command execution from remote locations where the
	 * uri scheme isn't file:// (This is because files on for example
	 * nfs are treated as remote) to partially mitigate the security
	 * risk of executing arbitrary commands.
	 */
872 873
	if (!eel_vfs_has_capability (desktop_file_uri,
				     EEL_VFS_CAPABILITY_SAFE_TO_EXECUTE)) {
874 875
		eel_show_error_dialog
			(_("Sorry, but you can't execute commands from "
876 877 878
			   "a remote site."), 
			 _("This is disabled due to security considerations."),
			 _("Can't Execute Remote Links"),
879
			 parent_window);
880
			 
881 882 883 884
		return;
	}
	
	error = NULL;
885
	ditem = gnome_desktop_item_new_from_uri (desktop_file_uri, 0,
886 887
						&error);	
	if (error != NULL) {
888
		message = g_strconcat (_("Details: "), error->message, NULL);
889
		eel_show_error_dialog
890 891 892
			(_("There was an error launching the application."),
			 message,
			 _("Error Launching Application"),
893 894 895 896 897 898 899
			 parent_window);			
			 
		g_error_free (error);
		g_free (message);
		return;
	}
	
900 901 902 903 904 905 906 907 908 909 910
	/* count the number of uris with local paths */
	count = 0;
	total = g_list_length ((GList *) parameter_uris);
	for (p = parameter_uris; p != NULL; p = p->next) {
		local_path = gnome_vfs_get_local_path_from_uri ((const char *) p->data);
		if (local_path != NULL) {
			g_free (local_path);
			count++;
		}
	}

911 912 913 914 915 916 917 918 919
	/* check if this app only supports local files */
	command_string = gnome_desktop_item_get_string (ditem, GNOME_DESKTOP_ITEM_EXEC);
	if ((strstr (command_string, "%F") || strstr (command_string, "%f"))
		&& !(strstr (command_string, "%U") || strstr (command_string, "%u"))
		&& parameter_uris != NULL) {
	
		if (count == 0) {
			/* all files are non-local */
			eel_show_error_dialog
920 921
				(_("This drop target only supports local files."),
				 _("To open non-local files copy them to a local folder and then"
922
				   " drop them again."),
923
				 _("Drop Target Only Supports Local Files"),
924 925 926 927 928 929 930 931
				 parent_window);
			
			gnome_desktop_item_unref (ditem);
			return;

		} else if (count != total) {
			/* some files were non-local */
			eel_show_warning_dialog
932 933
				(_("This drop target only supports local files."),
				 _("To open non-local files copy them to a local folder and then"
934
				   " drop them again. The local files you dropped have already been opened."),
935
				 _("Drop Target Only Supports Local Files"),
936 937 938
				 parent_window);
		}		
	}
939

940
	envp = my_gdk_spawn_make_environment_for_screen (screen, NULL);
941
	
942 943 944 945 946 947 948
	/* we append local paths only if all parameters are local */
	if (count == total) {
		flags = GNOME_DESKTOP_ITEM_LAUNCH_APPEND_PATHS;
	} else {
		flags = GNOME_DESKTOP_ITEM_LAUNCH_APPEND_URIS;
	}

949
	error = NULL;
950

951
#ifdef HAVE_STARTUP_NOTIFICATION
952 953
	timestamp = slowly_and_stupidly_obtain_timestamp (GDK_WINDOW_XDISPLAY (GTK_WIDGET (parent_window)->window));
	gnome_desktop_item_set_launch_time (ditem, timestamp);
954
#endif
955 956 957
	gnome_desktop_item_launch_with_env (ditem, (GList *) parameter_uris,
					    flags, envp,
					    &error);
958
	if (error != NULL) {
959
		message = g_strconcat (_("Details: "), error->message, NULL);
960
		eel_show_error_dialog
961 962 963
			(_("There was an error launching the application."),
			 message,
			 _("Error Launching Application"),
964 965 966 967 968 969 970
			 parent_window);			
			 
		g_error_free (error);
		g_free (message);
	}
	
	gnome_desktop_item_unref (ditem);
971
	g_strfreev (envp);
972
}