92 lines
No EOL
3 KiB
C#
92 lines
No EOL
3 KiB
C#
// Copyright © 2013 The CefSharp Authors. All rights reserved.
|
|
//
|
|
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
|
|
|
|
using System;
|
|
using System.ComponentModel;
|
|
using System.IO;
|
|
using System.Windows.Forms;
|
|
using CefSharp;
|
|
using Timer = System.Threading.Timer;
|
|
// ReSharper disable NotAccessedField.Local
|
|
|
|
namespace XeroBrowser
|
|
{
|
|
public class DownloadHandler : IDownloadHandler
|
|
{
|
|
public event EventHandler<DownloadItem> OnBeforeDownloadFired;
|
|
|
|
public event EventHandler<DownloadItem> OnDownloadUpdatedFired;
|
|
|
|
private readonly FrmBrowser _frmref;
|
|
public DownloadHandler(FrmBrowser frm)
|
|
{
|
|
_frmref = frm;
|
|
}
|
|
|
|
private readonly DownloadProgress _downloadProgress = new DownloadProgress();
|
|
private DownloadItem _downloadItem;
|
|
public void OnBeforeDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback)
|
|
{
|
|
OnBeforeDownloadFired?.Invoke(this, downloadItem);
|
|
|
|
this._downloadItem = downloadItem;
|
|
|
|
try
|
|
{
|
|
_frmref.Invoke((Action)delegate
|
|
{
|
|
_downloadProgress.Show();
|
|
_downloadProgress.FormClosing += DownloadProgress_FormClosing;
|
|
});
|
|
}
|
|
catch (ObjectDisposedException){}
|
|
|
|
|
|
if (!callback.IsDisposed)
|
|
{
|
|
using (callback)
|
|
{
|
|
callback.Continue(downloadItem.SuggestedFileName, showDialog: true);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void DownloadProgress_FormClosing(object sender, FormClosingEventArgs e)
|
|
{
|
|
_downloadItem.IsCancelled = true;
|
|
}
|
|
|
|
private Timer _fivesecondtimer;
|
|
public void OnDownloadUpdated(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback)
|
|
{
|
|
OnDownloadUpdatedFired?.Invoke(this, downloadItem);
|
|
|
|
|
|
if (downloadItem.IsComplete)
|
|
{
|
|
_fivesecondtimer = new Timer(tcallback, null, 1000 * 5, 1000 * 5);
|
|
}
|
|
|
|
|
|
_frmref.Invoke((Action)delegate {
|
|
_downloadProgress.ProgressBar1.Value = downloadItem.PercentComplete;
|
|
_downloadProgress.ProgressPrecentage.Text = downloadItem.PercentComplete + @"% " + (downloadItem.ReceivedBytes / 0x100000) + @"MB / " + (downloadItem.TotalBytes / 0x100000) + @"MB";
|
|
_downloadProgress.ProgressFileName.Text = Path.GetFileName(downloadItem.FullPath);
|
|
_downloadProgress.Update();
|
|
try { } catch (InvalidAsynchronousStateException){}
|
|
});
|
|
}
|
|
|
|
private void tcallback(object state)
|
|
{
|
|
try {
|
|
_frmref.Invoke((Action)delegate
|
|
{
|
|
_downloadProgress.Close();
|
|
});
|
|
}
|
|
catch (InvalidOperationException){}
|
|
}
|
|
}
|
|
} |