Example #1
0
        public async void DeleteItemWithStatus(StorageDeleteOption deleteOption)
        {
            var deleteFromRecycleBin = AppInstance.FilesystemViewModel.WorkingDirectory.StartsWith(App.AppSettings.RecycleBinPath);

            if (deleteFromRecycleBin)
            {
                // Permanently delete if deleting from recycle bin
                deleteOption = StorageDeleteOption.PermanentDelete;
            }

            PostedStatusBanner bannerResult = null;

            if (deleteOption == StorageDeleteOption.PermanentDelete)
            {
                bannerResult = AppInstance.BottomStatusStripControl.OngoingTasksControl.PostBanner(null,
                                                                                                   AppInstance.FilesystemViewModel.WorkingDirectory,
                                                                                                   0,
                                                                                                   StatusBanner.StatusBannerSeverity.Ongoing,
                                                                                                   StatusBanner.StatusBannerOperation.Delete);
            }
            else
            {
                bannerResult = AppInstance.BottomStatusStripControl.OngoingTasksControl.PostBanner(null,
                                                                                                   AppInstance.FilesystemViewModel.WorkingDirectory,
                                                                                                   0,
                                                                                                   StatusBanner.StatusBannerSeverity.Ongoing,
                                                                                                   StatusBanner.StatusBannerOperation.Recycle);
            }

            Stopwatch sw = new Stopwatch();

            sw.Start();

            var res = await DeleteItemAsync(deleteOption, AppInstance, bannerResult.Progress);

            bannerResult.Remove();
            sw.Stop();
            if (!res)
            {
                if (res.ErrorCode == FilesystemErrorCode.ERROR_UNAUTHORIZED)
                {
                    bannerResult.Remove();
                    AppInstance.BottomStatusStripControl.OngoingTasksControl.PostBanner(
                        "AccessDeniedDeleteDialog/Title".GetLocalized(),
                        "AccessDeniedDeleteDialog/Text".GetLocalized(),
                        0,
                        StatusBanner.StatusBannerSeverity.Error,
                        StatusBanner.StatusBannerOperation.Delete);
                }
                else if (res.ErrorCode == FilesystemErrorCode.ERROR_NOTFOUND)
                {
                    bannerResult.Remove();
                    AppInstance.BottomStatusStripControl.OngoingTasksControl.PostBanner(
                        "FileNotFoundDialog/Title".GetLocalized(),
                        "FileNotFoundDialog/Text".GetLocalized(),
                        0,
                        StatusBanner.StatusBannerSeverity.Error,
                        StatusBanner.StatusBannerOperation.Delete);
                }
                else if (res.ErrorCode == FilesystemErrorCode.ERROR_INUSE)
                {
                    bannerResult.Remove();
                    AppInstance.BottomStatusStripControl.OngoingTasksControl.PostActionBanner(
                        "FileInUseDeleteDialog/Title".GetLocalized(),
                        "FileInUseDeleteDialog/Text".GetLocalized(),
                        "FileInUseDeleteDialog/PrimaryButtonText".GetLocalized(),
                        "FileInUseDeleteDialog/SecondaryButtonText".GetLocalized(), () => { DeleteItemWithStatus(deleteOption); });
                }
            }
            else if (sw.Elapsed.TotalSeconds >= 10)
            {
                if (deleteOption == StorageDeleteOption.PermanentDelete)
                {
                    AppInstance.BottomStatusStripControl.OngoingTasksControl.PostBanner(
                        "Deletion Complete",
                        "The operation has completed.",
                        0,
                        StatusBanner.StatusBannerSeverity.Success,
                        StatusBanner.StatusBannerOperation.Delete);
                }
                else
                {
                    AppInstance.BottomStatusStripControl.OngoingTasksControl.PostBanner(
                        "Recycle Complete",
                        "The operation has completed.",
                        0,
                        StatusBanner.StatusBannerSeverity.Success,
                        StatusBanner.StatusBannerOperation.Recycle);
                }
            }

            AppInstance.NavigationToolbar.CanGoForward = false;
        }
Example #2
0
        public async Task DecompressArchive()
        {
            BaseStorageFile archive = await StorageHelpers.ToStorageItem <BaseStorageFile>(associatedInstance.SlimContentPage.SelectedItem.ItemPath);

            if (archive != null)
            {
                DecompressArchiveDialog          decompressArchiveDialog    = new();
                DecompressArchiveDialogViewModel decompressArchiveViewModel = new(archive);
                decompressArchiveDialog.ViewModel = decompressArchiveViewModel;

                ContentDialogResult option = await decompressArchiveDialog.ShowAsync();

                if (option == ContentDialogResult.Primary)
                {
                    // Check if archive still exists
                    if (!StorageHelpers.Exists(archive.Path))
                    {
                        return;
                    }

                    CancellationTokenSource extractCancellation = new();
                    PostedStatusBanner      banner = App.OngoingTasksViewModel.PostOperationBanner(
                        string.Empty,
                        "ExtractingArchiveText".GetLocalized(),
                        0,
                        ReturnResult.InProgress,
                        FileOperationType.Extract,
                        extractCancellation);

                    BaseStorageFolder destinationFolder     = decompressArchiveViewModel.DestinationFolder;
                    string            destinationFolderPath = decompressArchiveViewModel.DestinationFolderPath;

                    if (destinationFolder == null)
                    {
                        BaseStorageFolder parentFolder = await StorageHelpers.ToStorageItem <BaseStorageFolder>(Path.GetDirectoryName(archive.Path));

                        destinationFolder = await FilesystemTasks.Wrap(() => parentFolder.CreateFolderAsync(Path.GetFileName(destinationFolderPath), CreationCollisionOption.GenerateUniqueName).AsTask());
                    }
                    if (destinationFolder == null)
                    {
                        return; // Could not create dest folder
                    }

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

                    await ZipHelpers.ExtractArchive(archive, destinationFolder, banner.Progress, extractCancellation.Token);

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

                    if (sw.Elapsed.TotalSeconds >= 6)
                    {
                        App.OngoingTasksViewModel.PostBanner(
                            "ExtractingCompleteText".GetLocalized(),
                            "ArchiveExtractionCompletedSuccessfullyText".GetLocalized(),
                            0,
                            ReturnResult.Success,
                            FileOperationType.Extract);
                    }

                    if (decompressArchiveViewModel.OpenDestinationFolderOnCompletion)
                    {
                        await NavigationHelpers.OpenPath(destinationFolderPath, associatedInstance, FilesystemItemType.Directory);
                    }
                }
            }
        }
Example #3
0
        public static async void CutItem(IShellPage associatedInstance)
        {
            DataPackage dataPackage = new DataPackage()
            {
                RequestedOperation = DataPackageOperation.Move
            };
            ConcurrentBag <IStorageItem> items = new ConcurrentBag <IStorageItem>();

            if (associatedInstance.SlimContentPage.IsItemSelected)
            {
                // First, reset DataGrid Rows that may be in "cut" command mode
                associatedInstance.SlimContentPage.ItemManipulationModel.RefreshItemsOpacity();

                var itemsCount            = associatedInstance.SlimContentPage.SelectedItems.Count;
                PostedStatusBanner banner = itemsCount > 50 ? App.OngoingTasksViewModel.PostOperationBanner(
                    string.Empty,
                    string.Format("StatusPreparingItemsDetails_Plural".GetLocalized(), itemsCount),
                    0,
                    ReturnResult.InProgress,
                    FileOperationType.Prepare, new CancellationTokenSource()) : null;

                try
                {
                    var dispatcherQueue = DispatcherQueue.GetForCurrentThread();
                    await associatedInstance.SlimContentPage.SelectedItems.ToList().ParallelForEachAsync(async listedItem =>
                    {
                        if (banner != null)
                        {
                            ((IProgress <float>)banner.Progress).Report(items.Count / (float)itemsCount * 100);
                        }

                        // FTP don't support cut, fallback to copy
                        if (listedItem is not FtpItem)
                        {
                            _ = dispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, () =>
                            {
                                // Dim opacities accordingly
                                listedItem.Opacity = Constants.UI.DimItemOpacity;
                            });
                        }
                        if (listedItem is FtpItem ftpItem)
                        {
                            if (ftpItem.PrimaryItemAttribute is StorageItemTypes.File or StorageItemTypes.Folder)
                            {
                                items.Add(await ftpItem.ToStorageItem());
                            }
                        }
                        else if (listedItem.PrimaryItemAttribute == StorageItemTypes.File || listedItem is ZipItem)
                        {
                            var result = await associatedInstance.FilesystemViewModel.GetFileFromPathAsync(listedItem.ItemPath)
                                         .OnSuccess(t => items.Add(t));
                            if (!result)
                            {
                                throw new IOException($"Failed to process {listedItem.ItemPath}.", (int)result.ErrorCode);
                            }
                        }
                        else
                        {
                            var result = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(listedItem.ItemPath)
                                         .OnSuccess(t => items.Add(t));
                            if (!result)
                            {
                                throw new IOException($"Failed to process {listedItem.ItemPath}.", (int)result.ErrorCode);
                            }
                        }
                    }, 10, banner?.CancellationToken ?? default);
                }
                catch (Exception ex)
                {
                    if (ex.HResult == (int)FileSystemStatusCode.Unauthorized)
                    {
                        // Try again with fulltrust process
                        var connection = await AppServiceConnectionHelper.Instance;
                        if (connection != null)
                        {
                            string filePaths = string.Join('|', associatedInstance.SlimContentPage.SelectedItems.Select(x => x.ItemPath));
                            AppServiceResponseStatus status = await connection.SendMessageAsync(new ValueSet()
                            {
                                { "Arguments", "FileOperation" },
                                { "fileop", "Clipboard" },
                                { "filepath", filePaths },
                                { "operation", (int)DataPackageOperation.Move }
                            });

                            if (status == AppServiceResponseStatus.Success)
                            {
                                banner?.Remove();
                                return;
                            }
                        }
                    }
                    associatedInstance.SlimContentPage.ItemManipulationModel.RefreshItemsOpacity();
                    banner?.Remove();
                    return;
                }

                banner?.Remove();
            }

            var onlyStandard = items.All(x => x is StorageFile || x is StorageFolder || x is SystemStorageFile || x is SystemStorageFolder);

            if (onlyStandard)
            {
                items = new ConcurrentBag <IStorageItem>(await items.ToStandardStorageItemsAsync());
            }
            if (!items.Any())
            {
                return;
            }
            dataPackage.Properties.PackageFamilyName = Windows.ApplicationModel.Package.Current.Id.FamilyName;
            dataPackage.SetStorageItems(items, false);
            try
            {
                Clipboard.SetContent(dataPackage);
            }
            catch
            {
                dataPackage = null;
            }
        }
Example #4
0
        public static async Task CopyItem(IShellPage associatedInstance)
        {
            DataPackage dataPackage = new DataPackage()
            {
                RequestedOperation = DataPackageOperation.Copy
            };
            ConcurrentBag <IStorageItem> items = new ConcurrentBag <IStorageItem>();

            if (associatedInstance.SlimContentPage.IsItemSelected)
            {
                var itemsCount            = associatedInstance.SlimContentPage.SelectedItems.Count;
                PostedStatusBanner banner = itemsCount > 50 ? App.OngoingTasksViewModel.PostOperationBanner(
                    string.Empty,
                    string.Format("StatusPreparingItemsDetails_Plural".GetLocalized(), itemsCount),
                    0,
                    ReturnResult.InProgress,
                    FileOperationType.Prepare, new CancellationTokenSource()) : null;

                try
                {
                    await associatedInstance.SlimContentPage.SelectedItems.ToList().ParallelForEach(async listedItem =>
                    {
                        if (banner != null)
                        {
                            ((IProgress <float>)banner.Progress).Report(items.Count / (float)itemsCount * 100);
                        }

                        if (listedItem is FtpItem ftpItem)
                        {
                            if (listedItem.PrimaryItemAttribute == StorageItemTypes.File)
                            {
                                items.Add(await new FtpStorageFile(ftpItem).ToStorageFileAsync());
                            }
                            else if (listedItem.PrimaryItemAttribute == StorageItemTypes.Folder)
                            {
                                items.Add(new FtpStorageFolder(ftpItem));
                            }
                        }
                        else if (listedItem.PrimaryItemAttribute == StorageItemTypes.File || listedItem is ZipItem)
                        {
                            var result = await associatedInstance.FilesystemViewModel.GetFileFromPathAsync(listedItem.ItemPath)
                                         .OnSuccess(t => items.Add(t));
                            if (!result)
                            {
                                throw new IOException($"Failed to process {listedItem.ItemPath}.", (int)result.ErrorCode);
                            }
                        }
                        else
                        {
                            var result = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(listedItem.ItemPath)
                                         .OnSuccess(t => items.Add(t));
                            if (!result)
                            {
                                throw new IOException($"Failed to process {listedItem.ItemPath}.", (int)result.ErrorCode);
                            }
                        }
                    }, 10, banner?.CancellationToken ?? default);
                }
                catch (Exception ex)
                {
                    if (ex.HResult == (int)FileSystemStatusCode.Unauthorized)
                    {
                        // Try again with fulltrust process
                        var connection = await AppServiceConnectionHelper.Instance;
                        if (connection != null)
                        {
                            string filePaths = string.Join('|', associatedInstance.SlimContentPage.SelectedItems.Select(x => x.ItemPath));
                            AppServiceResponseStatus status = await connection.SendMessageAsync(new ValueSet()
                            {
                                { "Arguments", "FileOperation" },
                                { "fileop", "Clipboard" },
                                { "filepath", filePaths },
                                { "operation", (int)DataPackageOperation.Copy }
                            });

                            if (status == AppServiceResponseStatus.Success)
                            {
                                banner?.Remove();
                                return;
                            }
                        }
                    }
                    banner?.Remove();
                    return;
                }

                banner?.Remove();
            }

            var onlyStandard = items.All(x => x is StorageFile || x is StorageFolder || x is SystemStorageFile || x is SystemStorageFolder);

            if (onlyStandard)
            {
                items = new ConcurrentBag <IStorageItem>(await items.ToStandardStorageItemsAsync());
            }
            if (!items.Any())
            {
                return;
            }
            dataPackage.Properties.PackageFamilyName = Windows.ApplicationModel.Package.Current.Id.FamilyName;
            dataPackage.SetStorageItems(items, false);
            try
            {
                Clipboard.SetContent(dataPackage);
            }
            catch
            {
                dataPackage = null;
            }
        }