nautilus-main.c 12.2 KB
Newer Older
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: 8; c-basic-offset: 8 -*- */
2 3

/*
4
 * Nautilus
5
 *
6 7
 * Copyright (C) 1999, 2000 Red Hat, Inc.
 * Copyright (C) 1999, 2000 Eazel, Inc.
8
 *
9 10 11 12
 * Nautilus is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
13
 *
14 15 16 17
 * Nautilus 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
 * General Public License for more details.
18
 *
19 20 21
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
 *
23
 * Authors: Elliot Lee <sopwith@redhat.com>,
24
 *          Darin Adler <darin@bentspoon.com>,
25
 *          John Sullivan <sullivan@eazel.com>
26 27
 *
 */
28

29
/* nautilus-main.c: Implementation of the routines that drive program lifecycle and main window creation/destruction. */
30

31
#include <config.h>
32
#include "nautilus-main.h"
33

34
#include "nautilus-application.h"
35
#include "nautilus-self-check-functions.h"
36
#include "nautilus-window.h"
37
#include <bonobo-activation/bonobo-activation.h>
38
#include <bonobo/bonobo-main.h>
39
#include <dlfcn.h>
40 41 42 43
#include <eel/eel-debug.h>
#include <eel/eel-glib-extensions.h>
#include <eel/eel-self-checks.h>
#include <gdk/gdkx.h>
44
#include <gtk/gtkmain.h>
Alexander Larsson's avatar
Alexander Larsson committed
45
#include <gtk/gtkiconfactory.h>
46
#include <gtk/gtksignal.h>
47
#include <gdk/gdk.h>
48
#include <glib/gi18n.h>
49
#include <libgnome/gnome-init.h>
50
#include <libgnomeui/gnome-ui-init.h>
51
#include <libgnomevfs/gnome-vfs-init.h>
52
#include <libnautilus-private/nautilus-directory-metafile.h>
53 54
#include <libnautilus-private/nautilus-global-preferences.h>
#include <libnautilus-private/nautilus-lib-self-check-functions.h>
55
#include <libxml/parser.h>
56 57 58
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif
59
#include <stdlib.h>
60
#include <string.h>
61
#include <unistd.h>
62

63
/* Keeps track of everyone who wants the main event loop kept active */
64
static GSList *event_loop_registrants;
65 66

static gboolean
67
is_event_loop_needed (void)
68
{
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
	return event_loop_registrants != NULL;
}

static int
quit_if_in_main_loop (gpointer callback_data)
{
	guint level;

	g_assert (callback_data == NULL);

	level = gtk_main_level ();

	/* We can be called even outside the main loop by gnome_vfs_shutdown,
	 * so check that we are in a loop before calling quit.
	 */
	if (level != 0) {
		gtk_main_quit ();
	}

	/* We need to be called again if we quit a nested loop. */
	return level > 1;
90 91 92
}

static void
Ramiro Estrugo's avatar
Ramiro Estrugo committed
93
eel_gtk_main_quit_all (void)
94
{
95 96 97 98
	/* Calling gtk_main_quit directly only kills the current/top event loop.
	 * This idler will be run by the current event loop, killing it, and then
	 * by the next event loop, ...
	 */
99
	g_idle_add (quit_if_in_main_loop, NULL);
100 101 102
}

static void
103
event_loop_unregister (GtkObject *object)
104 105 106
{
	event_loop_registrants = g_slist_remove (event_loop_registrants, object);
	if (!is_event_loop_needed ()) {
Ramiro Estrugo's avatar
Ramiro Estrugo committed
107
		eel_gtk_main_quit_all ();
108 109 110
	}
}

111 112 113 114 115 116 117 118 119
static gboolean
initial_event_loop_needed (gpointer data)
{
	if (!is_event_loop_needed ()) {
		eel_gtk_main_quit_all ();
	}
	return FALSE;
}

120
void
121
nautilus_main_event_loop_register (GtkObject *object)
122
{
123
	g_signal_connect (object, "destroy", G_CALLBACK (event_loop_unregister), NULL);
124
	event_loop_registrants = g_slist_prepend (event_loop_registrants, object);
125 126 127
}

gboolean
128
nautilus_main_is_event_loop_mainstay (GtkObject *object)
129
{
130 131
	return g_slist_length (event_loop_registrants) == 1
		&& event_loop_registrants->data == object;
132 133 134 135 136
}

void
nautilus_main_event_loop_quit (void)
{
137 138
	while (event_loop_registrants != NULL) {
		gtk_object_destroy (event_loop_registrants->data);
139 140 141
	}
}

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
/* Copied from libnautilus/nautilus-program-choosing.c; In this case,
 * though, it's really needed because we have no real alternative when
 * no DESKTOP_STARTUP_ID (with its accompanying timestamp) is
 * provided...
 */
static Time
slowly_and_stupidly_obtain_timestamp (Display *xdisplay)
{
	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,
169
				       (Visual *)CopyFromParent,
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
				       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;
}

195
int
196
main (int argc, char *argv[])
Elliot Lee's avatar
Elliot Lee committed
197
{
198
	gboolean kill_shell;
199
	gboolean restart_shell;
200
	gboolean no_default_window;
Alexander Larsson's avatar
Alexander Larsson committed
201
	gboolean browser_window;
202
	gboolean no_desktop;
203 204
	const char *startup_id;
	char *startup_id_copy;
205
	char *session_to_load;
Alexander Larsson's avatar
Alexander Larsson committed
206 207
	gchar *geometry;
	const gchar **remaining;
208
	gboolean perform_self_check;
Alexander Larsson's avatar
Alexander Larsson committed
209
	GOptionContext *context;
210
	NautilusApplication *application;
211
	char **argv_copy;
212
	GnomeProgram *program;
Alexander Larsson's avatar
Alexander Larsson committed
213 214
	
	const GOptionEntry options[] = {
215
#ifndef NAUTILUS_OMIT_SELF_CHECK
Alexander Larsson's avatar
Alexander Larsson committed
216
		{ "check", 'c', 0, G_OPTION_ARG_NONE, &perform_self_check, 
217
		  N_("Perform a quick set of self-check tests."), NULL },
218
#endif
Alexander Larsson's avatar
Alexander Larsson committed
219
		{ "geometry", 'g', 0, G_OPTION_ARG_STRING, &geometry,
220
		  N_("Create the initial window with the given geometry."), N_("GEOMETRY") },
Alexander Larsson's avatar
Alexander Larsson committed
221
		{ "no-default-window", 'n', 0, G_OPTION_ARG_NONE, &no_default_window,
222
		  N_("Only create windows for explicitly specified URIs."), NULL },
Alexander Larsson's avatar
Alexander Larsson committed
223
		{ "no-desktop", '\0', 0, G_OPTION_ARG_NONE, &no_desktop,
224
		  N_("Do not manage the desktop (ignore the preference set in the preferences dialog)."), NULL },
Alexander Larsson's avatar
Alexander Larsson committed
225
		{ "browser", '\0', 0, G_OPTION_ARG_NONE, &browser_window, 
Alexander Larsson's avatar
Alexander Larsson committed
226
		  N_("open a browser window."), NULL },
Alexander Larsson's avatar
Alexander Larsson committed
227
		{ "quit", 'q', 0, G_OPTION_ARG_NONE, &kill_shell, 
228
		  N_("Quit Nautilus."), NULL },
Alexander Larsson's avatar
Alexander Larsson committed
229
		{ "restart", '\0', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &restart_shell,
230
		  N_("Restart Nautilus."), NULL },
Alexander Larsson's avatar
Alexander Larsson committed
231
		{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &remaining, NULL,  N_("[URI...]") },
232 233 234
		{ "load-session", 'l', 0, G_OPTION_ARG_STRING, &session_to_load,
		  /* Translators: --no-default-window is a nautilus command line parameter, don't modify it. */
		  N_("Load a saved session from the specified file. Implies \"--no-default-window\"."), N_("FILENAME") },
235

Alexander Larsson's avatar
Alexander Larsson committed
236 237
		{ NULL }
	};
238

239 240
	setlocale (LC_ALL, "");

Darin Adler's avatar
Darin Adler committed
241
	if (g_getenv ("NAUTILUS_DEBUG") != NULL) {
242
		eel_make_warnings_and_criticals_stop_in_debugger ();
243
	}
244
	
245
	/* Initialize gettext support */
246 247 248
	bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
	textdomain (GETTEXT_PACKAGE);
249

250 251 252 253 254 255 256 257 258 259 260
	startup_id = g_getenv ("DESKTOP_STARTUP_ID");
	startup_id_copy = NULL;
	if (startup_id != NULL && *startup_id != '\0') {
		/* Clear the DESKTOP_STARTUP_ID, but make sure to copy it first */
		startup_id_copy = g_strdup (startup_id);
		putenv ("DESKTOP_STARTUP_ID=");
	}

	/* we'll do it ourselves due to complicated factory setup */
	gtk_window_set_auto_startup_notification (FALSE);

261
	/* Get parameters. */
Alexander Larsson's avatar
Alexander Larsson committed
262
	remaining = NULL;
263
	geometry = NULL;
264
	session_to_load = NULL;
265
	kill_shell = FALSE;
266
	no_default_window = FALSE;
267
	no_desktop = FALSE;
268
	perform_self_check = FALSE;
269
	restart_shell = FALSE;
Alexander Larsson's avatar
Alexander Larsson committed
270
	browser_window = FALSE;
271

272
	g_set_application_name (_("File Manager"));
Alexander Larsson's avatar
Alexander Larsson committed
273 274 275 276
	context = g_option_context_new (_("\n\nBrowse the file system with the file manager"));

	g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE);

277 278 279
	program = gnome_program_init ("nautilus", VERSION,
				      LIBGNOMEUI_MODULE, argc, argv,
				      GNOME_PROGRAM_STANDARD_PROPERTIES,
Alexander Larsson's avatar
Alexander Larsson committed
280
				      GNOME_PARAM_GOPTION_CONTEXT, context,
281 282
				      GNOME_PARAM_HUMAN_READABLE_NAME, _("Nautilus"),
				      NULL);
Alexander Larsson's avatar
Alexander Larsson committed
283

284 285 286 287
	if (session_to_load != NULL) {
		no_default_window = TRUE;
	}

288 289 290 291 292 293 294 295 296
	/* Do this here so that gdk_display is initialized */
	if (startup_id_copy == NULL) {
		/* Create a fake one containing a timestamp that we can use */
		Time timestamp;
		timestamp = slowly_and_stupidly_obtain_timestamp (gdk_display);
		startup_id_copy = g_strdup_printf ("_TIME%lu",
						   timestamp);
	}

297 298
        /* Set default icon for all nautilus windows */
	gtk_window_set_default_icon_name ("gnome-fs-directory");
299 300 301 302 303 304
	
	/* Need to set this to the canonical DISPLAY value, since
	   thats where we're registering per-display components */
	bonobo_activation_set_activation_env_value ("DISPLAY",
						    gdk_display_get_name (gdk_display_get_default()));
	
305

Alexander Larsson's avatar
Alexander Larsson committed
306
	if (perform_self_check && remaining != NULL) {
307 308 309
		/* translators: %s is an option (e.g. --check) */
		fprintf (stderr, _("nautilus: %s cannot be used with URIs.\n"),
			"--check");
310 311
		return EXIT_FAILURE;
	}
312
	if (perform_self_check && (kill_shell || restart_shell)) {
313 314 315
		fprintf (stderr, _("nautilus: --check cannot be used with other options.\n"));
		return EXIT_FAILURE;
	}
Alexander Larsson's avatar
Alexander Larsson committed
316
	if (kill_shell && remaining != NULL) {
317 318
		fprintf (stderr, _("nautilus: %s cannot be used with URIs.\n"),
			"--quit");
319 320
		return EXIT_FAILURE;
	}
Alexander Larsson's avatar
Alexander Larsson committed
321
	if (restart_shell && remaining != NULL) {
322 323
		fprintf (stderr, _("nautilus: %s cannot be used with URIs.\n"),
			"--restart");
324 325
		return EXIT_FAILURE;
	}
Alexander Larsson's avatar
Alexander Larsson committed
326
	if (geometry != NULL && remaining != NULL && remaining[0] != NULL && remaining[1] != NULL) {
327 328 329
		fprintf (stderr, _("nautilus: --geometry cannot be used with more than one URI.\n"));
		return EXIT_FAILURE;
	}
330 331

	/* Initialize the services that we use. */
332
	LIBXML_TEST_VERSION
333

334 335
	/* Initialize preferences. This is needed so that proper 
	 * defaults are available before any preference peeking 
336
	 * happens.
337
	 */
338
	nautilus_global_preferences_init ();
339 340 341 342 343
	if (no_desktop) {
		eel_preferences_set_is_invisible
			(NAUTILUS_PREFERENCES_SHOW_DESKTOP, TRUE);
		eel_preferences_set_is_invisible
			(NAUTILUS_PREFERENCES_DESKTOP_IS_HOME_DIR, TRUE);
344
	}
345 346 347
	
	bonobo_activate (); /* do now since we need it before main loop */

348 349
	application = NULL;
 
350
	/* Do either the self-check or the real work. */
351 352
	if (perform_self_check) {
#ifndef NAUTILUS_OMIT_SELF_CHECK
353
		/* Run the checks (each twice) for nautilus and libnautilus-private. */
354

355
		nautilus_directory_use_self_contained_metafile_factory ();
Ramiro Estrugo's avatar
Ramiro Estrugo committed
356

357 358
		nautilus_run_self_checks ();
		nautilus_run_lib_self_checks ();
Ramiro Estrugo's avatar
Ramiro Estrugo committed
359 360
		eel_exit_if_self_checks_failed ();

361 362
		nautilus_run_self_checks ();
		nautilus_run_lib_self_checks ();
Ramiro Estrugo's avatar
Ramiro Estrugo committed
363
		eel_exit_if_self_checks_failed ();
364
#endif
365
	} else {
366
		/* Run the nautilus application. */
367 368 369
		application = nautilus_application_new ();
		nautilus_application_startup
			(application,
370
			 kill_shell, restart_shell, no_default_window, no_desktop,
371
			 !(kill_shell || restart_shell),
Alexander Larsson's avatar
Alexander Larsson committed
372
			 browser_window,
373
			 startup_id_copy,
374
			 geometry,
375
			 session_to_load,
Alexander Larsson's avatar
Alexander Larsson committed
376
			 remaining);
377 378
		g_free (startup_id_copy);

379 380 381 382 383
		/* The application startup does things in an idle, so
		   we need to check whether the main loop is needed in an idle
		*/
		g_idle_add (initial_event_loop_needed, NULL);
		gtk_main ();
384
	}
385

386 387 388 389 390
	/* This has to be done before gnome_vfs_shutdown, because
 	 * it might call nautilus_file_get_uri() which might call
 	 * gnome_vfs_uri_append_string()
 	 */
 	eel_debug_shut_down ();
Darin Adler's avatar
Darin Adler committed
391

Darin Adler's avatar
Darin Adler committed
392
	gnome_vfs_shutdown ();
393 394 395 396 397 398 399 400

	/* This has to be done after gnome_vfs_shutdown, because shutdown
	 * can call pending completion callbacks which reference application.
	 */
	if (application != NULL) {
		bonobo_object_unref (application);
	}

401 402 403 404
	/* If told to restart, exec() myself again. This is used when
	 * the program is told to restart with CORBA, for example when
	 * an update takes place.
	 */
405

Darin Adler's avatar
Darin Adler committed
406
	if (g_getenv ("_NAUTILUS_RESTART") != NULL) {
407
		g_unsetenv ("_NAUTILUS_RESTART");
408
		
409 410 411 412 413
		/* Might eventually want to copy all the parameters
		 * from argv into the new exec. For now, though, that
		 * would just interfere with the re-creation of
		 * windows based on the window info stored in gconf,
		 * including whether the desktop was started.
414 415 416
		 */
		argv_copy = g_new0 (char *, 2);
		argv_copy[0] = argv[0];
417
		
418
		execvp (argv[0], argv_copy);
419 420
	}

421
	g_object_unref (G_OBJECT (program));
Alexander Larsson's avatar
Alexander Larsson committed
422

423
	return EXIT_SUCCESS;
Elliot Lee's avatar
Elliot Lee committed
424
}