static void Main(string[] args)
{
// to prevent loss of settings when on update
if (Settings.Default.UpdateSettings)
{
Settings.Default.Upgrade();
Settings.Default.UpdateSettings = false;
Settings.Default.last_version = "nope";
// to prevent nullreference in case settings file did not exist
if (Settings.Default.HideBrowsers == null)
Settings.Default.HideBrowsers = new StringCollection();
if (Settings.Default.AutoBrowser == null)
Settings.Default.AutoBrowser = new StringCollection();
Settings.Default.Save();
}
// check for update
if (Settings.Default.check_update != "nope" &&
DateTime.Now.Subtract(time(Settings.Default.check_update)).TotalDays > 7)
{
var uc = new UpdateChecker();
Task.Factory.StartNew(() => uc.check());
}
//checking if a url is being opened or app is ran from start menu (without arguments)
if (args.Length > 0)
{
//check to see if auto select rules match
url = args[0];
//add http:// to url if it is missing a protocol
var uri = new UriBuilder(url).Uri;
url = uri.ToString();
foreach (var sr in Settings.Default.AutoBrowser.Cast<string>()
// maybe i should use a better way to split the pattern and browser name ?
.Select(x => x.Split(new[] { "[#!][$~][?_]" }, StringSplitOptions.None))
// to make sure * doesn't match when non-* rules exist.
.OrderBy(x => ((x[0].Contains("*")) ? 1 : 0) + (x[0] == "*" ? 1 : 0)))
{
var pattern = sr[0];
var browser = sr[1];
// matching the domain to pattern
if (DoesDomainMatchPattern(uri.Host, pattern))
{
// ignore the display browser select entry to prevent app running itself
if (browser != "display BrowserSelect")
{
//todo: handle the case if browser is not found (e.g. imported settings or uninstalled browser)
Form1.open_url((Browser)browser);
return;
}
else
{
// simply break the loop to let the app display selection dialogue
break;
}
}
}
}
// display main form
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}