/// <summary> /// This function tries to fetch the api key from the key file (if available) /// </summary> /// <returns>This function returns a bool telling the caller if a key was found</returns> public bool Fetch() { try { string[] cfg = _reader.ReadAllLines("CIDER.cfg"); Regex regex = new Regex(@"KEY:.*"); foreach (string s in cfg) { Match match = regex.Match(s); if (_reader.FileExists(s.Remove(0, 4)) & match.Success) { string[] key = _reader.ReadAllLines(s.Remove(0, 4)); _data.APIKey = key[0]; return(true); } } logger.Info("No API Key found: Maps feature not available"); return(false); } catch (IndexOutOfRangeException ex) { System.Windows.MessageBox.Show("To use all features correctly, please add a reference to a .key file containing an BingMaps API Key.", "BingMaps API Key", MessageBoxButton.OK, MessageBoxImage.Error); logger.Info(ex, "No API Key found: Maps feature not available"); return(false); } catch (Exception ex) { logger.Warn(ex, "Error whilst reading API Keys"); return(false); } }
/// <summary> /// This is the constructor for the MainWindow ViewModel /// </summary> public MainWindowViewModel(IKeyManager Manager, DataProvider data, IReader reader, IDialogCoordinator dialogCoordinator, bool IsTesting = false) { AddLicenses(); //connect delegate commands to icommand handlers _changeToHeightCommand = new DelegateCommand(OnChangeToHeight); _changeToLoadCommand = new DelegateCommand(OnChangeToLoad); _changeToAboutCommand = new DelegateCommand(OnChangeToAbout); _changeToAccelerationGraphCommand = new DelegateCommand(OnChangeToAccelerationGraph); _changeToAccelerationTimedCommand = new DelegateCommand(OnChangeToAccelerationTimed); _changeToAngleGraphCommand = new DelegateCommand(OnChangeToAngleGraph); _changeToAngleTimedCommand = new DelegateCommand(OnChangeToAngleTimed); _changeToMapRouteCommand = new DelegateCommand(OnChangeToMapRoute); _changeToMapTimedCommand = new DelegateCommand(OnChangeToMapTimed); _changeToVelocityGraphCommand = new DelegateCommand(OnChangeToVelocityGraph); _changeToVelocityTimedCommand = new DelegateCommand(OnChangeToVelocityTimed); _changeToHorizonCommand = new DelegateCommand(OnChangeToHorizonCommand); dataProvider = data; coordinator = dialogCoordinator; manager = Manager; MapEnabled = true; _mapAvailable = true; if (!reader.FileExists("CIDER.cfg")) { reader.WriteAllText("", "CIDER.cfg"); } if (!manager.Fetch()) { MapEnabled = false; _mapAvailable = false; displayApiKeyInfo = true; } if (!IsTesting) { if (ThemeManager.DetectAppStyle().Item1 == ThemeManager.GetAppTheme("BaseDark")) { var theme = ThemeManager.DetectAppStyle(); ThemeManager.ChangeAppTheme(App.Current, "BaseDark"); } } // Check the license try { LicenseWriter licenseWriter = new LicenseWriter(reader); _licenseAccepted = licenseWriter.ReadAgreementState(); LicenseManager.LicensesAccepted = _licenseAccepted; if (_licenseAccepted == false) { Licenses licenses = new Licenses(); licenses.Show(); _licenseAccepted = LicenseManager.LicensesAccepted; } } catch (Exception ex) { logger.Error(ex, "Couldn't configure License State"); _licenseAccepted = false; } ButtonState(false); MapEnabled = false; if (_licenseAccepted) { ButtonState(true); } KeyManager.MapKeyChangedEvent += KeyManager_MapKeyChangedEvent; LicenseHolder.LicenseChangedEvent += LicenseHolder_LicenseChangedEvent; }