PowerToys/Wox/Update/UpdateChecker.cs

56 lines
1.5 KiB
C#
Raw Normal View History

2014-12-14 23:16:29 +08:00
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using Newtonsoft.Json;
2015-01-11 21:52:30 +08:00
using Wox.Core;
using Wox.Core.UserSettings;
2015-01-11 21:52:30 +08:00
using Wox.Core.Version;
2014-12-14 23:16:29 +08:00
using Wox.Helper;
using Wox.Infrastructure;
2014-12-21 22:03:03 +08:00
using Wox.Infrastructure.Http;
2014-12-14 23:16:29 +08:00
namespace Wox.Update
{
public class UpdateChecker
{
2014-12-16 22:25:22 +08:00
private static Release newRelease;
private static bool checkedUpdate = false;
2014-12-14 23:16:29 +08:00
/// <summary>
/// If new release is available, then return the new release
/// otherwise, return null
/// </summary>
/// <returns></returns>
2014-12-16 22:25:22 +08:00
public Release CheckUpgrade(bool forceCheck = false)
2014-12-14 23:16:29 +08:00
{
2014-12-16 22:25:22 +08:00
if (checkedUpdate && !forceCheck) return newRelease;
2015-01-11 21:52:30 +08:00
string json = HttpRequest.Get(APIServer.LastestReleaseURL,HttpProxy.Instance);
2014-12-21 22:03:03 +08:00
if (string.IsNullOrEmpty(json)) return null;
2014-12-16 22:25:22 +08:00
2014-12-21 22:03:03 +08:00
try
2014-12-14 23:16:29 +08:00
{
2014-12-21 22:03:03 +08:00
newRelease = JsonConvert.DeserializeObject<Release>(json);
if (!IsNewerThanCurrent(newRelease))
2014-12-14 23:16:29 +08:00
{
2014-12-21 22:03:03 +08:00
newRelease = null;
2014-12-14 23:16:29 +08:00
}
2014-12-21 22:03:03 +08:00
checkedUpdate = true;
2014-12-14 23:16:29 +08:00
}
2014-12-21 22:03:03 +08:00
catch{}
2014-12-14 23:16:29 +08:00
2014-12-16 22:25:22 +08:00
return newRelease;
2014-12-14 23:16:29 +08:00
}
private bool IsNewerThanCurrent(Release release)
{
if (release == null) return false;
2015-01-11 21:52:30 +08:00
return new SemanticVersion(release.version) > VersionManager.Instance.CurrentVersion;
2014-12-14 23:16:29 +08:00
}
}
}