Beispiel #1
0
 public BeGlobalWindowViewModel(BeGlobalWindow mainWindow, BeGlobalTranslationOptions options, bool tellMeAction)
 {
     LoginViewModel    = new LoginViewModel(options);
     SettingsViewModel = new SettingsViewModel(options);
     Options           = options;
     if (options != null)
     {
         var mtModel = SettingsViewModel.TranslationOptions.FirstOrDefault(m => m.Model.Equals(options.Model));
         if (mtModel != null)
         {
             var selectedModelIndex = SettingsViewModel.TranslationOptions.IndexOf(mtModel);
             SettingsViewModel.SelectedModelOption = SettingsViewModel.TranslationOptions[selectedModelIndex];
         }
         else if (SettingsViewModel.TranslationOptions.Count.Equals(0))
         {
             var translationModel = new TranslationModel
             {
                 Model       = options.Model,
                 DisplayName = options.Model
             };
             SettingsViewModel.TranslationOptions.Add(translationModel);
             SettingsViewModel.SelectedModelOption = translationModel;
         }
     }
     _mainWindow   = mainWindow;
     _tellMeAction = tellMeAction;
     _normalizeSourceTextHelper = new NormalizeSourceTextHelper();
 }
Beispiel #2
0
        public LoginViewModel(BeGlobalTranslationOptions options)
        {
            Options = options;

            _loginMethod = !string.IsNullOrEmpty(Options.AuthenticationMethod)? Options.AuthenticationMethod : Constants.APICredentials;
            SetAuthentications();
        }
        public override void Execute()
        {
            var currentProject      = SdlTradosStudio.Application?.GetController <ProjectsController>().CurrentProject;
            var settings            = currentProject?.GetTranslationProviderConfiguration();
            var translationProvider = settings?.Entries?.FirstOrDefault(entry =>
                                                                        entry.MainTranslationProvider.Uri.OriginalString.Contains("sdlmachinetranslationcloudprovider"));

            if (translationProvider != null)
            {
                var uri           = translationProvider.MainTranslationProvider?.Uri;
                var languagePairs = GetProjectLanguagePairs(currentProject);
                var options       = new BeGlobalTranslationOptions(uri);
                var beGlobalVm    = new BeGlobalWindowViewModel(options, languagePairs, null);
                beGlobalVm.BeGlobalWindow.DataContext = beGlobalVm;
                beGlobalVm.BeGlobalWindow.ShowDialog();
                if (beGlobalVm.BeGlobalWindow.DialogResult.HasValue && beGlobalVm.BeGlobalWindow.DialogResult.Value)
                {
                    settings.Entries
                    .Find(entry =>
                          entry.MainTranslationProvider.Uri.OriginalString.Contains("sdlmachinetranslationcloudprovider"))
                    .MainTranslationProvider.Uri = options.Uri;

                    currentProject.UpdateTranslationProviderConfiguration(settings);
                }
            }
        }
Beispiel #4
0
        public LoginViewModel(
            BeGlobalTranslationOptions options,
            LanguagePair[] languagePairs,
            LanguageMappingsViewModel languageMappingsViewModel,
            BeGlobalWindowViewModel beGlobalWindowViewModel)
        {
            _normalizeSourceTextHelper = new NormalizeSourceTextHelper();
            _languagePairs             = languagePairs;
            LanguageMappingsViewModel  = languageMappingsViewModel;
            BeGlobalWindowViewModel    = beGlobalWindowViewModel;
            Options = options;

            AuthenticationOptions = new List <Authentication>
            {
                new Authentication
                {
                    DisplayName = Constants.ClientAuthentication,
                    Type        = Constants.Client
                },
                new Authentication
                {
                    DisplayName = Constants.UserAuthentication,
                    Type        = Constants.User
                }
            };
            SelectedOption = options.UseClientAuthentication ? AuthenticationOptions[0] : AuthenticationOptions[1];
            LoginMethod    = SelectedOption.Type;
            GetEngines();
        }
Beispiel #5
0
        public BeGlobalWindowViewModel(BeGlobalWindow mainWindow, BeGlobalTranslationOptions options,
                                       TranslationProviderCredential credentialStore, LanguagePair[] languagePairs)
        {
            LoginViewModel             = new LoginViewModel(options);
            SettingsViewModel          = new SettingsViewModel(options);
            Options                    = options;
            _mainWindow                = mainWindow;
            _languagePairs             = languagePairs;
            _normalizeSourceTextHelper = new NormalizeSourceTextHelper();

            if (credentialStore == null)
            {
                return;
            }
            if (options.UseClientAuthentication)
            {
                _mainWindow.LoginTab.ClientKeyBox.Password    = options.ClientId;
                _mainWindow.LoginTab.ClientSecretBox.Password = options.ClientSecret;
            }
            else
            {
                LoginViewModel.Email = options.ClientId;
                _mainWindow.LoginTab.PasswordBox.Password = options.ClientSecret;
            }
        }
        public override void Execute()
        {
            var currentProject      = SdlTradosStudio.Application.GetController <ProjectsController>().CurrentProject;
            var settings            = currentProject.GetTranslationProviderConfiguration();
            var translationProvider = settings.Entries.FirstOrDefault(entry => entry.MainTranslationProvider.Uri.OriginalString.Contains("beglobaltranslationprovider"));

            if (translationProvider != null)
            {
                var uri            = translationProvider.MainTranslationProvider.Uri;
                var options        = new BeGlobalTranslationOptions(uri);
                var beGlobalWindow = new BeGlobalWindow();
                var beGlobalVm     = new BeGlobalWindowViewModel(beGlobalWindow, options, true);
                beGlobalWindow.DataContext = beGlobalVm;
                beGlobalWindow.ShowDialog();
                if (beGlobalWindow.DialogResult.HasValue && beGlobalWindow.DialogResult.Value)
                {
                    settings.Entries
                    .Find(entry =>
                          entry.MainTranslationProvider.Uri.OriginalString.Contains("beglobaltranslationprovider"))
                    .MainTranslationProvider.Uri = options.Uri;

                    currentProject.UpdateTranslationProviderConfiguration(settings);
                }
            }
        }
Beispiel #7
0
        public BeGlobalWindowViewModel(BeGlobalWindow mainWindow, BeGlobalTranslationOptions options, LanguagePair[] languagePairs)
        {
            Options                    = options;
            _mainWindow                = mainWindow;
            _languagePairs             = languagePairs;
            _normalizeSourceTextHelper = new NormalizeSourceTextHelper();
            TranslationOptions         = new ObservableCollection <TranslationModel>();

            var beGlobalTranslator = new BeGlobalV4Translator(Options?.Model);
            var accountId          = beGlobalTranslator.GetUserInformation();

            var subscriptionInfo = beGlobalTranslator.GetLanguagePairs(accountId.ToString());

            GetEngineModels(subscriptionInfo.LanguagePairs);
            SetEngineModel();
        }
 public SettingsViewModel(BeGlobalTranslationOptions options)
 {
     TranslationOptions = new ObservableCollection <TranslationModel>();
     MessageVisibility  = "Collapsed";
     if (options != null)
     {
         ReSendChecked = options.ResendDrafts;
         if (options.Model != null)
         {
             var model = TranslationOptions.FirstOrDefault(m => m.Model.Equals(options.Model));
             if (model != null)
             {
                 var selectedModelIndex = TranslationOptions.IndexOf(model);
                 SelectedModelOption = TranslationOptions[selectedModelIndex];
             }
         }
     }
 }
        public LanguageMappingsViewModel(BeGlobalTranslationOptions options)
        {
            Options            = options;
            TranslationOptions = new ObservableCollection <TranslationModel>();

            if (Options != null)
            {
                ReSendChecked = options.ResendDrafts;
                if (options.Model != null)
                {
                    var model = TranslationOptions.FirstOrDefault(m => m.Model.Equals(options.Model));
                    if (model != null)
                    {
                        var selectedModelIndex = TranslationOptions.IndexOf(model);
                        SelectedModelOption = TranslationOptions[selectedModelIndex];
                    }
                }
            }
        }
Beispiel #10
0
 public LoginViewModel(BeGlobalTranslationOptions options)
 {
     ClientAuthVisibility  = "Collapsed";
     UserAuthVisibility    = "Visible";
     AuthenticationOptions = new List <Authentication>
     {
         new Authentication
         {
             DisplayName = "Client Authentication",
             Type        = "Client"
         },
         new Authentication
         {
             DisplayName = "User Authentication",
             Type        = "User"
         }
     };
     SelectedOption = options.UseClientAuthentication ? AuthenticationOptions[0] : AuthenticationOptions[1];
 }
Beispiel #11
0
 public BeGlobalV4Translator(string server, BeGlobalTranslationOptions options)
 {
     try
     {
         _messageBoxService = new MessageBoxService();
         _flavor            = options.Model;
         _client            = new RestClient(string.Format($"{server}/v4"));
         IRestRequest request;
         if (options.UseClientAuthentication)
         {
             request = new RestRequest("/token", Method.POST)
             {
                 RequestFormat = DataFormat.Json
             };
             request.AddBody(new { clientId = options.ClientId, clientSecret = options.ClientSecret });
         }
         else
         {
             request = new RestRequest("/token/user", Method.POST)
             {
                 RequestFormat = DataFormat.Json
             };
             request.AddBody(new { username = options.ClientId, password = options.ClientSecret });
         }
         AddTraceId(request);
         request.RequestFormat = DataFormat.Json;
         var response = _client.Execute(request);
         if (response.StatusCode != HttpStatusCode.OK)
         {
             throw new Exception(Constants.TokenFailed + response.Content);
         }
         dynamic json = JsonConvert.DeserializeObject(response.Content);
         _client.AddDefaultHeader("Authorization", $"Bearer {json.accessToken}");
     }
     catch (Exception ex)
     {
         Log.Logger.Error($"{Constants.BeGlobalV4Translator} {ex.Message}\n {ex.StackTrace}");
     }
 }
        public BeGlobalWindowViewModel(BeGlobalWindow mainWindow, BeGlobalTranslationOptions options,
                                       TranslationProviderCredential credentialStore, LanguagePair[] languagePairs)
        {
            LanguageMappingsViewModel = new LanguageMappingsViewModel(options);
            LoginViewModel            = new LoginViewModel(options, languagePairs, LanguageMappingsViewModel, this);
            Options     = options;
            _mainWindow = mainWindow;

            if (credentialStore == null)
            {
                return;
            }
            if (options.UseClientAuthentication)
            {
                _mainWindow.LoginTab.ClientIdBox.Password     = options.ClientId;
                _mainWindow.LoginTab.ClientSecretBox.Password = options.ClientSecret;
            }
            else
            {
                LoginViewModel.Email = options.ClientId;
                _mainWindow.LoginTab.UserPasswordBox.Password = options.ClientSecret;
            }
        }
Beispiel #13
0
        public BeGlobalWindowViewModel(BeGlobalTranslationOptions options, LanguagePair[] languagePairs, TranslationProviderCredential credentials)
        {
            SelectedTabIndex           = 0;
            Options                    = options;
            LoginViewModel             = new LoginViewModel(options);
            LanguageMappingsViewModel  = new LanguageMappingsViewModel(options);
            _languagePairs             = languagePairs;
            _normalizeSourceTextHelper = new NormalizeSourceTextHelper();
            _credentials               = credentials;

            TranslationOptions = new ObservableCollection <TranslationModel>();
            _messageBoxService = new MessageBoxService();
            if (Options != null)
            {
                ReSendChecked = options.ResendDrafts;
            }

            if (credentials == null)
            {
                return;
            }
            var credential = _credentials.Credential.Replace("sdlmachinetranslationcloudprovider:///", string.Empty);

            if (credential.Contains("#"))
            {
                var splitedCredentials = credentials.Credential.Split('#');
                if (splitedCredentials.Length == 2 && !string.IsNullOrEmpty(splitedCredentials[0]) && !string.IsNullOrEmpty(splitedCredentials[1]))
                {
                    var beGlobalTranslator = new BeGlobalV4Translator(Options, _messageBoxService, _credentials);
                    var accountId          = beGlobalTranslator.GetUserInformation();
                    var subscriptionInfo   = beGlobalTranslator.GetLanguagePairs(accountId.ToString());
                    GetEngineModels(subscriptionInfo);
                    SetEngineModel();
                    SetAuthenticationOptions();
                }
            }
        }
Beispiel #14
0
        public BeGlobalV4Translator(BeGlobalTranslationOptions beGlobalTranslationOptions, MessageBoxService messageBoxService, TranslationProviderCredential credentials)
        {
            try
            {
                _messageBoxService    = messageBoxService;
                _flavor               = beGlobalTranslationOptions.Model;
                _authenticationMethod = beGlobalTranslationOptions.AuthenticationMethod;
                _studioCredentials    = new StudioCredentials();
                _client               = new RestClient($"{Url}/v4")
                {
                    CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore)
                };

                if (!string.IsNullOrEmpty(_authenticationMethod))
                {
                    if (_authenticationMethod.Equals(Enums.GetDisplayName(Enums.LoginOptions.APICredentials)))
                    {
                        var splitedCredentials = credentials?.Credential.Split('#');
                        // the below condition is needed in case the ClientId is not set and credentials exists
                        if (string.IsNullOrEmpty(beGlobalTranslationOptions.ClientId) &&
                            splitedCredentials.Length == 2 && !string.IsNullOrEmpty(splitedCredentials[0]) && !string.IsNullOrEmpty(splitedCredentials[1]))
                        {
                            beGlobalTranslationOptions.ClientId     = splitedCredentials[0];
                            beGlobalTranslationOptions.ClientSecret = splitedCredentials[1];
                        }
                        if (!string.IsNullOrEmpty(beGlobalTranslationOptions.ClientId) && !string.IsNullOrEmpty(beGlobalTranslationOptions.ClientSecret))
                        {
                            var request = new RestRequest("/token", Method.POST)
                            {
                                RequestFormat = DataFormat.Json
                            };
                            request.AddBody(new { clientId = beGlobalTranslationOptions.ClientId, clientSecret = beGlobalTranslationOptions.ClientSecret });
                            request.RequestFormat = DataFormat.Json;
                            var response = _client.Execute(request);
                            if (response.StatusCode != HttpStatusCode.OK)
                            {
                                throw new Exception(Constants.TokenFailed + response.Content);
                            }
                            dynamic json = JsonConvert.DeserializeObject(response.Content);
                            _client.AddDefaultHeader("Authorization", $"Bearer {json.accessToken}");
                        }
                    }
                    else
                    {
                        var accessToken = string.Empty;
                        Application.Current?.Dispatcher?.Invoke(() =>
                        {
                            accessToken = _studioCredentials.GetToken();
                        });
                        accessToken = _studioCredentials.GetToken();

                        if (!string.IsNullOrEmpty(accessToken))
                        {
                            _client.AddDefaultHeader("Authorization", $"Bearer {accessToken}");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Logger.Error($"{Constants.BeGlobalV4Translator} {ex.Message}\n {ex.StackTrace}");
            }
        }
 public LanguageMappingsViewModel(BeGlobalTranslationOptions options)
 {
     MessageVisibility = "Collapsed";
 }