/**
* Page.xaml.cs
*
* Copyright 2009, Moxiecode Systems AB
* Released under GPL License.
*
* License: http://www.plupload.com/license
* Contributing: http://www.plupload.com/contributing
*/
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
using System.Net;
using System.IO;
using System.Collections.Generic;
using System.Threading;
using Moxiecode.Plupload;
namespace Moxiecode.Plupload {
///
/// Partial page class for the Silverlight page.
///
public partial class Page : UserControl {
#region private fields
private Dictionary files;
private int idCount = 0;
private FileReference currentFile;
private string id, filter;
private bool multiselect;
private bool disabled = false;
#endregion
///
/// Main constructor.
///
/// Silverlight init params.
public Page(IDictionary init_params) {
InitializeComponent();
HtmlPage.RegisterScriptableObject("Upload", this);
this.files = new Dictionary();
this.id = init_params["id"];
this.filter = init_params["filter"];
this.multiselect = Convert.ToBoolean(init_params["multiselect"]);
this.FireEvent("Init");
this.MouseLeftButtonUp += new MouseButtonEventHandler(OnClick);
this.MouseLeftButtonDown += new MouseButtonEventHandler(OnMouseLeftButtonDown);
this.MouseEnter += new MouseEventHandler(OnMouseEnter);
this.MouseLeave += new MouseEventHandler(OnMouseLeave);
}
private void OnClick(object sender, MouseEventArgs e) {
if (this.disabled) {
return;
}
OpenFileDialog dlg = new OpenFileDialog();
this.FireEvent("StartSelectFiles");
try {
dlg.Multiselect = this.multiselect;
dlg.Filter = this.filter;
if ((bool) dlg.ShowDialog()) {
foreach (FileInfo file in dlg.Files) {
FileReference uploadFile = new FileReference("u" + this.idCount++, file);
uploadFile.UploadChunkComplete += delegate(object up_sender, UploadEventArgs args) {
FileReference evtFile = (FileReference) up_sender;
this.FireEvent("UploadChunkSuccessful", evtFile.Id, args.Chunk, args.Chunks, args.Response);
};
uploadFile.UploadComplete += delegate(object up_sender, UploadEventArgs args) {
FileReference evtFile = (FileReference) up_sender;
this.FireEvent("UploadSuccessful", evtFile.Id, args.Response);
};
uploadFile.Error += delegate(object up_sender, ErrorEventArgs args) {
FileReference evtFile = (FileReference) up_sender;
this.FireEvent("UploadChunkError", evtFile.Id, args.Chunk, args.Chunks, args.Message);
};
uploadFile.Progress += delegate(object up_sender, ProgressEventArgs args) {
FileReference evtFile = (FileReference) up_sender;
this.FireEvent("UploadFileProgress", evtFile.Id, args.Loaded, args.Total);
};
this.FireEvent("SelectFile", uploadFile.Id, uploadFile.Name, uploadFile.Size);
this.files[uploadFile.Id] = uploadFile;
}
this.FireEvent("SelectSuccessful");
} else
this.FireEvent("SelectCancelled");
} catch (Exception ex) {
this.FireEvent("SelectError", ex.Message);
}
}
private void OnMouseLeftButtonDown(object sender, MouseEventArgs e) {
this.FireEvent("MouseLeftButtonDown");
}
private void OnMouseEnter(object sender, MouseEventArgs e) {
this.FireEvent("MouseEnter");
}
private void OnMouseLeave(object sender, MouseEventArgs e) {
this.FireEvent("MouseLeave");
}
///
/// Reference to page level plupload.silverlight script object.
///
public ScriptObject PluploadScriptObject {
get { return ((ScriptObject) HtmlPage.Window.Eval("plupload.silverlight")); }
}
///
/// Fires a specific event to the page level multi upload script.
///
/// Event name to fire.
public void FireEvent(string name) {
this.PluploadScriptObject.Invoke("trigger", new string[] { this.id, name });
}
///
/// Fires a specific event to the page level multi upload script.
///
/// Event name to fire.
/// Numerous parameters to send.
public void FireEvent(string name, params object[] paramlist) {
List