2015-01-19 23:08:53 +08:00
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using System.Windows.Threading;
|
|
|
|
|
using NAppUpdate.Framework;
|
|
|
|
|
using NAppUpdate.Framework.Common;
|
|
|
|
|
using NAppUpdate.Framework.Sources;
|
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;
|
|
|
|
|
|
|
|
|
|
public static UpdaterManager Instance
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (instance == null)
|
|
|
|
|
{
|
|
|
|
|
instance = new UpdaterManager();
|
|
|
|
|
}
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-19 23:08:53 +08:00
|
|
|
|
private UpdaterManager()
|
|
|
|
|
{
|
|
|
|
|
UpdateManager.Instance.UpdateSource = GetUpdateSource();
|
|
|
|
|
}
|
2015-01-14 22:19:44 +08:00
|
|
|
|
|
|
|
|
|
public void CheckUpdate()
|
|
|
|
|
{
|
2015-01-19 23:08:53 +08:00
|
|
|
|
// Get a local pointer to the UpdateManager instance
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
((UpdateProcessAsyncResult) asyncResult).EndInvoke();
|
|
|
|
|
}
|
|
|
|
|
catch(System.Exception e)
|
|
|
|
|
{
|
|
|
|
|
Log.Error(e);
|
|
|
|
|
updManager.CleanUp();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-01-19 23:08:53 +08:00
|
|
|
|
|
|
|
|
|
// No updates were found, or an error has occured. We might want to check that...
|
|
|
|
|
if (updManager.UpdatesAvailable == 0)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("All is up to date!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updManager.BeginPrepareUpdates(result =>
|
|
|
|
|
{
|
|
|
|
|
((UpdateProcessAsyncResult)result).EndInvoke();
|
|
|
|
|
|
|
|
|
|
// 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,false,true);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// this.WindowState = WindowState.Normal;
|
|
|
|
|
MessageBox.Show(
|
|
|
|
|
"An error occurred while trying to install software updates");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updManager.CleanUp();
|
|
|
|
|
}, null);
|
|
|
|
|
}, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Reinstall()
|
|
|
|
|
{
|
|
|
|
|
UpdateManager.Instance.ReinstateIfRestarted();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnPrepareUpdatesCompleted(bool obj)
|
|
|
|
|
{
|
|
|
|
|
UpdateManager updManager = UpdateManager.Instance;
|
|
|
|
|
|
|
|
|
|
DialogResult dr = MessageBox.Show(
|
|
|
|
|
"Updates are ready to install. Do you wish to install them now?",
|
|
|
|
|
"Software updates ready",
|
|
|
|
|
MessageBoxButtons.YesNo);
|
|
|
|
|
|
|
|
|
|
if (dr == DialogResult.Yes)
|
2015-01-14 22:19:44 +08:00
|
|
|
|
{
|
2015-01-19 23:08:53 +08:00
|
|
|
|
// This is a synchronous method by design, make sure to save all user work before calling
|
|
|
|
|
// it as it might restart your application
|
|
|
|
|
updManager.ApplyUpdates(true,true,true);
|
2015-01-14 22:19:44 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-01-19 23:08:53 +08:00
|
|
|
|
|
|
|
|
|
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 NAppUpdate.Framework.Sources.SimpleWebSource("http://127.0.0.1:8888/Update.xml");
|
|
|
|
|
return source;
|
|
|
|
|
}
|
2015-01-14 22:19:44 +08:00
|
|
|
|
}
|
|
|
|
|
}
|