comparison 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
comparison
equal deleted inserted replaced
9:7127d5b5ac75 10:9e77c0dccb66
1 using ServerMonitorApp.Properties; 1 using NAppUpdate.Framework;
2 using NAppUpdate.Framework.Sources;
3 using NAppUpdate.Framework.Tasks;
4 using ServerMonitorApp.Properties;
2 using System; 5 using System;
3 using System.Collections.Generic; 6 using System.Collections.Generic;
4 using System.ComponentModel; 7 using System.ComponentModel;
5 using System.Data; 8 using System.Data;
6 using System.Drawing; 9 using System.Drawing;
66 } 69 }
67 } 70 }
68 monitor.CheckStatusChanged += Monitor_CheckStatusChanged; 71 monitor.CheckStatusChanged += Monitor_CheckStatusChanged;
69 RefreshDisplay(); 72 RefreshDisplay();
70 CollectPrivateKeyPasswords(); 73 CollectPrivateKeyPasswords();
74 CheckForUpdate();
71 } 75 }
72 76
73 private ServerForm ShowServerForm(Server server, bool activate = true) 77 private ServerForm ShowServerForm(Server server, bool activate = true)
74 { 78 {
75 bool isNewServer = false; 79 bool isNewServer = false;
285 if (e.ClickedItem == ShowServerMonitorMenuItem) 289 if (e.ClickedItem == ShowServerMonitorMenuItem)
286 ShowWindow(); 290 ShowWindow();
287 else if (e.ClickedItem == ExitMenuItem) 291 else if (e.ClickedItem == ExitMenuItem)
288 Application.Exit(); 292 Application.Exit();
289 } 293 }
294
295 private void CheckForUpdate()
296 {
297 //System.Threading.Thread.Sleep(5000);
298 UpdateManager manager = UpdateManager.Instance;
299 manager.ReinstateIfRestarted();
300 manager.UpdateSource = new SimpleWebSource(@"c:\temp\feed.xml");
301 if (manager.State == UpdateManager.UpdateProcessState.NotChecked)
302 manager.BeginCheckForUpdates(CheckForUpdatesCallback, null);
303 }
304
305 private void CheckForUpdatesCallback(IAsyncResult result)
306 {
307 UpdateManager manager = UpdateManager.Instance;
308 if (manager.UpdatesAvailable > 0)
309 {
310 GetUpdateInfo(out string version, out string _);
311 if (Settings.Default.IgnoreUpdate != version)
312 Invoke((MethodInvoker)(() => UpdatePanel.Show()));
313 }
314 }
315
316 private void PrepareUpdatesCallback(IAsyncResult result)
317 {
318 UpdateManager manager = UpdateManager.Instance;
319 manager.EndCheckForUpdates(result);
320 manager.ApplyUpdates(true);
321 }
322
323 private void UpdateLabel_Click(object sender, EventArgs e)
324 {
325 GetUpdateInfo(out string version, out string changeMessage);
326 string message = "Server Monitor version {0} is available for download." + Environment.NewLine
327 + Environment.NewLine
328 + "What's new:" + Environment.NewLine
329 + "{1}" + Environment.NewLine
330 + Environment.NewLine
331 + "Would you like to download and apply the update now?";
332 using (UpdateDialog dialog = new UpdateDialog { Message = string.Format(message, version, changeMessage) })
333 {
334 DialogResult result = dialog.ShowDialog();
335 if (dialog.Checked && result == DialogResult.Cancel)
336 {
337 Settings.Default.IgnoreUpdate = version;
338 Settings.Default.Save();
339 UpdatePanel.Hide();
340 }
341 if (result != DialogResult.OK)
342 return;
343 }
344 UpdateManager.Instance.BeginPrepareUpdates(PrepareUpdatesCallback, null);
345 }
346
347 private void GetUpdateInfo(out string version, out string changeMessage)
348 {
349 string[] parts = UpdateManager.Instance.Tasks.First().Description.Split(new char[] { ':' }, 2);
350 version = parts[0];
351 changeMessage = parts[1];
352 }
290 } 353 }
291 } 354 }