Ejemplo n.º 1
0
        /// <summary>
        /// If the plug-in settings can be changed by the user, SDL Trados Studio will display a Settings button.
        /// By clicking this button, users raise the plug-in user interface, in which they can modify any applicable settings, in our implementation
        /// the delimiter character and the list file name.
        /// </summary>
        /// <param name="owner"></param>
        /// <param name="translationProvider"></param>
        /// <param name="languagePairs"></param>
        /// <param name="credentialStore"></param>
        /// <returns></returns>
        public bool Edit(IWin32Window owner, ITranslationProvider translationProvider, LanguagePair[] languagePairs, ITranslationProviderCredentialStore credentialStore)
        {
            LetsMTTranslationProvider editProvider = translationProvider as LetsMTTranslationProvider;

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

            editProvider.DownloadProfileList(true);

            SettingsForm settings = new SettingsForm(ref editProvider, languagePairs, credentialStore);

            if (!settings.TranslationProviderInitialized)
            {
                return(false);
            }

            DialogResult dResult = settings.ShowDialog(owner);

            if (dResult == DialogResult.OK)
            {
                return(true);
            }

            return(false);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Checks whether the given translation provider has a working connection to the translation API.
        /// If not then display the password form and re-initialized the translation provider.
        /// </summary>
        /// <param name="editProvider">The translation provider to be checked.</param>
        /// <param name="credentialStore">The credential store that should contain</param>
        /// <returns>Returns true if the credential store already has the respective credentials or if the translation provider is successfully re-initialized. Returns false otherwise.</returns>
        private bool CheckTranslationProviderCredentials(LetsMTTranslationProvider editProvider, ITranslationProviderCredentialStore credentialStore)
        {
            if (credentialStore.GetCredential(editProvider.Uri) != null)
            {
                return(true);
            }

            PasswordForm pf = new PasswordForm();

            while (pf.ShowDialog(this) == DialogResult.OK)
            {
                //TODO: check how to minimize the amount odfsystem list calls
                string credentials = string.Format("{0}\t{1}", pf.strToken, pf.strAppId);
                TranslationProviderCredential tc = new TranslationProviderCredential(credentials, pf.bRemember);
                credentialStore.AddCredential(editProvider.Uri, tc);

                // Create a new connection to the translation API
                editProvider.InitService(pf.strToken);

                if (!LetsMTTranslationProviderWinFormsUI.ValidateTranslationProviderLocaly(editProvider))
                {
                    //IF USERNAME INFOREC REMOVE DATA FROM STORE
                    credentialStore.RemoveCredential(editProvider.Uri);
                }
                else
                {
                    editProvider.DownloadProfileList(true);
                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 3
0
        public ITranslationProvider CreateTranslationProvider(Uri translationProviderUri, string translationProviderState, ITranslationProviderCredentialStore credentialStore)
        {
            if (translationProviderUri == null || credentialStore == null) //Throw allowed exception if no arguments. TODO: translationProviderState, optional?
            {
                throw new ArgumentNullException("Missing arguments.");
            }

            if (!SupportsTranslationProviderUri(translationProviderUri)) //Wrong Uri, Trados is probably looking for another provider
            {
                throw new Exception("Cannot handle URI.");
            }

            //TranslationProviderCredential credentialData = credentialStore.GetCredential(translationProviderUri); //Make sure we have credentials, if not, throw exception to ask user
            //if(credentialData == null)
            //    throw new TranslationProviderAuthenticationException();

            //string credential = credentialData.Credential; //Get the credentials in form "{0}\t{1}\t{3}", where 0 - username, 1 - password and 3 - appId


            LetsMTTranslationProvider translationProvider = new LetsMTTranslationProvider(credentialStore, translationProviderUri, 85); //Create the provider passing required parameters

            //if(!translationProvider.ValidateCredentials()) //If credentials are incorrect, ask again
            //  throw new TranslationProviderAuthenticationException();

            translationProvider.LoadState(translationProviderState);

            return(translationProvider); //Provider is good and user is authorized, return
        }
Ejemplo n.º 4
0
        public static bool ValidateTranslationProviderLocaly(LetsMTTranslationProvider testProvider)
        {
            bool bCredentialsValid = true;

            try
            {
                testProvider.m_service.GetUserInfo(testProvider.m_strAppID);
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("The HTTP request is unauthorized"))
                {
                    MessageBox.Show("Invalid Client ID.", "Validation error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    bCredentialsValid = false;
                }
                else if (ex.Message.Contains("code:"))
                {
                    Form UForm = null;

                    if ((UForm = testProvider.IsFormAlreadyOpen(typeof(LimitationForm))) == null)
                    {
                        Regex          r               = new Regex(@"(?<=cide: 1)\d+");
                        Match          m               = r.Match(ex.Message);
                        string         erNum           = m.Value;
                        string         Error_url       = string.Format("https://www.letsmt.eu/Error.aspx?code={0}&user={1}", erNum, testProvider.m_service.ClientCredentials.UserName.UserName);
                        AutoResetEvent formLoadedEvent = new AutoResetEvent(false);
                        var            t               = new Thread(() => testProvider.CallForm(Error_url, formLoadedEvent));

                        t.SetApartmentState(ApartmentState.STA);
                        t.Start();
                        bCredentialsValid = false;

                        // wait until the limitationForm has loaded
                        try
                        {
                            formLoadedEvent.WaitOne();
                        }
                        catch (AbandonedMutexException aex)
                        {
                            // the limitform crashed or something. continue
                        }
                        ////close the form if it is open
                        //UForm.Close();
                    }
                }
                else
                {
                    MessageBox.Show(ex.ToString());
                    MessageBox.Show(ex.Message);
                    MessageBox.Show("Cannot connect to server.", "Validation error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    bCredentialsValid = false;
                }
            }


            return(bCredentialsValid);
        }
 public Advanced_options(ref LetsMTTranslationProvider editProvider)
 {
     m_editProvider = editProvider;
     InitializeComponent();
     mascedScoreBox.Text    = m_editProvider.m_resultScore.ToString();
     maskedTimeoutBox.Text  = m_editProvider.m_timeout.ToString();
     checkBox1.Checked      = m_editProvider.m_dynamicResultScore;
     mascedScoreBox.Enabled = !checkBox1.Checked;
     label1.Enabled         = mascedScoreBox.Enabled;
 }
Ejemplo n.º 6
0
        public SettingsForm(ref LetsMTTranslationProvider editProvider, LanguagePair[] languagePairs, ITranslationProviderCredentialStore credentialStore)
        {
            // we allow to skip the check if credential store == null. there is code other than LetsMTTranslationProviderWinFormsUI.Edit() method that calls SettingsForm
            // and it does not have easy access to the credentialStore. the alternative would be to save credentialStore somewhare and make it available ouside the method
            if (credentialStore != null && !CheckTranslationProviderCredentials(editProvider, credentialStore))
            {
                return;
            }

            TranslationProviderInitialized = true;

            m_score           = editProvider.m_resultScore;
            m_credentialStore = credentialStore;
            DialogResult      = DialogResult.Cancel;
            m_trackGoupChange = false;


            Text = PluginResources.Plugin_NiceName + " Settings";

            InitializeComponent();

            m_translationProvider = editProvider;

            LetsMTAPI.UserData userData = m_translationProvider.m_service.GetUserInfo("");

            // get teh username whitout group
            string username  = m_translationProvider.m_username;
            string userEmail = userData.email;

            if (!string.IsNullOrEmpty(userEmail))
            {
                username = userEmail;
            }

            m_username = username;



            // get teh username whitout group
            string activeGroup         = "";
            string userDataActiveGroup = userData.activeGroup;

            if (userDataActiveGroup != null)
            {
                activeGroup = userDataActiveGroup;
            }

            m_activeGroup = activeGroup;

            // since we use UserIds for logging in we should have had received only one group
            this.GroupLabel.Text = "Group: " + userData.userGroups[0].name.Trim();

            wndProfileProperties.DisplayMember = "text";
            wndProfileProperties.ValueMember   = "value";

            sourceSelectComboBox.DisplayMember = "Text";
            sourceSelectComboBox.ValueMember   = "Value";

            targetSelectComboBox.DisplayMember = "Text";
            targetSelectComboBox.ValueMember   = "Value";

            termCorporaSelectComboBox.DisplayMember = "Text";
            termCorporaSelectComboBox.ValueMember   = "Value";

            qualityEstimateTextBox.Text     = editProvider.m_minAllowedQualityEstimateScore.ToString();
            qualityEstimateCheckBox.Checked = editProvider.m_useQualityEstimates;
            qualityEstimateTextBox.Enabled  = editProvider.m_useQualityEstimates;
            qeWasCheckedWhenAvailable       = editProvider.m_useQualityEstimates;


            //fill the system list
            m_pairs        = languagePairs;
            m_checkedState = new Dictionary <string, string>();
            m_checkedTerms = new Dictionary <MyTuple <string, string>, string>();

            FillProfileList();

            m_trackGoupChange = true;
        }
 /// <summary>
 /// Instantiates the variables and fills the list file content into a Dictionary collection object.
 /// </summary>
 /// <param name="provider"></param>
 /// <param name="languages"></param>
 public LetsMTTranslationProviderLanguageDirection(LetsMTTranslationProvider provider, LanguagePair languages)
 {
     _provider          = provider;
     _languageDirection = languages;
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Show the plug-in settings form when the user is adding the translation provider plug-in through the GUI of SDL Trados Studio
        /// </summary>
        /// <param name="owner"></param>
        /// <param name="languagePairs"></param>
        /// <param name="credentialStore"></param>
        /// <returns></returns>
        public ITranslationProvider[] Browse(IWin32Window owner, LanguagePair[] languagePairs, ITranslationProviderCredentialStore credentialStore)
        {
            LetsMTTranslationProviderOptions opts = new LetsMTTranslationProviderOptions();

            int letsmtNum = 0;
            Uri letsmtUri = null;

            TranslationProviderCredential credentialData = null;

            do
            {
                letsmtNum++;
                letsmtUri      = new Uri(opts.Uri.ToString() + letsmtNum.ToString());
                credentialData = credentialStore.GetCredential(letsmtUri);
            } while (credentialData != null);

            int    lastUsedLetsmtNum = letsmtNum - 1;
            Uri    lastUsedLetsmtUri = new Uri(opts.Uri.ToString() + lastUsedLetsmtNum.ToString());
            string lastUsedToken     = null;

            if (lastUsedLetsmtNum > 0)
            {
                credentialData = credentialStore.GetCredential(lastUsedLetsmtUri);
                ApiCredential apiCredentaial = ApiCredential.ParseCredential(credentialData.Credential);
                lastUsedToken = apiCredentaial.Token;
            }

            PasswordForm pf = new PasswordForm(lastUsedToken);

            while (pf.ShowDialog(owner) == DialogResult.OK)
            {
                if (lastUsedToken != pf.strToken)
                {
                    //TODO: check how to minimize the amount odfsystem list calls
                    string credentials = string.Format("{0}\t{1}", pf.strToken, pf.strAppId);

                    TranslationProviderCredential tc = new TranslationProviderCredential(credentials, pf.bRemember);
                    //ad a new uri to handle multiple plugins and users

                    credentialStore.AddCredential(letsmtUri, tc);
                }
                else
                {
                    letsmtUri = lastUsedLetsmtUri;
                }

                LetsMTTranslationProvider testProvider = new LetsMTTranslationProvider(credentialStore, letsmtUri, 85);// (dialog.Options);

                //credentialStore.AddCredential(opts.Uri, tc);
                ////TODO: Check if we need a "testProvider"
                //LetsMTTranslationProvider testProvider = new LetsMTTranslationProvider(credentialStore, opts.Uri);// (dialog.Options);

                if (ValidateTranslationProviderLocaly(testProvider))
                {
                    Sdl.LanguagePlatform.TranslationMemoryApi.ITranslationProvider[] ResultProv = new ITranslationProvider[] { testProvider };
                    //Open system select screen emidetly for user frendlier setup
                    if (Edit(owner, ResultProv[ResultProv.Length - 1], languagePairs, credentialStore))
                    {
                        return(ResultProv);
                    }
                }
                else
                {
                    //IF USERNAME INFOREC REMOVE DATA FROM STORE
                    credentialStore.RemoveCredential(letsmtUri);
                }
            }
            return(null);
        }