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.Collections.Generic;
|
|
|
|
|
using XenAdmin.Network;
|
|
|
|
|
using XenAdmin.Model;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using XenAdmin.Dialogs;
|
|
|
|
|
using XenAdmin.Actions;
|
2019-03-01 00:31:48 +01:00
|
|
|
|
using XenAPI;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace XenAdmin.Commands
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Launches the dialog for creating a new folder.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal class NewFolderCommand : Command
|
|
|
|
|
{
|
2018-11-13 01:09:10 +01:00
|
|
|
|
public event Action<string[]> FoldersCreated;
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of this Command. The parameter-less constructor is required if
|
|
|
|
|
/// this Command is to be attached to a ToolStrip menu item or button. It should not be used in any other scenario.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public NewFolderCommand()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public NewFolderCommand(IMainWindow mainWindow, IEnumerable<SelectedItem> selection)
|
|
|
|
|
: base(mainWindow, selection)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public NewFolderCommand(IMainWindow mainWindow, Folder folder)
|
|
|
|
|
: base(mainWindow, folder)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public NewFolderCommand(IMainWindow mainWindow, Folder folder, Control parent)
|
|
|
|
|
: base(mainWindow, folder)
|
|
|
|
|
{
|
2020-09-13 00:21:05 +02:00
|
|
|
|
Parent = parent;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-31 12:31:16 +02:00
|
|
|
|
private void Run(Folder folder, IWin32Window ownerWindow)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
IXenConnection connection;
|
|
|
|
|
String name;
|
|
|
|
|
|
|
|
|
|
// Different dialogs depending whether we're at the top level or adding a subfolder.
|
|
|
|
|
// Also offer them a choice of connections if the connection they're on is Read Only
|
|
|
|
|
// (although we will also sudo a Read Only command when the FolderAction is run).
|
|
|
|
|
if (folder == null || folder.IsRootFolder || folder.Connection == null || CrossConnectionCommand.IsReadOnly(folder.Connection))
|
|
|
|
|
{
|
2018-11-25 04:32:42 +01:00
|
|
|
|
using (var dialog = new NameAndConnectionPrompt
|
|
|
|
|
{
|
|
|
|
|
Text = Messages.NEW_FOLDER_DIALOG_TITLE,
|
|
|
|
|
OKText = Messages.CREATE_MNEMONIC_R,
|
|
|
|
|
HelpID = "NewFolderDialog"
|
|
|
|
|
})
|
|
|
|
|
{
|
|
|
|
|
if (dialog.ShowDialog(ownerWindow) != DialogResult.OK)
|
|
|
|
|
return;
|
|
|
|
|
name = dialog.PromptedName;
|
|
|
|
|
connection = dialog.Connection;
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-11-25 04:32:42 +01:00
|
|
|
|
using (var dialog = new InputPromptDialog {
|
|
|
|
|
Text = Messages.NEW_FOLDER_DIALOG_TITLE,
|
|
|
|
|
PromptText = Messages.NEW_FOLDER_NAME,
|
|
|
|
|
OkButtonText = Messages.NEW_FOLDER_BUTTON,
|
|
|
|
|
HelpID = "NewFolderDialog"
|
|
|
|
|
})
|
|
|
|
|
{
|
|
|
|
|
if (dialog.ShowDialog(ownerWindow) != DialogResult.OK)
|
|
|
|
|
return;
|
|
|
|
|
name = dialog.InputText;
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
connection = folder.Connection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<string> newPaths = new List<string>();
|
|
|
|
|
foreach (string s in name.Split(';'))
|
|
|
|
|
{
|
|
|
|
|
string n = s;
|
|
|
|
|
Folders.FixupRelativePath(ref n);
|
2013-08-05 15:28:21 +02:00
|
|
|
|
if (string.IsNullOrEmpty(n))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
newPaths.Add(Folders.AppendPath(folder == null ? Folders.PATH_SEPARATOR : folder.opaque_ref, n));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (newPaths.Count > 0)
|
|
|
|
|
{
|
2016-06-30 16:41:46 +02:00
|
|
|
|
var action = new CreateFolderAction(connection, newPaths.ToArray());
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2018-11-13 01:09:10 +01:00
|
|
|
|
action.Completed += Action_Completed;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
action.RunAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-13 01:09:10 +01:00
|
|
|
|
private void Action_Completed(ActionBase sender)
|
|
|
|
|
{
|
|
|
|
|
sender.Completed -= Action_Completed;
|
|
|
|
|
|
|
|
|
|
var action = sender as CreateFolderAction;
|
|
|
|
|
if (action != null && action.Succeeded)
|
|
|
|
|
{
|
|
|
|
|
if (FoldersCreated != null)
|
|
|
|
|
FoldersCreated(action.NewPaths);
|
|
|
|
|
|
|
|
|
|
Program.MainWindow.TrySelectNewNode(delegate(object o)
|
|
|
|
|
{
|
|
|
|
|
Folder ff = o as Folder;
|
|
|
|
|
return ff != null && action.NewPaths[0] == ff.opaque_ref;
|
|
|
|
|
}, true, true, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-31 12:31:16 +02:00
|
|
|
|
protected override void RunCore(SelectedItemCollection selection)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2021-08-31 12:31:16 +02:00
|
|
|
|
Run((Folder)selection[0].XenObject, Parent);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-31 12:31:16 +02:00
|
|
|
|
protected override bool CanRunCore(SelectedItemCollection selection)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2013-12-31 16:47:52 +01:00
|
|
|
|
return (selection.ContainsOneItemOfType<Folder>()
|
|
|
|
|
|| selection.ContainsOneItemOfType<GroupingTag>(t => t.Grouping is OrganizationViewFolders))
|
|
|
|
|
&& ConnectionAvailable();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool ConnectionAvailable()
|
|
|
|
|
{
|
|
|
|
|
foreach (IXenConnection connection in MainWindowCommandInterface.GetXenConnectionsCopy())
|
|
|
|
|
{
|
|
|
|
|
if (connection.IsConnected)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-13 00:21:05 +02:00
|
|
|
|
public override string MenuText => Messages.NEW_FOLDER;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2020-09-13 00:21:05 +02:00
|
|
|
|
public override Image MenuImage => Images.StaticImages._000_Folder_open_h32bit_16;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2021-08-31 12:31:16 +02:00
|
|
|
|
protected override string GetCantRunReasonCore(IXenObject item)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2021-08-31 12:31:16 +02:00
|
|
|
|
return ConnectionAvailable() ? base.GetCantRunReasonCore(item) : Messages.FOLDER_NO_CONNECTION;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|