ShareX/ShareX.HelpersLib/UpdateChecker/UpdateChecker.cs

109 lines
3.4 KiB
C#
Raw Normal View History

2013-11-03 23:53:49 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2024-01-03 12:57:14 +13:00
Copyright (c) 2007-2024 ShareX Team
2013-11-03 23:53:49 +13:00
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;
2022-01-13 12:59:56 +13:00
using System.Threading.Tasks;
2014-07-23 12:18:49 +12:00
using System.Web;
using System.Windows.Forms;
2013-11-03 23:53:49 +13:00
2014-12-11 09:25:20 +13:00
namespace ShareX.HelpersLib
2013-11-03 23:53:49 +13:00
{
public abstract class UpdateChecker
2013-11-03 23:53:49 +13:00
{
2018-10-17 23:31:39 +13:00
/// <summary>For testing purposes.</summary>
2020-09-08 19:18:00 +12:00
public static bool ForceUpdate { get; private set; } = false;
2018-10-17 23:31:39 +13:00
2014-07-23 12:18:49 +12:00
public UpdateStatus Status { get; set; }
public Version CurrentVersion { get; set; }
2014-07-23 12:18:49 +12:00
public Version LatestVersion { get; set; }
public ReleaseChannelType ReleaseType { get; set; }
public bool IsDev { get; set; }
public bool IsPortable { get; set; }
2024-04-02 18:29:07 +13:00
public bool IgnoreRevision { get; set; }
2013-11-03 23:53:49 +13:00
2021-12-12 10:21:19 +13:00
private string fileName;
2014-07-23 12:18:49 +12:00
2021-12-12 10:21:19 +13:00
public string FileName
2014-07-23 12:18:49 +12:00
{
get
{
2021-12-12 10:21:19 +13:00
if (string.IsNullOrEmpty(fileName))
2014-07-23 12:18:49 +12:00
{
return HttpUtility.UrlDecode(DownloadURL.Substring(DownloadURL.LastIndexOf('/') + 1));
}
2021-12-12 10:21:19 +13:00
return fileName;
2014-07-23 12:18:49 +12:00
}
set
{
2021-12-12 10:21:19 +13:00
fileName = value;
2014-07-23 12:18:49 +12:00
}
}
public string DownloadURL { get; set; }
public void RefreshStatus()
2013-11-03 23:53:49 +13:00
{
2014-07-23 12:18:49 +12:00
if (CurrentVersion == null)
{
CurrentVersion = Version.Parse(Application.ProductVersion);
}
if (Status != UpdateStatus.UpdateCheckFailed && CurrentVersion != null && LatestVersion != null && !string.IsNullOrEmpty(DownloadURL) &&
2024-04-02 18:29:07 +13:00
(ForceUpdate || Helpers.CompareVersion(CurrentVersion, LatestVersion, IgnoreRevision) < 0))
2014-07-23 12:18:49 +12:00
{
Status = UpdateStatus.UpdateAvailable;
}
else
{
Status = UpdateStatus.UpToDate;
}
2013-11-03 23:53:49 +13:00
}
public abstract Task CheckUpdateAsync();
2022-01-13 12:59:56 +13:00
public void DownloadUpdate()
{
DebugHelper.WriteLine("Updating ShareX from version {0} to {1}", CurrentVersion, LatestVersion);
if (IsPortable)
{
URLHelpers.OpenURL(DownloadURL);
}
else
{
using (DownloaderForm updaterForm = new DownloaderForm(this))
{
updaterForm.ShowDialog();
if (updaterForm.Status == DownloaderFormStatus.InstallStarted)
{
Application.Exit();
}
}
}
}
2013-11-03 23:53:49 +13:00
}
}