Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Max Player - DownloadItem.vb

DownloadItem.vb

Caricato da: Totem
Scarica il programma completo

  1. Imports Max_Player.Music
  2. Public Class DownloadItem
  3.     Private _Url, _Path As String
  4.     Private Pause As Boolean = False
  5.     Private Cancel As Boolean = False
  6.     Private DownloadSpeed As Single
  7.  
  8.     ''' <summary>
  9.     ''' Indica l'indirizzo del file che si sta scaricando, in Internet.
  10.     ''' </summary>
  11.     Public Property Url() As String
  12.         Get
  13.             Return _Url
  14.         End Get
  15.         Set(ByVal value As String)
  16.             _Url = value
  17.         End Set
  18.     End Property
  19.  
  20.     ''' <summary>
  21.     ''' Indica il percorso su disco fisso del file che si sta scaricando.
  22.     ''' </summary>
  23.     Public Property Path() As String
  24.         Get
  25.             Return _Path
  26.         End Get
  27.         Set(ByVal value As String)
  28.             _Path = value
  29.             lblName.Text = IO.Path.GetFileName(value)
  30.         End Set
  31.     End Property
  32.  
  33.     Private Function GetImageIndex(ByVal FileName As String) As Int32
  34.         Select Case IO.Path.GetExtension(FileName).ToLower
  35.             Case ".wma"
  36.                 Return 1
  37.             Case ".wav"
  38.                 Return 2
  39.             Case ".mid", ".kar"
  40.                 Return 3
  41.             Case ".mp3"
  42.                 Return 4
  43.             Case ".mpg", ".wmv", ".avi", ".mp4"
  44.                 Return 5
  45.             Case Else
  46.                 Return 7
  47.         End Select
  48.     End Function
  49.  
  50.     Private Function Clamp(ByVal Value As Single, ByVal Min As Single, ByVal Max As Single) As Single
  51.         If Value < Min Then
  52.             Return Min
  53.         End If
  54.         If Value > Max Then
  55.             Return Max
  56.         End If
  57.         Return Value
  58.     End Function
  59.  
  60.     Private Function FormatSpeed(ByVal BitsPerSecond As Single) As String
  61.         If BitsPerSecond < 1000.0 Then
  62.             Return String.Format("{0:N2} bit/s", BitsPerSecond)
  63.         ElseIf BitsPerSecond < 1000000.0 Then
  64.             Return String.Format("{0:N2} Kbit/s", BitsPerSecond / 1000.0)
  65.         ElseIf BitsPerSecond < 1000000000.0 Then
  66.             Return String.Format("{0:N2} Mbit/s", BitsPerSecond / 1000000.0)
  67.         ElseIf BitsPerSecond < 1000000000000.0 Then
  68.             Return String.Format("{0:N2} Gbit/s", BitsPerSecond / 1000000000.0)
  69.         Else
  70.             Return String.Format("{0:N2} Tbit/s", BitsPerSecond / 1000000000000.0)
  71.         End If
  72.     End Function
  73.  
  74.     Sub New(ByVal Url As String, ByVal Path As String)
  75.         Me.InitializeComponent()
  76.  
  77.         Me.Url = Url
  78.         Me.Path = Path
  79.  
  80.         If IO.File.Exists(Me.Path) Then
  81.             lnkAllActions.Text = "Riprendi"
  82.             lnkCancel.Enabled = True
  83.         Else
  84.             lnkAllActions.Text = "Inizia"
  85.         End If
  86.  
  87.         lblName.Text = IO.Path.GetFileName(Me.Path)
  88.         lblStatus.Text = "Inattivo"
  89.         imgFileType.Image = My.Forms.Form1.imgBigIcons.Images(GetImageIndex(Me.Path))
  90.     End Sub
  91.  
  92.     Private Sub lnkStartPauseResume_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkAllActions.LinkClicked
  93.         Select Case lnkAllActions.Text
  94.             Case "Inizia"
  95.                 Pause = False
  96.                 bgDownloader.RunWorkerAsync()
  97.                 lnkAllActions.Text = "Sospendi"
  98.                 lnkCancel.Enabled = True
  99.             Case "Sospendi"
  100.                 Pause = True
  101.                 lnkAllActions.Text = "Riprendi"
  102.                 lnkCancel.Enabled = True
  103.             Case "Riprendi"
  104.                 Pause = False
  105.                 bgDownloader.RunWorkerAsync()
  106.                 lnkAllActions.Text = "Sospendi"
  107.             Case "Aggiungi al catalogo"
  108.                 Dim File As New MediaFile(Me.Path, IO.Path.GetFileNameWithoutExtension(Me.Path))
  109.  
  110.                 If Globals.CurrentCategory Is Nothing Then
  111.                     If My.Forms.Form1.lstCategories.Items.Count = 0 Then
  112.                         MessageBox.Show("Non è presente nessuna categoria in cui poter aggiungere il file!", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  113.                         Exit Sub
  114.                     End If
  115.                     Globals.CurrentCategory = My.Forms.Form1.lstCategories.Items(0).Tag
  116.                 End If
  117.                 File.Category = Globals.CurrentCategory
  118.  
  119.                 Dim EditDialog As New FileEditDialog(File, True)
  120.                 If EditDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
  121.                     My.Forms.Form1.LoadFileIntoList(EditDialog.MediaFile.Category)
  122.                     My.Forms.Form1.LoadCategoryIntoList()
  123.                 End If
  124.                 EditDialog.Dispose()
  125.                 lnkAllActions.Enabled = False
  126.                 My.Forms.DownloadManagerForm.RemoveDownloadItem(Me)
  127.         End Select
  128.     End Sub
  129.  
  130.     Private Sub bgDownloader_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgDownloader.DoWork
  131.         Dim StartIndex As Int64 = 0
  132.  
  133.         If IO.File.Exists(Me.Path) Then
  134.             StartIndex = FileLen(Me.Path)
  135.         End If
  136.  
  137.         Dim Request As Net.HttpWebRequest = Net.HttpWebRequest.Create(Me.Url)
  138.         Dim Response As Net.HttpWebResponse
  139.         Dim FileSize As Int64
  140.         Dim ReadingStream As IO.Stream
  141.         Dim WritingStream As IO.FileStream
  142.  
  143.         Request.AddRange(StartIndex)
  144.         Request.Credentials = Net.CredentialCache.DefaultCredentials
  145.         Response = Request.GetResponse()
  146.         FileSize = Response.ContentLength
  147.         ReadingStream = Response.GetResponseStream()
  148.  
  149.         If StartIndex = FileSize Then
  150.             MessageBox.Show("Questo download è già stato completato!", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
  151.             Exit Sub
  152.         End If
  153.  
  154.         If StartIndex = 0 Then
  155.             WritingStream = New IO.FileStream(Me.Path, IO.FileMode.Create)
  156.         Else
  157.             WritingStream = New IO.FileStream(Me.Path, IO.FileMode.Append)
  158.         End If
  159.  
  160.         Dim BytesRead As Int32
  161.         Dim Buffer(4095) As Byte
  162.         Dim Crono As New Stopwatch
  163.         Dim CronoIndex As Int32 = 0
  164.  
  165.         Try
  166.             Do While WritingStream.Position < FileSize + StartIndex
  167.                 If CronoIndex = 0 Then
  168.                     Crono.Reset()
  169.                     Crono.Start()
  170.                 End If
  171.                 BytesRead = ReadingStream.Read(Buffer, 0, Buffer.Length)
  172.                 If CronoIndex = 100 Then
  173.                     Crono.Stop()
  174.                     DownloadSpeed = Buffer.Length * 800 / (Crono.ElapsedMilliseconds / 1000)
  175.                     CronoIndex = 0
  176.                 Else
  177.                     CronoIndex += 1
  178.                 End If
  179.                 WritingStream.Write(Buffer, 0, BytesRead)
  180.                 If Pause Or Cancel Then
  181.                     Exit Do
  182.                 End If
  183.                 bgDownloader.ReportProgress(100 * Clamp(WritingStream.Position / (FileSize + StartIndex), 0, 1))
  184.                 If BytesRead = 0 Then
  185.                     Exit Do
  186.                 End If
  187.             Loop
  188.         Catch Ex As Exception
  189.             Stop
  190.         Finally
  191.             ReadingStream.Close()
  192.             WritingStream.Close()
  193.             Request = Nothing
  194.             Response = Nothing
  195.         End Try
  196.  
  197.         If Pause Or Cancel Then
  198.             e.Cancel = True
  199.         End If
  200.         If Cancel Then
  201.             IO.File.Delete(Me.Path)
  202.         End If
  203.     End Sub
  204.  
  205.     Private Sub bgDownloader_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgDownloader.ProgressChanged
  206.         lblStatus.Text = String.Format("Completato: {0:N0}%", e.ProgressPercentage)
  207.         lblSpeed.Text = FormatSpeed(DownloadSpeed)
  208.         prgProgress.Value = e.ProgressPercentage
  209.     End Sub
  210.  
  211.     Private Sub bgDownloader_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgDownloader.RunWorkerCompleted
  212.         If e.Cancelled Then
  213.             If Pause Then
  214.                 lblStatus.Text = "In pausa"
  215.             End If
  216.             If Cancel Then
  217.                 My.Forms.DownloadManagerForm.RemoveDownloadItem(Me)
  218.             End If
  219.             If e.Error IsNot Nothing Then
  220.                 lblStatus.Text = "Interrotto"
  221.             End If
  222.         Else
  223.             lblStatus.Text = "Completato"
  224.             lnkAllActions.Text = "Aggiungi al catalogo"
  225.             lnkCancel.Text = "Rimuovi"
  226.         End If
  227.     End Sub
  228.  
  229.     Private Sub DownloadItem_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Enter
  230.         Me.BackColor = Color.LightBlue
  231.     End Sub
  232.  
  233.     Private Sub DownloadItem_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Leave
  234.         Me.BackColor = Color.White
  235.     End Sub
  236.  
  237.     Private Sub lnkCancel_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkCancel.LinkClicked
  238.         If MessageBox.Show("Anullare questo download?", "Max Player - Download Manager", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.No Then
  239.             Exit Sub
  240.         End If
  241.         If bgDownloader.IsBusy Then
  242.             Cancel = True
  243.         Else
  244.             My.Forms.DownloadManagerForm.RemoveDownloadItem(Me)
  245.         End If
  246.     End Sub
  247. End Class