From 2d466c6fcfb6194490a899fe3d80a21cd0f0e745 Mon Sep 17 00:00:00 2001 From: Emanuel Alves Date: Mon, 12 Jun 2017 22:11:19 +0100 Subject: [PATCH] Major tweaks to the update-flow; New Update notification is now actionable; New QuickLook pop-up showing the changelog; Code re-organized and optimized; --- QuickLook/Updater.cs | 110 ++++++++++++++++++++++++++++++++----------- 1 file changed, 82 insertions(+), 28 deletions(-) diff --git a/QuickLook/Updater.cs b/QuickLook/Updater.cs index a72c6d5..0b8860a 100644 --- a/QuickLook/Updater.cs +++ b/QuickLook/Updater.cs @@ -16,11 +16,16 @@ namespace QuickLook class Updater { - public static bool CheckForUpdates() + public static bool CheckForUpdates(bool silent = false) { var lversion = ""; var dpath = ""; + var mdchangelog = ""; bool success = false; + int cleanNewVersion = 0; + int cleanCurrentVersion = 0; + string changeLogPath = Directory.GetCurrentDirectory() + @"\quicklook_updates\changelog.md"; + try { //check github api for json file containing the latest version info @@ -31,31 +36,99 @@ namespace QuickLook dynamic results = JsonConvert.DeserializeObject(jsonrsp); lversion = results["name"]; dpath = results["assets"][0]["browser_download_url"]; + mdchangelog = results["body"]; + lversion = lversion + ".0"; //update-version.cmd adds an aditional 0 to the version, github api doesnt. + cleanNewVersion = Convert.ToInt32(lversion.Replace(".", "")); + cleanCurrentVersion = Convert.ToInt32(Application.ProductVersion.Replace(".", "")); + + string tmpFolderPath = Directory.GetCurrentDirectory() + @"\quicklook_updates"; + if (!Directory.Exists(tmpFolderPath)) + { + Directory.CreateDirectory(tmpFolderPath); + } + else + { + //wipe the temporary download folder + System.IO.DirectoryInfo di = new DirectoryInfo(tmpFolderPath); + foreach (FileInfo file in di.GetFiles()) + { + file.Delete(); + } + foreach (DirectoryInfo dir in di.GetDirectories()) + { + dir.Delete(true); + } + } + + if (File.Exists(changeLogPath)) + { + File.Delete(changeLogPath); + } + + // Create the file. + using (FileStream fs = File.Create(changeLogPath)) + { + Byte[] info = new UTF8Encoding(true).GetBytes(mdchangelog); + fs.Write(info, 0, info.Length); + } + success = true; } catch (Exception e) { - TrayIconManager.GetInstance().ShowNotification("QuickLook - Update error", "An error occured while trying to check for updates.", true); success = false; } - lversion = lversion + ".0"; //update-version.cmd adds an aditional 0 to the version, github api doesnt. - int cleanNewVersion = Convert.ToInt32(lversion.Replace(".", "")); - int cleanCurrentVersion = Convert.ToInt32(Application.ProductVersion.Replace(".", "")); if ((cleanCurrentVersion < cleanNewVersion) && success) { - TrayIconManager.GetInstance().ShowNotification("QuickLook Update", "A new version of QuickLook is being downloaded.", false); - TriggerUpdate(dpath); //this function will be called when the user accepts the update + Action acpt = new Action(() => UpdateConfirmation(changeLogPath, dpath)); + Action dcln = new Action(() => CancelUpdate()); + + TrayIconManager.GetInstance().ShowNotification("QuickLook", "A new version of QuickLook is available. Click here to learn more.", false, acpt, dcln); + //TriggerUpdate(dpath); //this function will be called when the user accepts the update return true; } + else if (!success) + { + if (!silent) + { + TrayIconManager.GetInstance().ShowNotification("QuickLook - Update error", "An error occured while trying to check for updates.", true); + } + return false; + } else { + if (!silent) + { + TrayIconManager.GetInstance().ShowNotification("QuickLook", "You have the latest version installed.", false); + } return false; } } + public static void UpdateConfirmation(string changelogPath, string dpath) + { + ViewWindowManager.GetInstance().InvokeViewer(changelogPath, false); + string message = "Do you want to download and install this new update?"; + string caption = "QuickLook - New Update Available"; + MessageBoxButtons buttons = MessageBoxButtons.YesNo; + DialogResult result; + result = MessageBox.Show(message, caption, buttons); + if (result == System.Windows.Forms.DialogResult.Yes) + { + TriggerUpdate(dpath); + ViewWindowManager.GetInstance().ClosePreview(); + TrayIconManager.GetInstance().ShowNotification("QuickLook", "QuickLook is downloading a new update.", false); + } + } + + public static void CancelUpdate() + { + //code to skip or postpone the update + } + public static void TriggerUpdate(string path) { BackgroundWorker QuickLookUpdateDownloader = new BackgroundWorker(); @@ -70,12 +143,11 @@ namespace QuickLook var r = e.Result; if (r is string) { - //executes the msi installer through a cmd command chain (cmd will require UAC elevation) + //executes the msi installer through a cmd command chain string command = @"""" + r + "\" && exit"; var commandDispatcherSettings = new ProcessStartInfo(); var commandDispatcherProcess = new Process(); commandDispatcherSettings.FileName = "cmd"; - commandDispatcherSettings.Verb = "runas"; commandDispatcherSettings.WindowStyle = ProcessWindowStyle.Hidden; commandDispatcherSettings.Arguments = "cmd /C " + command; commandDispatcherProcess.StartInfo = commandDispatcherSettings; @@ -97,25 +169,7 @@ namespace QuickLook string newUpdateFileLocation = tmpFolderPath + @"\quicklook_update_" + Guid.NewGuid().ToString() + ".msi"; bool success = false; try - { - if (!Directory.Exists(tmpFolderPath)) - { - Directory.CreateDirectory(tmpFolderPath); - } - else - { - //wipe the temporary download folder - System.IO.DirectoryInfo di = new DirectoryInfo(tmpFolderPath); - foreach (FileInfo file in di.GetFiles()) - { - file.Delete(); - } - foreach (DirectoryInfo dir in di.GetDirectories()) - { - dir.Delete(true); - } - } - + { //download the new update msi package from the URL specified on the json file var fileReader = new WebClient(); fileReader.DownloadFile(new Uri(dpath), newUpdateFileLocation);