mirror of
https://github.com/QL-Win/QuickLook.git
synced 2025-12-24 02:00:55 +08:00
Squashed commit of the following:
commit015311ccc6Author: Emanuel Alves <emanuel-alves@outlook.com> Date: Tue Jun 13 12:40:05 2017 +0100 Prevent the updater from running when it's already running. Updated timeout, slower networks require more than 2 minutes do download the update. commitbd03f4db10Author: Emanuel Alves <emanuel-alves@outlook.com> Date: Tue Jun 13 12:16:35 2017 +0100 Clean-up. commit7244fefb93Merge:b50f0b1811ead6Author: Emanuel Alves <emanuel-alves@outlook.com> Date: Tue Jun 13 12:04:15 2017 +0100 Merge branch 'master' into autoupdate-system commit811ead6728Merge:2d13dab0e29297Author: Emanuel Alves <emanuel-alves@outlook.com> Date: Tue Jun 13 12:02:37 2017 +0100 Merge remote-tracking branch 'upstream/master' commitb50f0b1e42Author: Emanuel Alves <emanuel-alves@outlook.com> Date: Tue Jun 13 12:00:01 2017 +0100 Version.CompareTo commitc22ae72a9bAuthor: Emanuel Alves <emanuel-alves@outlook.com> Date: Tue Jun 13 11:41:04 2017 +0100 WebClientEx Changed the download method to a custom web-client wrapper which supports timeout - WebClientEx. Copyright notice; commit2d466c6fcfAuthor: Emanuel Alves <emanuel-alves@outlook.com> Date: Mon Jun 12 22:11:19 2017 +0100 Major tweaks to the update-flow; New Update notification is now actionable; New QuickLook pop-up showing the changelog; Code re-organized and optimized; commit2d13dab813Merge:ef24bdddd7bed7Author: Emanuel Alves <emanuel-alves@outlook.com> Date: Mon Jun 12 18:06:26 2017 +0100 Merge remote-tracking branch 'upstream/master' commitef24bddddeMerge:4c04b0c2f9634eAuthor: Emanuel Alves <emanuel-alves@outlook.com> Date: Fri Jun 9 22:35:20 2017 +0100 Merge branch 'master' of https://github.com/emannxx/QuickLook commit4c04b0c9deAuthor: Paddy Xu <xupaddy@gmail.com> Date: Fri Jun 9 21:40:10 2017 +0300 Expose BalloonTip events to the outside world commit13568ff368Author: Paddy Xu <xupaddy@gmail.com> Date: Fri Jun 9 21:26:18 2017 +0300 Expose TopMost parameter to outside world commit1e215cb7c7Author: Paddy Xu <xupaddy@gmail.com> Date: Fri Jun 9 21:13:21 2017 +0300 Use NamedPipe, instead of second instance, to invoke preview from command line commit2f9634e3d0Merge:ad083d878eba88Author: Emanuel Alves <emanuel-alves@outlook.com> Date: Fri Jun 9 22:29:55 2017 +0100 Merge branch 'master' of https://github.com/emannxx/QuickLook commitad083d8a57Author: Paddy Xu <xupaddy@gmail.com> Date: Fri Jun 9 21:40:10 2017 +0300 Expose BalloonTip events to the outside world commiteb6524f15cAuthor: Paddy Xu <xupaddy@gmail.com> Date: Fri Jun 9 21:26:18 2017 +0300 Expose TopMost parameter to outside world commit9a0b4f23feAuthor: Paddy Xu <xupaddy@gmail.com> Date: Fri Jun 9 21:13:21 2017 +0300 Use NamedPipe, instead of second instance, to invoke preview from command line commitef9b854939Author: Emanuel Alves <emanuel-alves@outlook.com> Date: Fri Jun 9 17:54:15 2017 +0100 - 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; commit4773ded83eAuthor: Emanuel Alves <emanuel-alves@outlook.com> Date: Thu Jun 8 22:19:16 2017 +0100 QuickLook Auto Update system - first iteration; [Dependency]: NewtonSoft JSON nuget package; Added - Updater.cs class which contains the auto-update static method. Workflow: Downloads json file from remote server -> Parses -> downloads new version to a specific folder inside the install dir -> Executs a CMD command chain which kills the current QuickLook process and then executes the MSI installer.
This commit is contained in:
@@ -81,6 +81,9 @@
|
||||
<HintPath>..\packages\FontAwesome.WPF.4.7.0.9\lib\net40\FontAwesome.WPF.dll</HintPath>
|
||||
<EmbedInteropTypes>False</EmbedInteropTypes>
|
||||
</Reference>
|
||||
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Newtonsoft.Json.10.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Drawing" />
|
||||
<Reference Include="System.Windows.Forms" />
|
||||
@@ -146,7 +149,11 @@
|
||||
<Compile Include="TrayIconManager.cs" />
|
||||
<Compile Include="Plugin\ContextObject.cs" />
|
||||
<Compile Include="Helpers\WindowHelper.cs" />
|
||||
<Compile Include="Updater.cs" />
|
||||
<Compile Include="ViewWindowManager.cs" />
|
||||
<Compile Include="WebClientEx.cs">
|
||||
<SubType>Component</SubType>
|
||||
</Compile>
|
||||
<Page Include="Plugin\InfoPanel\InfoPanel.xaml">
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
<SubType>Designer</SubType>
|
||||
|
||||
@@ -50,7 +50,7 @@ namespace QuickLook
|
||||
new MenuItem($"v{Application.ProductVersion}") {Enabled = false},
|
||||
new MenuItem("-"),
|
||||
new MenuItem("Check for &Updates...",
|
||||
(sender, e) => Process.Start(@"http://pooi.moe/QuickLook/")),
|
||||
(sender, e) => Updater.CheckForUpdates()),
|
||||
_itemAutorun,
|
||||
new MenuItem("&Quit", (sender, e) => System.Windows.Application.Current.Shutdown())
|
||||
})
|
||||
|
||||
214
QuickLook/Updater.cs
Normal file
214
QuickLook/Updater.cs
Normal file
@@ -0,0 +1,214 @@
|
||||
// Copyright © 2017 Paddy Xu
|
||||
//
|
||||
// This file is part of QuickLook program.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Net;
|
||||
using System.IO;
|
||||
using Newtonsoft.Json;
|
||||
using System.Windows.Forms;
|
||||
using System.Diagnostics;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace QuickLook
|
||||
{
|
||||
class Updater
|
||||
{
|
||||
private static BackgroundWorker QuickLookUpdateDownloader = new BackgroundWorker();
|
||||
|
||||
public static bool CheckForUpdates(bool silent = false)
|
||||
{
|
||||
if (QuickLookUpdateDownloader.IsBusy)
|
||||
{
|
||||
TrayIconManager.GetInstance().ShowNotification("", "A new version is already being downloaded.", false);
|
||||
return false;
|
||||
}
|
||||
var lversion = "";
|
||||
var dpath = "";
|
||||
var mdchangelog = "";
|
||||
bool success = false;
|
||||
Version vNew = new Version();
|
||||
Version vCurrent = new Version();
|
||||
string changeLogPath = Directory.GetCurrentDirectory() + @"\quicklook_updates\changelog.md";
|
||||
try
|
||||
{
|
||||
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["name"];
|
||||
dpath = results["assets"][0]["browser_download_url"];
|
||||
mdchangelog = results["body"];
|
||||
vNew = new Version(lversion);
|
||||
vCurrent = new Version(Application.ProductVersion);
|
||||
|
||||
string tmpFolderPath = Directory.GetCurrentDirectory() + @"\quicklook_updates";
|
||||
if (!Directory.Exists(tmpFolderPath))
|
||||
{
|
||||
Directory.CreateDirectory(tmpFolderPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
using (FileStream fs = File.Create(changeLogPath))
|
||||
{
|
||||
Byte[] info = new UTF8Encoding(true).GetBytes(mdchangelog);
|
||||
fs.Write(info, 0, info.Length);
|
||||
}
|
||||
|
||||
success = true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
success = false;
|
||||
}
|
||||
|
||||
if ((vNew.CompareTo(vCurrent) > 0) && success)
|
||||
{
|
||||
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);
|
||||
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("", "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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
string command = @"""" + r + "\" && exit";
|
||||
var commandDispatcherSettings = new ProcessStartInfo();
|
||||
var commandDispatcherProcess = new Process();
|
||||
commandDispatcherSettings.FileName = "cmd";
|
||||
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
|
||||
{
|
||||
WebClientEx client = new WebClientEx(300000);
|
||||
var downloadedStream = client.DownloadDataStream(dpath);
|
||||
var fileStream = File.Create(newUpdateFileLocation);
|
||||
downloadedStream.WriteTo(fileStream);
|
||||
fileStream.Close();
|
||||
client.Dispose();
|
||||
fileStream.Dispose();
|
||||
downloadedStream.Dispose();
|
||||
success = true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
success = false;
|
||||
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (success)
|
||||
{
|
||||
e.Result = newUpdateFileLocation;
|
||||
}
|
||||
else
|
||||
{
|
||||
e.Result = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
47
QuickLook/WebClientEx.cs
Normal file
47
QuickLook/WebClientEx.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace QuickLook
|
||||
{
|
||||
public class WebClientEx : WebClient
|
||||
{
|
||||
public WebClientEx() : this(60 * 1000)
|
||||
{
|
||||
}
|
||||
|
||||
public WebClientEx(int timeout)
|
||||
{
|
||||
Timeout = timeout;
|
||||
}
|
||||
|
||||
public int Timeout { get; set; }
|
||||
|
||||
protected override WebRequest GetWebRequest(Uri address)
|
||||
{
|
||||
var request = base.GetWebRequest(address);
|
||||
|
||||
request.Timeout = Timeout;
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
public MemoryStream DownloadDataStream(string address)
|
||||
{
|
||||
var buffer = DownloadData(address);
|
||||
|
||||
return new MemoryStream(buffer);
|
||||
}
|
||||
|
||||
public MemoryStream DownloadDataStream(Uri address)
|
||||
{
|
||||
var buffer = DownloadData(address);
|
||||
|
||||
return new MemoryStream(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<packages>
|
||||
<package id="FontAwesome.WPF" version="4.7.0.9" targetFramework="net452" />
|
||||
<package id="Newtonsoft.Json" version="10.0.2" targetFramework="net462" />
|
||||
</packages>
|
||||
Reference in New Issue
Block a user