Check updates

This commit is contained in:
niksedk 2014-06-22 00:31:08 +02:00
parent d0946d0498
commit 35bea46883
3 changed files with 94 additions and 10 deletions

View File

@ -20,6 +20,7 @@ namespace Nikse.SubtitleEdit.Forms
buttonDownloadAndInstall.Visible = false;
textBoxChangeLog.Visible = false;
buttonCancel.Text = Configuration.Settings.Language.General.OK;
buttonCancel.Visible = false;
}
private void CheckForUpdates_KeyDown(object sender, KeyEventArgs e)
@ -43,21 +44,32 @@ namespace Nikse.SubtitleEdit.Forms
{
timerCheckForUpdates.Stop();
labelStatus.Text = string.Format(Configuration.Settings.Language.CheckForUpdates.CheckingForUpdatesFailedX, "Time out");
buttonCancel.Visible = true;
}
else if (_updatesHelper.Error != null)
{
timerCheckForUpdates.Stop();
labelStatus.Text = string.Format(Configuration.Settings.Language.CheckForUpdates.CheckingForUpdatesFailedX, _updatesHelper.Error);
buttonCancel.Visible = true;
}
else if (_updatesHelper.Done)
{
timerCheckForUpdates.Stop();
if (_updatesHelper.IsUpdateAvailable())
{
Height = 600;
textBoxChangeLog.Text = _updatesHelper.LatestChangeLog;
textBoxChangeLog.Visible = true;
labelStatus.Text = Configuration.Settings.Language.CheckForUpdates.CheckingForUpdatesNewVersion;
buttonDownloadAndInstall.Visible = true;
buttonCancel.Text = Configuration.Settings.Language.General.Cancel;
buttonCancel.Visible = true;
}
else
{
labelStatus.Text = Configuration.Settings.Language.CheckForUpdates.CheckingForUpdatesNoneAvailable;
buttonCancel.Visible = true;
}
}
_seconds += timerCheckForUpdates.Interval / 1000.0;
}

View File

@ -1,11 +1,15 @@
using System;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
namespace Nikse.SubtitleEdit.Logic.Forms
{
public class CheckForUpdatesHelper
{
private static Regex regex = new Regex(@"\d\.\d", RegexOptions.Compiled); // 3.4.0 (xth June 2014)
private const string ReleasesUrl = "https://api.github.com/repos/SubtitleEdit/subtitleedit/releases";
private const string ChangeLogUrl = "https://raw.githubusercontent.com/SubtitleEdit/subtitleedit/master/src/Changelog.txt";
@ -18,7 +22,7 @@ namespace Nikse.SubtitleEdit.Logic.Forms
{
get
{
return _successCount == 2;
return _successCount == 1;
}
private set
{
@ -70,7 +74,8 @@ namespace Nikse.SubtitleEdit.Logic.Forms
try
{
_changeLog = GetStringFromResponse(result);
LatestChangeLog = _changeLog;
LatestChangeLog = GetLastestChangeLog(_changeLog);
LatestVersionNumber = GetLastestVersionNumber(LatestChangeLog);
}
catch (Exception exception)
{
@ -81,6 +86,45 @@ namespace Nikse.SubtitleEdit.Logic.Forms
}
}
private string GetLastestVersionNumber(string latestChangeLog)
{
foreach (string line in latestChangeLog.Replace(Environment.NewLine, "\n").Split('\n'))
{
string s = line.Trim();
if (!s.Contains("x") && !s.Contains("*") && s.Contains("(") && s.Contains(")") && regex.IsMatch(s))
{
int indexOfSpace = s.IndexOf(" ");
if (indexOfSpace > 0)
return s.Substring(0, indexOfSpace).Trim();
}
}
return null;
}
private 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)
return sb.ToString();
if (!releaseOn)
{
if (!s.Contains("x") && !s.Contains("*") && s.Contains("(") && s.Contains(")") && regex.IsMatch(s))
releaseOn = true;
}
if (releaseOn)
{
sb.AppendLine(line);
}
}
return sb.ToString();
}
private string GetStringFromResponse(IAsyncResult result)
{
HttpWebResponse response = (result.AsyncState as HttpWebRequest).EndGetResponse(result) as HttpWebResponse;
@ -107,11 +151,39 @@ namespace Nikse.SubtitleEdit.Logic.Forms
public void CheckForUpdates()
{
// load github release json
StartDownloadString(ReleasesUrl, "application/json", new AsyncCallback(FinishWebRequestReleases));
//StartDownloadString(ReleasesUrl, "application/json", new AsyncCallback(FinishWebRequestReleases));
// load change log
StartDownloadString(ChangeLogUrl, null, new AsyncCallback(FinishWebRequestChangeLog));
}
public bool IsUpdateAvailable()
{
string[] currentVersionInfo = Utilities.AssemblyVersion.Split('.');
string minorMinorVersion = string.Empty;
if (currentVersionInfo.Length >= 3 && currentVersionInfo[2] != "0")
minorMinorVersion = "." + currentVersionInfo[2];
string currentVersion = String.Format("{0}.{1}{2}", currentVersionInfo[0], currentVersionInfo[1], minorMinorVersion);
if (currentVersion == LatestVersionNumber)
return false;
string[] latestVersionInfo = LatestVersionNumber.Split('.');
try
{
if (int.Parse(latestVersionInfo[0]) > int.Parse(currentVersionInfo[0]))
return true;
if (int.Parse(latestVersionInfo[0]) == int.Parse(currentVersionInfo[0]) && int.Parse(latestVersionInfo[1]) > int.Parse(currentVersionInfo[1]))
return true;
if (int.Parse(latestVersionInfo[0]) == int.Parse(currentVersionInfo[0]) && int.Parse(latestVersionInfo[1]) == int.Parse(currentVersionInfo[1]) && int.Parse(latestVersionInfo[2]) > int.Parse(currentVersionInfo[2]))
return true;
}
catch
{
return false;
}
return false;
}
}
}

View File

@ -337,7 +337,7 @@ namespace Nikse.SubtitleEdit.Logic
CheckingForUpdatesFailedX = "Checking for updates failed: {0}",
CheckingForUpdatesNoneAvailable = "You're using the latest version of Subtitle Edit :)",
CheckingForUpdatesNewVersion = "New version available!",
InstallUpdate = "Download and install",
InstallUpdate = "Go to download page",
};
ChooseAudioTrack = new LanguageStructure.ChooseAudioTrack