ShareX/ShareX.HelpersLib/UpdateChecker/DownloaderForm.cs

267 lines
9 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
2016-01-04 04:16:01 +13:00
Copyright (c) 2007-2016 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.Drawing;
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
{
public partial class DownloaderForm : BlackStyleForm
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; }
public string Filename { get; set; }
2013-11-03 23:53:49 +13:00
public string SavePath { get; private set; }
public IWebProxy Proxy { get; set; }
public string Changelog { get; 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;
private FileStream fileStream;
private Rectangle fillRect;
2014-05-10 12:23:47 +12:00
private DownloaderForm()
2013-11-03 23:53:49 +13:00
{
InitializeComponent();
fillRect = new Rectangle(0, 0, ClientSize.Width, ClientSize.Height);
UpdateFormSize();
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
}
2016-04-07 04:02:48 +12:00
public DownloaderForm(UpdateChecker updateChecker) : this(updateChecker.DownloadURL, updateChecker.Filename)
2013-11-03 23:53:49 +13:00
{
Proxy = updateChecker.Proxy;
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
2016-04-07 04:02:48 +12:00
public DownloaderForm(string url, string filename) : this()
{
2014-05-10 12:23:47 +12:00
URL = url;
Filename = filename;
lblFilename.Text = string.Format(Resources.DownloaderForm_DownloaderForm_Filename___0_, Filename);
}
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)
{
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();
});
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
{
2014-05-10 12:23:47 +12:00
ProcessStartInfo psi = new ProcessStartInfo(SavePath);
if (InstallType == InstallType.Silent)
{
psi.Arguments = "/SILENT";
}
else if (InstallType == InstallType.VerySilent)
{
psi.Arguments = "/VERYSILENT";
}
if (Helpers.IsDefaultInstallDir())
{
psi.Verb = "runas";
}
2014-05-10 12:23:47 +12:00
psi.UseShellExecute = true;
Process.Start(psi);
}
2014-05-10 12:23:47 +12:00
catch { }
}
}
2014-05-10 12:23:47 +12:00
protected void OnInstallRequested()
{
if (InstallRequested != null)
{
DialogResult = DialogResult.OK;
2014-05-10 12:23:47 +12:00
InstallRequested(SavePath);
2013-11-03 23:53:49 +13:00
}
}
private void ChangeStatus(string status)
{
lblStatus.Text = string.Format(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);
2015-09-08 08:20:35 +12:00
lblProgress.Text = string.Format(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;
btnAction.Text = Resources.DownloaderForm_StartDownload_Cancel;
2013-11-03 23:53:49 +13:00
SavePath = Path.Combine(Path.GetTempPath(), Filename);
2013-11-03 23:53:49 +13:00
fileStream = new FileStream(SavePath, FileMode.Create, FileAccess.Write, FileShare.Read);
fileDownloader = new FileDownloader(URL, fileStream, Proxy, AcceptHeader);
2013-11-03 23:53:49 +13:00
fileDownloader.FileSizeReceived += (v1, v2) => ChangeProgress();
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;
fileDownloader.ExceptionThrowed += (v1, v2) => ChangeStatus(fileDownloader.LastException.Message);
fileDownloader.StartDownload();
ChangeStatus(Resources.DownloaderForm_StartDownload_Getting_file_size_);
2013-11-03 23:53:49 +13:00
}
}
private void UpdateFormSize()
{
if (cbShowChangelog.Checked)
{
ClientSize = new Size(fillRect.Width, fillRect.Height);
}
else
{
ClientSize = new Size(fillRect.Width, txtChangelog.Location.Y - 3);
}
}
private void fileDownloader_DownloadCompleted(object sender, EventArgs e)
{
ChangeStatus(Resources.DownloaderForm_fileDownloader_DownloadCompleted_Download_completed_);
2013-11-03 23:53:49 +13:00
Status = DownloaderFormStatus.DownloadCompleted;
btnAction.Text = Resources.DownloaderForm_fileDownloader_DownloadCompleted_Install;
2013-11-03 23:53:49 +13:00
if (AutoStartInstall)
{
Install();
}
}
private void cbShowChangelog_CheckedChanged(object sender, EventArgs e)
{
UpdateFormSize();
}
private void UpdaterForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (Status == DownloaderFormStatus.DownloadStarted && fileDownloader != null)
{
fileDownloader.StopDownload();
}
}
}
}