Пример #1
0
        public async Task <ReturnResult> DeleteItemsAsync(IEnumerable <IStorageItemWithPath> source, bool showDialog, bool permanently, bool registerHistory)
        {
            try
            {
                PostedStatusBanner banner;
                if (permanently)
                {
                    banner = associatedInstance.StatusCenterActions.PostBanner(string.Empty,
                                                                               associatedInstance.FilesystemViewModel.WorkingDirectory,
                                                                               0,
                                                                               ReturnResult.InProgress,
                                                                               FileOperationType.Delete);
                }
                else
                {
                    banner = associatedInstance.StatusCenterActions.PostBanner(string.Empty,
                                                                               associatedInstance.FilesystemViewModel.WorkingDirectory,
                                                                               0,
                                                                               ReturnResult.InProgress,
                                                                               FileOperationType.Recycle);
                }

                var returnStatus = ReturnResult.InProgress;
                banner.ErrorCode.ProgressChanged += (s, e) => returnStatus = e.ToStatus();

                var pathsUnderRecycleBin = GetPathsUnderRecycleBin(source);

                if (App.AppSettings.ShowConfirmDeleteDialog && showDialog) // Check if the setting to show a confirmation dialog is on
                {
                    var deleteFromRecycleBin = pathsUnderRecycleBin.Count > 0;

                    List <FilesystemItemsOperationItemModel> incomingItems = new List <FilesystemItemsOperationItemModel>();

                    for (int i = 0; i < source.Count(); i++)
                    {
                        incomingItems.Add(new FilesystemItemsOperationItemModel(FilesystemOperationType.Delete, source.ElementAt(i).Path ?? source.ElementAt(i).Item.Path, null));
                    }

                    FilesystemOperationDialog dialog = FilesystemOperationDialogViewModel.GetDialog(new FilesystemItemsOperationDataModel(
                                                                                                        FilesystemOperationType.Delete,
                                                                                                        false,
                                                                                                        !deleteFromRecycleBin ? permanently : deleteFromRecycleBin,
                                                                                                        !deleteFromRecycleBin,
                                                                                                        incomingItems,
                                                                                                        new List <FilesystemItemsOperationItemModel>()));

                    ContentDialogResult result = await dialog.ShowAsync();

                    if (result != ContentDialogResult.Primary)
                    {
                        banner.Remove();
                        return(ReturnResult.Cancelled); // Return if the result isn't delete
                    }

                    // Delete selected items if the result is Yes
                    permanently = dialog.ViewModel.PermanentlyDelete;
                }

                var sw = new Stopwatch();
                sw.Start();

                IStorageHistory history;
                var             rawStorageHistory = new List <IStorageHistory>();

                bool  originalPermanently = permanently;
                float progress;
                for (int i = 0; i < source.Count(); i++)
                {
                    if (pathsUnderRecycleBin.Contains(source.ElementAt(i).Path))
                    {
                        permanently = true;
                    }
                    else
                    {
                        permanently = originalPermanently;
                    }

                    rawStorageHistory.Add(await filesystemOperations.DeleteAsync(source.ElementAt(i), null, banner.ErrorCode, permanently, cancellationToken));
                    progress = ((float)i / (float)source.Count()) * 100.0f;
                    ((IProgress <float>)banner.Progress).Report(progress);
                }

                if (rawStorageHistory.Any() && rawStorageHistory.TrueForAll((item) => item != null))
                {
                    history = new StorageHistory(
                        rawStorageHistory[0].OperationType,
                        rawStorageHistory.SelectMany((item) => item.Source).ToList(),
                        rawStorageHistory.SelectMany((item) => item.Destination).ToList());

                    if (!permanently && registerHistory)
                    {
                        App.HistoryWrapper.AddHistory(history);
                    }
                }

                banner.Remove();
                sw.Stop();

                PostBannerHelpers.PostBanner_Delete(returnStatus, permanently ? FileOperationType.Delete : FileOperationType.Recycle, sw, associatedInstance);
                return(returnStatus);
            }
            catch (System.Exception ex)
            {
                NLog.LogManager.GetCurrentClassLogger().Warn($"Delete items operation failed:\n{ex}");
                return(ReturnResult.Failed);
            }
        }
Пример #2
0
        public async Task <ReturnResult> DeleteItemsAsync(IEnumerable <IStorageItemWithPath> source, bool showDialog, bool permanently, bool registerHistory)
        {
            PostedStatusBanner banner;

            if (permanently)
            {
                banner = associatedInstance.StatusCenterActions.PostBanner(string.Empty,
                                                                           associatedInstance.FilesystemViewModel.WorkingDirectory,
                                                                           0,
                                                                           ReturnResult.InProgress,
                                                                           FileOperationType.Delete);
            }
            else
            {
                banner = associatedInstance.StatusCenterActions.PostBanner(string.Empty,
                                                                           associatedInstance.FilesystemViewModel.WorkingDirectory,
                                                                           0,
                                                                           ReturnResult.InProgress,
                                                                           FileOperationType.Recycle);
            }

            var returnStatus = ReturnResult.InProgress;

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

            var pathsUnderRecycleBin = GetPathsUnderRecycleBin(source);

            if (App.AppSettings.ShowConfirmDeleteDialog && showDialog) // Check if the setting to show a confirmation dialog is on
            {
                var deleteFromRecycleBin = pathsUnderRecycleBin.Count > 0;

                ConfirmDeleteDialog dialog = new ConfirmDeleteDialog(
                    deleteFromRecycleBin,
                    !deleteFromRecycleBin ? permanently : deleteFromRecycleBin,
                    associatedInstance.SlimContentPage.SelectedItems.Count);

                if (UIHelpers.IsAnyContentDialogOpen())
                {
                    // Can show only one dialog at a time
                    banner.Remove();
                    return(ReturnResult.Cancelled);
                }
                await dialog.ShowAsync();

                if (dialog.Result != DialogResult.Delete) // Delete selected items if the result is Yes
                {
                    banner.Remove();
                    return(ReturnResult.Cancelled); // Return if the result isn't delete
                }

                permanently = dialog.PermanentlyDelete;
            }

            var sw = new Stopwatch();

            sw.Start();

            IStorageHistory history;
            var             rawStorageHistory = new List <IStorageHistory>();

            bool  originalPermanently = permanently;
            float progress;

            for (int i = 0; i < source.Count(); i++)
            {
                if (pathsUnderRecycleBin.Contains(source.ElementAt(i).Path))
                {
                    permanently = true;
                }
                else
                {
                    permanently = originalPermanently;
                }

                rawStorageHistory.Add(await filesystemOperations.DeleteAsync(source.ElementAt(i), null, banner.ErrorCode, permanently, cancellationToken));
                progress = ((float)i / (float)source.Count()) * 100.0f;
                ((IProgress <float>)banner.Progress).Report(progress);
            }

            if (rawStorageHistory.Any() && rawStorageHistory.TrueForAll((item) => item != null))
            {
                history = new StorageHistory(
                    rawStorageHistory[0].OperationType,
                    rawStorageHistory.SelectMany((item) => item.Source).ToList(),
                    rawStorageHistory.SelectMany((item) => item.Destination).ToList());

                if (!permanently && registerHistory)
                {
                    App.HistoryWrapper.AddHistory(history);
                }
            }

            banner.Remove();
            sw.Stop();

            PostBannerHelpers.PostBanner_Delete(returnStatus, permanently ? FileOperationType.Delete : FileOperationType.Recycle, sw, associatedInstance);

            return(returnStatus);
        }
        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);
        }
Пример #4
0
        public async Task <ReturnResult> DeleteItemAsync(IStorageItemWithPath source, bool showDialog, bool permanently, bool registerHistory)
        {
            PostedStatusBanner banner;
            bool deleteFromRecycleBin = recycleBinHelpers.IsPathUnderRecycleBin(source.Path);

            if (deleteFromRecycleBin)
            {
                permanently = true;
            }

            if (permanently)
            {
                banner = associatedInstance.StatusCenterActions.PostBanner(string.Empty,
                                                                           associatedInstance.FilesystemViewModel.WorkingDirectory,
                                                                           0,
                                                                           ReturnResult.InProgress,
                                                                           FileOperationType.Delete);
            }
            else
            {
                banner = associatedInstance.StatusCenterActions.PostBanner(string.Empty,
                                                                           associatedInstance.FilesystemViewModel.WorkingDirectory,
                                                                           0,
                                                                           ReturnResult.InProgress,
                                                                           FileOperationType.Recycle);
            }

            var returnStatus = ReturnResult.InProgress;

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

            if (App.AppSettings.ShowConfirmDeleteDialog && showDialog) // Check if the setting to show a confirmation dialog is on
            {
                List <FilesystemItemsOperationItemModel> incomingItems = new List <FilesystemItemsOperationItemModel>
                {
                    new FilesystemItemsOperationItemModel(FilesystemOperationType.Delete, source.Path ?? source.Item.Path, null)
                };

                FilesystemOperationDialog dialog = FilesystemOperationDialogViewModel.GetDialog(new FilesystemItemsOperationDataModel(
                                                                                                    FilesystemOperationType.Delete,
                                                                                                    false,
                                                                                                    !deleteFromRecycleBin ? permanently : deleteFromRecycleBin,
                                                                                                    !deleteFromRecycleBin,
                                                                                                    incomingItems,
                                                                                                    new List <FilesystemItemsOperationItemModel>()));

                ContentDialogResult result = await dialog.ShowAsync();

                if (result != ContentDialogResult.Primary)
                {
                    banner.Remove();
                    return(ReturnResult.Cancelled); // Return if the result isn't delete
                }

                // Delete selected item if the result is Yes
                permanently = dialog.ViewModel.PermanentlyDelete;
            }

            var sw = new Stopwatch();

            sw.Start();

            IStorageHistory history = await filesystemOperations.DeleteAsync(source, banner.Progress, banner.ErrorCode, permanently, cancellationToken);

            ((IProgress <float>)banner.Progress).Report(100.0f);

            if (!permanently && registerHistory)
            {
                App.HistoryWrapper.AddHistory(history);
            }

            banner.Remove();
            sw.Stop();

            PostBannerHelpers.PostBanner_Delete(returnStatus, permanently ? FileOperationType.Delete : FileOperationType.Recycle, sw, associatedInstance);

            return(returnStatus);
        }
Пример #5
0
        public async Task <ReturnResult> DeleteItemsAsync(IEnumerable <IStorageItemWithPath> source, bool showDialog, bool permanently, bool registerHistory)
        {
            bool deleteFromRecycleBin = false;

            foreach (IStorageItemWithPath item in source)
            {
                if (await recycleBinHelpers.IsRecycleBinItem(item.Path))
                {
                    deleteFromRecycleBin = true;
                    break;
                }
            }

            PostedStatusBanner banner;

            if (permanently)
            {
                banner = associatedInstance.BottomStatusStripControl.OngoingTasksControl.PostBanner(string.Empty,
                                                                                                    associatedInstance.FilesystemViewModel.WorkingDirectory,
                                                                                                    0,
                                                                                                    ReturnResult.InProgress,
                                                                                                    FileOperationType.Delete);
            }
            else
            {
                banner = associatedInstance.BottomStatusStripControl.OngoingTasksControl.PostBanner(string.Empty,
                                                                                                    associatedInstance.FilesystemViewModel.WorkingDirectory,
                                                                                                    0,
                                                                                                    ReturnResult.InProgress,
                                                                                                    FileOperationType.Recycle);
            }

            ReturnResult returnStatus = ReturnResult.InProgress;

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

            if (App.AppSettings.ShowConfirmDeleteDialog && showDialog) // Check if the setting to show a confirmation dialog is on
            {
                ConfirmDeleteDialog dialog = new ConfirmDeleteDialog(
                    deleteFromRecycleBin,
                    !deleteFromRecycleBin ? permanently : deleteFromRecycleBin,
                    associatedInstance.ContentPage.SelectedItemsPropertiesViewModel);

                await dialog.ShowAsync();

                if (dialog.Result != DialogResult.Delete) // Delete selected  items if the result is Yes
                {
                    return(ReturnResult.Cancelled);       // Return if the result isn't delete
                }

                permanently = dialog.PermanentlyDelete;
            }

            Stopwatch sw = new Stopwatch();

            sw.Start();

            IStorageHistory history;
            var             rawStorageHistory = new List <IStorageHistory>();

            bool originalPermanently = permanently;

            foreach (IStorageItemWithPath item in source)
            {
                if (await recycleBinHelpers.IsRecycleBinItem(item.Path))
                {
                    permanently = true;
                }
                else
                {
                    permanently = originalPermanently;
                }

                rawStorageHistory.Add(await filesystemOperations.DeleteAsync(item, banner.Progress, banner.ErrorCode, permanently, cancellationToken));
            }

            if (rawStorageHistory.TrueForAll((item) => item != null))
            {
                history = new StorageHistory(
                    rawStorageHistory[0].OperationType,
                    rawStorageHistory.SelectMany((item) => item.Source).ToList(),
                    rawStorageHistory.SelectMany((item) => item.Destination).ToList());

                if (!permanently && registerHistory)
                {
                    App.HistoryWrapper.AddHistory(history);
                }
            }

            banner.Remove();
            sw.Stop();

            PostBannerHelpers.PostBanner_Delete(returnStatus, permanently ? FileOperationType.Delete : FileOperationType.Recycle, sw, associatedInstance);

            return(returnStatus);
        }