Skip to content

Commit

Permalink
Added: Checking for updates (manually or optionally on start).
Browse files Browse the repository at this point in the history
Updated: CHANGELOG.
  • Loading branch information
margru committed Jan 18, 2012
1 parent f4f1d1e commit 329785f
Show file tree
Hide file tree
Showing 10 changed files with 309 additions and 107 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ CHANGELOG for "XBMC on iMON Display"

v0.1.9.2 Beta: January 18, 2012
-------------------------------
- General
[+] Checking for updates available
- XBMC
[x] Control messages displayed correctly and with 1000ms timeout now (Issue #13)

Expand Down
12 changes: 12 additions & 0 deletions Properties/Settings.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 Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,8 @@
<Setting Name="XbmcOnConnectedText" Type="System.String" Scope="User">
<Value Profile="(Default)">Connected</Value>
</Setting>
<Setting Name="GeneralCheckForUpdateOnStart" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
</Settings>
</SettingsFile>
146 changes: 146 additions & 0 deletions Updating.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Reflection;
using System.Windows.Forms;

namespace iMon.XBMC
{
internal static class Updating
{
# region Constants

private const string ChangelogFile = "CHANGELOG";
private const string ChangelogUrl = "https://raw.github.com/BlackMan82/xbmc-on-imon/master/";
private const string UpdateBaseUrl = "https://github.com/downloads/BlackMan82/";
private const string ProjectName = "xbmc-on-imon";
//private const string VersionNumberIndicator = "*";
private const string VersionNumberIndicator = "v";
//private const int VersionNumberPosition = 1;
private const int VersionNumberPosition = 0;

private const string LoggingArea = "Update";

# endregion

# region Private variables

private static Version currentVersion;
private static Version newestVersion;

# endregion

# region Constructor

static Updating()
{
Updating.currentVersion = new Version(Assembly.GetExecutingAssembly().GetName().Version.ToString());

// For testing purposes only
//Updating.currentVersion = new Version("0.1.8.0");
}

# endregion

# region Private functions

private static string getDownloadUrl()
{
return UpdateBaseUrl + ProjectName + "/" + ProjectName + "-v" + newestVersion.ToString() + ".zip";
}

private static bool getNewestVersionNumber()
{
WebResponse response;
string sourceUrl = ChangelogUrl + ChangelogFile;
string line;
bool result = false;
WebRequest request = WebRequest.Create(sourceUrl);

Logging.Log(LoggingArea, "Source for updates: " + sourceUrl);

try
{
response = request.GetResponse();
}
catch (WebException ex)
{
Logging.Error(LoggingArea, ex.Message);
return result;
}

Logging.Log(LoggingArea, "CHANGELOG found");

StreamReader reader = new StreamReader(response.GetResponseStream());
while (!reader.EndOfStream)
{
line = reader.ReadLine();
if (line.StartsWith(VersionNumberIndicator))
{
Logging.Log(LoggingArea, "Version number found, trying to parse: " + line);
try
{
string versionNumber = line.Split(' ')[VersionNumberPosition].Substring(1);
Version.TryParse(versionNumber, out Updating.newestVersion);
}
catch (Exception)
{
result = false;
Logging.Error(LoggingArea, "Version number parsing failed");
continue;
}

result = true;
Logging.Log(LoggingArea, "Latest version number parsed: " + newestVersion);
break;
}
}

if (!result)
Logging.Error(LoggingArea, "Version number not found!");

return result;

//Updating.newestVersion = new Version(reader.ReadLine()); // the first line in the file is a version number
//Updating.dNewestVersion = double.Parse(Updating.sNewestVersion, CultureInfo.InvariantCulture);
//string Link = reader.ReadLine(); // the second line in the file is a link to the latest update
}

# endregion

# region Public functions

public static bool update(bool automaticUpdate)
{
bool updateIsAvailable = false;

Logging.Log(LoggingArea, "Checking for update");

if (!getNewestVersionNumber())
return false;

if (Updating.newestVersion > Updating.currentVersion)
{
updateIsAvailable = true;
Logging.Log(LoggingArea, "Update available");
if (MessageBox.Show("An update is available! (" + Updating.newestVersion.ToString() + ")\nDo you wish to download it?", Assembly.GetExecutingAssembly().GetName().Name + ": Update available", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
{
Process.Start(Updating.getDownloadUrl());
}

}
else
{
updateIsAvailable = false;
Logging.Log(LoggingArea, "Update not available");
if (!automaticUpdate)
MessageBox.Show("You are using the most up to date version!", Assembly.GetExecutingAssembly().GetName().Name + ": Update not available");
}

return updateIsAvailable;
}

# endregion
}
}
Loading

0 comments on commit 329785f

Please sign in to comment.