diff ServerMonitor/Forms/ServerSummaryForm.cs @ 10:9e77c0dccb66

Add update checker
author Brad Greco <brad@bgreco.net>
date Mon, 08 Apr 2019 21:31:03 -0400
parents 052aa62cb42a
children 75ca86e0862c
line wrap: on
line diff
--- a/ServerMonitor/Forms/ServerSummaryForm.cs	Mon Apr 08 21:29:54 2019 -0400
+++ b/ServerMonitor/Forms/ServerSummaryForm.cs	Mon Apr 08 21:31:03 2019 -0400
@@ -1,4 +1,7 @@
-using ServerMonitorApp.Properties;
+using NAppUpdate.Framework;
+using NAppUpdate.Framework.Sources;
+using NAppUpdate.Framework.Tasks;
+using ServerMonitorApp.Properties;
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
@@ -68,6 +71,7 @@
             monitor.CheckStatusChanged += Monitor_CheckStatusChanged;
             RefreshDisplay();
             CollectPrivateKeyPasswords();
+            CheckForUpdate();
         }
 
         private ServerForm ShowServerForm(Server server, bool activate = true)
@@ -287,5 +291,64 @@
             else if (e.ClickedItem == ExitMenuItem)
                 Application.Exit();
         }
+
+        private void CheckForUpdate()
+        {
+            //System.Threading.Thread.Sleep(5000);
+            UpdateManager manager = UpdateManager.Instance;
+            manager.ReinstateIfRestarted();
+            manager.UpdateSource = new SimpleWebSource(@"c:\temp\feed.xml");
+            if (manager.State == UpdateManager.UpdateProcessState.NotChecked)
+                manager.BeginCheckForUpdates(CheckForUpdatesCallback, null);
+        }
+
+        private void CheckForUpdatesCallback(IAsyncResult result)
+        {
+            UpdateManager manager = UpdateManager.Instance;
+            if (manager.UpdatesAvailable > 0)
+            {
+                GetUpdateInfo(out string version, out string _);
+                if (Settings.Default.IgnoreUpdate != version)
+                    Invoke((MethodInvoker)(() => UpdatePanel.Show()));
+            }
+        }
+
+        private void PrepareUpdatesCallback(IAsyncResult result)
+        {
+            UpdateManager manager = UpdateManager.Instance;
+            manager.EndCheckForUpdates(result);
+            manager.ApplyUpdates(true);
+        }
+
+        private void UpdateLabel_Click(object sender, EventArgs e)
+        {
+            GetUpdateInfo(out string version, out string changeMessage);
+            string message = "Server Monitor version {0} is available for download." + Environment.NewLine
+                + Environment.NewLine
+                + "What's new:" + Environment.NewLine
+                + "{1}" + Environment.NewLine
+                + Environment.NewLine
+                + "Would you like to download and apply the update now?";
+            using (UpdateDialog dialog = new UpdateDialog { Message = string.Format(message, version, changeMessage) })
+            {
+                DialogResult result = dialog.ShowDialog();
+                if (dialog.Checked && result == DialogResult.Cancel)
+                {
+                    Settings.Default.IgnoreUpdate = version;
+                    Settings.Default.Save();
+                    UpdatePanel.Hide();
+                }
+                if (result != DialogResult.OK)
+                    return;
+            }
+            UpdateManager.Instance.BeginPrepareUpdates(PrepareUpdatesCallback, null);
+        }
+
+        private void GetUpdateInfo(out string version, out string changeMessage)
+        {
+            string[] parts = UpdateManager.Instance.Tasks.First().Description.Split(new char[] { ':' }, 2);
+            version = parts[0];
+            changeMessage = parts[1];
+        }
     }
 }