Moved the browser-detection code to the utility library

This commit is contained in:
Kenneth Skovhede
2014-06-23 11:22:06 +02:00
parent 5ef8528eba
commit ba29ecd880
2 changed files with 62 additions and 45 deletions
@@ -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;
}
+61 -3
View File
@@ -45,14 +45,72 @@ namespace Duplicati.Library.Utility
/// Opens the given URL in a browser
/// </summary>
/// <param name="url">The url to open, must start with http:// or https://</param>
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);
}
}
/// <summary>
/// Opens the given URL in a browser
/// </summary>
/// <param name="url">The url to open, must start with http:// or https://</param>
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();