Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class myDownload : nsIDownload
- {
- [Serializable]
- public delegate void SimpleEventHandler(object sender);
- private nsIDownload _download;
- #region Events
- protected EventHandlerList Events = new EventHandlerList();
- private static readonly object StartEvent = new object();
- private static readonly object CompleteEvent = new object();
- private static readonly object FailEvent = new object();
- private static readonly object ProgressEvent = new object();
- #region public event DownloadStarted
- /// <summary>
- /// Occurs before the Download was started.
- /// </summary>
- [Category("DownloadStarted")]
- [Description("Occurs before the Download was started.")]
- public event SimpleEventHandler DownloadStarted
- {
- add { Events.AddHandler(StartEvent, value); }
- remove { Events.RemoveHandler(StartEvent, value); }
- }
- /// <summary>Raises the <see cref="DownloadStarted"/> event.</summary>
- /// <param name="e">The data for the event.</param>
- protected virtual void OnDownloadStarted()
- {
- var evnt = ((SimpleEventHandler)Events[StartEvent]);
- if (evnt != null) evnt(this);
- }
- #endregion
- #region public event DownloadProgress
- /// <summary>
- /// Occurs before the Download was started.
- /// </summary>
- [Category("DownloadProgress")]
- [Description("Occurs before the Download is progressing.")]
- public event EventHandler<GeckoDownloadProgressEventArgs> DownloadProgress
- {
- add { Events.AddHandler(ProgressEvent, value); }
- remove { Events.RemoveHandler(ProgressEvent, value); }
- }
- /// <summary>Raises the <see cref="DownloadStarted"/> event.</summary>
- /// <param name="e">The data for the event.</param>
- protected virtual void OnDownloadProgress(GeckoDownloadProgressEventArgs e)
- {
- var evnt = ((EventHandler<GeckoDownloadProgressEventArgs>)Events[ProgressEvent]);
- if (evnt != null) evnt(this, e);
- }
- #endregion
- #region public event DownloadComplete
- /// <summary>
- /// Occurs before the Download was started.
- /// </summary>
- [Category("DownloadComplete")]
- [Description("Occurs before the Download was completed.")]
- public event SimpleEventHandler DownloadComplete
- {
- add { Events.AddHandler(CompleteEvent, value); }
- remove { Events.RemoveHandler(CompleteEvent, value); }
- }
- /// <summary>Raises the <see cref="DownloadStarted"/> event.</summary>
- /// <param name="e">The data for the event.</param>
- protected virtual void OnDownloadComplete()
- {
- var evnt = ((SimpleEventHandler)Events[CompleteEvent]);
- if (evnt != null) evnt(this);
- }
- #endregion
- #region public event DownloadFailed
- /// <summary>
- /// Occurs before the Download was started.
- /// </summary>
- [Category("DownloadFailed")]
- [Description("Occurs before the Download was started.")]
- public event SimpleEventHandler DownloadFailed
- {
- add { Events.AddHandler(FailEvent, value); }
- remove { Events.RemoveHandler(FailEvent, value); }
- }
- /// <summary>Raises the <see cref="DownloadStarted"/> event.</summary>
- /// <param name="e">The data for the event.</param>
- protected virtual void OnDownloadFailed()
- {
- var evnt = ((SimpleEventHandler)Events[FailEvent]);
- if (evnt != null) evnt(this);
- }
- #endregion
- #endregion
- public long ID;
- public readonly Uri Source;
- public readonly string File;
- public readonly long Size;
- public readonly string Mime;
- public myDownload(nsIDownload download)
- {
- _download = download;
- ID = DateTime.Now.Ticks;
- Source = (new nsURI(_download.GetSourceAttribute())).ToUri();
- File = nsString.Get(_download.GetTargetFileAttribute().GetTargetAttribute);
- Size = _download.GetSizeAttribute();
- var a = _download.GetMIMEInfoAttribute();
- if (a != null)
- Mime = nsString.Get(a.GetMIMETypeAttribute);
- }
- # region STATIC Downloads Methods
- static nsIDownloadManager DownloadManager = null;
- static public void CleanUpDownloads()
- {
- if (DownloadManager == null)
- DownloadManager = Xpcom.CreateInstance<nsIDownloadManager>("@mozilla.org/download-manager;1");
- nsISimpleEnumerator enu = DownloadManager.GetActiveDownloadsAttribute();
- while (enu.HasMoreElements())
- {
- nsIDownload d = (nsIDownload)enu.GetNext();
- nsICancelable c = d.GetCancelableAttribute();
- DownloadManager.CancelDownload(d.GetIdAttribute());
- };
- DownloadManager.CleanUp();
- }
- static public myDownload downloadFile(string localfile, string url)
- {
- if (DownloadManager == null)
- DownloadManager = Xpcom.CreateInstance<nsIDownloadManager>("@mozilla.org/download-manager;1");
- nsIURI source = IOService.CreateNsIUri(url);
- nsIURI dest = IOService.CreateFileNsIUri(localfile);
- var objTarget = Xpcom.CreateInstance<nsILocalFile>("@mozilla.org/file/local;1");
- using (nsAString tmp = new nsAString(localfile))
- objTarget.InitWithPath(tmp);
- nsIWebBrowserPersist persist = Xpcom.CreateInstance<nsIWebBrowserPersist>("@mozilla.org/embedding/browser/nsWebBrowserPersist;1");
- nsIDownload download = null;
- nsAStringBase t = (nsAStringBase)new nsAString(Path.GetFileName(localfile));
- download = DownloadManager.AddDownload(0, source, dest, t, null, 0, null, (nsICancelable)persist);
- if (download != null)
- {
- myDownload myD = new myDownload(download);
- persist.SetPersistFlagsAttribute(2 | 32 | 16384);
- persist.SetProgressListenerAttribute((nsIWebProgressListener)myD);
- persist.SaveURI(source, null, null, null, null, (nsISupports)dest);
- return myD;
- }
- return null;
- }
- #endregion
- public void CancelDownload()
- {
- if (DownloadManager == null)
- DownloadManager = Xpcom.CreateInstance<nsIDownloadManager>("@mozilla.org/download-manager;1");
- DownloadManager.CancelDownload(GetIdAttribute());
- DownloadManager.RemoveDownload(GetIdAttribute());
- }
- public void PauseDownload()
- {
- DownloadManager.PauseDownload(GetIdAttribute());
- }
- public void ResumeDownload()
- {
- DownloadManager.ResumeDownload(GetIdAttribute());
- }
- public long GetAmountTransferredAttribute()
- {
- return _download.GetAmountTransferredAttribute();
- }
- public Gecko.nsICancelable GetCancelableAttribute()
- {
- return _download.GetCancelableAttribute();
- }
- public void GetDisplayNameAttribute(Gecko.nsAStringBase aDisplayName)
- {
- _download.GetDisplayNameAttribute(aDisplayName);
- }
- public uint GetIdAttribute()
- {
- return _download.GetIdAttribute();
- }
- public Gecko.nsIMIMEInfo GetMIMEInfoAttribute()
- {
- return _download.GetMIMEInfoAttribute();
- }
- public int GetPercentCompleteAttribute()
- {
- return _download.GetPercentCompleteAttribute();
- }
- public Gecko.nsIURI GetReferrerAttribute()
- {
- return _download.GetReferrerAttribute();
- }
- public bool GetResumableAttribute()
- {
- return _download.GetResumableAttribute();
- }
- public long GetSizeAttribute()
- {
- return _download.GetSizeAttribute();
- }
- public nsIURI GetSourceAttribute()
- {
- return _download.GetSourceAttribute();
- }
- public double GetSpeedAttribute()
- {
- return _download.GetSpeedAttribute();
- }
- public long GetStartTimeAttribute()
- {
- return _download.GetStartTimeAttribute();
- }
- public short GetStateAttribute()
- {
- return _download.GetStateAttribute();
- }
- public nsIURI GetTargetAttribute()
- {
- return _download.GetTargetAttribute();
- }
- public nsIFile GetTargetFileAttribute()
- {
- return _download.GetTargetFileAttribute();
- }
- public void Init(Gecko.nsIURI aSource, Gecko.nsIURI aTarget, Gecko.nsAStringBase aDisplayName, Gecko.nsIMIMEInfo aMIMEInfo, long startTime, Gecko.nsIFile aTempFile, Gecko.nsICancelable aCancelable)
- {
- _download.Init(aSource, aTarget, aDisplayName, aMIMEInfo, startTime, aTempFile, aCancelable);
- }
- public void OnLocationChange(Gecko.nsIWebProgress aWebProgress, Gecko.nsIRequest aRequest, Gecko.nsIURI aLocation, uint aFlags)
- {
- _download.OnLocationChange(aWebProgress, aRequest, aLocation, aFlags);
- }
- public void OnProgressChange(Gecko.nsIWebProgress aWebProgress, Gecko.nsIRequest aRequest, int aCurSelfProgress, int aMaxSelfProgress, int aCurTotalProgress, int aMaxTotalProgress)
- {
- OnDownloadProgress(new GeckoDownloadProgressEventArgs(aCurTotalProgress, aMaxTotalProgress, _download.GetPercentCompleteAttribute(), _download.GetSpeedAttribute()));
- _download.OnProgressChange(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress);
- }
- public void OnProgressChange64(Gecko.nsIWebProgress aWebProgress, Gecko.nsIRequest aRequest, long aCurSelfProgress, long aMaxSelfProgress, long aCurTotalProgress, long aMaxTotalProgress)
- {
- OnDownloadProgress(new GeckoDownloadProgressEventArgs(aCurTotalProgress, aMaxTotalProgress, _download.GetPercentCompleteAttribute(), _download.GetSpeedAttribute()));
- _download.OnProgressChange64(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress);
- }
- public bool OnRefreshAttempted(Gecko.nsIWebProgress aWebProgress, Gecko.nsIURI aRefreshURI, int aMillis, bool aSameURI)
- {
- return _download.OnRefreshAttempted(aWebProgress, aRefreshURI, aMillis, aSameURI);
- }
- public void OnSecurityChange(Gecko.nsIWebProgress aWebProgress, Gecko.nsIRequest aRequest, uint aState)
- {
- _download.OnSecurityChange(aWebProgress, aRequest, aState);
- }
- public void OnStateChange(Gecko.nsIWebProgress aWebProgress, Gecko.nsIRequest aRequest, uint aStateFlags, int aStatus)
- {
- if ((aStateFlags & nsIWebProgressListenerConstants.STATE_START) != 0)
- {
- // Start
- OnDownloadStarted();
- }
- if ((aStateFlags & nsIWebProgressListenerConstants.STATE_STOP) != 0)
- {
- // Stop
- if (aStatus != 0)
- {
- // ERROR FailEvent
- OnDownloadFailed();
- }
- else
- {
- // COMPLETE CompleteEvent
- OnDownloadComplete();
- }
- }
- _download.OnStateChange(aWebProgress, aRequest, aStateFlags, aStatus);
- }
- public void OnStatusChange(Gecko.nsIWebProgress aWebProgress, Gecko.nsIRequest aRequest, int aStatus, string aMessage)
- {
- _download.OnStatusChange(aWebProgress, aRequest, aStatus, aMessage);
- }
- }
- }
- public class GeckoDownloadProgressEventArgs
- : EventArgs
- {
- public readonly long CurrentProgress;
- public readonly long MaxProgress;
- public readonly int ProgressPercent;
- public readonly double Speed;
- public readonly TimeSpan LeftTime;
- /// <summary>Creates a new instance of a <see cref="GeckoDownloadEventArgs"/> object.</summary>
- /// <param name="value"></param>
- /// <param name="response"></param>
- internal GeckoDownloadProgressEventArgs(long currentProgress, long maxProgress, int progressPercent, double speed)
- {
- CurrentProgress = currentProgress;
- MaxProgress = maxProgress;
- ProgressPercent = progressPercent;
- Speed = speed;
- if (speed > 100.0)
- LeftTime = new TimeSpan(0, 0, (int)((maxProgress - currentProgress) / speed));
- else
- LeftTime = new TimeSpan(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement