Imports Max_Player.Music
Public Class DownloadItem
Private _Url, _Path As String
Private Pause As Boolean = False
Private Cancel As Boolean = False
Private DownloadSpeed As Single
''' <summary>
''' Indica l'indirizzo del file che si sta scaricando, in Internet.
''' </summary>
Public Property Url() As String
Get
Return _Url
End Get
Set(ByVal value As String)
_Url = value
End Set
End Property
''' <summary>
''' Indica il percorso su disco fisso del file che si sta scaricando.
''' </summary>
Public Property Path() As String
Get
Return _Path
End Get
Set(ByVal value As String)
_Path = value
lblName.Text = IO.Path.GetFileName(value)
End Set
End Property
Private Function GetImageIndex(ByVal FileName As String) As Int32
Select Case IO.Path.GetExtension(FileName).ToLower
Case ".wma"
Return 1
Case ".wav"
Return 2
Case ".mid", ".kar"
Return 3
Case ".mp3"
Return 4
Case ".mpg", ".wmv", ".avi", ".mp4"
Return 5
Case Else
Return 7
End Select
End Function
Private Function Clamp(ByVal Value As Single, ByVal Min As Single, ByVal Max As Single) As Single
If Value < Min Then
Return Min
End If
If Value > Max Then
Return Max
End If
Return Value
End Function
Private Function FormatSpeed(ByVal BitsPerSecond As Single) As String
If BitsPerSecond < 1000.0 Then
Return String.Format("{0:N2} bit/s", BitsPerSecond)
ElseIf BitsPerSecond < 1000000.0 Then
Return String.Format("{0:N2} Kbit/s", BitsPerSecond / 1000.0)
ElseIf BitsPerSecond < 1000000000.0 Then
Return String.Format("{0:N2} Mbit/s", BitsPerSecond / 1000000.0)
ElseIf BitsPerSecond < 1000000000000.0 Then
Return String.Format("{0:N2} Gbit/s", BitsPerSecond / 1000000000.0)
Else
Return String.Format("{0:N2} Tbit/s", BitsPerSecond / 1000000000000.0)
End If
End Function
Sub New(ByVal Url As String, ByVal Path As String)
Me.InitializeComponent()
Me.Url = Url
Me.Path = Path
If IO.
File.
Exists(Me.
Path) Then
lnkAllActions.Text = "Riprendi"
lnkCancel.Enabled = True
Else
lnkAllActions.Text = "Inizia"
End If
lblName.Text = IO.Path.GetFileName(Me.Path)
lblStatus.Text = "Inattivo"
imgFileType.Image = My.Forms.Form1.imgBigIcons.Images(GetImageIndex(Me.Path))
End Sub
Private Sub lnkStartPauseResume_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkAllActions.LinkClicked
Select Case lnkAllActions.Text
Case "Inizia"
Pause = False
bgDownloader.RunWorkerAsync()
lnkAllActions.Text = "Sospendi"
lnkCancel.Enabled = True
Case "Sospendi"
Pause = True
lnkAllActions.Text = "Riprendi"
lnkCancel.Enabled = True
Case "Riprendi"
Pause = False
bgDownloader.RunWorkerAsync()
lnkAllActions.Text = "Sospendi"
Case "Aggiungi al catalogo"
Dim File As New MediaFile
(Me.
Path, IO.
Path.
GetFileNameWithoutExtension(Me.
Path))
If Globals.CurrentCategory Is Nothing Then
If My.Forms.Form1.lstCategories.Items.Count = 0 Then
MessageBox.Show("Non è presente nessuna categoria in cui poter aggiungere il file!", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit Sub
End If
Globals.CurrentCategory = My.Forms.Form1.lstCategories.Items(0).Tag
End If
File.
Category = Globals.
CurrentCategory
Dim EditDialog
As New FileEditDialog
(File,
True)
If EditDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
My.Forms.Form1.LoadFileIntoList(EditDialog.MediaFile.Category)
My.Forms.Form1.LoadCategoryIntoList()
End If
EditDialog.Dispose()
lnkAllActions.Enabled = False
My.Forms.DownloadManagerForm.RemoveDownloadItem(Me)
End Select
End Sub
Private Sub bgDownloader_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgDownloader.DoWork
Dim StartIndex As Int64 = 0
If IO.
File.
Exists(Me.
Path) Then
StartIndex = FileLen(Me.Path)
End If
Dim Request As Net.HttpWebRequest = Net.HttpWebRequest.Create(Me.Url)
Dim Response As Net.HttpWebResponse
Dim FileSize As Int64
Dim ReadingStream As IO.Stream
Dim WritingStream As IO.FileStream
Request.AddRange(StartIndex)
Request.Credentials = Net.CredentialCache.DefaultCredentials
Response = Request.GetResponse()
FileSize = Response.ContentLength
ReadingStream = Response.GetResponseStream()
If StartIndex = FileSize Then
MessageBox.Show("Questo download è già stato completato!", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
If StartIndex = 0 Then
WritingStream = New IO.FileStream(Me.Path, IO.FileMode.Create)
Else
WritingStream = New IO.FileStream(Me.Path, IO.FileMode.Append)
End If
Dim BytesRead As Int32
Dim Buffer(4095) As Byte
Dim Crono As New Stopwatch
Dim CronoIndex As Int32 = 0
Try
Do While WritingStream.Position < FileSize + StartIndex
If CronoIndex = 0 Then
Crono.Reset()
Crono.Start()
End If
BytesRead = ReadingStream.Read(Buffer, 0, Buffer.Length)
If CronoIndex = 100 Then
Crono.Stop()
DownloadSpeed = Buffer.Length * 800 / (Crono.ElapsedMilliseconds / 1000)
CronoIndex = 0
Else
CronoIndex += 1
End If
WritingStream.Write(Buffer, 0, BytesRead)
If Pause Or Cancel Then
Exit Do
End If
bgDownloader.ReportProgress(100 * Clamp(WritingStream.Position / (FileSize + StartIndex), 0, 1))
If BytesRead = 0 Then
Exit Do
End If
Loop
Catch Ex As Exception
Stop
Finally
ReadingStream.Close()
WritingStream.Close()
Request = Nothing
Response = Nothing
End Try
If Pause Or Cancel Then
e.Cancel = True
End If
If Cancel Then
End If
End Sub
Private Sub bgDownloader_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgDownloader.ProgressChanged
lblStatus.Text = String.Format("Completato: {0:N0}%", e.ProgressPercentage)
lblSpeed.Text = FormatSpeed(DownloadSpeed)
prgProgress.Value = e.ProgressPercentage
End Sub
Private Sub bgDownloader_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgDownloader.RunWorkerCompleted
If e.Cancelled Then
If Pause Then
lblStatus.Text = "In pausa"
End If
If Cancel Then
My.Forms.DownloadManagerForm.RemoveDownloadItem(Me)
End If
If e.Error IsNot Nothing Then
lblStatus.Text = "Interrotto"
End If
Else
lblStatus.Text = "Completato"
lnkAllActions.Text = "Aggiungi al catalogo"
lnkCancel.Text = "Rimuovi"
End If
End Sub
Private Sub DownloadItem_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Enter
Me.BackColor = Color.LightBlue
End Sub
Private Sub DownloadItem_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Leave
Me.BackColor = Color.White
End Sub
Private Sub lnkCancel_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkCancel.LinkClicked
If MessageBox.Show("Anullare questo download?", "Max Player - Download Manager", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.No Then
Exit Sub
End If
If bgDownloader.IsBusy Then
Cancel = True
Else
My.Forms.DownloadManagerForm.RemoveDownloadItem(Me)
End If
End Sub
End Class