protected virtual void OnCompleted(FileOperationEventArgs e)
        {
            EventHandler <FileOperationEventArgs> handler = Completed;

            if (handler != null)
            {
                handler(this, e);
            }
        }
        protected virtual void OnQueryDecryptionPassphrase(FileOperationEventArgs e)
        {
            EventHandler <FileOperationEventArgs> handler = QueryDecryptionPassphrase;

            if (handler != null)
            {
                handler(this, e);
            }
        }
        protected virtual void OnWipeQueryConfirmation(FileOperationEventArgs e)
        {
            EventHandler <FileOperationEventArgs> handler = WipeQueryConfirmation;

            if (handler != null)
            {
                handler(this, e);
            }
        }
        protected virtual void OnKnownKeyAdded(FileOperationEventArgs e)
        {
            EventHandler <FileOperationEventArgs> handler = KnownKeyAdded;

            if (handler != null)
            {
                handler(this, e);
            }
        }
        protected virtual void OnQuerySharedPublicKeys(FileOperationEventArgs e)
        {
            EventHandler <FileOperationEventArgs> handler = QuerySharedPublicKeys;

            if (handler != null)
            {
                handler(this, e);
            }
        }
        protected virtual void OnQuerySaveFileAs(FileOperationEventArgs e)
        {
            EventHandler <FileOperationEventArgs> handler = QuerySaveFileAs;

            if (handler != null)
            {
                handler(this, e);
            }
        }
        private bool OpenAxCryptDocument(string fullName, FileOperationEventArgs e)
        {
            e.AxCryptDocument = null;
            try
            {
                IRuntimeFileInfo source = OS.Current.FileInfo(fullName);
                e.OpenFileFullName = source.FullName;
                foreach (AesKey key in _fileSystemState.KnownKeys.Keys)
                {
                    e.AxCryptDocument = AxCryptFile.Document(source, key, new ProgressContext());
                    if (e.AxCryptDocument.PassphraseIsValid)
                    {
                        break;
                    }
                    e.AxCryptDocument.Dispose();
                    e.AxCryptDocument = null;
                }

                Passphrase passphrase;
                while (e.AxCryptDocument == null)
                {
                    OnQueryDecryptionPassphrase(e);
                    if (e.Cancel)
                    {
                        e.Status = FileOperationStatus.Canceled;
                        return(false);
                    }
                    passphrase        = new Passphrase(e.Passphrase);
                    e.AxCryptDocument = AxCryptFile.Document(source, passphrase.DerivedPassphrase, new ProgressContext());
                    if (!e.AxCryptDocument.PassphraseIsValid)
                    {
                        e.AxCryptDocument.Dispose();
                        e.AxCryptDocument = null;
                        continue;
                    }
                    e.Key = passphrase.DerivedPassphrase;
                    OnKnownKeyAdded(e);
                }
            }
            catch (IOException ioex)
            {
                if (e.AxCryptDocument != null)
                {
                    e.AxCryptDocument.Dispose();
                    e.AxCryptDocument = null;
                }
                FileOperationStatus status = ioex is FileNotFoundException ? FileOperationStatus.FileDoesNotExist : FileOperationStatus.Exception;
                e.Status = status;
                return(false);
            }
            return(true);
        }
        private static bool TryFindDecryptionKey(IDataStore fileInfo, FileOperationEventArgs e)
        {
            Guid          cryptoId;
            LogOnIdentity logOnIdentity = fileInfo.TryFindPassphrase(out cryptoId);

            if (logOnIdentity == null)
            {
                return(false);
            }

            e.CryptoId      = cryptoId;
            e.LogOnIdentity = logOnIdentity;
            return(true);
        }
        private static bool InfoFromDecryptedDocument(IDataStore sourceFileInfo, FileOperationEventArgs e)
        {
            EncryptedProperties properties = New <AxCryptFile>().CreateEncryptedProperties(sourceFileInfo, e.LogOnIdentity);

            if (!properties.IsValid)
            {
                return(false);
            }

            e.CryptoId = properties.DecryptionParameter.CryptoId;
            IDataStore destination = New <IDataStore>(Resolve.Portable.Path().Combine(Resolve.Portable.Path().GetDirectoryName(sourceFileInfo.FullName), properties.FileMetaData.FileName));

            e.SaveFileFullName = destination.FullName;
            e.AxCryptFile      = sourceFileInfo;
            return(true);
        }
 protected virtual void OnKnownKeyAdded(FileOperationEventArgs e)
 {
     EventHandler<FileOperationEventArgs> handler = KnownKeyAdded;
     if (handler != null)
     {
         handler(this, e);
     }
 }
 /// <summary>
 /// Create a new instance, reporting progress
 /// </summary>
 /// <param name="fileSystemState">The current FileSystemStatem instance</param>
 /// <param name="progress">The instance of ProgressContext to report progress via</param>
 public FileOperationsController(FileSystemState fileSystemState, ProgressContext progress)
 {
     _eventArgs = new FileOperationEventArgs();
     _fileSystemState = fileSystemState;
     _progress = progress;
 }
        private bool OpenAxCryptDocument(string fullName, FileOperationEventArgs e)
        {
            e.AxCryptDocument = null;
            try
            {
                IRuntimeFileInfo source = OS.Current.FileInfo(fullName);
                e.OpenFileFullName = source.FullName;
                foreach (AesKey key in _fileSystemState.KnownKeys.Keys)
                {
                    e.AxCryptDocument = AxCryptFile.Document(source, key, new ProgressContext());
                    if (e.AxCryptDocument.PassphraseIsValid)
                    {
                        break;
                    }
                    e.AxCryptDocument.Dispose();
                    e.AxCryptDocument = null;
                }

                Passphrase passphrase;
                while (e.AxCryptDocument == null)
                {
                    OnQueryDecryptionPassphrase(e);
                    if (e.Cancel)
                    {
                        e.Status = FileOperationStatus.Canceled;
                        return false;
                    }
                    passphrase = new Passphrase(e.Passphrase);
                    e.AxCryptDocument = AxCryptFile.Document(source, passphrase.DerivedPassphrase, new ProgressContext());
                    if (!e.AxCryptDocument.PassphraseIsValid)
                    {
                        e.AxCryptDocument.Dispose();
                        e.AxCryptDocument = null;
                        continue;
                    }
                    e.Key = passphrase.DerivedPassphrase;
                    OnKnownKeyAdded(e);
                }
            }
            catch (IOException ioex)
            {
                if (e.AxCryptDocument != null)
                {
                    e.AxCryptDocument.Dispose();
                    e.AxCryptDocument = null;
                }
                FileOperationStatus status = ioex is FileNotFoundException ? FileOperationStatus.FileDoesNotExist : FileOperationStatus.Exception;
                e.Status = status;
                return false;
            }
            return true;
        }
 protected virtual void OnWipeQueryConfirmation(FileOperationEventArgs e)
 {
     EventHandler<FileOperationEventArgs> handler = WipeQueryConfirmation;
     if (handler != null)
     {
         handler(this, e);
     }
 }
 protected virtual void OnCompleted(FileOperationEventArgs e)
 {
     EventHandler<FileOperationEventArgs> handler = Completed;
     if (handler != null)
     {
         handler(this, e);
     }
 }
 private void HandleQueryDecryptionPassphraseEvent(object sender, FileOperationEventArgs e)
 {
     string passphraseText = AskForDecryptPassphrase();
     if (passphraseText == null)
     {
         e.Cancel = true;
         return;
     }
     e.Passphrase = passphraseText;
 }
 /// <summary>
 /// Create a new instance, reporting progress
 /// </summary>
 /// <param name="progress">The instance of ProgressContext to report progress via</param>
 public FileOperationsController(IProgressContext progress)
 {
     _eventArgs = new FileOperationEventArgs();
     _progress  = progress;
 }
        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();
            }
        }
 protected virtual async Task OnKnownKeyAdded(FileOperationEventArgs e)
 {
     await(KnownKeyAdded?.ExecuteAsync(e) ?? Constant.CompletedTask);
 }
 /// <summary>
 /// Create a new instance, reporting progress
 /// </summary>
 /// <param name="fileSystemState">The current FileSystemStatem instance</param>
 /// <param name="progress">The instance of ProgressContext to report progress via</param>
 public FileOperationsController(FileSystemState fileSystemState, ProgressContext progress)
 {
     _eventArgs       = new FileOperationEventArgs();
     _fileSystemState = fileSystemState;
     _progress        = progress;
 }
 protected virtual void OnQueryEncryptionPassphrase(FileOperationEventArgs e)
 {
     EventHandler<FileOperationEventArgs> handler = QueryEncryptionPassphrase;
     if (handler != null)
     {
         handler(this, e);
     }
 }
 protected virtual void OnQuerySaveFileAs(FileOperationEventArgs e)
 {
     EventHandler<FileOperationEventArgs> handler = QuerySaveFileAs;
     if (handler != null)
     {
         handler(this, e);
     }
 }
 protected virtual Task OnQueryDecryptionPassphrase(FileOperationEventArgs e)
 {
     return(QueryDecryptionPassphrase?.Invoke(e));
 }