public async Task ReloadUnmountedDrivesAsync()
        {
            var unmountedDrives = await GetUnmountedDrivesAsync();

            var oldRoots = _unmountedDrives.Select(d => d.FullName).ToHashSet();
            var newRoots = unmountedDrives.Select(d => d.FullName).ToHashSet();

            var addedDrives = unmountedDrives
                              .Where(udm => !oldRoots.Contains(udm.FullName))
                              .ToArray();
            var removedDrives = UnmountedDrives
                                .Where(udm => !newRoots.Contains(udm.FullName))
                                .ToArray();

            foreach (var unmountedDriveModel in addedDrives)
            {
                _unmountedDrives.Add(unmountedDriveModel);

                DriveAdded.Raise(this, CreateFrom(unmountedDriveModel));
            }

            foreach (var unmountedDriveModel in removedDrives)
            {
                _unmountedDrives.Remove(unmountedDriveModel);

                DriveRemoved.Raise(this, CreateFrom(unmountedDriveModel));
            }
        }
Example #2
0
        public void ReloadMountedDrives()
        {
            var oldRoots = _mountedDrives.Select(d => d.RootDirectory).ToHashSet();

            var drives   = GetMountedDrives();
            var newRoots = drives.Select(d => d.RootDirectory).ToHashSet();

            var addedDrives = drives
                              .Where(dm => !oldRoots.Contains(dm.RootDirectory))
                              .ToArray();
            var removedDrives = MountedDrives
                                .Where(dm => !newRoots.Contains(dm.RootDirectory))
                                .ToArray();

            foreach (var driveModel in addedDrives)
            {
                _mountedDrives.Add(driveModel);

                DriveAdded.Raise(this, CreateFrom(driveModel));
            }

            foreach (var driveModel in removedDrives)
            {
                _mountedDrives.Remove(driveModel);

                DriveRemoved.Raise(this, CreateFrom(driveModel));
            }

            // TODO: updated drives
        }
Example #3
0
        private async static void HardDeviceList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, async() =>
            {
                switch (e.Action)
                {
                case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
                    {
                        if (DriveAdded != null)
                        {
                            foreach (DriveDataBase Drive in e.NewItems)
                            {
                                await DriveAdded.InvokeAsync(null, await DriveChangeDeferredEventArgs.CreateAsync(Drive));
                            }
                        }
                        break;
                    }

                case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
                    {
                        if (DriveRemoved != null)
                        {
                            foreach (DriveDataBase Drive in e.OldItems)
                            {
                                await DriveRemoved.InvokeAsync(null, await DriveChangeDeferredEventArgs.CreateAsync(Drive));
                            }
                        }

                        break;
                    }
                }
            });
        }
Example #4
0
        public void ReloadMountedDrives()
        {
            var oldRoots = _mountedDrives.Select(d => d.RootDirectory).ToHashSet();

            var drives   = GetMountedDrives();
            var newRoots = drives.Select(d => d.RootDirectory).ToHashSet();

            var addedDrives = drives
                              .Where(dm => !oldRoots.Contains(dm.RootDirectory))
                              .ToArray();
            var removedDrives = MountedDrives
                                .Where(dm => !newRoots.Contains(dm.RootDirectory))
                                .ToArray();
            var updatedDrives = _mountedDrives.Except(removedDrives).ToArray();

            foreach (var driveModel in updatedDrives)
            {
                var newDriveModel = drives.Single(d => d.RootDirectory == driveModel.RootDirectory);
                if (!CheckIfDiffer(driveModel, newDriveModel))
                {
                    continue;
                }

                Update(driveModel, newDriveModel);

                DriveUpdated.Raise(this, CreateFrom(driveModel));
            }

            foreach (var driveModel in addedDrives)
            {
                _mountedDrives.Add(driveModel);

                DriveAdded.Raise(this, CreateFrom(driveModel));
            }

            foreach (var driveModel in removedDrives)
            {
                _mountedDrives.Remove(driveModel);

                DriveRemoved.Raise(this, CreateFrom(driveModel));
            }
        }
Example #5
0
        private void VolumeChangeHandler(object sender, EventArrivedEventArgs e)
        {
            var uuid = e.NewEvent.GetQualifierValue("UUID").ToString();

            var eventType = e.NewEvent.Properties["EventType"].Value.ToString();

            if (eventType == "3") //"Remove" event
            {
                if (AllDrives.TryGetValue(uuid, out var removedDrive) && !removedDrive.Monitored && AllDrives.TryRemove(uuid, out _))
                {
                    DriveRemoved?.Invoke(this, new DriveConnectionEventArgs(removedDrive));
                }
                return;
            }

            var volume      = e.NewEvent.GetPropertyValue("DriveName").ToString();
            var volumeLabel = new DriveInfo(volume.Substring(0, 1)).VolumeLabel;

            Debug.WriteLine($"Drive connected. UUID={uuid}, volume={volume}, volumeLabel={volumeLabel}");
            var drive = AllDrives.GetOrAdd(
                uuid,
                key => new UsbDrive(key)
            {
                DriveLetter = volume,
                VolumeName  = volumeLabel
            }
                );

            drive.DriveLetter = volume;
            drive.VolumeName  = volumeLabel;
            if (drive.Monitored)
            {
                drive.ExecuteCommand();
            }
            DriveArrived?.Invoke(this, new DriveConnectionEventArgs(drive));
        }