Guest User

Downloader in Visual Basic

a guest
Feb 21st, 2018
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. VB.NET
  2. Public Class myDownload
  3.     Inherits nsIDownload
  4.    
  5.     <Serializable()>  _
  6.     Public Delegate Sub SimpleEventHandler(ByVal sender As Object)
  7.    
  8.     Private _download As nsIDownload
  9.     #Region "Events"
  10.    
  11.     Protected Events As EventHandlerList = New EventHandlerList
  12.    
  13.     Private Shared StartEvent As Object = New Object
  14.    
  15.     Private Shared CompleteEvent As Object = New Object
  16.    
  17.     Private Shared FailEvent As Object = New Object
  18.    
  19.     Private Shared ProgressEvent As Object = New Object
  20.     #Region "public event DownloadStarted"
  21.    
  22.     <Category("DownloadStarted"),  _
  23.      Description("Occurs before the Download was started.")>  _
  24.     Public Event DownloadStarted As SimpleEventHandler
  25.    
  26.     ''' <summary>Raises the <see cref="DownloadStarted"/> event.</summary>
  27.    ''' <param name="e">The data for the event.</param>
  28.    Protected Overridable Sub OnDownloadStarted()
  29.         Dim evnt = CType(Me.Events(StartEvent),SimpleEventHandler)
  30.         If (Not (evnt) Is Nothing) Then
  31.             evnt(Me)
  32.         End If
  33.        
  34.     End Sub
  35.     #End Region
  36.     #Region "public event DownloadProgress"
  37.    
  38.     <Category("DownloadProgress"),  _
  39.      Description("Occurs before the Download is progressing.")>  _
  40.     Public Event DownloadProgress As EventHandler(Of GeckoDownloadProgressEventArgs)
  41.    
  42.     ''' <summary>Raises the <see cref="DownloadStarted"/> event.</summary>
  43.    ''' <param name="e">The data for the event.</param>
  44.    Protected Overridable Sub OnDownloadProgress(ByVal e As GeckoDownloadProgressEventArgs)
  45.         Dim evnt = CType(Me.Events(ProgressEvent),EventHandler(Of GeckoDownloadProgressEventArgs))
  46.         If (Not (evnt) Is Nothing) Then
  47.             evnt(Me, e)
  48.         End If
  49.        
  50.     End Sub
  51.     #End Region
  52.     #Region "public event DownloadComplete"
  53.    
  54.     <Category("DownloadComplete"),  _
  55.      Description("Occurs before the Download was completed.")>  _
  56.     Public Event DownloadComplete As SimpleEventHandler
  57.    
  58.     ''' <summary>Raises the <see cref="DownloadStarted"/> event.</summary>
  59.    ''' <param name="e">The data for the event.</param>
  60.    Protected Overridable Sub OnDownloadComplete()
  61.         Dim evnt = CType(Me.Events(CompleteEvent),SimpleEventHandler)
  62.         If (Not (evnt) Is Nothing) Then
  63.             evnt(Me)
  64.         End If
  65.        
  66.     End Sub
  67.     #End Region
  68.     #Region "public event DownloadFailed"
  69.    
  70.     <Category("DownloadFailed"),  _
  71.      Description("Occurs before the Download was started.")>  _
  72.     Public Event DownloadFailed As SimpleEventHandler
  73.    
  74.     ''' <summary>Raises the <see cref="DownloadStarted"/> event.</summary>
  75.    ''' <param name="e">The data for the event.</param>
  76.    Protected Overridable Sub OnDownloadFailed()
  77.         Dim evnt = CType(Me.Events(FailEvent),SimpleEventHandler)
  78.         If (Not (evnt) Is Nothing) Then
  79.             evnt(Me)
  80.         End If
  81.        
  82.     End Sub
  83.     #End Region
  84.     #End Region
  85.    
  86.     Public ID As Long
  87.    
  88.     Public Source As Uri
  89.    
  90.     Public File As String
  91.    
  92.     Public Size As Long
  93.    
  94.     Public Mime As String
  95.    
  96.     Public Sub New(ByVal download As nsIDownload)
  97.         MyBase.New
  98.         Me._download = download
  99.         Me.ID = DateTime.Now.Ticks
  100.         Me.Source = New nsURI(Me._download.GetSourceAttribute).ToUri
  101.         Me.File = nsString.Get(Me._download.GetTargetFileAttribute.GetTargetAttribute)
  102.         Me.Size = Me._download.GetSizeAttribute
  103.         Dim a = Me._download.GetMIMEInfoAttribute
  104.         If (Not (a) Is Nothing) Then
  105.             Me.Mime = nsString.Get(a.GetMIMETypeAttribute)
  106.         End If
  107.        
  108.     End Sub
  109.     #Region "STATIC Downloads Methods"
  110.    
  111.     Private Shared DownloadManager As nsIDownloadManager = Nothing
  112.    
  113.     Public Shared Sub CleanUpDownloads()
  114.         If (DownloadManager Is Nothing) Then
  115.             DownloadManager = Xpcom.CreateInstance(Of nsIDownloadManager)("@mozilla.org/download-manager;1")
  116.         End If
  117.        
  118.         Dim enu As nsISimpleEnumerator = DownloadManager.GetActiveDownloadsAttribute
  119.        
  120.         While enu.HasMoreElements
  121.             Dim d As nsIDownload = CType(enu.GetNext,nsIDownload)
  122.             Dim c As nsICancelable = d.GetCancelableAttribute
  123.             DownloadManager.CancelDownload(d.GetIdAttribute)
  124.            
  125.         End While
  126.        
  127.        
  128.         DownloadManager.CleanUp
  129.     End Sub
  130.    
  131.     Public Shared Function downloadFile(ByVal localfile As String, ByVal url As String) As myDownload
  132.         If (DownloadManager Is Nothing) Then
  133.             DownloadManager = Xpcom.CreateInstance(Of nsIDownloadManager)("@mozilla.org/download-manager;1")
  134.         End If
  135.        
  136.         Dim source As nsIURI = IOService.CreateNsIUri(url)
  137.         Dim dest As nsIURI = IOService.CreateFileNsIUri(localfile)
  138.         Dim objTarget = Xpcom.CreateInstance(Of nsILocalFile)("@mozilla.org/file/local;1")
  139.         Dim tmp As nsAString = New nsAString(localfile)
  140.         objTarget.InitWithPath(tmp)
  141.         Dim persist As nsIWebBrowserPersist = Xpcom.CreateInstance(Of nsIWebBrowserPersist)("@mozilla.org/embedding/browser/nsWebBrowserPersist;1")
  142.         Dim download As nsIDownload = Nothing
  143.         Dim t As nsAStringBase = CType(New nsAString(Path.GetFileName(localfile)),nsAStringBase)
  144.         download = DownloadManager.AddDownload(0, source, dest, t, Nothing, 0, Nothing, CType(persist,nsICancelable))
  145.         If (Not (download) Is Nothing) Then
  146.             Dim myD As myDownload = New myDownload(download)
  147.             persist.SetPersistFlagsAttribute((2 Or (32 Or 16384)))
  148.             persist.SetProgressListenerAttribute(CType(myD,nsIWebProgressListener))
  149.             persist.SaveURI(source, Nothing, Nothing, Nothing, Nothing, CType(dest,nsISupports))
  150.             Return myD
  151.         End If
  152.        
  153.         Return Nothing
  154.     End Function
  155.     #End Region
  156.    
  157.     Public Sub CancelDownload()
  158.         If (DownloadManager Is Nothing) Then
  159.             DownloadManager = Xpcom.CreateInstance(Of nsIDownloadManager)("@mozilla.org/download-manager;1")
  160.         End If
  161.        
  162.         DownloadManager.CancelDownload(Me.GetIdAttribute)
  163.         DownloadManager.RemoveDownload(Me.GetIdAttribute)
  164.     End Sub
  165.    
  166.     Public Sub PauseDownload()
  167.         DownloadManager.PauseDownload(Me.GetIdAttribute)
  168.     End Sub
  169.    
  170.     Public Sub ResumeDownload()
  171.         DownloadManager.ResumeDownload(Me.GetIdAttribute)
  172.     End Sub
  173.    
  174.     Public Function GetAmountTransferredAttribute() As Long
  175.         Return Me._download.GetAmountTransferredAttribute
  176.     End Function
  177.    
  178.     Public Function GetCancelableAttribute() As Gecko.nsICancelable
  179.         Return Me._download.GetCancelableAttribute
  180.     End Function
  181.    
  182.     Public Sub GetDisplayNameAttribute(ByVal aDisplayName As Gecko.nsAStringBase)
  183.         Me._download.GetDisplayNameAttribute(aDisplayName)
  184.     End Sub
  185.    
  186.     Public Function GetIdAttribute() As UInteger
  187.         Return Me._download.GetIdAttribute
  188.     End Function
  189.    
  190.     Public Function GetMIMEInfoAttribute() As Gecko.nsIMIMEInfo
  191.         Return Me._download.GetMIMEInfoAttribute
  192.     End Function
  193.    
  194.     Public Function GetPercentCompleteAttribute() As Integer
  195.         Return Me._download.GetPercentCompleteAttribute
  196.     End Function
  197.    
  198.     Public Function GetReferrerAttribute() As Gecko.nsIURI
  199.         Return Me._download.GetReferrerAttribute
  200.     End Function
  201.    
  202.     Public Function GetResumableAttribute() As Boolean
  203.         Return Me._download.GetResumableAttribute
  204.     End Function
  205.    
  206.     Public Function GetSizeAttribute() As Long
  207.         Return Me._download.GetSizeAttribute
  208.     End Function
  209.    
  210.     Public Function GetSourceAttribute() As nsIURI
  211.         Return Me._download.GetSourceAttribute
  212.     End Function
  213.    
  214.     Public Function GetSpeedAttribute() As Double
  215.         Return Me._download.GetSpeedAttribute
  216.     End Function
  217.    
  218.     Public Function GetStartTimeAttribute() As Long
  219.         Return Me._download.GetStartTimeAttribute
  220.     End Function
  221.    
  222.     Public Function GetStateAttribute() As Short
  223.         Return Me._download.GetStateAttribute
  224.     End Function
  225.    
  226.     Public Function GetTargetAttribute() As nsIURI
  227.         Return Me._download.GetTargetAttribute
  228.     End Function
  229.    
  230.     Public Function GetTargetFileAttribute() As nsIFile
  231.         Return Me._download.GetTargetFileAttribute
  232.     End Function
  233.    
  234.     Public Sub Init(ByVal aSource As Gecko.nsIURI, ByVal aTarget As Gecko.nsIURI, ByVal aDisplayName As Gecko.nsAStringBase, ByVal aMIMEInfo As Gecko.nsIMIMEInfo, ByVal startTime As Long, ByVal aTempFile As Gecko.nsIFile, ByVal aCancelable As Gecko.nsICancelable)
  235.         Me._download.Init(aSource, aTarget, aDisplayName, aMIMEInfo, startTime, aTempFile, aCancelable)
  236.     End Sub
  237.    
  238.     Public Sub OnLocationChange(ByVal aWebProgress As Gecko.nsIWebProgress, ByVal aRequest As Gecko.nsIRequest, ByVal aLocation As Gecko.nsIURI, ByVal aFlags As UInteger)
  239.         Me._download.OnLocationChange(aWebProgress, aRequest, aLocation, aFlags)
  240.     End Sub
  241.    
  242.     Public Sub OnProgressChange(ByVal aWebProgress As Gecko.nsIWebProgress, ByVal aRequest As Gecko.nsIRequest, ByVal aCurSelfProgress As Integer, ByVal aMaxSelfProgress As Integer, ByVal aCurTotalProgress As Integer, ByVal aMaxTotalProgress As Integer)
  243.         Me.OnDownloadProgress(New GeckoDownloadProgressEventArgs(aCurTotalProgress, aMaxTotalProgress, Me._download.GetPercentCompleteAttribute, Me._download.GetSpeedAttribute))
  244.         Me._download.OnProgressChange(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress)
  245.     End Sub
  246.    
  247.     Public Sub OnProgressChange64(ByVal aWebProgress As Gecko.nsIWebProgress, ByVal aRequest As Gecko.nsIRequest, ByVal aCurSelfProgress As Long, ByVal aMaxSelfProgress As Long, ByVal aCurTotalProgress As Long, ByVal aMaxTotalProgress As Long)
  248.         Me.OnDownloadProgress(New GeckoDownloadProgressEventArgs(aCurTotalProgress, aMaxTotalProgress, Me._download.GetPercentCompleteAttribute, Me._download.GetSpeedAttribute))
  249.         Me._download.OnProgressChange64(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress)
  250.     End Sub
  251.    
  252.     Public Function OnRefreshAttempted(ByVal aWebProgress As Gecko.nsIWebProgress, ByVal aRefreshURI As Gecko.nsIURI, ByVal aMillis As Integer, ByVal aSameURI As Boolean) As Boolean
  253.         Return Me._download.OnRefreshAttempted(aWebProgress, aRefreshURI, aMillis, aSameURI)
  254.     End Function
  255.    
  256.     Public Sub OnSecurityChange(ByVal aWebProgress As Gecko.nsIWebProgress, ByVal aRequest As Gecko.nsIRequest, ByVal aState As UInteger)
  257.         Me._download.OnSecurityChange(aWebProgress, aRequest, aState)
  258.     End Sub
  259.    
  260.     Public Sub OnStateChange(ByVal aWebProgress As Gecko.nsIWebProgress, ByVal aRequest As Gecko.nsIRequest, ByVal aStateFlags As UInteger, ByVal aStatus As Integer)
  261.         If ((aStateFlags And nsIWebProgressListenerConstants.STATE_START)  _
  262.                     <> 0) Then
  263.             ' Start
  264.            Me.OnDownloadStarted
  265.         End If
  266.        
  267.         If ((aStateFlags And nsIWebProgressListenerConstants.STATE_STOP)  _
  268.                     <> 0) Then
  269.             ' Stop
  270.            If (aStatus <> 0) Then
  271.                 ' ERROR FailEvent
  272.                Me.OnDownloadFailed
  273.             Else
  274.                 ' COMPLETE CompleteEvent
  275.                Me.OnDownloadComplete
  276.             End If
  277.            
  278.         End If
  279.        
  280.         Me._download.OnStateChange(aWebProgress, aRequest, aStateFlags, aStatus)
  281.     End Sub
  282.    
  283.     Public Sub OnStatusChange(ByVal aWebProgress As Gecko.nsIWebProgress, ByVal aRequest As Gecko.nsIRequest, ByVal aStatus As Integer, ByVal aMessage As String)
  284.         Me._download.OnStatusChange(aWebProgress, aRequest, aStatus, aMessage)
  285.     End Sub
  286. End Class
  287. UnknownPublic Class GeckoDownloadProgressEventArgs
  288.     Inherits EventArgs
  289.    
  290.     Public CurrentProgress As Long
  291.    
  292.     Public MaxProgress As Long
  293.    
  294.     Public ProgressPercent As Integer
  295.    
  296.     Public Speed As Double
  297.    
  298.     Public LeftTime As TimeSpan
  299.    
  300.     ''' <summary>Creates a new instance of a <see cref="GeckoDownloadEventArgs"/> object.</summary>
  301.    ''' <param name="value"></param>
  302.    ''' <param name="response"></param>
  303.    Friend Sub New(ByVal currentProgress As Long, ByVal maxProgress As Long, ByVal progressPercent As Integer, ByVal speed As Double)
  304.         MyBase.New
  305.         Me.CurrentProgress = currentProgress
  306.         Me.MaxProgress = maxProgress
  307.         Me.ProgressPercent = progressPercent
  308.         Me.Speed = speed
  309.         If (speed > 100) Then
  310.             Me.LeftTime = New TimeSpan(0, 0, CType(((maxProgress - currentProgress)  _
  311.                             / speed),Integer))
  312.         Else
  313.             Me.LeftTime = New TimeSpan(0)
  314.         End If
  315.        
  316.     End Sub
  317. End Class
  318.  
  319. c#
  320. public class myDownload : nsIDownload
  321.     {
  322.         [Serializable]
  323.         public delegate void SimpleEventHandler(object sender);
  324.  
  325.         private nsIDownload _download;
  326.  
  327.         #region Events
  328.         protected EventHandlerList Events = new EventHandlerList();
  329.  
  330.         private static readonly object StartEvent = new object();
  331.         private static readonly object CompleteEvent = new object();
  332.         private static readonly object FailEvent = new object();
  333.         private static readonly object ProgressEvent = new object();
  334.  
  335.  
  336.         #region public event DownloadStarted
  337.  
  338.         /// <summary>
  339.         /// Occurs before the Download was started.
  340.         /// </summary>
  341.         [Category("DownloadStarted")]
  342.         [Description("Occurs before the Download was started.")]
  343.         public event SimpleEventHandler DownloadStarted
  344.         {
  345.             add { Events.AddHandler(StartEvent, value); }
  346.             remove { Events.RemoveHandler(StartEvent, value); }
  347.         }
  348.  
  349.         /// <summary>Raises the <see cref="DownloadStarted"/> event.</summary>
  350.         /// <param name="e">The data for the event.</param>
  351.         protected virtual void OnDownloadStarted()
  352.         {
  353.             var evnt = ((SimpleEventHandler)Events[StartEvent]);
  354.             if (evnt != null) evnt(this);
  355.         }
  356.         #endregion
  357.  
  358.  
  359.         #region public event DownloadProgress
  360.  
  361.         /// <summary>
  362.         /// Occurs before the Download was started.
  363.         /// </summary>
  364.         [Category("DownloadProgress")]
  365.         [Description("Occurs before the Download is progressing.")]
  366.         public event EventHandler<GeckoDownloadProgressEventArgs> DownloadProgress
  367.         {
  368.             add { Events.AddHandler(ProgressEvent, value); }
  369.             remove { Events.RemoveHandler(ProgressEvent, value); }
  370.         }
  371.  
  372.         /// <summary>Raises the <see cref="DownloadStarted"/> event.</summary>
  373.         /// <param name="e">The data for the event.</param>
  374.         protected virtual void OnDownloadProgress(GeckoDownloadProgressEventArgs e)
  375.         {
  376.             var evnt = ((EventHandler<GeckoDownloadProgressEventArgs>)Events[ProgressEvent]);
  377.             if (evnt != null) evnt(this, e);
  378.         }
  379.         #endregion
  380.  
  381.  
  382.         #region public event DownloadComplete
  383.  
  384.         /// <summary>
  385.         /// Occurs before the Download was started.
  386.         /// </summary>
  387.         [Category("DownloadComplete")]
  388.         [Description("Occurs before the Download was completed.")]
  389.         public event SimpleEventHandler DownloadComplete
  390.         {
  391.             add { Events.AddHandler(CompleteEvent, value); }
  392.             remove { Events.RemoveHandler(CompleteEvent, value); }
  393.         }
  394.  
  395.         /// <summary>Raises the <see cref="DownloadStarted"/> event.</summary>
  396.         /// <param name="e">The data for the event.</param>
  397.         protected virtual void OnDownloadComplete()
  398.         {
  399.             var evnt = ((SimpleEventHandler)Events[CompleteEvent]);
  400.             if (evnt != null) evnt(this);
  401.         }
  402.         #endregion
  403.  
  404.         #region public event DownloadFailed
  405.  
  406.         /// <summary>
  407.         /// Occurs before the Download was started.
  408.         /// </summary>
  409.         [Category("DownloadFailed")]
  410.         [Description("Occurs before the Download was started.")]
  411.         public event SimpleEventHandler DownloadFailed
  412.         {
  413.             add { Events.AddHandler(FailEvent, value); }
  414.             remove { Events.RemoveHandler(FailEvent, value); }
  415.         }
  416.  
  417.         /// <summary>Raises the <see cref="DownloadStarted"/> event.</summary>
  418.         /// <param name="e">The data for the event.</param>
  419.         protected virtual void OnDownloadFailed()
  420.         {
  421.             var evnt = ((SimpleEventHandler)Events[FailEvent]);
  422.             if (evnt != null) evnt(this);
  423.         }
  424.         #endregion
  425.  
  426.  
  427.  
  428.         #endregion
  429.  
  430.         public long ID;
  431.         public readonly Uri Source;
  432.         public readonly string File;
  433.         public readonly long Size;
  434.         public readonly string Mime;
  435.  
  436.  
  437.         public myDownload(nsIDownload download)
  438.         {
  439.             _download = download;
  440.             ID = DateTime.Now.Ticks;
  441.             Source = (new nsURI(_download.GetSourceAttribute())).ToUri();
  442.             File = nsString.Get(_download.GetTargetFileAttribute().GetTargetAttribute);
  443.             Size = _download.GetSizeAttribute();
  444.             var a = _download.GetMIMEInfoAttribute();
  445.             if (a != null)
  446.                 Mime = nsString.Get(a.GetMIMETypeAttribute);
  447.         }
  448.  
  449.         # region STATIC Downloads Methods
  450.         static nsIDownloadManager DownloadManager = null;
  451.  
  452.         static public void CleanUpDownloads()
  453.         {
  454.             if (DownloadManager == null)
  455.                 DownloadManager = Xpcom.CreateInstance<nsIDownloadManager>("@mozilla.org/download-manager;1");
  456.  
  457.             nsISimpleEnumerator enu = DownloadManager.GetActiveDownloadsAttribute();
  458.  
  459.             while (enu.HasMoreElements())
  460.             {
  461.                 nsIDownload d = (nsIDownload)enu.GetNext();
  462.                 nsICancelable c = d.GetCancelableAttribute();
  463.                 DownloadManager.CancelDownload(d.GetIdAttribute());
  464.             };
  465.             DownloadManager.CleanUp();
  466.         }
  467.  
  468.         static public myDownload downloadFile(string localfile, string url)
  469.         {
  470.             if (DownloadManager == null)
  471.                 DownloadManager = Xpcom.CreateInstance<nsIDownloadManager>("@mozilla.org/download-manager;1");
  472.  
  473.             nsIURI source = IOService.CreateNsIUri(url);
  474.             nsIURI dest = IOService.CreateFileNsIUri(localfile);
  475.  
  476.             var objTarget = Xpcom.CreateInstance<nsILocalFile>("@mozilla.org/file/local;1");
  477.             using (nsAString tmp = new nsAString(localfile))
  478.                 objTarget.InitWithPath(tmp);
  479.  
  480.             nsIWebBrowserPersist persist = Xpcom.CreateInstance<nsIWebBrowserPersist>("@mozilla.org/embedding/browser/nsWebBrowserPersist;1");
  481.  
  482.             nsIDownload download = null;
  483.  
  484.             nsAStringBase t = (nsAStringBase)new nsAString(Path.GetFileName(localfile));
  485.  
  486.  
  487.             download = DownloadManager.AddDownload(0, source, dest, t, null, 0, null, (nsICancelable)persist);
  488.  
  489.             if (download != null)
  490.             {
  491.                 myDownload myD = new myDownload(download);
  492.                 persist.SetPersistFlagsAttribute(2 | 32 | 16384);
  493.                 persist.SetProgressListenerAttribute((nsIWebProgressListener)myD);
  494.                 persist.SaveURI(source, null, null, null, null, (nsISupports)dest);
  495.                 return myD;
  496.             }
  497.             return null;
  498.         }
  499.         #endregion
  500.  
  501.         public void CancelDownload()
  502.         {
  503.             if (DownloadManager == null)
  504.                 DownloadManager = Xpcom.CreateInstance<nsIDownloadManager>("@mozilla.org/download-manager;1");
  505.  
  506.             DownloadManager.CancelDownload(GetIdAttribute());
  507.             DownloadManager.RemoveDownload(GetIdAttribute());
  508.         }
  509.  
  510.         public void PauseDownload()
  511.         {
  512.             DownloadManager.PauseDownload(GetIdAttribute());
  513.         }
  514.  
  515.         public void ResumeDownload()
  516.         {
  517.             DownloadManager.ResumeDownload(GetIdAttribute());
  518.         }
  519.  
  520.         public long GetAmountTransferredAttribute()
  521.         {
  522.             return _download.GetAmountTransferredAttribute();
  523.         }
  524.  
  525.         public Gecko.nsICancelable GetCancelableAttribute()
  526.         {
  527.             return _download.GetCancelableAttribute();
  528.         }
  529.  
  530.  
  531.         public void GetDisplayNameAttribute(Gecko.nsAStringBase aDisplayName)
  532.         {
  533.             _download.GetDisplayNameAttribute(aDisplayName);
  534.         }
  535.  
  536.         public uint GetIdAttribute()
  537.         {
  538.             return _download.GetIdAttribute();
  539.         }
  540.  
  541.         public Gecko.nsIMIMEInfo GetMIMEInfoAttribute()
  542.         {
  543.             return _download.GetMIMEInfoAttribute();
  544.         }
  545.  
  546.         public int GetPercentCompleteAttribute()
  547.         {
  548.             return _download.GetPercentCompleteAttribute();
  549.         }
  550.  
  551.         public Gecko.nsIURI GetReferrerAttribute()
  552.         {
  553.             return _download.GetReferrerAttribute();
  554.         }
  555.  
  556.         public bool GetResumableAttribute()
  557.         {
  558.             return _download.GetResumableAttribute();
  559.         }
  560.  
  561.         public long GetSizeAttribute()
  562.         {
  563.             return _download.GetSizeAttribute();
  564.         }
  565.  
  566.         public nsIURI GetSourceAttribute()
  567.         {
  568.             return _download.GetSourceAttribute();
  569.         }
  570.  
  571.         public double GetSpeedAttribute()
  572.         {
  573.             return _download.GetSpeedAttribute();
  574.         }
  575.  
  576.         public long GetStartTimeAttribute()
  577.         {
  578.             return _download.GetStartTimeAttribute();
  579.         }
  580.  
  581.         public short GetStateAttribute()
  582.         {
  583.             return _download.GetStateAttribute();
  584.         }
  585.  
  586.         public nsIURI GetTargetAttribute()
  587.         {
  588.  
  589.             return _download.GetTargetAttribute();
  590.         }
  591.  
  592.         public nsIFile GetTargetFileAttribute()
  593.         {
  594.             return _download.GetTargetFileAttribute();
  595.         }
  596.  
  597.  
  598.         public void Init(Gecko.nsIURI aSource, Gecko.nsIURI aTarget, Gecko.nsAStringBase aDisplayName, Gecko.nsIMIMEInfo aMIMEInfo, long startTime, Gecko.nsIFile aTempFile, Gecko.nsICancelable aCancelable)
  599.         {
  600.             _download.Init(aSource, aTarget, aDisplayName, aMIMEInfo, startTime, aTempFile, aCancelable);
  601.         }
  602.  
  603.  
  604.         public void OnLocationChange(Gecko.nsIWebProgress aWebProgress, Gecko.nsIRequest aRequest, Gecko.nsIURI aLocation, uint aFlags)
  605.         {
  606.             _download.OnLocationChange(aWebProgress, aRequest, aLocation, aFlags);
  607.         }
  608.  
  609.  
  610.         public void OnProgressChange(Gecko.nsIWebProgress aWebProgress, Gecko.nsIRequest aRequest, int aCurSelfProgress, int aMaxSelfProgress, int aCurTotalProgress, int aMaxTotalProgress)
  611.         {
  612.             OnDownloadProgress(new GeckoDownloadProgressEventArgs(aCurTotalProgress, aMaxTotalProgress, _download.GetPercentCompleteAttribute(), _download.GetSpeedAttribute()));
  613.  
  614.             _download.OnProgressChange(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress);
  615.         }
  616.  
  617.  
  618.         public void OnProgressChange64(Gecko.nsIWebProgress aWebProgress, Gecko.nsIRequest aRequest, long aCurSelfProgress, long aMaxSelfProgress, long aCurTotalProgress, long aMaxTotalProgress)
  619.         {
  620.             OnDownloadProgress(new GeckoDownloadProgressEventArgs(aCurTotalProgress, aMaxTotalProgress, _download.GetPercentCompleteAttribute(), _download.GetSpeedAttribute()));
  621.  
  622.             _download.OnProgressChange64(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress);
  623.  
  624.         }
  625.  
  626.         public bool OnRefreshAttempted(Gecko.nsIWebProgress aWebProgress, Gecko.nsIURI aRefreshURI, int aMillis, bool aSameURI)
  627.         {
  628.             return _download.OnRefreshAttempted(aWebProgress, aRefreshURI, aMillis, aSameURI);
  629.         }
  630.  
  631.  
  632.         public void OnSecurityChange(Gecko.nsIWebProgress aWebProgress, Gecko.nsIRequest aRequest, uint aState)
  633.         {
  634.             _download.OnSecurityChange(aWebProgress, aRequest, aState);
  635.         }
  636.  
  637.  
  638.         public void OnStateChange(Gecko.nsIWebProgress aWebProgress, Gecko.nsIRequest aRequest, uint aStateFlags, int aStatus)
  639.         {
  640.             if ((aStateFlags & nsIWebProgressListenerConstants.STATE_START) != 0)
  641.             {
  642.                 // Start
  643.  
  644.                 OnDownloadStarted();
  645.             }
  646.             if ((aStateFlags & nsIWebProgressListenerConstants.STATE_STOP) != 0)
  647.             {
  648.                 // Stop
  649.                 if (aStatus != 0)
  650.                 {
  651.                     // ERROR FailEvent
  652.                     OnDownloadFailed();
  653.                 }
  654.                 else
  655.                 {
  656.                     // COMPLETE CompleteEvent
  657.                     OnDownloadComplete();
  658.                 }
  659.             }
  660.  
  661.             _download.OnStateChange(aWebProgress, aRequest, aStateFlags, aStatus);
  662.         }
  663.  
  664.  
  665.         public void OnStatusChange(Gecko.nsIWebProgress aWebProgress, Gecko.nsIRequest aRequest, int aStatus, string aMessage)
  666.         {
  667.             _download.OnStatusChange(aWebProgress, aRequest, aStatus, aMessage);
  668.         }
  669.     }
  670. }
  671.  
  672. public class GeckoDownloadProgressEventArgs
  673.     : EventArgs
  674. {
  675.     public readonly long CurrentProgress;
  676.  
  677.     public readonly long MaxProgress;
  678.     public readonly int ProgressPercent;
  679.     public readonly double Speed;
  680.     public readonly TimeSpan LeftTime;
  681.  
  682.     /// <summary>Creates a new instance of a <see cref="GeckoDownloadEventArgs"/> object.</summary>
  683.     /// <param name="value"></param>
  684.     /// <param name="response"></param>
  685.     internal GeckoDownloadProgressEventArgs(long currentProgress, long maxProgress, int progressPercent, double speed)
  686.     {
  687.         CurrentProgress = currentProgress;
  688.         MaxProgress = maxProgress;
  689.         ProgressPercent = progressPercent;
  690.         Speed = speed;
  691.         if (speed > 100.0)
  692.             LeftTime = new TimeSpan(0, 0, (int)((maxProgress - currentProgress) / speed));
  693.         else
  694.             LeftTime = new TimeSpan(0);
  695.     }
  696. }
Add Comment
Please, Sign In to add comment