Skip to content

Commit

Permalink
Frontend-GNOME: single application instance support
Browse files Browse the repository at this point in the history
This feature is required since the feature regression of Ubuntu's Messaging Menu
library, see:
https://bugs.launchpad.net/indicator-messages/+bug/1065732

On the other hand it will also aid Smuxi users to not start multiple Smuxi
instances by accident.
  • Loading branch information
meebey committed Apr 23, 2015
1 parent 34ec120 commit 928e4e9
Showing 1 changed file with 56 additions and 7 deletions.
63 changes: 56 additions & 7 deletions src/Frontend-GNOME/Main.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
/*
* $Id$
* $URL$
* $Rev$
* $Author$
* $Date$
*
* Smuxi - Smart MUltipleXed Irc
*
* Copyright (c) 2005-2006 Mirco Bauer <meebey@meebey.net>
* Copyright (c) 2005-2008, 2012-2013, 2015 Mirco Bauer <meebey@meebey.net>
*
* Full GPL License: <http://www.gnu.org/licenses/gpl.txt>
*
Expand All @@ -27,6 +21,10 @@
*/

using System;
using System.Runtime.Remoting;
using System.Reflection;
using Gtk.Extensions;
using Smuxi.Common;

namespace Smuxi.Frontend.Gnome
{
Expand All @@ -35,6 +33,8 @@ public class MainClass
#if LOG4NET
private static readonly log4net.ILog _Logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
#endif
static readonly string LibraryTextDomain = "smuxi-frontend-gnome";
static SingleApplicationInstance<CommandLineInterface> Instance { get; set; }

public static void Main(string[] args)
{
Expand Down Expand Up @@ -72,6 +72,26 @@ public static void Main(string[] args)
#endif

try {
try {
Instance = new SingleApplicationInstance<CommandLineInterface>();
if (Instance.IsFirstInstance) {
Instance.FirstInstance = new CommandLineInterface();
} else {
var msg = _("Bringing already running Smuxi instance to foreground...");
#if LOG4NET
_Logger.Info(msg);
#else
Console.WriteLine(msg);
#endif
Instance.FirstInstance.PresentMainWindow();
return;
}
} catch (Exception ex) {
#if LOG4NET
_Logger.Warn("Single application instance error, ignoring...", ex);
#endif
}

Frontend.Init(args);
} catch (Exception e) {
#if LOG4NET
Expand All @@ -97,5 +117,34 @@ private static void ShowHelp()
Console.WriteLine(" -d --debug Enable debug output");
Console.WriteLine(" -e --engine engine-name Connect to engine");
}

static string _(string msg)
{
return LibraryCatalog.GetString(msg, LibraryTextDomain);
}
}

public class CommandLineInterface : SingleApplicationInterface
{
public void PresentMainWindow()
{
if (!Frontend.IsGtkInitialized || !Frontend.InGtkApplicationRun) {
return;
}

Gtk.Application.Invoke(delegate {
var window = Frontend.MainWindow;
if (window == null) {
return;
}
window.PresentWithServerTime();
});
}

public override object InitializeLifetimeService()
{
// live forever
return null;
}
}
}

0 comments on commit 928e4e9

Please sign in to comment.