Skip to content

Commit

Permalink
Added event to parse custom AppCast file. (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
integral-llc authored and ravibpatel committed Oct 26, 2017
1 parent e74e519 commit 23ace4e
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 72 deletions.
213 changes: 147 additions & 66 deletions AutoUpdater.NET/AutoUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,17 @@ public static class AutoUpdater
/// </summary>
public static event CheckForUpdateEventHandler CheckForUpdateEvent;

/// <summary>
/// A delegate type for hooking up parsing logic.
/// </summary>
/// <param name="args">An object containing the AppCast file received from server.</param>
public delegate void ParseUpdateAppInfoHandler(ParseUpdateInformationEventArgs args);

/// <summary>
/// An event that clients can use to be notified whenever the AppCast file needs parsing.
/// </summary>
public static event ParseUpdateAppInfoHandler ParseUpdateInfoEvent;

/// <summary>
/// Start checking for new version of application and display dialog to the user if update is available.
/// </summary>
Expand Down Expand Up @@ -276,90 +287,128 @@ private static void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
e.Cancel = false;
return;
}
UpdateInfoEventArgs args = null;
if (ParseUpdateInfoEvent != null)
{
using (Stream appCastStream = webResponse.GetResponseStream())
{
if (appCastStream != null)
{
using (StreamReader streamReader = new StreamReader(appCastStream))
{
string data = streamReader.ReadToEnd();
ParseUpdateInformationEventArgs parseArgs = new ParseUpdateInformationEventArgs(data);
ParseUpdateInfoEvent(parseArgs);

XmlDocument receivedAppCastDocument;

using (Stream appCastStream = webResponse.GetResponseStream())
if (parseArgs.UpdateInfo != null)
{
CurrentVersion = parseArgs.UpdateInfo.CurrentVersion;
if (CurrentVersion == null)
{
e.Cancel = false;
webResponse.Close();
return;
}
ChangeLogURL = GetURL(webResponse.ResponseUri, parseArgs.UpdateInfo.ChangelogURL);
DownloadURL = GetURL(webResponse.ResponseUri, parseArgs.UpdateInfo.DownloadURL);
Mandatory = parseArgs.UpdateInfo.Mandatory;
args = parseArgs.UpdateInfo;
}
else
{
e.Cancel = false;
webResponse.Close();
return;
}
}
}
}
}
else
{
receivedAppCastDocument = new XmlDocument();
XmlDocument receivedAppCastDocument;

if (appCastStream != null)
using (Stream appCastStream = webResponse.GetResponseStream())
{
try
receivedAppCastDocument = new XmlDocument();

if (appCastStream != null)
{
receivedAppCastDocument.Load(appCastStream);
try
{
receivedAppCastDocument.Load(appCastStream);
}
catch (XmlException)
{
e.Cancel = false;
webResponse.Close();
return;
}
}
catch (XmlException)
else
{
e.Cancel = false;
webResponse.Close();
return;
}
}
else
{
e.Cancel = false;
webResponse.Close();
return;
}
}

XmlNodeList appCastItems = receivedAppCastDocument.SelectNodes("item");
XmlNodeList appCastItems = receivedAppCastDocument.SelectNodes("item");

if (appCastItems != null)
{
foreach (XmlNode item in appCastItems)
if (appCastItems != null)
{
XmlNode appCastVersion = item.SelectSingleNode("version");
if (appCastVersion != null)
foreach (XmlNode item in appCastItems)
{
String appVersion = appCastVersion.InnerText;
CurrentVersion = new Version(appVersion);

if (CurrentVersion == null)
XmlNode appCastVersion = item.SelectSingleNode("version");
if (appCastVersion != null)
{
webResponse.Close();
return;
}
}
else
continue;
String appVersion = appCastVersion.InnerText;
CurrentVersion = new Version(appVersion);

XmlNode appCastChangeLog = item.SelectSingleNode("changelog");
if (CurrentVersion == null)
{
webResponse.Close();
return;
}
}
else
continue;
XmlNode appCastChangeLog = item.SelectSingleNode("changelog");

ChangeLogURL = GetURL(webResponse.ResponseUri, appCastChangeLog);
ChangeLogURL = GetURL(webResponse.ResponseUri, appCastChangeLog?.InnerText);

XmlNode appCastUrl = item.SelectSingleNode("url");
XmlNode appCastUrl = item.SelectSingleNode("url");

DownloadURL = GetURL(webResponse.ResponseUri, appCastUrl);
DownloadURL = GetURL(webResponse.ResponseUri, appCastUrl?.InnerText);

XmlNode mandatory = item.SelectSingleNode("mandatory");
XmlNode mandatory = item.SelectSingleNode("mandatory");

if (mandatory != null)
{
Mandatory = Boolean.Parse(mandatory.InnerText);
if (Mandatory)
if (mandatory != null)
{
ShowRemindLaterButton = false;
ShowSkipButton = false;
Mandatory = Boolean.Parse(mandatory.InnerText);
if (Mandatory)
{
ShowRemindLaterButton = false;
ShowSkipButton = false;
}
}
}

if (IntPtr.Size.Equals(8))
{
XmlNode appCastUrl64 = item.SelectSingleNode("url64");
if (IntPtr.Size.Equals(8))
{
XmlNode appCastUrl64 = item.SelectSingleNode("url64");

var downloadURL64 = GetURL(webResponse.ResponseUri, appCastUrl64);
var downloadURL64 = GetURL(webResponse.ResponseUri, appCastUrl64?.InnerText);

if (!string.IsNullOrEmpty(downloadURL64))
{
DownloadURL = downloadURL64;
if (!string.IsNullOrEmpty(downloadURL64))
{
DownloadURL = downloadURL64;
}
}
}
}
}

webResponse.Close();
webResponse.Close();
}

if (!Mandatory)
{
Expand Down Expand Up @@ -408,34 +457,36 @@ private static void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
}
}

var args = new UpdateInfoEventArgs
if (args == null)
{
DownloadURL = DownloadURL,
ChangelogURL = ChangeLogURL,
CurrentVersion = CurrentVersion,
InstalledVersion = InstalledVersion,
IsUpdateAvailable = CurrentVersion > InstalledVersion
};
args = new UpdateInfoEventArgs
{
DownloadURL = DownloadURL,
ChangelogURL = ChangeLogURL,
CurrentVersion = CurrentVersion,
Mandatory = Mandatory
};
}
args.IsUpdateAvailable = CurrentVersion > InstalledVersion;
args.InstalledVersion = InstalledVersion;

e.Cancel = false;
e.Result = args;
}

private static string GetURL(Uri baseUri, XmlNode xmlNode)
private static string GetURL(Uri baseUri, String url)
{
var temp = xmlNode?.InnerText ?? "";

if (!string.IsNullOrEmpty(temp) && Uri.IsWellFormedUriString(temp, UriKind.Relative))
if (!string.IsNullOrEmpty(url) && Uri.IsWellFormedUriString(url, UriKind.Relative))
{
Uri uri = new Uri(baseUri, temp);
Uri uri = new Uri(baseUri, url);

if (uri.IsAbsoluteUri)
{
temp = uri.AbsoluteUri;
url = uri.AbsoluteUri;
}
}

return temp;
return url;
}

private static void Exit()
Expand Down Expand Up @@ -558,5 +609,35 @@ public class UpdateInfoEventArgs : EventArgs
/// Returns version of the application currently installed on the user's PC.
/// </summary>
public Version InstalledVersion { get; set; }

/// <summary>
/// Shows if the update is required or optional.
/// </summary>
public bool Mandatory { get; set; }
}

/// <summary>
/// An object of this class contains the AppCast file received from server..
/// </summary>
public class ParseUpdateInformationEventArgs : EventArgs
{
/// <summary>
/// Remote data received from the AppCast file.
/// </summary>
public string RemoteData { get; }

/// <summary>
/// Set this object with values received from the AppCast file.
/// </summary>
public UpdateInfoEventArgs UpdateInfo { get; set; }

/// <summary>
/// An object containing the AppCast file received from server.
/// </summary>
/// <param name="remoteData">A string containing remote data received from the AppCast file.</param>
public ParseUpdateInformationEventArgs(string remoteData)
{
RemoteData = remoteData;
}
}
}
5 changes: 5 additions & 0 deletions AutoUpdaterTest/AutoUpdaterTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Deployment" />
Expand Down Expand Up @@ -63,6 +67,7 @@
<DesignTime>True</DesignTime>
</Compile>
<None Include="app.config" />
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand Down
46 changes: 40 additions & 6 deletions AutoUpdaterTest/FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System.Windows.Forms;
using AutoUpdaterDotNET;
using AutoUpdaterTest.Properties;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace AutoUpdaterTest
{
Expand All @@ -21,6 +23,11 @@ public FormMain()

private void FormMain_Load(object sender, EventArgs e)
{
//Uncomment below lines to handle parsing logic of non XML AppCast file.

//AutoUpdater.Start("http://rbsoft.org/updates/AutoUpdaterTest.json");
//AutoUpdater.ParseUpdateInfoEvent += AutoUpdaterOnParseUpdateInfoEvent;

//Uncomment below line to see russian version

//AutoUpdater.CurrentCulture = CultureInfo.CreateSpecificCulture("ru");
Expand Down Expand Up @@ -77,19 +84,46 @@ private void FormMain_Load(object sender, EventArgs e)
//timer.Start();
}

private void AutoUpdaterOnParseUpdateInfoEvent(ParseUpdateInformationEventArgs args)
{
dynamic json = JsonConvert.DeserializeObject(args.RemoteData);
args.UpdateInfo = new UpdateInfoEventArgs
{
CurrentVersion = json.version,
ChangelogURL = json.changelog,
Mandatory = json.mandatory,
DownloadURL = json.url
};
}

private void AutoUpdaterOnCheckForUpdateEvent(UpdateInfoEventArgs args)
{
if (args != null)
{
if (args.IsUpdateAvailable)
{
var dialogResult =
MessageBox.Show(
$@"There is new version {args.CurrentVersion} available. You are using version {args.InstalledVersion}. Do you want to update the application now?", @"Update Available",
MessageBoxButtons.YesNo,
MessageBoxIcon.Information);
DialogResult dialogResult;
if (args.Mandatory)
{
dialogResult =
MessageBox.Show(
$@"There is new version {args.CurrentVersion} available. You are using version {args.InstalledVersion}. This is required update. Press Ok to begin updating the application.", @"Update Available",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
else
{
dialogResult =
MessageBox.Show(
$@"There is new version {args.CurrentVersion} available. You are using version {
args.InstalledVersion
}. Do you want to update the application now?", @"Update Available",
MessageBoxButtons.YesNo,
MessageBoxIcon.Information);
}


if (dialogResult.Equals(DialogResult.Yes))
if (dialogResult.Equals(DialogResult.Yes) || dialogResult.Equals(DialogResult.OK))
{
try
{
Expand Down
4 changes: 4 additions & 0 deletions AutoUpdaterTest/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net40" />
</packages>

0 comments on commit 23ace4e

Please sign in to comment.