Skip to content

Commit 8e3c666

Browse files
authored
Merge pull request #262 from duke7553/dynamic-drives-list
Dynamic Drives List
2 parents b556239 + 90b3e92 commit 8e3c666

13 files changed

+461
-239
lines changed

Files UWP/App.xaml.cs

Lines changed: 198 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,18 @@
2323
using System.Collections.ObjectModel;
2424
using Windows.Devices.Enumeration;
2525
using System.Text.RegularExpressions;
26+
using Windows.Devices.Portable;
27+
using Windows.UI.Xaml.Media.Imaging;
28+
using Windows.ApplicationModel.Core;
29+
using Windows.UI.Core;
30+
using Windows.Storage.Search;
2631

2732
namespace Files
2833
{
2934
sealed partial class App : Application
3035
{
3136
public static ProHome selectedTabInstance { get; set; }
37+
DeviceWatcher watcher;
3238
public App()
3339
{
3440
this.InitializeComponent();
@@ -113,91 +119,207 @@ public App()
113119
localSettings.Values["DrivesDisplayed_NewTab"] = false;
114120
}
115121

116-
FindDrives();
117-
//DeviceWatcher watcher = DeviceInformation.CreateWatcher();
118-
//watcher.Added += (sender, info) => FindDrives();
119-
//watcher.Removed += (sender, info) => FindDrives();
120-
//watcher.Start();
122+
//FindDrives();
123+
121124
}
122125

123-
private async void FindDrives()
126+
public void PopulateDrivesListWithLocalDisks()
124127
{
125-
foundDrives.Clear();
126-
var knownRemDevices = new ObservableCollection<string>();
127-
foreach (var f in await KnownFolders.RemovableDevices.GetFoldersAsync())
128-
{
129-
var path = f.Path;
130-
knownRemDevices.Add(path);
131-
}
132-
133128
var driveLetters = DriveInfo.GetDrives().Select(x => x.RootDirectory.Root).ToList().OrderBy(x => x.Root.FullName).ToList();
134-
135-
if (!driveLetters.Any()) return;
136-
137129
driveLetters.ForEach(async roots =>
138130
{
139131
try
140132
{
141-
//if (roots.Name == @"C:\") return;
142133
var content = string.Empty;
143-
string icon;
144-
if (knownRemDevices.Contains(roots.Name))
134+
string icon = null;
135+
if (!(await KnownFolders.RemovableDevices.GetFoldersAsync()).Select(x => x.Path).ToList().Contains(roots.Name))
145136
{
146-
content = $"Removable Drive ({roots.Name})";
147-
icon = "\uE88E";
148-
}
149-
else
150-
{
151-
content = $"Local Disk ({roots.Name})";
137+
// TODO: Display Custom Names for Local Disks as well
138+
content = $"Local Disk ({roots.Name.TrimEnd('\\')})";
152139
icon = "\uEDA2";
153-
}
154-
StorageFolder drive = await StorageFolder.GetFolderFromPathAsync(roots.Name);
155-
var retrivedProperties = await drive.Properties.RetrievePropertiesAsync(new string[] { "System.FreeSpace", "System.Capacity" });
156-
157-
ulong totalSpaceProg = 0;
158-
ulong freeSpaceProg = 0;
159-
string free_space_text = "Unknown";
160-
string total_space_text = "Unknown";
161-
Visibility capacityBarVis = Visibility.Visible;
162-
try
163-
{
164-
var sizeAsGBString = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.FreeSpace"]).GigaBytes;
165-
freeSpaceProg = Convert.ToUInt64(sizeAsGBString);
166140

167-
sizeAsGBString = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.Capacity"]).GigaBytes;
168-
totalSpaceProg = Convert.ToUInt64(sizeAsGBString);
169-
170-
171-
free_space_text = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.FreeSpace"]).ToString();
172-
total_space_text = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.Capacity"]).ToString();
173-
}
174-
catch (UnauthorizedAccessException)
175-
{
176-
capacityBarVis = Visibility.Collapsed;
177-
}
178-
catch (NullReferenceException)
179-
{
180-
capacityBarVis = Visibility.Collapsed;
141+
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low,
142+
async () =>
143+
{
144+
StorageFolder drive = await StorageFolder.GetFolderFromPathAsync(roots.Name);
145+
var retrivedProperties = await drive.Properties.RetrievePropertiesAsync(new string[] { "System.FreeSpace", "System.Capacity" });
146+
147+
ulong totalSpaceProg = 0;
148+
ulong freeSpaceProg = 0;
149+
string free_space_text = "Unknown";
150+
string total_space_text = "Unknown";
151+
Visibility capacityBarVis = Visibility.Visible;
152+
try
153+
{
154+
var sizeAsGBString = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.FreeSpace"]).GigaBytes;
155+
freeSpaceProg = Convert.ToUInt64(sizeAsGBString);
156+
157+
sizeAsGBString = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.Capacity"]).GigaBytes;
158+
totalSpaceProg = Convert.ToUInt64(sizeAsGBString);
159+
160+
161+
free_space_text = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.FreeSpace"]).ToString();
162+
total_space_text = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.Capacity"]).ToString();
163+
}
164+
catch (UnauthorizedAccessException)
165+
{
166+
capacityBarVis = Visibility.Collapsed;
167+
}
168+
catch (NullReferenceException)
169+
{
170+
capacityBarVis = Visibility.Collapsed;
171+
}
172+
173+
App.foundDrives.Add(new DriveItem()
174+
{
175+
driveText = content,
176+
glyph = icon,
177+
maxSpace = totalSpaceProg,
178+
spaceUsed = totalSpaceProg - freeSpaceProg,
179+
tag = roots.Name,
180+
progressBarVisibility = capacityBarVis,
181+
spaceText = free_space_text + " free of " + total_space_text,
182+
});
183+
});
181184
}
182185

183-
foundDrives.Add(new DriveItem()
184-
{
185-
driveText = content,
186-
glyph = icon,
187-
maxSpace = totalSpaceProg,
188-
spaceUsed = totalSpaceProg - freeSpaceProg,
189-
tag = roots.Name,
190-
progressBarVisibility = capacityBarVis,
191-
spaceText = free_space_text + " free of " + total_space_text,
192-
});
193186
}
194187
catch (UnauthorizedAccessException e)
195188
{
196189
Debug.WriteLine(e.Message);
197190
}
191+
192+
});
193+
194+
195+
196+
}
197+
198+
private async void Watcher_EnumerationCompleted(DeviceWatcher sender, object args)
199+
{
200+
PopulateDrivesListWithLocalDisks();
201+
DeviceAdded(sender, null);
202+
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low,
203+
() =>
204+
{
205+
App.foundDrives.Add(new DriveItem()
206+
{
207+
driveText = "OneDrive",
208+
tag = "OneDrive",
209+
cloudGlyphVisibility = Visibility.Visible,
210+
driveGlyphVisibility = Visibility.Collapsed
211+
});
198212
});
199213
}
200214

215+
private void DeviceUpdated(DeviceWatcher sender, DeviceInformationUpdate args)
216+
{
217+
Debug.WriteLine("Devices updated");
218+
}
219+
220+
221+
private async void DeviceRemoved(DeviceWatcher sender, DeviceInformationUpdate args)
222+
{
223+
var devices = DriveInfo.GetDrives().Select(x => x.RootDirectory.Root).ToList().OrderBy(x => x.Root.FullName).ToList();
224+
225+
foreach (DriveItem driveItem in foundDrives)
226+
{
227+
if (!driveItem.tag.Equals("OneDrive"))
228+
{
229+
if (!devices.Any(x => x.Name == driveItem.tag) || devices.Equals(null))
230+
{
231+
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low,
232+
() =>
233+
{
234+
foundDrives.Remove(driveItem);
235+
});
236+
return;
237+
238+
}
239+
}
240+
241+
}
242+
}
243+
244+
private async void DeviceAdded(DeviceWatcher sender, DeviceInformation args)
245+
{
246+
try
247+
{
248+
//var device = StorageDevice.FromId(args.Id);
249+
var devices = (await KnownFolders.RemovableDevices.GetFoldersAsync()).OrderBy(x => x.Path);
250+
foreach(StorageFolder device in devices)
251+
{
252+
var letter = device.Path;
253+
if(!foundDrives.Any(x => x.tag == letter))
254+
{
255+
//if (roots.Name == @"C:\") return;
256+
var content = device.DisplayName;
257+
string icon = null;
258+
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low,
259+
async () =>
260+
{
261+
if (content.Contains("DVD"))
262+
{
263+
icon = "\uE958";
264+
}
265+
else
266+
{
267+
icon = "\uE88E";
268+
}
269+
270+
StorageFolder drive = await StorageFolder.GetFolderFromPathAsync(letter);
271+
var retrivedProperties = await drive.Properties.RetrievePropertiesAsync(new string[] { "System.FreeSpace", "System.Capacity" });
272+
273+
ulong totalSpaceProg = 0;
274+
ulong freeSpaceProg = 0;
275+
string free_space_text = "Unknown";
276+
string total_space_text = "Unknown";
277+
Visibility capacityBarVis = Visibility.Visible;
278+
try
279+
{
280+
var sizeAsGBString = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.FreeSpace"]).GigaBytes;
281+
freeSpaceProg = Convert.ToUInt64(sizeAsGBString);
282+
283+
sizeAsGBString = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.Capacity"]).GigaBytes;
284+
totalSpaceProg = Convert.ToUInt64(sizeAsGBString);
285+
286+
287+
free_space_text = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.FreeSpace"]).ToString();
288+
total_space_text = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.Capacity"]).ToString();
289+
}
290+
catch (UnauthorizedAccessException)
291+
{
292+
capacityBarVis = Visibility.Collapsed;
293+
}
294+
catch (NullReferenceException)
295+
{
296+
capacityBarVis = Visibility.Collapsed;
297+
}
298+
299+
if (!foundDrives.Any(x => x.tag == letter))
300+
{
301+
foundDrives.Add(new DriveItem()
302+
{
303+
driveText = content,
304+
glyph = icon,
305+
maxSpace = totalSpaceProg,
306+
spaceUsed = totalSpaceProg - freeSpaceProg,
307+
tag = letter,
308+
progressBarVisibility = capacityBarVis,
309+
spaceText = free_space_text + " free of " + total_space_text,
310+
});
311+
}
312+
});
313+
314+
}
315+
}
316+
}
317+
catch (UnauthorizedAccessException e)
318+
{
319+
Debug.WriteLine(e.Message);
320+
}
321+
}
322+
201323
public static Windows.UI.Xaml.UnhandledExceptionEventArgs exceptionInfo { get; set; }
202324
public static string exceptionStackTrace { get; set; }
203325
public Dialogs.ExceptionDialog exceptionDialog;
@@ -264,6 +386,12 @@ protected override void OnLaunched(LaunchActivatedEventArgs e)
264386

265387

266388
}
389+
watcher = DeviceInformation.CreateWatcher(StorageDevice.GetDeviceSelector());
390+
watcher.Added += DeviceAdded;
391+
watcher.Removed += DeviceRemoved;
392+
watcher.Updated += DeviceUpdated;
393+
watcher.EnumerationCompleted += Watcher_EnumerationCompleted;
394+
watcher.Start();
267395
// Ensure the current window is active
268396
Window.Current.Activate();
269397

@@ -294,6 +422,12 @@ protected override void OnActivated(IActivatedEventArgs args)
294422
rootFrame.Navigate(typeof(InstanceTabsView), @trimmedPath, new SuppressNavigationTransitionInfo());
295423
}
296424
// Ensure the current window is active.
425+
watcher = DeviceInformation.CreateWatcher(StorageDevice.GetDeviceSelector());
426+
watcher.Added += DeviceAdded;
427+
watcher.Removed += DeviceRemoved;
428+
watcher.Updated += DeviceUpdated;
429+
watcher.EnumerationCompleted += Watcher_EnumerationCompleted;
430+
watcher.Start();
297431
Window.Current.Activate();
298432
return;
299433
}
@@ -329,6 +463,7 @@ private void OnSuspending(object sender, SuspendingEventArgs e)
329463
{
330464
var deferral = e.SuspendingOperation.GetDeferral();
331465
//TODO: Save application state and stop any background activity
466+
watcher.Stop();
332467
deferral.Complete();
333468
}
334469
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<ContentDialog
2+
x:Class="Files.Dialogs.ExtractFilesDialog"
3+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:local="using:Files.Dialogs"
6+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
7+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
8+
mc:Ignorable="d"
9+
CornerRadius="4"
10+
Title="Extract Compressed Archive"
11+
PrimaryButtonText="Extract"
12+
CloseButtonText="Cancel"
13+
DefaultButton="Primary"
14+
PrimaryButtonClick="ContentDialog_PrimaryButtonClick"
15+
CloseButtonClick="ContentDialog_CloseButtonClick">
16+
17+
<Grid MinWidth="375">
18+
<StackPanel Orientation="Vertical">
19+
<TextBlock TextWrapping="WrapWholeWords" Text="Pick a location to extract this compressed archive to. You'll need to stay in the current folder until we're done. A new tab will open up with the extracted items."/>
20+
<Grid ColumnSpacing="5" Margin="0,14,0,0" >
21+
<Grid.ColumnDefinitions>
22+
<ColumnDefinition Width="75*"/>
23+
<ColumnDefinition Width="25*"/>
24+
</Grid.ColumnDefinitions>
25+
<TextBox x:Name="DestPathText" Grid.Column="0"/>
26+
<Button x:Name="BrowseButton" Grid.Column="1" Content="Browse" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Click="BrowseButton_Click"/>
27+
</Grid>
28+
</StackPanel>
29+
</Grid>
30+
</ContentDialog>

0 commit comments

Comments
 (0)