Browse Source

Implemented check for updates.

pull/1/head
Daniel Grunwald 14 years ago
parent
commit
20c0542768
  1. 97
      ILSpy/AboutPage.cs

97
ILSpy/AboutPage.cs

@ -4,10 +4,14 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Input; using System.Windows.Input;
using System.Xml;
using System.Xml.Linq;
using ICSharpCode.Decompiler; using ICSharpCode.Decompiler;
using ICSharpCode.ILSpy.TextView; using ICSharpCode.ILSpy.TextView;
@ -15,6 +19,8 @@ namespace ICSharpCode.ILSpy
{ {
static class AboutPage static class AboutPage
{ {
static readonly Uri UpdateUrl = new Uri("http://www.ilspy.net/updates.xml");
static AvailableVersionInfo latestAvailableVersion; static AvailableVersionInfo latestAvailableVersion;
public static void Display(DecompilerTextView textView) public static void Display(DecompilerTextView textView)
@ -24,34 +30,22 @@ namespace ICSharpCode.ILSpy
output.AddUIElement( output.AddUIElement(
delegate { delegate {
StackPanel stackPanel = new StackPanel(); StackPanel stackPanel = new StackPanel();
stackPanel.HorizontalAlignment = HorizontalAlignment.Center;
stackPanel.Orientation = Orientation.Horizontal; stackPanel.Orientation = Orientation.Horizontal;
if (latestAvailableVersion == null) { if (latestAvailableVersion == null) {
Button button = new Button(); AddUpdateCheckButton(stackPanel, textView);
button.Content = "Check for updates";
button.Cursor = Cursors.Arrow;
stackPanel.Children.Add(button);
button.Click += delegate {
button.Content = "Checking...";
button.IsEnabled = false;
GetLatestVersion().ContinueWith(
delegate (Task<AvailableVersionInfo> task) {
try {
latestAvailableVersion = task.Result;
stackPanel.Children.Clear();
ShowAvailableVersion(latestAvailableVersion, stackPanel);
} catch (Exception ex) {
AvalonEditTextOutput exceptionOutput = new AvalonEditTextOutput();
exceptionOutput.WriteLine(ex.ToString());
textView.Show(exceptionOutput);
}
}, TaskScheduler.FromCurrentSynchronizationContext());
};
} else { } else {
// we already retrieved the latest version sometime earlier // we already retrieved the latest version sometime earlier
ShowAvailableVersion(latestAvailableVersion, stackPanel); ShowAvailableVersion(latestAvailableVersion, stackPanel);
} }
return stackPanel; CheckBox checkBox = new CheckBox();
checkBox.Margin = new Thickness(4);
checkBox.Content = "Automatically check for updates every week";
return new StackPanel {
Margin = new Thickness(0, 4, 0, 0),
Cursor = Cursors.Arrow,
Children = { stackPanel, checkBox }
};
}); });
output.WriteLine(); output.WriteLine();
output.WriteLine(); output.WriteLine();
@ -65,13 +59,44 @@ namespace ICSharpCode.ILSpy
textView.Show(output); textView.Show(output);
} }
static void AddUpdateCheckButton(StackPanel stackPanel, DecompilerTextView textView)
{
Button button = new Button();
button.Content = "Check for updates";
button.Cursor = Cursors.Arrow;
stackPanel.Children.Add(button);
button.Click += delegate {
button.Content = "Checking...";
button.IsEnabled = false;
GetLatestVersion().ContinueWith(
delegate (Task<AvailableVersionInfo> task) {
try {
latestAvailableVersion = task.Result;
stackPanel.Children.Clear();
ShowAvailableVersion(latestAvailableVersion, stackPanel);
} catch (Exception ex) {
AvalonEditTextOutput exceptionOutput = new AvalonEditTextOutput();
exceptionOutput.WriteLine(ex.ToString());
textView.Show(exceptionOutput);
}
}, TaskScheduler.FromCurrentSynchronizationContext());
};
}
static void ShowAvailableVersion(AvailableVersionInfo availableVersion, StackPanel stackPanel) static void ShowAvailableVersion(AvailableVersionInfo availableVersion, StackPanel stackPanel)
{ {
Version currentVersion = new Version(RevisionClass.Major + "." + RevisionClass.Minor + "." + RevisionClass.Build + "." + RevisionClass.Revision); Version currentVersion = new Version(RevisionClass.Major + "." + RevisionClass.Minor + "." + RevisionClass.Build + "." + RevisionClass.Revision);
if (currentVersion == availableVersion.Version) { if (currentVersion == availableVersion.Version) {
stackPanel.Children.Add(new Image { Width = 16, Height = 16, Source = Images.OK, Margin = new Thickness(4,0,4,0) });
stackPanel.Children.Add( stackPanel.Children.Add(
new TextBlock { Text = "You are using the latest release.", new Image {
Width = 16, Height = 16,
Source = Images.OK,
Margin = new Thickness(4,0,4,0)
});
stackPanel.Children.Add(
new TextBlock {
Text = "You are using the latest release.",
VerticalAlignment = VerticalAlignment.Bottom VerticalAlignment = VerticalAlignment.Bottom
}); });
} else if (currentVersion < availableVersion.Version) { } else if (currentVersion < availableVersion.Version) {
@ -93,15 +118,31 @@ namespace ICSharpCode.ILSpy
stackPanel.Children.Add(button); stackPanel.Children.Add(button);
} }
} else { } else {
stackPanel.Children.Add(new TextBlock { Text = "You are using a nightly builds newer than the latest release." }); stackPanel.Children.Add(new TextBlock { Text = "You are using a nightly build newer than the latest release." });
} }
} }
static Task<AvailableVersionInfo> GetLatestVersion() static Task<AvailableVersionInfo> GetLatestVersion()
{ {
TaskCompletionSource<AvailableVersionInfo> tcs = new TaskCompletionSource<AvailableVersionInfo>(); var tcs = new TaskCompletionSource<AvailableVersionInfo>();
tcs.SetException(new NotImplementedException()); WebClient wc = new WebClient();
//tcs.SetResult(new AvailableVersionInfo { Version = new Version(0,2,0,37), DownloadUrl = "http://www.ilspy.net/" }); wc.DownloadDataCompleted += delegate(object sender, DownloadDataCompletedEventArgs e) {
if (e.Error != null) {
tcs.SetException(e.Error);
} else {
try {
XDocument doc = XDocument.Load(new MemoryStream(e.Result));
var bands = doc.Root.Elements("band");
var currentBand = bands.FirstOrDefault(b => (string)b.Attribute("id") == "stable") ?? bands.First();
Version version = new Version((string)currentBand.Element("latestVersion"));
string url = (string)currentBand.Element("downloadUrl");
tcs.SetResult(new AvailableVersionInfo { Version = version, DownloadUrl = url });
} catch (Exception ex) {
tcs.SetException(ex);
}
}
};
wc.DownloadDataAsync(UpdateUrl);
return tcs.Task; return tcs.Task;
} }

Loading…
Cancel
Save