Index: shell/e-shell.c
===================================================================
--- shell/e-shell.c	(revision 34749)
+++ shell/e-shell.c	(working copy)
@@ -1334,7 +1334,7 @@
 }
 
 gboolean
-e_shell_quit(EShell *shell)
+e_shell_can_quit (EShell *shell)
 {
 	EShellPrivate *priv;
 	GSList *component_infos;
@@ -1368,23 +1368,44 @@
 			break;
 	}
 
-	if (can_quit) {
-		GList *p = shell->priv->windows;
+	return can_quit;
+}
 
-		for (; p != NULL; p = p->next) {
-			gtk_widget_set_sensitive (GTK_WIDGET (p->data), FALSE);
-			if (p == shell->priv->windows)
-				e_shell_window_save_defaults (p->data);
-		}
-		can_quit = !es_run_quit(shell);
+gboolean
+e_shell_do_quit (EShell *shell)
+{
+	EShellPrivate *priv;
+	GList *p;
+	gboolean can_quit;
 
-		/* Mark a safe quit by destroying the lock. */
-		e_file_lock_destroy ();
+	g_return_val_if_fail (E_IS_SHELL (shell), FALSE);
+
+	priv = shell->priv;
+
+	if (priv->preparing_to_quit)
+		return FALSE;
+
+	for (p = shell->priv->windows; p != NULL; p = p->next) {
+		gtk_widget_set_sensitive (GTK_WIDGET (p->data), FALSE);
+
+		if (p == shell->priv->windows)
+			e_shell_window_save_defaults (p->data);
 	}
 
+	can_quit = !es_run_quit (shell);
+
+	/* Mark a safe quit by destroying the lock. */
+	e_file_lock_destroy ();
+
 	return can_quit;
 }
 
+gboolean
+e_shell_quit (EShell *shell)
+{
+	return e_shell_can_quit (shell) && e_shell_do_quit (shell);
+}
+
 /**
  * gboolean (*EMainShellFunc) (EShell *shell, EShellWindow *window, gpointer user_data);
  * Function used in @ref e_shell_foreach_shell_window.
Index: shell/e-shell.h
===================================================================
--- shell/e-shell.h	(revision 34749)
+++ shell/e-shell.h	(working copy)
@@ -129,7 +129,9 @@
 			      const char   *type,
 			      EShellWindow *shell_window);
 
-gboolean e_shell_quit (EShell *shell);
+gboolean e_shell_can_quit (EShell *shell);
+gboolean e_shell_do_quit  (EShell *shell);
+gboolean e_shell_quit     (EShell *shell);
 
 const char *e_shell_construct_result_to_string (EShellConstructResult result);
 
Index: shell/ChangeLog
===================================================================
--- shell/ChangeLog	(revision 34749)
+++ shell/ChangeLog	(working copy)
@@ -1,3 +1,15 @@
+2008-01-02  Milan Crha  <mcrha@redhat.com>
+
+	** Fix for bug #219197
+
+	* e-shell.h:
+	* e-shell.c: (e_shell_can_quit), (e_shell_do_quit), (e_shell_quit):
+	Call of e_shell_quit divided into two functions, one to check if quit
+	is acceptable by all components and second to do quit.
+	* main.c: (gnome_master_client_save_yourself_cb),
+	(gnome_master_client_die_cb), (main):
+	Quit application when session dies.
+
 2007-12-10  Srinivasa Ragavan  <sragavan@novell.com>
 
 	** Fix for bug #502501
Index: shell/main.c
===================================================================
--- shell/main.c	(revision 34749)
+++ shell/main.c	(working copy)
@@ -61,6 +61,7 @@
 #include <libgnome/gnome-util.h>
 #include <libgnome/gnome-sound.h>
 #include <libgnomeui/gnome-ui-init.h>
+#include <libgnomeui/gnome-client.h>
 
 #include <bonobo/bonobo-main.h>
 #include <bonobo/bonobo-moniker-util.h>
@@ -549,6 +550,18 @@
 #define setup_segv_redirect() 0
 #endif
 
+static gint
+gnome_master_client_save_yourself_cb (GnomeClient *client, GnomeSaveStyle save_style, gint shutdown, GnomeInteractStyle interact_style, gint fast, gpointer user_data)
+{
+	return !shell || e_shell_can_quit (shell);
+}
+
+static void
+gnome_master_client_die_cb (GnomeClient *client)
+{
+	e_shell_do_quit (shell);
+}
+
 static const GOptionEntry options[] = {
 	{ "component", 'c', 0, G_OPTION_ARG_STRING, &default_component_id,
 	  N_("Start Evolution activating the specified component"), NULL },
@@ -588,6 +601,7 @@
 	gboolean skip_warning_dialog;
 #endif
 	GnomeProgram *program;
+	GnomeClient *master_client;
 	GOptionContext *context;
 	char *filename;
 
@@ -657,6 +671,11 @@
 			g_warning ("Could not set up debugging output file.");
 	}
 
+	master_client = gnome_master_client ();
+
+	g_signal_connect (G_OBJECT (master_client), "save_yourself", G_CALLBACK (gnome_master_client_save_yourself_cb), NULL);
+	g_signal_connect (G_OBJECT (master_client), "die", G_CALLBACK (gnome_master_client_die_cb), NULL);
+
 	glade_init ();
 	e_cursors_init ();
 	e_icon_factory_init ();
