Skip to content

Commit

Permalink
added an update checker
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
zumoshi committed Sep 2, 2016
1 parent 11c7603 commit cf93c03
Show file tree
Hide file tree
Showing 14 changed files with 270 additions and 12 deletions.
6 changes: 5 additions & 1 deletion BrowserSelect/BrowserSelect.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
<Compile Include="IconExtractor.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UpdateChecker.cs" />
<Compile Include="VButton.cs">
<SubType>Component</SubType>
</Compile>
Expand Down Expand Up @@ -119,7 +120,9 @@
<DependentUpon>Resources.resx</DependentUpon>
<DesignTime>True</DesignTime>
</Compile>
<None Include="app.config" />
<None Include="app.config">
<SubType>Designer</SubType>
</None>
<None Include="installer.nsi" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
Expand All @@ -134,6 +137,7 @@
<ItemGroup>
<Content Include="bs.ico" />
<Content Include="License.txt" />
<None Include="Resources\new-icon.png" />
<None Include="Resources\bitcoin.png" />
<None Include="Resources\Button-help-icon.png" />
</ItemGroup>
Expand Down
18 changes: 18 additions & 0 deletions BrowserSelect/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ private void Form1_Load(object sender, EventArgs e)
// create a wildcard rule for this domain (always button)
_alwaysRule = generate_rule(Program.url);

// check for new version
if (Settings.Default.last_version != "nope")
{
btn_help.BackgroundImage = Resources.update_available;
btn_help.Click -= btn_help_Click;
btn_help.Click += btn_update_click;
}
center_me();
}

Expand Down Expand Up @@ -285,5 +292,16 @@ private void btn_help_Click(object sender, EventArgs e)
{
(new frm_help_main()).ShowDialog();
}

void btn_update_click(object sender, EventArgs e)
{
var lv = Settings.Default.last_version;
var cv = Application.ProductVersion;
cv = cv.Remove(cv.Length - 2);
MessageBox.Show(String.Format(
"New Update Available!\nCurrent Version: {1}\nLast Version: {0}" +
"\nto Update download and install the new version from project's github.",
lv, cv));
}
}
}
31 changes: 31 additions & 0 deletions BrowserSelect/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Specialized;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using BrowserSelect.Properties;

Expand All @@ -23,13 +24,21 @@ static void Main(string[] args)
{
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)
{
Expand Down Expand Up @@ -73,6 +82,28 @@ static void Main(string[] args)
Application.Run(new Form1());
}

// from : http://stackoverflow.com/a/250400/1461004
public static double time()
{
return time(DateTime.Now);
}
public static DateTime time(string unixTimeStamp)
{
return time(double.Parse(unixTimeStamp));
}
public static DateTime time(double unixTimeStamp)
{
// Unix timestamp is seconds past epoch
System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToLocalTime();
return dtDateTime;
}
public static double time(DateTime dateTime)
{
return (TimeZoneInfo.ConvertTimeToUtc(dateTime) -
new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc)).TotalSeconds;
}


public static string Args2Str(List<string> args)
{
Expand Down
10 changes: 10 additions & 0 deletions BrowserSelect/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions BrowserSelect/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,7 @@
<data name="Button-help-icon" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\Button-help-icon.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="update_available" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\new-icon.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
</root>
24 changes: 24 additions & 0 deletions BrowserSelect/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions BrowserSelect/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,11 @@
<Setting Name="UpdateSettings" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
<Setting Name="check_update" Type="System.String" Scope="User">
<Value Profile="(Default)">nope</Value>
</Setting>
<Setting Name="last_version" Type="System.String" Scope="User">
<Value Profile="(Default)">nope</Value>
</Setting>
</Settings>
</SettingsFile>
Binary file added BrowserSelect/Resources/new-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
63 changes: 63 additions & 0 deletions BrowserSelect/UpdateChecker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using BrowserSelect.Properties;

namespace BrowserSelect
{
class UpdateChecker
{
public String CVer => current_version;
public String LVer => last_version;
public Boolean Checked => init;
public Boolean Updated => new_version();

private string current_version = "x";
private string last_version = "x";
private bool init = false;

public void check()
{
if (new_version())
Settings.Default.last_version = last_version;
if (Settings.Default.check_update != "nope")
Settings.Default.check_update = Program.time().ToString();
Settings.Default.Save();
}
string get_last_version()
{
// request to releases/latest redirects the user to /releases/tag.
// since tag is the version number we can get latest version from Location header
// and make a HEAD request instead of get to save bandwidth
var req = (HttpWebRequest)WebRequest.Create("https://github.com/zumoshi/BrowserSelect/releases/latest");
req.Method = "HEAD";
req.AllowAutoRedirect = false;
using (var res = req.GetResponse())
{
return res.Headers["Location"].Split('/').Last();
}
}

void get_versions()
{
try
{
last_version = get_last_version();
current_version = ((Func<String, String>)((x) => x.Substring(0, x.Length - 2)))(Application.ProductVersion);
init = true;
}
catch (Exception) { }
}

bool new_version()
{
if (!init)
get_versions();
return last_version != current_version;
}
}
}
6 changes: 6 additions & 0 deletions BrowserSelect/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
<setting name="UpdateSettings" serializeAs="String">
<value>True</value>
</setting>
<setting name="check_update" serializeAs="String">
<value>nope</value>
</setting>
<setting name="last_version" serializeAs="String">
<value>nope</value>
</setting>
</BrowserSelect.Properties.Settings>
</userSettings>
</configuration>
60 changes: 51 additions & 9 deletions BrowserSelect/frm_settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cf93c03

Please sign in to comment.