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
|
2022-01-12 05:32:17 +13:00
|
|
|
|
Copyright (c) 2007-2022 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.Net;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
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; }
|
2017-12-15 09:57:13 +13:00
|
|
|
|
public string DownloadLocation { get; private set; }
|
2013-11-03 23:53:49 +13:00
|
|
|
|
public IWebProxy Proxy { get; set; }
|
2013-11-15 19:37:30 +13:00
|
|
|
|
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; }
|
2014-05-10 16:17:56 +12:00
|
|
|
|
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
|
|
|
|
|
2014-10-18 00:08:33 +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;
|
2014-05-10 16:17:56 +12:00
|
|
|
|
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()
|
2017-12-15 11:47:24 +13:00
|
|
|
|
{
|
|
|
|
|
URL = url;
|
2021-12-12 10:21:19 +13:00
|
|
|
|
FileName = fileName;
|
|
|
|
|
lblFilename.Text = Helpers.SafeStringFormat(Resources.DownloaderForm_DownloaderForm_Filename___0_, FileName);
|
2017-12-15 11:47:24 +13:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
{
|
2013-11-28 12:18:00 +13:00
|
|
|
|
Proxy = updateChecker.Proxy;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2013-11-28 12:18:00 +13:00
|
|
|
|
if (updateChecker is GitHubUpdateChecker)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2013-11-28 12:18:00 +13:00
|
|
|
|
AcceptHeader = "application/octet-stream";
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
2013-11-15 19:37:30 +13:00
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
|
|
private void DownloaderForm_Shown(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (AutoStartDownload)
|
|
|
|
|
{
|
|
|
|
|
StartDownload();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void btnAction_MouseClick(object sender, MouseEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (e.Button == MouseButtons.Left)
|
|
|
|
|
{
|
|
|
|
|
if (Status == DownloaderFormStatus.Waiting)
|
|
|
|
|
{
|
|
|
|
|
StartDownload();
|
|
|
|
|
}
|
|
|
|
|
else if (Status == DownloaderFormStatus.DownloadCompleted)
|
|
|
|
|
{
|
2014-05-10 16:17:56 +12:00
|
|
|
|
DialogResult = DialogResult.OK;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
Install();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-05-10 16:17:56 +12:00
|
|
|
|
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)
|
|
|
|
|
{
|
2014-05-10 16:17:56 +12:00
|
|
|
|
if (RunInstallerInBackground)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2018-08-04 02:43:21 +12:00
|
|
|
|
Thread thread = new Thread(() =>
|
2014-05-10 16:17:56 +12:00
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(delay);
|
|
|
|
|
RunInstaller();
|
|
|
|
|
});
|
2018-08-04 02:43:21 +12:00
|
|
|
|
thread.Start();
|
2014-05-10 16:17:56 +12:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Hide();
|
2013-11-03 23:53:49 +13:00
|
|
|
|
RunInstaller();
|
2014-05-10 16:17:56 +12:00
|
|
|
|
}
|
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
|
2014-04-09 23:07:33 +12:00
|
|
|
|
{
|
2018-12-07 07:51:41 +13:00
|
|
|
|
using (Process process = new Process())
|
2018-03-26 09:36:15 +13:00
|
|
|
|
{
|
2018-12-07 07:51:41 +13:00
|
|
|
|
ProcessStartInfo psi = new ProcessStartInfo()
|
|
|
|
|
{
|
|
|
|
|
FileName = DownloadLocation,
|
|
|
|
|
Arguments = "/UPDATE",
|
2019-01-01 13:45:01 +13:00
|
|
|
|
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";
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-23 22:08:29 +13:00
|
|
|
|
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
|
|
|
|
}
|
2014-04-09 23:07:33 +12:00
|
|
|
|
}
|
2018-05-16 20:09:01 +12:00
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
}
|
2014-05-10 12:23:47 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-04-10 00:21:18 +12:00
|
|
|
|
|
2014-05-10 12:23:47 +12:00
|
|
|
|
protected void OnInstallRequested()
|
|
|
|
|
{
|
|
|
|
|
if (InstallRequested != null)
|
|
|
|
|
{
|
2014-05-10 16:17:56 +12:00
|
|
|
|
DialogResult = DialogResult.OK;
|
2017-12-15 09:57:13 +13:00
|
|
|
|
InstallRequested(DownloadLocation);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ChangeStatus(string status)
|
|
|
|
|
{
|
2017-11-02 00:38:07 +13:00
|
|
|
|
lblStatus.Text = Helpers.SafeStringFormat(Resources.DownloaderForm_ChangeStatus_Status___0_, status);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ChangeProgress()
|
|
|
|
|
{
|
|
|
|
|
if (fileDownloader != null)
|
|
|
|
|
{
|
|
|
|
|
pbProgress.Value = (int)Math.Round(fileDownloader.DownloadPercentage);
|
2017-11-02 00:38:07 +13:00
|
|
|
|
lblProgress.Text = Helpers.SafeStringFormat(CultureInfo.CurrentCulture, Resources.DownloaderForm_ChangeProgress_Progress,
|
2014-05-29 17:39:06 +12:00
|
|
|
|
fileDownloader.DownloadPercentage, fileDownloader.DownloadSpeed / 1024, fileDownloader.DownloadedSize / 1024, fileDownloader.FileSize / 1024);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void StartDownload()
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(URL) && Status == DownloaderFormStatus.Waiting)
|
|
|
|
|
{
|
|
|
|
|
Status = DownloaderFormStatus.DownloadStarted;
|
2014-10-18 00:08:33 +13:00
|
|
|
|
btnAction.Text = Resources.DownloaderForm_StartDownload_Cancel;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2016-07-23 04:07:23 +12:00
|
|
|
|
string folderPath = Path.Combine(Path.GetTempPath(), "ShareX");
|
2022-05-03 18:34:18 +12:00
|
|
|
|
FileHelpers.CreateDirectory(folderPath);
|
2021-12-12 10:21:19 +13:00
|
|
|
|
DownloadLocation = Path.Combine(folderPath, FileName);
|
2016-07-23 04:07:23 +12:00
|
|
|
|
|
2020-09-06 22:35:32 +12:00
|
|
|
|
DebugHelper.WriteLine($"Downloading: \"{URL}\" -> \"{DownloadLocation}\"");
|
|
|
|
|
|
2017-12-15 09:57:13 +13:00
|
|
|
|
fileDownloader = new FileDownloader(URL, DownloadLocation, Proxy, AcceptHeader);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
fileDownloader.FileSizeReceived += (v1, v2) => ChangeProgress();
|
2014-10-18 00:08:33 +13:00
|
|
|
|
fileDownloader.DownloadStarted += (v1, v2) => ChangeStatus(Resources.DownloaderForm_StartDownload_Downloading_);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
fileDownloader.ProgressChanged += (v1, v2) => ChangeProgress();
|
|
|
|
|
fileDownloader.DownloadCompleted += fileDownloader_DownloadCompleted;
|
2019-05-08 22:38:12 +12:00
|
|
|
|
fileDownloader.ExceptionThrown += (v1, v2) => ChangeStatus(fileDownloader.LastException.Message);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
fileDownloader.StartDownload();
|
|
|
|
|
|
2014-10-18 00:08:33 +13:00
|
|
|
|
ChangeStatus(Resources.DownloaderForm_StartDownload_Getting_file_size_);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void fileDownloader_DownloadCompleted(object sender, EventArgs e)
|
|
|
|
|
{
|
2014-10-18 00:08:33 +13:00
|
|
|
|
ChangeStatus(Resources.DownloaderForm_fileDownloader_DownloadCompleted_Download_completed_);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
Status = DownloaderFormStatus.DownloadCompleted;
|
2014-10-18 00:08:33 +13:00
|
|
|
|
btnAction.Text = Resources.DownloaderForm_fileDownloader_DownloadCompleted_Install;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
|
|
if (AutoStartInstall)
|
|
|
|
|
{
|
|
|
|
|
Install();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-15 13:32:41 +13:00
|
|
|
|
private void DownloaderForm_FormClosing(object sender, FormClosingEventArgs e)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
if (Status == DownloaderFormStatus.DownloadStarted && fileDownloader != null)
|
|
|
|
|
{
|
|
|
|
|
fileDownloader.StopDownload();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|