1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
#region Disclaimer / License
// Copyright (C) 2015, The Duplicati Team
// http://www.duplicati.com, info@duplicati.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using Duplicati.Library.Common;
namespace Duplicati.Library.Utility
{
public static class UrlUtility
{
/// <summary>
/// The file path to the system browser selected
/// </summary>
public static readonly string SystemBrowser = null;
/// <summary>
/// A delegate for handing error messages
/// </summary>
/// <param name="errormessage">The message to display the error for</param>
public delegate void ErrorHandlerDelegate(string errormessage);
/// <summary>
/// The errorhandler callback method
/// </summary>
public static ErrorHandlerDelegate ErrorHandler;
/// <summary>
/// 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, string browserprogram = null)
{
if (!url.StartsWith("http://", StringComparison.Ordinal) && !url.StartsWith("https://", StringComparison.Ordinal))
throw new Exception("Malformed URL");
if (string.IsNullOrWhiteSpace(browserprogram))
browserprogram = SystemBrowser;
//Fallback is to just show the window in a browser
if (Platform.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 (Platform.IsClientPosix)
{
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://", StringComparison.Ordinal) && !url.StartsWith("https://", StringComparison.Ordinal))
throw new Exception("Malformed URL");
if (string.IsNullOrEmpty(browserprogram))
{
try
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = url;
process.StartInfo.UseShellExecute = true;
process.Start();
}
catch
{
//The straightforward method gives an error: "The requested lookup key was not found in any active activation context"
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "rundll32.exe";
process.StartInfo.Arguments = "url.dll,FileProtocolHandler " + url;
process.StartInfo.UseShellExecute = true;
process.Start();
}
}
else
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = browserprogram;
process.StartInfo.Arguments = url;
process.StartInfo.UseShellExecute = true;
process.Start();
}
}
catch
{
if (ErrorHandler != null)
ErrorHandler(string.Format("Unable to open a browser window, please manually visit: \r\n{0}", url));
}
}
}
}
|