Beta support

This commit is contained in:
Jaex 2014-07-23 03:18:49 +03:00
parent dd171e810d
commit b0453e4055
12 changed files with 75 additions and 124 deletions

View file

@ -331,6 +331,16 @@ public static int CompareVersion(string version1, string version2)
return NormalizeVersion(version1).CompareTo(NormalizeVersion(version2));
}
/// <summary>
/// If version1 newer than version2 = 1
/// If version1 equal to version2 = 0
/// If version1 older than version2 = -1
/// </summary>
public static int CompareVersion(Version version1, Version version2)
{
return version1.Normalize().CompareTo(version2.Normalize());
}
/// <summary>
/// If version newer than ApplicationVersion = 1
/// If version equal to ApplicationVersion = 0
@ -346,14 +356,6 @@ private static Version NormalizeVersion(string version)
return Version.Parse(version).Normalize();
}
/// <summary>
/// If latestVersion newer than currentVersion = true
/// </summary>
public static bool CheckVersion(Version currentVersion, Version latestVersion)
{
return currentVersion.Normalize() < latestVersion.Normalize();
}
public static bool IsWindowsXP()
{
return OSVersion.Major == 5 && OSVersion.Minor == 1;

View file

@ -331,7 +331,6 @@
<DependentUpon>MyPictureBox.cs</DependentUpon>
</Compile>
<Compile Include="UpdateChecker\XMLUpdateChecker.cs" />
<Compile Include="UpdateChecker\UpdateInfo.cs" />
<Compile Include="Links.cs" />
<Compile Include="Vector2.cs" />
<Compile Include="WindowState.cs" />

View file

@ -75,7 +75,7 @@ private DownloaderForm()
}
public DownloaderForm(UpdateChecker updateChecker)
: this(updateChecker.UpdateInfo)
: this(updateChecker.DownloadURL, updateChecker.Filename)
{
Proxy = updateChecker.Proxy;
@ -85,11 +85,6 @@ public DownloaderForm(UpdateChecker updateChecker)
}
}
public DownloaderForm(UpdateInfo updateInfo)
: this(updateInfo.DownloadURL, updateInfo.Filename)
{
}
public DownloaderForm(string url, string filename)
: this()
{

View file

@ -54,8 +54,6 @@ public GitHubUpdateChecker(string owner, string repo)
public override void CheckUpdate()
{
UpdateInfo = new UpdateInfo { CurrentVersion = this.CurrentVersion };
try
{
List<GitHubRelease> releases = GetReleases();
@ -67,7 +65,7 @@ public override void CheckUpdate()
if (latestRelease != null && !string.IsNullOrEmpty(latestRelease.tag_name) && latestRelease.tag_name.Length > 1 &&
latestRelease.tag_name[0] == 'v')
{
UpdateInfo.LatestVersion = new Version(latestRelease.tag_name.Substring(1));
LatestVersion = new Version(latestRelease.tag_name.Substring(1));
if (latestRelease.assets != null && latestRelease.assets.Count > 0)
{
@ -75,9 +73,9 @@ public override void CheckUpdate()
{
if (asset != null && !string.IsNullOrEmpty(asset.name) && asset.name.EndsWith(".exe", StringComparison.InvariantCultureIgnoreCase))
{
UpdateInfo.Filename = asset.name;
UpdateInfo.DownloadURL = asset.url;
UpdateInfo.RefreshStatus();
Filename = asset.name;
DownloadURL = asset.url;
RefreshStatus();
return;
}
}
@ -90,7 +88,7 @@ public override void CheckUpdate()
DebugHelper.WriteException(e, "GitHub update check failed");
}
UpdateInfo.Status = UpdateStatus.UpdateCheckFailed;
Status = UpdateStatus.UpdateCheckFailed;
}
public string GetLatestDownloadURL()

View file

@ -25,21 +25,59 @@
using System;
using System.Net;
using System.Web;
using System.Windows.Forms;
namespace HelpersLib
{
public abstract class UpdateChecker
{
public UpdateStatus Status { get; set; }
public Version CurrentVersion { get; set; }
public Version LatestVersion { get; set; }
public ReleaseChannelType ReleaseType { get; set; }
public bool IsBeta { get; set; }
public IWebProxy Proxy { get; set; }
public UpdateInfo UpdateInfo { get; protected set; }
public UpdateChecker()
private string filename;
public string Filename
{
CurrentVersion = Version.Parse(Application.ProductVersion);
ReleaseType = ReleaseChannelType.Stable;
get
{
if (string.IsNullOrEmpty(filename))
{
return HttpUtility.UrlDecode(DownloadURL.Substring(DownloadURL.LastIndexOf('/') + 1));
}
return filename;
}
set
{
filename = value;
}
}
public string DownloadURL { get; set; }
private const bool forceUpdate = false; // For testing purposes
public void RefreshStatus()
{
if (CurrentVersion == null)
{
CurrentVersion = Version.Parse(Application.ProductVersion);
}
if (Status != UpdateStatus.UpdateCheckFailed && CurrentVersion != null && LatestVersion != null && !string.IsNullOrEmpty(DownloadURL) &&
(forceUpdate || Helpers.CompareVersion(CurrentVersion, LatestVersion) < 0 || (IsBeta && Helpers.CompareVersion(CurrentVersion, LatestVersion) == 0)))
{
Status = UpdateStatus.UpdateAvailable;
}
else
{
Status = UpdateStatus.UpToDate;
}
}
public abstract void CheckUpdate();

View file

@ -70,7 +70,7 @@ private void UpdateControls()
pbLoading.Visible = false;
lblCheckingUpdates.Visible = false;
switch (updateChecker.UpdateInfo.Status)
switch (updateChecker.Status)
{
case UpdateStatus.UpdateCheckFailed:
lblStatus.Text = "Update check failed";
@ -90,7 +90,7 @@ private void UpdateControls()
private void llblUpdateAvailable_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
if (updateChecker != null && updateChecker.UpdateInfo != null && updateChecker.UpdateInfo.Status == UpdateStatus.UpdateAvailable)
if (updateChecker != null && updateChecker.Status == UpdateStatus.UpdateAvailable)
{
using (DownloaderForm updaterForm = new DownloaderForm(updateChecker))
{

View file

@ -1,85 +0,0 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (C) 2007-2014 ShareX Developers
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using System;
using System.Web;
using System.Windows.Forms;
namespace HelpersLib
{
public class UpdateInfo
{
public UpdateStatus Status { get; set; }
public Version CurrentVersion { get; set; }
public Version LatestVersion { get; set; }
private string filename;
public string Filename
{
get
{
if (string.IsNullOrEmpty(filename))
{
return HttpUtility.UrlDecode(DownloadURL.Substring(DownloadURL.LastIndexOf('/') + 1));
}
return filename;
}
set
{
filename = value;
}
}
public string DownloadURL { get; set; }
public ReleaseChannelType ReleaseChannel { get; set; }
private bool forceUpdate = false; // For testing purposes
public UpdateInfo()
{
ReleaseChannel = ReleaseChannelType.Stable;
}
public void RefreshStatus()
{
if (CurrentVersion == null)
{
CurrentVersion = Version.Parse(Application.ProductVersion);
}
if (Status != UpdateStatus.UpdateCheckFailed && CurrentVersion != null && LatestVersion != null && !string.IsNullOrEmpty(DownloadURL) &&
(forceUpdate || Helpers.CheckVersion(CurrentVersion, LatestVersion)))
{
Status = UpdateStatus.UpdateAvailable;
}
else
{
Status = UpdateStatus.UpToDate;
}
}
}
}

View file

@ -45,8 +45,6 @@ public XMLUpdateChecker(string url, string applicationName)
public override void CheckUpdate()
{
UpdateInfo = new UpdateInfo { CurrentVersion = this.CurrentVersion, ReleaseChannel = ReleaseType };
try
{
using (WebClient wc = new WebClient())
@ -83,9 +81,9 @@ public override void CheckUpdate()
if (xe != null)
{
UpdateInfo.LatestVersion = new Version(xe.Element("Version").Value);
UpdateInfo.DownloadURL = xe.Element("URL").Value;
UpdateInfo.RefreshStatus();
LatestVersion = new Version(xe.Element("Version").Value);
DownloadURL = xe.Element("URL").Value;
RefreshStatus();
return;
}
}
@ -97,7 +95,7 @@ public override void CheckUpdate()
DebugHelper.WriteException(e, "XML update check failed");
}
UpdateInfo.Status = UpdateStatus.UpdateCheckFailed;
Status = UpdateStatus.UpdateCheckFailed;
}
}
}

View file

@ -533,8 +533,9 @@ private void CheckUpdate()
{
UpdateChecker updateChecker = TaskHelpers.CheckUpdate();
if (updateChecker.UpdateInfo != null && updateChecker.UpdateInfo.Status == UpdateStatus.UpdateAvailable &&
MessageBox.Show("A newer version of ShareX is available.\r\nWould you like to download and install it?", string.Format("{0} {1} is available", Application.ProductName, updateChecker.UpdateInfo.LatestVersion.ToString()),
if (updateChecker != null && updateChecker.Status == UpdateStatus.UpdateAvailable &&
MessageBox.Show("A newer version of ShareX is available.\r\nWould you like to download and install it?",
string.Format("ShareX {0} is available", updateChecker.LatestVersion.ToString()),
MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
{
using (DownloaderForm updaterForm = new DownloaderForm(updateChecker))

View file

@ -213,6 +213,8 @@ public static string ToolsFolder
#endregion Paths
public const bool IsBeta = true;
public static string Title
{
get
@ -221,6 +223,7 @@ public static string Title
string title = string.Format("ShareX {0}.{1}", version.Major, version.Minor);
if (version.Build > 0) title += "." + version.Build;
if (IsPortable) title += " Portable";
if (IsBeta) title += " Beta";
return title;
}
}

View file

@ -11,5 +11,5 @@
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("82E6AC09-0FEF-4390-AD9F-0DD3F5561EFC")]
[assembly: AssemblyVersion("9.2.1")]
[assembly: AssemblyFileVersion("9.2.1")]
[assembly: AssemblyVersion("9.3.0")]
[assembly: AssemblyFileVersion("9.3.0")]

View file

@ -423,13 +423,15 @@ public static Icon GetProgressIcon(int percentage)
public static UpdateChecker CheckUpdate()
{
UpdateChecker updateChecker = new GitHubUpdateChecker("ShareX", "ShareX");
updateChecker.IsBeta = Program.IsBeta;
updateChecker.Proxy = ProxyInfo.Current.GetWebProxy();
updateChecker.CheckUpdate();
// Fallback if GitHub API fails
if (updateChecker.UpdateInfo == null || updateChecker.UpdateInfo.Status == UpdateStatus.UpdateCheckFailed)
if (updateChecker.Status == UpdateStatus.None || updateChecker.Status == UpdateStatus.UpdateCheckFailed)
{
updateChecker = new XMLUpdateChecker("http://getsharex.com/Update.xml", "ShareX");
updateChecker.IsBeta = Program.IsBeta;
updateChecker.Proxy = ProxyInfo.Current.GetWebProxy();
updateChecker.CheckUpdate();
}