/// <summary>
        /// Obtain the EDSM log and sync it with the local datastore
        /// </summary>
        private void edsmObtainLogClicked(object sender, RoutedEventArgs e)
        {
            IEDDIStarSystemRepository starSystemRepository = new EDDIStarSystemSqLiteRepository();
            StarMapConfiguration      starMapConfiguration = StarMapConfiguration.FromFile();

            string commanderName;

            if (String.IsNullOrEmpty(starMapConfiguration.commanderName))
            {
                // Fetch the commander name from the companion app
                CompanionAppService companionAppService = new CompanionAppService(debug);
                Commander           cmdr = companionAppService.Profile();
                if (cmdr != null && cmdr.Name != null)
                {
                    commanderName = cmdr.Name;
                }
                else
                {
                    edsmFetchLogsButton.IsEnabled = false;
                    edsmFetchLogsButton.Content   = "Companion app not configured and no name supplied; cannot obtain logs";
                    return;
                }
            }
            else
            {
                commanderName = starMapConfiguration.commanderName;
            }

            edsmFetchLogsButton.IsEnabled = false;
            edsmFetchLogsButton.Content   = "Obtaining log...";

            StarMapService starMapService = new StarMapService(starMapConfiguration.apiKey, commanderName);

            Dictionary <string, StarMapLogInfo> systems = starMapService.getStarMapLog();

            foreach (string system in systems.Keys)
            {
                EDDIStarSystem CurrentStarSystemData = starSystemRepository.GetEDDIStarSystem(system);
                if (CurrentStarSystemData == null)
                {
                    // We have no record of this system; set it up
                    CurrentStarSystemData      = new EDDIStarSystem();
                    CurrentStarSystemData.Name = system;
                    // Due to the potential large number of systems being imported we don't pull individual system data at this time
                }
                CurrentStarSystemData.TotalVisits   = systems[system].visits;
                CurrentStarSystemData.LastVisit     = systems[system].lastVisit;
                CurrentStarSystemData.PreviousVisit = systems[system].previousVisit;
                starSystemRepository.SaveEDDIStarSystem(CurrentStarSystemData);
            }

            edsmFetchLogsButton.Content = "Log obtained";
        }
        public MainWindow()
        {
            InitializeComponent();

            // Configured the EDDI tab
            EDDIConfiguration eddiConfiguration = EDDIConfiguration.FromFile();

            eddiHomeSystemText.Text    = eddiConfiguration.HomeSystem;
            eddiHomeStationText.Text   = eddiConfiguration.HomeStation;
            eddiInsuranceDecimal.Value = eddiConfiguration.Insurance;
            debug = eddiConfiguration.Debug;

            // Configure the Companion App tab
            CompanionAppCredentials companionAppCredentials = CompanionAppCredentials.FromFile();

            // See if the credentials work
            companionAppService = new CompanionAppService(debug);
            try
            {
                commander = companionAppService.Profile();
                setUpCompanionAppComplete("Your connection to the companion app is operational, Commander " + commander.Name);
            }
            catch (Exception ex)
            {
                if (companionAppService.CurrentState == CompanionAppService.State.NEEDS_LOGIN)
                {
                    // Fall back to stage 1
                    setUpCompanionAppStage1();
                }
                else if (companionAppService.CurrentState == CompanionAppService.State.NEEDS_CONFIRMATION)
                {
                    // Fall back to stage 2
                    setUpCompanionAppStage2();
                }
            }

            if (commander != null)
            {
                setShipyardFromConfiguration();
            }

            // Configure the NetLog tab
            NetLogConfiguration netLogConfiguration = NetLogConfiguration.FromFile();

            netLogPathTextBox.Text = netLogConfiguration.path;

            // Configure the EDSM tab
            StarMapConfiguration starMapConfiguration = StarMapConfiguration.FromFile();

            edsmApiKeyTextBox.Text        = starMapConfiguration.apiKey;
            edsmCommanderNameTextBox.Text = starMapConfiguration.commanderName;

            // Configure the Text-to-speech tab
            SpeechServiceConfiguration speechServiceConfiguration = SpeechServiceConfiguration.FromFile();
            List <String> speechOptions = new List <String>();

            speechOptions.Add("Windows TTS default");
            try
            {
                using (SpeechSynthesizer synth = new SpeechSynthesizer())
                {
                    foreach (InstalledVoice voice in synth.GetInstalledVoices())
                    {
                        if (voice.Enabled)
                        {
                            speechOptions.Add(voice.VoiceInfo.Name);
                        }
                    }
                }

                ttsVoiceDropDown.ItemsSource = speechOptions;
                ttsVoiceDropDown.Text        = speechServiceConfiguration.StandardVoice == null ? "Windows TTS default" : speechServiceConfiguration.StandardVoice;
            }
            catch (Exception e)
            {
                using (System.IO.StreamWriter errLog = new System.IO.StreamWriter(Environment.GetEnvironmentVariable("AppData") + @"\EDDI\speech.log", true))
                {
                    errLog.WriteLine("" + System.Threading.Thread.CurrentThread.ManagedThreadId + ": Caught exception " + e);
                }
            }
            ttsVolumeSlider.Value        = speechServiceConfiguration.Volume;
            ttsRateSlider.Value          = speechServiceConfiguration.Rate;
            ttsEffectsLevelSlider.Value  = speechServiceConfiguration.EffectsLevel;
            ttsDistortCheckbox.IsChecked = speechServiceConfiguration.DistortOnDamage;

            ttsTestShipDropDown.ItemsSource = ShipDefinitions.ShipModels;
            ttsTestShipDropDown.Text        = "Adder";
        }
 // Handle changes to the companion app tab
 private void companionAppNextClicked(object sender, RoutedEventArgs e)
 {
     // See if the user is entering their email address and password
     if (companionAppEmailText.Visibility == Visibility.Visible)
     {
         // Stage 1 of authentication - login
         companionAppService.Credentials.email    = companionAppEmailText.Text.Trim();
         companionAppService.Credentials.password = companionAppPasswordText.Password.Trim();
         try
         {
             // It is possible that we have valid cookies at this point so don't log in, but we did
             // need the credentials
             if (companionAppService.CurrentState == CompanionAppService.State.NEEDS_LOGIN)
             {
                 companionAppService.Login();
             }
             if (companionAppService.CurrentState == CompanionAppService.State.NEEDS_CONFIRMATION)
             {
                 setUpCompanionAppStage2();
             }
             else if (companionAppService.CurrentState == CompanionAppService.State.READY)
             {
                 if (commander == null)
                 {
                     commander = companionAppService.Profile();
                 }
                 setUpCompanionAppComplete("Your connection to the companion app is operational, Commander " + commander.Name);
                 setShipyardFromConfiguration();
             }
         }
         catch (EliteDangerousCompanionAppAuthenticationException ex)
         {
             companionAppText.Text = ex.Message;
         }
         catch (EliteDangerousCompanionAppErrorException ex)
         {
             companionAppText.Text = ex.Message;
         }
         catch (Exception ex)
         {
             companionAppText.Text = "Unexpected problem\r\nPlease report this at http://github.com/CmdrMcDonald/EliteDangerousDataProvider/issues\r\n" + ex;
         }
     }
     else if (companionAppCodeText.Visibility == Visibility.Visible)
     {
         // Stage 2 of authentication - confirmation
         string code = companionAppCodeText.Text.Trim();
         try
         {
             companionAppService.Confirm(code);
             // All done - see if it works
             commander = companionAppService.Profile();
             setUpCompanionAppComplete("Your connection to the companion app is operational, Commander " + commander.Name);
             setShipyardFromConfiguration();
         }
         catch (EliteDangerousCompanionAppAuthenticationException ex)
         {
             setUpCompanionAppStage1(ex.Message);
         }
         catch (EliteDangerousCompanionAppErrorException ex)
         {
             setUpCompanionAppStage1(ex.Message);
         }
         catch (Exception ex)
         {
             setUpCompanionAppStage1("Unexpected problem\r\nPlease report this at http://github.com/CmdrMcDonald/EliteDangerousDataProvider/issues\r\n" + ex);
         }
     }
 }