Skip to content

Commit

Permalink
Merge #2958 Workaround to launch URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Jan 8, 2020
2 parents e181495 + ca149bc commit 089e77d
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 48 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ All notable changes to this project will be documented in this file.
- [GUI] Multiple manual downloads, uncached filter, purge option (#2930 by: HebaruSan; reviewed: DasSkelett)
- [Cmdline] Return failure on failed commands for headless prompt (#2941 by: HebaruSan; reviewed: DasSkelett)
- [GUI] Obey system colors for dark theme support (#2937 by: HebaruSan; reviewed: DasSkelett)
- [Multiple] Workaround to launch URLs (#2958 by: HebaruSan; reviewed: DasSkelett)

### Internal

Expand Down
5 changes: 1 addition & 4 deletions ConsoleUI/ModInfoScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,7 @@ public static bool LaunchURL(Uri u)
// then wait 1.5 seconds and refresh the screen when it closes.
ConsoleMessageDialog d = new ConsoleMessageDialog("Launching...", new List<string>());
d.Run(() => {
Process.Start(new ProcessStartInfo() {
UseShellExecute = true,
FileName = u.ToString()
});
Utilities.ProcessStartURL(u.ToString());
System.Threading.Thread.Sleep(1500);
});
return true;
Expand Down
47 changes: 47 additions & 0 deletions Core/Utilities.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.IO;
using System.Diagnostics;
using System.Transactions;
using ChinhDo.Transactions.FileManager;
using log4net;

namespace CKAN
{
Expand Down Expand Up @@ -75,5 +77,50 @@ private static void _CopyDirectory(string sourceDirPath, string destDirPath, boo
}
}
}

/// <summary>
/// Launch a URL. For YEARS this was done by Process.Start in a
/// cross-platform way, but Microsoft chose to break that,
/// so now every .NET app has to write its own custom code for it,
/// with special code for each platform.
/// https://github.com/dotnet/corefx/issues/10361
/// </summary>
/// <param name="url">URL to launch</param>
/// <returns>
/// true if launched, false otherwise
/// </returns>
public static bool ProcessStartURL(string url)
{
try
{
if (Platform.IsMac)
{
Process.Start("open", $"\"{url}\"");
return true;
}
else if (Platform.IsUnix)
{
Process.Start("xdg-open", $"\"{url}\"");
return true;
}
else
{
// Try the old way
Process.Start(new ProcessStartInfo(url)
{
UseShellExecute = true,
Verb = "open"
});
return true;
}
}
catch (Exception exc)
{
log.Error($"Exception for URL {url}", exc);
}
return false;
}

private static readonly ILog log = LogManager.GetLogger(typeof(Utilities));
}
}
6 changes: 3 additions & 3 deletions GUI/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,7 @@ private void manageKspInstancesMenuItem_Click(object sender, EventArgs e)

private void openKspDirectoryToolStripMenuItem_Click(object sender, EventArgs e)
{
Process.Start(Instance.manager.CurrentInstance.GameDir());
Utilities.ProcessStartURL(Instance.manager.CurrentInstance.GameDir());
}

private void CompatibleKspVersionsToolStripMenuItem_Click(object sender, EventArgs e)
Expand Down Expand Up @@ -1296,12 +1296,12 @@ private void MainTabControl_OnSelectedIndexChanged(object sender, EventArgs e)

private void reportClientIssueToolStripMenuItem_Click(object sender, EventArgs e)
{
Process.Start("https://github.com/KSP-CKAN/CKAN/issues/new/choose");
Utilities.ProcessStartURL("https://github.com/KSP-CKAN/CKAN/issues/new/choose");
}

private void reportMetadataIssueToolStripMenuItem_Click(object sender, EventArgs e)
{
Process.Start("https://github.com/KSP-CKAN/NetKAN/issues/new/choose");
Utilities.ProcessStartURL("https://github.com/KSP-CKAN/NetKAN/issues/new/choose");
}

private void ModList_MouseDown(object sender, MouseEventArgs e)
Expand Down
12 changes: 2 additions & 10 deletions GUI/MainInstall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,7 @@ private void InstallMods(object sender, DoWorkEventArgs e)
// Launch the URL describing this host's throttling practices, if any
if (kraken.infoUrl != null)
{
Process.Start(new ProcessStartInfo()
{
UseShellExecute = true,
FileName = kraken.infoUrl.ToString()
});
Utilities.ProcessStartURL(kraken.infoUrl.ToString());
}
// Now pretend they clicked the menu option for the settings
Enabled = false;
Expand All @@ -303,11 +299,7 @@ private void InstallMods(object sender, DoWorkEventArgs e)
{
if (GUI.user.RaiseYesNoDialog(Properties.Resources.MainInstallLibCurlMissing))
{
Process.Start(new ProcessStartInfo()
{
UseShellExecute = true,
FileName = "https://github.com/KSP-CKAN/CKAN/wiki/libcurl"
});
Utilities.ProcessStartURL("https://github.com/KSP-CKAN/CKAN/wiki/libcurl");
}
throw;
}
Expand Down
4 changes: 2 additions & 2 deletions GUI/MainModInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private void ContentsDownloadButton_Click(object sender, EventArgs e)

private void ContentsOpenButton_Click(object sender, EventArgs e)
{
Process.Start(manager.Cache.GetCachedFilename(SelectedModule.ToModule()));
Utilities.ProcessStartURL(manager.Cache.GetCachedFilename(SelectedModule.ToModule()));
}

private void LinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
Expand Down Expand Up @@ -608,7 +608,7 @@ internal void OpenFileBrowser(TreeNode node)
return;
}

Process.Start(location);
Utilities.ProcessStartURL(location);
}
}
}
2 changes: 1 addition & 1 deletion GUI/MainModList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ private async void ModList_CellValueChanged(object sender, DataGridViewCellEvent
DataGridViewLinkCell cell = gridCell as DataGridViewLinkCell;
string cmd = cell?.Value.ToString();
if (!string.IsNullOrEmpty(cmd))
Process.Start(cmd);
Utilities.ProcessStartURL(cmd);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion GUI/ManageKspInstances.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ private void OpenDirectoryMenuItem_Click(object sender, EventArgs e)
return;
}

System.Diagnostics.Process.Start(path);
Utilities.ProcessStartURL(path);
}

private void RenameButton_Click(object sender, EventArgs e)
Expand Down
7 changes: 1 addition & 6 deletions GUI/SettingsDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,7 @@ private void ResetCacheButton_Click(object sender, EventArgs e)

private void OpenCacheButton_Click(object sender, EventArgs e)
{
Process.Start(new ProcessStartInfo()
{
FileName = config.DownloadCacheDir,
UseShellExecute = true,
Verb = "open"
});
Utilities.ProcessStartURL(config.DownloadCacheDir);
}

private void ReposListBox_SelectedIndexChanged(object sender, EventArgs e)
Expand Down
31 changes: 10 additions & 21 deletions GUI/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ public static void HideConsoleWindow()
public static bool CheckURLValid(string source)
{
Uri uri_result;
return Uri.TryCreate(source, UriKind.Absolute, out uri_result) && uri_result.Scheme == Uri.UriSchemeHttp;
return Uri.TryCreate(source, UriKind.Absolute, out uri_result)
&& (uri_result.Scheme == Uri.UriSchemeHttp
|| uri_result.Scheme == Uri.UriSchemeHttps);
}

/// <summary>
Expand All @@ -95,30 +97,17 @@ public static bool TryOpenWebPage(string url, IEnumerable<string> prefixes = nul
{
// Default prefixes to try if not provided
if (prefixes == null)
prefixes = new string[] { "http://", "https://" };

try // opening the page normally
{
Process.Start(url);
return true; // we did it! return true
prefixes = new string[] { "https://", "http://" };
}
catch (Exception) // something bad happened

foreach (string fullUrl in new string[] { url }
.Concat(prefixes.Select(p => p + url).Where(CheckURLValid)))
{
foreach (string prefixed_url in prefixes.Select(p=>p+url).Where(CheckURLValid))
{
try // with a new prefix
{
Process.Start(prefixed_url);
return true;
}
catch (Exception)
{
// move along to the next prefix
}
}
// We tried all prefixes, and still no luck.
return false;
if (Utilities.ProcessStartURL(fullUrl))
return true;
}
return false;
}

/// <summary>
Expand Down

0 comments on commit 089e77d

Please sign in to comment.