PowerToys/Wox.Core/Updater/UpdaterManager.cs

183 lines
5.6 KiB
C#
Raw Normal View History

2015-01-27 21:51:29 +08:00
using System;
2015-02-01 22:46:56 +08:00
using System.Collections.Generic;
2015-01-27 21:51:29 +08:00
using System.Reflection;
2015-10-31 07:17:34 +08:00
using System.Threading;
using System.Windows.Forms;
using NAppUpdate.Framework;
using NAppUpdate.Framework.Common;
using NAppUpdate.Framework.Sources;
2015-02-01 22:46:56 +08:00
using NAppUpdate.Framework.Tasks;
2015-01-27 21:51:29 +08:00
using Newtonsoft.Json;
using Wox.Core.Resource;
2015-01-21 23:00:56 +08:00
using Wox.Core.UserSettings;
2015-01-27 21:51:29 +08:00
using Wox.Infrastructure.Http;
2015-01-20 20:05:38 +08:00
using Wox.Infrastructure.Logger;
2015-01-14 22:19:44 +08:00
namespace Wox.Core.Updater
{
public class UpdaterManager
{
private static UpdaterManager instance;
private const string VersionCheckURL = "http://api.getwox.com/release/latest/";
2015-02-02 23:28:40 +08:00
private const string UpdateFeedURL = "http://upgrade.getwox.com/update.xml";
//private const string UpdateFeedURL = "http://127.0.0.1:8888/update.xml";
2015-01-27 21:51:29 +08:00
private static SemanticVersion currentVersion;
private UserSettingStorage _settings;
2015-01-14 22:19:44 +08:00
2015-02-01 22:46:56 +08:00
public event EventHandler PrepareUpdateReady;
public event EventHandler UpdateError;
public Release NewRelease { get; set; }
2015-01-14 22:19:44 +08:00
public static UpdaterManager Instance
{
get
{
if (instance == null)
{
instance = new UpdaterManager();
}
return instance;
}
}
private UpdaterManager()
{
UpdateManager.Instance.UpdateSource = GetUpdateSource();
_settings = UserSettingStorage.Instance;
}
2015-01-14 22:19:44 +08:00
2015-01-27 21:51:29 +08:00
public SemanticVersion CurrentVersion
2015-01-21 23:00:56 +08:00
{
2015-01-27 21:51:29 +08:00
get
{
if (currentVersion == null)
{
currentVersion = new SemanticVersion(Assembly.GetExecutingAssembly().GetName().Version);
}
return currentVersion;
}
}
private bool IsNewerThanCurrent(Release release)
{
if (release == null) return false;
return new SemanticVersion(release.version) > CurrentVersion;
2015-01-21 23:00:56 +08:00
}
2015-02-01 22:46:56 +08:00
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;
}
2015-01-14 22:19:44 +08:00
public void CheckUpdate()
2015-01-27 21:51:29 +08:00
{
2015-02-27 18:04:49 +08:00
ThreadPool.QueueUserWorkItem(o =>
2015-01-27 21:51:29 +08:00
{
2015-02-27 18:04:49 +08:00
string json = HttpRequest.Get(VersionCheckURL, HttpProxy.Instance);
if (!string.IsNullOrEmpty(json))
2015-01-27 21:51:29 +08:00
{
2015-02-27 18:04:49 +08:00
try
2015-01-27 21:51:29 +08:00
{
2015-02-27 18:04:49 +08:00
NewRelease = JsonConvert.DeserializeObject<Release>(json);
if (IsNewerThanCurrent(NewRelease) && !_settings.DontPromptUpdateMsg)
2015-02-27 18:04:49 +08:00
{
StartUpdate();
}
}
2016-01-07 05:34:42 +08:00
catch (Exception e)
2015-02-27 18:04:49 +08:00
{
Log.Error(e);
2015-01-27 21:51:29 +08:00
}
}
2015-02-27 18:04:49 +08:00
});
2015-01-27 21:51:29 +08:00
}
private void StartUpdate()
2015-01-14 22:19:44 +08:00
{
UpdateManager updManager = UpdateManager.Instance;
updManager.BeginCheckForUpdates(asyncResult =>
{
if (asyncResult.IsCompleted)
{
// still need to check for caught exceptions if any and rethrow
2015-01-20 20:05:38 +08:00
try
{
2015-01-21 23:00:56 +08:00
((UpdateProcessAsyncResult)asyncResult).EndInvoke();
2015-01-20 20:05:38 +08:00
}
2016-01-07 05:34:42 +08:00
catch (Exception e)
2015-01-20 20:05:38 +08:00
{
Log.Error(e);
updManager.CleanUp();
return;
}
// No updates were found, or an error has occured. We might want to check that...
if (updManager.UpdatesAvailable == 0)
{
return;
}
}
updManager.BeginPrepareUpdates(result =>
{
((UpdateProcessAsyncResult)result).EndInvoke();
2015-02-01 22:46:56 +08:00
OnPrepareUpdateReady();
}, null);
}, null);
}
2015-02-01 22:46:56 +08:00
public void CleanUp()
{
UpdateManager.Instance.CleanUp();
}
2015-01-21 23:00:56 +08:00
2015-02-01 22:46:56 +08:00
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, _settings.EnableUpdateLog, false);
2015-02-01 22:46:56 +08:00
}
2016-01-07 05:34:42 +08:00
catch (Exception e)
2015-02-01 22:46:56 +08:00
{
string updateError = InternationalizationManager.Instance.GetTranslation("update_wox_update_error");
Log.Error(e);
MessageBox.Show(updateError);
OnUpdateError();
}
2015-01-21 23:00:56 +08:00
2015-02-01 22:46:56 +08:00
UpdateManager.Instance.CleanUp();
}
private IUpdateSource GetUpdateSource()
{
2015-02-01 22:46:56 +08:00
var source = new WoxUpdateSource(UpdateFeedURL, HttpRequest.GetWebProxy(HttpProxy.Instance));
return source;
}
2015-02-01 22:46:56 +08:00
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);
}
2015-01-14 22:19:44 +08:00
}
}