Skip to content

Commit c69e4e2

Browse files
authored
Fix issues with drag&drop from special locations (#9435)
1 parent 446d7a9 commit c69e4e2

File tree

8 files changed

+47
-53
lines changed

8 files changed

+47
-53
lines changed

src/Files.FullTrust/Win32API.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ public static string GenerateUniquePath(string path)
505505

506506
for (ushort count = 1; File.Exists(uniquePath); count++)
507507
{
508-
if (countMatch != null)
508+
if (countMatch.Success)
509509
{
510510
uniquePath = Path.Combine(directory, $"{nameWithoutExt[..countMatch.Index]}({count}){extension}");
511511
}
@@ -523,7 +523,7 @@ public static string GenerateUniquePath(string path)
523523

524524
for (ushort Count = 1; Directory.Exists(uniquePath); Count++)
525525
{
526-
if (countMatch != null)
526+
if (countMatch.Success)
527527
{
528528
uniquePath = Path.Combine(directory, $"{Name[..countMatch.Index]}({Count})");
529529
}

src/Files.Shared/ShellOperationResult.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Linq;
23

34
namespace Files.Shared
45
{
@@ -9,8 +10,17 @@ public ShellOperationResult()
910
Items = new List<ShellOperationItemResult>();
1011
}
1112

13+
/// <summary>
14+
/// File operation results: success and error code. Can contains multiple results for the same source file.
15+
/// E.g. if the shell shows a "replace" confirmation dialog, results can be both COPYENGINE_S_PENDING and COPYENGINE_S_USER_IGNORED.
16+
/// </summary>
1217
public List<ShellOperationItemResult> Items { get; set; }
13-
public bool Succeeded { get; set; }
18+
19+
/// <summary>
20+
/// Final results of a file operation. Contains last status for each source file.
21+
/// </summary>
22+
public List<ShellOperationItemResult> Final =>
23+
Items.GroupBy(x => new { Src = x.Source, Dst = x.Destination }).Select(x => x.Last()).ToList();
1424
}
1525

1626
public class ShellOperationItemResult

src/Files.Uwp/Filesystem/FilesystemOperations/FilesystemOperations.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ await DialogDisplayHelper.ShowDialogAsync(
195195
else
196196
{
197197
// CopyFileFromApp only works on file not directories
198-
var fsSourceFolder = await source.ToStorageItemResult(associatedInstance);
198+
var fsSourceFolder = await source.ToStorageItemResult();
199199
var fsDestinationFolder = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination));
200200
var fsResult = (FilesystemResult)(fsSourceFolder.ErrorCode | fsDestinationFolder.ErrorCode);
201201

@@ -241,7 +241,7 @@ await DialogDisplayHelper.ShowDialogAsync(
241241
Debug.WriteLine(System.Runtime.InteropServices.Marshal.GetLastWin32Error());
242242

243243
FilesystemResult<BaseStorageFolder> destinationResult = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination));
244-
var sourceResult = await source.ToStorageItemResult(associatedInstance);
244+
var sourceResult = await source.ToStorageItemResult();
245245
fsResult = sourceResult.ErrorCode | destinationResult.ErrorCode;
246246

247247
if (fsResult)
@@ -396,7 +396,7 @@ await DialogDisplayHelper.ShowDialogAsync(
396396
{
397397
Debug.WriteLine(System.Runtime.InteropServices.Marshal.GetLastWin32Error());
398398

399-
var fsSourceFolder = await source.ToStorageItemResult(associatedInstance);
399+
var fsSourceFolder = await source.ToStorageItemResult();
400400
var fsDestinationFolder = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination));
401401
fsResult = fsSourceFolder.ErrorCode | fsDestinationFolder.ErrorCode;
402402

@@ -445,7 +445,7 @@ await DialogDisplayHelper.ShowDialogAsync(
445445
Debug.WriteLine(System.Runtime.InteropServices.Marshal.GetLastWin32Error());
446446

447447
FilesystemResult<BaseStorageFolder> destinationResult = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination));
448-
var sourceResult = await source.ToStorageItemResult(associatedInstance);
448+
var sourceResult = await source.ToStorageItemResult();
449449
fsResult = sourceResult.ErrorCode | destinationResult.ErrorCode;
450450

451451
if (fsResult)
@@ -611,7 +611,7 @@ public async Task<IStorageHistory> RenameAsync(IStorageItemWithPath source,
611611
&& !FilesystemHelpers.ContainsRestrictedCharacters(newName)
612612
&& !FilesystemHelpers.ContainsRestrictedFileName(newName))
613613
{
614-
var renamed = await source.ToStorageItemResult(associatedInstance)
614+
var renamed = await source.ToStorageItemResult()
615615
.OnSuccess(async (t) =>
616616
{
617617
if (t.Name.Equals(newName, StringComparison.CurrentCultureIgnoreCase))

src/Files.Uwp/Filesystem/FilesystemOperations/ShellFilesystemOperations.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public async Task<IStorageHistory> CopyItemsAsync(IList<IStorageItemWithPath> so
109109
result &= (FilesystemResult)(status == AppServiceResponseStatus.Success
110110
&& response.Get("Success", false));
111111
var shellOpResult = JsonConvert.DeserializeObject<ShellOperationResult>(response.Get("Result", ""));
112-
copyResult.Items.AddRange(shellOpResult?.Items ?? Enumerable.Empty<ShellOperationItemResult>());
112+
copyResult.Items.AddRange(shellOpResult?.Final ?? Enumerable.Empty<ShellOperationItemResult>());
113113
}
114114
if (sourceReplace.Any())
115115
{
@@ -126,7 +126,7 @@ public async Task<IStorageHistory> CopyItemsAsync(IList<IStorageItemWithPath> so
126126
result &= (FilesystemResult)(status == AppServiceResponseStatus.Success
127127
&& response.Get("Success", false));
128128
var shellOpResult = JsonConvert.DeserializeObject<ShellOperationResult>(response.Get("Result", ""));
129-
copyResult.Items.AddRange(shellOpResult?.Items ?? Enumerable.Empty<ShellOperationItemResult>());
129+
copyResult.Items.AddRange(shellOpResult?.Final ?? Enumerable.Empty<ShellOperationItemResult>());
130130
}
131131

132132
if (connection != null)
@@ -260,7 +260,7 @@ await sourceMatch.Select(x => x.dest).ToListAsync(),
260260
var result = (FilesystemResult)(status == AppServiceResponseStatus.Success
261261
&& response.Get("Success", false));
262262
var shellOpResult = JsonConvert.DeserializeObject<ShellOperationResult>(response.Get("Result", ""));
263-
createResult.Items.AddRange(shellOpResult?.Items ?? Enumerable.Empty<ShellOperationItemResult>());
263+
createResult.Items.AddRange(shellOpResult?.Final ?? Enumerable.Empty<ShellOperationItemResult>());
264264

265265
result &= (FilesystemResult)createResult.Items.All(x => x.Succeeded);
266266

@@ -271,7 +271,7 @@ await sourceMatch.Select(x => x.dest).ToListAsync(),
271271
if (createdSources.Any())
272272
{
273273
var item = StorageHelpers.FromPathAndType(createdSources.Single().Destination, source.ItemType);
274-
var storageItem = await item.ToStorageItem(associatedInstance);
274+
var storageItem = await item.ToStorageItem();
275275
return (new StorageHistory(FileOperationType.CreateNew, item.CreateList(), null), storageItem);
276276
}
277277
return (null, null);
@@ -396,7 +396,7 @@ public async Task<IStorageHistory> DeleteItemsAsync(IList<IStorageItemWithPath>
396396
var result = (FilesystemResult)(status == AppServiceResponseStatus.Success
397397
&& response.Get("Success", false));
398398
var shellOpResult = JsonConvert.DeserializeObject<ShellOperationResult>(response.Get("Result", ""));
399-
deleteResult.Items.AddRange(shellOpResult?.Items ?? Enumerable.Empty<ShellOperationItemResult>());
399+
deleteResult.Items.AddRange(shellOpResult?.Final ?? Enumerable.Empty<ShellOperationItemResult>());
400400

401401
if (connection != null)
402402
{
@@ -522,7 +522,7 @@ public async Task<IStorageHistory> MoveItemsAsync(IList<IStorageItemWithPath> so
522522
result &= (FilesystemResult)(status == AppServiceResponseStatus.Success
523523
&& response.Get("Success", false));
524524
var shellOpResult = JsonConvert.DeserializeObject<ShellOperationResult>(response.Get("Result", ""));
525-
moveResult.Items.AddRange(shellOpResult?.Items ?? Enumerable.Empty<ShellOperationItemResult>());
525+
moveResult.Items.AddRange(shellOpResult?.Final ?? Enumerable.Empty<ShellOperationItemResult>());
526526
}
527527
if (sourceReplace.Any())
528528
{
@@ -539,7 +539,7 @@ public async Task<IStorageHistory> MoveItemsAsync(IList<IStorageItemWithPath> so
539539
result &= (FilesystemResult)(status == AppServiceResponseStatus.Success
540540
&& response.Get("Success", false));
541541
var shellOpResult = JsonConvert.DeserializeObject<ShellOperationResult>(response.Get("Result", ""));
542-
moveResult.Items.AddRange(shellOpResult?.Items ?? Enumerable.Empty<ShellOperationItemResult>());
542+
moveResult.Items.AddRange(shellOpResult?.Final ?? Enumerable.Empty<ShellOperationItemResult>());
543543
}
544544

545545
if (connection != null)
@@ -647,7 +647,7 @@ public async Task<IStorageHistory> RenameAsync(IStorageItemWithPath source, stri
647647
var result = (FilesystemResult)(status == AppServiceResponseStatus.Success
648648
&& response.Get("Success", false));
649649
var shellOpResult = JsonConvert.DeserializeObject<ShellOperationResult>(response.Get("Result", ""));
650-
renameResult.Items.AddRange(shellOpResult?.Items ?? Enumerable.Empty<ShellOperationItemResult>());
650+
renameResult.Items.AddRange(shellOpResult?.Final ?? Enumerable.Empty<ShellOperationItemResult>());
651651

652652
result &= (FilesystemResult)renameResult.Items.All(x => x.Succeeded);
653653

@@ -745,7 +745,7 @@ public async Task<IStorageHistory> RestoreItemsFromTrashAsync(IList<IStorageItem
745745
var result = (FilesystemResult)(status == AppServiceResponseStatus.Success
746746
&& response.Get("Success", false));
747747
var shellOpResult = JsonConvert.DeserializeObject<ShellOperationResult>(response.Get("Result", ""));
748-
moveResult.Items.AddRange(shellOpResult?.Items ?? Enumerable.Empty<ShellOperationItemResult>());
748+
moveResult.Items.AddRange(shellOpResult?.Final ?? Enumerable.Empty<ShellOperationItemResult>());
749749

750750
if (connection != null)
751751
{

src/Files.Uwp/Helpers/StorageHelpers.cs

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ namespace Files.Uwp.Helpers
1515
/// </summary>
1616
public static class StorageHelpers
1717
{
18-
public static async Task<IStorageItem> ToStorageItem(this IStorageItemWithPath item, IShellPage associatedInstance = null)
18+
public static async Task<IStorageItem> ToStorageItem(this IStorageItemWithPath item)
1919
{
20-
return (await item.ToStorageItemResult(associatedInstance)).Result;
20+
return (await item.ToStorageItemResult()).Result;
2121
}
2222

23-
public static async Task<TRequested> ToStorageItem<TRequested>(string path, IShellPage associatedInstance = null) where TRequested : IStorageItem
23+
public static async Task<TRequested> ToStorageItem<TRequested>(string path) where TRequested : IStorageItem
2424
{
2525
FilesystemResult<BaseStorageFile> file = null;
2626
FilesystemResult<BaseStorageFolder> folder = null;
@@ -110,28 +110,14 @@ public static async Task<TRequested> ToStorageItem<TRequested>(string path, IShe
110110

111111
async Task GetFile()
112112
{
113-
if (associatedInstance == null || associatedInstance.FilesystemViewModel == null)
114-
{
115-
var rootItem = await FilesystemTasks.Wrap(() => DrivesManager.GetRootFromPathAsync(path));
116-
file = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFileFromPathAsync(path, rootItem));
117-
}
118-
else
119-
{
120-
file = await associatedInstance.FilesystemViewModel.GetFileFromPathAsync(path);
121-
}
113+
var rootItem = await FilesystemTasks.Wrap(() => DrivesManager.GetRootFromPathAsync(path));
114+
file = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFileFromPathAsync(path, rootItem));
122115
}
123116

124117
async Task GetFolder()
125118
{
126-
if (associatedInstance == null)
127-
{
128-
var rootItem = await FilesystemTasks.Wrap(() => DrivesManager.GetRootFromPathAsync(path));
129-
folder = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFolderFromPathAsync(path, rootItem));
130-
}
131-
else
132-
{
133-
folder = await associatedInstance?.FilesystemViewModel?.GetFolderFromPathAsync(path);
134-
}
119+
var rootItem = await FilesystemTasks.Wrap(() => DrivesManager.GetRootFromPathAsync(path));
120+
folder = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFolderFromPathAsync(path, rootItem));
135121
}
136122
}
137123

@@ -141,18 +127,16 @@ public static async Task<long> GetFileSize(this IStorageFile file)
141127
return (long)properties.Size;
142128
}
143129

144-
public static async Task<FilesystemResult<IStorageItem>> ToStorageItemResult(this IStorageItemWithPath item, IShellPage associatedInstance = null)
130+
public static async Task<FilesystemResult<IStorageItem>> ToStorageItemResult(this IStorageItemWithPath item)
145131
{
146132
var returnedItem = new FilesystemResult<IStorageItem>(null, FileSystemStatusCode.Generic);
147-
var rootItem = associatedInstance == null ? await FilesystemTasks.Wrap(() => DrivesManager.GetRootFromPathAsync(item.Path)) : null;
133+
var rootItem = await FilesystemTasks.Wrap(() => DrivesManager.GetRootFromPathAsync(item.Path));
148134
if (!string.IsNullOrEmpty(item.Path))
149135
{
150136
returnedItem = (item.ItemType == FilesystemItemType.File) ?
151-
ToType<IStorageItem, BaseStorageFile>(associatedInstance != null ?
152-
await associatedInstance.FilesystemViewModel.GetFileFromPathAsync(item.Path) :
137+
ToType<IStorageItem, BaseStorageFile>(
153138
await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFileFromPathAsync(item.Path, rootItem))) :
154-
ToType<IStorageItem, BaseStorageFolder>(associatedInstance != null ?
155-
await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(item.Path) :
139+
ToType<IStorageItem, BaseStorageFolder>(
156140
await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFolderFromPathAsync(item.Path, rootItem)));
157141
}
158142
if (returnedItem.Result == null && item.Item != null)
@@ -169,9 +153,9 @@ public static IStorageItemWithPath FromPathAndType(string customPath, Filesystem
169153
(IStorageItemWithPath)new StorageFolderWithPath(null, customPath);
170154
}
171155

172-
public static async Task<FilesystemItemType> GetTypeFromPath(string path, IShellPage associatedInstance = null)
156+
public static async Task<FilesystemItemType> GetTypeFromPath(string path)
173157
{
174-
IStorageItem item = await ToStorageItem<IStorageItem>(path, associatedInstance);
158+
IStorageItem item = await ToStorageItem<IStorageItem>(path);
175159

176160
return item == null ? FilesystemItemType.File : (item.IsOfType(StorageItemTypes.Folder) ? FilesystemItemType.Directory : FilesystemItemType.File);
177161
}

src/Files.Uwp/Helpers/WallpaperHelpers.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ namespace Files.Uwp.Helpers
99
{
1010
public static class WallpaperHelpers
1111
{
12-
public static async void SetAsBackground(WallpaperType type, string filePath, IShellPage associatedInstance)
12+
public static async void SetAsBackground(WallpaperType type, string filePath)
1313
{
1414
if (UserProfilePersonalizationSettings.IsSupported())
1515
{
1616
// Get the path of the selected file
17-
BaseStorageFile sourceFile = await StorageHelpers.ToStorageItem<BaseStorageFile>(filePath, associatedInstance);
17+
BaseStorageFile sourceFile = await StorageHelpers.ToStorageItem<BaseStorageFile>(filePath);
1818
if (sourceFile == null)
1919
{
2020
return;

src/Files.Uwp/Interacts/BaseLayoutCommandImplementationModel.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public virtual async void CreateShortcut(RoutedEventArgs e)
107107

108108
public virtual void SetAsLockscreenBackgroundItem(RoutedEventArgs e)
109109
{
110-
WallpaperHelpers.SetAsBackground(WallpaperType.LockScreen, SlimContentPage.SelectedItem.ItemPath, associatedInstance);
110+
WallpaperHelpers.SetAsBackground(WallpaperType.LockScreen, SlimContentPage.SelectedItem.ItemPath);
111111
}
112112

113113
public virtual async void SetAsDesktopBackgroundItem(RoutedEventArgs e)
@@ -130,7 +130,7 @@ public virtual async void SetAsDesktopBackgroundItem(RoutedEventArgs e)
130130
}
131131
else
132132
{
133-
WallpaperHelpers.SetAsBackground(WallpaperType.Desktop, SlimContentPage.SelectedItem.ItemPath, associatedInstance);
133+
WallpaperHelpers.SetAsBackground(WallpaperType.Desktop, SlimContentPage.SelectedItem.ItemPath);
134134
}
135135
}
136136

@@ -396,14 +396,14 @@ async void Manager_DataRequested(DataTransferManager sender, DataRequestedEventA
396396
}
397397
else if (item.PrimaryItemAttribute == StorageItemTypes.Folder && !item.IsZipItem)
398398
{
399-
if (await StorageHelpers.ToStorageItem<BaseStorageFolder>(item.ItemPath, associatedInstance) is BaseStorageFolder folder)
399+
if (await StorageHelpers.ToStorageItem<BaseStorageFolder>(item.ItemPath) is BaseStorageFolder folder)
400400
{
401401
items.Add(folder);
402402
}
403403
}
404404
else
405405
{
406-
if (await StorageHelpers.ToStorageItem<BaseStorageFile>(item.ItemPath, associatedInstance) is BaseStorageFile file)
406+
if (await StorageHelpers.ToStorageItem<BaseStorageFile>(item.ItemPath) is BaseStorageFile file)
407407
{
408408
items.Add(file);
409409
}

src/Files.Uwp/ViewModels/Properties/FileProperties.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ private async void ViewModel_PropertyChanged(object sender, System.ComponentMode
356356
private async Task<string> GetHashForFileAsync(ListedItem fileItem, string nameOfAlg, CancellationToken token, IProgress<float> progress, IShellPage associatedInstance)
357357
{
358358
HashAlgorithmProvider algorithmProvider = HashAlgorithmProvider.OpenAlgorithm(nameOfAlg);
359-
BaseStorageFile file = await StorageHelpers.ToStorageItem<BaseStorageFile>((fileItem as ShortcutItem)?.TargetPath ?? fileItem.ItemPath, associatedInstance);
359+
BaseStorageFile file = await StorageHelpers.ToStorageItem<BaseStorageFile>((fileItem as ShortcutItem)?.TargetPath ?? fileItem.ItemPath);
360360
if (file == null)
361361
{
362362
return "";

0 commit comments

Comments
 (0)