Skip to content

Code Quality: Removed NetworkDrivesViewModel and NetworkDrivesAPI #15155

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Apr 16, 2024
20 changes: 19 additions & 1 deletion src/Files.App/Data/Contracts/INetworkDrivesService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,22 @@ namespace Files.App.Data.Contracts
public interface INetworkDrivesService
{
/// <summary>
/// Enumerates network storage devices
/// Gets enumerated network storage drives.
/// </summary>
ObservableCollection<ILocatableFolder> Drives { get; }

/// <summary>
/// Enumerates network storage drives.
/// </summary>
/// <returns>A collection of network storage devices</returns>
IAsyncEnumerable<ILocatableFolder> GetDrivesAsync();

/// <summary>
/// Updates network storage drives to up-to-date.
/// </summary>
/// <returns></returns>
Task UpdateDrivesAsync();

/// <summary>
/// Displays the operating system dialog for connecting to a network storage device
/// </summary>
Expand All @@ -25,5 +36,12 @@ public interface INetworkDrivesService
/// <param name="drive">An item representing the network storage device to disconnect from</param>
/// <returns>True or false to indicate status</returns>
bool DisconnectNetworkDrive(ILocatableFolder drive);

/// <summary>
/// Authenticates the specified network share point.
/// </summary>
/// <param name="path">A path to the network share point.</param>
/// <returns>True If succeeds; otherwise, false.</returns>
Task<bool> AuthenticateNetworkShare(string path);
}
}
3 changes: 2 additions & 1 deletion src/Files.App/Data/Models/ItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public sealed class ItemViewModel : ObservableObject, IDisposable
private readonly IWindowsJumpListService jumpListService = Ioc.Default.GetRequiredService<IWindowsJumpListService>();
private readonly IDialogService dialogService = Ioc.Default.GetRequiredService<IDialogService>();
private IUserSettingsService UserSettingsService { get; } = Ioc.Default.GetRequiredService<IUserSettingsService>();
private readonly INetworkDrivesService NetworkDrivesService = Ioc.Default.GetRequiredService<INetworkDrivesService>();
private readonly IFileTagsSettingsService fileTagsSettingsService = Ioc.Default.GetRequiredService<IFileTagsSettingsService>();
private readonly ISizeProvider folderSizeProvider = Ioc.Default.GetRequiredService<ISizeProvider>();
private readonly IStorageCacheService fileListCache = Ioc.Default.GetRequiredService<IStorageCacheService>();
Expand Down Expand Up @@ -1506,7 +1507,7 @@ private async Task<int> EnumerateItemsFromStandardFolderAsync(string path, Cance

if (isNetwork)
{
var auth = await NetworkDrivesAPI.AuthenticateNetworkShare(path);
var auth = await NetworkDrivesService.AuthenticateNetworkShare(path);
if (!auth)
return -1;
}
Expand Down
118 changes: 118 additions & 0 deletions src/Files.App/Data/Models/NetworkConnectionDialog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright (c) 2024 Files Community
// Licensed under the MIT License. See the LICENSE.

using System.Runtime.InteropServices;
using System.Windows.Forms;
using Vanara.Extensions;
using Vanara.InteropServices;
using static Vanara.PInvoke.Mpr;

namespace Files.App.Data.Models
{
/// <summary>
/// A dialog box that allows the user to browse and connect to network resources.
/// </summary>
/// <remarks>
/// Forked from Vanara.
/// </remarks>
public sealed class NetworkConnectionDialog : CommonDialog
{
private readonly NETRESOURCE netRes = new();
private CONNECTDLGSTRUCT dialogOptions;

/// <summary>Initializes a new instance of the <see cref="NetworkConnectionDialog"/> class.</summary>
public NetworkConnectionDialog()
{
dialogOptions.cbStructure = (uint)Marshal.SizeOf(typeof(CONNECTDLGSTRUCT));
netRes.dwType = NETRESOURCEType.RESOURCETYPE_DISK;
}

/// <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 => 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
{
get => dialogOptions.dwFlags.IsFlagSet(CONN_DLG.CONNDLG_HIDE_BOX);
set => dialogOptions.dwFlags = dialogOptions.dwFlags.SetFlags(CONN_DLG.CONNDLG_HIDE_BOX, value);
}

/// <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(CONN_DLG.CONNDLG_PERSIST);
set
{
dialogOptions.dwFlags = dialogOptions.dwFlags.SetFlags(CONN_DLG.CONNDLG_PERSIST, value);
dialogOptions.dwFlags = dialogOptions.dwFlags.SetFlags(CONN_DLG.CONNDLG_NOT_PERSIST, !value);
}
}

/// <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; set => netRes.lpRemoteName = value; }

/// <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(CONN_DLG.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(CONN_DLG.CONNDLG_USE_MRU, value);
}
}

/// <inheritdoc/>
public override void Reset()
{
dialogOptions.dwDevNum = -1;
dialogOptions.dwFlags = 0;
dialogOptions.lpConnRes = IntPtr.Zero;
ReadOnlyPath = false;
}

/// <inheritdoc/>
protected override bool RunDialog(IntPtr hwndOwner)
{
using var lpNetResource = SafeCoTaskMemHandle.CreateFromStructure(netRes);

dialogOptions.hwndOwner = hwndOwner;
dialogOptions.lpConnRes = lpNetResource.DangerousGetHandle();

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

var result = WNetConnectionDialog1(dialogOptions);

dialogOptions.lpConnRes = IntPtr.Zero;

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

result.ThrowIfFailed();

return true;
}
}
}
76 changes: 0 additions & 76 deletions src/Files.App/Data/Models/NetworkDrivesViewModel.cs

This file was deleted.

1 change: 0 additions & 1 deletion src/Files.App/Helpers/Application/AppLifecycleHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ public static IHost ConfigureHost()
.AddSingleton<SidebarViewModel>()
.AddSingleton<SettingsViewModel>()
.AddSingleton<DrivesViewModel>()
.AddSingleton<NetworkDrivesViewModel>()
.AddSingleton<StatusCenterViewModel>()
.AddSingleton<AppearanceViewModel>()
.AddTransient<HomeViewModel>()
Expand Down
4 changes: 2 additions & 2 deletions src/Files.App/Helpers/Navigation/NavigationHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static class NavigationHelpers
{
private static MainPageViewModel MainPageViewModel { get; } = Ioc.Default.GetRequiredService<MainPageViewModel>();
private static DrivesViewModel DrivesViewModel { get; } = Ioc.Default.GetRequiredService<DrivesViewModel>();
private static NetworkDrivesViewModel NetworkDrivesViewModel { get; } = Ioc.Default.GetRequiredService<NetworkDrivesViewModel>();
private static INetworkDrivesService NetworkDrivesService { get; } = Ioc.Default.GetRequiredService<INetworkDrivesService>();

public static Task OpenPathInNewTab(string? path, bool focusNewTab)
{
Expand Down Expand Up @@ -169,7 +169,7 @@ private static async Task UpdateTabInfoAsync(TabBarItem tabItem, object navigati
}
else if (PathNormalization.NormalizePath(PathNormalization.GetPathRoot(currentPath)) == normalizedCurrentPath) // If path is a drive's root
{
var matchingDrive = NetworkDrivesViewModel.Drives.Cast<DriveItem>().FirstOrDefault(netDrive => normalizedCurrentPath.Contains(PathNormalization.NormalizePath(netDrive.Path), StringComparison.OrdinalIgnoreCase));
var matchingDrive = NetworkDrivesService.Drives.Cast<DriveItem>().FirstOrDefault(netDrive => normalizedCurrentPath.Contains(PathNormalization.NormalizePath(netDrive.Path), StringComparison.OrdinalIgnoreCase));
matchingDrive ??= DrivesViewModel.Drives.Cast<DriveItem>().FirstOrDefault(drive => normalizedCurrentPath.Contains(PathNormalization.NormalizePath(drive.Path), StringComparison.OrdinalIgnoreCase));
tabLocationHeader = matchingDrive is not null ? matchingDrive.Text : normalizedCurrentPath;
}
Expand Down
Loading
Loading