/********************************************************************
* FulcrumWeb RAD Framework - Fulcrum of your business *
* Copyright (c) 2002-2009 FulcrumWeb, ALL RIGHTS RESERVED *
* *
* THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED *
* FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE *
* COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE *
* AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT *
* AND PERMISSION FROM FULCRUMWEB. CONSULT THE END USER LICENSE *
* AGREEMENT FOR INFORMATION ON ADDITIONAL RESTRICTIONS. *
********************************************************************/
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
namespace Framework.Silverlight.Client
{
/// <summary>
/// Dialog that shows when application performs files uploading.
/// </summary>
public class CxUploadDialog : CxDialog
{
private readonly CxUploadPanel m_UploadPanel = new CxUploadPanel();
private long m_FileLenght;
string strFileLenght = "";
public event EventHandler CancelUpload;
//----------------------------------------------------------------------------
/// <summary>
/// Default .ctor
/// </summary>
public CxUploadDialog()
{
Buttons = NxDialogButtons.Custom;
HoldSizePercent = 40;
IsFixedSize = true;
Width = 500;
m_UploadPanel.SetValue(Grid.RowProperty, 1);
LayoutRoot.Children.Add(m_UploadPanel);
}
//----------------------------------------------------------------------------
/// <summary>
/// Creates custom buttons set.
/// Dialog calls to this method if DefaultButton property set as 'Custom'.
/// </summary>
/// <returns>Created list of dialog buttons.</returns>
protected override List<Button> CreateCustomButtons()
{
List<Button> btns = base.CreateCustomButtons();
CxDialogButton btnCancel = new CxDialogButton();
btnCancel.DialogResult = NxDialogResult.Cancel;
btnCancel.Content = "Cancel";
btns.Add(btnCancel);
btnCancel.Click += btnCancel_Click;
return btns;
}
//----------------------------------------------------------------------------
/// <summary>
/// Handles Click event.
/// </summary>
void btnCancel_Click(object sender, RoutedEventArgs e)
{
if(CancelUpload != null)
{
CancelUpload(this, EventArgs.Empty);
}
}
//----------------------------------------------------------------------------
/// <summary>
/// Sets progress bar position.
/// </summary>
/// <param name="fileLength">Length of whole file.</param>
/// <param name="bytesUploaded">Already uploaded bytes count.</param>
/// <param name="percentUploaded">Already uploaded bytes in percents.</param>
/// <param name="speed"></param>
public void SetProgress(long fileLength, long bytesUploaded, double percentUploaded, float speed)
{
m_UploadPanel.PersentValue = percentUploaded;
m_UploadPanel.PersentText = string.Concat(percentUploaded, "%");
if (m_FileLenght != fileLength)
{
m_FileLenght = fileLength;
strFileLenght = GetFileSizeAsString(fileLength);
}
m_UploadPanel.UploadedBytesText = string.Format(
"Uploaded: {0} from {1}", GetFileSizeAsString(bytesUploaded), strFileLenght);
m_UploadPanel.UpoadSpeedText = string.Concat("Upload speed kb/s: ", speed);
}
//----------------------------------------------------------------------------
/// <summary>
/// Converts and returns given file size in suitable for users string format.
/// </summary>
/// <param name="size">File size to convert.</param>
/// <returns>Converted file size.</returns>
public static string GetFileSizeAsString(long size)
{
double s = size;
string[] format = new [] { "{0} bytes", "{0} KB", "{0} MB", "{0} GB", "{0} TB", "{0} PB", "{0} EB", "{0} ZB", "{0} YB" };
int i = 0;
while (i < format.Length - 1 && s >= 1024)
{
s = (100 * s / 1024) / 100.0;
i++;
}
return string.Format(format[i], s.ToString("###,###,##0.##"));
}
}
}