Click here to Skip to main content
15,881,757 members
Articles / Mobile Apps / Windows Mobile

PlanetFinder for Windows Mobile

Rate me:
Please Sign up or sign in to vote.
4.26/5 (8 votes)
17 Dec 2008GPL37 min read 29.4K   552   30  
PlanetFinder application for Windows Mobile Smartphones
/*

Copyright (c) 2000 Benjamin Crowell,
              2008 Kostas Giannakakis. All rights reserved.

About the GPL License
	This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

using System;
using System.Globalization;
using System.Threading;
using Microsoft.Win32;

namespace PlanetFinder
{
	public class PositionSettings
	{
        private CultureInfo cultureInfo = new CultureInfo("en-US");
        
        private double longitude;
		private double latitude;
		private string positionName;

        private String app_key_name;

		public double Longitude
		{
			get { return longitude; }
            set { longitude = value; }
		}
		
		public double Latitude
		{
			get { return latitude; }
            set { latitude = value; }
		}
		
		public string PositionName
		{
			get {return positionName;}
            set { positionName = value; }
		}		

        public PositionSettings(String app_key_name, double localTimezone)
		{
            this.app_key_name = app_key_name;
            
            positionName = "";

			if (!GetStoredValues())
			{
                GetBiggestInTimezone(localTimezone);
			}
		}

        public void ChangeTimezone(double localTimezone)
        {
            GetBiggestInTimezone(localTimezone);
        }

		public void Close()
		{
			StoreValues();
		}
		
		private bool GetStoredValues()
		{
			string latStored, longStored, pointStored = "";
			
			try
			{
                latStored = (string)Registry.GetValue(app_key_name, "Latitude", "");
                longStored = (string)Registry.GetValue(app_key_name, "Longitude", "");
                pointStored = (string)Registry.GetValue(app_key_name, "Point", "");
			} catch (Exception)
			{
				return false;
			}
			
			
			if (latStored == null || latStored.Length == 0 ||
			    longStored == null || longStored.Length == 0)
			{
				return false;
			}

			double lat, lon;
			try
			{
				lat = Double.Parse(latStored, cultureInfo);
                lon = Double.Parse(longStored, cultureInfo);
			} catch (Exception)
			{
				return false;
			}
			
			if (ValidateLongitude(lon) && ValidateLatitude(lat))
			{
				latitude = lat;
				longitude = lon;
				positionName = pointStored;
				return true;
			}
			
			return false;
		}
		
		private void StoreValues()
		{
			try
			{
                Registry.SetValue(app_key_name, "Point", positionName);
                Registry.SetValue(app_key_name, "Latitude", 
				                  String.Format(cultureInfo, "{0:0.00}", latitude));
                Registry.SetValue(app_key_name, "Longitude",
				                  String.Format(cultureInfo, "{0:0.00}", longitude));
			} catch (Exception)
			{
			}			
		}
		
		private bool ValidateLongitude(double lon)
		{
			if (lon >= -180.0 && lon <= 180.0)
				return true;
			return false;
		}
		
		private bool ValidateLatitude(double lat)
		{
			if (lat >= -90.0 && lat <= 90.0)
				return true;
			return false;
		}

        private void GetBiggestInTimezone(double localTimezone)
		{			
            CityList cities = CityList.GetInstance();
			City biggest = null;
			biggest = cities.GetBiggestInTimezone(localTimezone);
			
			if (biggest != null)
			{			
				latitude = biggest.LatDegrees;
				longitude = biggest.LonDegrees;
				positionName = biggest.Name;
			}
			else
			{
				latitude = longitude = 0;
                positionName = "";
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer (Senior) Self employed
Greece Greece
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions