public static async Task <IStorageItem> CreateFileFromDialogResultTypeForResult(AddItemDialogItemType itemType, ShellNewEntry itemInfo, IShellPage associatedInstance) { string currentPath = null; if (associatedInstance.SlimContentPage != null) { currentPath = associatedInstance.FilesystemViewModel.WorkingDirectory; if (App.LibraryManager.TryGetLibrary(currentPath, out var library)) { if (!library.IsEmpty && library.Folders.Count == 1) // TODO: handle libraries with multiple folders { currentPath = library.Folders.First(); } } } // Skip rename dialog when ShellNewEntry has a Command (e.g. ".accdb", ".gdoc") string userInput = null; if (itemType != AddItemDialogItemType.File || itemInfo?.Command == null) { DynamicDialog dialog = DynamicDialogFactory.GetFor_RenameDialog(); await dialog.ShowAsync(); // Show rename dialog if (dialog.DynamicResult != DynamicDialogResult.Primary) { return(null); } userInput = dialog.ViewModel.AdditionalData as string; } // Create file based on dialog result (ReturnResult Status, IStorageItem Item)created = (ReturnResult.Failed, null); switch (itemType) { case AddItemDialogItemType.Folder: userInput = !string.IsNullOrWhiteSpace(userInput) ? userInput : "NewFolder".GetLocalized(); created = await associatedInstance.FilesystemHelpers.CreateAsync( StorageHelpers.FromPathAndType(PathNormalization.Combine(currentPath, userInput), FilesystemItemType.Directory), true); break; case AddItemDialogItemType.File: userInput = !string.IsNullOrWhiteSpace(userInput) ? userInput : itemInfo?.Name ?? "NewFile".GetLocalized(); created = await associatedInstance.FilesystemHelpers.CreateAsync( StorageHelpers.FromPathAndType(PathNormalization.Combine(currentPath, userInput + itemInfo?.Extension), FilesystemItemType.File), true); break; } if (created.Status == ReturnResult.AccessUnauthorized) { await DialogDisplayHelper.ShowDialogAsync("AccessDenied".GetLocalized(), "AccessDeniedCreateDialog/Text".GetLocalized()); } return(created.Item); }
public static Func <ListedItem, string> GetItemGroupKeySelector(GroupOption option) { return(option switch { GroupOption.Name => x => new string(x.ItemName.Take(1).ToArray()).ToUpperInvariant(), GroupOption.Size => x => x.PrimaryItemAttribute != StorageItemTypes.Folder ? GetGroupSizeKey(x.FileSizeBytes) : x.FileSizeDisplay, GroupOption.DateCreated => x => dateTimeFormatter.ToTimeSpanLabel(x.ItemDateCreatedReal).Text, GroupOption.DateModified => x => dateTimeFormatter.ToTimeSpanLabel(x.ItemDateModifiedReal).Text, GroupOption.FileType => x => x.PrimaryItemAttribute == StorageItemTypes.Folder && !x.IsShortcutItem ? x.ItemType : x.FileExtension?.ToLowerInvariant() ?? " ", GroupOption.SyncStatus => x => x.SyncStatusString, GroupOption.FileTag => x => x.FileTags?.FirstOrDefault(), GroupOption.OriginalFolder => x => (x as RecycleBinItem)?.ItemOriginalFolder, GroupOption.DateDeleted => x => dateTimeFormatter.ToTimeSpanLabel((x as RecycleBinItem)?.ItemDateDeletedReal ?? DateTimeOffset.Now).Text, GroupOption.FolderPath => x => PathNormalization.GetParentDir(x.ItemPath.TrimPath()), _ => null, });
public static PostedStatusBanner PostBanner_Move(IEnumerable <IStorageItemWithPath> source, IEnumerable <string> destination, ReturnResult returnStatus, bool canceled, int itemsMoved) { var sourceDir = PathNormalization.GetParentDir(source.FirstOrDefault()?.Path); var destinationDir = PathNormalization.GetParentDir(destination.FirstOrDefault()); if (canceled) { return(OngoingTasksViewModel.PostBanner( "StatusMoveCanceled".GetLocalized(), string.Format(source.Count() > 1 ? itemsMoved > 1 ? "StatusMoveCanceledDetails_Plural".GetLocalized() : "StatusMoveCanceledDetails_Plural2".GetLocalized() : "StatusMoveCanceledDetails_Singular".GetLocalized(), source.Count(), sourceDir, destinationDir, itemsMoved), 0, ReturnResult.Cancelled, FileOperationType.Move)); } else if (returnStatus == ReturnResult.InProgress) { return(OngoingTasksViewModel.PostOperationBanner( string.Empty, string.Format(source.Count() > 1 ? "StatusMovingItemsDetails_Plural".GetLocalized() : "StatusMovingItemsDetails_Singular".GetLocalized(), source.Count(), sourceDir, destinationDir), 0, ReturnResult.InProgress, FileOperationType.Move, new CancellationTokenSource())); } else if (returnStatus == ReturnResult.Success) { return(OngoingTasksViewModel.PostBanner( "StatusMoveComplete".GetLocalized(), string.Format(source.Count() > 1 ? "StatusMovedItemsDetails_Plural".GetLocalized() : "StatusMovedItemsDetails_Singular".GetLocalized(), source.Count(), sourceDir, destinationDir, itemsMoved), 0, ReturnResult.Success, FileOperationType.Move)); } else { return(OngoingTasksViewModel.PostBanner( "StatusMoveFailed".GetLocalized(), string.Format(source.Count() > 1 ? "StatusMoveFailedDetails_Plural".GetLocalized() : "StatusMoveFailedDetails_Singular".GetLocalized(), source.Count(), sourceDir, destinationDir), 0, ReturnResult.Failed, FileOperationType.Move)); } }
public static PostedStatusBanner PostBanner_Delete(IEnumerable <IStorageItemWithPath> source, ReturnResult returnStatus, bool permanently, bool canceled, int itemsDeleted) { var sourceDir = PathNormalization.GetParentDir(source.FirstOrDefault()?.Path); if (canceled) { if (permanently) { return(OngoingTasksViewModel.PostBanner( "StatusDeletionCancelled".GetLocalized(), string.Format(source.Count() > 1 ? itemsDeleted > 1 ? "StatusDeleteCanceledDetails_Plural".GetLocalized() : "StatusDeleteCanceledDetails_Plural2".GetLocalized() : "StatusDeleteCanceledDetails_Singular".GetLocalized(), source.Count(), sourceDir, null, itemsDeleted), 0, ReturnResult.Cancelled, FileOperationType.Delete)); } else { return(OngoingTasksViewModel.PostBanner( "StatusRecycleCancelled".GetLocalized(), string.Format(source.Count() > 1 ? itemsDeleted > 1 ? "StatusMoveCanceledDetails_Plural".GetLocalized() : "StatusMoveCanceledDetails_Plural2".GetLocalized() : "StatusMoveCanceledDetails_Singular".GetLocalized(), source.Count(), sourceDir, "TheRecycleBin".GetLocalized(), itemsDeleted), 0, ReturnResult.Cancelled, FileOperationType.Recycle)); } } else if (returnStatus == ReturnResult.InProgress) { if (permanently) { // deleting items from <x> return(OngoingTasksViewModel.PostOperationBanner(string.Empty, string.Format(source.Count() > 1 ? "StatusDeletingItemsDetails_Plural".GetLocalized() : "StatusDeletingItemsDetails_Singular".GetLocalized(), source.Count(), sourceDir), 0, ReturnResult.InProgress, FileOperationType.Delete, new CancellationTokenSource())); } else { // "Moving items from <x> to recycle bin" return(OngoingTasksViewModel.PostOperationBanner(string.Empty, string.Format(source.Count() > 1 ? "StatusMovingItemsDetails_Plural".GetLocalized() : "StatusMovingItemsDetails_Singular".GetLocalized(), source.Count(), sourceDir, "TheRecycleBin".GetLocalized()), 0, ReturnResult.InProgress, FileOperationType.Recycle, new CancellationTokenSource())); } } else if (returnStatus == ReturnResult.Success) { if (permanently) { return(OngoingTasksViewModel.PostBanner( "StatusDeletionComplete".GetLocalized(), string.Format(source.Count() > 1 ? "StatusDeletedItemsDetails_Plural".GetLocalized() : "StatusDeletedItemsDetails_Singular".GetLocalized(), source.Count(), sourceDir, itemsDeleted), 0, ReturnResult.Success, FileOperationType.Delete)); } else { return(OngoingTasksViewModel.PostBanner( "StatusRecycleComplete".GetLocalized(), string.Format(source.Count() > 1 ? "StatusMovedItemsDetails_Plural".GetLocalized() : "StatusMovedItemsDetails_Singular".GetLocalized(), source.Count(), sourceDir, "TheRecycleBin".GetLocalized()), 0, ReturnResult.Success, FileOperationType.Recycle)); } } else { if (permanently) { return(OngoingTasksViewModel.PostBanner( "StatusDeletionFailed".GetLocalized(), string.Format(source.Count() > 1 ? "StatusDeletionFailedDetails_Plural".GetLocalized() : "StatusDeletionFailedDetails_Singular".GetLocalized(), source.Count(), sourceDir), 0, ReturnResult.Failed, FileOperationType.Delete)); } else { return(OngoingTasksViewModel.PostBanner( "StatusRecycleFailed".GetLocalized(), string.Format(source.Count() > 1 ? "StatusMoveFailedDetails_Plural".GetLocalized() : "StatusMoveFailedDetails_Singular".GetLocalized(), source.Count(), sourceDir, "TheRecycleBin".GetLocalized()), 0, ReturnResult.Failed, FileOperationType.Recycle)); } } }
private static async Task <FilesystemResult> OpenFile(string path, IShellPage associatedInstance, IEnumerable <string> selectItems, ShellLinkItem shortcutInfo, bool openViaApplicationPicker = false, string args = default) { var opened = (FilesystemResult)false; bool isHiddenItem = NativeFileOperationsHelper.HasFileAttribute(path, System.IO.FileAttributes.Hidden); bool isShortcutItem = path.EndsWith(".lnk", StringComparison.Ordinal) || path.EndsWith(".url", StringComparison.Ordinal) || !string.IsNullOrEmpty(shortcutInfo.TargetPath); if (isShortcutItem) { if (string.IsNullOrEmpty(shortcutInfo.TargetPath)) { await Win32Helpers.InvokeWin32ComponentAsync(path, associatedInstance, args); } else { if (!path.EndsWith(".url", StringComparison.Ordinal)) { StorageFileWithPath childFile = await associatedInstance.FilesystemViewModel.GetFileWithPathFromPathAsync(shortcutInfo.TargetPath); if (childFile != null) { // Add location to Recent Items List if (childFile.Item is SystemStorageFile) { await App.RecentItemsManager.AddToRecentItems(childFile.Path); } } } await Win32Helpers.InvokeWin32ComponentAsync(shortcutInfo.TargetPath, associatedInstance, $"{args} {shortcutInfo.Arguments}", shortcutInfo.RunAsAdmin, shortcutInfo.WorkingDirectory); } opened = (FilesystemResult)true; } else if (isHiddenItem) { await Win32Helpers.InvokeWin32ComponentAsync(path, associatedInstance, args); } else { opened = await associatedInstance.FilesystemViewModel.GetFileWithPathFromPathAsync(path) .OnSuccess(async childFile => { // Add location to Recent Items List if (childFile.Item is SystemStorageFile) { await App.RecentItemsManager.AddToRecentItems(childFile.Path); } if (openViaApplicationPicker) { LauncherOptions options = new LauncherOptions { DisplayApplicationPicker = true }; if (!await Launcher.LaunchFileAsync(childFile.Item, options)) { var connection = await AppServiceConnectionHelper.Instance; if (connection != null) { await connection.SendMessageAsync(new ValueSet() { { "Arguments", "InvokeVerb" }, { "FilePath", path }, { "Verb", "openas" } }); } } } else { //try using launcher first bool launchSuccess = false; BaseStorageFileQueryResult fileQueryResult = null; //Get folder to create a file query (to pass to apps like Photos, Movies & TV..., needed to scroll through the folder like what Windows Explorer does) BaseStorageFolder currentFolder = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(path)); if (currentFolder != null) { QueryOptions queryOptions = new QueryOptions(CommonFileQuery.DefaultQuery, null); //We can have many sort entries SortEntry sortEntry = new SortEntry() { AscendingOrder = associatedInstance.InstanceViewModel.FolderSettings.DirectorySortDirection == SortDirection.Ascending }; //Basically we tell to the launched app to follow how we sorted the files in the directory. var sortOption = associatedInstance.InstanceViewModel.FolderSettings.DirectorySortOption; switch (sortOption) { case SortOption.Name: sortEntry.PropertyName = "System.ItemNameDisplay"; queryOptions.SortOrder.Clear(); queryOptions.SortOrder.Add(sortEntry); break; case SortOption.DateModified: sortEntry.PropertyName = "System.DateModified"; queryOptions.SortOrder.Clear(); queryOptions.SortOrder.Add(sortEntry); break; case SortOption.DateCreated: sortEntry.PropertyName = "System.DateCreated"; queryOptions.SortOrder.Clear(); queryOptions.SortOrder.Add(sortEntry); break; //Unfortunately this is unsupported | Remarks: https://docs.microsoft.com/en-us/uwp/api/windows.storage.search.queryoptions.sortorder?view=winrt-19041 //case Enums.SortOption.Size: //sortEntry.PropertyName = "System.TotalFileSize"; //queryOptions.SortOrder.Clear(); //queryOptions.SortOrder.Add(sortEntry); //break; //Unfortunately this is unsupported | Remarks: https://docs.microsoft.com/en-us/uwp/api/windows.storage.search.queryoptions.sortorder?view=winrt-19041 //case Enums.SortOption.FileType: //sortEntry.PropertyName = "System.FileExtension"; //queryOptions.SortOrder.Clear(); //queryOptions.SortOrder.Add(sortEntry); //break; //Handle unsupported default: //keep the default one in SortOrder IList break; } var options = new LauncherOptions(); if (currentFolder.AreQueryOptionsSupported(queryOptions)) { fileQueryResult = currentFolder.CreateFileQueryWithOptions(queryOptions); options.NeighboringFilesQuery = fileQueryResult.ToStorageFileQueryResult(); } // Now launch file with options. var storageItem = (StorageFile)await FilesystemTasks.Wrap(() => childFile.Item.ToStorageFileAsync().AsTask()); if (storageItem != null) { launchSuccess = await Launcher.LaunchFileAsync(storageItem, options); } } if (!launchSuccess) { await Win32Helpers.InvokeWin32ComponentAsync(path, associatedInstance, args); } } }); } return(opened); }
public static async Task CreateFolderWithSelectionAsync(IShellPage associatedInstance) { try { var items = associatedInstance.SlimContentPage.SelectedItems.ToList().Select((item) => StorageHelpers.FromPathAndType( item.ItemPath, item.PrimaryItemAttribute == StorageItemTypes.File ? FilesystemItemType.File : FilesystemItemType.Directory)); var folder = await CreateFileFromDialogResultTypeForResult(AddItemDialogItemType.Folder, null, associatedInstance); if (folder == null) { return; } await associatedInstance.FilesystemHelpers.MoveItemsAsync(items, items.Select(x => PathNormalization.Combine(folder.Path, x.Name)), false, true); } catch (Exception ex) { App.Logger.Warn(ex); } }