|
| 1 | +// Copyright (c) 2024 Files Community |
| 2 | +// Licensed under the MIT License. See the LICENSE. |
| 3 | + |
| 4 | +using System.Security.Principal; |
| 5 | + |
| 6 | +namespace Files.App.Storage.Watchers |
| 7 | +{ |
| 8 | + public class RecycleBinWatcher : ITrashWatcher |
| 9 | + { |
| 10 | + private readonly List<SystemIO.FileSystemWatcher> _watchers = []; |
| 11 | + |
| 12 | + /// <inheritdoc/> |
| 13 | + public event EventHandler<SystemIO.FileSystemEventArgs>? ItemAdded; |
| 14 | + |
| 15 | + /// <inheritdoc/> |
| 16 | + public event EventHandler<SystemIO.FileSystemEventArgs>? ItemDeleted; |
| 17 | + |
| 18 | + /// <inheritdoc/> |
| 19 | + public event EventHandler<SystemIO.FileSystemEventArgs>? ItemChanged; |
| 20 | + |
| 21 | + /// <inheritdoc/> |
| 22 | + public event EventHandler<SystemIO.FileSystemEventArgs>? ItemRenamed; |
| 23 | + |
| 24 | + /// <inheritdoc/> |
| 25 | + public event EventHandler<SystemIO.FileSystemEventArgs>? RefreshRequested; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Initializes an instance of <see cref="RecycleBinWatcher"/> class. |
| 29 | + /// </summary> |
| 30 | + public RecycleBinWatcher() |
| 31 | + { |
| 32 | + StartWatcher(); |
| 33 | + } |
| 34 | + |
| 35 | + /// <inheritdoc/> |
| 36 | + public void StartWatcher() |
| 37 | + { |
| 38 | + // NOTE: |
| 39 | + // SHChangeNotifyRegister only works if recycle bin is open in File Explorer. |
| 40 | + // Create file system watcher to monitor recycle bin folder(s) instead. |
| 41 | + |
| 42 | + // Listen changes only on the Recycle Bin that the current logon user has |
| 43 | + var sid = WindowsIdentity.GetCurrent().User?.ToString() ?? string.Empty; |
| 44 | + if (string.IsNullOrEmpty(sid)) |
| 45 | + return; |
| 46 | + |
| 47 | + // TODO: Use IStorageDevicesService to enumerate drives |
| 48 | + foreach (var drive in SystemIO.DriveInfo.GetDrives()) |
| 49 | + { |
| 50 | + var recyclePath = SystemIO.Path.Combine(drive.Name, "$RECYCLE.BIN", sid); |
| 51 | + |
| 52 | + if (drive.DriveType is SystemIO.DriveType.Network || |
| 53 | + !SystemIO.Directory.Exists(recyclePath)) |
| 54 | + continue; |
| 55 | + |
| 56 | + SystemIO.FileSystemWatcher watcher = new() |
| 57 | + { |
| 58 | + Path = recyclePath, |
| 59 | + Filter = "*.*", |
| 60 | + NotifyFilter = SystemIO.NotifyFilters.LastWrite | SystemIO.NotifyFilters.FileName | SystemIO.NotifyFilters.DirectoryName |
| 61 | + }; |
| 62 | + |
| 63 | + watcher.Created += Watcher_Changed; |
| 64 | + watcher.Deleted += Watcher_Changed; |
| 65 | + watcher.EnableRaisingEvents = true; |
| 66 | + |
| 67 | + _watchers.Add(watcher); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /// <inheritdoc/> |
| 72 | + public void StopWatcher() |
| 73 | + { |
| 74 | + foreach (var watcher in _watchers) |
| 75 | + watcher.Dispose(); |
| 76 | + } |
| 77 | + |
| 78 | + private void Watcher_Changed(object sender, SystemIO.FileSystemEventArgs e) |
| 79 | + { |
| 80 | + // Don't listen changes on files starting with '$I' |
| 81 | + if (string.IsNullOrEmpty(e.Name) || |
| 82 | + e.Name.StartsWith("$I", StringComparison.Ordinal)) |
| 83 | + return; |
| 84 | + |
| 85 | + switch (e.ChangeType) |
| 86 | + { |
| 87 | + case SystemIO.WatcherChangeTypes.Created: |
| 88 | + ItemAdded?.Invoke(this, e); |
| 89 | + break; |
| 90 | + case SystemIO.WatcherChangeTypes.Deleted: |
| 91 | + ItemDeleted?.Invoke(this, e); |
| 92 | + break; |
| 93 | + case SystemIO.WatcherChangeTypes.Renamed: |
| 94 | + ItemRenamed?.Invoke(this, e); |
| 95 | + break; |
| 96 | + default: |
| 97 | + RefreshRequested?.Invoke(this, e); |
| 98 | + break; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /// <inheritdoc/> |
| 103 | + public void Dispose() |
| 104 | + { |
| 105 | + StopWatcher(); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments