private void DetectLocationButton_Click(object sender, RoutedEventArgs e) { Mouse.OverrideCursor = Cursors.Wait; AutoLocation autoLocation = Common.DetectLocation(); if (!autoLocation.Success) { System.Windows.MessageBox.Show(autoLocation.Errortext, "Error while detecting location", MessageBoxButton.OK, MessageBoxImage.Error); } else { Settings.Default.RedshiftLatitude = autoLocation.Latitude; Settings.Default.RedshiftLongitude = autoLocation.Longitude; } Mouse.OverrideCursor = null; }
public static AutoLocation DetectLocation() { Main.WriteLogMessage("Detecting location via API.", DebugConsole.LogType.Info); AutoLocation returnValue = new AutoLocation(); try { Main.WriteLogMessage($"Pinging {Main.GEO_API_DOMAIN}", DebugConsole.LogType.Info); PingReply pingReply = new Ping().Send(Main.GEO_API_DOMAIN, 5000); if (pingReply.Status != IPStatus.Success) { Main.WriteLogMessage("API is not reachable.", DebugConsole.LogType.Error); returnValue.Success = false; returnValue.Errortext = $"Location provider is not reachable.{Environment.NewLine}Please make sure that your internet connection works properly and try again in a few minutes."; return(returnValue); } if (IPAddress.IsLoopback(pingReply.Address) || pingReply.Address.Equals(IPAddress.Any)) { Main.WriteLogMessage("API is routed to localhost.", DebugConsole.LogType.Error); returnValue.Success = false; returnValue.Errortext = $"The location provider is blocked by your proxy or hosts-file.{Environment.NewLine}Please insert your location manually or allow connections to {Main.GEO_API_DOMAIN}."; return(returnValue); } } catch (PingException) { Main.WriteLogMessage("API is not reachable.", DebugConsole.LogType.Error); returnValue.Success = false; returnValue.Errortext = $"Location provider is not reachable.{Environment.NewLine}Please make sure that your internet connection works properly and try again in a few minutes."; return(returnValue); } HttpWebResponse response = null; StreamReader reader = null; string latitude; string longitude; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Main.GEO_API_TARGET); request.Proxy = null; response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode != HttpStatusCode.OK) { Main.WriteLogMessage("A server side error occured.", DebugConsole.LogType.Error); returnValue.Success = false; returnValue.Errortext = $"An error on the server side of the location provider occured.{Environment.NewLine}Please try again later."; return(returnValue); } reader = new StreamReader(response.GetResponseStream()); if (reader.EndOfStream || reader.ReadLine() != "success") { Main.WriteLogMessage("A server side error occured.", DebugConsole.LogType.Error); returnValue.Success = false; returnValue.Errortext = $"An error on the server side of the location provider occured.{Environment.NewLine}Please try again later."; return(returnValue); } latitude = reader.ReadLine(); longitude = reader.ReadLine(); } catch (WebException) { Main.WriteLogMessage("A server side error occured.", DebugConsole.LogType.Error); returnValue.Success = false; returnValue.Errortext = $"An error on the server side of the location provider occured.{Environment.NewLine}Please try again later."; return(returnValue); } finally { response?.Close(); reader?.Close(); } Main.WriteLogMessage("Location detected", DebugConsole.LogType.Info); returnValue.Success = true; returnValue.Latitude = decimal.Parse(latitude, System.Globalization.NumberStyles.Float, new CultureInfo("en-US")); returnValue.Longitude = decimal.Parse(longitude, System.Globalization.NumberStyles.Float, new CultureInfo("en-US")); return(returnValue); }