public async Task <ReturnResult> Redo(IStorageHistory history)
        {
            ReturnResult returnStatus = ReturnResult.InProgress;
            Progress <FileSystemStatusCode> errorCode = new Progress <FileSystemStatusCode>();

            errorCode.ProgressChanged += (s, e) => { returnStatus = e.ToStatus(); };

            switch (history.OperationType)
            {
            case FileOperationType.CreateNew:     // CreateNew PASS
            {
                if (IsHistoryNull(history))
                {
                    break;
                }

                for (int i = 0; i < history.Source.Count(); i++)
                {
                    await filesystemOperations.CreateAsync(history.Source.ElementAt(i), errorCode, cancellationToken);
                }

                break;
            }

            case FileOperationType.Rename:     // Rename PASS
            {
                if (IsHistoryNull(history))
                {
                    break;
                }

                NameCollisionOption collision = NameCollisionOption.GenerateUniqueName;
                for (int i = 0; i < history.Source.Count(); i++)
                {
                    await filesystemOperations.RenameAsync(
                        history.Source.ElementAt(i),
                        Path.GetFileName(history.Destination.ElementAt(i).Path),
                        collision,
                        errorCode,
                        cancellationToken);
                }

                break;
            }

            case FileOperationType.Copy:     // Copy PASS
            {
                if (IsHistoryNull(history))
                {
                    break;
                }

                return(await filesystemHelpers.CopyItemsAsync(history.Source, history.Destination.Select((item) => item.Path), false, false));
            }

            case FileOperationType.Move:     // Move PASS
            {
                if (IsHistoryNull(history))
                {
                    break;
                }

                return(await filesystemHelpers.MoveItemsAsync(history.Source, history.Destination.Select((item) => item.Path), false, false));
            }

            case FileOperationType.Extract:     // Extract PASS
            {
                returnStatus = ReturnResult.Success;
                Debugger.Break();

                break;
            }

            case FileOperationType.Recycle:     // Recycle PASS
            {
                if (IsHistoryNull(history.Destination))
                {
                    break;
                }

                List <IStorageHistory> rawStorageHistory = new List <IStorageHistory>();
                for (int i = 0; i < history.Source.Count(); i++)
                {
                    rawStorageHistory.Add(await filesystemOperations.DeleteAsync(
                                              history.Source.ElementAt(i),
                                              null,
                                              errorCode,
                                              false,
                                              cancellationToken));
                }

                if (rawStorageHistory.TrueForAll((item) => item != null))
                {
                    IStorageHistory newHistory = new StorageHistory(
                        FileOperationType.Recycle,
                        rawStorageHistory.SelectMany((item) => item?.Source).ToList(),
                        rawStorageHistory.SelectMany((item) => item?.Destination).ToList());

                    // We need to change the recycled item paths (since IDs are different) - for Undo() to work
                    App.HistoryWrapper.ModifyCurrentHistory(newHistory);
                }
                else
                {
                    App.HistoryWrapper.RemoveHistory(history, true);
                }

                break;
            }

            case FileOperationType.Restore:     // Restore PASS
            {
                if (IsHistoryNull(history))
                {
                    break;
                }

                for (int i = 0; i < history.Destination.Count(); i++)
                {
                    await filesystemHelpers.RestoreFromTrashAsync(history.Source.ElementAt(i), history.Destination.ElementAt(i).Path, false);
                }

                break;
            }

            case FileOperationType.Delete:     // Delete PASS
            {
                returnStatus = ReturnResult.Success;

                break;
            }
            }

            return(returnStatus);
        }
Example #2
0
        public async Task <ReturnResult> Undo(IStorageHistory history)
        {
            ReturnResult returnStatus = ReturnResult.InProgress;
            Progress <FileSystemStatusCode> errorCode = new();

            errorCode.ProgressChanged += (s, e) => returnStatus = e.ToStatus();

            switch (history.OperationType)
            {
            case FileOperationType.CreateNew:     // Opposite: Delete created items
                if (!IsHistoryNull(history.Source))
                {
                    return(await helpers.DeleteItemsAsync(history.Source, false, true, false));
                }
                break;

            case FileOperationType.CreateLink:     // Opposite: Delete created items
                if (!IsHistoryNull(history.Destination))
                {
                    return(await helpers.DeleteItemsAsync(history.Destination, false, true, false));
                }
                break;

            case FileOperationType.Rename:     // Opposite: Restore original item names
                if (!IsHistoryNull(history))
                {
                    NameCollisionOption collision = NameCollisionOption.GenerateUniqueName;
                    for (int i = 0; i < history.Destination.Count(); i++)
                    {
                        string name = Path.GetFileName(history.Source[i].Path);
                        await operations.RenameAsync(history.Destination[i], name, collision, errorCode, cancellationToken);
                    }
                }
                break;

            case FileOperationType.Copy:     // Opposite: Delete copied items
                if (!IsHistoryNull(history.Destination))
                {
                    return(await helpers.DeleteItemsAsync(history.Destination, false, true, false));
                }
                break;

            case FileOperationType.Move:     // Opposite: Move the items to original directory
                if (!IsHistoryNull(history))
                {
                    return(await helpers.MoveItemsAsync(history.Destination, history.Source.Select(item => item.Path), false, false));
                }
                break;

            case FileOperationType.Extract:     // Opposite: No opposite for archive extraction
                returnStatus = ReturnResult.Success;
                Debugger.Break();
                break;

            case FileOperationType.Recycle:     // Opposite: Restore recycled items
                if (!IsHistoryNull(history))
                {
                    returnStatus = await helpers.RestoreItemsFromTrashAsync(history.Destination, history.Source.Select(item => item.Path), false);

                    if (returnStatus is ReturnResult.IntegrityCheckFailed)     // Not found, corrupted
                    {
                        App.HistoryWrapper.RemoveHistory(history, false);
                    }
                }
                break;

            case FileOperationType.Restore:     // Opposite: Move restored items to Recycle Bin
                if (!IsHistoryNull(history.Destination))
                {
                    var newHistory = await operations.DeleteItemsAsync(history.Destination, null, errorCode, false, cancellationToken);

                    if (newHistory is null)
                    {
                        App.HistoryWrapper.RemoveHistory(history, false);
                    }
                    else
                    {
                        // We need to change the recycled item paths (since IDs are different) - for Redo() to work
                        App.HistoryWrapper.ModifyCurrentHistory(newHistory);
                    }
                }
                break;

            case FileOperationType.Delete:     // Opposite: No opposite for pernament deletion
                returnStatus = ReturnResult.Success;
                break;
            }

            return(returnStatus);
        }