PowerToys/Wox/Update/UpdateChecker.cs

100 lines
3.0 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;
using Wox.Helper;
using Wox.Infrastructure;
namespace Wox.Update
{
public class UpdateChecker
{
2014-12-16 22:25:22 +08:00
private const string updateURL = "https://api.getwox.com/release/latest/";
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;
2014-12-14 23:16:29 +08:00
HttpWebResponse response = HttpRequest.CreateGetHttpResponse(updateURL, HttpProxy.Instance);
Stream s = response.GetResponseStream();
if (s != null)
{
StreamReader reader = new StreamReader(s, Encoding.UTF8);
string json = reader.ReadToEnd();
try
{
2014-12-16 22:25:22 +08:00
newRelease = JsonConvert.DeserializeObject<Release>(json);
2014-12-14 23:16:29 +08:00
}
catch
{
}
}
2014-12-16 22:25:22 +08:00
if (!IsNewerThanCurrent(newRelease))
2014-12-14 23:16:29 +08:00
{
2014-12-16 22:25:22 +08:00
newRelease = null;
2014-12-14 23:16:29 +08:00
}
2014-12-16 22:25:22 +08:00
checkedUpdate = true;
return newRelease;
2014-12-14 23:16:29 +08:00
}
private bool IsNewerThanCurrent(Release release)
{
if (release == null) return false;
string currentVersion = ConfigurationManager.AppSettings["version"];
return CompareVersion(release.version, currentVersion) > 0;
}
/// <summary>
/// if version1 > version2 return 1
/// else -1
/// </summary>
/// <param name="version1"></param>
/// <param name="version2"></param>
/// <returns></returns>
private int CompareVersion(string version1, string version2)
{
if (version1 == version2) return 0;
if (string.IsNullOrEmpty(version1) || string.IsNullOrEmpty(version2)) return 0;
//semantic version, e.g. 1.1.0
List<int> version1List = version1.Split('.').Select(int.Parse).ToList();
List<int> version2List = version2.Split('.').Select(int.Parse).ToList();
if (version1List[0] > version2List[0])
{
return 1;
}
else if (version1List[0] == version2List[0])
{
if (version1List[1] > version2List[1])
{
return 1;
}
else if (version1List[1] == version2List[1])
{
if (version1List[2] > version2List[2])
{
return 1;
}
}
}
return -1;
}
}
}