mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 19:26:39 +02:00
Add upgrade dialog
This commit is contained in:
@@ -7,5 +7,10 @@
|
||||
public string download_link1 { get; set; }
|
||||
public string download_link2 { get; set; }
|
||||
public string description { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return version;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Reflection;
|
||||
using System.Windows.Forms;
|
||||
using System.Windows.Threading;
|
||||
using NAppUpdate.Framework;
|
||||
using NAppUpdate.Framework.Common;
|
||||
using NAppUpdate.Framework.Sources;
|
||||
using NAppUpdate.Framework.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using Wox.Core.i18n;
|
||||
using Wox.Core.UserSettings;
|
||||
@@ -18,9 +22,15 @@ namespace Wox.Core.Updater
|
||||
{
|
||||
private static UpdaterManager instance;
|
||||
private const string VersionCheckURL = "https://api.getwox.com/release/latest/";
|
||||
private const string UpdateFeedURL = "http://upgrade.getwox.com/update.xml";
|
||||
//private const string UpdateFeedURL = "http://upgrade.getwox.com/update.xml";
|
||||
private const string UpdateFeedURL = "http://127.0.0.1:8888/update.xml";
|
||||
private static SemanticVersion currentVersion;
|
||||
|
||||
public event EventHandler PrepareUpdateReady;
|
||||
public event EventHandler UpdateError;
|
||||
|
||||
public Release NewRelease { get; set; }
|
||||
|
||||
public static UpdaterManager Instance
|
||||
{
|
||||
get
|
||||
@@ -57,6 +67,19 @@ namespace Wox.Core.Updater
|
||||
return new SemanticVersion(release.version) > CurrentVersion;
|
||||
}
|
||||
|
||||
public List<string> GetAvailableUpdateFiles()
|
||||
{
|
||||
List<string> files = new List<string>();
|
||||
foreach (var task in UpdateManager.Instance.Tasks)
|
||||
{
|
||||
if (task is FileUpdateTask)
|
||||
{
|
||||
files.Add(((FileUpdateTask)task).LocalPath);
|
||||
}
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
public void CheckUpdate()
|
||||
{
|
||||
string json = HttpRequest.Get(VersionCheckURL, HttpProxy.Instance);
|
||||
@@ -64,14 +87,15 @@ namespace Wox.Core.Updater
|
||||
{
|
||||
try
|
||||
{
|
||||
Release newRelease = JsonConvert.DeserializeObject<Release>(json);
|
||||
if (IsNewerThanCurrent(newRelease))
|
||||
NewRelease = JsonConvert.DeserializeObject<Release>(json);
|
||||
if (IsNewerThanCurrent(NewRelease) && !UserSettingStorage.Instance.DontPromptUpdateMsg)
|
||||
{
|
||||
StartUpdate();
|
||||
}
|
||||
}
|
||||
catch
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Log.Error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -105,45 +129,52 @@ namespace Wox.Core.Updater
|
||||
updManager.BeginPrepareUpdates(result =>
|
||||
{
|
||||
((UpdateProcessAsyncResult)result).EndInvoke();
|
||||
string updateReady = InternationalizationManager.Instance.GetTranslation("update_wox_update_ready");
|
||||
string updateInstall = InternationalizationManager.Instance.GetTranslation("update_wox_update_install");
|
||||
updateInstall = string.Format(updateInstall, updManager.UpdatesAvailable);
|
||||
DialogResult dr = MessageBox.Show(updateInstall, updateReady, MessageBoxButtons.YesNo);
|
||||
|
||||
if (dr == DialogResult.Yes)
|
||||
{
|
||||
|
||||
// ApplyUpdates is a synchronous method by design. Make sure to save all user work before calling
|
||||
// it as it might restart your application
|
||||
// get out of the way so the console window isn't obstructed
|
||||
try
|
||||
{
|
||||
updManager.ApplyUpdates(true, UserSettingStorage.Instance.EnableUpdateLog, false);
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
string updateError =
|
||||
InternationalizationManager.Instance.GetTranslation("update_wox_update_error");
|
||||
Log.Error(e);
|
||||
MessageBox.Show(updateError);
|
||||
}
|
||||
|
||||
updManager.CleanUp();
|
||||
}
|
||||
else
|
||||
{
|
||||
updManager.CleanUp();
|
||||
}
|
||||
OnPrepareUpdateReady();
|
||||
}, null);
|
||||
}, null);
|
||||
}
|
||||
|
||||
public void CleanUp()
|
||||
{
|
||||
UpdateManager.Instance.CleanUp();
|
||||
}
|
||||
|
||||
public void ApplyUpdates()
|
||||
{
|
||||
// ApplyUpdates is a synchronous method by design. Make sure to save all user work before calling
|
||||
// it as it might restart your application
|
||||
// get out of the way so the console window isn't obstructed
|
||||
try
|
||||
{
|
||||
UpdateManager.Instance.ApplyUpdates(true, UserSettingStorage.Instance.EnableUpdateLog, false);
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
string updateError = InternationalizationManager.Instance.GetTranslation("update_wox_update_error");
|
||||
Log.Error(e);
|
||||
MessageBox.Show(updateError);
|
||||
OnUpdateError();
|
||||
}
|
||||
|
||||
UpdateManager.Instance.CleanUp();
|
||||
}
|
||||
|
||||
private IUpdateSource GetUpdateSource()
|
||||
{
|
||||
// Normally this would be a web based source.
|
||||
// But for the demo app, we prepare an in-memory source.
|
||||
var source = new SimpleWebSource(UpdateFeedURL);
|
||||
var source = new WoxUpdateSource(UpdateFeedURL, HttpRequest.GetWebProxy(HttpProxy.Instance));
|
||||
return source;
|
||||
}
|
||||
|
||||
protected virtual void OnPrepareUpdateReady()
|
||||
{
|
||||
var handler = PrepareUpdateReady;
|
||||
if (handler != null) handler(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
protected virtual void OnUpdateError()
|
||||
{
|
||||
var handler = UpdateError;
|
||||
if (handler != null) handler(this, EventArgs.Empty);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
68
Wox.Core/Updater/WoxUpdateSource.cs
Normal file
68
Wox.Core/Updater/WoxUpdateSource.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using NAppUpdate.Framework.Common;
|
||||
using NAppUpdate.Framework.Sources;
|
||||
using NAppUpdate.Framework.Utils;
|
||||
|
||||
namespace Wox.Core.Updater
|
||||
{
|
||||
internal class WoxUpdateSource : IUpdateSource
|
||||
{
|
||||
public IWebProxy Proxy { get; set; }
|
||||
|
||||
public string FeedUrl { get; set; }
|
||||
|
||||
public WoxUpdateSource(string feedUrl,IWebProxy proxy)
|
||||
{
|
||||
this.FeedUrl = feedUrl;
|
||||
this.Proxy = proxy;
|
||||
}
|
||||
|
||||
private void TryResolvingHost()
|
||||
{
|
||||
Uri uri = new Uri(this.FeedUrl);
|
||||
try
|
||||
{
|
||||
Dns.GetHostEntry(uri.Host);
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
throw new WebException(string.Format("Failed to resolve {0}. Check your connectivity.", (object)uri.Host), WebExceptionStatus.ConnectFailure);
|
||||
}
|
||||
}
|
||||
|
||||
public string GetUpdatesFeed()
|
||||
{
|
||||
this.TryResolvingHost();
|
||||
string str = string.Empty;
|
||||
WebRequest webRequest = WebRequest.Create(this.FeedUrl);
|
||||
webRequest.Method = "GET";
|
||||
webRequest.Proxy = this.Proxy;
|
||||
using (WebResponse response = webRequest.GetResponse())
|
||||
{
|
||||
Stream responseStream = response.GetResponseStream();
|
||||
if (responseStream != null)
|
||||
{
|
||||
using (StreamReader streamReader = new StreamReader(responseStream, true))
|
||||
str = streamReader.ReadToEnd();
|
||||
}
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
public bool GetData(string url, string baseUrl, Action<UpdateProgressInfo> onProgress, ref string tempLocation)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(baseUrl) && !baseUrl.EndsWith("/"))
|
||||
baseUrl += "/";
|
||||
FileDownloader fileDownloader = !Uri.IsWellFormedUriString(url, UriKind.Absolute) ? (!Uri.IsWellFormedUriString(baseUrl, UriKind.Absolute) ? (string.IsNullOrEmpty(baseUrl) ? new FileDownloader(url) : new FileDownloader(new Uri(new Uri(baseUrl), url))) : new FileDownloader(new Uri(new Uri(baseUrl, UriKind.Absolute), url))) : new FileDownloader(url);
|
||||
fileDownloader.Proxy = this.Proxy;
|
||||
if (string.IsNullOrEmpty(tempLocation) || !Directory.Exists(Path.GetDirectoryName(tempLocation)))
|
||||
tempLocation = Path.GetTempFileName();
|
||||
return fileDownloader.DownloadToFile(tempLocation, onProgress);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -71,6 +71,7 @@
|
||||
<Compile Include="Exception\WoxPluginException.cs" />
|
||||
<Compile Include="Updater\Release.cs" />
|
||||
<Compile Include="Updater\UpdaterManager.cs" />
|
||||
<Compile Include="Updater\WoxUpdateSource.cs" />
|
||||
<Compile Include="UserSettings\HttpProxy.cs" />
|
||||
<Compile Include="i18n\AvailableLanguages.cs" />
|
||||
<Compile Include="i18n\IInternationalization.cs" />
|
||||
|
||||
Reference in New Issue
Block a user