Beispiel #1
0
        public virtual async Task <FileOperationContext> OpenAndLaunchApplication(IEnumerable <LogOnIdentity> identities, IDataStore encryptedDataStore, IProgressContext progress)
        {
            if (identities == null)
            {
                throw new ArgumentNullException(nameof(identities));
            }
            if (encryptedDataStore == null)
            {
                throw new ArgumentNullException(nameof(encryptedDataStore));
            }
            if (progress == null)
            {
                throw new ArgumentNullException(nameof(progress));
            }

            if (!encryptedDataStore.IsAvailable)
            {
                if (Resolve.Log.IsWarningEnabled)
                {
                    Resolve.Log.LogWarning("Tried to open non-existing '{0}'.".InvariantFormat(encryptedDataStore.FullName));
                }
                return(new FileOperationContext(encryptedDataStore.FullName, ErrorStatus.FileDoesNotExist));
            }

            ActiveFile activeFile = _fileSystemState.FindActiveFileFromEncryptedPath(encryptedDataStore.FullName);

            if (activeFile == null || !activeFile.DecryptedFileInfo.IsAvailable)
            {
                activeFile = TryDecryptToActiveFile(encryptedDataStore, identities);
            }
            else
            {
                activeFile = CheckKeysForAlreadyDecryptedFile(activeFile, identities, progress);
            }

            if (activeFile == null)
            {
                return(new FileOperationContext(encryptedDataStore.FullName, ErrorStatus.InvalidKey));
            }

            using (FileLock destinationLock = New <FileLocker>().Acquire(activeFile.DecryptedFileInfo))
            {
                if (!activeFile.DecryptedFileInfo.IsAvailable)
                {
                    activeFile = Decrypt(activeFile.Identity, activeFile.EncryptedFileInfo, destinationLock, activeFile, progress);
                }
                _fileSystemState.Add(activeFile);
                await _fileSystemState.Save();

                if (encryptedDataStore.IsWriteProtected || !New <LicensePolicy>().Capabilities.Has(LicenseCapability.EditExistingFiles))
                {
                    activeFile.DecryptedFileInfo.IsWriteProtected = true;
                }

                FileOperationContext status = await LaunchApplicationForDocument(activeFile, destinationLock);

                return(status);
            }
        }
        private async Task InvokeAsync <T>(IEnumerable <T> files, Func <T, IProgressContext, Task <FileOperationContext> > workAsync, Func <FileOperationContext, Task> allCompleteAsync)
        {
            WorkerGroupProgressContext groupProgress = new WorkerGroupProgressContext(new CancelProgressContext(new ProgressContext()), New <ISingleThread>());

            await New <IProgressBackground>().WorkAsync(nameof(DoFilesAsync),
                                                        async(IProgressContext progress) =>
            {
                progress.NotifyLevelStart();

                FileOperationContext result = await Task.Run(async() =>
                {
                    FileOperationContext context = null;

                    foreach (T file in files)
                    {
                        try
                        {
                            context = await workAsync(file, progress);
                        }
                        catch (Exception ex) when(ex is OperationCanceledException)
                        {
                            return(new FileOperationContext(file.ToString(), ErrorStatus.Canceled));
                        }
                        catch (Exception ex) when(ex is AxCryptException)
                        {
                            AxCryptException ace = ex as AxCryptException;
                            New <IReport>().Exception(ace);
                            return(new FileOperationContext(ace.DisplayContext.Default(file), ace.InnerException?.Message ?? ace.Message, ace.ErrorStatus));
                        }
                        catch (Exception ex)
                        {
                            New <IReport>().Exception(ex);
                            return(new FileOperationContext(file.ToString(), ex.Message, ErrorStatus.Exception));
                        }
                        if (context.ErrorStatus != ErrorStatus.Success)
                        {
                            return(context);
                        }
                        progress.Totals.AddFileCount(1);
                    }
                    return(new FileOperationContext(progress.Totals));
                });
                progress.NotifyLevelFinished();
                return(result);
            },
                                                        (FileOperationContext status) => allCompleteAsync(status),
                                                        groupProgress).Free();
        }
        private async Task <bool> OpenAxCryptDocumentAsync(IDataStore sourceFileInfo, FileOperationEventArgs e)
        {
            _progress.NotifyLevelStart();
            try
            {
                e.OpenFileFullName = sourceFileInfo.FullName;
                if (TryFindDecryptionKey(sourceFileInfo, e))
                {
                    return(InfoFromDecryptedDocument(sourceFileInfo, e));
                }

                while (true)
                {
                    await OnQueryDecryptionPassphrase(e);

                    if (e.Cancel)
                    {
                        e.Status = new FileOperationContext(sourceFileInfo.FullName, ErrorStatus.Canceled);
                        return(false);
                    }
                    if (e.Skip)
                    {
                        e.Status = new FileOperationContext(String.Empty, ErrorStatus.Success);
                        return(true);
                    }
                    if (InfoFromDecryptedDocument(sourceFileInfo, e))
                    {
                        await OnKnownKeyAdded(e);

                        return(true);
                    }
                }
            }
            catch (IOException ioex)
            {
                New <IReport>().Exception(ioex);
                FileOperationContext status = new FileOperationContext(sourceFileInfo.FullName, ioex.Message, ioex.IsFileOrDirectoryNotFound() ? ErrorStatus.FileDoesNotExist : ErrorStatus.Exception);
                e.Status = status;
                return(false);
            }
            finally
            {
                _progress.NotifyLevelFinished();
            }
        }
        private async Task BackgroundWorkWithProgressOnUIThreadAsync(string name, Func <IProgressContext, Task <FileOperationContext> > workAsync, Func <FileOperationContext, Task> completeAsync, IProgressContext progress)
        {
            ProgressBackgroundEventArgs e = new ProgressBackgroundEventArgs(progress);

            try
            {
                Interlocked.Increment(ref _workerCount);
                OnOperationStarted(e);

                FileOperationContext result = await Task.Run(() => workAsync(progress));
                await completeAsync(result);
            }
            finally
            {
                OnOperationCompleted(e);
                Interlocked.Decrement(ref _workerCount);
            }
        }
 public FileOperationEventArgs()
 {
     Status = new FileOperationContext(String.Empty, ErrorStatus.Unknown);
 }