|
| 1 | +// Copyright (c) 2024 Files Community |
| 2 | +// Licensed under the MIT License. See the LICENSE. |
| 3 | + |
| 4 | +using Microsoft.Management.Infrastructure; |
| 5 | +using Windows.Devices.Enumeration; |
| 6 | +using Windows.Devices.Portable; |
| 7 | + |
| 8 | +namespace Files.App.Storage.Watchers |
| 9 | +{ |
| 10 | + /// <inheritdoc cref="IDeviceWatcher"/> |
| 11 | + public class DeviceWatcher : IDeviceWatcher |
| 12 | + { |
| 13 | + private Windows.Devices.Enumeration.DeviceWatcher? _winDeviceWatcher; |
| 14 | + |
| 15 | + private ManagementEventWatcher? _mmiInsertWatcher, _mmiRemoveWatcher, _mmiModifyWatcher; |
| 16 | + |
| 17 | + /// <inheritdoc/> |
| 18 | + public bool CanBeStarted |
| 19 | + => _winDeviceWatcher?.Status is DeviceWatcherStatus.Created or DeviceWatcherStatus.Stopped or DeviceWatcherStatus.Aborted; |
| 20 | + |
| 21 | + /// <inheritdoc/> |
| 22 | + public event EventHandler<DeviceEventArgs>? DeviceAdded; |
| 23 | + |
| 24 | + /// <inheritdoc/> |
| 25 | + public event EventHandler<DeviceEventArgs>? DeviceChanged; |
| 26 | + |
| 27 | + /// <inheritdoc/> |
| 28 | + public event EventHandler<DeviceEventArgs>? DeviceDeleted; |
| 29 | + |
| 30 | + /// <inheritdoc/> |
| 31 | + public event EventHandler<DeviceEventArgs>? DeviceInserted; |
| 32 | + |
| 33 | + /// <inheritdoc/> |
| 34 | + public event EventHandler<DeviceEventArgs>? DeviceEjected; |
| 35 | + |
| 36 | + /// <inheritdoc/> |
| 37 | + public event EventHandler? EnumerationCompleted; |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Initializes an instance of <see cref="DeviceWatcher"/> class. |
| 41 | + /// </summary> |
| 42 | + public DeviceWatcher() |
| 43 | + { |
| 44 | + StartWatcher(); |
| 45 | + } |
| 46 | + |
| 47 | + /// <inheritdoc/> |
| 48 | + public void StartWatcher() |
| 49 | + { |
| 50 | + _winDeviceWatcher = DeviceInformation.CreateWatcher(StorageDevice.GetDeviceSelector()); |
| 51 | + _winDeviceWatcher.Added += Watcher_Added; |
| 52 | + _winDeviceWatcher.Removed += Watcher_Removed; |
| 53 | + _winDeviceWatcher.EnumerationCompleted += Watcher_EnumerationCompleted; |
| 54 | + |
| 55 | + var insertQuery = "SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_LogicalDisk'"; |
| 56 | + _mmiInsertWatcher = new(insertQuery); |
| 57 | + _mmiInsertWatcher.EventArrived += new EventArrivedEventHandler(Device_Inserted); |
| 58 | + _mmiInsertWatcher.Start(); |
| 59 | + |
| 60 | + var modifyQuery = "SELECT * FROM __InstanceModificationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_LogicalDisk' and TargetInstance.DriveType = 5"; |
| 61 | + _mmiModifyWatcher = new(modifyQuery); |
| 62 | + _mmiModifyWatcher.EventArrived += new EventArrivedEventHandler(Device_Modified); |
| 63 | + _mmiModifyWatcher.Start(); |
| 64 | + |
| 65 | + var removeQuery = "SELECT * FROM __InstanceDeletionEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_LogicalDisk'"; |
| 66 | + _mmiRemoveWatcher = new(removeQuery); |
| 67 | + _mmiRemoveWatcher.EventArrived += new EventArrivedEventHandler(Device_Removed); |
| 68 | + _mmiRemoveWatcher.Start(); |
| 69 | + } |
| 70 | + |
| 71 | + /// <inheritdoc/> |
| 72 | + public void StopWatcher() |
| 73 | + { |
| 74 | + if (_winDeviceWatcher?.Status is DeviceWatcherStatus.Started or DeviceWatcherStatus.EnumerationCompleted) |
| 75 | + _winDeviceWatcher.Stop(); |
| 76 | + |
| 77 | + if (_winDeviceWatcher is not null) |
| 78 | + { |
| 79 | + _winDeviceWatcher.Added -= Watcher_Added; |
| 80 | + _winDeviceWatcher.Removed -= Watcher_Removed; |
| 81 | + _winDeviceWatcher.EnumerationCompleted -= Watcher_EnumerationCompleted; |
| 82 | + } |
| 83 | + |
| 84 | + _mmiInsertWatcher?.Dispose(); |
| 85 | + _mmiRemoveWatcher?.Dispose(); |
| 86 | + _mmiModifyWatcher?.Dispose(); |
| 87 | + _mmiInsertWatcher = null; |
| 88 | + _mmiRemoveWatcher = null; |
| 89 | + _mmiModifyWatcher = null; |
| 90 | + } |
| 91 | + |
| 92 | + private void Watcher_Added(Windows.Devices.Enumeration.DeviceWatcher sender, DeviceInformation args) |
| 93 | + { |
| 94 | + DeviceAdded?.Invoke(this, new(args.Name, args.Id)); |
| 95 | + } |
| 96 | + |
| 97 | + private void Watcher_Removed(Windows.Devices.Enumeration.DeviceWatcher sender, DeviceInformationUpdate args) |
| 98 | + { |
| 99 | + DeviceDeleted?.Invoke(this, new(string.Empty, args.Id)); |
| 100 | + } |
| 101 | + |
| 102 | + private void Watcher_EnumerationCompleted(Windows.Devices.Enumeration.DeviceWatcher sender, object args) |
| 103 | + { |
| 104 | + EnumerationCompleted?.Invoke(this, EventArgs.Empty); |
| 105 | + } |
| 106 | + |
| 107 | + private void Device_Modified(object sender, EventArrivedEventArgs e) |
| 108 | + { |
| 109 | + CimInstance obj = (CimInstance)e.NewEvent.Instance.CimInstanceProperties["TargetInstance"].Value; |
| 110 | + var deviceName = obj.CimInstanceProperties["Name"]?.Value.ToString(); |
| 111 | + var deviceId = obj.CimInstanceProperties["DeviceID"]?.Value.ToString(); |
| 112 | + var volumeName = obj.CimInstanceProperties["VolumeName"]?.Value.ToString(); |
| 113 | + var eventType = volumeName is not null ? DeviceEvent.Inserted : DeviceEvent.Ejected; |
| 114 | + |
| 115 | + Debug.WriteLine($"Drive modify event: {deviceName}, {deviceId}, {eventType}"); |
| 116 | + |
| 117 | + if (eventType is DeviceEvent.Inserted) |
| 118 | + DeviceInserted?.Invoke(sender, new DeviceEventArgs(deviceName ?? string.Empty, deviceId ?? string.Empty)); |
| 119 | + else |
| 120 | + DeviceEjected?.Invoke(sender, new DeviceEventArgs(deviceName ?? string.Empty, deviceId ?? string.Empty)); |
| 121 | + } |
| 122 | + |
| 123 | + private void Device_Removed(object sender, EventArrivedEventArgs e) |
| 124 | + { |
| 125 | + CimInstance obj = (CimInstance)e.NewEvent.Instance.CimInstanceProperties["TargetInstance"].Value; |
| 126 | + var deviceName = (string)obj.CimInstanceProperties["Name"].Value; |
| 127 | + var deviceId = (string)obj.CimInstanceProperties["DeviceID"].Value; |
| 128 | + |
| 129 | + Debug.WriteLine($"Drive removed event: {deviceName}, {deviceId}"); |
| 130 | + |
| 131 | + DeviceDeleted?.Invoke(sender, new DeviceEventArgs(deviceName, deviceId)); |
| 132 | + } |
| 133 | + |
| 134 | + private void Device_Inserted(object sender, EventArrivedEventArgs e) |
| 135 | + { |
| 136 | + CimInstance obj = (CimInstance)e.NewEvent.Instance.CimInstanceProperties["TargetInstance"].Value; |
| 137 | + var deviceName = (string)obj.CimInstanceProperties["Name"].Value; |
| 138 | + var deviceId = (string)obj.CimInstanceProperties["DeviceID"].Value; |
| 139 | + |
| 140 | + Debug.WriteLine($"Drive added event: {deviceName}, {deviceId}"); |
| 141 | + |
| 142 | + DeviceAdded?.Invoke(sender, new DeviceEventArgs(deviceName, deviceId)); |
| 143 | + } |
| 144 | + |
| 145 | + /// <inheritdoc/> |
| 146 | + public void Dispose() |
| 147 | + { |
| 148 | + StopWatcher(); |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments