// Renders a KML-File for hCard objects.  This script was derived from the vcard and googlemap tails script
// version 0.2
// 2007-08-23
//
// --------------------------------------------------------------------
//
// This is a Tails script.
//
// To install, you need Tails: http://blog.codeeg.com/tails-firefox-extension-03/
// Then restart Firefox and revisit this script and click on 'Install'.
//
// To uninstall, go to Tools/Manage Tails Scripts...,
// select "Als KML-Datei speichern", and click Uninstall.
//
// --------------------------------------------------------------------
//
// ==TailsScript==
// @name          Als KML-Datei speichern
// @namespace     http://blog.codeeg.com/tails
// @description   Erstellt eine KML-Datei für hCard + geo Objekte.
// @include       hcard
// ==/TailsScript==

getData: function() {
  if (/*(this.object.__name == 'hcard') &&*/ this.object.geo) {
     return {fileName: this.buildFileName(this.object), contents:this.buildKML(this.object)};
  } else {
     return {fileName: '', contents:''};
  }
},
buildKML: function(hcard) {
  var res='<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://earth.google.com/kml/2.1"><Document><Placemark>';
  res+="<name>"+this.toUTF8(hcard.fn)+"</name>";
  res+="<Point><coordinates>"+hcard.geo.longitude+","+hcard.geo.latitude+"</coordinates></Point>";
  res+='</Placemark></Document></kml>';
  return res;
},
buildFileName: function(hcard) {
  return (hcard.categories+"-"+hcard.locality+"-"+hcard.fn).replace(/[^a-zA-Z0-9äöüÄÖÜß_-]/g, "_")+".kml";
},
toUTF8: function(str) {
  var unicodeConverter = Components
    .classes["@mozilla.org/intl/scriptableunicodeconverter"]
    .createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
  unicodeConverter.charset = 'UTF-8';
  return unicodeConverter.ConvertFromUnicode(str);
}