[smuxi: 3/5] Frontend-GNOME: support passing links by command line



commit 3466fa15baa6dce2d38d96862ff2b9c4b33ff562
Author: Mirco Bauer <meebey meebey net>
Date:   Sun Apr 5 12:42:58 2015 +0200

    Frontend-GNOME: support passing links by command line
    
    TODO: add parameter for allowing multiple instances explicitly

 src/Frontend-GNOME/Frontend.cs |   14 +----
 src/Frontend-GNOME/Main.cs     |  150 +++++++++++++++++++++++++++------------
 2 files changed, 105 insertions(+), 59 deletions(-)
---
diff --git a/src/Frontend-GNOME/Frontend.cs b/src/Frontend-GNOME/Frontend.cs
index 574e73d..0cb5892 100644
--- a/src/Frontend-GNOME/Frontend.cs
+++ b/src/Frontend-GNOME/Frontend.cs
@@ -182,7 +182,7 @@ namespace Smuxi.Frontend.Gnome
             }
         }
 
-        public static void Init(string[] args)
+        public static void Init(string[] args, string engine)
         {
             System.Threading.Thread.CurrentThread.Name = "Main";
            
@@ -214,18 +214,6 @@ namespace Smuxi.Frontend.Gnome
                 ConnectEngineToGUI();
             } else {
                 // there are remote engines defined, means we have to ask
-                string engine = null;
-                for (int i = 0; i < args.Length; i++) {
-                    var arg = args[i];
-                    switch (arg) {
-                        case "-e":
-                        case "--engine":
-                            if (args.Length >=  i + 1) {
-                                engine = args[i + 1];
-                            }
-                            break;
-                    }
-                }
                 //_SplashScreenWindow.Destroy();
                 _SplashScreenWindow = null;
                 try {
diff --git a/src/Frontend-GNOME/Main.cs b/src/Frontend-GNOME/Main.cs
index 7063ab4..84781c7 100644
--- a/src/Frontend-GNOME/Main.cs
+++ b/src/Frontend-GNOME/Main.cs
@@ -24,10 +24,11 @@ using System;
 using System.Runtime.Remoting;
 using System.Reflection;
 using Gtk.Extensions;
+using NDesk.Options;
 using Smuxi.Common;
 
 namespace Smuxi.Frontend.Gnome
-{ 
+{
     public class MainClass
     {
 #if LOG4NET
@@ -38,52 +39,83 @@ namespace Smuxi.Frontend.Gnome
 
         public static void Main(string[] args)
         {
-            bool debug = false;
-            foreach (string arg in args) {
-                switch (arg) {
-                    case "-d":
-                    case "--debug":
-                        debug = true;
-                        break;
-                    case "-h":
-                    case "--help":
-                        ShowHelp();
-                        Environment.Exit(0);
-                        break;
-                    /*
-                    // don't block other parameters as we pass them to
-                    // GTK+ / GNOME too
-                    default:
-                        Console.WriteLine("Invalid option: " + arg);
-                        Environment.Exit(1);
-                        break;
-                    */
+            var debug = false;
+            var link = String.Empty;
+            var engine = String.Empty;
+            var options = new OptionSet();
+            options.Add(
+                "d|debug",
+                _("Enable debug output"),
+                v => {
+                    debug = true;
                 }
-            }
-            
+            );
+            options.Add(
+                "h|help",
+                _("Show this help"),
+                v => {
+                    Console.WriteLine("Usage: smuxi-frontend-gnome [options]");
+                    Console.WriteLine();
+                    Console.WriteLine(_("Options:"));
+                    options.WriteOptionDescriptions(Console.Out);
+                    Environment.Exit(0);
+                }
+            );
+            options.Add(
+                "e|engine=",
+                _("Connect to engine"),
+                v => {
+                    engine = v;
+                }
+            );
+            options.Add(
+                "open|open-link=",
+                _("Opens the specified link in Smuxi"),
+                v => {
+                    link = v;
+                }
+            );
+
+            try {
+                options.Parse(args);
+
 #if LOG4NET
-            // initialize log level
-            log4net.Repository.ILoggerRepository repo = log4net.LogManager.GetRepository();
-            if (debug) {
-                repo.Threshold = log4net.Core.Level.Debug;
-            } else {
-                repo.Threshold = log4net.Core.Level.Info;
-            }
+                // initialize log level
+                log4net.Repository.ILoggerRepository repo = log4net.LogManager.GetRepository();
+                if (debug) {
+                    repo.Threshold = log4net.Core.Level.Debug;
+                } else {
+                    repo.Threshold = log4net.Core.Level.Info;
+                }
 #endif
 
-            try {
                 try {
                     Instance = new SingleApplicationInstance<CommandLineInterface>();
                     if (Instance.IsFirstInstance) {
                         Instance.FirstInstance = new CommandLineInterface();
+                        if (!String.IsNullOrEmpty(link)) {
+                            Instance.FirstInstance.OpenLink(link);
+                        }
                     } else {
-                        var msg = _("Bringing already running Smuxi instance to foreground...");
+                        if (!String.IsNullOrEmpty(link)) {
+                            var msg = _("Passing link to already running Smuxi instance...");
+#if LOG4NET
+                            _Logger.Info(msg);
+#else
+                            Console.WriteLine(msg);
+#endif
+                            Instance.FirstInstance.OpenLink(link);
+                        } else {
+                            var msg = _("Bringing already running Smuxi instance to foreground...");
 #if LOG4NET
-                        _Logger.Info(msg);
+                            _Logger.Info(msg);
 #else
-                        Console.WriteLine(msg);
+                            Console.WriteLine(msg);
 #endif
-                        Instance.FirstInstance.PresentMainWindow();
+                            Instance.FirstInstance.PresentMainWindow();
+                        }
+
+                        // don't initialize/spawn another instance
                         return;
                     }
                 } catch (Exception ex) {
@@ -92,7 +124,7 @@ namespace Smuxi.Frontend.Gnome
 #endif
                 }
 
-                Frontend.Init(args);
+                Frontend.Init(args, engine);
             } catch (Exception e) {
 #if LOG4NET
                 _Logger.Fatal(e);
@@ -108,16 +140,6 @@ namespace Smuxi.Frontend.Gnome
             }
         }
 
-        private static void ShowHelp()
-        {
-            Console.WriteLine("Usage: smuxi-frontend-gnome [options]");
-            Console.WriteLine();
-            Console.WriteLine("Options:");
-            Console.WriteLine("  -h --help                Show this help");
-            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);
@@ -126,6 +148,10 @@ namespace Smuxi.Frontend.Gnome
 
     public class CommandLineInterface : SingleApplicationInterface
     {
+#if LOG4NET
+        static readonly log4net.ILog Logger = 
log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
+#endif
+
         public void PresentMainWindow()
         {
             if (!Frontend.IsGtkInitialized || !Frontend.InGtkApplicationRun) {
@@ -141,6 +167,38 @@ namespace Smuxi.Frontend.Gnome
             });
         }
 
+        public void OpenLink(string link)
+        {
+            if (Frontend.Session == null) {
+                // we don't have a session yet, probably local instance that is
+                // just starting or a remote engine that isn't connected yet
+                EventHandler handler = null;
+                handler = delegate {
+                    if (Frontend.Session == null) {
+                        return;
+                    }
+                    // we can't know which thread invokes SessionPropertyChanged
+                    Gtk.Application.Invoke((o, e) => {
+#if LOG4NET
+                        Logger.Info("Opening the link...");
+#endif
+                        Frontend.OpenLink(new Uri(link));
+                    });
+                    // only process the link once
+                    Frontend.SessionPropertyChanged -= handler;
+                };
+#if LOG4NET
+                Logger.Info("Delaying opening the link as the session isn't initialized yet...");
+#endif
+                // install event handler and wait till the session gets initialized
+                Frontend.SessionPropertyChanged += handler;
+            } else {
+                Gtk.Application.Invoke((o, e) => {
+                    Frontend.OpenLink(new Uri(link));
+                });
+            }
+        }
+
         public override object InitializeLifetimeService()
         {
             // live forever


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