Ejemplo n.º 1
0
 private async Task<FilesystemResult> PerformAdminOperation(ValueSet operation)
 {
     var elevateConfirmDialog = new Files.Dialogs.ElevateConfirmDialog();
     var elevateConfirmResult = await elevateConfirmDialog.ShowAsync();
     if (elevateConfirmResult == ContentDialogResult.Primary)
     {
         if (associatedInstance.ServiceConnection != null &&
             await associatedInstance.ServiceConnection.Elevate())
         {
             // Try again with fulltrust process (admin)
             if (associatedInstance.ServiceConnection != null)
             {
                 var (status, response) = await associatedInstance.ServiceConnection.SendMessageForResponseAsync(operation);
                 return (FilesystemResult)(status == AppServiceResponseStatus.Success
                     && response.Get("Success", false));
             }
         }
     }
     return (FilesystemResult)false;
 }
        public async Task <IStorageHistory> DeleteAsync(IStorageItemWithPath source,
                                                        IProgress <float> progress,
                                                        IProgress <FileSystemStatusCode> errorCode,
                                                        bool permanently,
                                                        CancellationToken cancellationToken)
        {
            bool deleteFromRecycleBin = recycleBinHelpers.IsPathUnderRecycleBin(source.Path);

            FilesystemResult fsResult = FileSystemStatusCode.InProgress;

            errorCode?.Report(fsResult);
            progress?.Report(0.0f);

            if (permanently)
            {
                fsResult = (FilesystemResult)NativeFileOperationsHelper.DeleteFileFromApp(source.Path);
            }
            if (!fsResult)
            {
                if (source.ItemType == FilesystemItemType.File)
                {
                    fsResult = await associatedInstance.FilesystemViewModel.GetFileFromPathAsync(source.Path)
                               .OnSuccess((t) => t.DeleteAsync(permanently ? StorageDeleteOption.PermanentDelete : StorageDeleteOption.Default).AsTask());
                }
                else if (source.ItemType == FilesystemItemType.Directory)
                {
                    fsResult = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(source.Path)
                               .OnSuccess((t) => t.DeleteAsync(permanently ? StorageDeleteOption.PermanentDelete : StorageDeleteOption.Default).AsTask());
                }
            }

            errorCode?.Report(fsResult);

            if (fsResult == FileSystemStatusCode.Unauthorized)
            {
                // Try again with fulltrust process (non admin: for shortcuts and hidden files)
                if (associatedInstance.ServiceConnection != null)
                {
                    var(status, response) = await associatedInstance.ServiceConnection.SendMessageForResponseAsync(new ValueSet()
                    {
                        { "Arguments", "FileOperation" },
                        { "fileop", "DeleteItem" },
                        { "filepath", source.Path },
                        { "permanently", permanently }
                    });

                    fsResult = (FilesystemResult)(status == AppServiceResponseStatus.Success &&
                                                  response.Get("Success", false));
                }
                if (!fsResult)
                {
                    var elevateConfirmDialog = new Files.Dialogs.ElevateConfirmDialog();
                    var elevateConfirmResult = await elevateConfirmDialog.ShowAsync();

                    if (elevateConfirmResult == ContentDialogResult.Primary)
                    {
                        if (await associatedInstance.ServiceConnection?.Elevate()) // TODO: enable this
                        {
                            // Try again with fulltrust process (admin)
                            if (associatedInstance.ServiceConnection != null)
                            {
                                var(status, response) = await associatedInstance.ServiceConnection.SendMessageForResponseAsync(new ValueSet()
                                {
                                    { "Arguments", "FileOperation" },
                                    { "fileop", "DeleteItem" },
                                    { "filepath", source.Path },
                                    { "permanently", permanently }
                                });

                                fsResult = (FilesystemResult)(status == AppServiceResponseStatus.Success &&
                                                              response.Get("Success", false));
                            }
                        }
                    }
                }
            }
            else if (fsResult == FileSystemStatusCode.InUse)
            {
                // TODO: retry or show dialog
                await DialogDisplayHelper.ShowDialogAsync("FileInUseDeleteDialog/Title".GetLocalized(), "FileInUseDeleteDialog/Text".GetLocalized());
            }

            if (deleteFromRecycleBin)
            {
                // Recycle bin also stores a file starting with $I for each item
                string iFilePath = Path.Combine(Path.GetDirectoryName(source.Path), Path.GetFileName(source.Path).Replace("$R", "$I"));
                await associatedInstance.FilesystemViewModel.GetFileFromPathAsync(iFilePath)
                .OnSuccess(t => t.DeleteAsync(StorageDeleteOption.PermanentDelete).AsTask());
            }
            errorCode?.Report(fsResult);
            progress?.Report(100.0f);

            if (fsResult)
            {
                await associatedInstance.FilesystemViewModel.RemoveFileOrFolderAsync(source.Path);

                if (!permanently)
                {
                    // Enumerate Recycle Bin
                    List <ShellFileItem> items = await recycleBinHelpers.EnumerateRecycleBin();

                    List <ShellFileItem> nameMatchItems = new List <ShellFileItem>();

                    // Get name matching files
                    if (items != null)
                    {
                        if (Path.GetExtension(source.Path) == ".lnk" || Path.GetExtension(source.Path) == ".url") // We need to check if it is a shortcut file
                        {
                            nameMatchItems = items.Where((item) => item.FilePath == Path.Combine(Path.GetDirectoryName(source.Path), Path.GetFileNameWithoutExtension(source.Path))).ToList();
                        }
                        else
                        {
                            nameMatchItems = items.Where((item) => item.FilePath == source.Path).ToList();
                        }
                    }

                    // Get newest file
                    ShellFileItem item = nameMatchItems.Where((item) => item.RecycleDate != null).OrderBy((item) => item.RecycleDate).FirstOrDefault();

                    return(new StorageHistory(FileOperationType.Recycle, source, StorageItemHelpers.FromPathAndType(item?.RecyclePath, source.ItemType)));
                }

                return(new StorageHistory(FileOperationType.Delete, source, null));
            }
            else
            {
                // Stop at first error
                return(null);
            }
        }