Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 47 additions & 30 deletions DanTup.BrowserSelector/ConfigReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,53 @@ static class ConfigReader
/// </summary>
public static string ConfigPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "BrowserSelector.ini");

static internal IEnumerable<UrlPreference> GetUrlPreferences()
{
if (!File.Exists(ConfigPath))
throw new InvalidOperationException(string.Format("The config file was not found:\r\n{0}\r\n", ConfigPath));

// Poor mans INI file reading... Skip comment lines (TODO: support comments on other lines).
var configLines =
File.ReadAllLines(ConfigPath)
.Select(l => l.Trim())
.Where(l => !string.IsNullOrWhiteSpace(l) && !l.StartsWith(";") && !l.StartsWith("#"));

// Read the browsers section into a dictionary.
var browsers = GetConfig(configLines, "browsers")
.Select(SplitConfig)
.Select(kvp => new Browser { Name = kvp.Key, Location = kvp.Value })
.ToDictionary(b => b.Name);

// If there weren't any at all, force IE in there (nobody should create a config file like this!).
if (!browsers.Any())
browsers.Add("ie", new Browser { Name = "ie", Location = @"iexplore.exe ""{0}""" });

// Read the url preferences, and add a catchall ("*") for the first browser.
var urls = GetConfig(configLines, "urls")
.Select(SplitConfig)
.Select(kvp => new UrlPreference { UrlPattern = kvp.Key, Browser = browsers.ContainsKey(kvp.Value) ? browsers[kvp.Value] : null })
.Union(new[] { new UrlPreference { UrlPattern = "*", Browser = browsers.FirstOrDefault().Value } }) // Add in a catchall that uses the first browser
.Where(up => up.Browser != null);

return urls;
}
static internal IEnumerable<UrlPreference> GetUrlPreferences()
{
if (!File.Exists(ConfigPath))
throw new InvalidOperationException(string.Format("The config file was not found:\r\n{0}\r\n", ConfigPath));

// Poor mans INI file reading... Skip comment lines (TODO: support comments on other lines).
var configLines =
File.ReadAllLines(ConfigPath)
.Select(l => l.Trim())
.Where(l => !string.IsNullOrWhiteSpace(l) && !l.StartsWith(";") && !l.StartsWith("#"));

// Read the browsers section into a dictionary.
var browsers = GetConfig(configLines, "browsers")
.Select(SplitConfig)
.Select(kvp => new Browser { Name = kvp.Key, Location = kvp.Value })
.ToDictionary(b => b.Name);

// If there weren't any at all, force IE in there (nobody should create a config file like this!).
if (!browsers.Any())
browsers.Add("ie", new Browser { Name = "ie", Location = @"iexplore.exe ""{0}""" });

// Read the url preferences, and add a catchall ("*") for the first browser.
var urls = GetConfig(configLines, "urls")
.Select(SplitConfig)
.Select(kvp => new UrlPreference { UrlPattern = kvp.Key, Browser = browsers.ContainsKey(kvp.Value) ? browsers[kvp.Value] : null })
/* .Union(new[] { new UrlPreference { UrlPattern = "*", Browser = browsers.FirstOrDefault().Value } }) // Add in a catchall that uses the first browser
.Where(up => up.Browser != null)*/;

return urls;
}

static internal IEnumerable<Browser> GetBrowsers()
{
if (!File.Exists(ConfigPath))
throw new InvalidOperationException(string.Format("The config file was not found:\r\n{0}\r\n", ConfigPath));

// Poor mans INI file reading... Skip comment lines (TODO: support comments on other lines).
var configLines =
File.ReadAllLines(ConfigPath)
.Select(l => l.Trim())
.Where(l => !string.IsNullOrWhiteSpace(l) && !l.StartsWith(";") && !l.StartsWith("#"));

// Read the browsers section into a dictionary.
return GetConfig(configLines, "browsers")
.Select(SplitConfig)
.Select(kvp => new Browser { Name = kvp.Key, Location = kvp.Value });
}

static IEnumerable<string> GetConfig(IEnumerable<string> configLines, string configName)
{
Expand Down
47 changes: 38 additions & 9 deletions DanTup.BrowserSelector/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
Expand Down Expand Up @@ -282,14 +282,43 @@ static void LaunchBrowser(string url, bool waitForClose = false)
return;
}
}

MessageBox.Show(string.Format("Unable to find a suitable browser matching {0}.", url), "BrowserSelector", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Unable to launch browser, sorry :(\r\n\r\nPlease send a copy of this error to DanTup.\r\n\r\n{0}.", ex.ToString()), "BrowserSelector", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
// No matching browser was found, handle default

var availableBrowsers = ConfigReader.GetBrowsers().ToList();
var openBrowsers = GetOpenBrowsers(availableBrowsers);
if (openBrowsers.Count > 0)
{
p = Process.Start(openBrowsers[0].Location, _url);
if (waitForClose) p.WaitForExit();
return;
}
else if (availableBrowsers.Count > 0)
{
p = Process.Start(availableBrowsers[0].Location, _url);
if (waitForClose) p.WaitForExit();
return;
}
MessageBox.Show($"Unable to find a suitable browser matching {url}.", "BrowserSelector", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
var nl = Environment.NewLine + Environment.NewLine;
MessageBox.Show(string.Format("Unable to launch browser, sorry :({0}Please send a copy of this error to DanTup.{0}{1}.", nl, ex), "BrowserSelector", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private static List<Browser> GetOpenBrowsers(List<Browser> browsers)
{
var ret = new List<Browser>();
foreach (var browser in browsers)
{
var match = Regex.Match(browser.Location, @"(\w+)\.exe", RegexOptions.IgnoreCase);
if (!match.Success) continue;
if (Process.GetProcessesByName(match.Groups[1].Value).Length < 1) continue;
ret.Add(browser);
}
return ret;
}


static void CreateSampleSettings()
Expand Down