|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Collections.ObjectModel; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.IO; |
| 6 | +using System.Linq; |
| 7 | +using System.Text; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Windows.Storage; |
| 10 | +using Windows.UI.Xaml; |
| 11 | + |
| 12 | +namespace Files.Filesystem |
| 13 | +{ |
| 14 | + class Drives |
| 15 | + { |
| 16 | + public Task<ObservableCollection<DriveItem>> LocalDrives { get; } |
| 17 | + |
| 18 | + public Task<ObservableCollection<DriveItem>> RemovableDrives { get; } |
| 19 | + |
| 20 | + public Task<ObservableCollection<DriveItem>> VirtualDrives { get; } |
| 21 | + |
| 22 | + public Task<ObservableCollection<DriveItem>> AllDrives { get; } |
| 23 | + |
| 24 | + Drives() |
| 25 | + { |
| 26 | + LocalDrives = GetLocalDrivesListAsync(); |
| 27 | + RemovableDrives = GetRemovableDrivesListAsync(); |
| 28 | + VirtualDrives = GetVirtualDrivesListAsync(); |
| 29 | + |
| 30 | + InitAllDrivesList(); |
| 31 | + } |
| 32 | + |
| 33 | + private async void InitAllDrivesList() |
| 34 | + { |
| 35 | + var localDrives = await LocalDrives; |
| 36 | + localDrives.CollectionChanged += (sender, args) => { }; |
| 37 | + } |
| 38 | + |
| 39 | + private Task<ObservableCollection<DriveItem>> GetLocalDrivesListAsync() |
| 40 | + { |
| 41 | + return Task.Run(() => |
| 42 | + { |
| 43 | + var drives = new ObservableCollection<DriveItem>(); |
| 44 | + |
| 45 | + var driveLetters = DriveInfo.GetDrives().Select(x => x.RootDirectory.Root).OrderBy(x => x.Root.FullName) |
| 46 | + .ToList(); |
| 47 | + var removableDevicesTask = KnownFolders.RemovableDevices.GetFoldersAsync().AsTask(); |
| 48 | + removableDevicesTask.Wait(); |
| 49 | + var removableDevicesPaths = removableDevicesTask.Result.Select(x => x.Path).ToList(); |
| 50 | + |
| 51 | + |
| 52 | + foreach (var driveLetter in driveLetters) |
| 53 | + { |
| 54 | + try |
| 55 | + { |
| 56 | + var content = string.Empty; |
| 57 | + string icon = null; |
| 58 | + |
| 59 | + if (!removableDevicesPaths.Contains(driveLetter.Name)) |
| 60 | + { |
| 61 | + // TODO: Display Custom Names for Local Disks as well |
| 62 | + if (InstanceTabsView.NormalizePath(driveLetter.Name) != InstanceTabsView.NormalizePath("A:") |
| 63 | + && InstanceTabsView.NormalizePath(driveLetter.Name) != |
| 64 | + InstanceTabsView.NormalizePath("B:")) |
| 65 | + { |
| 66 | + content = $"Local Disk ({driveLetter.Name.TrimEnd('\\')})"; |
| 67 | + icon = "\uEDA2"; |
| 68 | + } |
| 69 | + else |
| 70 | + { |
| 71 | + content = $"Floppy Disk ({driveLetter.Name.TrimEnd('\\')})"; |
| 72 | + icon = "\uE74E"; |
| 73 | + } |
| 74 | + |
| 75 | + Visibility capacityBarVis = Visibility.Visible; |
| 76 | + ulong totalSpaceProg = 0; |
| 77 | + ulong freeSpaceProg = 0; |
| 78 | + string free_space_text = "Unknown"; |
| 79 | + string total_space_text = "Unknown"; |
| 80 | + |
| 81 | + try |
| 82 | + { |
| 83 | + |
| 84 | + var driveTask = StorageFolder.GetFolderFromPathAsync(driveLetter.Name).AsTask(); |
| 85 | + driveTask.Wait(); |
| 86 | + var retrivedPropertiesTask = driveTask.Result.Properties |
| 87 | + .RetrievePropertiesAsync(new string[] { "System.FreeSpace", "System.Capacity" }) |
| 88 | + .AsTask(); |
| 89 | + var retrivedProperties = retrivedPropertiesTask.Result; |
| 90 | + |
| 91 | + var sizeAsGBString = ByteSizeLib.ByteSize |
| 92 | + .FromBytes((ulong)retrivedProperties["System.FreeSpace"]).GigaBytes; |
| 93 | + freeSpaceProg = Convert.ToUInt64(sizeAsGBString); |
| 94 | + |
| 95 | + sizeAsGBString = ByteSizeLib.ByteSize |
| 96 | + .FromBytes((ulong)retrivedProperties["System.Capacity"]).GigaBytes; |
| 97 | + totalSpaceProg = Convert.ToUInt64(sizeAsGBString); |
| 98 | + |
| 99 | + free_space_text = ByteSizeLib.ByteSize |
| 100 | + .FromBytes((ulong)retrivedProperties["System.FreeSpace"]).ToString(); |
| 101 | + total_space_text = ByteSizeLib.ByteSize |
| 102 | + .FromBytes((ulong)retrivedProperties["System.Capacity"]).ToString(); |
| 103 | + } |
| 104 | + catch (UnauthorizedAccessException) |
| 105 | + { |
| 106 | + capacityBarVis = Visibility.Collapsed; |
| 107 | + } |
| 108 | + catch (NullReferenceException) |
| 109 | + { |
| 110 | + capacityBarVis = Visibility.Collapsed; |
| 111 | + } |
| 112 | + |
| 113 | + drives.Add(new DriveItem() |
| 114 | + { |
| 115 | + driveText = content, |
| 116 | + glyph = icon, |
| 117 | + maxSpace = totalSpaceProg, |
| 118 | + spaceUsed = totalSpaceProg - freeSpaceProg, |
| 119 | + tag = driveLetter.Name, |
| 120 | + progressBarVisibility = capacityBarVis, |
| 121 | + spaceText = free_space_text + " free of " + total_space_text, |
| 122 | + }); |
| 123 | + } |
| 124 | + } |
| 125 | + catch (UnauthorizedAccessException e) |
| 126 | + { |
| 127 | + Debug.WriteLine(e.Message); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return drives; |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + private Task<ObservableCollection<DriveItem>> GetRemovableDrivesListAsync() |
| 136 | + { |
| 137 | + return Task.Run(() => |
| 138 | + { |
| 139 | + var drives = new ObservableCollection<DriveItem>(); |
| 140 | + |
| 141 | + // TODO Get RemovableDrives |
| 142 | + |
| 143 | + return drives; |
| 144 | + }); |
| 145 | + } |
| 146 | + |
| 147 | + private Task<ObservableCollection<DriveItem>> GetVirtualDrivesListAsync() |
| 148 | + { |
| 149 | + return Task.Run(() => |
| 150 | + { |
| 151 | + var drives = new ObservableCollection<DriveItem>(); |
| 152 | + |
| 153 | + var oneDriveItem = new DriveItem() |
| 154 | + { |
| 155 | + driveText = "OneDrive", |
| 156 | + tag = "OneDrive", |
| 157 | + cloudGlyphVisibility = Visibility.Visible, |
| 158 | + driveGlyphVisibility = Visibility.Collapsed |
| 159 | + }; |
| 160 | + |
| 161 | + drives.Add(oneDriveItem); |
| 162 | + |
| 163 | + return drives; |
| 164 | + }); |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments