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
|
2015-08-13 13:07:38 +12:00
|
|
|
|
Copyright (c) 2007-2015 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.Drawing.Drawing2D;
|
|
|
|
|
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
|
|
|
|
{
|
2015-09-15 17:28:18 +12: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; }
|
2013-11-15 19:37:30 +13:00
|
|
|
|
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; }
|
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;
|
|
|
|
|
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();
|
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
|
|
|
|
}
|
|
|
|
|
|
2014-05-10 12:23:47 +12:00
|
|
|
|
public DownloaderForm(UpdateChecker updateChecker)
|
2014-07-23 12:18:49 +12:00
|
|
|
|
: 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
|
|
|
|
|
2014-05-10 12:23:47 +12:00
|
|
|
|
public DownloaderForm(string url, string filename)
|
2014-05-07 20:59:19 +12:00
|
|
|
|
: this()
|
|
|
|
|
{
|
2014-05-10 12:23:47 +12:00
|
|
|
|
URL = url;
|
|
|
|
|
Filename = filename;
|
2014-10-18 00:08:33 +13:00
|
|
|
|
lblFilename.Text = string.Format(Resources.DownloaderForm_DownloaderForm_Filename___0_, Filename);
|
2014-05-07 20:59:19 +12: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
|
|
|
|
{
|
2014-05-10 16:17:56 +12:00
|
|
|
|
Thread thread = new Thread(() =>
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(delay);
|
|
|
|
|
RunInstaller();
|
|
|
|
|
});
|
|
|
|
|
thread.Start();
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
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";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
psi.UseShellExecute = true;
|
|
|
|
|
Process.Start(psi);
|
2014-04-09 23:07:33 +12:00
|
|
|
|
}
|
2014-05-10 12:23:47 +12:00
|
|
|
|
catch { }
|
|
|
|
|
}
|
|
|
|
|
}
|
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;
|
2014-05-10 12:23:47 +12:00
|
|
|
|
InstallRequested(SavePath);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ChangeStatus(string status)
|
|
|
|
|
{
|
2014-10-18 00:08:33 +13:00
|
|
|
|
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;
|
2014-10-18 00:08:33 +13:00
|
|
|
|
btnAction.Text = Resources.DownloaderForm_StartDownload_Cancel;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2013-11-15 19:37:30 +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);
|
2013-11-15 19:37:30 +13:00
|
|
|
|
fileDownloader = new FileDownloader(URL, fileStream, 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;
|
|
|
|
|
fileDownloader.ExceptionThrowed += (v1, v2) => ChangeStatus(fileDownloader.LastException.Message);
|
|
|
|
|
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 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)
|
|
|
|
|
{
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|