public async Task <ReturnResult> DeleteItemAsync(IStorageItem source, bool showDialog, bool permanently, bool registerHistory) { try { 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, 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); } catch (System.Exception ex) { NLog.LogManager.GetCurrentClassLogger().Warn($"Delete item operation failed:\n{ex}"); return(ReturnResult.Failed); } }
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); } }
public async Task <ReturnResult> DeleteItemsAsync(IEnumerable <IStorageItemWithPath> source, bool showDialog, bool permanently, bool registerHistory) { source = await source.ToListAsync(); var returnStatus = ReturnResult.InProgress; var deleteFromRecycleBin = source.Select(item => item.Path).Any(path => recycleBinHelpers.IsPathUnderRecycleBin(path)); var canBeSentToBin = !deleteFromRecycleBin && await recycleBinHelpers.HasRecycleBin(source.FirstOrDefault()?.Path); if (((!permanently && !canBeSentToBin) || UserSettingsService.PreferencesSettingsService.ShowConfirmDeleteDialog) && showDialog) // Check if the setting to show a confirmation dialog is on { List <FilesystemItemsOperationItemModel> incomingItems = new List <FilesystemItemsOperationItemModel>(); foreach (var src in source) { if (recycleBinHelpers.IsPathUnderRecycleBin(src.Path)) { var binItems = associatedInstance.FilesystemViewModel.FilesAndFolders; var matchingItem = binItems.FirstOrDefault(x => x.ItemPath == src.Path); // Get original file name incomingItems.Add(new FilesystemItemsOperationItemModel(FilesystemOperationType.Delete, src.Path, null, matchingItem?.ItemName)); } else { incomingItems.Add(new FilesystemItemsOperationItemModel(FilesystemOperationType.Delete, src.Path, null)); } } var dialog = FilesystemOperationDialogViewModel.GetDialog(new FilesystemItemsOperationDataModel( FilesystemOperationType.Delete, false, canBeSentToBin ? permanently : true, canBeSentToBin, incomingItems, new List <FilesystemItemsOperationItemModel>())); if (await dialog.TryShowAsync() != DialogResult.Primary) { return(ReturnResult.Cancelled); // Return if the result isn't delete } // Delete selected items if the result is Yes permanently = dialog.ViewModel.PermanentlyDelete; } // post the status banner var banner = PostBannerHelpers.PostBanner_Delete(source, returnStatus, permanently, false, 0); banner.ErrorCode.ProgressChanged += (s, e) => returnStatus = e.ToStatus(); var token = banner.CancellationToken; var sw = new Stopwatch(); sw.Start(); IStorageHistory history = await filesystemOperations.DeleteItemsAsync((IList <IStorageItemWithPath>) source, banner.Progress, banner.ErrorCode, permanently, token); ((IProgress <float>)banner.Progress).Report(100.0f); await Task.Yield(); if (!permanently && registerHistory) { App.HistoryWrapper.AddHistory(history); } var itemsDeleted = history?.Source.Count() ?? 0; source.ForEach(x => App.JumpList.RemoveFolder(x.Path)); // Remove items from jump list banner.Remove(); sw.Stop(); PostBannerHelpers.PostBanner_Delete(source, returnStatus, permanently, token.IsCancellationRequested, itemsDeleted); return(returnStatus); }