Skip to content

Commit cf93c03

Browse files
committed
added an update checker
it will poll the github latest release url once per week and will replace the help icon on main window corner with a shiny yellow New icon that when clicked will display current and latest version number prompting the user to upgrade. it is disabled by default. to enable you have to check a checkbox in settings . user may also manually initiate the update check proccess.
1 parent 11c7603 commit cf93c03

File tree

14 files changed

+270
-12
lines changed

14 files changed

+270
-12
lines changed

BrowserSelect/BrowserSelect.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
<Compile Include="IconExtractor.cs" />
8989
<Compile Include="Program.cs" />
9090
<Compile Include="Properties\AssemblyInfo.cs" />
91+
<Compile Include="UpdateChecker.cs" />
9192
<Compile Include="VButton.cs">
9293
<SubType>Component</SubType>
9394
</Compile>
@@ -119,7 +120,9 @@
119120
<DependentUpon>Resources.resx</DependentUpon>
120121
<DesignTime>True</DesignTime>
121122
</Compile>
122-
<None Include="app.config" />
123+
<None Include="app.config">
124+
<SubType>Designer</SubType>
125+
</None>
123126
<None Include="installer.nsi" />
124127
<None Include="Properties\Settings.settings">
125128
<Generator>SettingsSingleFileGenerator</Generator>
@@ -134,6 +137,7 @@
134137
<ItemGroup>
135138
<Content Include="bs.ico" />
136139
<Content Include="License.txt" />
140+
<None Include="Resources\new-icon.png" />
137141
<None Include="Resources\bitcoin.png" />
138142
<None Include="Resources\Button-help-icon.png" />
139143
</ItemGroup>

BrowserSelect/Form1.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ private void Form1_Load(object sender, EventArgs e)
4242
// create a wildcard rule for this domain (always button)
4343
_alwaysRule = generate_rule(Program.url);
4444

45+
// check for new version
46+
if (Settings.Default.last_version != "nope")
47+
{
48+
btn_help.BackgroundImage = Resources.update_available;
49+
btn_help.Click -= btn_help_Click;
50+
btn_help.Click += btn_update_click;
51+
}
4552
center_me();
4653
}
4754

@@ -285,5 +292,16 @@ private void btn_help_Click(object sender, EventArgs e)
285292
{
286293
(new frm_help_main()).ShowDialog();
287294
}
295+
296+
void btn_update_click(object sender, EventArgs e)
297+
{
298+
var lv = Settings.Default.last_version;
299+
var cv = Application.ProductVersion;
300+
cv = cv.Remove(cv.Length - 2);
301+
MessageBox.Show(String.Format(
302+
"New Update Available!\nCurrent Version: {1}\nLast Version: {0}" +
303+
"\nto Update download and install the new version from project's github.",
304+
lv, cv));
305+
}
288306
}
289307
}

BrowserSelect/Program.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Specialized;
44
using System.Linq;
55
using System.Text.RegularExpressions;
6+
using System.Threading.Tasks;
67
using System.Windows.Forms;
78
using BrowserSelect.Properties;
89

@@ -23,13 +24,21 @@ static void Main(string[] args)
2324
{
2425
Settings.Default.Upgrade();
2526
Settings.Default.UpdateSettings = false;
27+
Settings.Default.last_version = "nope";
2628
// to prevent nullreference in case settings file did not exist
2729
if (Settings.Default.HideBrowsers == null)
2830
Settings.Default.HideBrowsers = new StringCollection();
2931
if (Settings.Default.AutoBrowser == null)
3032
Settings.Default.AutoBrowser = new StringCollection();
3133
Settings.Default.Save();
3234
}
35+
// check for update
36+
if (Settings.Default.check_update != "nope" &&
37+
DateTime.Now.Subtract(time(Settings.Default.check_update)).TotalDays > 7)
38+
{
39+
var uc = new UpdateChecker();
40+
Task.Factory.StartNew(() => uc.check());
41+
}
3342
//checking if a url is being opened or app is ran from start menu (without arguments)
3443
if (args.Length > 0)
3544
{
@@ -73,6 +82,28 @@ static void Main(string[] args)
7382
Application.Run(new Form1());
7483
}
7584

85+
// from : http://stackoverflow.com/a/250400/1461004
86+
public static double time()
87+
{
88+
return time(DateTime.Now);
89+
}
90+
public static DateTime time(string unixTimeStamp)
91+
{
92+
return time(double.Parse(unixTimeStamp));
93+
}
94+
public static DateTime time(double unixTimeStamp)
95+
{
96+
// Unix timestamp is seconds past epoch
97+
System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
98+
dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToLocalTime();
99+
return dtDateTime;
100+
}
101+
public static double time(DateTime dateTime)
102+
{
103+
return (TimeZoneInfo.ConvertTimeToUtc(dateTime) -
104+
new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc)).TotalSeconds;
105+
}
106+
76107

77108
public static string Args2Str(List<string> args)
78109
{

BrowserSelect/Properties/Resources.Designer.cs

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

BrowserSelect/Properties/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,7 @@
124124
<data name="Button-help-icon" type="System.Resources.ResXFileRef, System.Windows.Forms">
125125
<value>..\Resources\Button-help-icon.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
126126
</data>
127+
<data name="update_available" type="System.Resources.ResXFileRef, System.Windows.Forms">
128+
<value>..\Resources\new-icon.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
129+
</data>
127130
</root>

BrowserSelect/Properties/Settings.Designer.cs

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

BrowserSelect/Properties/Settings.settings

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,11 @@
1111
<Setting Name="UpdateSettings" Type="System.Boolean" Scope="User">
1212
<Value Profile="(Default)">True</Value>
1313
</Setting>
14+
<Setting Name="check_update" Type="System.String" Scope="User">
15+
<Value Profile="(Default)">nope</Value>
16+
</Setting>
17+
<Setting Name="last_version" Type="System.String" Scope="User">
18+
<Value Profile="(Default)">nope</Value>
19+
</Setting>
1420
</Settings>
1521
</SettingsFile>
1.14 KB
Loading

BrowserSelect/UpdateChecker.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Net;
5+
using System.Text;
6+
using System.Windows.Forms;
7+
using System.Windows.Forms.VisualStyles;
8+
using BrowserSelect.Properties;
9+
10+
namespace BrowserSelect
11+
{
12+
class UpdateChecker
13+
{
14+
public String CVer => current_version;
15+
public String LVer => last_version;
16+
public Boolean Checked => init;
17+
public Boolean Updated => new_version();
18+
19+
private string current_version = "x";
20+
private string last_version = "x";
21+
private bool init = false;
22+
23+
public void check()
24+
{
25+
if (new_version())
26+
Settings.Default.last_version = last_version;
27+
if (Settings.Default.check_update != "nope")
28+
Settings.Default.check_update = Program.time().ToString();
29+
Settings.Default.Save();
30+
}
31+
string get_last_version()
32+
{
33+
// request to releases/latest redirects the user to /releases/tag.
34+
// since tag is the version number we can get latest version from Location header
35+
// and make a HEAD request instead of get to save bandwidth
36+
var req = (HttpWebRequest)WebRequest.Create("https://github.com/zumoshi/BrowserSelect/releases/latest");
37+
req.Method = "HEAD";
38+
req.AllowAutoRedirect = false;
39+
using (var res = req.GetResponse())
40+
{
41+
return res.Headers["Location"].Split('/').Last();
42+
}
43+
}
44+
45+
void get_versions()
46+
{
47+
try
48+
{
49+
last_version = get_last_version();
50+
current_version = ((Func<String, String>)((x) => x.Substring(0, x.Length - 2)))(Application.ProductVersion);
51+
init = true;
52+
}
53+
catch (Exception) { }
54+
}
55+
56+
bool new_version()
57+
{
58+
if (!init)
59+
get_versions();
60+
return last_version != current_version;
61+
}
62+
}
63+
}

BrowserSelect/app.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
<setting name="UpdateSettings" serializeAs="String">
1111
<value>True</value>
1212
</setting>
13+
<setting name="check_update" serializeAs="String">
14+
<value>nope</value>
15+
</setting>
16+
<setting name="last_version" serializeAs="String">
17+
<value>nope</value>
18+
</setting>
1319
</BrowserSelect.Properties.Settings>
1420
</userSettings>
1521
</configuration>

0 commit comments

Comments
 (0)