diff --git a/HelpersLib/HelpersLib.csproj b/HelpersLib/HelpersLib.csproj index ac62339de..887630d0e 100644 --- a/HelpersLib/HelpersLib.csproj +++ b/HelpersLib/HelpersLib.csproj @@ -193,6 +193,7 @@ + Form @@ -282,7 +283,7 @@ MyPictureBox.cs - + diff --git a/HelpersLib/UpdateChecker/GitHubUpdateChecker.cs b/HelpersLib/UpdateChecker/GitHubUpdateChecker.cs index 2523afd9e..85abe9922 100644 --- a/HelpersLib/UpdateChecker/GitHubUpdateChecker.cs +++ b/HelpersLib/UpdateChecker/GitHubUpdateChecker.cs @@ -34,15 +34,12 @@ You should have received a copy of the GNU General Public License namespace HelpersLib { - public class GitHubUpdateChecker + public class GitHubUpdateChecker : UpdateChecker { - private const string APIURL = "https://api.github.com"; - public string Owner { get; private set; } public string Repo { get; private set; } - public Version CurrentVersion { get; private set; } - public IWebProxy Proxy { get; set; } - public UpdateInfo UpdateInfo { get; private set; } + + private const string APIURL = "https://api.github.com"; private string ReleasesURL { @@ -52,14 +49,13 @@ private string ReleasesURL } } - public GitHubUpdateChecker(string owner, string repo, Version currentVersion) + public GitHubUpdateChecker(string owner, string repo) { Owner = owner; Repo = repo; - CurrentVersion = currentVersion; } - public bool CheckUpdate() + public override void CheckUpdate() { UpdateInfo = new UpdateInfo { CurrentVersion = this.CurrentVersion }; @@ -85,7 +81,7 @@ public bool CheckUpdate() UpdateInfo.Filename = asset.name; UpdateInfo.DownloadURL = asset.url; UpdateInfo.RefreshStatus(); - return true; + return; } } } @@ -97,8 +93,6 @@ public bool CheckUpdate() } UpdateInfo.Status = UpdateStatus.UpdateCheckFailed; - - return false; } private string GetDownloadURL(GitHubRelease release) diff --git a/HelpersLib/UpdateChecker/UpdateChecker.cs b/HelpersLib/UpdateChecker/UpdateChecker.cs index 42e9e77d8..979ff7596 100644 --- a/HelpersLib/UpdateChecker/UpdateChecker.cs +++ b/HelpersLib/UpdateChecker/UpdateChecker.cs @@ -35,96 +35,18 @@ You should have received a copy of the GNU General Public License namespace HelpersLib { - public class UpdateChecker + public abstract class UpdateChecker { - public string URL { get; private set; } - public string ApplicationName { get; private set; } - public Version ApplicationVersion { get; private set; } - public ReleaseChannelType ReleaseChannel { get; private set; } - public IWebProxy Proxy { get; private set; } - public UpdateInfo UpdateInfo { get; private set; } + public Version CurrentVersion { get; set; } + public ReleaseChannelType ReleaseType { get; set; } + public IWebProxy Proxy { get; set; } + public UpdateInfo UpdateInfo { get; protected set; } - public UpdateChecker(string url, string applicationName, Version applicationVersion, - ReleaseChannelType channel = ReleaseChannelType.Stable, IWebProxy proxy = null) + public UpdateChecker() { - URL = url; - ApplicationName = applicationName; - ApplicationVersion = applicationVersion; - ReleaseChannel = channel; - Proxy = proxy; + ReleaseType = ReleaseChannelType.Stable; } - public bool CheckUpdate() - { - UpdateInfo = new UpdateInfo(); - UpdateInfo.ReleaseChannel = ReleaseChannel; - UpdateInfo.CurrentVersion = ApplicationVersion; - - try - { - RequestCachePolicy cachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore); - - using (WebClient wc = new WebClient { Proxy = Proxy, CachePolicy = cachePolicy }) - using (MemoryStream ms = new MemoryStream(wc.DownloadData(URL))) - using (XmlTextReader xml = new XmlTextReader(ms)) - { - XDocument xd = XDocument.Load(xml); - - if (xd != null) - { - string node; - - switch (ReleaseChannel) - { - default: - case ReleaseChannelType.Stable: - node = "Stable"; - break; - case ReleaseChannelType.Beta: - node = "Beta|Stable"; - break; - case ReleaseChannelType.Dev: - node = "Dev|Beta|Stable"; - break; - } - - string path = string.Format("Update/{0}/{1}", ApplicationName, node); - XElement xe = xd.GetNode(path); - - if (xe != null) - { - UpdateInfo.LatestVersion = new Version(xe.GetValue("Version")); - UpdateInfo.DownloadURL = xe.GetValue("URL"); - UpdateInfo.UpdateNotes = xe.GetValue("Summary"); - UpdateInfo.RefreshStatus(); - - if (UpdateInfo.Status == UpdateStatus.UpdateAvailable && !string.IsNullOrEmpty(UpdateInfo.UpdateNotes) && - UpdateInfo.UpdateNotes.IsValidUrl()) - { - try - { - wc.Encoding = Encoding.UTF8; - UpdateInfo.UpdateNotes = wc.DownloadString(UpdateInfo.UpdateNotes.Trim()); - } - catch (Exception ex) - { - DebugHelper.WriteException(ex); - } - } - - return true; - } - } - } - } - catch (Exception ex) - { - DebugHelper.WriteException(ex); - } - - UpdateInfo.Status = UpdateStatus.UpdateCheckFailed; - - return false; - } + public abstract void CheckUpdate(); } } \ No newline at end of file diff --git a/HelpersLib/UpdateChecker/UpdateCheckerLabel.cs b/HelpersLib/UpdateChecker/UpdateCheckerLabel.cs index bdbbb0d6f..d8ae680c2 100644 --- a/HelpersLib/UpdateChecker/UpdateCheckerLabel.cs +++ b/HelpersLib/UpdateChecker/UpdateCheckerLabel.cs @@ -29,9 +29,11 @@ You should have received a copy of the GNU General Public License namespace HelpersLib { + public delegate UpdateChecker CheckUpdate(); + public partial class UpdateCheckerLabel : UserControl { - private GitHubUpdateChecker updateChecker; + private UpdateChecker updateChecker; private bool isBusy; public UpdateCheckerLabel() @@ -39,10 +41,8 @@ public UpdateCheckerLabel() InitializeComponent(); } - public void CheckUpdate(GitHubUpdateChecker updateChecker) + public void CheckUpdate(CheckUpdate checkUpdate) { - this.updateChecker = updateChecker; - if (!isBusy) { isBusy = true; @@ -53,13 +53,13 @@ public void CheckUpdate(GitHubUpdateChecker updateChecker) pbLoading.Visible = true; lblCheckingUpdates.Visible = true; - new Thread(CheckingUpdate).Start(); + new Thread(() => CheckingUpdate(checkUpdate)).Start(); } } - private void CheckingUpdate() + private void CheckingUpdate(CheckUpdate checkUpdate) { - updateChecker.CheckUpdate(); + updateChecker = checkUpdate(); UpdateControls(); isBusy = false; } @@ -93,7 +93,7 @@ private void llblUpdateAvailable_LinkClicked(object sender, LinkLabelLinkClicked { if (updateChecker != null && updateChecker.UpdateInfo != null && updateChecker.UpdateInfo.Status == UpdateStatus.UpdateAvailable) { - UpdaterForm updaterForm = UpdaterForm.GetGitHubUpdaterForm(updateChecker); + UpdaterForm updaterForm = new UpdaterForm(updateChecker); updaterForm.ShowDialog(); if (updaterForm.Status == DownloaderFormStatus.InstallStarted) diff --git a/HelpersLib/UpdateChecker/UpdateInfo.cs b/HelpersLib/UpdateChecker/UpdateInfo.cs index f83c351a8..f14b07dac 100644 --- a/HelpersLib/UpdateChecker/UpdateInfo.cs +++ b/HelpersLib/UpdateChecker/UpdateInfo.cs @@ -26,6 +26,7 @@ You should have received a copy of the GNU General Public License using HelpersLib; using System; using System.Text; +using System.Web; namespace HelpersLib { @@ -34,9 +35,27 @@ public class UpdateInfo public UpdateStatus Status { get; set; } public Version CurrentVersion { get; set; } public Version LatestVersion { get; set; } - public string Filename { 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 string UpdateNotes { get; set; } public ReleaseChannelType ReleaseChannel { get; set; } private bool forceUpdate = false; // For testing purposes diff --git a/HelpersLib/UpdateChecker/UpdaterForm.cs b/HelpersLib/UpdateChecker/UpdaterForm.cs index 4957ffcd6..7a53756d8 100644 --- a/HelpersLib/UpdateChecker/UpdaterForm.cs +++ b/HelpersLib/UpdateChecker/UpdaterForm.cs @@ -71,39 +71,20 @@ private UpdaterForm() AutoStartInstall = true; } - public UpdaterForm(string url, IWebProxy proxy, string filename = "", string changelog = "") + public UpdaterForm(UpdateChecker updateChecker) : this() { - URL = url; - Proxy = proxy; - - if (string.IsNullOrEmpty(filename)) - { - Filename = HttpUtility.UrlDecode(URL.Substring(URL.LastIndexOf('/') + 1)); - } - else - { - Filename = filename; - } - + URL = updateChecker.UpdateInfo.DownloadURL; + Proxy = updateChecker.Proxy; + Filename = updateChecker.UpdateInfo.Filename; lblFilename.Text = "Filename: " + Filename; - if (!string.IsNullOrEmpty(changelog)) + if (updateChecker is GitHubUpdateChecker) { - Changelog = changelog; - txtChangelog.Text = Changelog; - cbShowChangelog.Visible = true; + AcceptHeader = "application/octet-stream"; } } - public static UpdaterForm GetGitHubUpdaterForm(GitHubUpdateChecker updateChecker) - { - UpdaterForm updaterForm = new UpdaterForm(updateChecker.UpdateInfo.DownloadURL, updateChecker.Proxy, - updateChecker.UpdateInfo.Filename, updateChecker.UpdateInfo.UpdateNotes); - updaterForm.AcceptHeader = "application/octet-stream"; - return updaterForm; - } - private void UpdaterForm_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; diff --git a/HelpersLib/UpdateChecker/XMLUpdateChecker.cs b/HelpersLib/UpdateChecker/XMLUpdateChecker.cs new file mode 100644 index 000000000..d7e28f099 --- /dev/null +++ b/HelpersLib/UpdateChecker/XMLUpdateChecker.cs @@ -0,0 +1,106 @@ +#region License Information (GPL v3) + +/* + ShareX - A program that allows you to take screenshots and share any file type + Copyright (C) 2008-2013 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 . +*/ + +#endregion License Information (GPL v3) + +using HelpersLib; +using System; +using System.Globalization; +using System.IO; +using System.Net; +using System.Net.Cache; +using System.Text; +using System.Xml; +using System.Xml.Linq; + +namespace HelpersLib +{ + public class XMLUpdateChecker : UpdateChecker + { + public string URL { get; private set; } + public string ApplicationName { get; private set; } + + public XMLUpdateChecker(string url, string applicationName) + { + URL = url; + ApplicationName = applicationName; + } + + public override void CheckUpdate() + { + UpdateInfo = new UpdateInfo { CurrentVersion = this.CurrentVersion, ReleaseChannel = ReleaseType }; + + try + { + using (WebClient wc = new WebClient()) + { + wc.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore); + wc.Headers.Add("user-agent", "ShareX"); + wc.Proxy = Proxy; + + using (MemoryStream ms = new MemoryStream(wc.DownloadData(URL))) + using (XmlTextReader xml = new XmlTextReader(ms)) + { + XDocument xd = XDocument.Load(xml); + + if (xd != null) + { + string node; + + switch (ReleaseType) + { + default: + case ReleaseChannelType.Stable: + node = "Stable"; + break; + case ReleaseChannelType.Beta: + node = "Beta|Stable"; + break; + case ReleaseChannelType.Dev: + node = "Dev|Beta|Stable"; + break; + } + + string path = string.Format("Update/{0}/{1}", ApplicationName, node); + XElement xe = xd.GetNode(path); + + if (xe != null) + { + UpdateInfo.LatestVersion = new Version(xe.Element("Version").Value); + UpdateInfo.DownloadURL = xe.Element("URL").Value; + UpdateInfo.RefreshStatus(); + return; + } + } + } + } + } + catch (Exception e) + { + DebugHelper.WriteException(e, "Update check failed"); + } + + UpdateInfo.Status = UpdateStatus.UpdateCheckFailed; + } + } +} \ No newline at end of file diff --git a/ShareX/Forms/AboutForm.cs b/ShareX/Forms/AboutForm.cs index 24fadb0ec..dfed594cb 100644 --- a/ShareX/Forms/AboutForm.cs +++ b/ShareX/Forms/AboutForm.cs @@ -43,10 +43,7 @@ public AboutForm() Text = Program.FullTitle; lblProductName.Text = Program.FullTitle; - GitHubUpdateChecker updateChecker = new GitHubUpdateChecker("ShareX", "ShareX", Program.AssemblyVersion); - updateChecker.Proxy = Uploader.ProxyInfo.GetWebProxy(); - - uclUpdate.CheckUpdate(updateChecker); + uclUpdate.CheckUpdate(TaskHelpers.CheckUpdate); rtbShareXInfo.AddContextMenu(); rtbCredits.AddContextMenu(); diff --git a/ShareX/Forms/MainForm.cs b/ShareX/Forms/MainForm.cs index 34ecb9130..d72263c42 100644 --- a/ShareX/Forms/MainForm.cs +++ b/ShareX/Forms/MainForm.cs @@ -411,15 +411,13 @@ private void UpdateUploaderMenuNames() private void CheckUpdate() { - GitHubUpdateChecker updateChecker = new GitHubUpdateChecker("ShareX", "ShareX", Program.AssemblyVersion); - updateChecker.Proxy = Uploader.ProxyInfo.GetWebProxy(); - updateChecker.CheckUpdate(); + UpdateChecker updateChecker = TaskHelpers.CheckUpdate(); if (updateChecker.UpdateInfo != null && updateChecker.UpdateInfo.Status == UpdateStatus.UpdateAvailable && MessageBox.Show("An update is available for ShareX.\r\nWould you like to download it?", "ShareX", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes) { - UpdaterForm updaterForm = UpdaterForm.GetGitHubUpdaterForm(updateChecker); + UpdaterForm updaterForm = new UpdaterForm(updateChecker); updaterForm.ShowDialog(); if (updaterForm.Status == DownloaderFormStatus.InstallStarted) diff --git a/ShareX/TaskHelpers.cs b/ShareX/TaskHelpers.cs index c72efa55b..eec820ba5 100644 --- a/ShareX/TaskHelpers.cs +++ b/ShareX/TaskHelpers.cs @@ -34,6 +34,7 @@ You should have received a copy of the GNU General Public License using System.IO; using System.Media; using System.Windows.Forms; +using UploadersLib; namespace ShareX { @@ -317,6 +318,25 @@ public static Icon GetProgressIcon(int percentage) return Icon.FromHandle(bmp.GetHicon()); } } + + public static UpdateChecker CheckUpdate() + { + UpdateChecker updateChecker = new GitHubUpdateChecker("ShareX", "ShareX"); + updateChecker.CurrentVersion = Program.AssemblyVersion; + updateChecker.Proxy = Uploader.ProxyInfo.GetWebProxy(); + updateChecker.CheckUpdate(); + + // Backup if GitHub API fails + if (updateChecker.UpdateInfo == null || updateChecker.UpdateInfo.Status == UpdateStatus.UpdateCheckFailed) + { + updateChecker = new XMLUpdateChecker("https://raw.github.com/ShareX/ShareX/master/Update.xml", "ShareX"); + updateChecker.CurrentVersion = Program.AssemblyVersion; + updateChecker.Proxy = Uploader.ProxyInfo.GetWebProxy(); + updateChecker.CheckUpdate(); + } + + return updateChecker; + } } public class PointInfo diff --git a/Update.xml b/Update.xml new file mode 100644 index 000000000..edad852ef --- /dev/null +++ b/Update.xml @@ -0,0 +1,9 @@ + + + + + 8.4.0 + https://github.com/ShareX/ShareX/releases/download/v8.4.0/ShareX-8.4.0-setup.exe + + + \ No newline at end of file