Skip to content

Code Quality: Removed Vanara from WindowsDialogService #16013

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Aug 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 34 additions & 116 deletions src/Files.App/Services/Windows/WindowsDialogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

using Microsoft.Extensions.Logging;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Vanara.Extensions;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.NetworkManagement.WNet;
Expand Down Expand Up @@ -162,139 +160,59 @@ public bool Open_FileSaveDialog(nint hWnd, bool pickFoldersOnly, string[] filter
}

/// <inheritdoc/>
public bool Open_NetworkConnectionDialog(nint hWind, bool hideRestoreConnectionCheckBox = false, bool persistConnectionAtLogon = false, bool readOnlyPath = false, string? remoteNetworkName = null, bool useMostRecentPath = false)
public unsafe bool Open_NetworkConnectionDialog(nint hWind, bool hideRestoreConnectionCheckBox = false, bool persistConnectionAtLogon = false, bool readOnlyPath = false, string? remoteNetworkName = null, bool useMostRecentPath = false)
{
using var dialog = new NetworkConnectionDialog()
{
HideRestoreConnectionCheckBox = hideRestoreConnectionCheckBox,
PersistConnectionAtLogon = persistConnectionAtLogon,
ReadOnlyPath = readOnlyPath,
RemoteNetworkName = remoteNetworkName!,
UseMostRecentPath = useMostRecentPath,
};

var window = Win32Helper.Win32Window.FromLong(hWind.ToInt64());
NETRESOURCEW netRes = default;
CONNECTDLGSTRUCTW dialogOptions = default;

return dialog.ShowDialog(window) == System.Windows.Forms.DialogResult.OK;
}
dialogOptions.cbStructure = (uint)Marshal.SizeOf(typeof(CONNECTDLGSTRUCTW));
netRes.dwType = NET_RESOURCE_TYPE.RESOURCETYPE_DISK;

private sealed class NetworkConnectionDialog : CommonDialog
{
private NETRESOURCEW netRes = new();
private CONNECTDLGSTRUCTW dialogOptions;
if (hideRestoreConnectionCheckBox)
dialogOptions.dwFlags |= CONNECTDLGSTRUCT_FLAGS.CONNDLG_HIDE_BOX;
else
dialogOptions.dwFlags &= ~CONNECTDLGSTRUCT_FLAGS.CONNDLG_HIDE_BOX;

/// <summary>Initializes a new instance of the <see cref="NetworkConnectionDialog"/> class.</summary>
public NetworkConnectionDialog()
if (persistConnectionAtLogon)
{
dialogOptions.cbStructure = (uint)Marshal.SizeOf(typeof(CONNECTDLGSTRUCTW));
netRes.dwType = NET_RESOURCE_TYPE.RESOURCETYPE_DISK;
dialogOptions.dwFlags |= CONNECTDLGSTRUCT_FLAGS.CONNDLG_PERSIST;
dialogOptions.dwFlags |= CONNECTDLGSTRUCT_FLAGS.CONNDLG_NOT_PERSIST;
}

/// <summary>Gets the connected device number. This value is only valid after successfully running the dialog.</summary>
/// <value>The connected device number. The value is 1 for A:, 2 for B:, 3 for C:, and so on. If the user made a deviceless connection, the value is –1.</value>
[Browsable(false)]
public int ConnectedDeviceNumber => (int)dialogOptions.dwDevNum;

/// <summary>Gets or sets a value indicating whether to hide the check box allowing the user to restore the connection at logon.</summary>
/// <value><c>true</c> if hiding restore connection check box; otherwise, <c>false</c>.</value>
[DefaultValue(false), Category("Appearance"), Description("Hide the check box allowing the user to restore the connection at logon.")]
public bool HideRestoreConnectionCheckBox
else
{
get => dialogOptions.dwFlags.HasFlag(CONNECTDLGSTRUCT_FLAGS.CONNDLG_HIDE_BOX);
set
{
if (value)
dialogOptions.dwFlags |= CONNECTDLGSTRUCT_FLAGS.CONNDLG_HIDE_BOX;
else
dialogOptions.dwFlags &= ~CONNECTDLGSTRUCT_FLAGS.CONNDLG_HIDE_BOX;
}
dialogOptions.dwFlags &= ~CONNECTDLGSTRUCT_FLAGS.CONNDLG_PERSIST;
dialogOptions.dwFlags &= ~CONNECTDLGSTRUCT_FLAGS.CONNDLG_NOT_PERSIST;
}

/// <summary>Gets or sets a value indicating whether restore the connection at logon.</summary>
/// <value><c>true</c> to restore connection at logon; otherwise, <c>false</c>.</value>
[DefaultValue(false), Category("Behavior"), Description("Restore the connection at logon.")]
public bool PersistConnectionAtLogon
{
get => dialogOptions.dwFlags.IsFlagSet(CONNECTDLGSTRUCT_FLAGS.CONNDLG_PERSIST);
set
{
dialogOptions.dwFlags = dialogOptions.dwFlags.SetFlags(CONNECTDLGSTRUCT_FLAGS.CONNDLG_PERSIST, value);
dialogOptions.dwFlags = dialogOptions.dwFlags.SetFlags(CONNECTDLGSTRUCT_FLAGS.CONNDLG_NOT_PERSIST, !value);
}
}
fixed (char* lpcRemoteName = remoteNetworkName)
netRes.lpRemoteName = lpcRemoteName;

/// <summary>
/// Gets or sets a value indicating whether to display a read-only path instead of allowing the user to type in a path. This is only
/// valid if <see cref="RemoteNetworkName"/> is not <see langword="null"/>.
/// </summary>
/// <value><c>true</c> to display a read only path; otherwise, <c>false</c>.</value>
[DefaultValue(false), Category("Appearance"), Description("Display a read-only path instead of allowing the user to type in a path.")]
public bool ReadOnlyPath { get; set; }

/// <summary>Gets or sets the name of the remote network.</summary>
/// <value>The name of the remote network.</value>
[DefaultValue(null), Category("Behavior"), Description("The value displayed in the path field.")]
public string RemoteNetworkName
{
get => netRes.lpRemoteName.ToString();
set
{
unsafe
{
fixed (char* lpcRemoteName = value)
netRes.lpRemoteName = lpcRemoteName;
}
}
}

/// <summary>Gets or sets a value indicating whether to enter the most recently used paths into the combination box.</summary>
/// <value><c>true</c> to use MRU path; otherwise, <c>false</c>.</value>
/// <exception cref="InvalidOperationException">UseMostRecentPath</exception>
[DefaultValue(false), Category("Behavior"), Description("Enter the most recently used paths into the combination box.")]
public bool UseMostRecentPath
{
get => dialogOptions.dwFlags.IsFlagSet(CONNECTDLGSTRUCT_FLAGS.CONNDLG_USE_MRU);
set
{
if (value && !string.IsNullOrEmpty(RemoteNetworkName))
throw new InvalidOperationException($"{nameof(UseMostRecentPath)} cannot be set to true if {nameof(RemoteNetworkName)} has a value.");

dialogOptions.dwFlags = dialogOptions.dwFlags.SetFlags(CONNECTDLGSTRUCT_FLAGS.CONNDLG_USE_MRU, value);
}
}
if (useMostRecentPath && !string.IsNullOrEmpty(remoteNetworkName))
throw new InvalidOperationException($"{nameof(useMostRecentPath)} cannot be set to true if {nameof(remoteNetworkName)} has a value.");

/// <inheritdoc/>
public unsafe override void Reset()
{
dialogOptions.dwDevNum = unchecked((uint)-1);
dialogOptions.dwFlags = 0;
dialogOptions.lpConnRes = null;
ReadOnlyPath = false;
}
if (useMostRecentPath)
dialogOptions.dwFlags |= CONNECTDLGSTRUCT_FLAGS.CONNDLG_USE_MRU;
else
dialogOptions.dwFlags &= ~CONNECTDLGSTRUCT_FLAGS.CONNDLG_USE_MRU;

/// <inheritdoc/>
protected unsafe override bool RunDialog(IntPtr hwndOwner)
{
dialogOptions.hwndOwner = new(hwndOwner);
dialogOptions.hwndOwner = new(hWind);

fixed (NETRESOURCEW* lpConnRes = &netRes)
dialogOptions.lpConnRes = lpConnRes;
dialogOptions.lpConnRes = &netRes;

if (ReadOnlyPath && !string.IsNullOrEmpty(netRes.lpRemoteName.ToString()))
dialogOptions.dwFlags |= CONNECTDLGSTRUCT_FLAGS.CONNDLG_RO_PATH;
if (readOnlyPath && !string.IsNullOrEmpty(netRes.lpRemoteName.ToString()))
dialogOptions.dwFlags |= CONNECTDLGSTRUCT_FLAGS.CONNDLG_RO_PATH;

var result = PInvoke.WNetConnectionDialog1W(ref dialogOptions);
var result = PInvoke.WNetConnectionDialog1W(ref dialogOptions);

dialogOptions.lpConnRes = null;
dialogOptions.lpConnRes = null;

if ((uint)result == unchecked((uint)-1))
return false;
if ((uint)result == unchecked((uint)-1))
return false;

if (result == 0)
throw new Win32Exception("Cannot display dialog");
if (result == 0)
throw new Win32Exception("Cannot display dialog");

return true;
}
return true;
}
}
}