From 7d8d8d63764fa24e4f19d5f843e1562fe73643bf Mon Sep 17 00:00:00 2001 From: bor Date: Wed, 8 Jun 2016 20:40:45 +0430 Subject: [PATCH] added url patten matching for issue #5 --- BrowserSelect/Browser.cs | 4 + BrowserSelect/Form1.cs | 2 +- BrowserSelect/Program.cs | 62 ++++++++- BrowserSelect/Properties/AssemblyInfo.cs | 4 +- BrowserSelect/Properties/Settings.Designer.cs | 11 ++ BrowserSelect/Properties/Settings.settings | 3 + BrowserSelect/frm_settings.Designer.cs | 122 +++++++++++++++--- BrowserSelect/frm_settings.cs | 60 +++++++++ BrowserSelect/frm_settings.resx | 11 ++ README.md | 20 ++- 10 files changed, 274 insertions(+), 25 deletions(-) diff --git a/BrowserSelect/Browser.cs b/BrowserSelect/Browser.cs index 1a548f7..fd2b632 100644 --- a/BrowserSelect/Browser.cs +++ b/BrowserSelect/Browser.cs @@ -18,6 +18,10 @@ public override string ToString() { return name; } + public static implicit operator Browser(System.String s) + { + return BrowserFinder.find().Where(b => b.name == s).First(); + } } static class BrowserFinder { public static List find() { diff --git a/BrowserSelect/Form1.cs b/BrowserSelect/Form1.cs index 164c4b5..24b0c03 100644 --- a/BrowserSelect/Form1.cs +++ b/BrowserSelect/Form1.cs @@ -67,7 +67,7 @@ private void browser_click(object sender, EventArgs e) { open_url(uc.browser); } - private void open_url(Browser b) { + public static void open_url(Browser b) { if (b.exec == "edge"){ //edge is a universal app , which means we can't just run it like other browsers Process.Start("microsoft-edge:"+Program.url diff --git a/BrowserSelect/Program.cs b/BrowserSelect/Program.cs index f646198..d042b6c 100644 --- a/BrowserSelect/Program.cs +++ b/BrowserSelect/Program.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; +using System.Text.RegularExpressions; using System.Windows.Forms; using BrowserSelect.Properties; @@ -14,11 +15,27 @@ static class Program { /// [STAThread] static void Main(string[] args) { + if (Settings.Default.HideBrowsers == null) + Settings.Default.HideBrowsers = new StringCollection(); + if (Settings.Default.AutoBrowser == null) + Settings.Default.AutoBrowser = new StringCollection(); + if (args.Length > 0) + { url = args[0]; + foreach (var rule in Settings.Default.AutoBrowser) + { + var sr=rule.Split(new[] {"[#!][$~][?_]"}, StringSplitOptions.None); + var pattern = sr[0]; + var browser = sr[1]; - if(Settings.Default.HideBrowsers == null) - Settings.Default.HideBrowsers = new StringCollection(); + if (IsDomainValid(new Uri(url).Host, pattern)) + { + Form1.open_url((Browser)browser); + return; + } + } + } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); @@ -36,5 +53,46 @@ public static string ProgramFilesx86() return Environment.GetEnvironmentVariable("ProgramFiles"); } + + + /// + /// Checks if a wildcard string matches a domain + /// taken from http://madskristensen.net/post/wildcard-search-for-domains-in-c + /// + public static bool IsDomainValid(string domain, string domainToCheck) + { + if (domainToCheck.Contains("*")) + { + string checkDomain = domainToCheck; + if (checkDomain.StartsWith("*.")) + checkDomain = "*" + checkDomain.Substring(2, checkDomain.Length - 2); + return DoesWildcardMatch(domain, checkDomain); + } + else + { + return domainToCheck.Equals(domain, StringComparison.OrdinalIgnoreCase); + } + } + /// + /// Performs a wildcard (*) search on any string. + /// + public static bool DoesWildcardMatch(string originalString, string searchString) + { + if (!searchString.StartsWith("*")) + { + int stop = searchString.IndexOf('*'); + if (!originalString.StartsWith(searchString.Substring(0, stop))) + return false; + } + if (!searchString.EndsWith("*")) + { + int start = searchString.LastIndexOf('*') + 1; + if (!originalString.EndsWith(searchString.Substring(start, searchString.Length - start))) + return false; + } + Regex regex = new Regex(searchString.Replace(@".", @"\.").Replace(@"*", @".*")); + return regex.IsMatch(originalString); + } + } } diff --git a/BrowserSelect/Properties/AssemblyInfo.cs b/BrowserSelect/Properties/AssemblyInfo.cs index 09fccea..56ccc63 100644 --- a/BrowserSelect/Properties/AssemblyInfo.cs +++ b/BrowserSelect/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.2.0")] -[assembly: AssemblyFileVersion("1.0.2.0")] +[assembly: AssemblyVersion("1.2.0.0")] +[assembly: AssemblyFileVersion("1.2.0.0")] diff --git a/BrowserSelect/Properties/Settings.Designer.cs b/BrowserSelect/Properties/Settings.Designer.cs index a0d24ec..694e602 100644 --- a/BrowserSelect/Properties/Settings.Designer.cs +++ b/BrowserSelect/Properties/Settings.Designer.cs @@ -33,5 +33,16 @@ public static Settings Default { this["HideBrowsers"] = value; } } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + public global::System.Collections.Specialized.StringCollection AutoBrowser { + get { + return ((global::System.Collections.Specialized.StringCollection)(this["AutoBrowser"])); + } + set { + this["AutoBrowser"] = value; + } + } } } diff --git a/BrowserSelect/Properties/Settings.settings b/BrowserSelect/Properties/Settings.settings index 22d8767..e844a28 100644 --- a/BrowserSelect/Properties/Settings.settings +++ b/BrowserSelect/Properties/Settings.settings @@ -5,5 +5,8 @@ + + + \ No newline at end of file diff --git a/BrowserSelect/frm_settings.Designer.cs b/BrowserSelect/frm_settings.Designer.cs index 03d1b58..6c3b2f8 100644 --- a/BrowserSelect/frm_settings.Designer.cs +++ b/BrowserSelect/frm_settings.Designer.cs @@ -23,19 +23,28 @@ protected override void Dispose(bool disposing) { /// the contents of this method with the code editor. /// private void InitializeComponent() { + System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frm_settings)); this.btn_setdefault = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.groupBox1 = new System.Windows.Forms.GroupBox(); - this.groupBox2 = new System.Windows.Forms.GroupBox(); this.browser_filter = new System.Windows.Forms.CheckedListBox(); + this.groupBox2 = new System.Windows.Forms.GroupBox(); + this.groupBox3 = new System.Windows.Forms.GroupBox(); + this.gv_filters = new System.Windows.Forms.DataGridView(); + this.label3 = new System.Windows.Forms.Label(); + this.pattern = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.browser = new System.Windows.Forms.DataGridViewComboBoxColumn(); + this.label4 = new System.Windows.Forms.Label(); this.groupBox1.SuspendLayout(); this.groupBox2.SuspendLayout(); + this.groupBox3.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.gv_filters)).BeginInit(); this.SuspendLayout(); // // btn_setdefault // - this.btn_setdefault.Location = new System.Drawing.Point(194, 58); + this.btn_setdefault.Location = new System.Drawing.Point(34, 108); this.btn_setdefault.Name = "btn_setdefault"; this.btn_setdefault.Size = new System.Drawing.Size(137, 23); this.btn_setdefault.TabIndex = 0; @@ -47,16 +56,16 @@ private void InitializeComponent() { // this.label1.Location = new System.Drawing.Point(6, 28); this.label1.Name = "label1"; - this.label1.Size = new System.Drawing.Size(352, 53); + this.label1.Size = new System.Drawing.Size(168, 86); this.label1.TabIndex = 1; this.label1.Text = "This is for windows 7 , windows 8 and newer detect this program automatically and" + " prompt for default browser on first url opened after installation."; // // label2 // - this.label2.Location = new System.Drawing.Point(12, 315); + this.label2.Location = new System.Drawing.Point(12, 362); this.label2.Name = "label2"; - this.label2.Size = new System.Drawing.Size(360, 35); + this.label2.Size = new System.Drawing.Size(180, 67); this.label2.TabIndex = 2; this.label2.Text = "If you think there should be any other options here that is missing please submit" + " an issue on the github page and i\'ll see if i can add it."; @@ -66,46 +75,117 @@ private void InitializeComponent() { this.groupBox1.Controls.Add(this.browser_filter); this.groupBox1.Location = new System.Drawing.Point(12, 12); this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(360, 198); + this.groupBox1.Size = new System.Drawing.Size(180, 198); this.groupBox1.TabIndex = 3; this.groupBox1.TabStop = false; this.groupBox1.Text = "Select Browsers"; // + // browser_filter + // + this.browser_filter.Dock = System.Windows.Forms.DockStyle.Fill; + this.browser_filter.FormattingEnabled = true; + this.browser_filter.Location = new System.Drawing.Point(3, 16); + this.browser_filter.Name = "browser_filter"; + this.browser_filter.Size = new System.Drawing.Size(174, 179); + this.browser_filter.TabIndex = 0; + this.browser_filter.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.browser_filter_ItemCheck); + // // groupBox2 // this.groupBox2.Controls.Add(this.btn_setdefault); this.groupBox2.Controls.Add(this.label1); - this.groupBox2.Location = new System.Drawing.Point(12, 216); + this.groupBox2.Location = new System.Drawing.Point(15, 216); this.groupBox2.Name = "groupBox2"; - this.groupBox2.Size = new System.Drawing.Size(360, 88); + this.groupBox2.Size = new System.Drawing.Size(177, 143); this.groupBox2.TabIndex = 0; this.groupBox2.TabStop = false; this.groupBox2.Text = "Default Browser"; // - // browser_filter + // groupBox3 // - this.browser_filter.Dock = System.Windows.Forms.DockStyle.Fill; - this.browser_filter.FormattingEnabled = true; - this.browser_filter.Location = new System.Drawing.Point(3, 16); - this.browser_filter.Name = "browser_filter"; - this.browser_filter.Size = new System.Drawing.Size(354, 179); - this.browser_filter.TabIndex = 0; - this.browser_filter.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.browser_filter_ItemCheck); + this.groupBox3.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) + | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.groupBox3.Controls.Add(this.label4); + this.groupBox3.Controls.Add(this.gv_filters); + this.groupBox3.Controls.Add(this.label3); + this.groupBox3.Location = new System.Drawing.Point(198, 12); + this.groupBox3.Name = "groupBox3"; + this.groupBox3.Size = new System.Drawing.Size(414, 397); + this.groupBox3.TabIndex = 4; + this.groupBox3.TabStop = false; + this.groupBox3.Text = "Auto Select Filters"; + // + // gv_filters + // + this.gv_filters.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) + | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.gv_filters.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.gv_filters.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { + this.pattern, + this.browser}); + this.gv_filters.Location = new System.Drawing.Point(6, 59); + this.gv_filters.Name = "gv_filters"; + this.gv_filters.Size = new System.Drawing.Size(402, 244); + this.gv_filters.TabIndex = 1; + this.gv_filters.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick); + // + // label3 + // + this.label3.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.label3.Location = new System.Drawing.Point(6, 16); + this.label3.Name = "label3"; + this.label3.Size = new System.Drawing.Size(402, 40); + this.label3.TabIndex = 0; + this.label3.Text = "Using this section you can add rules that based on them browser select will autom" + + "atically choose a browser instead of displaying the list for you to choose manua" + + "lly."; + // + // pattern + // + this.pattern.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill; + this.pattern.DataPropertyName = "Pattern"; + this.pattern.HeaderText = "Pattern"; + this.pattern.Name = "pattern"; + this.pattern.Resizable = System.Windows.Forms.DataGridViewTriState.False; + // + // browser + // + this.browser.DataPropertyName = "Browser"; + this.browser.HeaderText = "Browser"; + this.browser.Name = "browser"; + // + // label4 + // + this.label4.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.label4.Location = new System.Drawing.Point(6, 306); + this.label4.Name = "label4"; + this.label4.Size = new System.Drawing.Size(408, 88); + this.label4.TabIndex = 2; + this.label4.Text = resources.GetString("label4.Text"); // // frm_settings // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(384, 359); + this.ClientSize = new System.Drawing.Size(624, 421); + this.Controls.Add(this.groupBox3); this.Controls.Add(this.groupBox2); this.Controls.Add(this.groupBox1); this.Controls.Add(this.label2); - this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow; + this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow; + this.MinimumSize = new System.Drawing.Size(360, 460); this.Name = "frm_settings"; this.Text = "Settings"; + this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.frm_settings_FormClosing); this.Load += new System.EventHandler(this.frm_settings_Load); this.groupBox1.ResumeLayout(false); this.groupBox2.ResumeLayout(false); + this.groupBox3.ResumeLayout(false); + ((System.ComponentModel.ISupportInitialize)(this.gv_filters)).EndInit(); this.ResumeLayout(false); } @@ -118,5 +198,11 @@ private void InitializeComponent() { private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.CheckedListBox browser_filter; private System.Windows.Forms.GroupBox groupBox2; + private System.Windows.Forms.GroupBox groupBox3; + private System.Windows.Forms.DataGridView gv_filters; + private System.Windows.Forms.Label label3; + private System.Windows.Forms.DataGridViewTextBoxColumn pattern; + private System.Windows.Forms.DataGridViewComboBoxColumn browser; + private System.Windows.Forms.Label label4; } } \ No newline at end of file diff --git a/BrowserSelect/frm_settings.cs b/BrowserSelect/frm_settings.cs index a2e2a8d..1129e98 100644 --- a/BrowserSelect/frm_settings.cs +++ b/BrowserSelect/frm_settings.cs @@ -15,6 +15,7 @@ public frm_settings() { InitializeComponent(); } + private List rules= new List(); private void frm_settings_Load(object sender, EventArgs e) { using (RegistryKey key = Registry.CurrentUser.OpenSubKey( @"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice")) { @@ -26,10 +27,20 @@ private void frm_settings_Load(object sender, EventArgs e) { } var browsers = BrowserFinder.find(); + var c = ((DataGridViewComboBoxColumn)gv_filters.Columns["browser"]); + //c.ValueType = typeof(Browser); + foreach (Browser b in browsers) { browser_filter.Items.Add(b,!Settings.Default.HideBrowsers.Contains(b.exec)); + c.Items.Add(b.ToString()); } + + foreach(var rule in Settings.Default.AutoBrowser) + rules.Add(rule); + var bs = new BindingSource(); + bs.DataSource = rules; + gv_filters.DataSource = bs; } private void btn_setdefault_Click(object sender, EventArgs e) { @@ -59,5 +70,54 @@ private void browser_filter_ItemCheck(object sender, ItemCheckEventArgs e) } Settings.Default.Save(); } + + private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) + { + + } + + private void frm_settings_FormClosing(object sender, FormClosingEventArgs e) + { + Settings.Default.AutoBrowser.Clear(); + foreach (var rule in rules) + { + if(rule.valid()) + Settings.Default.AutoBrowser.Add(rule.ToString()); + } + Settings.Default.Save(); + } + } + class AutoMatchRule + { + public string Pattern { get; set; } + public string Browser { get; set; } + + public static implicit operator AutoMatchRule(System.String s) + { + var ss = s.Split(new[] { "[#!][$~][?_]" }, StringSplitOptions.None); + return new AutoMatchRule() + { + Pattern = ss[0], + Browser = ss[1] + }; + } + + public override string ToString() + { + return Pattern+ "[#!][$~][?_]"+Browser; + } + + public bool valid() + { + try //because they may be null + { + return Browser.Length > 0 && Pattern.Length > 0; + } + catch + { + return false; + } + + } } } diff --git a/BrowserSelect/frm_settings.resx b/BrowserSelect/frm_settings.resx index 1af7de1..0520e09 100644 --- a/BrowserSelect/frm_settings.resx +++ b/BrowserSelect/frm_settings.resx @@ -117,4 +117,15 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + You may use wildcards for pattern. e.g. *.yahoo.com or mail.*.net or *.google.* +you can open browser select manually from startmenu to change the rules. browser select always opens itself when launched from startmenu regardless of Filters added. +changes will be saved automatically when you close this window , make sure to fill all the fields (pattern and browser) and select another row before closing to make sure it will be saved successfully. + + + True + + + True + \ No newline at end of file diff --git a/README.md b/README.md index e9bd362..129eca0 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,24 @@ to install Download this file than set it as the default browser. ![select default browser](https://raw.githubusercontent.com/zumoshi/BrowserSelect/master/screenshots/photo_2015-10-12_16-43-08.jpg) -it has been tested on windows 8.1 x64 but should work in windows 7 and newer , requires **.net framework 4**. +it has been tested on windows 7, windows 8.1 and windows 10. requires **.net framework 4**. # Download -you can download browser select here : [Browser select v1.1.0 (186KB)](https://github.com/zumoshi/BrowserSelect/releases/download/1.1.0/BrowserSelect.exe) +you can download browser select here : [Browser select v1.2.0 (191KB)](https://github.com/zumoshi/BrowserSelect/releases/download/1.2.0/BrowserSelect.exe) + + + +# Changelog + +v1.2 +- you can now add url patterns to select the browser based on url automatically. + +v1.1 +- added option to select browsers that are displayed on the list (and remove/hide some) + +v1.0.2 +- added option to set browser select as default browser in settings + +v1.0.1 +- added edge browser for windows 10 (it wouldn't show up due edge being a Universal App) \ No newline at end of file