Change update logic

This commit is contained in:
qianlifeng
2015-01-27 21:51:29 +08:00
parent c7e73924c8
commit 4ecff94aec
9 changed files with 66 additions and 57 deletions

View File

@@ -0,0 +1,11 @@
namespace Wox.Core.Updater
{
public class Release
{
public string version { get; set; }
public string download_link { get; set; }
public string download_link1 { get; set; }
public string download_link2 { get; set; }
public string description { get; set; }
}
}

View File

@@ -0,0 +1,95 @@
using System;
using Wox.Core.Exception;
namespace Wox.Core.Updater
{
public class SemanticVersion : IComparable
{
public int MAJOR { get; set; }
public int MINOR { get; set; }
public int PATCH { get; set; }
public SemanticVersion(System.Version version)
{
MAJOR = version.Major;
MINOR = version.Minor;
PATCH = version.Build;
}
public SemanticVersion(int major, int minor, int patch)
{
MAJOR = major;
MINOR = minor;
PATCH = patch;
}
public SemanticVersion(string version)
{
var strings = version.Split('.');
if (strings.Length != 3)
{
throw new WoxException("Invalid semantic version");
}
MAJOR = int.Parse(strings[0]);
MINOR = int.Parse(strings[1]);
PATCH = int.Parse(strings[2]);
}
public static bool operator >(SemanticVersion v1, SemanticVersion v2)
{
return v1.CompareTo(v2) > 0;
}
public static bool operator <(SemanticVersion v1, SemanticVersion v2)
{
return v1.CompareTo(v2) < 0;
}
public static bool operator ==(SemanticVersion v1, SemanticVersion v2)
{
if (ReferenceEquals(v1, null))
{
return ReferenceEquals(v2, null);
}
if (ReferenceEquals(v2, null))
{
return false;
}
return v1.Equals(v2);
}
public static bool operator !=(SemanticVersion v1, SemanticVersion v2)
{
return !(v1 == v2);
}
public override string ToString()
{
return string.Format("{0}.{1}.{2}", MAJOR, MINOR, PATCH);
}
public override bool Equals(object version)
{
var v2 = (SemanticVersion)version;
return MAJOR == v2.MAJOR && MINOR == v2.MINOR && PATCH == v2.PATCH;
}
public int CompareTo(object version)
{
var v2 = (SemanticVersion)version;
if (MAJOR == v2.MAJOR)
{
if (MINOR == v2.MINOR)
{
if (PATCH == v2.PATCH)
{
return 0;
}
return PATCH - v2.PATCH;
}
return MINOR - v2.MINOR;
}
return MAJOR - v2.MAJOR;
}
}
}

View File

@@ -1,13 +1,15 @@

using System;
using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Windows.Threading;
using NAppUpdate.Framework;
using NAppUpdate.Framework.Common;
using NAppUpdate.Framework.Sources;
using Newtonsoft.Json;
using Wox.Core.i18n;
using Wox.Core.UserSettings;
using Wox.Infrastructure.Http;
using Wox.Infrastructure.Logger;
namespace Wox.Core.Updater
@@ -15,6 +17,9 @@ namespace Wox.Core.Updater
public class UpdaterManager
{
private static UpdaterManager instance;
private const string VersionCheckURL = "https://api.getwox.com/release/latest/";
private const string UpdateFeedURL = "http://127.0.0.1:8888/Update.xml";
private static SemanticVersion currentVersion;
public static UpdaterManager Instance
{
@@ -33,12 +38,45 @@ namespace Wox.Core.Updater
UpdateManager.Instance.UpdateSource = GetUpdateSource();
}
public bool IsUpdateAvailable()
public SemanticVersion CurrentVersion
{
return UpdateManager.Instance.UpdatesAvailable > 0;
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;
}
public void CheckUpdate()
{
string json = HttpRequest.Get(VersionCheckURL, HttpProxy.Instance);
if (!string.IsNullOrEmpty(json))
{
try
{
Release newRelease = JsonConvert.DeserializeObject<Release>(json);
if (IsNewerThanCurrent(newRelease))
{
StartUpdate();
}
}
catch
{
}
}
}
private void StartUpdate()
{
UpdateManager updManager = UpdateManager.Instance;
updManager.BeginCheckForUpdates(asyncResult =>
@@ -104,7 +142,7 @@ namespace Wox.Core.Updater
{
// Normally this would be a web based source.
// But for the demo app, we prepare an in-memory source.
var source = new SimpleWebSource("http://127.0.0.1:8888/Update.xml");
var source = new SimpleWebSource(UpdateFeedURL);
return source;
}
}