Skip to content

Commit

Permalink
Extract Updateversion code from MainWindow
Browse files Browse the repository at this point in the history
  • Loading branch information
ejsmile committed Mar 7, 2012
1 parent ff547aa commit feee9b3
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 49 deletions.
45 changes: 45 additions & 0 deletions Client/CheckUpdate.cs
@@ -0,0 +1,45 @@
using System;
using System.Threading;
using System.Xml;
using ToDoLib;
using System.Windows;

namespace Client
{
class CheckUpdate
{
public const string updateXMLUrl = @"https://raw.github.com/benrhughes/todotxt.net/master/Updates.xml";
public const string updateClientUrl = @"https://github.com/benrhughes/todotxt.net/downloads";

public delegate void CheckUpdateVershion(string version);
public event CheckUpdateVershion OnCheckedUpdateVershion;

private XmlDocument xDoc;

public CheckUpdate()
{
xDoc = new XmlDocument();
}

public void Check()
{
ThreadPool.QueueUserWorkItem(x => CheckForUpdates());
}

private void CheckForUpdates()
{
try
{
xDoc.Load(new XmlTextReader(updateXMLUrl));

var version = xDoc.SelectSingleNode("//version").InnerText;
var changelog = xDoc.SelectSingleNode("//changelog").InnerText;
OnCheckedUpdateVershion(version);
}
catch (Exception ex)
{
Log.Error("Error checking for updates", ex);
}
}
}
}
1 change: 1 addition & 0 deletions Client/Client.csproj
Expand Up @@ -78,6 +78,7 @@
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType> <SubType>Designer</SubType>
</ApplicationDefinition> </ApplicationDefinition>
<Compile Include="CheckUpdate.cs" />
<Compile Include="FilterDialog.xaml.cs"> <Compile Include="FilterDialog.xaml.cs">
<DependentUpon>FilterDialog.xaml</DependentUpon> <DependentUpon>FilterDialog.xaml</DependentUpon>
</Compile> </Compile>
Expand Down
64 changes: 15 additions & 49 deletions Client/MainWindow.xaml.cs
Expand Up @@ -56,6 +56,7 @@ enum SortType
TrayMainWindows _tray; TrayMainWindows _tray;
HotKeyMainWindows _hotkey; HotKeyMainWindows _hotkey;
ObserverChangeFile _changefile; ObserverChangeFile _changefile;
CheckUpdate _checkupdate;


WindowLocation _previousWindowLocaiton; WindowLocation _previousWindowLocaiton;


Expand All @@ -75,6 +76,11 @@ public MainWindow()
_changefile = new ObserverChangeFile(); _changefile = new ObserverChangeFile();
_changefile.OnFileTaskListChange += () => Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate() { this.Refresh(); })); _changefile.OnFileTaskListChange += () => Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate() { this.Refresh(); }));


//CheckUpdate new version
_checkupdate = new CheckUpdate();
_checkupdate.OnCheckedUpdateVershion += (string version) => Dispatcher.BeginInvoke(new CheckUpdate.CheckUpdateVershion(this.ShowUpdateMenu), version);
_checkupdate.Check();

webBrowser1.Navigate("about:blank"); webBrowser1.Navigate("about:blank");


// migrate the user settings from the previous version, if necessary // migrate the user settings from the previous version, if necessary
Expand Down Expand Up @@ -103,29 +109,12 @@ public MainWindow()
Log.Error(msg, ex); Log.Error(msg, ex);
MessageBox.Show(ex.Message, msg, MessageBoxButton.OK); MessageBox.Show(ex.Message, msg, MessageBoxButton.OK);
} }


ThreadPool.QueueUserWorkItem(x => CheckForUpdates());
} }


#region private methods #region private methods


private void HideUnHideWindow() private void Reload()
{
if (this.WindowState == WindowState.Minimized)
{
this.Show();
this.Topmost = true;
this.Activate();
this.WindowState = WindowState.Normal;
}
else
{
this.WindowState = WindowState.Minimized;
this.Hide();
}
}

private void Reload()
{ {
try try
{ {
Expand Down Expand Up @@ -417,37 +406,14 @@ private void FilterAndSort(SortType sort)
} }
} }


private void CheckForUpdates()
{
const string updateXMLUrl = @"https://raw.github.com/benrhughes/todotxt.net/master/Updates.xml";

var xDoc = new XmlDocument();

try
{
xDoc.Load(new XmlTextReader(updateXMLUrl));

var version = xDoc.SelectSingleNode("//version").InnerText;
var changelog = xDoc.SelectSingleNode("//changelog").InnerText;

var assemblyVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();

if (version != assemblyVersion)
{
Dispatcher.Invoke(new Action<string>(ShowUpdateMenu), version);
}
}
catch (Exception ex)
{
Log.Error("Error checking for updates", ex);
}

}

private void ShowUpdateMenu(string version) private void ShowUpdateMenu(string version)
{ {
this.UpdateMenu.Header = "New version: " + version; var assemblyVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
this.UpdateMenu.Visibility = Visibility.Visible; if (version != assemblyVersion)
{
this.UpdateMenu.Header = "New version: " + version;
this.UpdateMenu.Visibility = Visibility.Visible;
}
} }
#endregion #endregion


Expand Down Expand Up @@ -749,7 +715,7 @@ private void Sort_Alphabetical(object sender, RoutedEventArgs e)
#region Update notification #region Update notification
private void Get_Update(object sender, RoutedEventArgs e) private void Get_Update(object sender, RoutedEventArgs e)
{ {
Process.Start("https://github.com/benrhughes/todotxt.net/downloads"); Process.Start(CheckUpdate.updateClientUrl);
} }
#endregion #endregion


Expand Down

0 comments on commit feee9b3

Please sign in to comment.