Introduction
It's a joy to work in a multinational company ...
As some of our friends here at work, you may choose to reveal your current location to your Skype buddies. This is nice and easy since you can just put it in your name or description and everyone will see where you are. This poses a problem should you travel frequently; it is more likely than not that your location tag will be out of sync.
As this is a something that happens to them once in a while I found it an interesting concept; fun enough to make it worth solving it.
So how does one establish his location? Short of installing a GPS on your machine, I would suggest checking your IP address and translating it based on one of the available databases.
The thought process goes as follows.
IP address discovery
First things first, how do I establish my IP address? This seems to be trivial enough at first glance, query your network adapters and get the IP, right? But more likely than not, you're behind a NAT, and addresses like 10.0.0.2 or 192.168.0.2 (being private addresses) do not help the slightest bit.
So we need to see what the address is as visible from the net. Interesting enough there is more than one service to tell you, but probably the easiest to consume seems to be http://whatismyip.org/
public static string WhatIsMyIp()
{
Uri myUri = new Uri("<a href="http:
HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(myUri);
webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0;
SLCC1; .NET CLR 2.0.50727)";
using (WebResponse webResponse = webRequest.GetResponse())
{
using (StreamReader reader = new StreamReader(
webResponse.GetResponseStream()))
{
return reader.ReadLine();
}
}
return string.Empty;
}
Seems to get the job done.
Now the IP to Geo-location translation
Oddly enough there are few services doing anything like that. Initially I've used the MaxMind's GeoIP Lite City , but pushing 10MB of a database to everyone wanting to use it is not really a nice thing to do. It might be interesting solution for you, and I would encourage you to check it out if distribution file size is not a concern.
I started to look around some more, and interesting enough, found a service that turned out just perfect. GeoIP Tool.
Not only does it auto-detect the IP but it will also mesh it with it's geographical location database. The existing code got quickly replaced by a simple:
public static GeoLocation WhatIsMyGeoLocation()
{
HttpWebRequest myWebRequest = (HttpWebRequest)HttpWebRequest.Create(
new Uri("<a href="http:
myWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0;
SLCC1; .NET CLR 2.0.50727)";
using(HttpWebResponse webResponse = (HttpWebResponse)(
myWebRequest.GetResponse()))
{
using (StreamReader reader = new StreamReader(
webResponse.GetResponseStream()))
{
XmlDocument document = new XmlDocument();
document.Load(reader);
XmlNodeList nodes = document.GetElementsByTagName("marker");
if (nodes.Count > 0)
{
XmlElement marker = nodes[0] as XmlElement;
if (marker != null)
{
GeoLocation response = new GeoLocation();
response.city = marker.GetAttribute("city");
response.country = marker.GetAttribute("country");
response.code = marker.GetAttribute("code");
response.host = marker.GetAttribute("host");
response.ip = marker.GetAttribute("ip");
response.latitude = marker.GetAttribute("lat");
response.lognitude = marker.GetAttribute("lng");
return response;
}
}
}
}
return null;
}
The Skype part
This part is fairly easy and straightforward, all you really need to do is to install Skype (make sure you install the latest version, even some earlier 3.0 versions do not seem to make the app work 100% if at all) and import it's COM interface into Visual Studio. Then all you need is available through: Interop.SKYPE4COMLib to consume it.
You may want to consult the Skype API, but I found it pretty easy to get into even without any really extensive reading done.
using SKYPE4COMLib;
namespace SkypeGeoLocation
{
public partial class MainForm : Form
{
private static Skype skype = new SkypeClass();
...
public static string SkypeName{
get
{
return skype.CurrentUserProfile.FullName;
}
set {
if (skype.CurrentUserProfile.FullName!= value)
{
skype.CurrentUserProfile.FullName = value;
}
}
}
public static string SkypeDescription{
get
{
return skype.CurrentUserProfile.MoodText;
}
set
{
if (skype.CurrentUserProfile.MoodText != value)
{
skype.CurrentUserProfile.MoodText = value;
}
}
}
public static void InitializeStructures(bool retryFailedRequests)
{
if (!skype.Client.IsRunning)
{
skype.Client.Start(true, true);
}
}
}
The Convergence - The Application
The application helps you maintain your location tag in your name so that whenever you travel to a different town your Skype name/description will reflect it.
Disclaimer: The application uses GeoIP Tool as its source of IP and geo-location. Visit their site for a wide variety of web based geo-location tools.
It does not contain any kind of tracking abilities other than it letting you to maintain your location tag. The application will not even change your location tag by itself but rather it will notify you that your location has changed and will allow you to change your tag with a simple press of a button.
Usage
In your Skype account set your name or description so that it has [] in it or press "Add location tab to..." in the application. Whenever the application will find that it will check if the text in the parenthesis matches your current location. If it does not it will suggest a new one and will highlight the relevant field red. You may set for this operation to be performed on every boot, you will however, only be notified when/if your location actually changed.