ShareX/ShareX.HelpersLib/UpdateChecker/DownloaderForm.cs

267 lines
9.1 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)
2014-12-11 09:25:20 +13:00
using ShareX.HelpersLib.Properties;
2013-11-03 23:53:49 +13:00
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Threading;
2023-10-28 18:15:24 +13:00
using System.Threading.Tasks;
2013-11-03 23:53:49 +13:00
using System.Windows.Forms;
2014-12-11 09:25:20 +13:00
namespace ShareX.HelpersLib
2013-11-03 23:53:49 +13:00
{
2022-01-12 06:51:31 +13:00
public partial class DownloaderForm : Form
2013-11-03 23:53:49 +13:00
{
2014-05-10 12:23:47 +12:00
public delegate void DownloaderInstallEventHandler(string filePath);
public event DownloaderInstallEventHandler InstallRequested;
2013-11-03 23:53:49 +13:00
public string URL { get; set; }
2021-12-12 10:21:19 +13:00
public string FileName { get; set; }
public string DownloadLocation { get; private set; }
public string AcceptHeader { get; set; }
2013-11-03 23:53:49 +13:00
public bool AutoStartDownload { get; set; }
public InstallType InstallType { get; set; }
public bool AutoStartInstall { get; set; }
public DownloaderFormStatus Status { get; private set; }
public bool RunInstallerInBackground { get; set; }
2013-11-03 23:53:49 +13:00
private FileDownloader fileDownloader;
2014-05-10 12:23:47 +12:00
private DownloaderForm()
2013-11-03 23:53:49 +13:00
{
InitializeComponent();
2022-01-12 06:51:31 +13:00
ShareXResources.ApplyTheme(this);
2013-11-03 23:53:49 +13:00
ChangeStatus(Resources.DownloaderForm_DownloaderForm_Waiting_);
2013-11-03 23:53:49 +13:00
Status = DownloaderFormStatus.Waiting;
AutoStartDownload = true;
InstallType = InstallType.Silent;
AutoStartInstall = true;
RunInstallerInBackground = true;
2013-11-03 23:53:49 +13:00
}
2021-12-12 10:21:19 +13:00
public DownloaderForm(string url, string fileName) : this()
{
URL = url;
2021-12-12 10:21:19 +13:00
FileName = fileName;
lblFilename.Text = Helpers.SafeStringFormat(Resources.DownloaderForm_DownloaderForm_Filename___0_, FileName);
}
2021-12-12 10:21:19 +13:00
public DownloaderForm(UpdateChecker updateChecker) : this(updateChecker.DownloadURL, updateChecker.FileName)
2013-11-03 23:53:49 +13:00
{
if (updateChecker is GitHubUpdateChecker)
2013-11-03 23:53:49 +13:00
{
AcceptHeader = "application/octet-stream";
2013-11-03 23:53:49 +13:00
}
}
2013-11-03 23:53:49 +13:00
2023-10-28 18:15:24 +13:00
private async void DownloaderForm_Shown(object sender, EventArgs e)
2013-11-03 23:53:49 +13:00
{
if (AutoStartDownload)
{
2023-10-28 18:15:24 +13:00
await StartDownload();
2013-11-03 23:53:49 +13:00
}
}
2023-10-28 18:15:24 +13:00
private async void btnAction_MouseClick(object sender, MouseEventArgs e)
2013-11-03 23:53:49 +13:00
{
if (e.Button == MouseButtons.Left)
{
if (Status == DownloaderFormStatus.Waiting)
{
2023-10-28 18:15:24 +13:00
await StartDownload();
2013-11-03 23:53:49 +13:00
}
else if (Status == DownloaderFormStatus.DownloadCompleted)
{
DialogResult = DialogResult.OK;
2013-11-03 23:53:49 +13:00
Install();
}
else
{
DialogResult = DialogResult.Cancel;
2013-11-03 23:53:49 +13:00
Close();
}
}
}
public void Install()
{
if (Status == DownloaderFormStatus.DownloadCompleted)
{
Status = DownloaderFormStatus.InstallStarted;
btnAction.Enabled = false;
RunInstallerWithDelay();
Close();
}
}
// This function will give time for ShareX to close so installer won't tell ShareX is already running
private void RunInstallerWithDelay(int delay = 1000)
{
if (RunInstallerInBackground)
2013-11-03 23:53:49 +13:00
{
Thread thread = new Thread(() =>
{
Thread.Sleep(delay);
RunInstaller();
});
2023-10-28 18:15:24 +13:00
thread.Start();
}
else
{
Hide();
2013-11-03 23:53:49 +13:00
RunInstaller();
}
2013-11-03 23:53:49 +13:00
}
private void RunInstaller()
{
2014-05-10 12:23:47 +12:00
if (InstallType == InstallType.Event)
2013-11-03 23:53:49 +13:00
{
2014-05-10 12:23:47 +12:00
OnInstallRequested();
}
else
{
try
{
2018-12-07 07:51:41 +13:00
using (Process process = new Process())
{
2018-12-07 07:51:41 +13:00
ProcessStartInfo psi = new ProcessStartInfo()
{
FileName = DownloadLocation,
Arguments = "/UPDATE",
UseShellExecute = true
2018-12-07 07:51:41 +13:00
};
if (InstallType == InstallType.Silent)
{
psi.Arguments += " /SILENT";
}
else if (InstallType == InstallType.VerySilent)
{
psi.Arguments += " /VERYSILENT";
}
if (Helpers.IsDefaultInstallDir() && !Helpers.IsMemberOfAdministratorsGroup())
2018-12-07 07:51:41 +13:00
{
psi.Verb = "runas";
}
process.StartInfo = psi;
process.Start();
2014-05-10 12:23:47 +12:00
}
}
2018-05-16 20:09:01 +12:00
catch
{
}
2014-05-10 12:23:47 +12:00
}
}
2014-05-10 12:23:47 +12:00
protected void OnInstallRequested()
{
if (InstallRequested != null)
{
DialogResult = DialogResult.OK;
InstallRequested(DownloadLocation);
2013-11-03 23:53:49 +13:00
}
}
private void ChangeStatus(string status)
{
lblStatus.Text = Helpers.SafeStringFormat(Resources.DownloaderForm_ChangeStatus_Status___0_, status);
2013-11-03 23:53:49 +13:00
}
2023-10-28 18:15:24 +13:00
private async Task StartDownload()
2013-11-03 23:53:49 +13:00
{
if (!string.IsNullOrEmpty(URL) && Status == DownloaderFormStatus.Waiting)
{
Status = DownloaderFormStatus.DownloadStarted;
btnAction.Text = Resources.DownloaderForm_StartDownload_Cancel;
2013-11-03 23:53:49 +13:00
string folderPath = Path.Combine(Path.GetTempPath(), "ShareX");
FileHelpers.CreateDirectory(folderPath);
2021-12-12 10:21:19 +13:00
DownloadLocation = Path.Combine(folderPath, FileName);
2020-09-06 22:35:32 +12:00
DebugHelper.WriteLine($"Downloading: \"{URL}\" -> \"{DownloadLocation}\"");
2023-10-28 18:15:24 +13:00
fileDownloader = new FileDownloader(URL, DownloadLocation);
fileDownloader.AcceptHeader = AcceptHeader;
2023-10-30 01:05:47 +13:00
fileDownloader.FileSizeReceived += FileDownloader_FileSizeReceived;
2023-10-28 18:15:24 +13:00
fileDownloader.ProgressChanged += FileDownloader_ProgressChanged;
2023-10-30 01:05:47 +13:00
ChangeStatus(Resources.DownloaderForm_StartDownload_Getting_file_size_);
2023-10-29 18:50:58 +13:00
try
{
2023-10-30 01:05:47 +13:00
bool downloadStatus = await fileDownloader.StartDownload();
if (downloadStatus)
{
ChangeStatus(Resources.DownloaderForm_fileDownloader_DownloadCompleted_Download_completed_);
Status = DownloaderFormStatus.DownloadCompleted;
btnAction.Text = Resources.DownloaderForm_fileDownloader_DownloadCompleted_Install;
if (AutoStartInstall)
{
Install();
}
}
2023-10-29 18:50:58 +13:00
}
catch (Exception e)
{
ChangeStatus(e.Message);
}
2013-11-03 23:53:49 +13:00
}
}
2023-10-30 01:05:47 +13:00
private void FileDownloader_FileSizeReceived()
2023-10-28 18:15:24 +13:00
{
ChangeStatus(Resources.DownloaderForm_StartDownload_Downloading_);
2023-10-30 01:05:47 +13:00
FileDownloader_ProgressChanged();
2023-10-28 18:15:24 +13:00
}
private void FileDownloader_ProgressChanged()
{
if (fileDownloader != null)
{
pbProgress.Value = (int)Math.Round(fileDownloader.DownloadPercentage);
lblProgress.Text = Helpers.SafeStringFormat(CultureInfo.CurrentCulture, Resources.DownloaderForm_ChangeProgress_Progress,
fileDownloader.DownloadPercentage, fileDownloader.DownloadSpeed / 1024, fileDownloader.DownloadedSize / 1024, fileDownloader.FileSize / 1024);
}
}
private void DownloaderForm_FormClosing(object sender, FormClosingEventArgs e)
2013-11-03 23:53:49 +13:00
{
if (Status == DownloaderFormStatus.DownloadStarted && fileDownloader != null)
{
fileDownloader.StopDownload();
}
}
}
}