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;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using XenAdmin.Actions;
|
2021-03-16 02:50:45 +01:00
|
|
|
|
using XenAdmin.Core;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace XenAdmin.Dialogs
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Monitors an AsyncAction, showing a progress bar and the action's Description. Shows the exception
|
|
|
|
|
/// to the user if the Action fails, and waits for the user to click OK. Has a cancel button: make
|
|
|
|
|
/// sure if enabled that the Action has sensible cancel behaviour. The cancel button is not shown by
|
|
|
|
|
/// default.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal partial class ActionProgressDialog : XenDialogBase
|
|
|
|
|
{
|
2021-09-23 14:13:07 +02:00
|
|
|
|
public event EventHandler CancelClicked;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
public readonly AsyncAction action;
|
2021-09-23 14:13:07 +02:00
|
|
|
|
|
|
|
|
|
public bool ShowTryAgainMessage { private get; set; } = true;
|
|
|
|
|
|
|
|
|
|
public bool ShowException { private get; set; } = true;
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
public bool ShowCancel
|
|
|
|
|
{
|
2021-09-23 14:13:07 +02:00
|
|
|
|
set => buttonCancel.Visible = value;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HideTitleBarIcons()
|
|
|
|
|
{
|
|
|
|
|
ControlBox = false;
|
|
|
|
|
MinimizeBox = false;
|
|
|
|
|
MaximizeBox = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ActionProgressDialog(String text)
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
labelStatus.Text = text;
|
2016-09-19 12:31:48 +02:00
|
|
|
|
labelSubActionStatus.Visible = false;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
progressBar1.Style = ProgressBarStyle.Marquee;
|
|
|
|
|
ShowIcon = false;
|
|
|
|
|
HideTitleBarIcons();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ActionProgressDialog(AsyncAction action, ProgressBarStyle progressBarStyle)
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
this.action = action;
|
2013-08-05 15:28:21 +02:00
|
|
|
|
action.Completed += action_Completed;
|
|
|
|
|
action.Changed += action_Changed;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
progressBar1.Style = progressBarStyle;
|
|
|
|
|
updateStatusLabel();
|
|
|
|
|
buttonCancel.Enabled = action.CanCancel;
|
|
|
|
|
ShowIcon = false;
|
|
|
|
|
HideTitleBarIcons();
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-05 15:28:21 +02:00
|
|
|
|
private void action_Changed(ActionBase sender)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
Program.AssertOffEventThread();
|
|
|
|
|
|
|
|
|
|
if (Disposing || IsDisposed || Program.Exiting)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
action.RecomputeCanCancel();
|
2015-04-16 15:17:02 +02:00
|
|
|
|
Program.Invoke(this, action_Changed_);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void action_Changed_()
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOnEventThread();
|
|
|
|
|
progressBar1.Value = action.PercentComplete;
|
|
|
|
|
updateStatusLabel();
|
|
|
|
|
buttonCancel.Enabled = action.CanCancel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateLabel(Label label, string description, string title)
|
|
|
|
|
{
|
|
|
|
|
label.Text = !string.IsNullOrEmpty(description)
|
|
|
|
|
? description
|
|
|
|
|
: !string.IsNullOrEmpty(title) ? title : string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updateStatusLabel()
|
|
|
|
|
{
|
|
|
|
|
UpdateLabel(labelStatus, action.Description, action.Title);
|
|
|
|
|
UpdateSubActionStatusLabel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateSubActionStatusLabel()
|
|
|
|
|
{
|
|
|
|
|
var multipleAction = action as MultipleAction;
|
|
|
|
|
labelSubActionStatus.Visible = multipleAction != null && multipleAction.ShowSubActionsDetails;
|
|
|
|
|
if (labelSubActionStatus.Visible)
|
2021-10-01 16:06:20 +02:00
|
|
|
|
{
|
|
|
|
|
UpdateLabel(labelSubActionStatus, multipleAction?.SubActionDescription, multipleAction?.SubActionTitle);
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-05 15:28:21 +02:00
|
|
|
|
private void action_Completed(ActionBase sender)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
if (Disposing || IsDisposed || Program.Exiting)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-09-23 14:13:07 +02:00
|
|
|
|
Program.Invoke(this, () =>
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2021-09-23 14:13:07 +02:00
|
|
|
|
if (action.Succeeded || action.Cancelled)
|
|
|
|
|
{
|
|
|
|
|
Close();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2021-09-23 14:13:07 +02:00
|
|
|
|
SwitchDialogToShowErrorState();
|
|
|
|
|
});
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SwitchDialogToShowErrorState()
|
|
|
|
|
{
|
|
|
|
|
this.SuspendLayout();
|
|
|
|
|
|
|
|
|
|
progressBar1.Visible = false;
|
|
|
|
|
buttonCancel.Visible = false;
|
|
|
|
|
buttonClose.Visible = true;
|
|
|
|
|
CancelButton = buttonClose;
|
|
|
|
|
ControlBox = true;
|
|
|
|
|
ShowIcon = true;
|
2021-09-23 14:13:07 +02:00
|
|
|
|
labelException.Visible = ShowException;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
icon.Visible = true;
|
2020-06-21 14:57:02 +02:00
|
|
|
|
icon.Image = Images.StaticImages._000_error_h32bit_32;
|
2021-09-23 14:13:07 +02:00
|
|
|
|
labelBottom.Visible = ShowTryAgainMessage;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
if (action.Exception == null)
|
|
|
|
|
{
|
|
|
|
|
labelException.Text = Messages.WIZARD_INTERNAL_ERROR;
|
|
|
|
|
}
|
|
|
|
|
else if (action.Exception is CancelledException)
|
|
|
|
|
{
|
|
|
|
|
labelException.Text = Messages.CANCELLED_BY_USER;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
labelException.Text = action.Exception.Message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.ResumeLayout();
|
|
|
|
|
this.CenterToParent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnShown(EventArgs e)
|
|
|
|
|
{
|
2021-03-16 02:50:45 +01:00
|
|
|
|
Text = BrandManager.BrandConsole;
|
|
|
|
|
action?.RunAsync();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
base.OnShown(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonClose_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
this.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonCancel_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
buttonCancel.Enabled = false;
|
|
|
|
|
|
|
|
|
|
if (CancelClicked != null)
|
|
|
|
|
CancelClicked(sender, e);
|
|
|
|
|
|
|
|
|
|
if (action != null)
|
|
|
|
|
action.Cancel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonClose_VisibleChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (buttonClose.Visible)
|
|
|
|
|
buttonClose.Focus();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|