Compare commits

...

18 Commits

Author SHA1 Message Date
Emanuel Alves
015311ccc6 Prevent the updater from running when it's already running.
Updated timeout, slower networks require more than 2 minutes do download the update.
2017-06-13 12:40:05 +01:00
Emanuel Alves
bd03f4db10 Clean-up. 2017-06-13 12:16:35 +01:00
Emanuel Alves
7244fefb93 Merge branch 'master' into autoupdate-system 2017-06-13 12:04:15 +01:00
Emanuel Alves
811ead6728 Merge remote-tracking branch 'upstream/master' 2017-06-13 12:02:37 +01:00
Emanuel Alves
b50f0b1e42 Version.CompareTo 2017-06-13 12:00:01 +01:00
Emanuel Alves
c22ae72a9b WebClientEx
Changed the download method to a custom web-client wrapper which supports timeout - WebClientEx.
Copyright notice;
2017-06-13 11:41:04 +01:00
Emanuel Alves
2d466c6fcf Major tweaks to the update-flow;
New Update notification is now actionable;
New QuickLook pop-up showing the changelog;
Code re-organized and optimized;
2017-06-12 22:11:19 +01:00
Emanuel Alves
2d13dab813 Merge remote-tracking branch 'upstream/master' 2017-06-12 18:06:26 +01:00
Emanuel Alves
ef24bdddde Merge branch 'master' of https://github.com/emannxx/QuickLook 2017-06-09 22:35:20 +01:00
Paddy Xu
4c04b0c9de Expose BalloonTip events to the outside world 2017-06-09 22:31:09 +01:00
Paddy Xu
13568ff368 Expose TopMost parameter to outside world 2017-06-09 22:31:09 +01:00
Paddy Xu
1e215cb7c7 Use NamedPipe, instead of second instance, to invoke preview from command line 2017-06-09 22:31:09 +01:00
Emanuel Alves
2f9634e3d0 Merge branch 'master' of https://github.com/emannxx/QuickLook 2017-06-09 22:29:55 +01:00
Paddy Xu
ad083d8a57 Expose BalloonTip events to the outside world 2017-06-09 22:27:53 +01:00
Paddy Xu
eb6524f15c Expose TopMost parameter to outside world 2017-06-09 22:27:53 +01:00
Paddy Xu
9a0b4f23fe Use NamedPipe, instead of second instance, to invoke preview from command line 2017-06-09 22:27:53 +01:00
Emanuel Alves
ef9b854939 - 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;
2017-06-09 17:54:15 +01:00
Emanuel Alves
4773ded83e 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.
2017-06-08 22:19:16 +01:00
6 changed files with 270 additions and 3 deletions

View File

@@ -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>

View File

@@ -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
View 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
View 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);
}
}
}

View File

@@ -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>

View File

@@ -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