2017-01-16 20:59:50 +01:00
|
|
|
|
/* Copyright (c) Citrix Systems, Inc.
|
2013-06-24 13:41:48 +02:00
|
|
|
|
* 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;
|
2016-10-02 23:04:58 +02:00
|
|
|
|
using System.Collections.Generic;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using XenAdmin.Controls;
|
|
|
|
|
using XenAPI;
|
|
|
|
|
|
|
|
|
|
namespace XenAdmin.Wizards.GenericPages
|
|
|
|
|
{
|
|
|
|
|
public interface IEnableableXenObjectComboBoxItem : IEnableableComboBoxItem
|
|
|
|
|
{
|
|
|
|
|
IXenObject Item { get; }
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-02 23:04:58 +02:00
|
|
|
|
public class DelayLoadingOptionComboBoxItem : IEnableableXenObjectComboBoxItem
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
public delegate void ReasonUpdatedEventHandler(object sender, EventArgs args);
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Event raised when the reason is updated
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event ReasonUpdatedEventHandler ReasonUpdated;
|
|
|
|
|
private string failureReason;
|
|
|
|
|
private IXenObject xenObject;
|
2018-05-24 18:45:27 +02:00
|
|
|
|
private const int DEFAULT_RETRIES = 10;
|
|
|
|
|
private const int DEFAULT_TIMEOUT = 200;
|
2016-10-02 23:04:58 +02:00
|
|
|
|
private readonly List<ReasoningFilter> _filters;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a new class instance and starts a thread to load data
|
|
|
|
|
/// </summary>
|
2016-10-02 23:04:58 +02:00
|
|
|
|
public DelayLoadingOptionComboBoxItem(IXenObject xenObject, List<ReasoningFilter> filters)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
this.xenObject = xenObject;
|
2016-10-02 23:04:58 +02:00
|
|
|
|
_filters = filters;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void CopyFrom(DelayLoadingOptionComboBoxItem toCopy)
|
|
|
|
|
{
|
|
|
|
|
xenObject = toCopy.xenObject;
|
|
|
|
|
failureReason = toCopy.FailureReason;
|
|
|
|
|
Enabled = toCopy.Enabled;
|
|
|
|
|
PreferAsSelectedItem = toCopy.PreferAsSelectedItem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Underlying Xen Object
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IXenObject Item
|
|
|
|
|
{
|
|
|
|
|
get { return xenObject; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// You would prefer this item to be the one that is selected
|
|
|
|
|
/// As the items are threaded they may exist and be disabled but required
|
|
|
|
|
/// as a selected item if they load successfully.
|
|
|
|
|
///
|
|
|
|
|
/// Use this in the event handler for the ReasonUpdated flag to find out
|
|
|
|
|
/// which item should be the selected one and thus which to
|
|
|
|
|
/// set in the combo box
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool PreferAsSelectedItem { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create a thread and fetch the reason
|
|
|
|
|
/// </summary>
|
2018-05-24 18:21:43 +02:00
|
|
|
|
public void LoadAsync()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2018-05-24 18:21:43 +02:00
|
|
|
|
//Set default reason without going through setter so as not to trigger the event
|
2013-06-24 13:41:48 +02:00
|
|
|
|
failureReason = Messages.DELAY_LOADING_COMBO_BOX_WAITING;
|
|
|
|
|
|
2018-05-24 18:45:27 +02:00
|
|
|
|
ThreadPool.QueueUserWorkItem(delegate { FetchFailureReasonWithRetry(DEFAULT_RETRIES, DEFAULT_TIMEOUT); });
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-24 18:21:43 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fetch the reason on the current thread
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void LoadSync()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2018-05-24 18:45:27 +02:00
|
|
|
|
FetchFailureReasonWithRetry(DEFAULT_RETRIES, DEFAULT_TIMEOUT);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-01 15:06:04 +01:00
|
|
|
|
public void CancelFilters()
|
|
|
|
|
{
|
|
|
|
|
foreach (ReasoningFilter filter in _filters)
|
|
|
|
|
filter.Cancel();
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-24 18:21:43 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Triggers event when setting the reason
|
|
|
|
|
/// </summary>
|
2013-06-24 13:41:48 +02:00
|
|
|
|
private void FetchFailureReasonWithRetry(int retries, int timeOut)
|
|
|
|
|
{
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-05-24 18:45:27 +02:00
|
|
|
|
FailureReason = FetchFailureReason();
|
|
|
|
|
return;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2018-05-24 18:45:27 +02:00
|
|
|
|
if (retries > 0)
|
|
|
|
|
Thread.Sleep(timeOut);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
} while (retries-- > 0);
|
|
|
|
|
|
2018-05-24 18:45:27 +02:00
|
|
|
|
FailureReason = Messages.DELAY_LOADED_COMBO_BOX_ITEM_FAILURE_UNKOWN;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Trigger event
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void OnReasonChanged(EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (ReasonUpdated != null)
|
|
|
|
|
ReasonUpdated(this, e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Enabled { get; private set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Setter will trigger reason updated event
|
|
|
|
|
/// If no failure result is found on setting set the enabled
|
|
|
|
|
/// </summary>
|
2016-10-02 23:04:58 +02:00
|
|
|
|
private string FailureReason
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
get { return failureReason; }
|
2016-10-02 23:04:58 +02:00
|
|
|
|
set
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
if (failureReason == value)
|
|
|
|
|
return;
|
|
|
|
|
failureReason = value;
|
|
|
|
|
|
|
|
|
|
Enabled = String.IsNullOrEmpty(failureReason);
|
|
|
|
|
|
|
|
|
|
OnReasonChanged(new EventArgs());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This will format the xen object and reason together
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(FailureReason))
|
2017-09-03 04:33:29 +02:00
|
|
|
|
return Item.Name();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2017-09-03 04:33:29 +02:00
|
|
|
|
return String.Format(Messages.DELAY_LOADED_COMBO_BOX_ITEM_FAILURE_REASON, Item.Name(), FailureReason);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fetch the reason from somewhere that may take some time
|
|
|
|
|
/// Called in a separate thread by the constructor
|
|
|
|
|
/// Returning String.Empty or null will mean no failure has been found
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2016-10-02 23:04:58 +02:00
|
|
|
|
protected virtual string FetchFailureReason()
|
|
|
|
|
{
|
|
|
|
|
foreach (ReasoningFilter filter in _filters)
|
|
|
|
|
{
|
|
|
|
|
if (filter.FailureFoundFor(Item))
|
|
|
|
|
{
|
|
|
|
|
return filter.Reason;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|