From 4ea3f81b4d2dbf8d8abdb9438ebb41d2f76d7274 Mon Sep 17 00:00:00 2001 From: Chris Kaczor Date: Sat, 22 Nov 2014 09:14:48 -0500 Subject: [PATCH] Move update message logic to common and remove restarting on update (installer will do that) --- Application/App.xaml.cs | 89 ++++++--------- Application/MainWindow.xaml.cs | 33 +++++- Application/Options/AboutOptionsPanel.xaml.cs | 2 +- .../Options/UpdateOptionsPanel.xaml.cs | 6 +- Application/Properties/Resources.Designer.cs | 42 +------ Application/Properties/Resources.resx | 16 --- Application/SplashWindow.xaml.cs | 4 +- Application/VersionCheck.cs | 103 +----------------- 8 files changed, 71 insertions(+), 224 deletions(-) diff --git a/Application/App.xaml.cs b/Application/App.xaml.cs index 862387b..8f5dc44 100644 --- a/Application/App.xaml.cs +++ b/Application/App.xaml.cs @@ -1,18 +1,15 @@ -using System; +using Common.Debug; +using Common.Helpers; +using Common.IO; +using Common.Settings; +using Common.Wpf.Extensions; +using FeedCenter.Properties; +using Microsoft.Win32; +using System; using System.Diagnostics; using System.Globalization; using System.IO; using System.Reflection; -using System.Threading; -using Microsoft.Win32; - -using Common.Debug; -using Common.Helpers; -using Common.IO; -using Common.Wpf.Extensions; -using Common.Settings; - -using FeedCenter.Properties; namespace FeedCenter { @@ -20,13 +17,7 @@ namespace FeedCenter { #region Debug properties - public static bool UseDebugPath; - - #endregion - - #region Properties - - public bool Restart; + private static bool _useDebugPath; #endregion @@ -35,12 +26,14 @@ namespace FeedCenter [STAThread] public static void Main() { + _useDebugPath = Environment.CommandLine.IndexOf("/debugPath", StringComparison.InvariantCultureIgnoreCase) != -1; + // Create and initialize the app object - App app = new App(); + var app = new App(); app.InitializeComponent(); // Create an isolation handle to see if we are already running - IDisposable isolationHandle = ApplicationIsolation.GetIsolationHandle(FeedCenter.Properties.Resources.ApplicationName); + var isolationHandle = ApplicationIsolation.GetIsolationHandle(FeedCenter.Properties.Resources.ApplicationName); // If there is another copy then pass it the command line and exit if (isolationHandle == null) @@ -54,7 +47,7 @@ namespace FeedCenter { // Set the data directory based on debug or not AppDomain.CurrentDomain.SetData("DataDirectory", - UseDebugPath + _useDebugPath ? Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) : UserSettingsPath); @@ -87,35 +80,21 @@ namespace FeedCenter } // Create the main window before the splash otherwise WPF gets messed up - MainWindow mainWindow = new MainWindow(); + var mainWindow = new MainWindow(); // Show the splash window - SplashWindow splashWindow = new SplashWindow(); + var splashWindow = new SplashWindow(); splashWindow.ShowDialog(); - // If we don't need to restart then fire up the main window - if (!app.Restart) - { - // Update the registry settings - SetStartWithWindows(Settings.Default.StartWithWindows); - SetDefaultFeedReader(Settings.Default.RegisterAsDefaultFeedReader); + // Update the registry settings + SetStartWithWindows(Settings.Default.StartWithWindows); + SetDefaultFeedReader(Settings.Default.RegisterAsDefaultFeedReader); - // Initialize the window - mainWindow.Initialize(); + // Initialize the window + mainWindow.Initialize(); - // Run the app - app.Run(mainWindow); - } - - // If we need to restart - if (app.Restart) - { - // Wait a bit to make sure any previous upgrade has settled - Thread.Sleep(2000); - - // Restart the application - Current.Restart(); - } + // Run the app + app.Run(mainWindow); // Terminate the tracer Tracer.Dispose(); @@ -143,11 +122,11 @@ namespace FeedCenter get { // If we're running in debug mode then use a local path for the database and logs - if (UseDebugPath) + if (_useDebugPath) return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); // Get the path to the local application data directory - string path = Path.Combine( + var path = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), FeedCenter.Properties.Resources.ApplicationName); @@ -162,13 +141,13 @@ namespace FeedCenter public static void SetStartWithWindows(bool value) { // Get the application name - string applicationName = FeedCenter.Properties.Resources.ApplicationDisplayName; + var applicationName = FeedCenter.Properties.Resources.ApplicationDisplayName; // Get application details - string publisherName = applicationName; - string productName = applicationName; - string allProgramsPath = Environment.GetFolderPath(Environment.SpecialFolder.Programs); - string shortcutPath = Path.Combine(allProgramsPath, publisherName); + var publisherName = applicationName; + var productName = applicationName; + var allProgramsPath = Environment.GetFolderPath(Environment.SpecialFolder.Programs); + var shortcutPath = Path.Combine(allProgramsPath, publisherName); // Build the auto start path shortcutPath = "\"" + Path.Combine(shortcutPath, productName) + ".appref-ms\""; @@ -180,10 +159,10 @@ namespace FeedCenter public static void SetDefaultFeedReader(bool value) { // Get the location of the assembly - string assemblyLocation = Assembly.GetExecutingAssembly().Location; + var assemblyLocation = Assembly.GetExecutingAssembly().Location; // Open the registry key (creating if needed) - using (RegistryKey registryKey = Registry.CurrentUser.CreateSubKey("Software\\Classes\\feed", RegistryKeyPermissionCheck.ReadWriteSubTree)) + using (var registryKey = Registry.CurrentUser.CreateSubKey("Software\\Classes\\feed", RegistryKeyPermissionCheck.ReadWriteSubTree)) { if (registryKey == null) return; @@ -193,7 +172,7 @@ namespace FeedCenter registryKey.SetValue("URL Protocol", string.Empty); // Open the icon subkey (creating if needed) - using (RegistryKey subKey = registryKey.CreateSubKey("DefaultIcon", RegistryKeyPermissionCheck.ReadWriteSubTree)) + using (var subKey = registryKey.CreateSubKey("DefaultIcon", RegistryKeyPermissionCheck.ReadWriteSubTree)) { if (subKey != null) { @@ -206,7 +185,7 @@ namespace FeedCenter } // Open the subkey for the command (creating if needed) - using (RegistryKey subKey = registryKey.CreateSubKey("shell\\open\\command", RegistryKeyPermissionCheck.ReadWriteSubTree)) + using (var subKey = registryKey.CreateSubKey("shell\\open\\command", RegistryKeyPermissionCheck.ReadWriteSubTree)) { if (subKey != null) { diff --git a/Application/MainWindow.xaml.cs b/Application/MainWindow.xaml.cs index 245a858..3fd4a9b 100644 --- a/Application/MainWindow.xaml.cs +++ b/Application/MainWindow.xaml.cs @@ -66,6 +66,14 @@ namespace FeedCenter public void Initialize() { + // Setup the update message properties and callbacks + UpdateCheck.ApplicationName = Properties.Resources.ApplicationDisplayName; + UpdateCheck.UpdateServer = Settings.Default.VersionLocation; + UpdateCheck.UpdateFile = Settings.Default.VersionFile; + UpdateCheck.ApplicationShutdown = ApplicationShutdown; + UpdateCheck.ApplicationCurrentMessage = ApplicationCurrentMessage; + UpdateCheck.ApplicationUpdateMessage = ApplicationUpdateMessage; + // Show the notification icon NotificationIcon.Initialize(this); @@ -105,6 +113,21 @@ namespace FeedCenter Tracer.WriteLine("MainForm creation finished"); } + private static bool ApplicationUpdateMessage(string title, string message) + { + return MessageBox.Show(message, title, MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes; + } + + private static void ApplicationCurrentMessage(string title, string message) + { + MessageBox.Show(message, title, MessageBoxButton.OK, MessageBoxImage.Information); + } + + private static void ApplicationShutdown() + { + Application.Current.Shutdown(); + } + #endregion #region Window overrides @@ -385,7 +408,7 @@ namespace FeedCenter // Increment the index and adjust if we've gone around the end _feedIndex = (_feedIndex + 1) % feedCount; } - while (startIndex != _feedIndex); + while (_feedIndex != startIndex); // If nothing was found then clear the current feed if (!found) @@ -456,7 +479,7 @@ namespace FeedCenter if (_feedIndex < 0) _feedIndex = feedCount - 1; } - while (startIndex != _feedIndex); + while (_feedIndex != startIndex); // If nothing was found then clear the current feed if (!found) @@ -690,7 +713,7 @@ namespace FeedCenter if (DateTime.Now - Settings.Default.LastVersionCheck >= Settings.Default.VersionCheckInterval) { // Get the update information - UpdateCheck.CheckForUpdate(Settings.Default.VersionLocation, Settings.Default.VersionFile); + UpdateCheck.CheckForUpdate(); // Update the last check time Settings.Default.LastVersionCheck = DateTime.Now; @@ -1192,7 +1215,7 @@ namespace FeedCenter private void HandleOpenAllToolbarButtonClick(object sender, RoutedEventArgs e) { - var multipleOpenAction = (MultipleOpenAction) _currentFeed.MultipleOpenAction; + var multipleOpenAction = _currentFeed.MultipleOpenAction; switch (multipleOpenAction) { @@ -1321,7 +1344,7 @@ namespace FeedCenter private void HandleNewVersionLinkClick(object sender, RoutedEventArgs e) { // Display update information - VersionCheck.DisplayUpdateInformation(true); + UpdateCheck.DisplayUpdateInformation(true); } } } diff --git a/Application/Options/AboutOptionsPanel.xaml.cs b/Application/Options/AboutOptionsPanel.xaml.cs index 0338f24..0650640 100644 --- a/Application/Options/AboutOptionsPanel.xaml.cs +++ b/Application/Options/AboutOptionsPanel.xaml.cs @@ -16,7 +16,7 @@ namespace FeedCenter.Options applicationNameLabel.Text = Properties.Resources.ApplicationDisplayName; - string version = UpdateCheck.CurrentVersion.ToString(); + string version = UpdateCheck.LocalVersion.ToString(); versionLabel.Text = string.Format(Properties.Resources.Version, version); companyLabel.Text = ((AssemblyCompanyAttribute) Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false)[0]).Company; diff --git a/Application/Options/UpdateOptionsPanel.xaml.cs b/Application/Options/UpdateOptionsPanel.xaml.cs index 27ac9c2..be6be57 100644 --- a/Application/Options/UpdateOptionsPanel.xaml.cs +++ b/Application/Options/UpdateOptionsPanel.xaml.cs @@ -1,4 +1,6 @@ -namespace FeedCenter.Options +using Common.Update; + +namespace FeedCenter.Options { public partial class UpdateOptionsPanel { @@ -32,7 +34,7 @@ private void HandleCheckVersionNowButtonClick(object sender, System.Windows.RoutedEventArgs e) { - VersionCheck.DisplayUpdateInformation(true); + UpdateCheck.DisplayUpdateInformation(true); } } } diff --git a/Application/Properties/Resources.Designer.cs b/Application/Properties/Resources.Designer.cs index eab4204..da8eb84 100644 --- a/Application/Properties/Resources.Designer.cs +++ b/Application/Properties/Resources.Designer.cs @@ -1,7 +1,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:4.0.30319.34209 +// Runtime Version:4.0.30319.34014 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -1360,46 +1360,6 @@ namespace FeedCenter.Properties { } } - /// - /// Looks up a localized string similar to An update to version {0} is now available.. - /// - public static string UpdateAvailable { - get { - return ResourceManager.GetString("UpdateAvailable", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to You are already running the most recent version. - /// - ///No updates are available at this time.. - /// - public static string UpdateCheckCurrent { - get { - return ResourceManager.GetString("UpdateCheckCurrent", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Version {1} of {0} is now available. - /// - ///Would you like to download and install it now?. - /// - public static string UpdateCheckNewVersion { - get { - return ResourceManager.GetString("UpdateCheckNewVersion", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to {0} Update. - /// - public static string UpdateCheckTitle { - get { - return ResourceManager.GetString("UpdateCheckTitle", resourceCulture); - } - } - /// /// Looks up a localized string similar to Version: {0}. /// diff --git a/Application/Properties/Resources.resx b/Application/Properties/Resources.resx index 9df5496..10c340b 100644 --- a/Application/Properties/Resources.resx +++ b/Application/Properties/Resources.resx @@ -184,22 +184,6 @@ FeedCenter - - Version {1} of {0} is now available. - -Would you like to download and install it now? - - - You are already running the most recent version. - -No updates are available at this time. - - - {0} Update - - - An update to version {0} is now available. - "{0}" has been successfully added to your feed list. diff --git a/Application/SplashWindow.xaml.cs b/Application/SplashWindow.xaml.cs index 609aafb..cf604b0 100644 --- a/Application/SplashWindow.xaml.cs +++ b/Application/SplashWindow.xaml.cs @@ -49,7 +49,7 @@ namespace FeedCenter _dispatcher = Dispatcher.CurrentDispatcher; // Get the version to display - string version = UpdateCheck.CurrentVersion.ToString(); + string version = UpdateCheck.LocalVersion.ToString(); // Show the version lblVersion.Content = string.Format(Properties.Resources.Version, version); @@ -198,7 +198,7 @@ namespace FeedCenter return false; // Return if the check worked and an update is available - return UpdateCheck.CheckForUpdate(Settings.Default.VersionLocation, Settings.Default.VersionFile) && UpdateCheck.UpdateAvailable; + return UpdateCheck.CheckForUpdate() && UpdateCheck.UpdateAvailable; } private static bool CheckDatabase() diff --git a/Application/VersionCheck.cs b/Application/VersionCheck.cs index 1598e74..7651deb 100644 --- a/Application/VersionCheck.cs +++ b/Application/VersionCheck.cs @@ -1,111 +1,10 @@ -using Common.Debug; -using Common.Update; +using Common.Update; using FeedCenter.Properties; -using System; -using System.ComponentModel; using System.Windows; -using Application = System.Windows.Forms.Application; namespace FeedCenter { internal static class VersionCheck { - public static async void DisplayUpdateInformation(bool showIfCurrent) - { - UpdateCheck.CheckForUpdate(Settings.Default.VersionLocation, Settings.Default.VersionFile); - - // Check for an update - if (UpdateCheck.UpdateAvailable) - { - // Load the version string from the server - Version serverVersion = UpdateCheck.VersionInfo.Version; - - // Format the check title - string updateCheckTitle = string.Format(Resources.UpdateCheckTitle, Resources.ApplicationDisplayName); - - // Format the message - string updateCheckMessage = string.Format(Resources.UpdateCheckNewVersion, Resources.ApplicationDisplayName, serverVersion); - - // Ask the user to update - if (MessageBox.Show(updateCheckMessage, updateCheckTitle, MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes) - return; - - // Get the update - await UpdateCheck.DownloadUpdate(); - - // Install the update - UpdateCheck.InstallUpdate(); - - // Set to restart - ((App) System.Windows.Application.Current).Restart = true; - - // Restart the application - System.Windows.Application.Current.Shutdown(); - } - else if (showIfCurrent) - { - // Format the check title - string updateCheckTitle = string.Format(Resources.UpdateCheckTitle, Resources.ApplicationDisplayName); - - // Format the message - string updateCheckMessage = string.Format(Resources.UpdateCheckCurrent, Resources.ApplicationDisplayName); - - MessageBox.Show(updateCheckMessage, updateCheckTitle, MessageBoxButton.OK, MessageBoxImage.Information); - } - } - - #region Background checking - - private static BackgroundWorker _backgroundWorker; - - public static void DisplayUpdateInformationAsync() - { - // Do nothing if we already have a worker - if (_backgroundWorker != null) - return; - - // Create a new worker - _backgroundWorker = new BackgroundWorker(); - - // Setup worker events - _backgroundWorker.DoWork += HandleBackgroundWorkerDoWork; - _backgroundWorker.RunWorkerCompleted += HandleBackgroundWorkerCompleted; - - // Run the worker - _backgroundWorker.RunWorkerAsync(); - - // Wait for the worker - while (_backgroundWorker.IsBusy) - Application.DoEvents(); - - // Clear out the worker - _backgroundWorker.Dispose(); - _backgroundWorker = null; - } - - private static void HandleBackgroundWorkerDoWork(object sender, DoWorkEventArgs e) - { - e.Result = null; - - try - { - UpdateCheck.CheckForUpdate(Settings.Default.VersionLocation, Settings.Default.VersionFile); - - // Get the update information and set it into the result - e.Result = UpdateCheck.UpdateAvailable; - } - catch (Exception exception) - { - Tracer.WriteException(exception); - } - } - - private static void HandleBackgroundWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) - { - // Display any update info - DisplayUpdateInformation(false); - } - - #endregion } }