From ba29ecd880fcda9599ed95261284b58be5307d3c Mon Sep 17 00:00:00 2001 From: Kenneth Skovhede Date: Mon, 23 Jun 2014 11:22:06 +0200 Subject: [PATCH] Moved the browser-detection code to the utility library --- .../Duplicati.GUI.TrayIcon/TrayIconBase.cs | 43 +------------ Duplicati/Library/Utility/UrlUtillity.cs | 64 ++++++++++++++++++- 2 files changed, 62 insertions(+), 45 deletions(-) diff --git a/Duplicati/GUI/Duplicati.GUI.TrayIcon/TrayIconBase.cs b/Duplicati/GUI/Duplicati.GUI.TrayIcon/TrayIconBase.cs index b9433be8c..be5a373b0 100644 --- a/Duplicati/GUI/Duplicati.GUI.TrayIcon/TrayIconBase.cs +++ b/Duplicati/GUI/Duplicati.GUI.TrayIcon/TrayIconBase.cs @@ -93,48 +93,7 @@ namespace Duplicati.GUI.TrayIcon public virtual IBrowserWindow ShowUrlInWindow(string url) { //Fallback is to just show the window in a browser - if (Duplicati.Library.Utility.Utility.IsClientOSX) - { - try - { - var cmd = string.IsNullOrWhiteSpace(Program.BrowserCommand) ? "open" : Program.BrowserCommand; - System.Diagnostics.Process.Start(cmd, "\"" + url + "\""); - } - catch - { - } - } - else if (Duplicati.Library.Utility.Utility.IsClientLinux) - { - try - { - var apps = new string[] {Program.BrowserCommand, "xdg-open", "chromium-browser", "google-chrome", "firefox", "mozilla", "konqueror", "netscape", "opera", "epiphany" }; - foreach(var n in apps) - if (!string.IsNullOrWhiteSpace(n) && Duplicati.Library.Utility.Utility.Which(n)) - { - System.Diagnostics.Process.Start(n, "\"" + url + "\""); - return null; - } - - Console.WriteLine("No suitable browser found, try installing \"xdg-open\""); - } - catch - { - } - } - else - { - try - { - if (!string.IsNullOrWhiteSpace(Program.BrowserCommand)) - System.Diagnostics.Process.Start(Program.BrowserCommand, "\"" + url + "\""); - else - System.Diagnostics.Process.Start("\"" + url + "\""); - } - catch - { - } - } + Duplicati.Library.Utility.UrlUtillity.OpenURL(url, Program.BrowserCommand); return null; } diff --git a/Duplicati/Library/Utility/UrlUtillity.cs b/Duplicati/Library/Utility/UrlUtillity.cs index 91237fac3..5e70e53fd 100644 --- a/Duplicati/Library/Utility/UrlUtillity.cs +++ b/Duplicati/Library/Utility/UrlUtillity.cs @@ -45,14 +45,72 @@ namespace Duplicati.Library.Utility /// Opens the given URL in a browser /// /// The url to open, must start with http:// or https:// - public static void OpenUrl(string url) + public static void OpenURL(string url, string browserprogram = null) { + if (!url.StartsWith("http://") && !url.StartsWith("https://")) + throw new Exception("Malformed URL"); + + if (string.IsNullOrWhiteSpace(browserprogram)) + browserprogram = SystemBrowser; + + //Fallback is to just show the window in a browser + if (Utility.IsClientOSX) + { + try + { + var cmd = string.IsNullOrWhiteSpace(browserprogram) ? "open" : browserprogram; + System.Diagnostics.Process.Start(cmd, "\"" + url + "\""); + } + catch + { + if (ErrorHandler != null) + ErrorHandler(string.Format("Unable to open a browser window, please manually visit: \r\n{0}", url)); + } + } + else if (Utility.IsClientLinux) + { + try + { + var apps = new string[] {browserprogram, "xdg-open", "chromium-browser", "google-chrome", "firefox", "mozilla", "konqueror", "netscape", "opera", "epiphany" }; + foreach(var n in apps) + if (!string.IsNullOrWhiteSpace(n) && Duplicati.Library.Utility.Utility.Which(n)) + { + System.Diagnostics.Process.Start(n, "\"" + url + "\""); + return; + } + + if (ErrorHandler != null) + ErrorHandler("No suitable browser found, try installing \"xdg-open\""); + + Console.WriteLine("No suitable browser found, try installing \"xdg-open\""); + } + catch + { + if (ErrorHandler != null) + ErrorHandler(string.Format("Unable to open a browser window, please manually visit: \r\n{0}", url)); + } + } + else + { + OpenUrlWindows(url, browserprogram); + } + } + + /// + /// Opens the given URL in a browser + /// + /// The url to open, must start with http:// or https:// + private static void OpenUrlWindows(string url, string browserprogram) + { + if (string.IsNullOrWhiteSpace(browserprogram)) + browserprogram = SystemBrowser; + try { if (!url.StartsWith("http://") && !url.StartsWith("https://")) throw new Exception("Malformed URL"); - if (string.IsNullOrEmpty(SystemBrowser)) + if (string.IsNullOrEmpty(browserprogram)) { try { @@ -74,7 +132,7 @@ namespace Duplicati.Library.Utility else { System.Diagnostics.Process process = new System.Diagnostics.Process(); - process.StartInfo.FileName = SystemBrowser; + process.StartInfo.FileName = browserprogram; process.StartInfo.Arguments = url; process.StartInfo.UseShellExecute = true; process.Start();