mirror of
https://github.com/QL-Win/QuickLook.git
synced 2026-02-28 01:00:17 +08:00
- Moved the download logic to a background worker so that the main app doesn't stop working while the file is being downloaded.
- The logic to check for new updates is now separated into one function; - Replaced all messageboxes to Notifications To-Do: Actionable Notifications; Show releases-notes popup;
This commit is contained in:
@@ -33,7 +33,7 @@ namespace QuickLook
|
||||
new MenuItem($"v{Application.ProductVersion}") {Enabled = false},
|
||||
new MenuItem("-"),
|
||||
new MenuItem("Check for &Updates...",
|
||||
(sender, e) => Updater.AutoUpdate()),
|
||||
(sender, e) => Updater.CheckForUpdates()),
|
||||
_itemAutorun,
|
||||
new MenuItem("&Quit", (sender, e) => System.Windows.Application.Current.Shutdown())
|
||||
})
|
||||
|
||||
@@ -8,117 +8,134 @@ using System.IO;
|
||||
using Newtonsoft.Json;
|
||||
using System.Windows.Forms;
|
||||
using System.Diagnostics;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace QuickLook
|
||||
{
|
||||
class Updater
|
||||
{
|
||||
public static void AutoUpdate()
|
||||
|
||||
public static bool CheckForUpdates()
|
||||
{
|
||||
var lversion = "";
|
||||
var dpath = "";
|
||||
bool success = false;
|
||||
try
|
||||
{
|
||||
//check remote server for json file containing the latest version info
|
||||
WebRequest WebRequest = WebRequest.Create("https://www.dropbox.com/s/9snksdc815ewlr7/quicklookstats.txt?dl=1");
|
||||
var response = WebRequest.GetResponse();
|
||||
//check github api for json file containing the latest version info
|
||||
HttpWebRequest QLWebRequest = (HttpWebRequest)WebRequest.Create("https://api.github.com/repos/xupefei/QuickLook/releases/latest");
|
||||
QLWebRequest.UserAgent = "QuickLook Auto Updater";
|
||||
var response = QLWebRequest.GetResponse();
|
||||
string jsonrsp = new StreamReader(response.GetResponseStream()).ReadToEnd();
|
||||
dynamic results = JsonConvert.DeserializeObject<dynamic>(jsonrsp);
|
||||
lversion = results.latestVersion;
|
||||
dpath = results.downloadPath;
|
||||
lversion = results["name"];
|
||||
dpath = results["assets"][0]["browser_download_url"];
|
||||
success = true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
string message = "An error occured while trying to check for updates.";
|
||||
string caption = "QuickLook - Update error";
|
||||
MessageBoxButtons buttons = MessageBoxButtons.OK;
|
||||
DialogResult result;
|
||||
result = MessageBox.Show(message, caption, buttons);
|
||||
TrayIconManager.GetInstance().ShowNotification("QuickLook - Update error", "An error occured while trying to check for updates.", true);
|
||||
success = false;
|
||||
}
|
||||
if (Application.ProductVersion != lversion)
|
||||
|
||||
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)
|
||||
{
|
||||
string message = "A new version (v" + lversion + ") of QuickLook is available.\nDo you want to download and install it?";
|
||||
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)
|
||||
{
|
||||
bool success = false;
|
||||
string tmpFolderPath = Directory.GetCurrentDirectory() + @"\quicklook_updates";
|
||||
string newUpdateFileLocation = tmpFolderPath + @"\quicklook_" + lversion + "_" + Guid.NewGuid().ToString() + ".msi";
|
||||
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);
|
||||
}
|
||||
}
|
||||
string Warn_message = "The update is now being downloaded and the installer will be executed right after.\nThe app won't work until this process finishes.\nPlease wait.";
|
||||
string Warn_caption = "QuickLook - Notice";
|
||||
MessageBoxButtons Warn_buttons = MessageBoxButtons.OK;
|
||||
DialogResult Warn_result;
|
||||
Warn_result = MessageBox.Show(Warn_message, Warn_caption, Warn_buttons);
|
||||
|
||||
//download the new update msi package from the URL specified on the json file
|
||||
var fileReader = new WebClient();
|
||||
var fileAddress = dpath;
|
||||
fileReader.DownloadFile(new Uri(fileAddress), newUpdateFileLocation);
|
||||
success = true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
success = false;
|
||||
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (success)
|
||||
{
|
||||
//kills quicklook process and executes the msi installer through a cmd command chain (cmd will require UAC elevation)
|
||||
string pId = Process.GetCurrentProcess().Id.ToString();
|
||||
string command = @"taskkill /f /PID " + pId + " && timeout 3 && \"" + newUpdateFileLocation + "\" && 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;
|
||||
commandDispatcherProcess.Start();
|
||||
commandDispatcherProcess.WaitForExit();
|
||||
}
|
||||
else
|
||||
{
|
||||
string Err_message = "An error occured while downloading the new update.";
|
||||
string Err_caption = "QuickLook - Update error";
|
||||
MessageBoxButtons Err_buttons = MessageBoxButtons.OK;
|
||||
DialogResult Err_result;
|
||||
Err_result = MessageBox.Show(Err_message, Err_caption, Err_buttons);
|
||||
}
|
||||
}
|
||||
}
|
||||
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
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
string message = "No new version available.";
|
||||
string caption = "QuickLook";
|
||||
MessageBoxButtons buttons = MessageBoxButtons.OK;
|
||||
DialogResult result;
|
||||
result = MessageBox.Show(message, caption, buttons);
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void TriggerUpdate(string path)
|
||||
{
|
||||
BackgroundWorker QuickLookUpdateDownloader = new BackgroundWorker();
|
||||
QuickLookUpdateDownloader.DoWork += QuickLookUpdateDownloader_DoWork;
|
||||
QuickLookUpdateDownloader.RunWorkerCompleted += QuickLookUpdateDownloader_RunWorkerCompleted;
|
||||
QuickLookUpdateDownloader.RunWorkerAsync(path);
|
||||
|
||||
}
|
||||
|
||||
private static void QuickLookUpdateDownloader_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
|
||||
{
|
||||
var r = e.Result;
|
||||
if (r is string)
|
||||
{
|
||||
//executes the msi installer through a cmd command chain (cmd will require UAC elevation)
|
||||
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;
|
||||
commandDispatcherProcess.Start();
|
||||
commandDispatcherProcess.WaitForExit();
|
||||
}
|
||||
else
|
||||
{
|
||||
TrayIconManager.GetInstance().ShowNotification("QuickLook - Update error", "An error occured while downloading the new version.", true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static void QuickLookUpdateDownloader_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
|
||||
{
|
||||
var dpath = e.Argument.ToString();
|
||||
|
||||
string tmpFolderPath = Directory.GetCurrentDirectory() + @"\quicklook_updates";
|
||||
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);
|
||||
success = true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
success = false;
|
||||
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (success)
|
||||
{
|
||||
e.Result = newUpdateFileLocation;
|
||||
}
|
||||
else
|
||||
{
|
||||
e.Result = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,4 +7,3 @@ echo. >> %2
|
||||
echo using System.Reflection;>> %2
|
||||
echo. >> %2
|
||||
echo [assembly: AssemblyVersion("%git_tag%")]>> %2
|
||||
echo [assembly: AssemblyInformationalVersion("%git_revision%")]>> %2
|
||||
|
||||
Reference in New Issue
Block a user