Advertisement
Guest User

Geckofx Download implementation

a guest
Apr 21st, 2013
1,478
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.08 KB | None | 0 0
  1. public class myDownload : nsIDownload
  2.     {
  3.         [Serializable]
  4.         public delegate void SimpleEventHandler(object sender);
  5.  
  6.         private nsIDownload _download;
  7.  
  8.         #region Events
  9.         protected EventHandlerList Events = new EventHandlerList();
  10.  
  11.         private static readonly object StartEvent = new object();
  12.         private static readonly object CompleteEvent = new object();
  13.         private static readonly object FailEvent = new object();
  14.         private static readonly object ProgressEvent = new object();
  15.  
  16.  
  17.         #region public event DownloadStarted
  18.  
  19.         /// <summary>
  20.         /// Occurs before the Download was started.
  21.         /// </summary>
  22.         [Category("DownloadStarted")]
  23.         [Description("Occurs before the Download was started.")]
  24.         public event SimpleEventHandler DownloadStarted
  25.         {
  26.             add { Events.AddHandler(StartEvent, value); }
  27.             remove { Events.RemoveHandler(StartEvent, value); }
  28.         }
  29.  
  30.         /// <summary>Raises the <see cref="DownloadStarted"/> event.</summary>
  31.         /// <param name="e">The data for the event.</param>
  32.         protected virtual void OnDownloadStarted()
  33.         {
  34.             var evnt = ((SimpleEventHandler)Events[StartEvent]);
  35.             if (evnt != null) evnt(this);
  36.         }
  37.         #endregion
  38.  
  39.  
  40.         #region public event DownloadProgress
  41.  
  42.         /// <summary>
  43.         /// Occurs before the Download was started.
  44.         /// </summary>
  45.         [Category("DownloadProgress")]
  46.         [Description("Occurs before the Download is progressing.")]
  47.         public event EventHandler<GeckoDownloadProgressEventArgs> DownloadProgress
  48.         {
  49.             add { Events.AddHandler(ProgressEvent, value); }
  50.             remove { Events.RemoveHandler(ProgressEvent, value); }
  51.         }
  52.  
  53.         /// <summary>Raises the <see cref="DownloadStarted"/> event.</summary>
  54.         /// <param name="e">The data for the event.</param>
  55.         protected virtual void OnDownloadProgress(GeckoDownloadProgressEventArgs e)
  56.         {
  57.             var evnt = ((EventHandler<GeckoDownloadProgressEventArgs>)Events[ProgressEvent]);
  58.             if (evnt != null) evnt(this, e);
  59.         }
  60.         #endregion
  61.  
  62.  
  63.         #region public event DownloadComplete
  64.  
  65.         /// <summary>
  66.         /// Occurs before the Download was started.
  67.         /// </summary>
  68.         [Category("DownloadComplete")]
  69.         [Description("Occurs before the Download was completed.")]
  70.         public event SimpleEventHandler DownloadComplete
  71.         {
  72.             add { Events.AddHandler(CompleteEvent, value); }
  73.             remove { Events.RemoveHandler(CompleteEvent, value); }
  74.         }
  75.  
  76.         /// <summary>Raises the <see cref="DownloadStarted"/> event.</summary>
  77.         /// <param name="e">The data for the event.</param>
  78.         protected virtual void OnDownloadComplete()
  79.         {
  80.             var evnt = ((SimpleEventHandler)Events[CompleteEvent]);
  81.             if (evnt != null) evnt(this);
  82.         }
  83.         #endregion
  84.  
  85.         #region public event DownloadFailed
  86.  
  87.         /// <summary>
  88.         /// Occurs before the Download was started.
  89.         /// </summary>
  90.         [Category("DownloadFailed")]
  91.         [Description("Occurs before the Download was started.")]
  92.         public event SimpleEventHandler DownloadFailed
  93.         {
  94.             add { Events.AddHandler(FailEvent, value); }
  95.             remove { Events.RemoveHandler(FailEvent, value); }
  96.         }
  97.  
  98.         /// <summary>Raises the <see cref="DownloadStarted"/> event.</summary>
  99.         /// <param name="e">The data for the event.</param>
  100.         protected virtual void OnDownloadFailed()
  101.         {
  102.             var evnt = ((SimpleEventHandler)Events[FailEvent]);
  103.             if (evnt != null) evnt(this);
  104.         }
  105.         #endregion
  106.  
  107.  
  108.  
  109.         #endregion
  110.  
  111.         public long ID;
  112.         public readonly Uri Source;
  113.         public readonly string File;
  114.         public readonly long Size;
  115.         public readonly string Mime;
  116.  
  117.  
  118.         public myDownload(nsIDownload download)
  119.         {
  120.             _download = download;
  121.             ID = DateTime.Now.Ticks;
  122.             Source = (new nsURI(_download.GetSourceAttribute())).ToUri();
  123.             File = nsString.Get(_download.GetTargetFileAttribute().GetTargetAttribute);
  124.             Size = _download.GetSizeAttribute();
  125.             var a = _download.GetMIMEInfoAttribute();
  126.             if (a != null)
  127.                 Mime = nsString.Get(a.GetMIMETypeAttribute);
  128.         }
  129.  
  130.         # region STATIC Downloads Methods
  131.         static nsIDownloadManager DownloadManager = null;
  132.  
  133.         static public void CleanUpDownloads()
  134.         {
  135.             if (DownloadManager == null)
  136.                 DownloadManager = Xpcom.CreateInstance<nsIDownloadManager>("@mozilla.org/download-manager;1");
  137.  
  138.             nsISimpleEnumerator enu = DownloadManager.GetActiveDownloadsAttribute();
  139.  
  140.             while (enu.HasMoreElements())
  141.             {
  142.                 nsIDownload d = (nsIDownload)enu.GetNext();
  143.                 nsICancelable c = d.GetCancelableAttribute();
  144.                 DownloadManager.CancelDownload(d.GetIdAttribute());
  145.             };
  146.             DownloadManager.CleanUp();
  147.         }
  148.  
  149.         static public myDownload downloadFile(string localfile, string url)
  150.         {
  151.             if (DownloadManager == null)
  152.                 DownloadManager = Xpcom.CreateInstance<nsIDownloadManager>("@mozilla.org/download-manager;1");
  153.  
  154.             nsIURI source = IOService.CreateNsIUri(url);
  155.             nsIURI dest = IOService.CreateFileNsIUri(localfile);
  156.  
  157.             var objTarget = Xpcom.CreateInstance<nsILocalFile>("@mozilla.org/file/local;1");
  158.             using (nsAString tmp = new nsAString(localfile))
  159.                 objTarget.InitWithPath(tmp);
  160.  
  161.             nsIWebBrowserPersist persist = Xpcom.CreateInstance<nsIWebBrowserPersist>("@mozilla.org/embedding/browser/nsWebBrowserPersist;1");
  162.  
  163.             nsIDownload download = null;
  164.  
  165.             nsAStringBase t = (nsAStringBase)new nsAString(Path.GetFileName(localfile));
  166.  
  167.  
  168.             download = DownloadManager.AddDownload(0, source, dest, t, null, 0, null, (nsICancelable)persist);
  169.  
  170.             if (download != null)
  171.             {
  172.                 myDownload myD = new myDownload(download);
  173.                 persist.SetPersistFlagsAttribute(2 | 32 | 16384);
  174.                 persist.SetProgressListenerAttribute((nsIWebProgressListener)myD);
  175.                 persist.SaveURI(source, null, null, null, null, (nsISupports)dest);
  176.                 return myD;
  177.             }
  178.             return null;
  179.         }
  180.         #endregion
  181.  
  182.         public void CancelDownload()
  183.         {
  184.             if (DownloadManager == null)
  185.                 DownloadManager = Xpcom.CreateInstance<nsIDownloadManager>("@mozilla.org/download-manager;1");
  186.  
  187.             DownloadManager.CancelDownload(GetIdAttribute());
  188.             DownloadManager.RemoveDownload(GetIdAttribute());
  189.         }
  190.  
  191.         public void PauseDownload()
  192.         {
  193.             DownloadManager.PauseDownload(GetIdAttribute());
  194.         }
  195.  
  196.         public void ResumeDownload()
  197.         {
  198.             DownloadManager.ResumeDownload(GetIdAttribute());
  199.         }
  200.  
  201.         public long GetAmountTransferredAttribute()
  202.         {
  203.             return _download.GetAmountTransferredAttribute();
  204.         }
  205.  
  206.         public Gecko.nsICancelable GetCancelableAttribute()
  207.         {
  208.             return _download.GetCancelableAttribute();
  209.         }
  210.  
  211.  
  212.         public void GetDisplayNameAttribute(Gecko.nsAStringBase aDisplayName)
  213.         {
  214.             _download.GetDisplayNameAttribute(aDisplayName);
  215.         }
  216.  
  217.         public uint GetIdAttribute()
  218.         {
  219.             return _download.GetIdAttribute();
  220.         }
  221.  
  222.         public Gecko.nsIMIMEInfo GetMIMEInfoAttribute()
  223.         {
  224.             return _download.GetMIMEInfoAttribute();
  225.         }
  226.  
  227.         public int GetPercentCompleteAttribute()
  228.         {
  229.             return _download.GetPercentCompleteAttribute();
  230.         }
  231.  
  232.         public Gecko.nsIURI GetReferrerAttribute()
  233.         {
  234.             return _download.GetReferrerAttribute();
  235.         }
  236.  
  237.         public bool GetResumableAttribute()
  238.         {
  239.             return _download.GetResumableAttribute();
  240.         }
  241.  
  242.         public long GetSizeAttribute()
  243.         {
  244.             return _download.GetSizeAttribute();
  245.         }
  246.  
  247.         public nsIURI GetSourceAttribute()
  248.         {
  249.             return _download.GetSourceAttribute();
  250.         }
  251.  
  252.         public double GetSpeedAttribute()
  253.         {
  254.             return _download.GetSpeedAttribute();
  255.         }
  256.  
  257.         public long GetStartTimeAttribute()
  258.         {
  259.             return _download.GetStartTimeAttribute();
  260.         }
  261.  
  262.         public short GetStateAttribute()
  263.         {
  264.             return _download.GetStateAttribute();
  265.         }
  266.  
  267.         public nsIURI GetTargetAttribute()
  268.         {
  269.  
  270.             return _download.GetTargetAttribute();
  271.         }
  272.  
  273.         public nsIFile GetTargetFileAttribute()
  274.         {
  275.             return _download.GetTargetFileAttribute();
  276.         }
  277.  
  278.  
  279.         public void Init(Gecko.nsIURI aSource, Gecko.nsIURI aTarget, Gecko.nsAStringBase aDisplayName, Gecko.nsIMIMEInfo aMIMEInfo, long startTime, Gecko.nsIFile aTempFile, Gecko.nsICancelable aCancelable)
  280.         {
  281.             _download.Init(aSource, aTarget, aDisplayName, aMIMEInfo, startTime, aTempFile, aCancelable);
  282.         }
  283.  
  284.  
  285.         public void OnLocationChange(Gecko.nsIWebProgress aWebProgress, Gecko.nsIRequest aRequest, Gecko.nsIURI aLocation, uint aFlags)
  286.         {
  287.             _download.OnLocationChange(aWebProgress, aRequest, aLocation, aFlags);
  288.         }
  289.  
  290.  
  291.         public void OnProgressChange(Gecko.nsIWebProgress aWebProgress, Gecko.nsIRequest aRequest, int aCurSelfProgress, int aMaxSelfProgress, int aCurTotalProgress, int aMaxTotalProgress)
  292.         {
  293.             OnDownloadProgress(new GeckoDownloadProgressEventArgs(aCurTotalProgress, aMaxTotalProgress, _download.GetPercentCompleteAttribute(), _download.GetSpeedAttribute()));
  294.  
  295.             _download.OnProgressChange(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress);
  296.         }
  297.  
  298.  
  299.         public void OnProgressChange64(Gecko.nsIWebProgress aWebProgress, Gecko.nsIRequest aRequest, long aCurSelfProgress, long aMaxSelfProgress, long aCurTotalProgress, long aMaxTotalProgress)
  300.         {
  301.             OnDownloadProgress(new GeckoDownloadProgressEventArgs(aCurTotalProgress, aMaxTotalProgress, _download.GetPercentCompleteAttribute(), _download.GetSpeedAttribute()));
  302.  
  303.             _download.OnProgressChange64(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress);
  304.  
  305.         }
  306.  
  307.         public bool OnRefreshAttempted(Gecko.nsIWebProgress aWebProgress, Gecko.nsIURI aRefreshURI, int aMillis, bool aSameURI)
  308.         {
  309.             return _download.OnRefreshAttempted(aWebProgress, aRefreshURI, aMillis, aSameURI);
  310.         }
  311.  
  312.  
  313.         public void OnSecurityChange(Gecko.nsIWebProgress aWebProgress, Gecko.nsIRequest aRequest, uint aState)
  314.         {
  315.             _download.OnSecurityChange(aWebProgress, aRequest, aState);
  316.         }
  317.  
  318.  
  319.         public void OnStateChange(Gecko.nsIWebProgress aWebProgress, Gecko.nsIRequest aRequest, uint aStateFlags, int aStatus)
  320.         {
  321.             if ((aStateFlags & nsIWebProgressListenerConstants.STATE_START) != 0)
  322.             {
  323.                 // Start
  324.  
  325.                 OnDownloadStarted();
  326.             }
  327.             if ((aStateFlags & nsIWebProgressListenerConstants.STATE_STOP) != 0)
  328.             {
  329.                 // Stop
  330.                 if (aStatus != 0)
  331.                 {
  332.                     // ERROR FailEvent
  333.                     OnDownloadFailed();
  334.                 }
  335.                 else
  336.                 {
  337.                     // COMPLETE CompleteEvent
  338.                     OnDownloadComplete();
  339.                 }
  340.             }
  341.  
  342.             _download.OnStateChange(aWebProgress, aRequest, aStateFlags, aStatus);
  343.         }
  344.  
  345.  
  346.         public void OnStatusChange(Gecko.nsIWebProgress aWebProgress, Gecko.nsIRequest aRequest, int aStatus, string aMessage)
  347.         {
  348.             _download.OnStatusChange(aWebProgress, aRequest, aStatus, aMessage);
  349.         }
  350.     }
  351. }
  352.  
  353. public class GeckoDownloadProgressEventArgs
  354.     : EventArgs
  355. {
  356.     public readonly long CurrentProgress;
  357.  
  358.     public readonly long MaxProgress;
  359.     public readonly int ProgressPercent;
  360.     public readonly double Speed;
  361.     public readonly TimeSpan LeftTime;
  362.  
  363.     /// <summary>Creates a new instance of a <see cref="GeckoDownloadEventArgs"/> object.</summary>
  364.     /// <param name="value"></param>
  365.     /// <param name="response"></param>
  366.     internal GeckoDownloadProgressEventArgs(long currentProgress, long maxProgress, int progressPercent, double speed)
  367.     {
  368.         CurrentProgress = currentProgress;
  369.         MaxProgress = maxProgress;
  370.         ProgressPercent = progressPercent;
  371.         Speed = speed;
  372.         if (speed > 100.0)
  373.             LeftTime = new TimeSpan(0, 0, (int)((maxProgress - currentProgress) / speed));
  374.         else
  375.             LeftTime = new TimeSpan(0);
  376.     }
  377. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement