namespace VideoBrowser.Controls.CefSharpBrowser.Handlers { using CefSharp; using Ookii.Dialogs.Wpf; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Windows; using VideoBrowser.Common; using VideoBrowser.Controls.CefSharpBrowser.Models; using VideoBrowser.Helpers; /// /// Defines the . /// public class DownloadHandler : IDownloadHandler, IDisposable { #region Constructors /// /// Initializes a new instance of the class. /// /// The downloadItemModels. public DownloadHandler(IList downloadItemModels) { this.DownloadItemModels = downloadItemModels; this.DownloadItemDict = new ConcurrentDictionary(); } #endregion Constructors #region Properties /// /// Gets a value indicating whether Disposed. /// public bool Disposed { get; private set; } /// /// Gets the DownloadItemDict. /// public IDictionary DownloadItemDict { get; } /// /// Gets the DownloadItemModels. /// public IList DownloadItemModels { get; } /// /// Gets or sets the DownloadPath. /// public string DownloadPath { get; set; } = AppEnvironment.UserVideoFolder; /// /// Gets or sets a value indicating whether IsShowDialog. /// public bool IsShowDialog { get; set; } = true; /// /// Gets or sets a value indicating whether IsCancelAllDownloads. /// private bool IsCancelAllDownloads { get; set; } /// /// Gets the Lock. /// private object Lock { get; } = new object(); #endregion Properties #region Methods /// /// The Dispose. /// public void Dispose() { if (this.Disposed) { return; } this.Disposed = true; } /// /// The OnBeforeDownload. /// /// The chromiumWebBrowser. /// The browser. /// The downloadItem. /// The callback. public void OnBeforeDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback) { lock (this.Lock) { if (this.DownloadItemDict.ContainsKey(downloadItem.Id) || callback.IsDisposed) { return; } UIThreadHelper.Invoke(() => { using (callback) { var fileName = downloadItem.SuggestedFileName; var filePath = Path.Combine(this.DownloadPath, downloadItem.SuggestedFileName); if (this.IsShowDialog) { var dialog = new VistaSaveFileDialog { FileName = fileName, CheckPathExists = true, InitialDirectory = this.DownloadPath, OverwritePrompt = true, Title = "Save Link to...", }; var element = chromiumWebBrowser as FrameworkElement; var window = Window.GetWindow(element); if (!(bool)dialog.ShowDialog(window)) { return; } filePath = dialog.FileName; } this.DownloadPath = Path.GetDirectoryName(filePath); var model = new DownloadProcessModel(downloadItem); this.DownloadItemDict.Add(downloadItem.Id, model); this.DownloadItemModels.Insert(0, model); callback.Continue(filePath, false); } }); } } /// /// The OnDownloadUpdated. /// /// The chromiumWebBrowser. /// The browser. /// The downloadItem. /// The callback. public void OnDownloadUpdated(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback) { lock (this.Lock) { var id = downloadItem.Id; if (!this.DownloadItemDict.TryGetValue(id, out DownloadProcessModel processModel)) { return; } if (processModel.IsCanceled || this.IsCancelAllDownloads) { this.DownloadItemDict.Remove(id); this.DownloadItemModels.Remove(processModel); callback.Cancel(); return; } processModel.UpdateInfo(downloadItem); } } #endregion Methods } }