|
| 1 | +// Copyright(c) 2023 Files Community |
| 2 | +// Licensed under the MIT License. See the LICENSE. |
| 3 | + |
| 4 | +using Files.App.Filesystem.StorageItems; |
| 5 | +using Microsoft.UI.Dispatching; |
| 6 | + |
| 7 | +namespace Files.App.ViewModels.Properties |
| 8 | +{ |
| 9 | + internal class CombinedFileProperties : CombinedProperties, IFileProperties |
| 10 | + { |
| 11 | + public CombinedFileProperties( |
| 12 | + SelectedItemsPropertiesViewModel viewModel, |
| 13 | + CancellationTokenSource tokenSource, |
| 14 | + DispatcherQueue coreDispatcher, |
| 15 | + List<ListedItem> listedItems, |
| 16 | + IShellPage instance) |
| 17 | + : base(viewModel, tokenSource, coreDispatcher, listedItems, instance) { } |
| 18 | + |
| 19 | + public async Task GetSystemFilePropertiesAsync() |
| 20 | + { |
| 21 | + var queries = await Task.WhenAll(List.AsParallel().Select(async item => { |
| 22 | + BaseStorageFile file = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFileFromPathAsync(item.ItemPath)); |
| 23 | + if (file is null) |
| 24 | + { |
| 25 | + // Could not access file, can't show any other property |
| 26 | + return null; |
| 27 | + } |
| 28 | + |
| 29 | + var list = await FileProperty.RetrieveAndInitializePropertiesAsync(file); |
| 30 | + |
| 31 | + list.Find(x => x.ID == "address").Value = |
| 32 | + await LocationHelpers.GetAddressFromCoordinatesAsync((double?)list.Find( |
| 33 | + x => x.Property == "System.GPS.LatitudeDecimal").Value, |
| 34 | + (double?)list.Find(x => x.Property == "System.GPS.LongitudeDecimal").Value); |
| 35 | + |
| 36 | + // Find Encoding Bitrate property and convert it to kbps |
| 37 | + var encodingBitrate = list.Find(x => x.Property == "System.Audio.EncodingBitrate"); |
| 38 | + if (encodingBitrate?.Value is not null) |
| 39 | + { |
| 40 | + var sizes = new string[] { "Bps", "KBps", "MBps", "GBps" }; |
| 41 | + var order = (int)Math.Floor(Math.Log((uint)encodingBitrate.Value, 1024)); |
| 42 | + var readableSpeed = (uint)encodingBitrate.Value / Math.Pow(1024, order); |
| 43 | + encodingBitrate.Value = $"{readableSpeed:0.##} {sizes[order]}"; |
| 44 | + } |
| 45 | + |
| 46 | + return list |
| 47 | + .Where(fileProp => !(fileProp.Value is null && fileProp.IsReadOnly)) |
| 48 | + .GroupBy(fileProp => fileProp.SectionResource) |
| 49 | + .Select(group => new FilePropertySection(group) { Key = group.Key }) |
| 50 | + .Where(section => !section.All(fileProp => fileProp.Value is null)); |
| 51 | + })); |
| 52 | + |
| 53 | + if (queries.Any(query => query is null)) |
| 54 | + return; |
| 55 | + |
| 56 | + // Display only the sections that all files have |
| 57 | + var keys = queries.Select(query => query!.Select(section => section.Key)).Aggregate((x, y) => x.Intersect(y)); |
| 58 | + var sections = queries[0]!.Where(section => keys.Contains(section.Key)).OrderBy(group => group.Priority).ToArray(); |
| 59 | + |
| 60 | + foreach (var group in sections) |
| 61 | + { |
| 62 | + var props = queries.SelectMany(query => query!.First(section => section.Key == group.Key)); |
| 63 | + foreach (FileProperty prop in group) |
| 64 | + { |
| 65 | + if (props.Where(x => x.Property == prop.Property).Any(x => !Equals(x.Value, prop.Value))) |
| 66 | + { |
| 67 | + // Has multiple values |
| 68 | + prop.Value = null; |
| 69 | + prop.PlaceholderText = "MultipleValues".GetLocalizedResource(); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + ViewModel.PropertySections = new ObservableCollection<FilePropertySection>(sections); |
| 75 | + } |
| 76 | + |
| 77 | + public async Task SyncPropertyChangesAsync() |
| 78 | + { |
| 79 | + var files = new List<BaseStorageFile>(); |
| 80 | + foreach (var item in List) |
| 81 | + { |
| 82 | + BaseStorageFile file = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFileFromPathAsync(item.ItemPath)); |
| 83 | + |
| 84 | + // Couldn't access the file to save properties |
| 85 | + if (file is null) |
| 86 | + return; |
| 87 | + |
| 88 | + files.Add(file); |
| 89 | + } |
| 90 | + |
| 91 | + var failedProperties = ""; |
| 92 | + |
| 93 | + foreach (var group in ViewModel.PropertySections) |
| 94 | + { |
| 95 | + foreach (FileProperty prop in group) |
| 96 | + { |
| 97 | + if (!prop.IsReadOnly && prop.Modified) |
| 98 | + { |
| 99 | + var newDict = new Dictionary<string, object>(); |
| 100 | + newDict.Add(prop.Property, prop.Value); |
| 101 | + |
| 102 | + foreach (var file in files) |
| 103 | + { |
| 104 | + try |
| 105 | + { |
| 106 | + if (file.Properties is not null) |
| 107 | + { |
| 108 | + await file.Properties.SavePropertiesAsync(newDict); |
| 109 | + } |
| 110 | + } |
| 111 | + catch |
| 112 | + { |
| 113 | + failedProperties += $"{file.Name}: {prop.Name}\n"; |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + if (!string.IsNullOrWhiteSpace(failedProperties)) |
| 121 | + { |
| 122 | + throw new Exception($"The following properties failed to save: {failedProperties}"); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + public async Task ClearPropertiesAsync() |
| 127 | + { |
| 128 | + var failedProperties = new List<string>(); |
| 129 | + var files = new List<BaseStorageFile>(); |
| 130 | + foreach (var item in List) |
| 131 | + { |
| 132 | + BaseStorageFile file = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFileFromPathAsync(item.ItemPath)); |
| 133 | + |
| 134 | + if (file is null) |
| 135 | + return; |
| 136 | + |
| 137 | + files.Add(file); |
| 138 | + } |
| 139 | + |
| 140 | + foreach (var group in ViewModel.PropertySections) |
| 141 | + { |
| 142 | + foreach (FileProperty prop in group) |
| 143 | + { |
| 144 | + if (!prop.IsReadOnly) |
| 145 | + { |
| 146 | + var newDict = new Dictionary<string, object>(); |
| 147 | + newDict.Add(prop.Property, null); |
| 148 | + |
| 149 | + foreach (var file in files) |
| 150 | + { |
| 151 | + try |
| 152 | + { |
| 153 | + if (file.Properties is not null) |
| 154 | + { |
| 155 | + await file.Properties.SavePropertiesAsync(newDict); |
| 156 | + } |
| 157 | + } |
| 158 | + catch |
| 159 | + { |
| 160 | + failedProperties.Add(prop.Name); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + _ = GetSystemFilePropertiesAsync(); |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments