private void UpdateView() { if (Core.ApplicationData.Instance.ActiveGeocache == null) { GeocacheData = null; GeocacheCoordinate = null; geocacheTypeCombo.SelectedItem = null; gcDistance.Text = ""; } else { Core.Data.GeocacheData gd = new Core.Data.GeocacheData(); Core.Data.GeocacheData.Copy(Core.ApplicationData.Instance.ActiveGeocache, gd); GeocacheData = gd; GeocacheCoordinate = Utils.Conversion.GetCoordinatesPresentation(gd.Lat, gd.Lon); geocacheTypeCombo.SelectedItem = gd.GeocacheType; if (Core.ApplicationData.Instance.ActiveGeocache.GeocacheDistance == null) { gcDistance.Text = ""; } else { gcDistance.Text = Core.ApplicationData.Instance.ActiveGeocache.GeocacheDistance.ToString(); } } }
private void Button_Click_3(object sender, RoutedEventArgs e) { if (Core.ApplicationData.Instance.ActiveDatabase != null) { Core.Data.GeocacheData gc = new Core.Data.GeocacheData(); int maxId = int.MaxValue - 411120 - 100; bool found = true; string gcCode = ""; while (found) { maxId--; gcCode = Utils.Conversion.GetCacheCodeFromCacheID(maxId); found = Core.ApplicationData.Instance.ActiveDatabase.GeocacheCollection.GetGeocache(gcCode) != null; } gc.Code = gcCode; gc.Archived = false; gc.Available = true; gc.City = ""; gc.Container = Utils.DataAccess.GetGeocacheContainer(3); gc.Country = ""; gc.Difficulty = 1.0; gc.EncodedHints = ""; gc.Favorites = 0; gc.Flagged = false; gc.Found = false; gc.GeocacheType = Utils.DataAccess.GetGeocacheType(2); gc.Lat = Core.ApplicationData.Instance.CenterLocation.Lat; gc.Locked = false; gc.Lon = Core.ApplicationData.Instance.CenterLocation.Lon; gc.LongDescription = ""; gc.LongDescriptionInHtml = false; gc.MemberOnly = false; gc.Municipality = ""; gc.Name = Localization.TranslationManager.Instance.Translate("DefaultName") as string; gc.Notes = ""; gc.Owner = Core.ApplicationData.Instance.AccountInfos.GetAccountInfo("GC").AccountName; gc.OwnerId = "0"; gc.PersonalNote = ""; gc.PlacedBy = gc.Owner; gc.PublishedTime = DateTime.Now; gc.ShortDescription = ""; gc.ShortDescriptionInHtml = false; gc.State = ""; gc.Terrain = 1.0; gc.Url = ""; Utils.DataAccess.AddGeocache(Core.ApplicationData.Instance.ActiveDatabase, gc); Core.ApplicationData.Instance.ActiveGeocache = Core.ApplicationData.Instance.ActiveDatabase.GeocacheCollection.GetGeocache(gcCode); } }
public void ImportGPX(string gpxDoc) { bool isGeoSpy = false; try { string firstPart; if (gpxDoc.Length < 2000) { firstPart = gpxDoc; } else { firstPart = gpxDoc.Substring(0, 2000); } XmlDocument doc = new XmlDocument(); doc.LoadXml(gpxDoc); XmlElement root = doc.DocumentElement; XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable); if (firstPart.IndexOf("http://www.topografix.com/GPX/1/1", StringComparison.OrdinalIgnoreCase) > 0) { nsmgr.AddNamespace("x", "http://www.topografix.com/GPX/1/1"); } else { nsmgr.AddNamespace("x", "http://www.topografix.com/GPX/1/0"); } if (firstPart.IndexOf("http://geoget.ararat.cz/GpxExtensions/v2", StringComparison.OrdinalIgnoreCase) > 0) { //GeoSpy isGeoSpy = true; _cachesGpxVersion = new Version(1, 0, 0); nsmgr.AddNamespace("y", "http://www.groundspeak.com/cache/1/0"); } else if (firstPart.IndexOf("http://www.groundspeak.com/cache/1/0/2", StringComparison.OrdinalIgnoreCase) > 0) { nsmgr.AddNamespace("y", "http://www.groundspeak.com/cache/1/0/2"); _cachesGpxVersion = new Version(1, 0, 2); } else if (firstPart.IndexOf("http://www.groundspeak.com/cache/1/0/1", StringComparison.OrdinalIgnoreCase) > 0) { if (firstPart.IndexOf("creator=\"Opencaching.de - http://www.opencaching.de\"") > 0) { nsmgr.AddNamespace("y", "http://www.groundspeak.com/cache/1/0"); } else if (firstPart.IndexOf("http://www.opencaching.com/xmlschemas/opencaching/1/0") > 0) { nsmgr.AddNamespace("y", "http://www.groundspeak.com/cache/1/0"); } else { nsmgr.AddNamespace("y", "http://www.groundspeak.com/cache/1/0/1"); } _cachesGpxVersion = new Version(1, 0, 1); } else if (firstPart.IndexOf("http://www.groundspeak.com/cache/1/1", StringComparison.OrdinalIgnoreCase) > 0) { nsmgr.AddNamespace("y", "http://www.groundspeak.com/cache/1/1"); _cachesGpxVersion = new Version(1, 1, 0); } else { nsmgr.AddNamespace("y", "http://www.groundspeak.com/cache/1/0"); _cachesGpxVersion = new Version(1, 0, 0); } if (isGeoSpy) { _gpxDataTime = DateTime.Parse(root.SelectSingleNode("x:metadata", nsmgr).SelectSingleNode("x:time", nsmgr).InnerText); } else { _gpxDataTime = DateTime.Parse(root.SelectSingleNode("x:time", nsmgr).InnerText); } XmlNodeList wps = root.SelectNodes("x:wpt", nsmgr); if (wps != null) { Version V102 = new Version(1, 0, 2); foreach (XmlNode wp in wps) { XmlNode n; if (isGeoSpy) { n = wp.SelectSingleNode("x:extensions", nsmgr).SelectSingleNode("y:cache", nsmgr); } else { n = wp.SelectSingleNode("y:cache", nsmgr); } if (n == null) { //assume Child waypoint and yeah, well....multiple sources like GC or OB should be handled //TODO: check source and don't assume GC //check if it is a child waypoint by checking the name string fullwpname = wp.SelectSingleNode("x:name", nsmgr).InnerText; string wpname = fullwpname.Substring(2); Core.Data.Geocache parentGeocache = null; string parentCode; parentGeocache = (from g in Core.ApplicationData.Instance.ActiveDatabase.GeocacheCollection where g.Code.Substring(2) == wpname select g).FirstOrDefault(); if (parentGeocache == null) { //parent is not available //now what? //just add it //continue; parentCode = string.Concat("GC", wpname); } else { parentCode = parentGeocache.Code; } Core.Data.IWaypointData cwp = null; cwp = (from a in Utils.DataAccess.GetWaypointsFromGeocache(Core.ApplicationData.Instance.ActiveDatabase, parentCode) where a.Code == fullwpname select a).FirstOrDefault(); if (cwp == null) { cwp = new Core.Data.WaypointData(); } else { if (cwp.DataFromDate >= _gpxDataTime) { continue; } } cwp.Code = wp.SelectSingleNode("x:name", nsmgr).InnerText; cwp.GeocacheCode = string.Concat(parentCode.Substring(0, 2), cwp.Code.Substring(2)); cwp.DataFromDate = _gpxDataTime; cwp.Comment = wp.SelectSingleNode("x:cmt", nsmgr).InnerText; if (SafeAttributeInnerText(wp, "lat", "").Length > 0) { cwp.Lat = Utils.Conversion.StringToDouble(wp.Attributes["lat"].InnerText); cwp.Lon = Utils.Conversion.StringToDouble(wp.Attributes["lon"].InnerText); if (Math.Abs((double)cwp.Lat) < 0.0001 && Math.Abs((double)cwp.Lon) < 0.0001) { cwp.Lat = null; cwp.Lon = null; } } else { cwp.Lat = null; cwp.Lon = null; } cwp.Description = wp.SelectSingleNode("x:desc", nsmgr).InnerText; cwp.ID = cwp.Code; cwp.Name = cwp.Code; cwp.Comment = wp.SelectSingleNode("x:cmt", nsmgr).InnerText; cwp.Time = DateTime.Parse(wp.SelectSingleNode("x:time", nsmgr).InnerText); cwp.Url = SafeInnerText(wp.SelectSingleNode("x:url", nsmgr), ""); cwp.UrlName = SafeInnerText(wp.SelectSingleNode("x:urlname", nsmgr), ""); cwp.WPType = Utils.DataAccess.GetWaypointType(wp.SelectSingleNode("x:sym", nsmgr).InnerText); if (cwp is Core.Data.WaypointData) { Utils.DataAccess.AddWaypoint(Core.ApplicationData.Instance.ActiveDatabase, cwp as Core.Data.WaypointData); } continue; } string code = wp.SelectSingleNode("x:name", nsmgr).InnerText; Core.Data.IGeocacheData gc = null; gc = (from a in Core.ApplicationData.Instance.ActiveDatabase.GeocacheCollection where a.Code == code select a).FirstOrDefault(); if (gc == null) { gc = new Core.Data.GeocacheData(); } else { if (gc.DataFromDate >= _gpxDataTime) { continue; } } gc.Available = bool.Parse(n.Attributes["available"].InnerText); gc.Archived = bool.Parse(SafeInnerText(n.Attributes["archived"], "False")); if (!(gc is Core.Data.Geocache) || !(gc as Core.Data.Geocache).Locked) { gc.Lat = Utils.Conversion.StringToDouble(SafeAttributeInnerText(wp, "lat", "0.0")); gc.Lon = Utils.Conversion.StringToDouble(SafeAttributeInnerText(wp, "lon", "0.0")); gc.Code = code; gc.DataFromDate = _gpxDataTime; gc.Name = n.SelectSingleNode("y:name", nsmgr).InnerText; gc.PublishedTime = DateTime.Parse(wp.SelectSingleNode("x:time", nsmgr).InnerText); if (isGeoSpy) { gc.Url = wp.SelectSingleNode("x:link", nsmgr).Attributes["href"].Value; } else { gc.Url = SafeInnerText(wp.SelectSingleNode("x:url", nsmgr), ""); } if (SafeInnerText(wp.SelectSingleNode("x:sym", nsmgr), "").EndsWith(" Found")) { gc.Found = true; } gc.Country = SafeInnerText(n.SelectSingleNode("y:country", nsmgr), ""); gc.State = SafeInnerText(n.SelectSingleNode("y:state", nsmgr), ""); gc.OwnerId = SafeAttributeInnerText(n.SelectSingleNode("y:owner", nsmgr), "id", ""); if (_cachesGpxVersion >= V102) { gc.GeocacheType = Utils.DataAccess.GetGeocacheType(int.Parse(SafeAttributeInnerText(n.SelectSingleNode("y:type", nsmgr), "id", "-1"))); gc.Container = Utils.DataAccess.GetGeocacheContainer(int.Parse(SafeAttributeInnerText(n.SelectSingleNode("y:container", nsmgr), "id", "-1"))); gc.Favorites = int.Parse(SafeInnerText(n.SelectSingleNode("y:favorite_points", nsmgr), "0")); gc.MemberOnly = bool.Parse(SafeInnerText(n.Attributes["memberonly"], "False")); //gc.CustomCoords = bool.Parse(SafeInnerText(n.Attributes["customcoords"], "False")); gc.PersonalNote = SafeInnerText(n.Attributes["personal_note"], ""); } else { string srchTxt = SafeInnerText(n.SelectSingleNode("y:type", nsmgr), "Unknown"); if (!srchTxt.StartsWith("Groundspeak")) { if (srchTxt.Contains("Trash")) { srchTxt = "Trash"; } else { int pos = srchTxt.IndexOf(' '); if (pos > 0) { srchTxt = srchTxt.Substring(0, pos); } } } gc.GeocacheType = Utils.DataAccess.GetGeocacheType(srchTxt); if (isGeoSpy) { gc.Container = Utils.DataAccess.GetGeocacheContainer("Virtual"); } else { gc.Container = Utils.DataAccess.GetGeocacheContainer(SafeInnerText(n.SelectSingleNode("y:container", nsmgr), "Unknown")); } } gc.PlacedBy = SafeInnerText(n.SelectSingleNode("y:placed_by", nsmgr), ""); gc.Owner = SafeInnerText(n.SelectSingleNode("y:owner", nsmgr), ""); gc.Terrain = Utils.Conversion.StringToDouble(SafeInnerText(n.SelectSingleNode("y:terrain", nsmgr), "1")); gc.Difficulty = Utils.Conversion.StringToDouble(SafeInnerText(n.SelectSingleNode("y:difficulty", nsmgr), "1")); gc.ShortDescription = SafeInnerText(n.SelectSingleNode("y:short_description", nsmgr), ""); gc.ShortDescriptionInHtml = bool.Parse(SafeAttributeInnerText(n.SelectSingleNode("y:short_description", nsmgr), "html", "False")); gc.LongDescription = SafeInnerText(n.SelectSingleNode("y:long_description", nsmgr), ""); gc.LongDescriptionInHtml = bool.Parse(SafeAttributeInnerText(n.SelectSingleNode("y:long_description", nsmgr), "html", "False")); gc.EncodedHints = SafeInnerText(n.SelectSingleNode("y:encoded_hints", nsmgr), ""); gc.AttributeIds = new List<int>(); XmlNode attrs = n.SelectSingleNode("y:attributes", nsmgr); if (attrs != null && attrs.ChildNodes != null) { foreach (XmlNode attr in attrs.ChildNodes) { int attrId = int.Parse(attr.Attributes["id"].InnerText); int attrInc = int.Parse(SafeAttributeInnerText(attr, "inc", "1")); if (attrInc == 1) { gc.AttributeIds.Add(attrId); } else { gc.AttributeIds.Add(-1 * attrId); } } } bool gcAdded = true; if (gc is Core.Data.GeocacheData) { gcAdded = Utils.DataAccess.AddGeocache(Core.ApplicationData.Instance.ActiveDatabase, gc as Core.Data.GeocacheData); } if (gcAdded) { gc = (from a in Core.ApplicationData.Instance.ActiveDatabase.GeocacheCollection where a.Code == code select a).FirstOrDefault(); Utils.Calculus.SetDistanceAndAngleGeocacheFromLocation(gc as Core.Data.Geocache, Core.ApplicationData.Instance.CenterLocation); //Logs XmlNode ln = n.SelectSingleNode("y:logs", nsmgr); if (ln != null) { XmlNodeList logs = ln.SelectNodes("y:log", nsmgr); if (logs != null) { foreach (XmlNode l in logs) { string lid = SafeAttributeInnerText(l, "id", ""); if (lid.StartsWith("GL")) { //lg.ID = lid; } else { if (string.IsNullOrEmpty(lid) || lid.StartsWith("-")) { continue; } try { lid = string.Concat("GL", Utils.Conversion.GetCacheCodeFromCacheID(int.Parse(lid)).Substring(2)); } catch { continue; } } Core.Data.ILogData lg = null; lg = (from a in Utils.DataAccess.GetLogs(Core.ApplicationData.Instance.ActiveDatabase, gc.Code) where a.ID == lid select a).FirstOrDefault(); if (lg == null) { lg = new Core.Data.LogData(); } lg.ID = lid; lg.GeocacheCode = gc.Code; lg.DataFromDate = _gpxDataTime; lg.Date = DateTime.Parse(l.SelectSingleNode("y:date", nsmgr).InnerText); lg.Encoded = bool.Parse(l.SelectSingleNode("y:text", nsmgr).Attributes["encoded"].InnerText); if (isGeoSpy) { lg.Text = GAPPSF.Utils.Conversion.StripHtmlTags(System.Web.HttpUtility.HtmlDecode(l.SelectSingleNode("y:text", nsmgr).InnerText).Replace("</p>", "\r\n\r\n")); } else { lg.Text = l.SelectSingleNode("y:text", nsmgr).InnerText; } lg.Finder = l.SelectSingleNode("y:finder", nsmgr).InnerText; if (l.SelectSingleNode("y:finder", nsmgr).Attributes["id"] != null) { lg.FinderId = l.SelectSingleNode("y:finder", nsmgr).Attributes["id"].InnerText; } else { //GCTour has no finder id lg.FinderId = "1"; } if (_cachesGpxVersion >= V102) { lg.LogType = Utils.DataAccess.GetLogType(int.Parse(l.SelectSingleNode("y:type", nsmgr).Attributes["id"].InnerText)); } else { lg.LogType = Utils.DataAccess.GetLogType(l.SelectSingleNode("y:type", nsmgr).InnerText); } if (lg is Core.Data.LogData) { Utils.DataAccess.AddLog(Core.ApplicationData.Instance.ActiveDatabase, lg as Core.Data.LogData); } //log images XmlNode lni = l.SelectSingleNode("y:images", nsmgr); if (lni != null) { XmlNodeList logis = lni.SelectNodes("y:image", nsmgr); if (logis != null) { foreach (XmlNode li in logis) { string url = li.SelectSingleNode("y:url", nsmgr).InnerText; Core.Data.ILogImageData lgi = null; lgi = (from a in Utils.DataAccess.GetLogImages(Core.ApplicationData.Instance.ActiveDatabase, lg.ID) where a.ID == url select a).FirstOrDefault(); if (lgi == null) { lgi = new Core.Data.LogImageData(); } lgi.Url = url; lgi.ID = lgi.Url; lgi.LogId = lg.ID; lgi.Name = li.SelectSingleNode("y:name", nsmgr).InnerText; lgi.DataFromDate = _gpxDataTime; if (lgi is Core.Data.LogImageData) { Utils.DataAccess.AddLogImage(Core.ApplicationData.Instance.ActiveDatabase, lgi as Core.Data.LogImageData); } } } } } } } } } } } } catch(Exception e) { Core.ApplicationData.Instance.Logger.AddLog(this, e); } }
public void ImportGPX(string gpxDoc) { bool isGeoSpy = false; try { string firstPart; if (gpxDoc.Length < 2000) { firstPart = gpxDoc; } else { firstPart = gpxDoc.Substring(0, 2000); } XmlDocument doc = new XmlDocument(); doc.LoadXml(gpxDoc); XmlElement root = doc.DocumentElement; XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable); if (firstPart.IndexOf("http://www.topografix.com/GPX/1/1", StringComparison.OrdinalIgnoreCase) > 0) { nsmgr.AddNamespace("x", "http://www.topografix.com/GPX/1/1"); } else { nsmgr.AddNamespace("x", "http://www.topografix.com/GPX/1/0"); } if (firstPart.IndexOf("http://geoget.ararat.cz/GpxExtensions/v2", StringComparison.OrdinalIgnoreCase) > 0) { //GeoSpy isGeoSpy = true; _cachesGpxVersion = new Version(1, 0, 0); nsmgr.AddNamespace("y", "http://www.groundspeak.com/cache/1/0"); } else if (firstPart.IndexOf("http://www.groundspeak.com/cache/1/0/2", StringComparison.OrdinalIgnoreCase) > 0) { nsmgr.AddNamespace("y", "http://www.groundspeak.com/cache/1/0/2"); _cachesGpxVersion = new Version(1, 0, 2); } else if (firstPart.IndexOf("http://www.groundspeak.com/cache/1/0/1", StringComparison.OrdinalIgnoreCase) > 0) { if (firstPart.IndexOf("creator=\"Opencaching.de - http://www.opencaching.de\"") > 0) { nsmgr.AddNamespace("y", "http://www.groundspeak.com/cache/1/0"); } else if (firstPart.IndexOf("http://www.opencaching.com/xmlschemas/opencaching/1/0") > 0) { nsmgr.AddNamespace("y", "http://www.groundspeak.com/cache/1/0"); } else { nsmgr.AddNamespace("y", "http://www.groundspeak.com/cache/1/0/1"); } _cachesGpxVersion = new Version(1, 0, 1); } else if (firstPart.IndexOf("http://www.groundspeak.com/cache/1/1", StringComparison.OrdinalIgnoreCase) > 0) { nsmgr.AddNamespace("y", "http://www.groundspeak.com/cache/1/1"); _cachesGpxVersion = new Version(1, 1, 0); } else { nsmgr.AddNamespace("y", "http://www.groundspeak.com/cache/1/0"); _cachesGpxVersion = new Version(1, 0, 0); } if (isGeoSpy) { _gpxDataTime = DateTime.Parse(root.SelectSingleNode("x:metadata", nsmgr).SelectSingleNode("x:time", nsmgr).InnerText); } else { _gpxDataTime = DateTime.Parse(root.SelectSingleNode("x:time", nsmgr).InnerText); } XmlNodeList wps = root.SelectNodes("x:wpt", nsmgr); if (wps != null) { Version V102 = new Version(1, 0, 2); foreach (XmlNode wp in wps) { XmlNode n; if (isGeoSpy) { n = wp.SelectSingleNode("x:extensions", nsmgr).SelectSingleNode("y:cache", nsmgr); } else { n = wp.SelectSingleNode("y:cache", nsmgr); } if (n == null) { //assume Child waypoint and yeah, well....multiple sources like GC or OB should be handled //TODO: check source and don't assume GC //check if it is a child waypoint by checking the name string fullwpname = wp.SelectSingleNode("x:name", nsmgr).InnerText; string wpname = fullwpname.Substring(2); Core.Data.Geocache parentGeocache = null; string parentCode; parentGeocache = (from g in Core.ApplicationData.Instance.ActiveDatabase.GeocacheCollection where g.Code.Substring(2) == wpname select g).FirstOrDefault(); if (parentGeocache == null) { //parent is not available //now what? //just add it //continue; parentCode = string.Concat("GC", wpname); } else { parentCode = parentGeocache.Code; } Core.Data.IWaypointData cwp = null; cwp = (from a in Utils.DataAccess.GetWaypointsFromGeocache(Core.ApplicationData.Instance.ActiveDatabase, parentCode) where a.Code == fullwpname select a).FirstOrDefault(); if (cwp == null) { cwp = new Core.Data.WaypointData(); } else { if (cwp.DataFromDate >= _gpxDataTime) { continue; } } cwp.Code = wp.SelectSingleNode("x:name", nsmgr).InnerText; cwp.GeocacheCode = string.Concat(parentCode.Substring(0, 2), cwp.Code.Substring(2)); cwp.DataFromDate = _gpxDataTime; cwp.Comment = wp.SelectSingleNode("x:cmt", nsmgr).InnerText; if (SafeAttributeInnerText(wp, "lat", "").Length > 0) { cwp.Lat = Utils.Conversion.StringToDouble(wp.Attributes["lat"].InnerText); cwp.Lon = Utils.Conversion.StringToDouble(wp.Attributes["lon"].InnerText); if (Math.Abs((double)cwp.Lat) < 0.0001 && Math.Abs((double)cwp.Lon) < 0.0001) { cwp.Lat = null; cwp.Lon = null; } } else { cwp.Lat = null; cwp.Lon = null; } cwp.Description = wp.SelectSingleNode("x:desc", nsmgr).InnerText; cwp.ID = cwp.Code; cwp.Name = cwp.Code; cwp.Comment = wp.SelectSingleNode("x:cmt", nsmgr).InnerText; cwp.Time = DateTime.Parse(wp.SelectSingleNode("x:time", nsmgr).InnerText); cwp.Url = SafeInnerText(wp.SelectSingleNode("x:url", nsmgr), ""); cwp.UrlName = SafeInnerText(wp.SelectSingleNode("x:urlname", nsmgr), ""); cwp.WPType = Utils.DataAccess.GetWaypointType(wp.SelectSingleNode("x:sym", nsmgr).InnerText); if (cwp is Core.Data.WaypointData) { Utils.DataAccess.AddWaypoint(Core.ApplicationData.Instance.ActiveDatabase, cwp as Core.Data.WaypointData); } continue; } string code = wp.SelectSingleNode("x:name", nsmgr).InnerText; Core.Data.IGeocacheData gc = null; gc = (from a in Core.ApplicationData.Instance.ActiveDatabase.GeocacheCollection where a.Code == code select a).FirstOrDefault(); if (gc == null) { gc = new Core.Data.GeocacheData(); } else { if (gc.DataFromDate >= _gpxDataTime) { continue; } } gc.Available = bool.Parse(n.Attributes["available"].InnerText); gc.Archived = bool.Parse(SafeInnerText(n.Attributes["archived"], "False")); if (!(gc is Core.Data.Geocache) || !(gc as Core.Data.Geocache).Locked) { gc.Lat = Utils.Conversion.StringToDouble(SafeAttributeInnerText(wp, "lat", "0.0")); gc.Lon = Utils.Conversion.StringToDouble(SafeAttributeInnerText(wp, "lon", "0.0")); gc.Code = code; gc.DataFromDate = _gpxDataTime; gc.Name = n.SelectSingleNode("y:name", nsmgr).InnerText; gc.PublishedTime = DateTime.Parse(wp.SelectSingleNode("x:time", nsmgr).InnerText); if (isGeoSpy) { gc.Url = wp.SelectSingleNode("x:link", nsmgr).Attributes["href"].Value; } else { gc.Url = SafeInnerText(wp.SelectSingleNode("x:url", nsmgr), ""); } if (SafeInnerText(wp.SelectSingleNode("x:sym", nsmgr), "").EndsWith(" Found")) { gc.Found = true; } gc.Country = SafeInnerText(n.SelectSingleNode("y:country", nsmgr), ""); gc.State = SafeInnerText(n.SelectSingleNode("y:state", nsmgr), ""); gc.OwnerId = SafeAttributeInnerText(n.SelectSingleNode("y:owner", nsmgr), "id", ""); if (_cachesGpxVersion >= V102) { gc.GeocacheType = Utils.DataAccess.GetGeocacheType(int.Parse(SafeAttributeInnerText(n.SelectSingleNode("y:type", nsmgr), "id", "-1"))); gc.Container = Utils.DataAccess.GetGeocacheContainer(int.Parse(SafeAttributeInnerText(n.SelectSingleNode("y:container", nsmgr), "id", "-1"))); gc.Favorites = int.Parse(SafeInnerText(n.SelectSingleNode("y:favorite_points", nsmgr), "0")); gc.MemberOnly = bool.Parse(SafeInnerText(n.Attributes["memberonly"], "False")); //gc.CustomCoords = bool.Parse(SafeInnerText(n.Attributes["customcoords"], "False")); gc.PersonalNote = SafeInnerText(n.Attributes["personal_note"], ""); } else { string srchTxt = SafeInnerText(n.SelectSingleNode("y:type", nsmgr), "Unknown"); if (!srchTxt.StartsWith("Groundspeak")) { if (srchTxt.Contains("Trash")) { srchTxt = "Trash"; } else { int pos = srchTxt.IndexOf(' '); if (pos > 0) { srchTxt = srchTxt.Substring(0, pos); } } } gc.GeocacheType = Utils.DataAccess.GetGeocacheType(srchTxt); if (isGeoSpy) { gc.Container = Utils.DataAccess.GetGeocacheContainer("Virtual"); } else { gc.Container = Utils.DataAccess.GetGeocacheContainer(SafeInnerText(n.SelectSingleNode("y:container", nsmgr), "Unknown")); } } gc.PlacedBy = SafeInnerText(n.SelectSingleNode("y:placed_by", nsmgr), ""); gc.Owner = SafeInnerText(n.SelectSingleNode("y:owner", nsmgr), ""); gc.Terrain = Utils.Conversion.StringToDouble(SafeInnerText(n.SelectSingleNode("y:terrain", nsmgr), "1")); gc.Difficulty = Utils.Conversion.StringToDouble(SafeInnerText(n.SelectSingleNode("y:difficulty", nsmgr), "1")); gc.ShortDescription = SafeInnerText(n.SelectSingleNode("y:short_description", nsmgr), ""); gc.ShortDescriptionInHtml = bool.Parse(SafeAttributeInnerText(n.SelectSingleNode("y:short_description", nsmgr), "html", "False")); gc.LongDescription = SafeInnerText(n.SelectSingleNode("y:long_description", nsmgr), ""); gc.LongDescriptionInHtml = bool.Parse(SafeAttributeInnerText(n.SelectSingleNode("y:long_description", nsmgr), "html", "False")); gc.EncodedHints = SafeInnerText(n.SelectSingleNode("y:encoded_hints", nsmgr), ""); gc.AttributeIds = new List <int>(); XmlNode attrs = n.SelectSingleNode("y:attributes", nsmgr); if (attrs != null && attrs.ChildNodes != null) { foreach (XmlNode attr in attrs.ChildNodes) { int attrId = int.Parse(attr.Attributes["id"].InnerText); int attrInc = int.Parse(SafeAttributeInnerText(attr, "inc", "1")); if (attrInc == 1) { gc.AttributeIds.Add(attrId); } else { gc.AttributeIds.Add(-1 * attrId); } } } bool gcAdded = true; if (gc is Core.Data.GeocacheData) { gcAdded = Utils.DataAccess.AddGeocache(Core.ApplicationData.Instance.ActiveDatabase, gc as Core.Data.GeocacheData); } if (gcAdded) { gc = (from a in Core.ApplicationData.Instance.ActiveDatabase.GeocacheCollection where a.Code == code select a).FirstOrDefault(); Utils.Calculus.SetDistanceAndAngleGeocacheFromLocation(gc as Core.Data.Geocache, Core.ApplicationData.Instance.CenterLocation); //Logs XmlNode ln = n.SelectSingleNode("y:logs", nsmgr); if (ln != null) { XmlNodeList logs = ln.SelectNodes("y:log", nsmgr); if (logs != null) { foreach (XmlNode l in logs) { string lid = SafeAttributeInnerText(l, "id", ""); if (lid.StartsWith("GL")) { //lg.ID = lid; } else { if (string.IsNullOrEmpty(lid) || lid.StartsWith("-")) { continue; } try { lid = string.Concat("GL", Utils.Conversion.GetCacheCodeFromCacheID(int.Parse(lid)).Substring(2)); } catch { continue; } } Core.Data.ILogData lg = null; lg = (from a in Utils.DataAccess.GetLogs(Core.ApplicationData.Instance.ActiveDatabase, gc.Code) where a.ID == lid select a).FirstOrDefault(); if (lg == null) { lg = new Core.Data.LogData(); } lg.ID = lid; lg.GeocacheCode = gc.Code; lg.DataFromDate = _gpxDataTime; lg.Date = DateTime.Parse(l.SelectSingleNode("y:date", nsmgr).InnerText); lg.Encoded = bool.Parse(l.SelectSingleNode("y:text", nsmgr).Attributes["encoded"].InnerText); if (isGeoSpy) { lg.Text = GAPPSF.Utils.Conversion.StripHtmlTags(System.Web.HttpUtility.HtmlDecode(l.SelectSingleNode("y:text", nsmgr).InnerText).Replace("</p>", "\r\n\r\n")); } else { lg.Text = l.SelectSingleNode("y:text", nsmgr).InnerText; } lg.Finder = l.SelectSingleNode("y:finder", nsmgr).InnerText; if (l.SelectSingleNode("y:finder", nsmgr).Attributes["id"] != null) { lg.FinderId = l.SelectSingleNode("y:finder", nsmgr).Attributes["id"].InnerText; } else { //GCTour has no finder id lg.FinderId = "1"; } if (_cachesGpxVersion >= V102) { lg.LogType = Utils.DataAccess.GetLogType(int.Parse(l.SelectSingleNode("y:type", nsmgr).Attributes["id"].InnerText)); } else { lg.LogType = Utils.DataAccess.GetLogType(l.SelectSingleNode("y:type", nsmgr).InnerText); } if (lg is Core.Data.LogData) { Utils.DataAccess.AddLog(Core.ApplicationData.Instance.ActiveDatabase, lg as Core.Data.LogData); } //log images XmlNode lni = l.SelectSingleNode("y:images", nsmgr); if (lni != null) { XmlNodeList logis = lni.SelectNodes("y:image", nsmgr); if (logis != null) { foreach (XmlNode li in logis) { string url = li.SelectSingleNode("y:url", nsmgr).InnerText; Core.Data.ILogImageData lgi = null; lgi = (from a in Utils.DataAccess.GetLogImages(Core.ApplicationData.Instance.ActiveDatabase, lg.ID) where a.ID == url select a).FirstOrDefault(); if (lgi == null) { lgi = new Core.Data.LogImageData(); } lgi.Url = url; lgi.ID = lgi.Url; lgi.LogId = lg.ID; lgi.Name = li.SelectSingleNode("y:name", nsmgr).InnerText; lgi.DataFromDate = _gpxDataTime; if (lgi is Core.Data.LogImageData) { Utils.DataAccess.AddLogImage(Core.ApplicationData.Instance.ActiveDatabase, lgi as Core.Data.LogImageData); } } } } } } } } } } } } catch (Exception e) { Core.ApplicationData.Instance.Logger.AddLog(this, e); } }
public async Task ImportFile(Core.Storage.Database db, string filename) { using (Utils.DataUpdater upd = new DataUpdater(db)) { await Task.Run(() => { try { DateTime nextUpdate = DateTime.Now.AddSeconds(1); using (FileStream fs = File.OpenRead(filename)) using (BinaryReader br = new BinaryReader(fs)) { string tag = br.ReadString(); if (tag == "GAPP") { bool IsLittleEndian = br.ReadBoolean(); int version = br.ReadInt32(); if (IsLittleEndian == BitConverter.IsLittleEndian && version <= 3) { int count = br.ReadInt32(); using (Utils.ProgressBlock prog = new Utils.ProgressBlock("ImportGAPPDataExchangeFile", "ImportGeocaches", count, 0)) { for (int index = 0; index < count; index++) { Core.Data.GeocacheData gc = new Core.Data.GeocacheData(); gc.Archived = br.ReadBoolean(); gc.AttributeIds = ReadIntegerArray(br); gc.Available = br.ReadBoolean(); gc.City = br.ReadString(); gc.Code = br.ReadString(); gc.Container = Utils.DataAccess.GetGeocacheContainer(br.ReadInt32()); bool dummyb = br.ReadBoolean(); gc.Country = br.ReadString(); if (br.ReadBoolean()) { gc.CustomLat = br.ReadDouble(); gc.CustomLon = br.ReadDouble(); } gc.Difficulty = br.ReadDouble(); gc.EncodedHints = br.ReadString(); gc.Favorites = br.ReadInt32(); gc.Flagged = br.ReadBoolean(); gc.Found = br.ReadBoolean(); string s = br.ReadString(); gc.GeocacheType = Utils.DataAccess.GetGeocacheType(br.ReadInt32()); string dummystr = br.ReadString(); gc.Lat = br.ReadDouble(); gc.Lon = br.ReadDouble(); gc.LongDescription = br.ReadString(); gc.LongDescriptionInHtml = br.ReadBoolean(); gc.MemberOnly = br.ReadBoolean(); gc.Municipality = br.ReadString(); gc.Name = br.ReadString(); gc.Notes = br.ReadString(); gc.Owner = br.ReadString(); gc.OwnerId = br.ReadString(); gc.PersonalNote = br.ReadString(); gc.PlacedBy = br.ReadString(); gc.PublishedTime = DateTime.Parse(br.ReadString()); dummyb = br.ReadBoolean(); gc.ShortDescription = br.ReadString(); gc.ShortDescriptionInHtml = br.ReadBoolean(); gc.State = br.ReadString(); gc.Terrain = br.ReadDouble(); dummystr = br.ReadString(); gc.Url = br.ReadString(); gc.DataFromDate = DateTime.Parse(br.ReadString()); if (version > 1) { gc.Locked = br.ReadBoolean(); } bool gcAdded = Utils.DataAccess.AddGeocache(db, gc); int logcount = br.ReadInt32(); for (int lc = 0; lc < logcount; lc++) { Core.Data.LogData log = new Core.Data.LogData(); log.DataFromDate = DateTime.Parse(br.ReadString()); log.Date = DateTime.Parse(br.ReadString()); log.Encoded = br.ReadBoolean(); log.Finder = br.ReadString(); log.FinderId = br.ReadString(); log.GeocacheCode = br.ReadString(); log.ID = br.ReadString(); log.LogType = Utils.DataAccess.GetLogType(br.ReadInt32()); log.TBCode = br.ReadString(); log.Text = br.ReadString(); if (gcAdded) { Utils.DataAccess.AddLog(db, log); } int logimgcount = br.ReadInt32(); for (int lic = 0; lic < logimgcount; lic++) { Core.Data.LogImageData li = new Core.Data.LogImageData(); li.DataFromDate = DateTime.Parse(br.ReadString()); li.ID = br.ReadString(); li.LogId = br.ReadString(); li.Name = br.ReadString(); li.Url = br.ReadString(); if (gcAdded) { Utils.DataAccess.AddLogImage(db, li); } } } int wpcount = br.ReadInt32(); for (int wpc = 0; wpc < wpcount; wpc++) { Core.Data.WaypointData wp = new Core.Data.WaypointData(); wp.Code = br.ReadString(); wp.Comment = br.ReadString(); wp.DataFromDate = DateTime.Parse(br.ReadString()); wp.Description = br.ReadString(); wp.GeocacheCode = br.ReadString(); wp.ID = br.ReadString(); if (br.ReadBoolean()) { wp.Lat = br.ReadDouble(); wp.Lon = br.ReadDouble(); } wp.Name = br.ReadString(); wp.Time = DateTime.Parse(br.ReadString()); wp.Url = br.ReadString(); wp.UrlName = br.ReadString(); wp.WPType = Utils.DataAccess.GetWaypointType(br.ReadInt32()); if (gcAdded) { Utils.DataAccess.AddWaypoint(db, wp); } } if (version > 2) { int usrwpcount = br.ReadInt32(); for (int wpc = 0; wpc < usrwpcount; wpc++) { Core.Data.UserWaypointData wp = new Core.Data.UserWaypointData(); wp.ID = br.ReadInt32().ToString(); wp.Description = br.ReadString(); wp.GeocacheCode = br.ReadString(); wp.Lat = br.ReadDouble(); wp.Lon = br.ReadDouble(); wp.Date = DateTime.Parse(br.ReadString()); if (gcAdded) { Utils.DataAccess.AddUserWaypoint(db, wp); } } } index++; if (DateTime.Now >= nextUpdate) { prog.Update("ImportGeocaches", count, index); nextUpdate = DateTime.Now.AddSeconds(1); } } } } else { Core.ApplicationData.Instance.Logger.AddLog(this, Core.Logger.Level.Error, "Version not supported"); } } else { Core.ApplicationData.Instance.Logger.AddLog(this, Core.Logger.Level.Error, "Wrong file signature"); } } } catch (Exception e) { Core.ApplicationData.Instance.Logger.AddLog(this, e); } }); } }
public static Core.Data.Geocache ImportGeocache(Core.Storage.Database db, LiveV6.Geocache gc) { Core.Data.Geocache result = null; if (gc != null) { result = db.GeocacheCollection.GetGeocache(gc.Code); Core.Data.IGeocacheData gcData; if (result == null) { gcData = new Core.Data.GeocacheData(); gcData.Code = gc.Code; } else { gcData = result; } gcData.Archived = gc.Archived ?? false; gcData.Available = gc.Available ?? true; if (result == null || !result.Locked) { gcData.Container = Utils.DataAccess.GetGeocacheContainer((int)gc.ContainerType.ContainerTypeId); if (gc.Attributes != null) { List <int> attr = new List <int>(); foreach (LiveV6.Attribute a in gc.Attributes) { attr.Add(a.IsOn ? a.AttributeTypeID : -1 * a.AttributeTypeID); } gcData.AttributeIds = attr; } if (gc.Latitude != null) { gcData.Lat = (double)gc.Latitude; } if (gc.Longitude != null) { gcData.Lon = (double)gc.Longitude; } if (gc.Country != null) { gcData.Country = gc.Country; } gcData.DataFromDate = DateTime.Now; gcData.Difficulty = gc.Difficulty; gcData.Terrain = gc.Terrain; gcData.Name = gc.Name; if (gc.FavoritePoints != null) { gcData.Favorites = (int)gc.FavoritePoints; } gcData.GeocacheType = Utils.DataAccess.GetGeocacheType((int)gc.CacheType.GeocacheTypeId); if (gc.LongDescription != null) { gcData.LongDescription = gc.LongDescription; gcData.LongDescriptionInHtml = gc.LongDescriptionIsHtml; } if (gc.EncodedHints != null) { gcData.EncodedHints = gc.EncodedHints; } gcData.MemberOnly = gc.IsPremium ?? false; gcData.Owner = gc.Owner.UserName; if (gc.Owner.Id != null) { gcData.OwnerId = gc.Owner.Id.ToString(); } gcData.PlacedBy = gc.PlacedBy; gcData.PublishedTime = gc.UTCPlaceDate; if (gcData.ShortDescription != null) { gcData.ShortDescription = gc.ShortDescription; gcData.ShortDescriptionInHtml = gc.ShortDescriptionIsHtml; } if (gc.State == "None") { gcData.State = ""; } else { gcData.State = gc.State; } gcData.PersonalNote = gc.GeocacheNote; if (gc.HasbeenFoundbyUser != null) { gcData.Found = (bool)gc.HasbeenFoundbyUser; } gcData.Url = gc.Url; gcData.PersonalNote = gc.GeocacheNote ?? ""; if (gcData is Core.Data.GeocacheData) { if (Utils.DataAccess.AddGeocache(db, gcData as Core.Data.GeocacheData)) { result = db.GeocacheCollection.GetGeocache(gcData.Code); } } if (result != null) { Utils.Calculus.SetDistanceAndAngleGeocacheFromLocation(result, Core.ApplicationData.Instance.CenterLocation); ImportLogs(db, gc.GeocacheLogs); ImportWaypoints(db, gc.AdditionalWaypoints); ImportUserWaypoints(db, gc.UserWaypoints, gc.Code); ImportGeocacheImages(db, gc.Images, gc.Code); } } } return(result); }
public static Core.Data.Geocache AddGeocache(Core.Storage.Database db, OKAPIService.Geocache gc) { Core.Data.Geocache result = null; if (gc != null) { result = db.GeocacheCollection.GetGeocache(gc.code); Core.Data.IGeocacheData gcData; if (result == null) { gcData = new Core.Data.GeocacheData(); gcData.Code = gc.code; } else { gcData = result; } gcData.Archived = gc.status == "Archived"; gcData.Available = gc.status == "Available"; if (result == null || !result.Locked) { gcData.DataFromDate = DateTime.Now; List<int> attris = new List<int>(); foreach (string at in gc.attr_acodes) { int code = OKAPIService.MapAttributeACodeToAttributeID(at); if (code > 0) { attris.Add(code); } } gcData.AttributeIds = attris; // 'none', 'nano', 'micro', 'small', 'regular', 'large', 'xlarge', 'other' // we chose to map the containers if (gc.size2 == "none") { gcData.Container = Utils.DataAccess.GetGeocacheContainer(5); } else if (gc.size2 == "micro" || gc.size2 == "nano") { gcData.Container = Utils.DataAccess.GetGeocacheContainer(2); } else if (gc.size2 == "small") { gcData.Container = Utils.DataAccess.GetGeocacheContainer(8); } else if (gc.size2 == "regular") { gcData.Container = Utils.DataAccess.GetGeocacheContainer(3); } else if (gc.size2 == "large" || gc.size2 == "xlarge") { gcData.Container = Utils.DataAccess.GetGeocacheContainer(4); } else { gcData.Container = Utils.DataAccess.GetGeocacheContainer(6); } gcData.Country = gc.country ?? ""; gcData.Difficulty = gc.difficulty; gcData.EncodedHints = gc.hint2; gcData.Found = gc.is_found; if (gc.type.ToLower().Contains("traditional")) { gcData.GeocacheType = Utils.DataAccess.GetGeocacheType(96001); } else if (gc.type.ToLower().Contains("multi")) { gcData.GeocacheType = Utils.DataAccess.GetGeocacheType(96002); } else if (gc.type.ToLower().Contains("quiz") || gc.type.ToLower().Contains("puzzle")) { gcData.GeocacheType = Utils.DataAccess.GetGeocacheType(96008); } else if (gc.type.ToLower().Contains("virtual")) { gcData.GeocacheType = Utils.DataAccess.GetGeocacheType(96003); } else if (gc.type.ToLower().Contains("event")) { gcData.GeocacheType = Utils.DataAccess.GetGeocacheType(96004); } else if (gc.type.ToLower().Contains("webcam")) { gcData.GeocacheType = Utils.DataAccess.GetGeocacheType(96006); } else if (gc.type.ToLower().Contains("location") || gc.type.Contains("moving")) { gcData.GeocacheType = Utils.DataAccess.GetGeocacheType(96007); } else { gcData.GeocacheType = Utils.DataAccess.GetGeocacheType(96005); } gcData.Lat = Utils.Conversion.StringToDouble(gc.location.Substring(0, gc.location.IndexOf('|'))); gcData.Lon = Utils.Conversion.StringToDouble(gc.location.Substring(gc.location.IndexOf('|') + 1)); gcData.LongDescription = gc.description; gcData.LongDescriptionInHtml = true; gcData.Name = gc.name; gcData.Owner = gc.owner.username; gcData.OwnerId = gc.owner.uuid; gcData.PlacedBy = gc.owner.username; gcData.PublishedTime = DateTime.Parse(gc.date_hidden); gcData.ShortDescription = ""; gcData.ShortDescriptionInHtml = true; gcData.State = gc.state ?? ""; gcData.Terrain = gc.terrain; gcData.Url = gc.url ?? ""; if (gcData is Core.Data.GeocacheData) { if (Utils.DataAccess.AddGeocache(db, gcData as Core.Data.GeocacheData)) { result = db.GeocacheCollection.GetGeocache(gcData.Code); } else { result = null; } } if (result != null) { Calculus.SetDistanceAndAngleGeocacheFromLocation(result, Core.ApplicationData.Instance.CenterLocation); } } } return result; }
public static Core.Data.Geocache ImportGeocache(Core.Storage.Database db, LiveV6.Geocache gc) { Core.Data.Geocache result = null; if (gc != null) { result = db.GeocacheCollection.GetGeocache(gc.Code); Core.Data.IGeocacheData gcData; if (result == null) { gcData = new Core.Data.GeocacheData(); gcData.Code = gc.Code; } else { gcData = result; } gcData.Archived = gc.Archived ?? false; gcData.Available = gc.Available ?? true; if (result == null || !result.Locked) { gcData.Container = Utils.DataAccess.GetGeocacheContainer((int)gc.ContainerType.ContainerTypeId); if (gc.Attributes != null) { List<int> attr = new List<int>(); foreach (LiveV6.Attribute a in gc.Attributes) { attr.Add(a.IsOn ? a.AttributeTypeID : -1 * a.AttributeTypeID); } gcData.AttributeIds = attr; } if (gc.Latitude != null) gcData.Lat = (double)gc.Latitude; if (gc.Longitude != null) gcData.Lon = (double)gc.Longitude; if (gc.Country != null) gcData.Country = gc.Country; gcData.DataFromDate = DateTime.Now; gcData.Difficulty = gc.Difficulty; gcData.Terrain = gc.Terrain; gcData.Name = gc.Name; if (gc.FavoritePoints != null) { gcData.Favorites = (int)gc.FavoritePoints; } gcData.GeocacheType = Utils.DataAccess.GetGeocacheType((int)gc.CacheType.GeocacheTypeId); if (gc.LongDescription != null) { gcData.LongDescription = gc.LongDescription; gcData.LongDescriptionInHtml = gc.LongDescriptionIsHtml; } if (gc.EncodedHints != null) { gcData.EncodedHints = gc.EncodedHints; } gcData.MemberOnly = gc.IsPremium ?? false; gcData.Owner = gc.Owner.UserName; if (gc.Owner.Id != null) { gcData.OwnerId = gc.Owner.Id.ToString(); } gcData.PlacedBy = gc.PlacedBy; gcData.PublishedTime = gc.UTCPlaceDate; if (gcData.ShortDescription != null) { gcData.ShortDescription = gc.ShortDescription; gcData.ShortDescriptionInHtml = gc.ShortDescriptionIsHtml; } if (gc.State == "None") { gcData.State = ""; } else { gcData.State = gc.State; } gcData.PersonalNote = gc.GeocacheNote; if (gc.HasbeenFoundbyUser != null) { gcData.Found = (bool)gc.HasbeenFoundbyUser; } gcData.Url = gc.Url; gcData.PersonalNote = gc.GeocacheNote ?? ""; if (gcData is Core.Data.GeocacheData) { if (Utils.DataAccess.AddGeocache(db, gcData as Core.Data.GeocacheData)) { result = db.GeocacheCollection.GetGeocache(gcData.Code); } } if (result != null) { Utils.Calculus.SetDistanceAndAngleGeocacheFromLocation(result, Core.ApplicationData.Instance.CenterLocation); ImportLogs(db, gc.GeocacheLogs); ImportWaypoints(db, gc.AdditionalWaypoints); ImportUserWaypoints(db, gc.UserWaypoints, gc.Code); ImportGeocacheImages(db, gc.Images, gc.Code); } } } return result; }
async public Task PasteGeocaches() { if (Core.ApplicationData.Instance.ActiveDatabase != null && !string.IsNullOrEmpty(_sourceDatabaseFilename) && Core.ApplicationData.Instance.ActiveDatabase.FileName != _sourceDatabaseFilename && _gcCodes != null && _gcCodes.Count > 0) { Core.Storage.Database srcDb = (from a in Core.ApplicationData.Instance.Databases where a.FileName == _sourceDatabaseFilename select a).FirstOrDefault(); if (srcDb != null) { using (Utils.DataUpdater upd = new Utils.DataUpdater(Core.ApplicationData.Instance.ActiveDatabase)) { await Task.Run(() => { DateTime nextUpdate = DateTime.Now.AddSeconds(1); using (Utils.ProgressBlock prog = new Utils.ProgressBlock("CopyingGeocaches", "CopyingGeocaches", _gcCodes.Count, 0, true)) { int index = 0; Core.Data.GeocacheData gcData = new Core.Data.GeocacheData(); Core.Data.LogData lgData = new Core.Data.LogData(); Core.Data.WaypointData wpData = new Core.Data.WaypointData(); Core.Data.UserWaypointData uwpData = new Core.Data.UserWaypointData(); Core.Data.LogImageData lgiData = new Core.Data.LogImageData(); Core.Data.GeocacheImageData gciData = new Core.Data.GeocacheImageData(); foreach (string gcCode in _gcCodes) { Core.Data.Geocache gc = srcDb.GeocacheCollection.GetGeocache(gcCode); if (gc != null) { Core.Data.GeocacheData.Copy(gc, gcData); Utils.DataAccess.AddGeocache(Core.ApplicationData.Instance.ActiveDatabase, gcData); List <Core.Data.Log> lgs = Utils.DataAccess.GetLogs(srcDb, gc.Code); foreach (var l in lgs) { Core.Data.LogData.Copy(l, lgData); Utils.DataAccess.AddLog(Core.ApplicationData.Instance.ActiveDatabase, lgData); List <Core.Data.LogImage> lgis = Utils.DataAccess.GetLogImages(srcDb, l.ID); foreach (var li in lgis) { Core.Data.LogImageData.Copy(li, lgiData); Utils.DataAccess.AddLogImage(Core.ApplicationData.Instance.ActiveDatabase, lgiData); } } List <Core.Data.Waypoint> wps = Utils.DataAccess.GetWaypointsFromGeocache(srcDb, gc.Code); foreach (var wp in wps) { Core.Data.WaypointData.Copy(wp, wpData); Utils.DataAccess.AddWaypoint(Core.ApplicationData.Instance.ActiveDatabase, wpData); } List <Core.Data.UserWaypoint> uwps = Utils.DataAccess.GetUserWaypointsFromGeocache(srcDb, gc.Code); foreach (var wp in uwps) { Core.Data.UserWaypointData.Copy(wp, uwpData); Utils.DataAccess.AddUserWaypoint(Core.ApplicationData.Instance.ActiveDatabase, uwpData); } List <Core.Data.GeocacheImage> gcis = Utils.DataAccess.GetGeocacheImages(srcDb, gc.Code); foreach (var wp in gcis) { Core.Data.GeocacheImageData.Copy(wp, gciData); Utils.DataAccess.AddGeocacheImage(Core.ApplicationData.Instance.ActiveDatabase, gciData); } index++; if (DateTime.Now >= nextUpdate) { if (!prog.Update("CopyingGeocaches", _gcCodes.Count, index)) { break; } } } } } }); } } } }
public void ImportMunzeesFromDfxAt(Core.Storage.Database db, string url) { using (Utils.ProgressBlock fixpr = new Utils.ProgressBlock("ImportingMunzees", "DownloadingData", 2, 0)) { int index = 0; MunzeeDataList munzl; try { System.Net.HttpWebRequest webRequest; webRequest = System.Net.WebRequest.Create(url) as System.Net.HttpWebRequest; webRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.17 Safari/533.4"; DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(MunzeeDataList)); using (System.IO.Stream stream = webRequest.GetResponse().GetResponseStream()) { munzl = ser.ReadObject(stream) as MunzeeDataList; } fixpr.Update("DownloadingData", 2, 1); DateTime nextProgUpdate = DateTime.MinValue; string usrname = (Core.ApplicationData.Instance.AccountInfos.GetAccountInfo("MZ").AccountName ?? "").ToLower(); using (Utils.ProgressBlock prog = new Utils.ProgressBlock("SavingGeocaches", "SavingGeocaches", munzl.Count, 0)) { foreach (MunzeeData md in munzl) { string code = string.Format("MZ{0}", int.Parse(md.munzee_id).ToString("X4")); Core.Data.IGeocacheData gc = db.GeocacheCollection.GetGeocache(code); if (gc == null) { gc = new Core.Data.GeocacheData(); } gc.Archived = md.archived == "1"; gc.Available = !gc.Archived; //gc.City = md.location.Split(new char[]{','})[0]; gc.City = ""; gc.Code = code; gc.Container = Utils.DataAccess.GetGeocacheContainer(1); gc.Country = md.country; gc.DataFromDate = DateTime.Now; gc.Difficulty = 1.0; gc.Found = md.captured != "0"; if (md.type == "") { gc.GeocacheType = Utils.DataAccess.GetGeocacheType(95342); } else if (md.type == "virtual") { gc.GeocacheType = Utils.DataAccess.GetGeocacheType(95343); } else if (md.type == "maintenance") { gc.GeocacheType = Utils.DataAccess.GetGeocacheType(95344); } else if (md.type == "business") { gc.GeocacheType = Utils.DataAccess.GetGeocacheType(95345); } else if (md.type == "mystery") { gc.GeocacheType = Utils.DataAccess.GetGeocacheType(95346); } else if (md.type == "nfc") { gc.GeocacheType = Utils.DataAccess.GetGeocacheType(95347); } else if (md.type == "premium") { gc.GeocacheType = Utils.DataAccess.GetGeocacheType(95348); } else { gc.GeocacheType = Utils.DataAccess.GetGeocacheType(95342); } gc.Lat = Utils.Conversion.StringToDouble(md.latitude); gc.Lon = Utils.Conversion.StringToDouble(md.longitude); gc.LongDescription = md.notes == null ? "" : md.notes.Replace("\r", "").Replace("\n", "\r\n"); gc.LongDescriptionInHtml = false; gc.MemberOnly = false; gc.Municipality = ""; gc.Name = md.friendly_name; gc.Owner = md.username; gc.OwnerId = ""; gc.PlacedBy = md.username; try { gc.PublishedTime = DateTime.ParseExact(md.deployed_at, "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture); } catch { gc.PublishedTime = DateTime.ParseExact(md.created_at, "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture); } gc.ShortDescription = ""; gc.ShortDescriptionInHtml = false; gc.State = ""; gc.Terrain = 1.0; gc.Url = string.Format("http://www.munzee.com/m/{0}/{1}/", md.username, md.code); bool gcAdded = true; if (gc is Core.Data.GeocacheData) { gcAdded = Utils.DataAccess.AddGeocache(Core.ApplicationData.Instance.ActiveDatabase, gc as Core.Data.GeocacheData); } if (gcAdded) { gc = (from a in Core.ApplicationData.Instance.ActiveDatabase.GeocacheCollection where a.Code == code select a).FirstOrDefault(); Utils.Calculus.SetDistanceAndAngleGeocacheFromLocation(gc as Core.Data.Geocache, Core.ApplicationData.Instance.CenterLocation); //check if found and if so, if log present if (gc.Found && !string.IsNullOrEmpty(md.captured_at)) { //foud and no log, add log bool foundLogPresent = false; List <Core.Data.Log> lgs = db.LogCollection.GetLogs(gc.Code); if (lgs != null) { Core.Data.Log l = (from Core.Data.Log lg in lgs where lg.LogType.AsFound && string.Compare(usrname, lg.Finder, true) == 0 select lg).FirstOrDefault(); foundLogPresent = (l != null); } if (!foundLogPresent) { Core.Data.LogData l = new Core.Data.LogData(); l.DataFromDate = DateTime.Now; l.Date = new DateTime(1970, 1, 1); l.Date = l.Date.AddSeconds(long.Parse(md.captured_at.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0])); l.Encoded = false; l.Finder = Core.ApplicationData.Instance.AccountInfos.GetAccountInfo("MZ").AccountName ?? ""; l.FinderId = "0"; l.GeocacheCode = gc.Code; l.ID = string.Format("ML{0}", gc.Code.Substring(2)); l.LogType = Utils.DataAccess.GetLogType(2); l.Text = "Captured"; Utils.DataAccess.AddLog(db, l); } } } index++; if (DateTime.Now >= nextProgUpdate) { prog.Update("SavingGeocaches", munzl.Count, index); nextProgUpdate = DateTime.Now.AddSeconds(0.5); } } } } catch (Exception e) { Core.ApplicationData.Instance.Logger.AddLog(this, e); } } }
public static Core.Data.Geocache AddGeocache(Core.Storage.Database db, OKAPIService.Geocache gc) { Core.Data.Geocache result = null; if (gc != null) { result = db.GeocacheCollection.GetGeocache(gc.code); Core.Data.IGeocacheData gcData; if (result == null) { gcData = new Core.Data.GeocacheData(); gcData.Code = gc.code; } else { gcData = result; } gcData.Archived = gc.status == "Archived"; gcData.Available = gc.status == "Available"; if (result == null || !result.Locked) { gcData.DataFromDate = DateTime.Now; List <int> attris = new List <int>(); foreach (string at in gc.attr_acodes) { int code = OKAPIService.MapAttributeACodeToAttributeID(at); if (code > 0) { attris.Add(code); } } gcData.AttributeIds = attris; // 'none', 'nano', 'micro', 'small', 'regular', 'large', 'xlarge', 'other' // we chose to map the containers if (gc.size2 == "none") { gcData.Container = Utils.DataAccess.GetGeocacheContainer(5); } else if (gc.size2 == "micro" || gc.size2 == "nano") { gcData.Container = Utils.DataAccess.GetGeocacheContainer(2); } else if (gc.size2 == "small") { gcData.Container = Utils.DataAccess.GetGeocacheContainer(8); } else if (gc.size2 == "regular") { gcData.Container = Utils.DataAccess.GetGeocacheContainer(3); } else if (gc.size2 == "large" || gc.size2 == "xlarge") { gcData.Container = Utils.DataAccess.GetGeocacheContainer(4); } else { gcData.Container = Utils.DataAccess.GetGeocacheContainer(6); } gcData.Country = gc.country ?? ""; gcData.Difficulty = gc.difficulty; gcData.EncodedHints = gc.hint2; gcData.Found = gc.is_found; if (gc.type.ToLower().Contains("traditional")) { gcData.GeocacheType = Utils.DataAccess.GetGeocacheType(96001); } else if (gc.type.ToLower().Contains("multi")) { gcData.GeocacheType = Utils.DataAccess.GetGeocacheType(96002); } else if (gc.type.ToLower().Contains("quiz") || gc.type.ToLower().Contains("puzzle")) { gcData.GeocacheType = Utils.DataAccess.GetGeocacheType(96008); } else if (gc.type.ToLower().Contains("virtual")) { gcData.GeocacheType = Utils.DataAccess.GetGeocacheType(96003); } else if (gc.type.ToLower().Contains("event")) { gcData.GeocacheType = Utils.DataAccess.GetGeocacheType(96004); } else if (gc.type.ToLower().Contains("webcam")) { gcData.GeocacheType = Utils.DataAccess.GetGeocacheType(96006); } else if (gc.type.ToLower().Contains("location") || gc.type.Contains("moving")) { gcData.GeocacheType = Utils.DataAccess.GetGeocacheType(96007); } else { gcData.GeocacheType = Utils.DataAccess.GetGeocacheType(96005); } gcData.Lat = Utils.Conversion.StringToDouble(gc.location.Substring(0, gc.location.IndexOf('|'))); gcData.Lon = Utils.Conversion.StringToDouble(gc.location.Substring(gc.location.IndexOf('|') + 1)); gcData.LongDescription = gc.description; gcData.LongDescriptionInHtml = true; gcData.Name = gc.name; gcData.Owner = gc.owner.username; gcData.OwnerId = gc.owner.uuid; gcData.PlacedBy = gc.owner.username; gcData.PublishedTime = DateTime.Parse(gc.date_hidden); gcData.ShortDescription = ""; gcData.ShortDescriptionInHtml = true; gcData.State = gc.state ?? ""; gcData.Terrain = gc.terrain; gcData.Url = gc.url ?? ""; if (gcData is Core.Data.GeocacheData) { if (Utils.DataAccess.AddGeocache(db, gcData as Core.Data.GeocacheData)) { result = db.GeocacheCollection.GetGeocache(gcData.Code); } else { result = null; } } if (result != null) { Calculus.SetDistanceAndAngleGeocacheFromLocation(result, Core.ApplicationData.Instance.CenterLocation); } } } return(result); }