Permalink
Browse files

Minor changes to the downloader, a few missing files added

  • Loading branch information...
1 parent d50561e commit a5fcdb19a859728f600ca5c1b4caaf2865f18163 @philc committed Oct 25, 2006
Showing with 77 additions and 34 deletions.
  1. +28 −0 Settings.cs
  2. BIN icon.ico
  3. +49 −34 src/downloader/FileDownloader.cs
View
@@ -0,0 +1,28 @@
+namespace InstallPad.Properties {
+
+
+ // This class allows you to handle specific events on the settings class:
+ // The SettingChanging event is raised before a setting's value is changed.
+ // The PropertyChanged event is raised after a setting's value is changed.
+ // The SettingsLoaded event is raised after the setting values are loaded.
+ // The SettingsSaving event is raised before the setting values are saved.
+ internal sealed partial class Settings {
+
+ public Settings() {
+ // // To add event handlers for saving and changing settings, uncomment the lines below:
+ //
+ // this.SettingChanging += this.SettingChangingEventHandler;
+ //
+ // this.SettingsSaving += this.SettingsSavingEventHandler;
+ //
+ }
+
+ private void SettingChangingEventHandler(object sender, System.Configuration.SettingChangingEventArgs e) {
+ // Add code to handle the SettingChangingEvent event here.
+ }
+
+ private void SettingsSavingEventHandler(object sender, System.ComponentModel.CancelEventArgs e) {
+ // Add code to handle the SettingsSaving event here.
+ }
+ }
+}
View
BIN icon.ico
Binary file not shown.
@@ -12,7 +12,7 @@
namespace CodeProject.Downloader
{
/// <summary>
- /// Downloads and resumes files from HTTP, FTP, and File URLS
+ /// Downloads and resumes files from HTTP, FTP, and File (file://) URLS
/// </summary>
public class FileDownloader
{
@@ -89,15 +89,11 @@ public void Download(string url, string destFolder)
// Find out the name of the file that the web server gave us.
string destFileName = Path.GetFileName(data.Response.ResponseUri.ToString());
-
+
// The place we're downloading to (not from) must not be a URI,
// because Path and File don't handle them...
destFolder = destFolder.Replace("file:///", "").Replace("file://", "");
this.downloadingTo = Path.Combine(destFolder, destFileName);
-
- // TODO cleanup
- //if (! destFolder.StartsWith("file://"))
- //this.downloadingTo=Path.GetFullPath(this.downloadingTo);
// Create the file on disk here, so even if we don't receive any data of the file
// it's still on disk. This allows us to download 0-byte files.
@@ -177,22 +173,18 @@ public void Download(List<string> urlList, string destFolder)
{
// validate input
if (urlList == null)
- {
throw new ArgumentException("Url list not specified.");
- }
if (urlList.Count == 0)
- {
throw new ArgumentException("Url list empty.");
- }
// try each url in the list.
// if one succeeds, we are done.
// if any fail, move to the next.
- Exception ex=null;
+ Exception ex = null;
foreach (string s in urlList)
{
- ex=null;
+ ex = null;
try
{
Download(s, destFolder);
@@ -210,28 +202,58 @@ public void Download(List<string> urlList, string destFolder)
}
/// <summary>
- /// Download a file from the url asynchronously.
+ /// Asynchronously download a file from the url.
/// </summary>
public void AsyncDownload(string url)
{
System.Threading.ThreadPool.QueueUserWorkItem(
new System.Threading.WaitCallback(this.WaitCallbackMethod), new string[] { url, "" });
}
/// <summary>
- /// Download a file from the url to the destination folder asynchronously.
+ /// Asynchronously download a file from the url to the destination folder.
/// </summary>
public void AsyncDownload(string url, string destFolder)
{
System.Threading.ThreadPool.QueueUserWorkItem(
new System.Threading.WaitCallback(this.WaitCallbackMethod), new string[] { url, destFolder });
}
/// <summary>
+ /// Asynchronously download a file from a list or URLs. If downloading from one of the URLs fails,
+ /// another URL is tried.
+ /// </summary>
+ public void AsyncDownload(List<string> urlList, string destFolder)
+ {
+ System.Threading.ThreadPool.QueueUserWorkItem(
+ new System.Threading.WaitCallback(this.WaitCallbackMethod), new object[] { urlList, destFolder });
+ }
+ /// <summary>
+ /// Asynchronously download a file from a list or URLs. If downloading from one of the URLs fails,
+ /// another URL is tried.
+ /// </summary>
+ public void AsyncDownload(List<string> urlList)
+ {
+ System.Threading.ThreadPool.QueueUserWorkItem(
+ new System.Threading.WaitCallback(this.WaitCallbackMethod), new object[] { urlList, "" });
+ }
+ /// <summary>
/// A WaitCallback used by the AsyncDownload methods.
/// </summary>
- private void WaitCallbackMethod(object data){
- // Expecting an array of two strings (url and dest folder)
- String[] strings = data as String[];
- this.Download(strings[0], strings[1]);
+ private void WaitCallbackMethod(object data)
+ {
+ // Can either be a string array of two strings (url and dest folder),
+ // or an object array containing a list<string> and a dest folder
+ if (data is string[])
+ {
+ String[] strings = data as String[];
+ this.Download(strings[0], strings[1]);
+ }
+ else
+ {
+ Object[] list = data as Object[];
+ List<String> urlList = list[0] as List<String>;
+ String destFolder = list[1] as string;
+ this.Download(urlList, destFolder);
+ }
}
private void SaveToFile(byte[] buffer, int count, string fileName)
{
@@ -272,16 +294,16 @@ class DownloadData
private long size;
private long start;
- private IWebProxy proxy=null;
+ private IWebProxy proxy = null;
public static DownloadData Create(string url, string destFolder)
{
return Create(url, destFolder, null);
}
- public static DownloadData Create(string url, string destFolder,IWebProxy proxy)
+ public static DownloadData Create(string url, string destFolder, IWebProxy proxy)
{
-
+
// This is what we will return
DownloadData downloadData = new DownloadData();
downloadData.proxy = proxy;
@@ -317,7 +339,7 @@ public static DownloadData Create(string url, string destFolder,IWebProxy proxy)
if (downloadData.IsProgressKnown && File.Exists(downloadTo))
{
// We only support resuming on http requests
- if (! (downloadData.Response is HttpWebResponse))
+ if (!(downloadData.Response is HttpWebResponse))
{
File.Delete(downloadTo);
}
@@ -337,7 +359,7 @@ public static DownloadData Create(string url, string destFolder,IWebProxy proxy)
req = downloadData.GetRequest(url);
((HttpWebRequest)req).AddRange((int)downloadData.start);
downloadData.response = req.GetResponse();
-
+
if (((HttpWebResponse)downloadData.Response).StatusCode != HttpStatusCode.PartialContent)
{
// They didn't support our resume request.
@@ -430,14 +452,7 @@ private WebRequest GetRequest(string url)
{
request.Proxy = this.proxy;
}
- /*else if (request is FtpWebRequest)
- {
- WebProxy proxy = new WebProxy();
- proxy.Address = new Uri("http://123yesman123.com");
- request.Proxy = proxy;
-
- }*/
-
+
return request;
}
@@ -462,7 +477,7 @@ public Stream DownloadStream
this.stream = this.response.GetResponseStream();
return this.stream;
}
- }
+ }
public long FileSize
{
get
@@ -484,10 +499,10 @@ public bool IsProgressKnown
// If the size of the remote url is -1, that means we
// couldn't determine it, and so we don't know
// progress information.
- return this.size>-1;
+ return this.size > -1;
}
}
- #endregion
+ #endregion
}
/// <summary>

0 comments on commit a5fcdb1

Please sign in to comment.