Skip to content

Commit 447aa95

Browse files
authored
Fixed some issues with undo/redo (#2632)
1 parent a469ccf commit 447aa95

18 files changed

+187
-322
lines changed

Files/Files.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@
200200
<Compile Include="Filesystem\FilesystemOperations\Helpers\FilesystemHelpers.cs" />
201201
<Compile Include="Filesystem\FilesystemOperations\Helpers\IFilesystemHelpers.cs" />
202202
<Compile Include="Filesystem\FilesystemOperations\IFilesystemOperations.cs" />
203-
<Compile Include="Filesystem\FilesystemOperations\PathWithType.cs" />
204203
<Compile Include="Filesystem\Search\FolderSearch.cs" />
205204
<Compile Include="Filesystem\StorageFileHelpers\FilesystemResult.cs" />
206205
<Compile Include="Filesystem\StorageFileHelpers\StorageFileExtensions.cs" />
@@ -219,7 +218,6 @@
219218
<Compile Include="Helpers\DialogDisplayHelper.cs" />
220219
<Compile Include="Helpers\DispatcherHelper.cs" />
221220
<Compile Include="View Models\Properties\FilePropertySection.cs" />
222-
<Compile Include="Helpers\ArrayHelpers.cs" />
223221
<Compile Include="Helpers\ItemsDataTemplateSelector.cs" />
224222
<Compile Include="Helpers\JumpListManager.cs" />
225223
<Compile Include="Helpers\NativeDirectoryChangesHelper.cs" />

Files/Filesystem/FilesystemOperations/FilesystemOperations.cs

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,23 @@ public FilesystemOperations(IShellPage associatedInstance)
4646

4747
#region IFilesystemOperations
4848

49-
public async Task<IStorageHistory> CreateAsync(PathWithType source, IProgress<FilesystemErrorCode> errorCode, CancellationToken cancellationToken)
49+
public async Task<IStorageHistory> CreateAsync(IStorageItemWithPath source, IProgress<FilesystemErrorCode> errorCode, CancellationToken cancellationToken)
5050
{
5151
try
5252
{
5353
switch (source.ItemType)
5454
{
5555
case FilesystemItemType.File:
5656
{
57-
StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(Path.GetDirectoryName(source.Path));
57+
StorageFolder folder = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(Path.GetDirectoryName(source.Path));
5858
await folder.CreateFileAsync(Path.GetFileName(source.Path));
5959

6060
break;
6161
}
6262

6363
case FilesystemItemType.Directory:
6464
{
65-
StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(Path.GetDirectoryName(source.Path));
65+
StorageFolder folder = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(Path.GetDirectoryName(source.Path));
6666
await folder.CreateFolderAsync(Path.GetFileName(source.Path));
6767

6868
break;
@@ -95,15 +95,14 @@ public async Task<IStorageHistory> CopyAsync(IStorageItem source,
9595
IProgress<FilesystemErrorCode> errorCode,
9696
CancellationToken cancellationToken)
9797
{
98-
return await CopyAsync(new PathWithType(source.Path,
99-
source.IsOfType(StorageItemTypes.File) ? FilesystemItemType.File : FilesystemItemType.Directory),
98+
return await CopyAsync(source.FromStorageItem(),
10099
destination,
101100
progress,
102101
errorCode,
103102
cancellationToken);
104103
}
105104

106-
public async Task<IStorageHistory> CopyAsync(PathWithType source,
105+
public async Task<IStorageHistory> CopyAsync(IStorageItemWithPath source,
107106
string destination,
108107
IProgress<float> progress,
109108
IProgress<FilesystemErrorCode> errorCode,
@@ -122,12 +121,12 @@ await DialogDisplayHelper.ShowDialogAsync(
122121
}
123122

124123
IStorageItem copiedItem = null;
125-
long itemSize = await FilesystemHelpers.GetItemSize(await source.Path.ToStorageItem());
124+
long itemSize = await FilesystemHelpers.GetItemSize(await source.ToStorageItem(associatedInstance));
126125
bool reportProgress = false; // TODO: The default value is false
127126

128127
if (source.ItemType == FilesystemItemType.Directory)
129128
{
130-
if (string.IsNullOrWhiteSpace(source.Path) ||
129+
if (!string.IsNullOrWhiteSpace(source.Path) &&
131130
Path.GetDirectoryName(destination).IsSubPathOf(source.Path)) // We check if user tried to copy anything above the source.ItemPath
132131
{
133132
ImpossibleActionResponseTypes responseType = ImpossibleActionResponseTypes.Abort;
@@ -169,14 +168,14 @@ await DialogDisplayHelper.ShowDialogAsync(
169168
progress?.Report((float)(itemSize * 100.0f / itemSize));
170169
}
171170

172-
FilesystemResult<StorageFolder> fsSourceFolderResult = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(Path.GetDirectoryName(source.Path));
173-
FilesystemResult<StorageFolder> fsDestinationFolderResult = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(Path.GetDirectoryName(destination));
171+
StorageFolder fsSourceFolder = (StorageFolder)await source.ToStorageItem(associatedInstance);
172+
StorageFolder fsDestinationFolder = (StorageFolder)await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(Path.GetDirectoryName(destination));
174173

175-
if (fsSourceFolderResult && fsDestinationFolderResult)
174+
if (fsSourceFolder != null && fsDestinationFolder != null)
176175
{
177176
FilesystemResult fsCopyResult = await FilesystemTasks.Wrap(async () =>
178177
{
179-
return await FilesystemHelpers.CloneDirectoryAsync(fsSourceFolderResult.Result, fsDestinationFolderResult.Result, Path.GetFileName(source.Path));
178+
return await FilesystemHelpers.CloneDirectoryAsync(fsSourceFolder, fsDestinationFolder, fsSourceFolder.Name);
180179
})
181180
.OnSuccess(t =>
182181
{
@@ -201,15 +200,15 @@ await DialogDisplayHelper.ShowDialogAsync(
201200

202201
if (fsResult)
203202
{
204-
StorageFile file = (StorageFile)await source.Path.ToStorageItem();
203+
StorageFile file = (StorageFile)await source.ToStorageItem(associatedInstance);
205204

206205
FilesystemResult<StorageFile> fsResultCopy = new FilesystemResult<StorageFile>(null, FilesystemErrorCode.ERROR_GENERIC);
207206

208207
if (file != null)
209208
{
210209
fsResultCopy = await FilesystemTasks.Wrap(() =>
211210
{
212-
return file.CopyAsync(fsResult.Result, Path.GetFileName(source.Path), NameCollisionOption.GenerateUniqueName).AsTask();
211+
return file.CopyAsync(fsResult.Result, Path.GetFileName(file.Name), NameCollisionOption.GenerateUniqueName).AsTask();
213212
});
214213
}
215214

@@ -222,7 +221,7 @@ await DialogDisplayHelper.ShowDialogAsync(
222221
// Try again with CopyFileFromApp
223222
if (NativeFileOperationsHelper.CopyFileFromApp(source.Path, destination, true))
224223
{
225-
copiedItem = await source.Path.ToStorageItem(); // Dangerous - the provided item may be different than output result!
224+
copiedItem = await source.ToStorageItem(associatedInstance); // Dangerous - the provided item may be different than output result!
226225
}
227226
else
228227
{
@@ -253,9 +252,7 @@ await DialogDisplayHelper.ShowDialogAsync(
253252

254253
progress?.Report(100.0f);
255254

256-
var pathWithType = new PathWithType(
257-
copiedItem != null ? (!string.IsNullOrWhiteSpace(copiedItem.Path) ? copiedItem.Path : destination) : destination,
258-
source.ItemType);
255+
var pathWithType = copiedItem.FromStorageItem(destination, source.ItemType);
259256

260257
return new StorageHistory(FileOperationType.Copy, source, pathWithType);
261258
}
@@ -266,15 +263,14 @@ public async Task<IStorageHistory> MoveAsync(IStorageItem source,
266263
IProgress<FilesystemErrorCode> errorCode,
267264
CancellationToken cancellationToken)
268265
{
269-
return await MoveAsync(new PathWithType(source.Path,
270-
source.IsOfType(StorageItemTypes.File) ? FilesystemItemType.File : FilesystemItemType.Directory),
266+
return await MoveAsync(source.FromStorageItem(),
271267
destination,
272268
progress,
273269
errorCode,
274270
cancellationToken);
275271
}
276272

277-
public async Task<IStorageHistory> MoveAsync(PathWithType source,
273+
public async Task<IStorageHistory> MoveAsync(IStorageItemWithPath source,
278274
string destination,
279275
IProgress<float> progress,
280276
IProgress<FilesystemErrorCode> errorCode,
@@ -303,15 +299,14 @@ public async Task<IStorageHistory> DeleteAsync(IStorageItem source,
303299
bool permanently,
304300
CancellationToken cancellationToken)
305301
{
306-
return await DeleteAsync(new PathWithType(source.Path,
307-
source.IsOfType(StorageItemTypes.File) ? FilesystemItemType.File : FilesystemItemType.Directory),
302+
return await DeleteAsync(source.FromStorageItem(),
308303
progress,
309304
errorCode,
310305
permanently,
311306
cancellationToken);
312307
}
313308

314-
public async Task<IStorageHistory> DeleteAsync(PathWithType source,
309+
public async Task<IStorageHistory> DeleteAsync(IStorageItemWithPath source,
315310
IProgress<float> progress,
316311
IProgress<FilesystemErrorCode> errorCode,
317312
bool permanently,
@@ -392,7 +387,7 @@ await associatedInstance.FilesystemViewModel.GetFileFromPathAsync(iFilePath)
392387
// Get newest file
393388
ShellFileItem item = nameMatchItems.Where((item) => item.RecycleDate != null).OrderBy((item) => item.RecycleDate).FirstOrDefault();
394389

395-
return new StorageHistory(FileOperationType.Recycle, source, new PathWithType(item?.RecyclePath, source.ItemType));
390+
return new StorageHistory(FileOperationType.Recycle, source, StorageItemHelpers.FromPathAndType(item?.RecyclePath, source.ItemType));
396391
}
397392

398393
return new StorageHistory(FileOperationType.Delete, source, null);
@@ -427,7 +422,7 @@ public async Task<IStorageHistory> RenameAsync(IStorageItem source,
427422
await source.RenameAsync(newName, collision);
428423

429424
errorCode?.Report(FilesystemErrorCode.ERROR_SUCCESS);
430-
return new StorageHistory(FileOperationType.Rename, new PathWithType(originalSource, itemType), new PathWithType(source.Path, itemType));
425+
return new StorageHistory(FileOperationType.Rename, StorageItemHelpers.FromPathAndType(originalSource, itemType), source.FromStorageItem());
431426
}
432427
catch (Exception e)
433428
{
@@ -439,7 +434,7 @@ public async Task<IStorageHistory> RenameAsync(IStorageItem source,
439434
return null;
440435
}
441436

442-
public async Task<IStorageHistory> RenameAsync(PathWithType source,
437+
public async Task<IStorageHistory> RenameAsync(IStorageItemWithPath source,
443438
string newName,
444439
NameCollisionOption collision,
445440
IProgress<FilesystemErrorCode> errorCode,
@@ -457,11 +452,11 @@ public async Task<IStorageHistory> RenameAsync(PathWithType source,
457452
{
458453
try
459454
{
460-
IStorageItem itemToRename = await source.Path.ToStorageItem();
455+
IStorageItem itemToRename = await source.ToStorageItem(associatedInstance);
461456
await itemToRename.RenameAsync(newName, collision);
462457

463458
errorCode?.Report(FilesystemErrorCode.ERROR_SUCCESS);
464-
return new StorageHistory(FileOperationType.Rename, source, new PathWithType(itemToRename.Path, source.ItemType));
459+
return new StorageHistory(FileOperationType.Rename, source, itemToRename.FromStorageItem());
465460
}
466461
catch (Exception e)
467462
{
@@ -473,7 +468,7 @@ public async Task<IStorageHistory> RenameAsync(PathWithType source,
473468
return null;
474469
}
475470

476-
public async Task<IStorageHistory> RestoreFromTrashAsync(PathWithType source,
471+
public async Task<IStorageHistory> RestoreFromTrashAsync(IStorageItemWithPath source,
477472
string destination,
478473
IProgress<float> progress,
479474
IProgress<FilesystemErrorCode> errorCode,
@@ -490,7 +485,7 @@ public async Task<IStorageHistory> RestoreFromTrashAsync(PathWithType source,
490485
fsResult = sourceFolder.ErrorCode | destinationFolder.ErrorCode;
491486
errorCode?.Report(fsResult);
492487

493-
if (sourceFolder && destinationFolder)
488+
if (fsResult)
494489
{
495490
fsResult = await FilesystemTasks.Wrap(() =>
496491
{
@@ -510,7 +505,7 @@ public async Task<IStorageHistory> RestoreFromTrashAsync(PathWithType source,
510505
fsResult = sourceFile.ErrorCode | destinationFolder.ErrorCode;
511506
errorCode?.Report(fsResult);
512507

513-
if (sourceFile && destinationFolder)
508+
if (fsResult)
514509
{
515510
fsResult = await FilesystemTasks.Wrap(() =>
516511
{
@@ -519,13 +514,21 @@ public async Task<IStorageHistory> RestoreFromTrashAsync(PathWithType source,
519514
NameCollisionOption.GenerateUniqueName).AsTask();
520515
});
521516
}
517+
else if (fsResult == FilesystemErrorCode.ERROR_UNAUTHORIZED)
518+
{
519+
// Try again with MoveFileFromApp
520+
fsResult = (FilesystemResult)NativeFileOperationsHelper.MoveFileFromApp(source.Path, destination);
521+
}
522522
errorCode?.Report(fsResult);
523523
}
524524

525-
// Recycle bin also stores a file starting with $I for each item
526-
string iFilePath = Path.Combine(Path.GetDirectoryName(source.Path), Path.GetFileName(source.Path).Replace("$R", "$I"));
527-
await associatedInstance.FilesystemViewModel.GetFileFromPathAsync(iFilePath)
528-
.OnSuccess(iFile => iFile.DeleteAsync().AsTask());
525+
if (fsResult)
526+
{
527+
// Recycle bin also stores a file starting with $I for each item
528+
string iFilePath = Path.Combine(Path.GetDirectoryName(source.Path), Path.GetFileName(source.Path).Replace("$R", "$I"));
529+
await associatedInstance.FilesystemViewModel.GetFileFromPathAsync(iFilePath)
530+
.OnSuccess(iFile => iFile.DeleteAsync().AsTask());
531+
}
529532

530533
errorCode?.Report(fsResult);
531534
if (fsResult != FilesystemErrorCode.ERROR_SUCCESS)
@@ -544,7 +547,7 @@ await associatedInstance.FilesystemViewModel.GetFileFromPathAsync(iFilePath)
544547
}
545548
}
546549

547-
return new StorageHistory(FileOperationType.Restore, source, new PathWithType(destination, source.ItemType));
550+
return new StorageHistory(FileOperationType.Restore, source, StorageItemHelpers.FromPathAndType(destination, source.ItemType));
548551
}
549552

550553
#endregion IFilesystemOperations

0 commit comments

Comments
 (0)