Example #1
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);
        }
Example #2
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);
        }
        public SearchResults[] SearchTranslationUnitsMasked(SearchSettings settings, TranslationUnit[] translationUnits, bool[] mask)
        {
            List <SearchResults> results = new List <SearchResults>();



            int i = 0;

            foreach (var tu in translationUnits)
            {
                if (mask == null || mask[i])
                {
                    if (_provider.m_userRetryWarning)
                    {
                        bool tryAgain = true;
                        while (tryAgain)
                        {
                            try
                            {
                                var result = SearchTranslationUnit(settings, tu);

                                // the system is up. don't allow any more RetryWarningForms to be shown
                                systemIsStarting  = false;
                                systemNotSelected = false;
                                systemCannotWake  = false;
                                // and if any form is still left open, close it.
                                Form UForm = null;
                                if ((UForm = IsFormAlreadyOpen(typeof(RetryWarningForm))) != null)
                                {
                                    UForm.Close();
                                }

                                results.Add(result);

                                tryAgain = false;
                            }
                            catch (InvalidOperationException ex)
                            {
                                if (ex.Message.StartsWith("Default system for this languge pair not selected."))
                                {
                                    systemNotSelected = true;
                                    lock (messageLocker)
                                    {
                                        // TODO: race condition (similar as in the large comment above)
                                        if (systemNotSelected)
                                        {
                                            // we don't pass a credential store. it is assumed that the credentials are correct.
                                            // TODO: double check if there isn't a situation when the _provider.m_profileCollection.GetProfileList() is called and the credentials are invalid
                                            DialogResult Result = MessageBox.Show("A system is not selected. Please select the default system for this language pair.", "System not selected.", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
                                            if (Result == DialogResult.OK)
                                            {
                                                _provider.DownloadProfileList(true);
                                                SettingsForm settingsForm = new SettingsForm(ref _provider, new LanguagePair[] { _languageDirection }, null);

                                                if (settingsForm.ShowDialog() == DialogResult.OK)
                                                {
                                                    systemNotSelected = false;
                                                }
                                            }
                                            else
                                            {
                                                //If user presses "X" or another process calls "close" function
                                                //end process and return "null"
                                                tryAgain = false;
                                                results.Add(null);
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    throw;
                                }
                            }
                            catch (FaultException <Fault> ex)
                            {
                                if (ex.Detail.ErrorCode == "21") // system is starting
                                {
                                    systemIsStarting = true;

                                    // allow only one thread to proceed displaying the RetryWarningForm. all other threads must wait
                                    lock (messageLocker)
                                    {
                                        /* for completeness sake:
                                         * a race condition may arise when one thread sets the systemIsStarting flag to false indicating that the next thread that passes here doesn't have to
                                         * display the RetryWarningForm but another thread sets it back to true again just before the following 'if (systemIsStarting)...' check.
                                         * a fix might be to not trust the flag and ckeck again whether the system is starting up or not exclusively in this thread before displaying the
                                         * RetryWarningForm, but this would amount to other changes in the code so we don't do that here. nothing bad happens anyway.
                                         */

                                        // check if the translation system hasn't been brought up by some other thread while this thread was waiting on the lock.
                                        // only the first thread that passes here has systemIsStarting set to true. the threads that follow don't have to display the RetryWarningForm again
                                        if (systemIsStarting)
                                        {
                                            Form UForm = null;
                                            if ((UForm = IsFormAlreadyOpen(typeof(RetryWarningForm))) != null)
                                            {
                                                UForm.Close();
                                            }
                                            RetryWarningForm warnForm = new RetryWarningForm();
                                            DialogResult     Result   = warnForm.ShowDialog();
                                            //DialogResult Result = MessageBox.Show("Automated system is starting up. Retry?", "System starting", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                                            if (Result == DialogResult.No)
                                            {
                                                _provider.m_userRetryWarning = false;
                                                tryAgain = false;
                                                results.Add(null);
                                            }
                                            else if (Result == DialogResult.Cancel)
                                            {
                                                //If user presses "X" or another process calls "close" function
                                                //end process and return "null"
                                                tryAgain = false;
                                                results.Add(null);
                                            }
                                            else
                                            {
                                                systemIsStarting = false;
                                            }
                                        }
                                    }
                                }
                                else if (ex.Detail.ErrorCode == "23") // Unable to wake the system up
                                {
                                    systemCannotWake = true;
                                    lock (messageLocker)
                                    {
                                        // TODO: race condition (similar as in the large comment above)
                                        if (systemCannotWake)
                                        {
                                            DialogResult Result = MessageBox.Show("The selected system is not started. Please select a different system or start the system manually.", "Cannot wake the system.", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
                                            if (Result == DialogResult.OK)
                                            {
                                                _provider.DownloadProfileList(true);
                                                SettingsForm settingsForm = new SettingsForm(ref _provider, new LanguagePair[] { _languageDirection }, null);

                                                if (settingsForm.ShowDialog() == DialogResult.OK)
                                                {
                                                    systemCannotWake = false;
                                                }
                                            }
                                            else
                                            {
                                                //If user presses "X" or another process calls "close" function
                                                //end process and return "null"
                                                tryAgain = false;
                                                results.Add(null);
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    throw;
                                }
                            }
                        }
                    }
                    else
                    {
                        try
                        {
                            var result = SearchTranslationUnit(settings, tu);
                            results.Add(result);
                        }
                        catch (FaultException <Fault> ex)
                        {
                            if (ex.Detail.ErrorCode == "21")
                            {
                                results.Add(null);
                            }
                            else
                            {
                                //throw;
                            }
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
                else
                {
                    results.Add(null);
                }
                i++;
            }

            return(results.ToArray());
        }