SubtitleEdit/libse/Forms/CheckForUpdatesHelper.cs

161 lines
5.0 KiB
C#
Raw Normal View History

using System;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
namespace Nikse.SubtitleEdit.Core.Forms
{
public class CheckForUpdatesHelper
{
2018-01-27 13:50:44 +01:00
private static readonly Regex VersionNumberRegex = new Regex(@"\d\.\d", RegexOptions.Compiled); // 3.4.0 (xth June 2014)
private const string ChangeLogUrl = "https://raw.githubusercontent.com/SubtitleEdit/subtitleedit/master/Changelog.txt";
private string _changeLog;
private int _successCount;
public string Error { get; set; }
2016-01-24 11:51:04 +01:00
2018-01-27 13:50:44 +01:00
public bool Done => _successCount == 1;
2016-01-24 11:51:04 +01:00
public string LatestVersionNumber { get; set; }
public string LatestChangeLog { get; set; }
private void StartDownloadString(string url, string contentType, AsyncCallback callback)
{
try
{
var request = (HttpWebRequest)WebRequest.Create(url);
request.UserAgent = "SubtitleEdit";
request.ContentType = contentType;
request.Timeout = Timeout.Infinite;
request.Method = "GET";
request.AllowAutoRedirect = true;
request.Accept = contentType;
2018-01-26 22:04:42 +01:00
request.Proxy = Utilities.GetProxy();
request.BeginGetResponse(callback, request);
}
catch (Exception exception)
{
if (Error == null)
{
Error = exception.Message;
}
}
}
private void FinishWebRequestChangeLog(IAsyncResult result)
{
try
{
_changeLog = GetStringFromResponse(result);
LatestChangeLog = GetLastestChangeLog(_changeLog);
LatestVersionNumber = GetLastestVersionNumber(LatestChangeLog);
}
catch (Exception exception)
{
if (Error == null)
{
Error = exception.Message;
}
}
}
private static string GetLastestVersionNumber(string latestChangeLog)
{
foreach (string line in latestChangeLog.Replace(Environment.NewLine, "\n").Split('\n'))
{
string s = line.Trim();
2018-01-27 13:50:44 +01:00
if (!s.Contains("BETA", StringComparison.OrdinalIgnoreCase) && !s.Contains('x') && !s.Contains('*') && s.Contains('(') && s.Contains(')') && VersionNumberRegex.IsMatch(s))
{
int indexOfSpace = s.IndexOf(' ');
if (indexOfSpace > 0)
2019-01-19 14:40:37 +01:00
{
return s.Substring(0, indexOfSpace).Trim();
2019-01-19 14:40:37 +01:00
}
}
}
return null;
}
private static string GetLastestChangeLog(string changeLog)
{
bool releaseOn = false;
var sb = new StringBuilder();
foreach (string line in changeLog.Replace(Environment.NewLine, "\n").Split('\n'))
{
string s = line.Trim();
if (s.Length == 0 && releaseOn)
2019-01-19 14:40:37 +01:00
{
return sb.ToString();
2019-01-19 14:40:37 +01:00
}
if (!releaseOn)
{
2018-01-27 13:50:44 +01:00
if (!s.Contains('x') && !s.Contains('*') && s.Contains('(') && s.Contains(')') && VersionNumberRegex.IsMatch(s))
2019-01-19 14:40:37 +01:00
{
releaseOn = true;
2019-01-19 14:40:37 +01:00
}
}
if (releaseOn)
{
sb.AppendLine(line);
}
}
return sb.ToString();
}
private string GetStringFromResponse(IAsyncResult result)
{
HttpWebResponse response = (result.AsyncState as HttpWebRequest).EndGetResponse(result) as HttpWebResponse;
System.IO.Stream responseStream = response.GetResponseStream();
byte[] buffer = new byte[5000000];
int count = 1;
int index = 0;
while (count > 0)
{
count = responseStream.Read(buffer, index, 2048);
index += count;
}
if (index > 0)
2019-01-19 14:40:37 +01:00
{
_successCount++;
2019-01-19 14:40:37 +01:00
}
return Encoding.UTF8.GetString(buffer, 0, index);
}
public CheckForUpdatesHelper()
{
Error = null;
_successCount = 0;
}
public void CheckForUpdates()
{
// load change log
StartDownloadString(ChangeLogUrl, null, FinishWebRequestChangeLog);
}
public bool IsUpdateAvailable()
{
2018-01-26 22:04:42 +01:00
if (!string.IsNullOrEmpty(Error))
{
return false;
}
try
{
//string[] currentVersionInfo = "3.3.14".Split('.'); // for testing...
return Version.Parse(LatestVersionNumber) > Version.Parse(Utilities.AssemblyVersion);
}
catch
{
return false;
}
}
}
2016-01-24 11:51:04 +01:00
}