[tasque] Added single instance logic for Windows and restructured accordingly



commit 162eadd4eb3166fdd35e763ecbaded647158133f
Author: Antonius Riha <antoniusriha gmail com>
Date:   Fri Nov 30 17:09:11 2012 +0100

    Added single instance logic for Windows and restructured accordingly

 src/Gtk.Tasque/Gtk.Tasque.csproj                   |    4 +-
 .../{GtkApplication.cs => GtkApplicationBase.cs}   |   85 +++++---------------
 src/Gtk.Tasque/GtkLinuxApplication.cs              |   66 +++++++++++++++
 src/Gtk.Tasque/GtkWinApplication.cs                |   84 +++++++++++++++++++
 src/tasque/Program.cs                              |    4 +-
 5 files changed, 175 insertions(+), 68 deletions(-)
---
diff --git a/src/Gtk.Tasque/Gtk.Tasque.csproj b/src/Gtk.Tasque/Gtk.Tasque.csproj
index 0a43d44..9e66469 100644
--- a/src/Gtk.Tasque/Gtk.Tasque.csproj
+++ b/src/Gtk.Tasque/Gtk.Tasque.csproj
@@ -91,7 +91,6 @@
     <Compile Include="CompletedTaskGroup.cs" />
     <Compile Include="CompletedTaskGroupModel.cs" />
     <Compile Include="DateButton.cs" />
-    <Compile Include="GtkApplication.cs" />
     <Compile Include="NoteDialog.cs" />
     <Compile Include="NoteWidget.cs" />
     <None Include="OSXApplication.cs" />
@@ -108,6 +107,9 @@
     <Compile Include="..\..\build\CommonAssemblyInfo.cs">
       <Link>Properties\CommonAssemblyInfo.cs</Link>
     </Compile>
+    <Compile Include="GtkApplicationBase.cs" />
+    <Compile Include="GtkLinuxApplication.cs" />
+    <Compile Include="GtkWinApplication.cs" />
   </ItemGroup>
   <ItemGroup Condition=" '$(Configuration)' == 'LinuxDebug' Or '$(Configuration)' == 'LinuxRelease' ">
     <Compile Include="RemoteControl.cs" />
diff --git a/src/Gtk.Tasque/GtkApplication.cs b/src/Gtk.Tasque/GtkApplicationBase.cs
similarity index 56%
rename from src/Gtk.Tasque/GtkApplication.cs
rename to src/Gtk.Tasque/GtkApplicationBase.cs
index c7c6681..ba79628 100644
--- a/src/Gtk.Tasque/GtkApplication.cs
+++ b/src/Gtk.Tasque/GtkApplicationBase.cs
@@ -25,35 +25,34 @@
 //      Antonius Riha <antoniusriha gmail com>
 // 
 using System;
+using System.Diagnostics;
 using System.IO;
+using Mono.Unix;
+using Gtk;
 
 namespace Tasque
 {
-	public class GtkApplication : NativeApplication
-	{
-		public GtkApplication ()
+	public abstract class GtkApplicationBase : NativeApplication
+	{		
+		public GtkApplicationBase ()
 		{
-			confDir = Path.Combine (
-				Environment.GetFolderPath (
-				Environment.SpecialFolder.ApplicationData),
-				"tasque");
+			confDir = Path.Combine (Environment.GetFolderPath (
+				Environment.SpecialFolder.ApplicationData), "tasque");
 			if (!Directory.Exists (confDir))
 				Directory.CreateDirectory (confDir);
 		}
+		
+		public override string ConfDir { get { return confDir; } }
 
 		public override void Initialize (string[] args)
 		{
-			Mono.Unix.Catalog.Init ("tasque", Defines.LocaleDir);
-
+			Catalog.Init ("tasque", Defines.LocaleDir);
 			Gtk.Application.Init ();
 			
-			base.Initialize (args);
-
 			// add package icon path to default icon theme search paths
-			Gtk.IconTheme.Default.PrependSearchPath (Defines.IconsDir);
-#if WIN && DEBUG
-			Gtk.IconTheme.Default.PrependSearchPath (Defines.DataDir + "\\icons");
-#endif
+			IconTheme.Default.PrependSearchPath (Defines.IconsDir);
+
+			base.Initialize (args);
 		}
 		
 		public override void StartMainLoop ()
@@ -65,53 +64,16 @@ namespace Tasque
 		{
 			Gtk.Application.Quit ();
 		}
-
-		public override string ConfDir
-		{
-			get
-			{
-				return confDir;
-			}
-		}
-
+		
 		public override void OpenUrl (string url)
 		{
 			try {
-				System.Diagnostics.Process.Start (url);
+				Process.Start (url);
 			} catch (Exception e) {
-				Logger.Error ("Error opening url [{0}]:\n{1}", url, e.ToString ());
+				Trace.TraceError ("Error opening url [{0}]:\n{1}", url, e.ToString ());
 			}
 		}
-
-		protected override bool IsRemoteInstanceRunning ()
-		{
-#if LINUX
-			// Register Tasque RemoteControl
-			try {
-				remoteInstance = RemoteControl.Register ();
-				if (remoteInstance != null) {
-					remoteInstance.RemoteInstanceKnocked = HandleRemoteInstanceKnocked;
-					Logger.Debug ("Tasque remote control active.");
-				} else {
-					// If Tasque is already running, open the tasks window
-					// so the user gets some sort of feedback when they
-					// attempt to run Tasque again.
-					RemoteControl remote = null;
-					try {
-						remote = RemoteControl.GetInstance ();
-						remote.KnockKnock ();
-					} catch {}
-
-					Logger.Debug ("Tasque is already running.  Exiting...");
-					return true;
-				}
-			} catch (Exception e) {
-				Logger.Debug ("Tasque remote control disabled (DBus exception): {0}", e.Message);
-			}
-			return false;
-#endif
-		}
-
+		
 		protected override void ShowMainWindow ()
 		{
 			TaskWindow.ShowWindow ();
@@ -119,21 +81,12 @@ namespace Tasque
 		
 		protected override event EventHandler RemoteInstanceKnocked;
 		
-		void HandleRemoteInstanceKnocked ()
+		protected void OnRemoteInstanceKnocked ()
 		{
 			if (RemoteInstanceKnocked != null)
 				RemoteInstanceKnocked (this, EventArgs.Empty);
 		}
 		
-		protected override void Dispose (bool disposing)
-		{
-			if (disposing)
-				remoteInstance.RemoteInstanceKnocked = null;
-		}
-
 		string confDir;
-#if LINUX
-		RemoteControl remoteInstance;
-#endif
 	}
 }
diff --git a/src/Gtk.Tasque/GtkLinuxApplication.cs b/src/Gtk.Tasque/GtkLinuxApplication.cs
new file mode 100644
index 0000000..5a92c06
--- /dev/null
+++ b/src/Gtk.Tasque/GtkLinuxApplication.cs
@@ -0,0 +1,66 @@
+// 
+// GtkLinuxApplication.cs
+//  
+// Author:
+//       Antonius Riha <antoniusriha gmail com>
+// 
+// Copyright (c) 2012 Antonius Riha
+// 
+// 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.
+#if LINUX
+using System;
+
+namespace Tasque
+{
+	public class GtkLinuxApplication : GtkApplicationBase
+	{
+		protected override void Dispose (bool disposing)
+		{
+			if (disposing && remoteInstance != null)
+				remoteInstance = null;
+			
+			base.Dispose (disposing);
+		}
+		
+		protected override bool IsRemoteInstanceRunning ()
+		{
+			// Register Tasque RemoteControl
+			try {
+				remoteInstance = RemoteControl.Register ();
+				if (remoteInstance != null) {
+					remoteInstance.RemoteInstanceKnocked = OnRemoteInstanceKnocked;
+					Logger.Debug ("Tasque remote control created.");
+				} else {
+					RemoteControl remote = null;
+					try {
+						remote = RemoteControl.GetInstance ();
+						remote.KnockKnock ();
+					} catch {}
+					return true;
+				}
+			} catch (Exception e) {
+				Logger.Debug ("Tasque remote control disabled (DBus exception): {0}", e.Message);
+			}
+			return false;
+		}
+		
+		RemoteControl remoteInstance;
+	}
+}
+#endif
diff --git a/src/Gtk.Tasque/GtkWinApplication.cs b/src/Gtk.Tasque/GtkWinApplication.cs
new file mode 100644
index 0000000..d8b4cc3
--- /dev/null
+++ b/src/Gtk.Tasque/GtkWinApplication.cs
@@ -0,0 +1,84 @@
+// 
+// GtkWinApplication.cs
+//  
+// Author:
+//       Antonius Riha <antoniusriha gmail com>
+// 
+// Copyright (c) 2012 Antonius Riha
+// 
+// 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.
+#if WIN
+using System;
+using System.Threading;
+
+namespace Tasque
+{
+	public class GtkWinApplication : GtkApplicationBase
+	{
+		public override void Initialize (string[] args)
+		{
+			base.Initialize (args);
+#if DEBUG
+			Gtk.IconTheme.Default.PrependSearchPath (Defines.DataDir + "\\icons");
+#endif
+		}
+		
+		protected override void Dispose (bool disposing)
+		{
+			if (disposing && waitHandle != null) {
+				waitHandle.Dispose ();
+				waitHandle = null;
+			}
+			
+			base.Dispose (disposing);
+		}
+		
+		protected override bool IsRemoteInstanceRunning ()
+		{
+			try {
+				waitHandle = EventWaitHandle.OpenExisting(waitHandleName);
+				waitHandle.Set();
+				return true;
+			} catch (WaitHandleCannotBeOpenedException) {
+				waitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, waitHandleName);
+				Logger.Debug ("EventWaitHandle created.");
+				
+				var porter = new Thread(new ThreadStart(WaitForAnotherInstance));
+				porter.Start();
+			}
+			return false;
+		}
+		
+		void WaitForAnotherInstance ()
+		{
+			while (!exiting) {
+				waitHandle.WaitOne ();
+				if (!exiting) {
+					Logger.Info ("Another app instance has just knocked on the door.");
+					OnRemoteInstanceKnocked ();
+				}
+			}
+		}
+		
+		bool exiting;
+		EventWaitHandle waitHandle;
+		readonly string waitHandleName = "Tasque." + Environment.UserName;
+	}
+}
+#endif
diff --git a/src/tasque/Program.cs b/src/tasque/Program.cs
index 0728a6c..ec2f65b 100644
--- a/src/tasque/Program.cs
+++ b/src/tasque/Program.cs
@@ -34,8 +34,10 @@ namespace Tasque
 			INativeApplication nativeApp;
 #if OSX
 			nativeApp = new OSXApplication ();
+#elif WIN32
+			nativeApp = new GtkWinApplication ();
 #else
-			nativeApp = new GtkApplication ();
+			nativeApp = new GtkLinuxApplication ();
 #endif
 			return nativeApp;
 		}



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