2023-01-24 15:29:31 +01:00
|
|
|
/* Copyright (c) Cloud Software Group, Inc.
|
2013-06-24 13:41:48 +02:00
|
|
|
*
|
|
|
|
* 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.IO;
|
|
|
|
using System.Windows.Forms;
|
|
|
|
using XenAdmin.Controls;
|
|
|
|
using XenAdmin.Controls.Common;
|
|
|
|
using XenAdmin.Core;
|
2015-07-15 13:21:52 +02:00
|
|
|
using XenAdmin.Dialogs;
|
2017-11-17 02:04:45 +01:00
|
|
|
using XenCenterLib;
|
2021-11-22 13:02:26 +01:00
|
|
|
using XenModel;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
2023-02-13 14:47:21 +01:00
|
|
|
namespace XenAdmin.Wizards.BugToolWizard
|
2013-06-24 13:41:48 +02:00
|
|
|
{
|
|
|
|
public partial class BugToolPageDestination : XenTabPage
|
|
|
|
{
|
2023-02-13 14:47:21 +01:00
|
|
|
private bool _buttonNextEnabled;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
public BugToolPageDestination()
|
|
|
|
{
|
|
|
|
InitializeComponent();
|
2019-08-30 19:58:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public override string Text => Messages.BUGTOOL_PAGE_DESTINATION_TEXT;
|
|
|
|
|
|
|
|
public override string PageTitle => Messages.BUGTOOL_PAGE_DESTINATION_PAGETITLE;
|
|
|
|
|
|
|
|
public override string HelpID => "ReportDestination";
|
|
|
|
|
|
|
|
public override bool EnableNext()
|
|
|
|
{
|
2023-02-13 14:47:21 +01:00
|
|
|
return _buttonNextEnabled;
|
2019-08-30 19:58:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void PageLoadedCore(PageLoadedDirection direction)
|
|
|
|
{
|
|
|
|
if (direction != PageLoadedDirection.Forward)
|
|
|
|
return;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
2023-02-13 14:47:21 +01:00
|
|
|
m_textBoxName.Text =
|
|
|
|
$"{Messages.BUGTOOL_FILE_PREFIX}{HelpersGUI.DateTimeToString(DateTime.Now, "yyyy-MM-dd-HH-mm-ss", false)}.zip";
|
2013-06-24 13:41:48 +02:00
|
|
|
|
2023-02-13 14:47:21 +01:00
|
|
|
var initialDirectory = Properties.Settings.Default.ServerStatusPath;
|
2019-08-30 19:58:10 +02:00
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
try
|
|
|
|
{
|
2019-08-30 19:58:10 +02:00
|
|
|
//previous versions were storing the zip file name rather than the directory; check if this is the case
|
|
|
|
if (!string.IsNullOrEmpty(initialDirectory) && !Directory.Exists(initialDirectory))
|
|
|
|
initialDirectory = Path.GetDirectoryName(initialDirectory);
|
2013-06-24 13:41:48 +02:00
|
|
|
}
|
2019-08-30 19:58:10 +02:00
|
|
|
catch
|
2013-06-24 13:41:48 +02:00
|
|
|
{
|
2019-08-30 19:58:10 +02:00
|
|
|
//ignore
|
2013-06-24 13:41:48 +02:00
|
|
|
}
|
2019-08-30 19:58:10 +02:00
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(initialDirectory) || !Directory.Exists(initialDirectory))
|
|
|
|
m_textBoxLocation.Text = Win32.GetKnownFolderPath(Win32.KnownFolders.Downloads);
|
|
|
|
else
|
|
|
|
m_textBoxLocation.Text = initialDirectory;
|
|
|
|
|
2023-02-13 14:47:21 +01:00
|
|
|
PerformCheck(CheckPathValid);
|
2013-06-24 13:41:48 +02:00
|
|
|
}
|
|
|
|
|
2019-08-30 19:58:10 +02:00
|
|
|
protected override void PageLeaveCore(PageLoadedDirection direction, ref bool cancel)
|
|
|
|
{
|
|
|
|
if (direction != PageLoadedDirection.Forward)
|
|
|
|
return;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
2023-02-13 14:47:21 +01:00
|
|
|
if (!PerformCheck(CheckPathValid, CheckDestinationFolderExists))
|
2019-08-30 19:58:10 +02:00
|
|
|
{
|
|
|
|
cancel = true;
|
|
|
|
return;
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
2019-08-30 19:58:10 +02:00
|
|
|
string path = OutputFile;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
2019-08-30 19:58:10 +02:00
|
|
|
if (File.Exists(path)) //confirm ok to overwrite
|
|
|
|
{
|
2020-04-22 15:47:03 +02:00
|
|
|
using (var dlg = new WarningDialog(string.Format(Messages.FILE_X_EXISTS_OVERWRITE, path),
|
2019-08-30 19:58:10 +02:00
|
|
|
ThreeButtonDialog.ButtonOK,
|
2020-04-22 15:47:03 +02:00
|
|
|
new ThreeButtonDialog.TBDButton(Messages.CANCEL, DialogResult.Cancel, selected: true)))
|
2019-08-30 19:58:10 +02:00
|
|
|
{
|
|
|
|
if (dlg.ShowDialog(this) != DialogResult.OK)
|
|
|
|
{
|
|
|
|
cancel = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
2019-08-30 19:58:10 +02:00
|
|
|
// Check we can write to the destination file - otherwise we only find out at the
|
|
|
|
// end of the ZipStatusReportAction, and the downloaded server files are lost,
|
|
|
|
// and the user will have to run the wizard again.
|
2023-02-13 14:47:21 +01:00
|
|
|
FileStream stream = null;
|
2019-08-30 19:58:10 +02:00
|
|
|
try
|
|
|
|
{
|
2023-02-13 14:47:21 +01:00
|
|
|
stream = File.OpenWrite(path);
|
2019-08-30 19:58:10 +02:00
|
|
|
}
|
|
|
|
catch (Exception exn)
|
|
|
|
{
|
2020-04-22 15:47:03 +02:00
|
|
|
using (var dlg = new ErrorDialog(string.Format(Messages.COULD_NOT_WRITE_FILE, path, exn.Message)))
|
2019-08-30 19:58:10 +02:00
|
|
|
dlg.ShowDialog(this);
|
|
|
|
|
|
|
|
cancel = true;
|
|
|
|
return;
|
|
|
|
}
|
2023-02-13 14:47:21 +01:00
|
|
|
finally
|
|
|
|
{
|
|
|
|
stream?.Dispose();
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
2019-08-30 19:58:10 +02:00
|
|
|
// Save away the output directory for next time
|
|
|
|
Properties.Settings.Default.ServerStatusPath = m_textBoxLocation.Text.Trim();
|
2013-06-24 13:41:48 +02:00
|
|
|
}
|
|
|
|
|
2016-12-21 14:33:04 +01:00
|
|
|
public override void SelectDefaultControl()
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(m_textBoxLocation.Text))
|
|
|
|
m_textBoxLocation.Select();
|
|
|
|
}
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
public string OutputFile
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
var name = m_textBoxName.Text.Trim();
|
|
|
|
var folder = m_textBoxLocation.Text.Trim();
|
|
|
|
if (!name.EndsWith(".zip"))
|
|
|
|
name = string.Concat(name, ".zip");
|
|
|
|
|
2023-02-13 14:47:21 +01:00
|
|
|
return $@"{folder}\{name}";
|
2015-11-03 16:45:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
private bool PerformCheck(params CheckDelegate[] checks)
|
|
|
|
{
|
2023-02-13 14:47:21 +01:00
|
|
|
var success = m_ctrlError.PerformCheck(checks);
|
|
|
|
_buttonNextEnabled = success;
|
2013-06-24 13:41:48 +02:00
|
|
|
OnPageUpdated();
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool CheckPathValid(out string error)
|
|
|
|
{
|
|
|
|
error = string.Empty;
|
|
|
|
|
|
|
|
var name = m_textBoxName.Text.Trim();
|
|
|
|
var folder = m_textBoxLocation.Text.Trim();
|
|
|
|
|
2023-02-13 14:47:21 +01:00
|
|
|
if (string.IsNullOrEmpty(name))
|
2013-06-24 13:41:48 +02:00
|
|
|
return false;
|
|
|
|
|
2023-02-13 14:47:21 +01:00
|
|
|
if (!PathValidator.IsFileNameValid(name, out var invalidNameMsg))
|
2013-06-24 13:41:48 +02:00
|
|
|
{
|
2021-11-22 13:02:26 +01:00
|
|
|
error = $"{Messages.BUGTOOL_PAGE_DESTINATION_INVALID_NAME} {invalidNameMsg}";
|
2013-06-24 13:41:48 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-13 14:47:21 +01:00
|
|
|
if (string.IsNullOrEmpty(folder))
|
2013-06-24 13:41:48 +02:00
|
|
|
return false;
|
|
|
|
|
2023-02-13 14:47:21 +01:00
|
|
|
var path = $"{folder}\\{name}";
|
2013-06-24 13:41:48 +02:00
|
|
|
|
2023-02-13 14:47:21 +01:00
|
|
|
if (!PathValidator.IsPathValid(path, out var invalidPathMsg))
|
2013-06-24 13:41:48 +02:00
|
|
|
{
|
2021-11-22 13:02:26 +01:00
|
|
|
error = $"{Messages.BUGTOOL_PAGE_DESTINATION_INVALID_FOLDER} {invalidPathMsg}";
|
2013-06-24 13:41:48 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-30 19:58:10 +02:00
|
|
|
private bool CheckDestinationFolderExists(out string error)
|
|
|
|
{
|
|
|
|
error = string.Empty;
|
|
|
|
var folder = m_textBoxLocation.Text.Trim();
|
|
|
|
|
|
|
|
if (Directory.Exists(folder))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
error = Messages.ERROR_DESTINATION_DIR_NON_EXIST;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
#region Control event handlers
|
|
|
|
|
|
|
|
private void m_textBoxName_TextChanged(object sender, EventArgs e)
|
|
|
|
{
|
2023-02-13 14:47:21 +01:00
|
|
|
PerformCheck(CheckPathValid);
|
2013-06-24 13:41:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void m_textBoxLocation_TextChanged(object sender, EventArgs e)
|
|
|
|
{
|
2023-02-13 14:47:21 +01:00
|
|
|
PerformCheck(CheckPathValid);
|
2013-06-24 13:41:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void BrowseButton_Click(object sender, EventArgs e)
|
|
|
|
{
|
2023-02-13 14:47:21 +01:00
|
|
|
using (var dialog = new FolderBrowserDialog
|
2019-08-30 19:58:10 +02:00
|
|
|
{
|
|
|
|
SelectedPath = m_textBoxLocation.Text.Trim(),
|
|
|
|
Description = Messages.FOLDER_BROWSER_BUG_TOOL
|
|
|
|
})
|
2013-06-24 13:41:48 +02:00
|
|
|
{
|
2023-02-13 14:47:21 +01:00
|
|
|
if (dialog.ShowDialog() == DialogResult.OK)
|
|
|
|
m_textBoxLocation.Text = dialog.SelectedPath;
|
2013-06-24 13:41:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 19:58:10 +02:00
|
|
|
#endregion
|
2013-06-24 13:41:48 +02:00
|
|
|
}
|
|
|
|
}
|