using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Net;
using System.IO;
using System.ComponentModel;
namespace TrayWeather.Utils
{
///
/// Download text data from the specified URI
///
class DownloadText
{
private BackgroundWorker textDownloader;
public delegate void DownloadedEventHandler(string Result);
public event DownloadedEventHandler DataDownloaded;
public DownloadText()
{
textDownloader = new System.ComponentModel.BackgroundWorker();
}
///
/// Download text data from the specified URI
///
/// Text data uri
/// Text codepage
public void GetTextAsync(Uri Uri, Encoding Codepage)
{
textDownloader.DoWork += new DoWorkEventHandler(xmlDownloader_DoWork);
textDownloader.RunWorkerCompleted += new RunWorkerCompletedEventHandler(xmlDownloader_RunWorkerCompleted);
GetTextArgs data = new GetTextArgs(Uri, Codepage);
textDownloader.RunWorkerAsync(data);
}
private void xmlDownloader_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
//Under Development
//******************************************************
//if (Settings.Default.DisplayNetworkErrors)
// throw e.Error;
}
else
{
this.DataDownloaded((string)e.Result);
}
}
private void xmlDownloader_DoWork(object sender, DoWorkEventArgs e)
{
GetTextArgs data = ((GetTextArgs)e.Argument);
Encoding encode = System.Text.Encoding.UTF8;
if (data.dataCodepage != null)
encode = data.dataCodepage;
e.Result = GetText(data.dataUri, data.dataCodepage);
}
private struct GetTextArgs
{
public GetTextArgs(Uri XmlUri, Encoding Codepage)
{
dataUri = XmlUri;
dataCodepage = Codepage;
}
public Uri dataUri;
public Encoding dataCodepage;
}
}
}