Beispiel #1
0
 /// <summary>
 /// Instantiates the variables and fills the list file content into
 /// a Dictionary collection object.
 /// </summary>
 public MtTranslationProviderLanguageDirection(MtTranslationProvider provider, LanguagePair languages, HtmlUtil htmlUtil)
 {
     _provider          = provider;
     _languageDirection = languages;
     _options           = _provider.Options;
     _htmlUtil          = htmlUtil;
 }
Beispiel #2
0
        public ITranslationProvider[] Browse(IWin32Window owner, LanguagePair[] languagePairs, ITranslationProviderCredentialStore credentialStore)
        {
            //construct options to send to form
            var loadOptions = new MtTranslationOptions();
            //get saved key if there is one and put it into options
            //get google credentials
            var getCredGt = GetMyCredentials(credentialStore, "mtenhancedprovidergt:///");

            if (getCredGt != null)
            {
                loadOptions.ApiKey           = getCredGt.Credential;
                loadOptions.PersistGoogleKey = getCredGt.Persist;
            }

            //get microsoft credentials
            var getCredMt = GetMyCredentials(credentialStore, "mtenhancedprovidermst:///");

            if (getCredMt != null)
            {
                try
                {
                    var creds = new GenericCredentials(getCredMt.Credential); //parse credential into username and password
                    loadOptions.ClientId              = creds.UserName;
                    loadOptions.ClientSecret          = creds.Password;
                    loadOptions.PersistMicrosoftCreds = getCredMt.Persist;
                }
                catch { } //swallow b/c it will just fail to fill in instead of crashing the whole program
            }

            var apiConnecter           = new ApiConnecter(loadOptions);
            var allSupportedLanguages  = ApiConnecter.SupportedLangs;
            var correspondingLanguages = languagePairs.Where(lp => allSupportedLanguages.Contains(lp.TargetCultureName.Substring(0, 2))).ToList();

            //loadOptions.LanguagesSupported = correspLanguages.ToDictionary(lp => lp.TargetCultureName, lp=>"MS Translator");
            //construct form
            var dialog = new MtProviderConfDialog(loadOptions, credentialStore, correspondingLanguages);

            //we are letting user delete creds but after testing it seems that it's ok if the individual credentials are null, b/c our method will re-add them to the credstore based on the uri
            if (dialog.ShowDialog(owner) == DialogResult.OK)
            {
                var testProvider = new MtTranslationProvider(dialog.Options);
                var apiKey       = dialog.Options.ApiKey;

                //we are setting credentials selectively based on the chosen provider to avoid saving the other if it is blank
                if (dialog.Options.SelectedProvider == MtTranslationOptions.ProviderType.GoogleTranslate)
                {
                    //set google credential
                    SetGoogleCredentials(credentialStore, apiKey, dialog.Options.PersistGoogleKey);
                }
                else if (dialog.Options.SelectedProvider == MtTranslationOptions.ProviderType.MicrosoftTranslator)
                {
                    //set mst cred
                    var creds2 = new GenericCredentials(dialog.Options.ClientId, dialog.Options.ClientSecret);
                    SetMstCredentials(credentialStore, creds2, dialog.Options.PersistMicrosoftCreds);
                }

                return(new ITranslationProvider[] { testProvider });
            }
            return(null);
        }
 /// <summary>
 /// Instantiates the variables and fills the list file content into
 /// a Dictionary collection object.
 /// </summary>
 /// <param name="provider"></param>
 /// <param name="languages"></param>
 #region "ListTranslationProviderLanguageDirection"
 public MtTranslationProviderLanguageDirection(MtTranslationProvider provider, LanguagePair languages)
 {
     #region "Instantiate"
     _provider          = provider;
     _languageDirection = languages;
     _options           = _provider.Options;
     #endregion
 }
        public ITranslationProvider CreateTranslationProvider(Uri translationProviderUri, string translationProviderState, ITranslationProviderCredentialStore credentialStore)
        {
            #region "CheckHandlesUri"
            if (!SupportsTranslationProviderUri(translationProviderUri))
            {
                throw new Exception(PluginResources.UriNotSupportedMessage);
            }
            #endregion

            //create options class based on URI passed to the method
            var loadOptions = new MtTranslationOptions(translationProviderUri);

            //start with MT...check if we are using MT
            if (loadOptions.SelectedProvider == MtTranslationOptions.ProviderType.MicrosoftTranslator)
            {
                var myUri = new Uri("mtenhancedprovidermst:///");
                if (credentialStore.GetCredential(myUri) != null)
                {
                    var credPersists = credentialStore.GetCredential(myUri).Persist;
                    var cred = new TranslationProviderCredential("", credPersists); //will this work??

                    cred = credentialStore.GetCredential(myUri); //if credential is there then just get it

                    var cred2 = new GenericCredentials(cred.Credential);//convert to generic credentials
                    //add creds to options
                    loadOptions.ClientId = cred2.UserName;
                    loadOptions.ClientSecret = cred2.Password;
                }
                else
                {
                    throw new TranslationProviderAuthenticationException();
                }
            }
            else //if we are using Google as the provider need to get API key
            {
                var myUri = new Uri("mtenhancedprovidergt:///");
                if (credentialStore.GetCredential(myUri) != null)
                {
                    var credPersists = credentialStore.GetCredential(myUri).Persist;
                    var cred = new TranslationProviderCredential("", credPersists); //will this work??

                    cred = credentialStore.GetCredential(myUri); //if credential is there then just get it
                    //add gt key to options
                    loadOptions.ApiKey = cred.Credential;
                }
                else
                {
                    throw new TranslationProviderAuthenticationException(); 
                    //throwing this exception ends up causing Studio to call MtTranslationProviderWinFormsUI.GetCredentialsFromUser();
                    //which we use to prompt the user to enter credentials
                }
            }
            
            //construct new provider with options..these options are going to include the cred.credential and the cred.persists
            var tp = new MtTranslationProvider(loadOptions);

            return tp;
        }
 /// <summary>
 /// Instantiates the variables and fills the list file content into
 /// a Dictionary collection object.
 /// </summary>
 /// <param name="provider"></param>
 /// <param name="languages"></param>
 #region "ListTranslationProviderLanguageDirection"
 public MtTranslationProviderLanguageDirection(MtTranslationProvider provider, LanguagePair languages)
 {
     #region "Instantiate"
     _provider          = provider;
     _languageDirection = languages;
     _options           = _provider.Options;
     _visitor           = new MtTranslationProviderElementVisitor(_options);
     #endregion
 }
        public ITranslationProvider CreateTranslationProvider(Uri translationProviderUri, string translationProviderState, ITranslationProviderCredentialStore credentialStore)
        {
            if (!SupportsTranslationProviderUri(translationProviderUri))
            {
                throw new Exception(PluginResources.UriNotSupportedMessage);
            }

            //create options class based on URI passed to the method
            var loadOptions     = new MtTranslationOptions(translationProviderUri);
            var regionsProvider = new RegionsProvider();

            //start with MT...check if we are using MT
            if (loadOptions.SelectedProvider == MtTranslationOptions.ProviderType.MicrosoftTranslator)
            {
                var myUri = new Uri(PluginResources.UriMs);
                if (credentialStore.GetCredential(myUri) != null)
                {
                    var cred = new TranslationProviderCredential(credentialStore.GetCredential(myUri).Credential, credentialStore.GetCredential(myUri).Persist);
                    loadOptions.ClientId = cred.Credential;
                    loadOptions.PersistMicrosoftCreds = cred.Persist;
                }
                else
                {
                    throw new TranslationProviderAuthenticationException();
                }
            }
            else //if we are using Google as the provider need to get API key
            {
                var myUri = new Uri(PluginResources.UriGt);
                if (credentialStore.GetCredential(myUri) != null)
                {
                    var cred = new TranslationProviderCredential(credentialStore.GetCredential(myUri).Credential, credentialStore.GetCredential(myUri).Persist);
                    loadOptions.ApiKey           = cred.Credential;
                    loadOptions.PersistGoogleKey = cred.Persist;
                }
                else
                {
                    throw new TranslationProviderAuthenticationException();
                    //throwing this exception ends up causing Studio to call MtTranslationProviderWinFormsUI.GetCredentialsFromUser();
                    //which we use to prompt the user to enter credentials
                }
            }

            //construct new provider with options..these options are going to include the cred.credential and the cred.persists
            var tp = new MtTranslationProvider(loadOptions, regionsProvider);

            return(tp);
        }
Beispiel #7
0
        public ITranslationProvider[] Browse(IWin32Window owner, LanguagePair[] languagePairs,
                                             ITranslationProviderCredentialStore credentialStore)
        {
            var options         = new MtTranslationOptions();
            var regionsProvider = new RegionsProvider();
            var mainWindowVm    = ShowProviderWindow(languagePairs, credentialStore, options, regionsProvider);

            if (!mainWindowVm.DialogResult)
            {
                return(null);
            }

            var provider = new MtTranslationProvider(options, regionsProvider);

            return(new ITranslationProvider[] { provider });
        }
Beispiel #8
0
 /// <summary>
 /// Instantiates the variables and fills the list file content into
 /// a Dictionary collection object.
 /// </summary>
 public MtTranslationProviderLanguageDirection(MtTranslationProvider provider, LanguagePair languages)
 {
     _provider          = provider;
     _languageDirection = languages;
     _options           = _provider.Options;
 }
Beispiel #9
0
        public ITranslationProvider CreateTranslationProvider(Uri translationProviderUri, string translationProviderState, ITranslationProviderCredentialStore credentialStore)
        {
            if (!SupportsTranslationProviderUri(translationProviderUri))
            {
                throw new Exception(PluginResources.UriNotSupportedMessage);
            }

            //create options class based on URI passed to the method
            var loadOptions     = new MtTranslationOptions(translationProviderUri);
            var regionsProvider = new RegionsProvider();
            var htmlUtil        = new HtmlUtil();

            //start with MT...check if we are using MT
            if (loadOptions.SelectedProvider == MtTranslationOptions.ProviderType.MicrosoftTranslator)
            {
                // The credential is saved with a different URI scheme than that of the plugin!
                // We will need to make this known and/or provide a workaround in identifying the credentials
                // added from the project automation API.
                // The following is a work-around which attempts to recover the credential, given various scenarios
                var credential = credentialStore.GetCredential(new Uri(PluginResources.UriMs))
                                 ?? credentialStore.GetCredential(translationProviderUri)
                                 ?? credentialStore.GetCredential(new Uri(translationProviderUri.Scheme + ":///"));
                if (credential != null)
                {
                    var cred = new TranslationProviderCredential(credential.Credential, credential.Persist);
                    loadOptions.ClientId = cred.Credential;
                    loadOptions.PersistMicrosoftCreds = cred.Persist;
                }
                else
                {
                    throw new TranslationProviderAuthenticationException();
                }
            }
            else             //if we are using Google as the provider need to get API key
            {
                // The credential is saved with a different URI scheme than that of the plugin!
                // We will need to make this known and/or provide a workaround in identifying the credentials
                // added from the project automation API.
                // The following is a work-around which attempts to recover the credential, given various scenarios
                var credential = credentialStore.GetCredential(new Uri(PluginResources.UriGt))
                                 ?? credentialStore.GetCredential(translationProviderUri)
                                 ?? credentialStore.GetCredential(new Uri(translationProviderUri.Scheme + ":///"));
                if (credential != null)
                {
                    var cred = new TranslationProviderCredential(credential.Credential, credential.Persist);
                    loadOptions.ApiKey           = cred.Credential;
                    loadOptions.PersistGoogleKey = cred.Persist;
                }
                else
                {
                    throw new TranslationProviderAuthenticationException();
                    //throwing this exception ends up causing Studio to call MtTranslationProviderWinFormsUI.GetCredentialsFromUser();
                    //which we use to prompt the user to enter credentials
                }
            }

            //construct new provider with options..these options are going to include the cred.credential and the cred.persists
            var tp = new MtTranslationProvider(loadOptions, regionsProvider, htmlUtil);

            return(tp);
        }