Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        public static async Task <bool> RenameFileItemAsync(ListedItem item, string newName, IShellPage associatedInstance)
        {
            if (item is AlternateStreamItem ads) // For alternate streams ItemName is not a substring ItemNameRaw
            {
                newName = item.ItemNameRaw.Replace(
                    item.ItemName.Substring(item.ItemName.LastIndexOf(":") + 1),
                    newName.Substring(newName.LastIndexOf(":") + 1),
                    StringComparison.Ordinal);
                newName = $"{ads.MainStreamName}:{newName}";
            }
            else
            {
                newName = item.ItemNameRaw.Replace(item.ItemName, newName, StringComparison.Ordinal);
            }
            if (item.ItemNameRaw == newName || string.IsNullOrEmpty(newName))
            {
                return(true);
            }

            ReturnResult renamed = ReturnResult.InProgress;

            if (item.PrimaryItemAttribute == StorageItemTypes.Folder)
            {
                renamed = await associatedInstance.FilesystemHelpers.RenameAsync(StorageHelpers.FromPathAndType(item.ItemPath, FilesystemItemType.Directory),
                                                                                 newName, NameCollisionOption.FailIfExists, true);
            }
            else
            {
                renamed = await associatedInstance.FilesystemHelpers.RenameAsync(StorageHelpers.FromPathAndType(item.ItemPath, FilesystemItemType.File),
                                                                                 newName, NameCollisionOption.FailIfExists, true);
            }

            if (renamed == ReturnResult.Success)
            {
                associatedInstance.ToolbarViewModel.CanGoForward = false;
                return(true);
            }
            return(false);
        }
Exemplo n.º 3
0
        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);
            }
        }