using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;
using System.Threading;
using SharpDownload;
namespace SharpDownloadUI{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class FormFileDownload : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;
private System.Windows.Forms.Button buttonDownloadFolder;
private System.Windows.Forms.TextBox textDestination;
private System.Windows.Forms.Button buttonDownload;
private SharpDownloadUI.DownloadStatus downloadStatus1;
private SharpDownloadUI.DownloadStatus downloadStatus2;
private SharpDownloadUI.DownloadStatus downloadStatus3;
private SharpDownloadUI.DownloadStatus downloadStatus4;
private System.Windows.Forms.ComboBox comboSource;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox textUserID;
private System.Windows.Forms.TextBox textPassword;
private FileDownload fileDown = null;
private DownloadStatus[] downloadStatus;
private int fileDownloadID = -1;
private Thread downloadThread = null;
private const string MODULE_NAME = "FileDownloadMonitor";
private const long MIN_SPLIT_SIZE = 20000;
private const int MAX_FILE_PARTS = 4;
private System.Windows.Forms.Button buttonPause;
private bool reallyClose = true;
/// <summary>
/// Public overloaded constructor
/// </summary>
/// <param name="FileDownloadID">The download id number</param>
public FormFileDownload(int FileDownloadID) : this(FileDownloadID, string.Empty)
{
}
/// <summary>
/// Public overloaded constructor
/// </summary>
/// <param name="FileDownloadID">The download id number</param>
/// <param name="URL">URL to set for download</param>
public FormFileDownload(int FileDownloadID, string URL) : this(FileDownloadID, URL, Application.StartupPath)
{
}
/// <summary>
/// Public overloaded constructor
/// </summary>
/// <param name="FileDownloadID">The download id number</param>
/// <param name="URL">URL to set for download</param>
/// <param name="LocalFolder">Local folder to download to</param>
public FormFileDownload(int FileDownloadID, string URL, string LocalFolder) : this(FileDownloadID, URL, LocalFolder, false)
{
}
/// <summary>
/// Public overloaded constructor
/// </summary>
/// <param name="FileDownloadID">The download id number</param>
/// <param name="URL">URL to set for download</param>
/// <param name="StartNow">Start downloading immediately?</param>
public FormFileDownload(int FileDownloadID, string URL, string LocalFolder, bool StartNow)
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
// Set the download id
fileDownloadID = FileDownloadID;
// Set the source and destination for the download
this.comboSource.Text = URL;
this.textDestination.Text = LocalFolder;
// Check if we start the download immediately
if (StartNow)
{
this.StartDownload();
}
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.textDestination = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.buttonDownload = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
this.buttonDownloadFolder = new System.Windows.Forms.Button();
this.downloadStatus1 = new SharpDownloadUI.DownloadStatus();
this.downloadStatus2 = new SharpDownloadUI.DownloadStatus();
this.downloadStatus3 = new SharpDownloadUI.DownloadStatus();
this.downloadStatus4 = new SharpDownloadUI.DownloadStatus();
this.comboSource = new System.Windows.Forms.ComboBox();
this.textUserID = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.textPassword = new System.Windows.Forms.TextBox();
this.buttonPause = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textDestination
//
this.textDestination.Location = new System.Drawing.Point(8, 64);
this.textDestination.Name = "textDestination";
this.textDestination.Size = new System.Drawing.Size(480, 20);
this.textDestination.TabIndex = 1;
this.textDestination.Text = "c:\\";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(120, 16);
this.label1.TabIndex = 5;
this.label1.Text = "URL to download from:";
//
// buttonDownload
//
this.buttonDownload.Location = new System.Drawing.Point(496, 24);
this.buttonDownload.Name = "buttonDownload";
this.buttonDownload.Size = new System.Drawing.Size(72, 24);
this.buttonDownload.TabIndex = 1;
this.buttonDownload.Text = "Download";
this.buttonDownload.Click += new System.EventHandler(this.buttonDownload_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(8, 48);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(130, 16);
this.label2.TabIndex = 6;
this.label2.Text = "Folder to save download:";
//
// folderBrowserDialog1
//
this.folderBrowserDialog1.Description = "Select a folder for your download";
//
// buttonDownloadFolder
//
this.buttonDownloadFolder.Location = new System.Drawing.Point(496, 64);
this.buttonDownloadFolder.Name = "buttonDownloadFolder";
this.buttonDownloadFolder.Size = new System.Drawing.Size(24, 24);
this.buttonDownloadFolder.TabIndex = 14;
this.buttonDownloadFolder.Text = "...";
this.buttonDownloadFolder.Click += new System.EventHandler(this.buttonDownloadFolder_Click);
//
// downloadStatus1
//
this.downloadStatus1.BytesDownloaded = ((long)(0));
this.downloadStatus1.Location = new System.Drawing.Point(8, 144);
this.downloadStatus1.Name = "downloadStatus1";
this.downloadStatus1.PartNumber = 0;
this.downloadStatus1.PartSize = ((long)(0));
this.downloadStatus1.Size = new System.Drawing.Size(552, 16);
this.downloadStatus1.Status = "";
this.downloadStatus1.TabIndex = 16;
//
// downloadStatus2
//
this.downloadStatus2.BytesDownloaded = ((long)(0));
this.downloadStatus2.Location = new System.Drawing.Point(8, 168);
this.downloadStatus2.Name = "downloadStatus2";
this.downloadStatus2.PartNumber = 0;
this.downloadStatus2.PartSize = ((long)(0));
this.downloadStatus2.Size = new System.Drawing.Size(552, 16);
this.downloadStatus2.Status = "";
this.downloadStatus2.TabIndex = 17;
//
// downloadStatus3
//
this.downloadStatus3.BytesDownloaded = ((long)(0));
this.downloadStatus3.Location = new System.Drawing.Point(8, 192);
this.downloadStatus3.Name = "downloadStatus3";
this.downloadStatus3.PartNumber = 0;
this.downloadStatus3.PartSize = ((long)(0));
this.downloadStatus3.Size = new System.Drawing.Size(552, 16);
this.downloadStatus3.Status = "";
this.downloadStatus3.TabIndex = 18;
//
// downloadStatus4
//
this.downloadStatus4.BytesDownloaded = ((long)(0));
this.downloadStatus4.Location = new System.Drawing.Point(8, 216);
this.downloadStatus4.Name = "downloadStatus4";
this.downloadStatus4.PartNumber = 0;
this.downloadStatus4.PartSize = ((long)(0));
this.downloadStatus4.Size = new System.Drawing.Size(552, 16);
this.downloadStatus4.Status = "";
this.downloadStatus4.TabIndex = 19;
//
// comboSource
//
this.comboSource.Items.AddRange(new object[] {
"ftp://ftp.cdrom.com/pub/simtelnet/winxp/arcade/simon32.zip",
"http://the.earth.li/~sgtatham/putty/latest/x86/pageant.exe"});
this.comboSource.Location = new System.Drawing.Point(8, 24);
this.comboSource.Name = "comboSource";
this.comboSource.Size = new System.Drawing.Size(480, 21);
this.comboSource.TabIndex = 0;
//
// textUserID
//
this.textUserID.Location = new System.Drawing.Point(8, 104);
this.textUserID.Name = "textUserID";
this.textUserID.Size = new System.Drawing.Size(200, 20);
this.textUserID.TabIndex = 2;
this.textUserID.Text = "";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(8, 88);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(46, 16);
this.label3.TabIndex = 22;
this.label3.Text = "User ID:";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(288, 88);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(57, 16);
this.label4.TabIndex = 24;
this.label4.Text = "Password:";
//
// textPassword
//
this.textPassword.Location = new System.Drawing.Point(288, 104);
this.textPassword.Name = "textPassword";
this.textPassword.Size = new System.Drawing.Size(200, 20);
this.textPassword.TabIndex = 3;
this.textPassword.Text = "";
//
// buttonPause
//
this.buttonPause.Enabled = false;
this.buttonPause.Location = new System.Drawing.Point(496, 104);
this.buttonPause.Name = "buttonPause";
this.buttonPause.TabIndex = 25;
this.buttonPause.Text = "Pause";
this.buttonPause.Click += new System.EventHandler(this.buttonPause_Click);
//
// FormFileDownload
//
this.AcceptButton = this.buttonDownload;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(578, 239);
this.Controls.Add(this.buttonPause);
this.Controls.Add(this.label4);
this.Controls.Add(this.textPassword);
this.Controls.Add(this.label3);
this.Controls.Add(this.textUserID);
this.Controls.Add(this.textDestination);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.comboSource);
this.Controls.Add(this.downloadStatus4);
this.Controls.Add(this.downloadStatus3);
this.Controls.Add(this.downloadStatus2);
this.Controls.Add(this.downloadStatus1);
this.Controls.Add(this.buttonDownloadFolder);
this.Controls.Add(this.buttonDownload);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "FormFileDownload";
this.Text = "File Download";
this.Closing += new System.ComponentModel.CancelEventHandler(this.FormFileDownload_Closing);
this.Load += new System.EventHandler(this.formFileDownload_Load);
this.ResumeLayout(false);
}
private void buttonDownload_Click(object sender, System.EventArgs e)
{
this.StartDownload();
}
private void StartDownload()
{
string urlRequested = this.comboSource.Text;
string localFolder = this.textDestination.Text;
// Disable download button for now...
buttonDownload.Enabled = false;
// Raise a download status event
OnDownloadStatusEvent(new DownloadStatusEventArgs(this.fileDownloadID, 0, DownloadStatusEnum.DownloadInit, urlRequested, localFolder, string.Empty));
// Create a new file download
fileDown = new FileDownload();
fileDown.SourceAddress = urlRequested;
Debug.WriteLine("SourceAddress=" + fileDown.SourceAddress, MODULE_NAME);
fileDown.LocalFolder = localFolder;
Debug.WriteLine("Downloadfolder=" + fileDown.LocalFolder, MODULE_NAME);
// Check if the file already exists
if (System.IO.File.Exists(fileDown.DownloadInfo.LocalPathFile))
{
// Raise an error for now
MessageBox.Show("This file already exists [" + fileDown.DownloadInfo.LocalPathFile + "]. Please select a different download folder.", null);
buttonDownload.Enabled = true;
return;
}
// Check if user id and password set
if (textUserID.Text.Length!=0 | textPassword.Text.Length!=0)
{
fileDown.DownloadInfo.UserName = textUserID.Text;
fileDown.DownloadInfo.Password = textPassword.Text;
}
// Bind to events
fileDown.DownloadStartedEvent+=new FileDownload.DownloadStartedEventHandler(DownloadStarted);
fileDown.PartDownloadStartedEvent+=new FileDownload.PartDownloadStartedEventHandler(PartDownloadStarted);
fileDown.DataReceivedEvent+=new FileDownload.DataReceivedEventHandler(DataReceived);
fileDown.DownloadStoppedEvent+=new FileDownload.DownloadStoppedEventHandler(DownloadStopped);
fileDown.DownloadFinishedEvent+=new FileDownload.DownloadFinishedEventHandler(DownloadFinished);
fileDown.DownloadCompletedEvent+=new FileDownload.DownloadCompletedEventHandler(DownloadCompleted);
fileDown.DownloadFailedEvent+=new FileDownload.DownloadFailedEventHandler(DownloadFailed);
// Set up progress references
downloadStatus = new DownloadStatus[4] {downloadStatus1, downloadStatus2, downloadStatus3, downloadStatus4};
for (int i=0;i<downloadStatus.Length;i++)
{
downloadStatus[i].PartNumber=i;
}
// Start the async download process
try
{
downloadThread = fileDown.AsyncDownload();
downloadThread.Name = "FileDownload:" + fileDownloadID;
}
catch (Exception eAsyncDownload)
{
OnDownloadStatusEvent(new DownloadStatusEventArgs(this.fileDownloadID, 0, DownloadStatusEnum.DownloadFailed, string.Empty, string.Empty, eAsyncDownload.Message));
MessageBox.Show("The download for [" + fileDown.SourceAddress + "] did not start. (" + eAsyncDownload.Message + ")");
}
// Check if download started OK
if (downloadThread!=null)
{
// Set form close bool
reallyClose = false;
// Start the thread
downloadThread.Start();
// Update UI
buttonDownload.Enabled = false;
buttonPause.Enabled = true;
this.Text = "File Download: " + fileDown.SourceAddress;
}
else
{
// Update UI
buttonDownload.Enabled = true;
}
}
private void buttonDownloadFolder_Click(object sender, System.EventArgs e)
{
// Show the select folder dialog
DialogResult result = folderBrowserDialog1.ShowDialog(this);
if (result==DialogResult.OK)
{
// Set the download folder
textDestination.Text = folderBrowserDialog1.SelectedPath;
}
}
private void formFileDownload_Load(object sender, System.EventArgs e)
{
// Auto-select first item
if (this.comboSource.Items.Count>0 && this.comboSource.Text==string.Empty)
{
this.comboSource.SelectedIndex = 0;
}
}
public int FileDownloadID
{
get
{
return fileDownloadID;
}
}
private void FormFileDownload_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// Check if it is OK to close the form completely
if (!reallyClose)
{
// Don't close the form - just hide it until we're finished
this.Hide();
e.Cancel = true;
}
}
protected bool ReallyClose
{
set
{
reallyClose=value;
}
}
#region Event Handlers
/// <summary>
/// Download started event handler
/// </summary>
/// <param name="e"></param>
private void DownloadStarted(FileDownload.DownloadStartedEventArgs e)
{
Debug.WriteLine("DownloadStarted event raised", MODULE_NAME);
try
{
// Raise a download status event
OnDownloadStatusEvent(new DownloadStatusEventArgs(this.fileDownloadID, fileDown.DownloadInfo.FileSize, DownloadStatusEnum.DownloadStarted, string.Empty, string.Empty, string.Empty));
}
catch (Exception eStarted)
{
// Log errors
Debug.WriteLine("Error occurred in DataStarted event handler ["+ eStarted.Message +"]", MODULE_NAME);
}
}
private void PartDownloadStarted(FileDownload.PartDownloadStartedEventArgs e)
{
Debug.WriteLine("PartDownloadStarted event raised", MODULE_NAME);
try
{
// Update download status display
downloadStatus[e.PartNumber].Status = "Download started";
downloadStatus[e.PartNumber].PartSize = fileDown[e.PartNumber].PartSize;
downloadStatus[e.PartNumber].UpdateDisplay();
}
catch (Exception eStarted)
{
// Log errors
Debug.WriteLine("Error occurred in DataStarted event handler ["+ eStarted.Message +"]", MODULE_NAME);
}
}
/// <summary>
/// Data received event handler
/// </summary>
/// <param name="e">data received event args</param>
private void DataReceived(FileDownload.DataReceivedEventArgs e)
{
try
{
// Update the part status display
downloadStatus[e.PartNumber].BytesDownloaded = e.BytesDownloaded;
downloadStatus[e.PartNumber].Status = "Receiving data...";
downloadStatus[e.PartNumber].UpdateDisplay();
// Calculate total bytes downloaded
long totalSize = 0;
for (int i=0;i<downloadStatus.Length;i++)
{
totalSize+=downloadStatus[i].BytesDownloaded;
}
// Raise a download status event
OnDownloadStatusEvent(new DownloadStatusEventArgs(this.fileDownloadID, totalSize, DownloadStatusEnum.DataReceived, string.Empty, string.Empty, string.Empty));
}
catch (Exception eReceived)
{
// Log errors
Debug.WriteLine("Error occurred in DataReceived event handler ["+ eReceived.Message +"]", MODULE_NAME);
}
}
/// <summary>
/// Data failed event handler
/// </summary>
/// <param name="e"></param>
private void DownloadFailed(FileDownload.DownloadFailedEventArgs e)
{
Debug.WriteLine("DownloadFailed event raised", MODULE_NAME);
try
{
// Raise a download status event
OnDownloadStatusEvent(new DownloadStatusEventArgs(this.fileDownloadID, 0, DownloadStatusEnum.DownloadFailed, string.Empty, string.Empty, e.Message));
MessageBox.Show(this, "Download failed! Reason: " + e.Message);
// Update UI
buttonDownload.Enabled = true;
buttonPause.Enabled = false;
}
catch (Exception eFailed)
{
// Log errors
Debug.WriteLine("Error occurred in DownloadFailed event handler ["+ eFailed.Message +"]", MODULE_NAME);
}
}
/// <summary>
/// Download stopped handler
/// </summary>
/// <param name="e"></param>
private void DownloadStopped(FileDownload.DownloadStoppedEventArgs e)
{
Debug.WriteLine("DownloadStopped event raised", MODULE_NAME);
try
{
// Set download status
downloadStatus[e.PartNumber].Status = "Download stopped";
downloadStatus[e.PartNumber].UpdateDisplay();
buttonDownload.Enabled = true;
buttonPause.Enabled = false;
}
catch (Exception eFinished)
{
// Log errors
Debug.WriteLine("Error occurred in DownloadStopped event handler ["+ eFinished.Message +"]", MODULE_NAME);
}
}
/// <summary>
/// Download finished callback
/// </summary>
/// <param name="e"></param>
private void DownloadFinished(FileDownload.DownloadFinishedEventArgs e)
{
Debug.WriteLine("DownloadFinished event raised", MODULE_NAME);
try
{
// Set download status
downloadStatus[e.PartNumber].Status = "Download complete";
downloadStatus[e.PartNumber].UpdateDisplay();
}
catch (Exception eFinished)
{
// Log errors
Debug.WriteLine("Error occurred in DownloadFinished event handler ["+ eFinished.Message +"]", MODULE_NAME);
}
}
/// <summary>
/// Download completed event handler
/// </summary>
/// <param name="e"></param>
private void DownloadCompleted(FileDownload.DownloadCompletedEventArgs e)
{
Debug.WriteLine("DownloadCompleted event raised", MODULE_NAME);
try
{
// Raise a download status event
OnDownloadStatusEvent(new DownloadStatusEventArgs(this.fileDownloadID, 0, DownloadStatusEnum.DownloadComplete, string.Empty, string.Empty, string.Empty));
// Close the file download monitor form
this.Close();
}
catch (Exception eCompleted)
{
// Log errors
Debug.WriteLine("Error occurred in DownloadCompleted event handler ["+ eCompleted.Message +"]", MODULE_NAME);
}
}
#endregion
#region DownloadStatus Event
/// <summary>
/// Download status event arguments
/// </summary>
public class DownloadStatusEventArgs : EventArgs
{
private readonly int fileDownloadID;
private readonly long fileSize;
private readonly DownloadStatusEnum downloadStatus;
private readonly string url;
private readonly string localFolder;
private string info;
/// <summary>
/// DownloadStatus event arguments
/// </summary>
/// <param name="FileDownloadID">The file download number that raised the event</param>
/// <param name="FileSize">The size of the file in Bytes</param>
/// <param name="DownloadStatus">Current status of the download</param>
public DownloadStatusEventArgs (int FileDownloadID, long FileSize, DownloadStatusEnum DownloadStatus, string URL, string LocalFolder, string Info)
{
fileDownloadID = FileDownloadID;
fileSize = FileSize;
downloadStatus = DownloadStatus;
url = URL;
localFolder = LocalFolder;
info = Info;
}
/// <summary>
/// Public read-only property for the download file number
/// </summary>
public int FileDownloadID
{
get
{
return fileDownloadID;
}
}
/// <summary>
/// Public read-only property for the size of the file in Bytes
/// </summary>
public long FileSize
{
get
{
return fileSize;
}
}
/// <summary>
/// Public read-only property shows the current download status
/// </summary>
public DownloadStatusEnum DownloadStatus
{
get
{
return downloadStatus;
}
}
/// <summary>
/// Public read-only property for URL
/// </summary>
public string URL
{
get
{
return url;
}
}
/// <summary>
/// Public read-only property for info
/// </summary>
public string LocalFolder
{
get
{
return localFolder;
}
}
/// <summary>
/// Public read-only property for info
/// </summary>
public string Info
{
get
{
return info;
}
}
}
/// <summary>
/// Delegate for Download Status Event
/// </summary>
public delegate void DownloadStatusEventHandler(DownloadStatusEventArgs e);
/// <summary>
/// The Download Status Event is raised when the download status is updated
/// </summary>
public event DownloadStatusEventHandler DownloadStatusEvent;
/// <summary>
/// OnDownloadStatusEvent
/// </summary>
/// <param name="e">DownloadStatusEventArgs</param>
protected virtual void OnDownloadStatusEvent(DownloadStatusEventArgs e)
{
if (DownloadStatusEvent != null)
{
DownloadStatusEvent(e);
}
}
#endregion
private void buttonPause_Click(object sender, System.EventArgs e)
{
if (fileDown!=null)
{
buttonPause.Enabled = false;
fileDown.Command = FileDownload.FileDownloadCommandEnum.Pause;
}
}
}
}
|