|
| 1 | +// Copyright (c) Files Community |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System.Numerics; |
| 5 | +using System.Runtime.CompilerServices; |
| 6 | +using Windows.Win32; |
| 7 | + |
| 8 | +namespace Files.App.Storage.Storables |
| 9 | +{ |
| 10 | + public partial class HomeFolder : IHomeFolder |
| 11 | + { |
| 12 | + public string Id => "Home"; // Will be "files://Home" in the future. |
| 13 | + |
| 14 | + public string Name => "Home"; |
| 15 | + |
| 16 | + /// <inheritdoc/> |
| 17 | + public async IAsyncEnumerable<IStorableChild> GetItemsAsync(StorableType type = StorableType.Folder, [EnumeratorCancellation] CancellationToken cancellationToken = default) |
| 18 | + { |
| 19 | + await foreach (var folder in GetQuickAccessFolderAsync(cancellationToken)) |
| 20 | + { |
| 21 | + cancellationToken.ThrowIfCancellationRequested(); |
| 22 | + |
| 23 | + yield return folder; |
| 24 | + } |
| 25 | + |
| 26 | + await foreach (var drive in GetLogicalDrivesAsync(cancellationToken)) |
| 27 | + { |
| 28 | + cancellationToken.ThrowIfCancellationRequested(); |
| 29 | + |
| 30 | + yield return drive; |
| 31 | + } |
| 32 | + |
| 33 | + await foreach (var location in GetNetworkLocationsAsync(cancellationToken)) |
| 34 | + { |
| 35 | + cancellationToken.ThrowIfCancellationRequested(); |
| 36 | + |
| 37 | + yield return location; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /// <inheritdoc/> |
| 42 | + public IAsyncEnumerable<IStorableChild> GetQuickAccessFolderAsync(CancellationToken cancellationToken = default) |
| 43 | + { |
| 44 | + IFolder folder = new WindowsFolder(new Guid("3936e9e4-d92c-4eee-a85a-bc16d5ea0819")); |
| 45 | + return folder.GetItemsAsync(StorableType.Folder, cancellationToken); |
| 46 | + } |
| 47 | + |
| 48 | + /// <inheritdoc/> |
| 49 | + public async IAsyncEnumerable<IStorableChild> GetLogicalDrivesAsync([EnumeratorCancellation] CancellationToken cancellationToken = default) |
| 50 | + { |
| 51 | + var availableDrives = PInvoke.GetLogicalDrives(); |
| 52 | + if (availableDrives is 0) |
| 53 | + yield break; |
| 54 | + |
| 55 | + int count = BitOperations.PopCount(availableDrives); |
| 56 | + var driveLetters = new char[count]; |
| 57 | + |
| 58 | + count = 0; |
| 59 | + char driveLetter = 'A'; |
| 60 | + while (availableDrives is not 0) |
| 61 | + { |
| 62 | + if ((availableDrives & 1) is not 0) |
| 63 | + driveLetters[count++] = driveLetter; |
| 64 | + |
| 65 | + availableDrives >>= 1; |
| 66 | + driveLetter++; |
| 67 | + } |
| 68 | + |
| 69 | + foreach (int letter in driveLetters) |
| 70 | + { |
| 71 | + cancellationToken.ThrowIfCancellationRequested(); |
| 72 | + |
| 73 | + if (WindowsStorable.TryParse($"{letter}:\\") is not IWindowsStorable driveRoot) |
| 74 | + throw new InvalidOperationException(); |
| 75 | + |
| 76 | + yield return new WindowsFolder(driveRoot.ThisPtr); |
| 77 | + await Task.Yield(); |
| 78 | + } |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + /// <inheritdoc/> |
| 83 | + public IAsyncEnumerable<IStorableChild> GetNetworkLocationsAsync(CancellationToken cancellationToken = default) |
| 84 | + { |
| 85 | + Guid FOLDERID_NetHood = new("{C5ABBF53-E17F-4121-8900-86626FC2C973}"); |
| 86 | + IFolder folder = new WindowsFolder(FOLDERID_NetHood); |
| 87 | + return folder.GetItemsAsync(StorableType.Folder, cancellationToken); |
| 88 | + } |
| 89 | + |
| 90 | + /// <inheritdoc/> |
| 91 | + public IAsyncEnumerable<IStorableChild> GetRecentFilesAsync(CancellationToken cancellationToken = default) |
| 92 | + { |
| 93 | + Guid FOLDERID_NetHood = new("{AE50C081-EBD2-438A-8655-8A092E34987A}"); |
| 94 | + IFolder folder = new WindowsFolder(FOLDERID_NetHood); |
| 95 | + return folder.GetItemsAsync(StorableType.Folder, cancellationToken); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments