[tomboy] Use GNOME Session d-bus API instead of Gnome.Client for session management



commit 481f1e4d0985933c148cd8adbe3555bfef36071d
Author: Aaron Borden <adborden live com>
Date:   Fri Jun 4 20:23:26 2010 -0700

    Use GNOME Session d-bus API instead of Gnome.Client for session management
    
    This is part of the cleanup to remove usage of obsolete libgnome APIs.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=580422

 Tomboy.mdp                 |    3 +
 Tomboy/GnomeApplication.cs |   98 ++++++++++++++++++++++++--------------------
 Tomboy/GnomeSession.cs     |   64 ++++++++++++++++++++++++++++
 Tomboy/Makefile.am         |    1 +
 Tomboy/Tomboy.cs           |    2 +-
 5 files changed, 123 insertions(+), 45 deletions(-)
---
diff --git a/Tomboy.mdp b/Tomboy.mdp
index e1e2134..4b0041c 100644
--- a/Tomboy.mdp
+++ b/Tomboy.mdp
@@ -140,6 +140,7 @@
     <File name="Tomboy/Services.cs" subtype="Code" buildaction="Compile" />
     <File name="Tomboy/GConfPreferencesClient.cs" subtype="Code" buildaction="Compile" />
     <File name="Tomboy/GnomeApplication.cs" subtype="Code" buildaction="Compile" />
+    <File name="Tomboy/GnomeSession.cs" subtype="Code" buildaction="Compile" />
     <File name="Tomboy/Keybinder.cs" subtype="Code" buildaction="Compile" />
     <File name="Tomboy/NativeApplication.cs" subtype="Code" buildaction="Compile" />
     <File name="Tomboy/PlatformFactory.cs" subtype="Code" buildaction="Compile" />
@@ -207,6 +208,8 @@
     <ProjectReference type="Gac" localcopy="True" refto="Mono.Cairo, Version=2.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756" />
     <ProjectReference type="Gac" localcopy="True" refto="nunit.framework, Version=2.4.8.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77" />
     <ProjectReference type="Gac" localcopy="True" refto="System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
+    <ProjectReference type="Gac" localcopy="True" refto="NDesk.DBus, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f6716e4f9b2ed099" />
+    <ProjectReference type="Gac" localcopy="True" refto="NDesk.DBus.GLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f6716e4f9b2ed099" />
   </References>
   <MonoDevelop.Autotools.MakefileInfo IntegrationEnabled="True" RelativeMakefileName="./Makefile.am" ExecuteTargetName="run">
     <BuildFilesVar />
diff --git a/Tomboy/GnomeApplication.cs b/Tomboy/GnomeApplication.cs
index 5c1a655..44dae3b 100644
--- a/Tomboy/GnomeApplication.cs
+++ b/Tomboy/GnomeApplication.cs
@@ -9,6 +9,9 @@ using Mono.Unix.Native;
 
 using Hyena;
 
+using NDesk.DBus;
+using org.gnome.SessionManager;
+
 namespace Tomboy
 {
 	public class GnomeApplication : INativeApplication
@@ -17,6 +20,7 @@ namespace Tomboy
 		private static string confDir;
 		private static string dataDir;
 		private static string cacheDir;
+		private static ObjectPath session_client_id;
 		private const string tomboyDirName = "tomboy";
 
 		static GnomeApplication ()
@@ -46,42 +50,38 @@ namespace Tomboy
 				SetProcessName (process_name);
 			} catch {} // Ignore exception if fail (not needed to run)
 
+			// Register handler for saving session when logging out of Gnome
+			BusG.Init ();
+			string startup_id = Environment.GetEnvironmentVariable ("DESKTOP_AUTOSTART_ID");
+			if (String.IsNullOrEmpty (startup_id))
+				startup_id = display_name;
+
+			try {
+				SessionManager session = Bus.Session.GetObject<SessionManager> (Constants.SessionManagerInterfaceName,
+				                                                                new ObjectPath (Constants.SessionManagerPath));
+				session_client_id = session.RegisterClient (display_name, startup_id);
+				
+				ClientPrivate client = Bus.Session.GetObject<ClientPrivate> (Constants.SessionManagerInterfaceName,
+				                                                             session_client_id);
+				client.QueryEndSession += OnQueryEndSession;
+				client.EndSession += OnEndSession;
+			} catch (Exception e) {
+				Logger.Debug ("Failed to register with session manager: {0}", e.Message);
+			}
+
 			Gtk.Application.Init ();
 			program = new Gnome.Program (display_name,
 			                             Defines.VERSION,
 			                             Gnome.Modules.UI,
 			                             args);
-
-			// Register handler for saving session when logging out of Gnome
-			Gnome.Client client = Gnome.Global.MasterClient ();
-			client.SaveYourself += OnSaveYourself;
 		}
 
 		public void RegisterSessionManagerRestart (string executable_path,
 		                string[] args,
 		                string[] environment)
 		{
-			if (executable_path == null)
-				return;
-
-			// Restart if we are running when the session ends or at crash...
-			Gnome.Client client = Gnome.Global.MasterClient ();
-			client.RestartStyle =
-			        Gnome.RestartStyle.IfRunning | Gnome.RestartStyle.Immediately;
-			client.Die += OnSessionManagerDie;
-
-			foreach (string env in environment) {
-				string [] split = env.Split (new char [] { '=' }, 2);
-				if (split.Length == 2) {
-					client.SetEnvironment (split[0], split[1]);
-				}
-			}
-
-			// Get the args for session restart...
-			string [] restart_args = new string [args.Length + 1];
-			restart_args [0] = executable_path;
-			args.CopyTo (restart_args, 1);
-			client.SetRestartCommand (restart_args.Length, restart_args);
+			// Nothing to do, we dropped the .desktop file in the autostart
+			// folder which should be enough to handle this in Gnome
 		}
 
 		public void RegisterSignalHandlers ()
@@ -125,25 +125,8 @@ namespace Tomboy
 				        Mono.Unix.Native.Stdlib.GetLastError ());
 		}
 
-		private void OnSessionManagerDie (object sender, EventArgs args)
-		{
-			// Don't let the exit signal run, which would cancel
-			// session management.
-			Gtk.Main.Quit ();
-		}
-
-		private void CancelSessionManagerRestart ()
-		{
-			Gnome.Client client = Gnome.Global.MasterClient ();
-			client.RestartStyle = Gnome.RestartStyle.IfRunning;
-			client.Flush ();
-		}
-
 		private void OnExitSignal (int signal)
 		{
-			// Don't auto-restart after exit/kill.
-			CancelSessionManagerRestart ();
-
 			if (ExitingEvent != null)
 				ExitingEvent (null, new EventArgs ());
 
@@ -151,12 +134,39 @@ namespace Tomboy
 				System.Environment.Exit (0);
 		}
 
-		private void OnSaveYourself (object sender, Gnome.SaveYourselfArgs args)
+		private void OnQueryEndSession (uint flags)
 		{
-			Logger.Log ("Received request for saving session");
+			Logger.Info ("Received end session query");
+
+			// The session might not actually end but it would be nice to start
+			// some cleanup actions like saving notes here
+
+			// Let the session manager know its OK to continue
+			try {
+				ClientPrivate client = Bus.Session.GetObject<ClientPrivate> (Constants.SessionManagerInterfaceName,
+				                                                             session_client_id);
+				client.EndSessionResponse(true, String.Empty);
+			} catch (Exception e) {
+				Logger.Debug("Failed to respond to session manager: {0}", e.Message);
+			}
+		}
+
+		private void OnEndSession (uint flags)
+		{
+			Logger.Info ("Received end session signal");
 
 			if (ExitingEvent != null)
 				ExitingEvent (null, new EventArgs ());
+
+			// Let the session manager know its OK to continue
+			// Ideally we would wait for all the exit events to finish
+			try {
+				ClientPrivate client = Bus.Session.GetObject<ClientPrivate> (Constants.SessionManagerInterfaceName,
+				                                                             session_client_id);
+				client.EndSessionResponse (true, String.Empty);
+			} catch (Exception e) {
+				Logger.Debug ("Failed to respond to session manager: {0}", e.Message);
+			}
 		}
 		
 		public void OpenUrl (string url, Gdk.Screen screen)
diff --git a/Tomboy/GnomeSession.cs b/Tomboy/GnomeSession.cs
new file mode 100644
index 0000000..c38957d
--- /dev/null
+++ b/Tomboy/GnomeSession.cs
@@ -0,0 +1,64 @@
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+// Copyright (c) 2010 Aaron Borden <adborden live com>
+//
+
+using System;
+using NDesk.DBus;
+using org.freedesktop.DBus;
+
+// Gnome Session DBus API
+// http://people.gnome.org/~mccann/gnome-session/docs/gnome-session.html
+// Here we've defined only what's needed to register and respond to the
+// SessionManager
+namespace org.gnome.SessionManager
+{
+	public static class Constants
+	{
+		public const string SessionManagerPath = "/org/gnome/SessionManager";
+		public const string SessionManagerInterfaceName = "org.gnome.SessionManager";
+		public const string ClientPrivateInterfaceName = "org.gnome.SessionManager.ClientPrivate";
+	}
+
+	[Interface (Constants.SessionManagerInterfaceName)]
+	public interface SessionManager
+	{
+		void Setenv (string variable, string val);
+		void InitializationError (string message, bool fatal);
+		ObjectPath RegisterClient (string app_id, string client_startup_id);
+		void UnregisterClient (ObjectPath client_id);
+	}
+
+	public delegate void StopCallback ();
+	public delegate void QueryEndSessionCallback (uint flags);
+	public delegate void EndSessionCallback (uint flags);
+	public delegate void CancelEndSessionCallback ();
+
+	[Interface (Constants.ClientPrivateInterfaceName)]
+	public interface ClientPrivate : Introspectable, Properties
+	{
+		void EndSessionResponse (bool is_ok, string reason);
+
+		event StopCallback Stop;
+		event EndSessionCallback EndSession;
+		event QueryEndSessionCallback QueryEndSession;
+		event CancelEndSessionCallback CancelEndSession;
+	}
+}
diff --git a/Tomboy/Makefile.am b/Tomboy/Makefile.am
index 0897365..6dae9b9 100644
--- a/Tomboy/Makefile.am
+++ b/Tomboy/Makefile.am
@@ -50,6 +50,7 @@ GNOME_CSFILES =					\
 	$(srcdir)/GConfPreferencesClient.cs	\
 	$(srcdir)/GnomeApplication.cs		\
 	$(srcdir)/GnomeFactory.cs		\
+	$(srcdir)/GnomeSession.cs		\
 	$(srcdir)/XKeybinder.cs			\
 	$(srcdir)/gtk-sharp-beans/*.cs
 
diff --git a/Tomboy/Tomboy.cs b/Tomboy/Tomboy.cs
index 136fde1..cd777aa 100644
--- a/Tomboy/Tomboy.cs
+++ b/Tomboy/Tomboy.cs
@@ -78,7 +78,7 @@ namespace Tomboy
 
 			// NOTE: It is important not to use the Preferences
 			//       class before this call.
-			Initialize ("tomboy", "tomboy", "tomboy", args);
+			Initialize ("tomboy", "Tomboy", "tomboy", args);
 
 			// Add private icon dir to search path
 			icon_theme = Gtk.IconTheme.Default;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]