home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 5_2007-2008.ISO / data / Zips / YouTube_Do2112345102008.psc / DownYouTube.ctl < prev    next >
Text File  |  2008-05-05  |  6KB  |  178 lines

  1. VERSION 5.00
  2. Begin VB.UserControl DownYouTube 
  3.    ClientHeight    =   2130
  4.    ClientLeft      =   0
  5.    ClientTop       =   0
  6.    ClientWidth     =   2760
  7.    Picture         =   "DownYouTube.ctx":0000
  8.    ScaleHeight     =   2130
  9.    ScaleWidth      =   2760
  10.    ToolboxBitmap   =   "DownYouTube.ctx":0312
  11. End
  12. Attribute VB_Name = "DownYouTube"
  13. Attribute VB_GlobalNameSpace = False
  14. Attribute VB_Creatable = True
  15. Attribute VB_PredeclaredId = False
  16. Attribute VB_Exposed = False
  17. Option Explicit
  18.  
  19. Event DownloadProgress(CurBytes As Long, MaxBytes As Long, SaveFile As String)
  20. Event DownloadError(SaveFile As String)
  21. Event DownloadComplete(MaxBytes As Long, SaveFile As String)
  22. Event DownloadAllComplete(FileNotDownload() As String)
  23. Event DownloadStage(sStage As String)
  24.  
  25. Private AsyncPropertyName() As String
  26. Private AsyncStatusCode() As Byte
  27.  
  28. Private Sub UserControl_AsyncReadProgress(AsyncProp As AsyncProperty)
  29.     On Error Resume Next
  30.  
  31.         If AsyncProp.BytesMax <> 0 Then
  32.             RaiseEvent DownloadProgress(CLng(AsyncProp.BytesRead), CLng(AsyncProp.BytesMax), AsyncProp.PropertyName)
  33.         End If
  34.  
  35.         Select Case AsyncProp.StatusCode
  36.           Case vbAsyncStatusCodeSendingRequest
  37.           RaiseEvent DownloadStage("Attempting Connection")
  38.             Debug.Print "Attempting to connect", AsyncProp.Target
  39.           Case vbAsyncStatusCodeConnecting
  40.           RaiseEvent DownloadStage("Connecting...")
  41.             Debug.Print "Connecting", AsyncProp.Status
  42.           Case vbAsyncStatusCodeBeginDownloadData
  43.           RaiseEvent DownloadStage("Beginning to Download")
  44.           RaiseEvent DownloadProgress(CLng(AsyncProp.BytesRead), CLng(AsyncProp.BytesMax), AsyncProp.PropertyName)
  45.             Debug.Print "Begin downloading", AsyncProp.Status
  46.             'Case vbAsyncStatusCodeDownloadingData
  47.             '  Debug.Print "Downloading", AsyncProp.Status 'show target URL
  48.           Case vbAsyncStatusCodeRedirecting
  49.             Debug.Print "Redirecting", AsyncProp.Status
  50.           Case vbAsyncStatusCodeEndDownloadData
  51.           RaiseEvent DownloadStage("Complete!")
  52.             Debug.Print "Download complete", AsyncProp.Status
  53.           Case vbAsyncStatusCodeError
  54.           RaiseEvent DownloadStage("Error!!")
  55.             Debug.Print "Error...aborting transfer", AsyncProp.Status
  56.             CancelAsyncRead AsyncProp.PropertyName
  57.         End Select
  58.  
  59. End Sub
  60.  
  61. Private Sub UserControl_AsyncReadComplete(AsyncProp As AsyncProperty)
  62.   Dim f() As Byte, fn As Long
  63.   Dim i As Integer
  64.  
  65.     On Error Resume Next
  66.  
  67.         Select Case AsyncProp.StatusCode
  68.           Case vbAsyncStatusCodeEndDownloadData
  69.             fn = FreeFile
  70.             f = AsyncProp.Value
  71.             RaiseEvent DownloadStage("Finalizing")
  72.             Debug.Print "Writting to file " & AsyncProp.PropertyName
  73.             Open AsyncProp.PropertyName For Binary Access Write As #fn
  74.             Put #fn, , f
  75.             Close #fn
  76.  
  77.             RaiseEvent DownloadComplete(CLng(AsyncProp.BytesMax), AsyncProp.PropertyName)
  78.  
  79.           Case vbAsyncStatusCodeError
  80.             CancelAsyncRead AsyncProp.PropertyName
  81.             RaiseEvent DownloadError(AsyncProp.PropertyName)
  82.         End Select
  83.  
  84.         For i = 1 To UBound(AsyncPropertyName)
  85.             If AsyncPropertyName(i) = AsyncProp.PropertyName Then
  86.                 AsyncStatusCode(i) = AsyncProp.StatusCode
  87.                 Exit For
  88.             End If
  89.         Next i
  90.  
  91.         CheckAllDownloadComplete
  92. End Sub
  93.  
  94. Private Sub UserControl_Initialize()
  95.     SizeIt
  96.     ReDim AsyncPropertyName(0)
  97.     ReDim AsyncStatusCode(0)
  98. End Sub
  99.  
  100. Private Sub UserControl_Resize()
  101.     SizeIt
  102. End Sub
  103.  
  104. Private Sub UserControl_Terminate()
  105.     If UBound(AsyncPropertyName) > 0 Then CancelAllDownload
  106. End Sub
  107.  
  108. Private Sub SizeIt()
  109.     On Error GoTo ErrorSizeIt
  110.     With UserControl
  111.         .Width = ScaleX(32, vbPixels, vbTwips)
  112.         .Height = ScaleY(32, vbPixels, vbTwips)
  113.     End With
  114. Exit Sub
  115.  
  116. ErrorSizeIt:
  117.     MsgBox err & ":Error in call to SizeIt()." _
  118.            & vbCrLf & vbCrLf & "Error Description: " & err.Description, vbCritical, "Warning"
  119.  
  120. Exit Sub
  121. End Sub
  122.  
  123. Public Sub BeginDownload(URL As String, SaveFile As String, Optional AsyncReadOptions = vbAsyncReadForceUpdate)
  124.     On Error GoTo ErrorBeginDownload
  125.     UserControl.AsyncRead URL, vbAsyncTypeByteArray, SaveFile, AsyncReadOptions
  126.  
  127.     ReDim Preserve AsyncPropertyName(UBound(AsyncPropertyName) + 1)
  128.     AsyncPropertyName(UBound(AsyncPropertyName)) = SaveFile
  129.     ReDim Preserve AsyncStatusCode(UBound(AsyncStatusCode) + 1)
  130.     AsyncStatusCode(UBound(AsyncStatusCode)) = 255
  131.  
  132. Exit Sub
  133.  
  134. ErrorBeginDownload:
  135.     MsgBox err & ":Error in call to BeginDownload()." _
  136.            & vbCrLf & vbCrLf & "Error Description: " & err.Description, vbCritical, "Warning"
  137.  
  138. Exit Sub
  139. End Sub
  140.  
  141. Public Function CancelAllDownload() As Boolean
  142.   Dim i As Integer
  143.  
  144.     On Error Resume Next
  145.  
  146.         For i = 1 To UBound(AsyncPropertyName)
  147.             CancelAsyncRead AsyncPropertyName(i)
  148.             RaiseEvent DownloadStage("Cancelling")
  149.             Debug.Print "Killing download " & AsyncPropertyName(i)
  150.         Next i
  151.  
  152.         ReDim AsyncPropertyName(0)
  153.         ReDim AsyncStatusCode(0)
  154.  
  155.         CancelAllDownload = True
  156. End Function
  157.  
  158. Private Function CheckAllDownloadComplete()
  159.   Dim i As Integer
  160.   Dim FileNotDownload() As String
  161.   Dim AllDownloadComplete As Boolean
  162.     ReDim FileNotDownload(0)
  163.     AllDownloadComplete = True
  164.     For i = 1 To UBound(AsyncStatusCode)
  165.         If AsyncStatusCode(i) = vbAsyncStatusCodeError Then
  166.             ReDim Preserve FileNotDownload(UBound(FileNotDownload) + 1)
  167.             FileNotDownload(UBound(FileNotDownload)) = AsyncPropertyName(i)
  168.           ElseIf AsyncStatusCode(i) <> vbAsyncStatusCodeEndDownloadData Then
  169.             AllDownloadComplete = False
  170.             Exit For
  171.         End If
  172.     Next i
  173.     If AllDownloadComplete Then
  174.         CancelAllDownload
  175.         RaiseEvent DownloadAllComplete(FileNotDownload)
  176.     End If
  177. End Function
  178.