/* Copyright (c) Citrix Systems, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms,
* with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using XenAdmin.Controls;
using XenAdmin.Controls.Common;
using XenAdmin.Core;
using XenAdmin.Mappings;
using XenAdmin.Network;
using XenAPI;
using XenCenterLib;
namespace XenAdmin.Wizards.GenericPages
{
///
/// Class representing the page of the ImportAppliance wizard where the user specifies
/// storage repositories for the VMs in the improted appliance
///
internal abstract partial class SelectVMStorageWithMultipleVirtualDisksPage : XenTabPage
{
private class StorageDetail
{
public string SysId { get; set; }
public IStorageResource ResourceData { get; set; }
public ulong RequiredSpace {
get { return (ResourceData == null) ? 0 : ResourceData.RequiredDiskCapacity; }
}
}
private class EnableableSrComboboxItem : IEnableableXenObjectComboBoxItem
{
private readonly ToStringWrapper stringWrapper;
private readonly ulong spaceRequired;
public EnableableSrComboboxItem(ToStringWrapper stringWrapper, ulong spaceRequired)
{
this.stringWrapper = stringWrapper;
this.spaceRequired = spaceRequired;
}
public IXenObject Item
{
get { return stringWrapper.item; }
}
public SR Sr
{
get { return Item as SR; }
}
public bool Enabled
{
get { return (ulong)stringWrapper.item.FreeSpace() > spaceRequired; }
}
public override string ToString()
{
if(Enabled)
return stringWrapper.ToString();
return String.Format(Messages.SELECT_STORAGE_DROPDOWN_ERROR_NOT_ENOUGH_SPACE, stringWrapper);
}
public string SrName
{
get { return stringWrapper.item.name_label; }
}
}
#region Private fields
private Dictionary m_spaceRequirements = new Dictionary();
private Dictionary m_vmMappings;
private bool m_buttonNextEnabled;
private bool m_buttonPreviousEnabled;
#endregion
public SelectVMStorageWithMultipleVirtualDisksPage()
{
InitializeComponent();
InitializeText();
}
protected void InitializeText()
{
m_labelIntro.Text = IntroductionText;
m_radioAllOnSameSr.Text = AllOnSameSRRadioButtonText;
m_radioSpecifySr.Text = OnSpecificSRRadioButtonText;
m_comboBoxSr.GotFocus += m_comboBoxSr_GotFocus;
m_colVmDisk.HeaderText = VmDiskColumnHeaderText;
SetupTabIndices();
}
~SelectVMStorageWithMultipleVirtualDisksPage()
{
m_comboBoxSr.GotFocus -= m_comboBoxSr_GotFocus;
}
private void SetupTabIndices()
{
m_radioAllOnSameSr.TabIndex = 3;
m_comboBoxSr.TabIndex = 4;
m_radioSpecifySr.TabIndex = 5;
m_dataGridView.TabIndex = 6;
}
protected abstract string IntroductionText { get; }
protected abstract string AllOnSameSRRadioButtonText { get; }
protected abstract string OnSpecificSRRadioButtonText { get; }
protected virtual string VmDiskColumnHeaderText
{
get
{
return m_colVmDisk.HeaderText;
}
}
protected virtual void AddAditionalMappings(Dictionary vmMappings)
{}
#region Base class (XenTabPage) overrides
protected override void PageLeaveCore(PageLoadedDirection direction, ref bool cancel)
{
TargetConnection = null;
if (!cancel && direction == PageLoadedDirection.Forward && IsDirty && ImplementsIsDirty())
cancel = !PerformCheck(CheckStorageRequirements);
}
protected override void PageLoadedCore(PageLoadedDirection direction)
{
SetButtonPreviousEnabled(true);
}
public abstract StorageResourceContainer ResourceData(string sysId);
private bool displayDiskCapacity = true;
public bool DisplayDiskCapacity
{
set { displayDiskCapacity = value; }
}
private IXenConnection targetConnection;
///
/// The connection from which the target storage is read
/// Defaults to the base class connection if not set
///
public IXenConnection TargetConnection
{
get
{
if(targetConnection == null)
return Connection;
return targetConnection;
}
set { targetConnection = value; }
}
protected virtual bool SrIsSuitable(SR sr)
{
return true;
}
protected virtual bool IsExtraSpaceNeeded(XenRef sourceRef, XenRef targetRef)
{
return true;
}
public override void PopulatePage()
{
m_dataGridView.Rows.Clear();
m_ctrlError.HideError();
bool checkSpaceRequirements = true;
var targetRefs = new List