From feee9b30587e63814c9ac30928d02ffd4554d0a1 Mon Sep 17 00:00:00 2001 From: Pavel Karasov Date: Wed, 7 Mar 2012 11:56:49 +0400 Subject: [PATCH] Extract Updateversion code from MainWindow --- Client/CheckUpdate.cs | 45 +++++++++++++++++++++++++++ Client/Client.csproj | 1 + Client/MainWindow.xaml.cs | 64 +++++++++------------------------------ 3 files changed, 61 insertions(+), 49 deletions(-) create mode 100644 Client/CheckUpdate.cs diff --git a/Client/CheckUpdate.cs b/Client/CheckUpdate.cs new file mode 100644 index 00000000..ea47a6e5 --- /dev/null +++ b/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); + } + } + } +} diff --git a/Client/Client.csproj b/Client/Client.csproj index 076bdd28..d3177763 100644 --- a/Client/Client.csproj +++ b/Client/Client.csproj @@ -78,6 +78,7 @@ MSBuild:Compile Designer + FilterDialog.xaml diff --git a/Client/MainWindow.xaml.cs b/Client/MainWindow.xaml.cs index 05c00abf..d69dc0da 100644 --- a/Client/MainWindow.xaml.cs +++ b/Client/MainWindow.xaml.cs @@ -56,6 +56,7 @@ enum SortType TrayMainWindows _tray; HotKeyMainWindows _hotkey; ObserverChangeFile _changefile; + CheckUpdate _checkupdate; WindowLocation _previousWindowLocaiton; @@ -75,6 +76,11 @@ public MainWindow() _changefile = new ObserverChangeFile(); _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"); // migrate the user settings from the previous version, if necessary @@ -103,29 +109,12 @@ public MainWindow() Log.Error(msg, ex); MessageBox.Show(ex.Message, msg, MessageBoxButton.OK); } - - ThreadPool.QueueUserWorkItem(x => CheckForUpdates()); + } #region private methods - private void HideUnHideWindow() - { - 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() + private void Reload() { try { @@ -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(ShowUpdateMenu), version); - } - } - catch (Exception ex) - { - Log.Error("Error checking for updates", ex); - } - - } - private void ShowUpdateMenu(string version) { - this.UpdateMenu.Header = "New version: " + version; - this.UpdateMenu.Visibility = Visibility.Visible; + var assemblyVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString(); + if (version != assemblyVersion) + { + this.UpdateMenu.Header = "New version: " + version; + this.UpdateMenu.Visibility = Visibility.Visible; + } } #endregion @@ -749,7 +715,7 @@ private void Sort_Alphabetical(object sender, RoutedEventArgs e) #region Update notification private void Get_Update(object sender, RoutedEventArgs e) { - Process.Start("https://github.com/benrhughes/todotxt.net/downloads"); + Process.Start(CheckUpdate.updateClientUrl); } #endregion