Ejemplo n.º 1
0
        protected virtual bool AddWaypoint(Framework.Data.Waypoint wp)
        {
            bool result = false;

            if (_wpsEmpty)
            {
                Core.Waypoints.Add(wp);
                result = true;
            }
            else
            {
                Framework.Data.Waypoint oldwp = Utils.DataAccess.GetWaypoint(Core.Waypoints, wp.Code);
                if (oldwp == null)
                {
                    Core.Waypoints.Add(wp);
                    result = true;
                }
                else
                {
                    if (wp.DataFromDate >= oldwp.DataFromDate)
                    {
                        Utils.DataAccess.UpdateWaypointData(oldwp, wp);
                    }
                }
            }
            return(result);
        }
Ejemplo n.º 2
0
 public static Framework.Data.Waypoint Waypoint(Framework.Interfaces.ICore core, LiveV6.AdditionalWaypoint wp)
 {
     Framework.Data.Waypoint result = null;
     if (wp != null)
     {
         Framework.Data.Waypoint tmp = DataAccess.GetWaypoint(core.Waypoints, wp.Code);
         result = new Framework.Data.Waypoint();
         if (tmp != null)
         {
             result.UpdateFrom(tmp);
         }
         result.Code         = wp.Code;
         result.DataFromDate = DateTime.Now;
         result.WPType       = DataAccess.GetWaypointType(core.WaypointTypes, wp.WptTypeID);
         result.Comment      = wp.Comment;
         result.Description  = wp.Description;
         result.GeocacheCode = wp.GeocacheCode;
         result.ID           = wp.Code;
         result.Lat          = wp.Latitude;
         result.Lon          = wp.Longitude;
         result.Name         = wp.Name;
         result.Time         = wp.UTCEnteredDate;
         result.Url          = wp.Url;
         result.UrlName      = wp.UrlName;
     }
     return(result);
 }
Ejemplo n.º 3
0
        //////////////////////////////////////////////////////////////
        //defines the filename based on wpt attribute & opens the file
        // if new
        //////////////////////////////////////////////////////////////
        private System.IO.StreamWriter WptCsv(Framework.Data.Geocache gc,
                                              Framework.Data.Waypoint wpt)
        {
            string csvName = null;
            string postFix = "";

            switch (wpt.WPType.ID)
            {
            case 217:
                csvName = "Parking_Area"; break;

            case 218:
                csvName = "Question_to_Answer"; break;

            case 219:
                csvName = "Stages_of_a_Multicache"; break;

            case 220:
                csvName = "Final_Location"; break;

            case 221:
                csvName = "Trailhead"; break;

            case 452:
                csvName = "Reference_Point"; break;

            default:
                csvName = wpt.WPType.Name.Replace(" ", "_"); break;
            }
            if (gc.Found)
            {
                postFix = "_Found";
            }
            if (!gc.Available || gc.Archived)
            {
                postFix = "_Disabled";
            }
            csvName = csvName + postFix;

            if (!sd.ContainsKey(csvName))
            { //new -> open File
                System.IO.StreamWriter cs = null;

                string csvPath = PluginSettings.Instance.POIExportPath + Path.DirectorySeparatorChar + csvName + ".csv";
                cs = new System.IO.StreamWriter(csvPath, false,
                                                Encoding.GetEncoding(1250), 32768);
                sd.Add(csvName, cs);
                //create wpt icon
                try
                {
                    using (Stream input = Assembly.GetExecutingAssembly().GetManifestResourceStream("GlobalcachingApplication.Plugins.ExportGarminPOI.Resources." + csvName + ".bmp"))
                        using (Stream output = File.Create(PluginSettings.Instance.POIExportPath + Path.DirectorySeparatorChar + csvName + ".bmp"))
                        {
                            input.CopyTo(output);
                        }
                }
                catch { } //ignore invalid resource or write errors here
            }
            return(sd[csvName]);
        }
Ejemplo n.º 4
0
        private void button3_Click(object sender, EventArgs e)
        {
            WaypointListItem wpi = listBox1.SelectedItem as WaypointListItem;

            if (wpi != null)
            {
                Framework.Data.Waypoint wp = wpi.WP;
                Framework.Data.Location l;
                if (wp.Lat != null && wp.Lon != null)
                {
                    l = new Framework.Data.Location((double)wp.Lat, (double)wp.Lon);
                }
                else
                {
                    l = new Framework.Data.Location(Core.ActiveGeocache.Lat, Core.ActiveGeocache.Lon);
                }
                using (Utils.Dialogs.GetLocationForm dlg = new Utils.Dialogs.GetLocationForm(Core, l))
                {
                    if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        textBox8.Text = Utils.Conversion.GetCoordinatesPresentation(dlg.Result);
                    }
                }
            }
        }
Ejemplo n.º 5
0
 public void OnWaypointRemoved(Framework.Data.Waypoint wp)
 {
     if (!_updating && WaypointRemoved != null)
     {
         WaypointRemoved(this, new Framework.EventArguments.WaypointEventArgs(wp));
     }
     OnListDataChanged();
 }
Ejemplo n.º 6
0
 public override void Remove(object value)
 {
     Framework.Data.Waypoint wp = value as Framework.Data.Waypoint;
     if (wp != null)
     {
         RemoveAt(IndexOf(wp));
     }
 }
Ejemplo n.º 7
0
 public void OnDataChanged(Framework.Data.Waypoint wp)
 {
     _dataChanged = true;
     if (!_updating && DataChanged != null)
     {
         DataChanged(this, new Framework.EventArguments.WaypointEventArgs(wp));
         _dataChanged     = false;
         wp.IsDataChanged = false;
     }
 }
Ejemplo n.º 8
0
        public static bool UpdateWaypointData(Framework.Data.Waypoint wp, Framework.Data.Waypoint newData)
        {
            bool result = false;

            if (wp.Code == newData.Code)
            {
                wp.UpdateFrom(newData);
                result = true;
            }
            return(result);
        }
Ejemplo n.º 9
0
 public static Framework.Data.Waypoint Waypoint(Framework.Interfaces.ICore core, OKAPIService.Waypoint wp)
 {
     Framework.Data.Waypoint result = null;
     if (wp != null)
     {
         Framework.Data.Waypoint tmp = DataAccess.GetWaypoint(core.Waypoints, wp.name);
         result = new Framework.Data.Waypoint();
         if (tmp != null)
         {
             result.UpdateFrom(tmp);
         }
         result.Code         = wp.name;
         result.DataFromDate = DateTime.Now;
         //for now map: parking, path, stage, physical-stage, virtual-stage, final, poi, other
         if (wp.sym == "parking")
         {
             result.WPType = DataAccess.GetWaypointType(core.WaypointTypes, 217);
         }
         else if (wp.sym == "path")
         {
             result.WPType = DataAccess.GetWaypointType(core.WaypointTypes, 452);
         }
         else if (wp.sym == "stage" || wp.sym == "physical-stage" || wp.sym == "virtual-stage")
         {
             result.WPType = DataAccess.GetWaypointType(core.WaypointTypes, 219);
         }
         else if (wp.sym == "final")
         {
             result.WPType = DataAccess.GetWaypointType(core.WaypointTypes, 220);
         }
         else if (wp.sym == "poi")
         {
             result.WPType = DataAccess.GetWaypointType(core.WaypointTypes, 452);
         }
         else
         {
             result.WPType = DataAccess.GetWaypointType(core.WaypointTypes, 452);
         }
         result.Comment      = wp.description;
         result.Description  = wp.description;
         result.GeocacheCode = wp.cache_code;
         result.ID           = wp.name;
         result.Lat          = Utils.Conversion.StringToDouble(wp.location.Substring(0, wp.location.IndexOf('|')));
         result.Lon          = Utils.Conversion.StringToDouble(wp.location.Substring(wp.location.IndexOf('|') + 1));
         result.Name         = wp.type_name;
         result.Time         = DateTime.Now;
         result.Url          = "";
         result.UrlName      = wp.type_name;
     }
     return(result);
 }
Ejemplo n.º 10
0
        public override void RemoveAt(int index)
        {
            if (index >= 0 && index < Count)
            {
                Framework.Data.Waypoint wp = this[index] as Framework.Data.Waypoint;
                //grouping
                (_wpGroups[wp.GeocacheCode ?? ""] as Hashtable).Remove(wp.Code);
                //end grouping

                _qaItems.Remove(wp.Code);
                wp.DataChanged -= new EventArguments.WaypointEventHandler(gc_DataChanged);
                base.RemoveAt(index);
                OnWaypointRemoved(wp);
            }
        }
Ejemplo n.º 11
0
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            WaypointListItem wpi = listBox1.SelectedItem as WaypointListItem;

            if (wpi != null)
            {
                Framework.Data.Waypoint wp = wpi.WP;
                button2.Enabled = true;
                button5.Enabled = true;
                button3.Enabled = true;
                button4.Enabled = true;

                textBox1.Text = wp.ID ?? "";
                textBox2.Text = wp.Code ?? "";
                textBox3.Text = wp.Name ?? "";
                textBox4.Text = wp.Comment ?? "";
                textBox5.Text = wp.Description ?? "";
                textBox6.Text = wp.Url ?? "";
                textBox7.Text = wp.UrlName ?? "";
                if (wp.Lat != null && wp.Lon != null)
                {
                    textBox8.Text = Utils.Conversion.GetCoordinatesPresentation((double)wp.Lat, (double)wp.Lon);
                }
                else
                {
                    textBox8.Text = "";
                }
                comboBoxWaypointType1.SelectedItem = wp.WPType;
            }
            else
            {
                button2.Enabled = false;
                button5.Enabled = false;
                button3.Enabled = false;
                button4.Enabled = false;

                textBox1.Text = "";
                textBox2.Text = "";
                textBox3.Text = "";
                textBox4.Text = "";
                textBox5.Text = "";
                textBox6.Text = "";
                textBox7.Text = "";
                textBox8.Text = "";
            }
        }
Ejemplo n.º 12
0
        public static bool AddWaypoint(Framework.Interfaces.ICore core, Framework.Data.Waypoint wp)
        {
            bool result = false;

            Framework.Data.Waypoint oldwp = DataAccess.GetWaypoint(core.Waypoints, wp.Code);
            if (oldwp == null)
            {
                core.Waypoints.Add(wp);
                result = true;
            }
            else
            {
                if (wp.DataFromDate >= oldwp.DataFromDate)
                {
                    DataAccess.UpdateWaypointData(oldwp, wp);
                }
            }
            return(result);
        }
Ejemplo n.º 13
0
        public void ProcessGeocachingComGPXWaypoints(ResultData data, string gpxDoc)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(gpxDoc);
                XmlElement          root  = doc.DocumentElement;
                XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
                nsmgr.AddNamespace("x", root.NamespaceURI);

                _gpxDataTime = DateTime.Parse(root.SelectSingleNode("x:time", nsmgr).InnerText);

                XmlNodeList wps = root.SelectNodes("x:wpt", nsmgr);
                if (wps != null)
                {
                    foreach (XmlNode n in wps)
                    {
                        Framework.Data.Waypoint wp = new Framework.Data.Waypoint();

                        wp.Code         = n.SelectSingleNode("x:name", nsmgr).InnerText;
                        wp.GeocacheCode = string.Concat("GC", wp.Code.Substring(2));
                        wp.DataFromDate = _gpxDataTime;
                        wp.Comment      = n.SelectSingleNode("x:cmt", nsmgr).InnerText;
                        wp.Lat          = Utils.Conversion.StringToDouble(n.Attributes["lat"].InnerText);
                        wp.Lon          = Utils.Conversion.StringToDouble(n.Attributes["lon"].InnerText);
                        wp.Description  = n.SelectSingleNode("x:desc", nsmgr).InnerText;
                        wp.ID           = wp.Code;
                        wp.Name         = wp.Code;
                        wp.Comment      = n.SelectSingleNode("x:cmt", nsmgr).InnerText;
                        wp.Time         = DateTime.Parse(n.SelectSingleNode("x:time", nsmgr).InnerText);
                        wp.Url          = n.SelectSingleNode("x:url", nsmgr).InnerText;
                        wp.UrlName      = n.SelectSingleNode("x:urlname", nsmgr).InnerText;
                        wp.WPType       = DataAccess.GetWaypointType(_core.WaypointTypes, n.SelectSingleNode("x:sym", nsmgr).InnerText);

                        data.Waypoints.Add(wp);
                    }
                }
            }
            catch
            {
            }
        }
Ejemplo n.º 14
0
        private object GetPosition(Framework.Data.Waypoint wpt)
        {
            double lat, lon;

            if (wpt.Lat != null)
            {
                lat = (double)wpt.Lat;
            }
            else
            {
                return("");
            }
            if (wpt.Lon != null)
            {
                lon = (double)wpt.Lon;
            }
            else
            {
                return("");
            }
            return(Utils.Conversion.GetCoordinatesPresentation(lat, lon));
        }
Ejemplo n.º 15
0
        private void button5_Click(object sender, EventArgs e)
        {
            WaypointListItem wpi = listBox1.SelectedItem as WaypointListItem;

            if (wpi != null)
            {
                Framework.Data.Waypoint wp = wpi.WP;
                wp.BeginUpdate();

                wp.Name        = textBox3.Text;
                wp.Comment     = textBox4.Text;
                wp.Description = textBox5.Text;
                wp.Url         = textBox6.Text;
                wp.UrlName     = textBox7.Text;
                Framework.Data.Location l = Utils.Conversion.StringToLocation(textBox8.Text);
                if (l == null)
                {
                    wp.Lat = null;
                    wp.Lon = null;
                }
                else
                {
                    wp.Lat = l.Lat;
                    wp.Lon = l.Lon;
                }
                wp.WPType = comboBoxWaypointType1.SelectedItem as Framework.Data.WaypointType;
                if (wp.WPType == null)
                {
                    wp.WPType = Core.WaypointTypes[0];
                }

                wp.EndUpdate();

                typeof(ListBox).InvokeMember("RefreshItems", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, listBox1, new object[] { });
            }
        }
Ejemplo n.º 16
0
        public override int Add(object value)
        {
            int result;

            Framework.Data.Waypoint wp = value as Framework.Data.Waypoint;
            if (wp != null)
            {
                //grouping
                if (_wpGroups[wp.GeocacheCode ?? ""] == null)
                {
                    _wpGroups[wp.GeocacheCode ?? ""] = new Hashtable();
                }
                (_wpGroups[wp.GeocacheCode ?? ""] as Hashtable)[wp.Code] = wp;
                //end grouping
                _qaItems[wp.Code] = wp;
                _sorted           = false;
                result            = base.Add(wp);
                wp.DataChanged   += new EventArguments.WaypointEventHandler(gc_DataChanged);
                OnWaypointAdded(wp);
            }
            else
            {
                //for now, only supported between begin and end update
                List <Waypoint> lgs = value as List <Waypoint>;
                if (lgs != null)
                {
                    _batchAddition = lgs;
                    result         = 0;
                }
                else
                {
                    result = -1;
                }
            }
            return(result);
        }
Ejemplo n.º 17
0
 public static Framework.Data.Waypoint Waypoint(Framework.Interfaces.ICore core, LiveV6.AdditionalWaypoint wp)
 {
     Framework.Data.Waypoint result = null;
     if (wp != null)
     {
         Framework.Data.Waypoint tmp = DataAccess.GetWaypoint(core.Waypoints, wp.Code);
         result = new Framework.Data.Waypoint();
         if (tmp != null)
         {
             result.UpdateFrom(tmp);
         }
         result.Code = wp.Code;
         result.DataFromDate = DateTime.Now;
         result.WPType = DataAccess.GetWaypointType(core.WaypointTypes, wp.WptTypeID);
         result.Comment = wp.Comment;
         result.Description = wp.Description;
         result.GeocacheCode = wp.GeocacheCode;
         result.ID = wp.Code;
         result.Lat = wp.Latitude;
         result.Lon = wp.Longitude;
         result.Name = wp.Name;
         result.Time = wp.UTCEnteredDate;
         result.Url = wp.Url;
         result.UrlName = wp.UrlName;
     }
     return result;
 }
Ejemplo n.º 18
0
        public override bool Load(bool geocachesOnly)
        {
            bool result = true;
            if (_dbcon != null)
            {
                List<string> activeAttr = new List<string>();
                using (MySqlCommand cmd = new MySqlCommand("select field_name from geocache_cfields", _dbcon))
                using (MySqlDataReader dr = cmd.ExecuteReader())
                    while (dr.Read())
                    {
                        string s = string.Format("{0}", dr["field_name"]);
                        activeAttr.Add(s);
                        Core.Geocaches.AddCustomAttribute(s);
                    }

                int gcCount = 0;
                int logCount = 0;
                int logimgCount = 0;
                int wptCount = 0;
                using (MySqlCommand cmd = new MySqlCommand("select geocache, log, waypoint, logimage from counter", _dbcon))
                using (MySqlDataReader dr = cmd.ExecuteReader())
                    if (dr.Read())
                    {
                        gcCount = (int)dr["geocache"];
                        logCount = (int)dr["log"];
                        logimgCount = (int)dr["waypoint"];
                        wptCount = (int)dr["logimage"];
                    }

                using (Utils.ProgressBlock fixscr = new ProgressBlock(this, STR_LOADING, STR_LOADINGDATA, 1, 0))
                {
                    using (Utils.ProgressBlock progress = new ProgressBlock(this, STR_LOADING, STR_LOADINGGEOCACHES, gcCount, 0))
                    {
                        int index = 0;
                        int procStep = 0;
                        using (MySqlCommand cmd = new MySqlCommand("select id, code, name, datafromdate, lat, lon, disttocent, angletocent, available, archived, country, state, cachetype, placedby, owner, ownerid, container, terrain, difficulty, encodedhints, url, memberonly, customcoords, attrids, favorites, selected, municipality, city, customlat, customlon, notes, publiceddate, personalnote, flagged, found, locked from geocache", _dbcon))
                        {
                            cmd.CommandTimeout = 0;
                            using (MySqlDataReader dr = cmd.ExecuteReader())
                                while (dr.Read())
                                {
                                    Framework.Data.Geocache gc = new Framework.Data.Geocache();

                                    gc.ID = (string)dr["id"];
                                    gc.Code = (string)dr["code"];
                                    if (string.IsNullOrEmpty(gc.ID) && !string.IsNullOrEmpty(gc.Code) && gc.Code.ToUpper().StartsWith("GC"))
                                    {
                                        gc.ID = Utils.Conversion.GetCacheIDFromCacheCode(gc.Code).ToString();
                                    }
                                    gc.Name = (string)dr["name"];
                                    gc.DataFromDate = DateTime.Parse((string)dr["datafromdate"]).ToLocalTime();
                                    gc.Lat = (double)(float)dr["lat"];
                                    gc.Lon = (double)(float)dr["lon"];
                                    //gc.DistanceToCenter = (int)dr["disttocent"];
                                    //gc.AngleToCenter = (int)dr["angletocent"];
                                    gc.Available = (int)dr["available"] != 0;
                                    gc.Archived = (int)dr["archived"] != 0;
                                    gc.Country = (string)dr["country"];
                                    gc.State = (string)dr["state"];
                                    gc.GeocacheType = Utils.DataAccess.GetGeocacheType(Core.GeocacheTypes, (int)dr["cachetype"]);
                                    gc.PlacedBy = (string)dr["placedby"];
                                    gc.Owner = (string)dr["owner"];
                                    gc.OwnerId = (string)dr["ownerid"];
                                    gc.Container = Utils.DataAccess.GetGeocacheContainer(Core.GeocacheContainers, (int)dr["container"]);
                                    gc.Terrain = (double)(float)dr["terrain"];
                                    gc.Difficulty = (double)(float)dr["difficulty"];
                                    gc.EncodedHints = (string)dr["encodedhints"];
                                    gc.Url = (string)dr["url"];
                                    gc.MemberOnly = (int)dr["memberonly"] != 0;
                                    gc.CustomCoords = (int)dr["customcoords"] != 0;
                                    string s = (string)dr["attrids"];
                                    string[] parts = s.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                                    List<int> attrList = new List<int>();
                                    if (parts != null)
                                    {
                                        foreach (string si in parts)
                                        {
                                            attrList.Add(int.Parse(si));
                                        }
                                    }
                                    gc.AttributeIds = attrList;

                                    gc.Municipality = (string)dr["municipality"];
                                    gc.City = (string)dr["city"];
                                    object o = dr["customlat"];
                                    if (o == null || o.GetType() == typeof(DBNull))
                                    {
                                        gc.CustomLat = null;
                                    }
                                    else
                                    {
                                        gc.CustomLat = (double?)(float)o;
                                    }
                                    o = dr["customlon"];
                                    if (o == null || o.GetType() == typeof(DBNull))
                                    {
                                        gc.CustomLon = null;
                                    }
                                    else
                                    {
                                        gc.CustomLon = (double?)(float)o;
                                    }
                                    gc.Notes = (string)dr["notes"];
                                    gc.PublishedTime = DateTime.Parse((string)dr["publiceddate"]).ToLocalTime();
                                    gc.PersonaleNote = (string)dr["personalnote"];
                                    gc.Flagged = (int)dr["flagged"] != 0;
                                    gc.Locked = (int)dr["locked"] != 0;
                                    gc.Found = (int)dr["found"] != 0;

                                    gc.Favorites = (int)dr["favorites"];
                                    gc.Selected = (int)dr["selected"] != 0;

                                    foreach (string attrField in activeAttr)
                                    {
                                        object objValue = null;
                                        try
                                        {
                                            objValue = dr[string.Format("_{0}", attrField)];
                                        }
                                        catch
                                        {
                                        }
                                        gc.SetCustomAttribute(attrField, objValue);
                                    }

                                    Calculus.SetDistanceAndAngleGeocacheFromLocation(gc, Core.CenterLocation);

                                    gc.Saved = true;
                                    gc.IsDataChanged = false;

                                    _geocachesInDB[gc.Code] = gc.Code;
                                    Core.Geocaches.Add(gc);

                                    index++;
                                    procStep++;
                                    if (procStep >= 1000)
                                    {
                                        progress.UpdateProgress(STR_LOADING, STR_LOADINGGEOCACHES, gcCount, index);
                                        procStep = 0;
                                    }
                                }
                        }
                    }

                    if (!geocachesOnly)
                    {

                        using (Utils.ProgressBlock progress = new ProgressBlock(this, STR_LOADING, STR_LOADINGLOGS, logCount, 0))
                        {
                            int index = 0;
                            int procStep = 0;
                            using (MySqlCommand cmd = new MySqlCommand("select ID, gccode, Finder, DataFromDate, LogType, Date from log", _dbcon))
                            {
                                cmd.CommandTimeout = 0;
                                using (MySqlDataReader dr = cmd.ExecuteReader())
                                    while (dr.Read())
                                    {
                                        Framework.Data.Log lg = new Framework.Data.Log();

                                        //id text, gccode text, tbcode text, date text, finder text, finderid text, logtext text, encoded integer, datafromdate text, logtype integer
                                        lg.ID = (string)dr["id"];
                                        lg.GeocacheCode = (string)dr["gccode"];
                                        lg.Date = DateTime.Parse((string)dr["date"]).ToLocalTime();
                                        lg.Finder = (string)dr["finder"];
                                        lg.DataFromDate = DateTime.Parse((string)dr["datafromdate"]).ToLocalTime();
                                        lg.LogType = Utils.DataAccess.GetLogType(Core.LogTypes, (int)dr["logtype"]);

                                        lg.Saved = true;
                                        lg.IsDataChanged = false;

                                        _logsInDB[lg.ID] = lg.ID;
                                        Core.Logs.Add(lg);

                                        index++;
                                        procStep++;
                                        if (procStep >= 20000)
                                        {
                                            progress.UpdateProgress(STR_LOADING, STR_LOADINGLOGS, logCount, index);
                                            procStep = 0;
                                        }
                                    }
                            }
                        }


                        //(id text, logid text, url text, name text, datafromdate text)
                        using (Utils.ProgressBlock progress = new ProgressBlock(this, STR_LOADING, STR_LOADINGLOGIMAGES, logimgCount, 0))
                        {
                            int index = 0;
                            int procStep = 0;
                            using (MySqlCommand cmd = new MySqlCommand("select * from logimage", _dbcon))
                            using (MySqlDataReader dr = cmd.ExecuteReader())
                                while (dr.Read())
                                {
                                    Framework.Data.LogImage lg = new Framework.Data.LogImage();

                                    lg.ID = (string)dr["id"];
                                    lg.LogID = (string)dr["logid"];
                                    lg.Url = (string)dr["url"];
                                    lg.Name = (string)dr["name"];
                                    lg.DataFromDate = DateTime.Parse((string)dr["datafromdate"]).ToLocalTime();

                                    lg.Saved = true;
                                    lg.IsDataChanged = false;

                                    _logimgsInDB[lg.ID] = lg.ID;
                                    Core.LogImages.Add(lg);

                                    index++;
                                    procStep++;
                                    if (procStep >= 2000)
                                    {
                                        progress.UpdateProgress("Loading...", "Loading log images...", logimgCount, index);
                                        procStep = 0;
                                    }
                                }
                        }


                        //id text, code text, geocachecode text, name text, datafromdate text, comment text, description text, url text, urlname text, wptype integer, lat real, lon real, time text
                        using (Utils.ProgressBlock progress = new ProgressBlock(this, STR_LOADING, STR_LOADINGWAYPOINTS, wptCount, 0))
                        {
                            int index = 0;
                            int procStep = 0;
                            using (MySqlCommand cmd = new MySqlCommand("select * from waypoint", _dbcon))
                            {
                                cmd.CommandTimeout = 0;
                                using (MySqlDataReader dr = cmd.ExecuteReader())
                                    while (dr.Read())
                                    {
                                        Framework.Data.Waypoint wp = new Framework.Data.Waypoint();

                                        wp.ID = (string)dr["id"];
                                        wp.Code = (string)dr["code"];
                                        wp.Url = (string)dr["url"];
                                        wp.UrlName = (string)dr["urlname"];
                                        wp.Name = (string)dr["name"];
                                        wp.DataFromDate = DateTime.Parse((string)dr["datafromdate"]).ToLocalTime();
                                        wp.Comment = (string)dr["comment"];
                                        wp.GeocacheCode = (string)dr["geocachecode"];
                                        wp.Description = (string)dr["description"];
                                        wp.WPType = Utils.DataAccess.GetWaypointType(Core.WaypointTypes, (int)dr["wptype"]);
                                        object o = dr["lat"];
                                        if (o == null || o.GetType() == typeof(DBNull))
                                        {
                                            wp.Lat = null;
                                        }
                                        else
                                        {
                                            wp.Lat = (double?)(float)o;
                                        }
                                        o = dr["lon"];
                                        if (o == null || o.GetType() == typeof(DBNull))
                                        {
                                            wp.Lon = null;
                                        }
                                        else
                                        {
                                            wp.Lon = (double?)(float)o;
                                        }
                                        wp.Time = DateTime.Parse((string)dr["time"]).ToLocalTime();

                                        wp.Saved = true;
                                        wp.IsDataChanged = false;

                                        _wptsInDB[wp.Code] = wp.Code;
                                        Core.Waypoints.Add(wp);

                                        index++;
                                        procStep++;
                                        if (procStep >= 20000)
                                        {
                                            progress.UpdateProgress(STR_LOADING, STR_LOADINGWAYPOINTS, wptCount, index);
                                            procStep = 0;
                                        }
                                    }
                            }
                        }

                        using (MySqlCommand cmd = new MySqlCommand("select * from userwaypoint", _dbcon))
                        using (MySqlDataReader dr = cmd.ExecuteReader())
                            while (dr.Read())
                            {
                                Framework.Data.UserWaypoint wp = new Framework.Data.UserWaypoint();

                                wp.ID = (int)dr["id"];
                                wp.GeocacheCode = (string)dr["geocachecode"];
                                wp.Description = (string)dr["description"];
                                wp.Lat = (double)(float)dr["lat"];
                                wp.Lon = (double)(float)dr["lon"];
                                wp.Date = DateTime.Parse((string)dr["time"]).ToLocalTime();

                                wp.Saved = true;
                                wp.IsDataChanged = false;

                                _usrwptsInDB[wp.ID] = wp.ID;
                                Core.UserWaypoints.Add(wp);
                            }

                    }
                }
            }

            return result;
        }
Ejemplo n.º 19
0
        protected override void ImportMethod()
        {
            System.Collections.Hashtable logTypes = new System.Collections.Hashtable();
            using (Utils.ProgressBlock fixpr = new Utils.ProgressBlock(this, STR_IMPORTING, STR_IMPORTINGDATA, 1, 0))
            {
                String connect = String.Format("data source=file:{0}", _filename);
                using (SqliteConnection dbcon = new SqliteConnection(connect))
                {
                    //System.Diagnostics.Debugger.Break();
                    logTypes.Add("Found it", 2);
                    logTypes.Add("Didn't find it", 3);
                    logTypes.Add("Write note", 4);
                    logTypes.Add("Archive", 5);
                    logTypes.Add("Needs Archived", 7);
                    logTypes.Add("Will Attend", 9);
                    logTypes.Add("Attended", 10);
                    logTypes.Add("Webcam Photo Taken", 11);
                    logTypes.Add("Unarchive", 12);
                    logTypes.Add("Temporarily Disable Listing", 22);
                    logTypes.Add("Temporarily Disable", 22);
                    logTypes.Add("Enable Listing", 23);
                    logTypes.Add("Enable", 23);
                    logTypes.Add("Publish Listing", 24);
                    logTypes.Add("Publish", 24);
                    logTypes.Add("Retract Listing", 25);
                    logTypes.Add("Retract", 25);
                    logTypes.Add("Needs Maintenance", 45);
                    logTypes.Add("Owner Maintenance", 46);
                    logTypes.Add("Update Coordinates", 47);
                    logTypes.Add("Post Reviewer Note", 68);
                    logTypes.Add("Announcement", 74);
                    int index    = 0;
                    int procStep = 0;

                    dbcon.Open();

                    SqliteCommand lookup = new SqliteCommand("select aId, aInc from attributes where aCode = @Code", dbcon);
                    lookup.CommandType = CommandType.Text;
                    DbParameter par = lookup.CreateParameter();
                    par.Direction     = ParameterDirection.Input;
                    par.ParameterName = "@Code";
                    lookup.Parameters.Add(par);
                    lookup.Prepare();

                    SqliteCommand import = new SqliteCommand("select count(1) from caches", dbcon);
                    import.CommandType = CommandType.Text;


                    int gcCount = (int)(long)import.ExecuteScalar();
                    if (gcCount > 0)
                    {
                        using (Utils.ProgressBlock progress = new Utils.ProgressBlock(this, STR_IMPORTING, STR_IMPORTINGGEOCACHES, gcCount, 0))
                        {
                            bool isPremiumAvailable  = false;
                            bool isFavPointAvailable = false;
                            bool isGCNoteAvailable   = false;

                            try
                            {
                                import.CommandText = "select IsPremium from Caches limit 1";
                                using (SqliteDataReader checkdr = import.ExecuteReader())
                                {
                                    isPremiumAvailable = true;
                                }
                            }
                            catch
                            {
                            }

                            try
                            {
                                import.CommandText = "select FavPoints from Caches limit 1";
                                using (SqliteDataReader checkdr = import.ExecuteReader())
                                {
                                    isFavPointAvailable = true;
                                }
                            }
                            catch
                            {
                            }

                            try
                            {
                                import.CommandText = "select gcnote from Caches limit 1";
                                using (SqliteDataReader checkdr = import.ExecuteReader())
                                {
                                    isGCNoteAvailable = true;
                                }
                            }
                            catch
                            {
                            }

                            import.CommandText = "select caches.Code, Name, LastGPXDate, PlacedDate, Latitude, Longitude, Status, " +
                                                 "Archived, Country, State, County, CacheType, PlacedBy, OwnerName, OwnerId, Container, Terrain, Difficulty, ShortHTM" +
                                                 ", LongHTM, " +
                                                 string.Format("{0}", isPremiumAvailable? "IsPremium, ":"") +
                                                 " HasCorrected, LatOriginal, LonOriginal, UserFlag, Found, " +
                                                 string.Format("{0}", isFavPointAvailable? "FavPoints, ":"") +
                                                 " ShortDescription, LongDescription, Hints, Url, UserNote" +
                                                 string.Format("{0}", isGCNoteAvailable ? ", gcnote" : "") +
                                                 " from caches" +
                                                 " inner join cachememo on cachememo.code = caches.code";

                            SqliteDataReader dr = import.ExecuteReader();

                            while (dr.Read())
                            {
                                Framework.Data.Geocache gc = new Framework.Data.Geocache();
                                int cacheType;
                                try
                                {
                                    cacheType = getCacheType(((String)dr["CacheType"])[0]);
                                }
                                catch (ArgumentOutOfRangeException)
                                {
                                    continue;
                                }
                                int container = getContainer(((String)dr["Container"])[0]);

                                gc.Code = (string)dr["code"];
                                gc.Name = (string)dr["name"];
                                if (string.IsNullOrEmpty((string)dr["LastGPXDate"]))
                                {
                                    gc.DataFromDate = DateTime.Now.Date;
                                }
                                else
                                {
                                    gc.DataFromDate = DateTime.Parse((string)dr["LastGPXDate"]);
                                }
                                gc.Available    = ((String)dr["Status"]).Equals("A");
                                gc.Archived     = (int)dr["archived"] != 0;
                                gc.Country      = (string)dr["country"];
                                gc.State        = (string)dr["state"];
                                gc.Municipality = (string)dr["county"];

                                gc.GeocacheType           = Utils.DataAccess.GetGeocacheType(Core.GeocacheTypes, cacheType);
                                gc.PlacedBy               = (string)dr["placedby"];
                                gc.Owner                  = (string)dr["OwnerName"];
                                gc.OwnerId                = dr["ownerid"].GetType() == typeof(DBNull) ? "" : dr["ownerid"].ToString();
                                gc.Container              = Utils.DataAccess.GetGeocacheContainer(Core.GeocacheContainers, container);
                                gc.Terrain                = (double)dr["terrain"];
                                gc.Difficulty             = (double)dr["difficulty"];
                                gc.ShortDescription       = (string)dr["ShortDescription"];
                                gc.ShortDescriptionInHtml = (int)dr["ShortHTM"] != 0;
                                gc.LongDescription        = (string)dr["LongDescription"];
                                gc.LongDescriptionInHtml  = (int)dr["LongHTM"] != 0;
                                gc.EncodedHints           = (string)dr["Hints"];
                                gc.Url = (string)dr["url"];
                                if (isPremiumAvailable)
                                {
                                    gc.MemberOnly = (int)dr["IsPremium"] != 0;
                                }
                                else
                                {
                                    gc.MemberOnly = false;
                                }
                                gc.CustomCoords = (int)dr["HasCorrected"] != 0;
                                if (gc.CustomCoords)
                                {
                                    gc.CustomLat = Utils.Conversion.StringToDouble(dr["Latitude"] as String);
                                    gc.CustomLon = Utils.Conversion.StringToDouble(dr["Longitude"] as String);
                                    gc.Lat       = Utils.Conversion.StringToDouble(dr["LatOriginal"] as string);
                                    gc.Lon       = Utils.Conversion.StringToDouble(dr["LonOriginal"] as string);
                                }
                                else
                                {
                                    gc.Lat = Utils.Conversion.StringToDouble(dr["Latitude"] as string);
                                    gc.Lon = Utils.Conversion.StringToDouble(dr["Longitude"] as string);
                                }

                                par.Value = gc.Code;
                                DbDataReader attrs    = lookup.ExecuteReader();
                                List <int>   attrList = new List <int>();
                                while (attrs.Read())
                                {
                                    int attr = (int)(int)attrs["aId"];
                                    if (attrs["aInc"].ToString() == "0")
                                    {
                                        attr *= -1;
                                    }
                                    attrList.Add(attr);
                                }
                                attrs.Close();
                                gc.AttributeIds = attrList;

                                gc.Notes         = (string)dr["UserNote"];
                                gc.PublishedTime = DateTime.Parse((string)dr["PlacedDate"]);
                                if (isGCNoteAvailable)
                                {
                                    gc.PersonaleNote = (string)dr["gcnote"];
                                }
                                else
                                {
                                    gc.PersonaleNote = "";
                                }
                                gc.Flagged = (int)dr["UserFlag"] != 0;
                                gc.Found   = (int)dr["Found"] != 0;

                                if (isFavPointAvailable)
                                {
                                    gc.Favorites = (int)(int)dr["FavPoints"];
                                }
                                else
                                {
                                    gc.Favorites = 0;
                                }

                                gc.Selected = false;

                                Calculus.SetDistanceAndAngleGeocacheFromLocation(gc, Core.CenterLocation);

                                AddGeocache(gc, null);
                                index++;
                                procStep++;
                                if (procStep >= 500)
                                {
                                    progress.UpdateProgress(STR_IMPORTING, STR_IMPORTINGGEOCACHES, gcCount, index);
                                    procStep = 0;
                                }
                            }
                            dr.Close();
                        }

                        import.CommandText = "select count(1) from logs";
                        int logCount = (int)(long)import.ExecuteScalar();
                        if (logCount > 0)
                        {
                            using (Utils.ProgressBlock progress = new Utils.ProgressBlock(this, STR_IMPORTING, STR_IMPORTINGLOGS, logCount, 0))
                            {
                                index              = 0;
                                procStep           = 0;
                                import.CommandText = "select l.lLogId, l.lParent, lDate, lTime, lBy, lownerid, lEncoded, lType, lText " +
                                                     " from logs l" +
                                                     " inner join logmemo m on m.lLogId = l.lLogId and m.lParent = l.lParent";
                                DbDataReader dr = import.ExecuteReader();
                                while (dr.Read())
                                {
                                    Framework.Data.Log lg = new Framework.Data.Log();

                                    String type    = (String)dr["lType"];
                                    int    logType = (int)logTypes[type];

                                    //id text, gccode text, tbcode text, date text, finder text, finderid text, logtext text, encoded integer, datafromdate text, logtype integer
                                    lg.ID           = dr["lLogiD"].ToString();
                                    lg.GeocacheCode = (string)dr["lParent"];
                                    lg.TBCode       = "";
                                    lg.Date         = (DateTime)dr["lDate"];
                                    lg.Finder       = (string)dr["lBy"];
                                    lg.FinderId     = dr["lownerid"].ToString();
                                    lg.Text         = (string)dr["lText"];
                                    lg.Encoded      = (long)dr["lEncoded"] != 0;
                                    lg.DataFromDate = DateTime.Now;
                                    lg.LogType      = Utils.DataAccess.GetLogType(Core.LogTypes, logType);

                                    AddLog(lg);

                                    index++;
                                    procStep++;
                                    if (procStep >= 1000)
                                    {
                                        progress.UpdateProgress(STR_IMPORTING, STR_IMPORTINGLOGS, logCount, index);
                                        procStep = 0;
                                    }
                                }
                                dr.Close();
                            }
                        }
                        import.CommandText = "select count(1) from logimages";

                        int logimgCount = 0;
                        try
                        {
                            logimgCount = (int)(long)import.ExecuteScalar();
                        }
                        catch
                        {
                            //table does not exists
                        }
                        if (logimgCount > 0)
                        {
                            using (Utils.ProgressBlock progress = new Utils.ProgressBlock(this, STR_IMPORTING, STR_IMPORTINGLOGIMAGES, logimgCount, 0))
                            {
                                index              = 0;
                                procStep           = 0;
                                import.CommandText = "select iCode, iLogId, iImage, iName from logimages";
                                DbDataReader dr = import.ExecuteReader();
                                while (dr.Read())
                                {
                                    Framework.Data.LogImage lg = new Framework.Data.LogImage();

                                    lg.ID    = (string)dr["iCode"];
                                    lg.LogID = dr["iLogID"].ToString();
                                    lg.Url   = (string)dr["iImage"];
                                    lg.Name  = (string)dr["iName"];

                                    AddLogImage(lg);

                                    index++;
                                    procStep++;
                                    if (procStep >= 500)
                                    {
                                        progress.UpdateProgress(STR_IMPORTING, STR_IMPORTINGLOGIMAGES, logimgCount, index);
                                        procStep = 0;
                                    }
                                }
                                dr.Close();
                            }
                        }
                        //id text, code text, geocachecode text, name text, datafromdate text, comment text, description text, url text, urlname text, wptype integer, lat real, lon real, time text
                        import.CommandText = "select count(1) from waypoints";

                        int wptCount = (int)(long)import.ExecuteScalar();
                        if (wptCount > 0)
                        {
                            using (Utils.ProgressBlock progress = new Utils.ProgressBlock(this, STR_IMPORTING, STR_IMPORTINGWAYPOINTS, wptCount, 0))
                            {
                                index              = 0;
                                procStep           = 0;
                                import.CommandText = "select w.cParent, w.cCode, cName, cDate, cType, cLat, cLon," +
                                                     " cComment, cUrl" +
                                                     " from waypoints w" +
                                                     " inner join wayMemo m on w.cParent = m.cParent and w.cCode=m.cCode";
                                DbDataReader dr = import.ExecuteReader();
                                while (dr.Read())
                                {
                                    Framework.Data.Waypoint wp = new Framework.Data.Waypoint();

                                    int wpType = getWPType(((string)dr["cType"])[0]);

                                    wp.ID   = (string)dr["cCode"];
                                    wp.Code = (string)dr["cCode"];
                                    wp.Url  = (string)dr["cUrl"];
                                    //wp.UrlName = (string)dr["urlname"];
                                    wp.Name         = (string)dr["cName"];
                                    wp.DataFromDate = (DateTime)dr["cDate"];
                                    wp.Comment      = (string)dr["cComment"];
                                    wp.GeocacheCode = (string)dr["cParent"];
                                    //wp.Description = (string)dr["description"];
                                    wp.WPType = Utils.DataAccess.GetWaypointType(Core.WaypointTypes, wpType);
                                    double lat = Utils.Conversion.StringToDouble(dr["clat"] as string);
                                    double lon = Utils.Conversion.StringToDouble(dr["clon"] as string);
                                    if (Math.Abs(lat) < 0.00001)
                                    {
                                        wp.Lat = null;
                                    }
                                    else
                                    {
                                        wp.Lat = lat;
                                    }
                                    if (Math.Abs(lon) < 0.00001)
                                    {
                                        wp.Lon = null;
                                    }
                                    else
                                    {
                                        wp.Lon = lon;
                                    }
                                    wp.Time = (DateTime)dr["cDate"];

                                    wp.Description = wp.WPType.Name;
                                    wp.UrlName     = wp.WPType.Name;

                                    AddWaypoint(wp);

                                    index++;
                                    procStep++;
                                    if (procStep >= 500)
                                    {
                                        progress.UpdateProgress(STR_IMPORTING, STR_IMPORTINGWAYPOINTS, wptCount, index);
                                        procStep = 0;
                                    }
                                }
                                dr.Close();
                            }
                        }

                        try
                        {
                            //import corrected if table exists
                            import.CommandText = "select kCode, kAfterLat, kAfterLon from Corrected";
                            DbDataReader dr = import.ExecuteReader();
                            while (dr.Read())
                            {
                                string gcCode = dr["kCode"] as string ?? "";
                                Framework.Data.Geocache gc = Utils.DataAccess.GetGeocache(Core.Geocaches, gcCode);
                                if (gc != null)
                                {
                                    object oLat = dr["kAfterLat"];
                                    object oLon = dr["kAfterLon"];
                                    if (oLat != null && oLat.GetType() != typeof(DBNull) &&
                                        oLon != null && oLon.GetType() != typeof(DBNull))
                                    {
                                        string sLat = oLat as string;
                                        string sLon = oLon as string;
                                        if (sLat.Length > 0 && sLon.Length > 0)
                                        {
                                            gc.CustomLat = Utils.Conversion.StringToDouble(sLat);
                                            gc.CustomLon = Utils.Conversion.StringToDouble(sLon);
                                        }
                                    }
                                }
                            }
                        }
                        catch
                        {
                        }
                    }
                }
            }
        }
Ejemplo n.º 20
0
 public static Framework.Data.Waypoint Waypoint(Framework.Interfaces.ICore core, OKAPIService.Waypoint wp)
 {
     Framework.Data.Waypoint result = null;
     if (wp != null)
     {
         Framework.Data.Waypoint tmp = DataAccess.GetWaypoint(core.Waypoints, wp.name);
         result = new Framework.Data.Waypoint();
         if (tmp != null)
         {
             result.UpdateFrom(tmp);
         }
         result.Code = wp.name;
         result.DataFromDate = DateTime.Now;
         //for now map: parking, path, stage, physical-stage, virtual-stage, final, poi, other
         if (wp.sym == "parking")
         {
             result.WPType = DataAccess.GetWaypointType(core.WaypointTypes, 217);
         }
         else if (wp.sym == "path")
         {
             result.WPType = DataAccess.GetWaypointType(core.WaypointTypes, 452);
         }
         else if (wp.sym == "stage" || wp.sym == "physical-stage" || wp.sym == "virtual-stage")
         {
             result.WPType = DataAccess.GetWaypointType(core.WaypointTypes, 219);
         }
         else if (wp.sym == "final")
         {
             result.WPType = DataAccess.GetWaypointType(core.WaypointTypes, 220);
         }
         else if (wp.sym == "poi")
         {
             result.WPType = DataAccess.GetWaypointType(core.WaypointTypes, 452);
         }
         else
         {
             result.WPType = DataAccess.GetWaypointType(core.WaypointTypes, 452);
         }
         result.Comment = wp.description;
         result.Description = wp.description;
         result.GeocacheCode = wp.cache_code;
         result.ID = wp.name;
         result.Lat = Utils.Conversion.StringToDouble(wp.location.Substring(0, wp.location.IndexOf('|')));
         result.Lon = Utils.Conversion.StringToDouble(wp.location.Substring(wp.location.IndexOf('|') + 1));
         result.Name = wp.type_name;
         result.Time = DateTime.Now;
         result.Url = "";
         result.UrlName = wp.type_name;
     }
     return result;
 }
Ejemplo n.º 21
0
        public void ProcessGeocachingComGPXCaches(ResultData data, 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
                            //check if it is a child waypoint by checking the name
                            Framework.Data.Geocache parentGeocache = null;
                            string wpname = wp.SelectSingleNode("x:name", nsmgr).InnerText.Substring(2);
                            parentGeocache = (from g in data.Geocaches where g.Code.Substring(2) == wpname select g).FirstOrDefault();
                            if (parentGeocache == null)
                            {
                                parentGeocache = (from Framework.Data.Geocache g in _core.Geocaches where g.Code.Substring(2) == wpname select g).FirstOrDefault();
                                if (parentGeocache == null)
                                {
                                    //parent is not available
                                    //now what?
                                    continue;
                                }
                            }

                            Framework.Data.Waypoint cwp = new Framework.Data.Waypoint();

                            cwp.Code = wp.SelectSingleNode("x:name", nsmgr).InnerText;
                            cwp.GeocacheCode = string.Concat(parentGeocache.Code.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 = DataAccess.GetWaypointType(_core.WaypointTypes, wp.SelectSingleNode("x:sym", nsmgr).InnerText);

                            data.Waypoints.Add(cwp);

                            continue;
                        }

                        Framework.Data.Geocache gc = new Framework.Data.Geocache();

                        gc.Lat = Utils.Conversion.StringToDouble(SafeAttributeInnerText(wp, "lat", "0.0"));
                        gc.Lon = Utils.Conversion.StringToDouble(SafeAttributeInnerText(wp, "lon", "0.0"));
                        gc.Code = wp.SelectSingleNode("x:name", nsmgr).InnerText;
                        gc.Title = n.SelectSingleNode("y:name", nsmgr).InnerText;
                        gc.DataFromDate = _gpxDataTime;
                        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.Available = bool.Parse(n.Attributes["available"].InnerText);
                        gc.Archived = bool.Parse(SafeInnerText(n.Attributes["archived"], "False"));
                        gc.Country = SafeInnerText(n.SelectSingleNode("y:country", nsmgr), "");
                        gc.State = SafeInnerText(n.SelectSingleNode("y:state", nsmgr), "");
                        gc.ID = SafeInnerText(n.Attributes["id"], "");
                        gc.OwnerId = SafeAttributeInnerText(n.SelectSingleNode("y:owner", nsmgr), "id", "");
                        if (_cachesGpxVersion >= V102)
                        {
                            gc.GeocacheType = DataAccess.GetGeocacheType(_core.GeocacheTypes, int.Parse(SafeAttributeInnerText(n.SelectSingleNode("y:type", nsmgr), "id", "-1")));
                            gc.Container = DataAccess.GetGeocacheContainer(_core.GeocacheContainers, 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.PersonaleNote = 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 = DataAccess.GetGeocacheType(_core.GeocacheTypes, srchTxt);
                            if (isGeoSpy)
                            {
                                gc.Container = DataAccess.GetGeocacheContainer(_core.GeocacheContainers, "Virtual");
                            }
                            else
                            {
                                gc.Container = DataAccess.GetGeocacheContainer(_core.GeocacheContainers, 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 = Conversion.StringToDouble(SafeInnerText(n.SelectSingleNode("y:terrain", nsmgr), "1"));
                        gc.Difficulty = 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), "");

                        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);
                                }
                            }
                        }

                        Calculus.SetDistanceAndAngleGeocacheFromLocation(gc, _core.CenterLocation);

                        data.Geocaches.Add(gc);

                        //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)
                                {
                                    Framework.Data.Log lg = new Framework.Data.Log();

                                    lg.GeocacheCode = gc.Code;
                                    string lid = SafeAttributeInnerText(l, "id", "");

                                    if (lid.StartsWith("GL"))
                                    {
                                        lg.ID = lid;
                                    }
                                    else
                                    {
                                        if (string.IsNullOrEmpty(lid) || lid.StartsWith("-"))
                                        {
                                            continue;
                                        }
                                        try
                                        {
                                            lg.ID = string.Concat("GL", Utils.Conversion.GetCacheCodeFromCacheID(int.Parse(lid)).Substring(2));
                                        }
                                        catch
                                        {
                                            continue;
                                        }
                                    }

                                    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 = 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 = DataAccess.GetLogType(_core.LogTypes, int.Parse(l.SelectSingleNode("y:type", nsmgr).Attributes["id"].InnerText));
                                    }
                                    else
                                    {
                                        lg.LogType = DataAccess.GetLogType(_core.LogTypes, l.SelectSingleNode("y:type", nsmgr).InnerText);
                                    }

                                    data.Logs.Add(lg);

                                    //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)
                                            {
                                                Framework.Data.LogImage lgi = new Framework.Data.LogImage();

                                                lgi.Url = li.SelectSingleNode("y:url", nsmgr).InnerText;
                                                lgi.ID = lgi.Url;
                                                lgi.LogID = lg.ID;
                                                lgi.Name = li.SelectSingleNode("y:name", nsmgr).InnerText;
                                                lgi.DataFromDate = _gpxDataTime;

                                                data.LogImages.Add(lgi);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch
            {
            }

        }
Ejemplo n.º 22
0
        protected override void ImportMethod()
        {
            System.Collections.Hashtable logTypes = new System.Collections.Hashtable();
            using (Utils.ProgressBlock fixpr = new Utils.ProgressBlock(this, STR_IMPORTING, STR_IMPORTINGDATA, 1, 0))
            {
                String connect = String.Format("data source=file:{0}", _filename);
                using (SqliteConnection dbcon = new SqliteConnection(connect))
                {

                    //System.Diagnostics.Debugger.Break();
                    logTypes.Add("Found it", 2);
                    logTypes.Add("Didn't find it", 3);
                    logTypes.Add("Write note", 4);
                    logTypes.Add("Archive", 5);
                    logTypes.Add("Needs Archived", 7);
                    logTypes.Add("Will Attend", 9);
                    logTypes.Add("Attended", 10);
                    logTypes.Add("Webcam Photo Taken", 11);
                    logTypes.Add("Unarchive", 12);
                    logTypes.Add("Temporarily Disable Listing", 22);
                    logTypes.Add("Enable Listing", 23);
                    logTypes.Add("Publish Listing", 24);
                    logTypes.Add("Retract Listing", 25);
                    logTypes.Add("Needs Maintenance", 45);
                    logTypes.Add("Owner Maintenance", 46);
                    logTypes.Add("Update Coordinates", 47);
                    logTypes.Add("Post Reviewer Note", 68);
                    logTypes.Add("Announcement", 74);
                    int index = 0;
                    int procStep = 0;

                    dbcon.Open();

                    SqliteCommand lookup = new SqliteCommand("select aId, aInc from attributes where aCode = @Code", dbcon);
                    lookup.CommandType = CommandType.Text;
                    DbParameter par = lookup.CreateParameter();
                    par.Direction = ParameterDirection.Input;
                    par.ParameterName = "@Code";
                    lookup.Parameters.Add(par);
                    lookup.Prepare();

                    SqliteCommand import = new SqliteCommand("select count(1) from caches", dbcon);
                    import.CommandType = CommandType.Text;


                    int gcCount = (int)(long)import.ExecuteScalar();
                    if (gcCount > 0)
                    {
                        using (Utils.ProgressBlock progress = new Utils.ProgressBlock(this, STR_IMPORTING, STR_IMPORTINGGEOCACHES, gcCount, 0))
                        {

                            bool isPremiumAvailable = false;
                            bool isFavPointAvailable = false;
                            bool isGCNoteAvailable = false;

                            try
                            {
                                import.CommandText = "select IsPremium from Caches limit 1";
                                using (SqliteDataReader checkdr = import.ExecuteReader())
                                {
                                    isPremiumAvailable = true;
                                }
                            }
                            catch
                            {
                            }

                            try
                            {
                                import.CommandText = "select FavPoints from Caches limit 1";
                                using (SqliteDataReader checkdr = import.ExecuteReader())
                                {
                                    isFavPointAvailable = true;
                                }
                            }
                            catch
                            {
                            }

                            try
                            {
                                import.CommandText = "select gcnote from Caches limit 1";
                                using (SqliteDataReader checkdr = import.ExecuteReader())
                                {
                                    isGCNoteAvailable = true;
                                }
                            }
                            catch
                            {
                            }

                            import.CommandText = "select caches.Code, Name, LastGPXDate, PlacedDate, Latitude, Longitude, Status, " +
                                "Archived, Country, State, CacheType, PlacedBy, OwnerName, OwnerId, Container, Terrain, Difficulty, ShortHTM" +
                                ", LongHTM, " +
                                string.Format("{0}", isPremiumAvailable? "IsPremium, ":"") +
                                " HasCorrected, LatOriginal, LonOriginal, UserFlag, Found, " +
                                string.Format("{0}", isFavPointAvailable? "FavPoints, ":"") +
                                " ShortDescription, LongDescription, Hints, Url, UserNote" +
                                string.Format("{0}", isGCNoteAvailable ? ", gcnote" : "") +
                                " from caches" +
                                " inner join cachememo on cachememo.code = caches.code";

                            SqliteDataReader dr = import.ExecuteReader();

                            while (dr.Read())
                            {
                                Framework.Data.Geocache gc = new Framework.Data.Geocache();
                                int cacheType;
                                try
                                {
                                    cacheType = getCacheType(((String)dr["CacheType"])[0]);
                                }
                                catch (ArgumentOutOfRangeException)
                                {
                                    continue;
                                }
                                int container = getContainer(((String)dr["Container"])[0]);

                                gc.Code = (string)dr["code"];
                                gc.Name = (string)dr["name"];
                                gc.DataFromDate = DateTime.Parse((string)dr["LastGPXDate"]);
                                gc.Available = ((String)dr["Status"]).Equals("A");
                                gc.Archived = (int)dr["archived"] != 0;
                                gc.Country = (string)dr["country"];
                                gc.State = (string)dr["state"];

                                gc.GeocacheType = Utils.DataAccess.GetGeocacheType(Core.GeocacheTypes, cacheType);
                                gc.PlacedBy = (string)dr["placedby"];
                                gc.Owner = (string)dr["OwnerName"];
                                gc.OwnerId = dr["ownerid"].GetType() == typeof(DBNull) ? "" : dr["ownerid"].ToString();
                                gc.Container = Utils.DataAccess.GetGeocacheContainer(Core.GeocacheContainers, container);
                                gc.Terrain = (double)dr["terrain"];
                                gc.Difficulty = (double)dr["difficulty"];
                                gc.ShortDescription = (string)dr["ShortDescription"];
                                gc.ShortDescriptionInHtml = (int)dr["ShortHTM"] != 0;
                                gc.LongDescription = (string)dr["LongDescription"];
                                gc.LongDescriptionInHtml = (int)dr["LongHTM"] != 0;
                                gc.EncodedHints = (string)dr["Hints"];
                                gc.Url = (string)dr["url"];
                                if (isPremiumAvailable)
                                {
                                    gc.MemberOnly = (int)dr["IsPremium"] != 0;
                                }
                                else
                                {
                                    gc.MemberOnly = false;
                                }
                                gc.CustomCoords = (int)dr["HasCorrected"] != 0;
                                if (gc.CustomCoords)
                                {
                                    gc.CustomLat = Utils.Conversion.StringToDouble(dr["Latitude"] as String);
                                    gc.CustomLon = Utils.Conversion.StringToDouble(dr["Longitude"] as String);
                                    gc.Lat = Utils.Conversion.StringToDouble(dr["LatOriginal"] as string);
                                    gc.Lon = Utils.Conversion.StringToDouble(dr["LonOriginal"] as string);
                                }
                                else
                                {
                                    gc.Lat = Utils.Conversion.StringToDouble(dr["Latitude"] as string);
                                    gc.Lon = Utils.Conversion.StringToDouble(dr["Longitude"] as string);
                                }

                                par.Value = gc.Code;
                                DbDataReader attrs = lookup.ExecuteReader();
                                List<int> attrList = new List<int>();
                                while (attrs.Read())
                                {
                                    int attr = (int)(int)attrs["aId"];
                                    if (attrs["aInc"].ToString() == "0")
                                    {
                                        attr *= -1;
                                    }
                                    attrList.Add(attr);
                                }
                                attrs.Close();
                                gc.AttributeIds = attrList;

                                gc.Notes = (string)dr["UserNote"];
                                gc.PublishedTime = DateTime.Parse((string)dr["PlacedDate"]);
                                if (isGCNoteAvailable)
                                {
                                    gc.PersonaleNote = (string)dr["gcnote"];
                                }
                                else
                                {
                                    gc.PersonaleNote = "";
                                }
                                gc.Flagged = (int)dr["UserFlag"] != 0;
                                gc.Found = (int)dr["Found"] != 0;

                                if (isFavPointAvailable)
                                {
                                    gc.Favorites = (int)(int)dr["FavPoints"];
                                }
                                else
                                {
                                    gc.Favorites = 0;
                                }

                                gc.Selected = false;

                                Calculus.SetDistanceAndAngleGeocacheFromLocation(gc, Core.CenterLocation);

                                AddGeocache(gc, null);
                                index++;
                                procStep++;
                                if (procStep >= 500)
                                {
                                    progress.UpdateProgress(STR_IMPORTING, STR_IMPORTINGGEOCACHES, gcCount, index);
                                    procStep = 0;
                                }
                            }
                            dr.Close();
                        }

                        import.CommandText = "select count(1) from logs";
                        int logCount = (int)(long)import.ExecuteScalar();
                        if (logCount > 0)
                        {
                            using (Utils.ProgressBlock progress = new Utils.ProgressBlock(this, STR_IMPORTING, STR_IMPORTINGLOGS, logCount, 0))
                            {
                                index = 0;
                                procStep = 0;
                                import.CommandText = "select l.lLogId, l.lParent, lDate, lTime, lBy, lownerid, lEncoded, lType, lText " +
                                    " from logs l" +
                                    " inner join logmemo m on m.lLogId = l.lLogId and m.lParent = l.lParent";
                                DbDataReader dr = import.ExecuteReader();
                                while (dr.Read())
                                {
                                    Framework.Data.Log lg = new Framework.Data.Log();

                                    String type = (String)dr["lType"];
                                    int logType = (int)logTypes[type];

                                    //id text, gccode text, tbcode text, date text, finder text, finderid text, logtext text, encoded integer, datafromdate text, logtype integer
                                    lg.ID = dr["lLogiD"].ToString();
                                    lg.GeocacheCode = (string)dr["lParent"];
                                    lg.TBCode = "";
                                    lg.Date = (DateTime)dr["lDate"];
                                    lg.Finder = (string)dr["lBy"];
                                    lg.FinderId = dr["lownerid"].ToString();
                                    lg.Text = (string)dr["lText"];
                                    lg.Encoded = (long)dr["lEncoded"]!=0;
                                    lg.DataFromDate = DateTime.Now;
                                    lg.LogType = Utils.DataAccess.GetLogType(Core.LogTypes, logType);

                                    AddLog(lg);

                                    index++;
                                    procStep++;
                                    if (procStep >= 1000)
                                    {
                                        progress.UpdateProgress(STR_IMPORTING, STR_IMPORTINGLOGS, logCount, index);
                                        procStep = 0;
                                    }
                                }
                                dr.Close();
                            }
                        }
                        import.CommandText = "select count(1) from logimages";

                        int logimgCount = 0;
                        try
                        {
                            logimgCount = (int)(long)import.ExecuteScalar();
                        }
                        catch
                        {
                            //table does not exists
                        }
                        if (logimgCount > 0)
                        {
                            using (Utils.ProgressBlock progress = new Utils.ProgressBlock(this, STR_IMPORTING, STR_IMPORTINGLOGIMAGES, logimgCount, 0))
                            {
                                index = 0;
                                procStep = 0;
                                import.CommandText = "select iCode, iLogId, iImage, iName from logimages";
                                DbDataReader dr = import.ExecuteReader();
                                while (dr.Read())
                                {
                                    Framework.Data.LogImage lg = new Framework.Data.LogImage();

                                    lg.ID = (string)dr["iCode"];
                                    lg.LogID = dr["iLogID"].ToString();
                                    lg.Url = (string)dr["iImage"];
                                    lg.Name = (string)dr["iName"];

                                    AddLogImage(lg);

                                    index++;
                                    procStep++;
                                    if (procStep >= 500)
                                    {
                                        progress.UpdateProgress(STR_IMPORTING, STR_IMPORTINGLOGIMAGES, logimgCount, index);
                                        procStep = 0;
                                    }
                                }
                                dr.Close();
                            }
                        }
                        //id text, code text, geocachecode text, name text, datafromdate text, comment text, description text, url text, urlname text, wptype integer, lat real, lon real, time text
                        import.CommandText = "select count(1) from waypoints";

                        int wptCount = (int)(long)import.ExecuteScalar();
                        if (wptCount > 0)
                        {
                            using (Utils.ProgressBlock progress = new Utils.ProgressBlock(this, STR_IMPORTING, STR_IMPORTINGWAYPOINTS, wptCount, 0))
                            {
                                index = 0;
                                procStep = 0;
                                import.CommandText = "select w.cParent, w.cCode, cName, cDate, cType, cLat, cLon," +
                                    " cComment, cUrl" +
                                    " from waypoints w" +
                                    " inner join wayMemo m on w.cParent = m.cParent and w.cCode=m.cCode";
                                DbDataReader dr = import.ExecuteReader();
                                while (dr.Read())
                                {
                                    Framework.Data.Waypoint wp = new Framework.Data.Waypoint();

                                    int wpType = getWPType(((string)dr["cType"])[0]);

                                    wp.ID = (string)dr["cCode"];
                                    wp.Code = (string)dr["cCode"];
                                    wp.Url = (string)dr["cUrl"];
                                    //wp.UrlName = (string)dr["urlname"];
                                    wp.Name = (string)dr["cName"];
                                    wp.DataFromDate = (DateTime)dr["cDate"];
                                    wp.Comment = (string)dr["cComment"];
                                    wp.GeocacheCode = (string)dr["cParent"];
                                    //wp.Description = (string)dr["description"];
                                    wp.WPType = Utils.DataAccess.GetWaypointType(Core.WaypointTypes, wpType);
                                    double lat = Utils.Conversion.StringToDouble(dr["clat"] as string);
                                    double lon = Utils.Conversion.StringToDouble(dr["clon"] as string);
                                    if (Math.Abs(lat) < 0.00001)
                                    {
                                        wp.Lat = null;
                                    }
                                    else
                                    {
                                        wp.Lat = lat;
                                    }
                                    if (Math.Abs(lon) < 0.00001)
                                    {
                                        wp.Lon = null;
                                    }
                                    else
                                    {
                                        wp.Lon = lon;
                                    }
                                    wp.Time = (DateTime)dr["cDate"];

                                    wp.Description = wp.WPType.Name;
                                    wp.UrlName = wp.WPType.Name;

                                    AddWaypoint(wp);

                                    index++;
                                    procStep++;
                                    if (procStep >= 500)
                                    {
                                        progress.UpdateProgress(STR_IMPORTING, STR_IMPORTINGWAYPOINTS, wptCount, index);
                                        procStep = 0;
                                    }
                                }
                                dr.Close();
                            }
                        }

                        try
                        {
                            //import corrected if table exists
                            import.CommandText = "select kCode, kAfterLat, kAfterLon from Corrected";
                            DbDataReader dr = import.ExecuteReader();
                            while (dr.Read())
                            {
                                string gcCode = dr["kCode"] as string ?? "";
                                Framework.Data.Geocache gc = Utils.DataAccess.GetGeocache(Core.Geocaches, gcCode);
                                if (gc != null)
                                {
                                    object oLat = dr["kAfterLat"];
                                    object oLon = dr["kAfterLon"];
                                    if (oLat != null && oLat.GetType() != typeof(DBNull) &&
                                        oLon != null && oLon.GetType() != typeof(DBNull))
                                    {
                                        string sLat = oLat as string;
                                        string sLon = oLon as string;
                                        if (sLat.Length > 0 && sLon.Length > 0)
                                        {
                                            gc.CustomLat = Utils.Conversion.StringToDouble(sLat);
                                            gc.CustomLon = Utils.Conversion.StringToDouble(sLon);
                                        }
                                    }
                                }
                            }
                        }
                        catch
                        {
                        }

                    }
                }
            }
        }
Ejemplo n.º 23
0
        public void Import(Framework.Interfaces.ICore core, Utils.BasePlugin.Plugin owner, string fileName)
        {
            using (Utils.ProgressBlock upd = new Utils.ProgressBlock(owner, STR_IMPORT, STR_IMPORT, 1, 0))
            {
                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(owner, STR_IMPORT, STR_IMPORT_CACHES, count, 0))
                            {
                                for (int index = 0; index < count; index++)
                                {
                                    Framework.Data.Geocache gc = new Framework.Data.Geocache();
                                    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(core.GeocacheContainers, br.ReadInt32());
                                    gc.CustomCoords = 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(core.GeocacheTypes, br.ReadInt32());
                                    gc.ID = 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.PersonaleNote = br.ReadString();
                                    gc.PlacedBy = br.ReadString();
                                    gc.PublishedTime = DateTime.Parse(br.ReadString());
                                    gc.Selected = br.ReadBoolean();
                                    gc.ShortDescription = br.ReadString();
                                    gc.ShortDescriptionInHtml = br.ReadBoolean();
                                    gc.State = br.ReadString();
                                    gc.Terrain = br.ReadDouble();
                                    gc.Title = br.ReadString();
                                    gc.Url = br.ReadString();
                                    gc.DataFromDate = DateTime.Parse(br.ReadString());
                                    if (version > 1)
                                    {
                                        gc.Locked = br.ReadBoolean();
                                    }

                                    Calculus.SetDistanceAndAngleGeocacheFromLocation(gc, core.CenterLocation);
                                    (owner as Import).AddImportedGeocache(gc);

                                    int logcount = br.ReadInt32();
                                    for (int lc = 0; lc < logcount; lc++)
                                    {
                                        Framework.Data.Log log = new Framework.Data.Log();
                                        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(core.LogTypes, br.ReadInt32());
                                        log.TBCode = br.ReadString();
                                        log.Text = br.ReadString();

                                        (owner as Import).AddImportedLog(log);

                                        int logimgcount = br.ReadInt32();
                                        for (int lic = 0; lic < logimgcount; lic++)
                                        {
                                            Framework.Data.LogImage li = new Framework.Data.LogImage();
                                            li.DataFromDate = DateTime.Parse(br.ReadString());
                                            li.ID = br.ReadString();
                                            li.LogID = br.ReadString();
                                            li.Name = br.ReadString();
                                            li.Url = br.ReadString();

                                            (owner as Import).AddImportedLogImage(li);
                                        }
                                    }

                                    int wpcount = br.ReadInt32();
                                    for (int wpc = 0; wpc < wpcount; wpc++)
                                    {
                                        Framework.Data.Waypoint wp = new Framework.Data.Waypoint();
                                        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(core.WaypointTypes, br.ReadInt32());

                                        (owner as Import).AddImportedWaypoint(wp);
                                    }

                                    if (version > 2)
                                    {
                                        int usrwpcount = br.ReadInt32();
                                        for (int wpc = 0; wpc < usrwpcount; wpc++)
                                        {
                                            Framework.Data.UserWaypoint wp = new Framework.Data.UserWaypoint();
                                            wp.ID = br.ReadInt32();
                                            wp.Description = br.ReadString();
                                            wp.GeocacheCode = br.ReadString();
                                            wp.Lat = br.ReadDouble();
                                            wp.Lon = br.ReadDouble();
                                            wp.Date = DateTime.Parse(br.ReadString());

                                            (owner as Import).AddImportedUserWaypoint(wp);
                                        }
                                    }

                                    if (index % 200 == 0)
                                    {
                                        prog.UpdateProgress(STR_IMPORT, STR_IMPORT_CACHES, count, index);
                                    }
                                }
                            }
                        }
                        else
                        {
                            //message
                        }
                    }
                    else
                    {
                        //message
                    }
                }
            }
        }
Ejemplo n.º 24
0
        public void ProcessGeocachingComGPXWaypoints(ResultData data, string gpxDoc)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(gpxDoc);
                XmlElement root = doc.DocumentElement;
                XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
                nsmgr.AddNamespace("x", root.NamespaceURI);

                _gpxDataTime = DateTime.Parse(root.SelectSingleNode("x:time", nsmgr).InnerText);

                XmlNodeList wps = root.SelectNodes("x:wpt", nsmgr);
                if (wps != null)
                {
                    foreach (XmlNode n in wps)
                    {
                        Framework.Data.Waypoint wp = new Framework.Data.Waypoint();

                        wp.Code = n.SelectSingleNode("x:name", nsmgr).InnerText;
                        wp.GeocacheCode = string.Concat("GC",wp.Code.Substring(2));
                        wp.DataFromDate = _gpxDataTime;
                        wp.Comment = n.SelectSingleNode("x:cmt", nsmgr).InnerText;
                        wp.Lat = Utils.Conversion.StringToDouble(n.Attributes["lat"].InnerText);
                        wp.Lon = Utils.Conversion.StringToDouble(n.Attributes["lon"].InnerText);
                        wp.Description = n.SelectSingleNode("x:desc", nsmgr).InnerText;
                        wp.ID = wp.Code;
                        wp.Name = wp.Code;
                        wp.Comment = n.SelectSingleNode("x:cmt", nsmgr).InnerText;
                        wp.Time = DateTime.Parse(n.SelectSingleNode("x:time", nsmgr).InnerText);
                        wp.Url = n.SelectSingleNode("x:url", nsmgr).InnerText;
                        wp.UrlName = n.SelectSingleNode("x:urlname", nsmgr).InnerText;
                        wp.WPType = DataAccess.GetWaypointType(_core.WaypointTypes, n.SelectSingleNode("x:sym", nsmgr).InnerText);

                        data.Waypoints.Add(wp);
                    }
                }

            }
            catch
            {
            }
        }
Ejemplo n.º 25
0
        public void Import(Framework.Interfaces.ICore core, Utils.BasePlugin.Plugin owner, string fileName)
        {
            using (Utils.ProgressBlock upd = new Utils.ProgressBlock(owner, STR_IMPORT, STR_IMPORT, 1, 0))
            {
                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(owner, STR_IMPORT, STR_IMPORT_CACHES, count, 0))
                                {
                                    for (int index = 0; index < count; index++)
                                    {
                                        Framework.Data.Geocache gc = new Framework.Data.Geocache();
                                        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(core.GeocacheContainers, br.ReadInt32());
                                        gc.CustomCoords = 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(core.GeocacheTypes, br.ReadInt32());
                                        gc.ID                     = 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.PersonaleNote          = br.ReadString();
                                        gc.PlacedBy               = br.ReadString();
                                        gc.PublishedTime          = DateTime.Parse(br.ReadString());
                                        gc.Selected               = br.ReadBoolean();
                                        gc.ShortDescription       = br.ReadString();
                                        gc.ShortDescriptionInHtml = br.ReadBoolean();
                                        gc.State                  = br.ReadString();
                                        gc.Terrain                = br.ReadDouble();
                                        gc.Title                  = br.ReadString();
                                        gc.Url                    = br.ReadString();
                                        gc.DataFromDate           = DateTime.Parse(br.ReadString());
                                        if (version > 1)
                                        {
                                            gc.Locked = br.ReadBoolean();
                                        }

                                        Calculus.SetDistanceAndAngleGeocacheFromLocation(gc, core.CenterLocation);
                                        (owner as Import).AddImportedGeocache(gc);

                                        int logcount = br.ReadInt32();
                                        for (int lc = 0; lc < logcount; lc++)
                                        {
                                            Framework.Data.Log log = new Framework.Data.Log();
                                            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(core.LogTypes, br.ReadInt32());
                                            log.TBCode       = br.ReadString();
                                            log.Text         = br.ReadString();

                                            (owner as Import).AddImportedLog(log);

                                            int logimgcount = br.ReadInt32();
                                            for (int lic = 0; lic < logimgcount; lic++)
                                            {
                                                Framework.Data.LogImage li = new Framework.Data.LogImage();
                                                li.DataFromDate = DateTime.Parse(br.ReadString());
                                                li.ID           = br.ReadString();
                                                li.LogID        = br.ReadString();
                                                li.Name         = br.ReadString();
                                                li.Url          = br.ReadString();

                                                (owner as Import).AddImportedLogImage(li);
                                            }
                                        }

                                        int wpcount = br.ReadInt32();
                                        for (int wpc = 0; wpc < wpcount; wpc++)
                                        {
                                            Framework.Data.Waypoint wp = new Framework.Data.Waypoint();
                                            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(core.WaypointTypes, br.ReadInt32());

                                            (owner as Import).AddImportedWaypoint(wp);
                                        }

                                        if (version > 2)
                                        {
                                            int usrwpcount = br.ReadInt32();
                                            for (int wpc = 0; wpc < usrwpcount; wpc++)
                                            {
                                                Framework.Data.UserWaypoint wp = new Framework.Data.UserWaypoint();
                                                wp.ID           = br.ReadInt32();
                                                wp.Description  = br.ReadString();
                                                wp.GeocacheCode = br.ReadString();
                                                wp.Lat          = br.ReadDouble();
                                                wp.Lon          = br.ReadDouble();
                                                wp.Date         = DateTime.Parse(br.ReadString());

                                                (owner as Import).AddImportedUserWaypoint(wp);
                                            }
                                        }

                                        if (index % 200 == 0)
                                        {
                                            prog.UpdateProgress(STR_IMPORT, STR_IMPORT_CACHES, count, index);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                //message
                            }
                        }
                        else
                        {
                            //message
                        }
                    }
            }
        }
Ejemplo n.º 26
0
        private bool insertFromDatabase(bool overwrite)
        {
            bool result = false;

            try
            {
                int lsize = sizeof(long);
                byte[] memBuffer = new byte[10 * 1024 * 1024];
                using (MemoryStream ms = new MemoryStream(memBuffer))
                using (BinaryReader br = new BinaryReader(ms))
                using (FileCollection fc = new FileCollection(_selectedInsertFromFilename))
                {

                    int gcCount = 0;
                    int logCount = 0;
                    int logimgCount = 0;
                    int geocacheimgCount = 0;
                    int wptCount = 0;
                    int usrwptCount = 0;

                    Hashtable htInsertedGeocaches = new Hashtable();
                    Hashtable htInsertedLogs = new Hashtable();

                    XmlDocument doc = new XmlDocument();
                    doc.Load(fc.DatabaseInfoFilename);
                    XmlElement root = doc.DocumentElement;
                    gcCount = int.Parse(root.SelectSingleNode("GeocacheCount").InnerText);
                    logCount = int.Parse(root.SelectSingleNode("LogCount").InnerText);
                    logimgCount = int.Parse(root.SelectSingleNode("LogImagesCount").InnerText);
                    wptCount = int.Parse(root.SelectSingleNode("WaypointCount").InnerText);
                    usrwptCount = int.Parse(root.SelectSingleNode("UserWaypointCount").InnerText);
                    if (root.SelectSingleNode("GeocacheImagesCount") != null)
                    {
                        geocacheimgCount = int.Parse(root.SelectSingleNode("GeocacheImagesCount").InnerText);
                    }

                    DateTime nextUpdateTime = DateTime.MinValue;
                    RecordInfo ri = new RecordInfo();
                    using (Utils.ProgressBlock fixscr = new Utils.ProgressBlock(this, STR_LOADING, STR_LOADINGDATA, 1, 0))
                    {
                        using (Utils.ProgressBlock progress = new Utils.ProgressBlock(this, STR_LOADING, STR_LOADINGGEOCACHES, gcCount, 0))
                        {
                            int index = 0;

                            FileStream fs = fc._fsGeocaches;
                            fs.Position = 0;
                            long eof = fs.Length;
                            while (fs.Position < eof)
                            {
                                ri.Offset = fs.Position;
                                fs.Read(memBuffer, 0, lsize + 1);
                                ms.Position = 0;
                                ri.Length = br.ReadInt64();
                                if (memBuffer[lsize] == 0)
                                {
                                    //free
                                    //ignore
                                    fs.Position = ri.Offset + ri.Length;
                                }
                                else if (memBuffer[lsize] == 2)
                                {
                                    //read
                                    fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                    ms.Position = 0;
                                    ri.ID = br.ReadString().Substring(2);
                                    bool newGeocache;
                                    Framework.Data.Geocache gc = Utils.DataAccess.GetGeocache(Core.Geocaches, ri.ID);
                                    if (gc == null)
                                    {
                                        gc = new Framework.Data.Geocache();
                                        htInsertedGeocaches.Add(ri.ID, gc);
                                        newGeocache = true;
                                    }
                                    else
                                    {
                                        if (overwrite || htInsertedGeocaches[ri.ID] != null)
                                        {
                                            newGeocache = false;
                                        }
                                        else
                                        {
                                            continue;
                                        }
                                    }
                                    gc.BeginUpdate();
                                    try
                                    {
                                        gc.Code = ri.ID;
                                        gc.ShortDescription = br.ReadString();
                                        gc.ShortDescriptionInHtml = br.ReadBoolean();
                                        gc.LongDescription = br.ReadString();
                                        gc.LongDescriptionInHtml = br.ReadBoolean();
                                    }
                                    catch
                                    {
                                    }
                                    gc.EndUpdate();
                                    if (newGeocache)
                                    {
                                        Core.Geocaches.Add(gc);
                                    }
                                }
                                else
                                {
                                    //read

                                    fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                    ms.Position = 0;
                                    ri.ID = br.ReadString();

                                    bool newGeocache;
                                    Framework.Data.Geocache gc = Utils.DataAccess.GetGeocache(Core.Geocaches, ri.ID);
                                    if (gc == null)
                                    {
                                        gc = new Framework.Data.Geocache();
                                        htInsertedGeocaches.Add(ri.ID, gc);
                                        newGeocache = true;
                                    }
                                    else
                                    {
                                        if (overwrite || htInsertedGeocaches[ri.ID] != null)
                                        {
                                            newGeocache = false;
                                        }
                                        else
                                        {
                                            continue;
                                        }
                                    }
                                    gc.BeginUpdate();
                                    try
                                    {
                                        gc.Code = ri.ID;
                                        gc.Archived = br.ReadBoolean();
                                        gc.AttributeIds = ReadIntegerArray(br);
                                        gc.Available = br.ReadBoolean();
                                        gc.City = br.ReadString();
                                        gc.Container = Utils.DataAccess.GetGeocacheContainer(Core.GeocacheContainers, br.ReadInt32());
                                        gc.CustomCoords = 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();
                                        gc.GeocacheType = Utils.DataAccess.GetGeocacheType(Core.GeocacheTypes, br.ReadInt32());
                                        gc.ID = br.ReadString();
                                        gc.Lat = br.ReadDouble();
                                        gc.Lon = br.ReadDouble();
                                        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.PersonaleNote = br.ReadString();
                                        gc.PlacedBy = br.ReadString();
                                        gc.PublishedTime = DateTime.Parse(br.ReadString());
                                        gc.State = br.ReadString();
                                        gc.Terrain = br.ReadDouble();
                                        gc.Title = br.ReadString();
                                        gc.Url = br.ReadString();
                                        gc.DataFromDate = DateTime.Parse(br.ReadString());
                                        gc.Locked = br.ReadBoolean();

                                        Calculus.SetDistanceAndAngleGeocacheFromLocation(gc, Core.CenterLocation);
                                    }
                                    catch
                                    {
                                    }
                                    gc.EndUpdate();

                                    if (newGeocache)
                                    {
                                        Core.Geocaches.Add(gc);
                                    }

                                    index++;
                                    if (DateTime.Now >= nextUpdateTime)
                                    {
                                        progress.UpdateProgress(STR_LOADING, STR_LOADINGGEOCACHES, gcCount, index);
                                        nextUpdateTime = DateTime.Now.AddSeconds(1);
                                    }
                                }
                            }
                        }

                        using (Utils.ProgressBlock progress = new ProgressBlock(this, STR_LOADING, STR_LOADINGLOGS, logCount, 0))
                        {
                            int index = 0;
                            nextUpdateTime = DateTime.MinValue;

                            FileStream fs = fc._fsLogs;
                            fs.Position = 0;
                            long eof = fs.Length;
                            while (fs.Position < eof)
                            {
                                ri.Offset = fs.Position;
                                fs.Read(memBuffer, 0, lsize + 1);
                                ms.Position = 0;
                                ri.Length = br.ReadInt64();
                                if (memBuffer[lsize] == 0)
                                {
                                    //free
                                    fs.Position = ri.Offset + ri.Length;
                                }
                                else if (memBuffer[lsize] == 2)
                                {
                                    //read
                                    fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                    ms.Position = 0;
                                    ri.ID = br.ReadString().Substring(2);
                                    fs.Position = ri.Offset + ri.Length;

                                    bool newLog;
                                    bool logComplete = true;
                                    Framework.Data.Log log = Utils.DataAccess.GetLog(Core.Logs, ri.ID);
                                    if (log == null)
                                    {
                                        newLog = true;
                                        log = htInsertedLogs[ri.ID] as Framework.Data.Log;
                                        if (log == null)
                                        {
                                            log = new Framework.Data.Log();
                                            htInsertedLogs.Add(ri.ID, log);
                                            logComplete = false;
                                        }
                                        else
                                        {
                                            logComplete = true;
                                        }
                                    }
                                    else
                                    {
                                        if (overwrite)
                                        {
                                            newLog = false;
                                        }
                                        else
                                        {
                                            continue;
                                        }
                                    }
                                    log.BeginUpdate();
                                    try
                                    {
                                        log.ID = ri.ID;
                                        log.TBCode = br.ReadString();
                                        log.FinderId = br.ReadString();
                                        log.Text = br.ReadString();
                                        log.Encoded = br.ReadBoolean();
                                    }
                                    catch
                                    {
                                    }
                                    log.EndUpdate();
                                    if (newLog && logComplete && htInsertedLogs[ri.ID] != null)
                                    {
                                        if (htInsertedGeocaches[log.GeocacheCode ?? ""] != null || Utils.DataAccess.GetGeocache(Core.Geocaches, log.GeocacheCode ?? "") != null)
                                        {
                                            Core.Logs.Add(log);
                                        }
                                        htInsertedLogs.Remove(ri.ID);
                                    }
                                }
                                else
                                {
                                    //read
                                    fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                    ms.Position = 0;
                                    ri.ID = br.ReadString();

                                    bool newLog;
                                    bool logComplete = true;
                                    Framework.Data.Log log = Utils.DataAccess.GetLog(Core.Logs, ri.ID);
                                    if (log == null)
                                    {
                                        newLog = true;
                                        log = htInsertedLogs[ri.ID] as Framework.Data.Log;
                                        if (log == null)
                                        {
                                            log = new Framework.Data.Log();
                                            htInsertedLogs.Add(ri.ID, log);
                                            logComplete = false;
                                        }
                                        else
                                        {
                                            logComplete = true;
                                        }
                                    }
                                    else
                                    {
                                        if (overwrite)
                                        {
                                            newLog = false;
                                        }
                                        else
                                        {
                                            continue;
                                        }
                                    }
                                    log.BeginUpdate();
                                    try
                                    {

                                        log.ID = ri.ID;
                                        log.DataFromDate = DateTime.Parse(br.ReadString());
                                        log.Date = DateTime.Parse(br.ReadString());
                                        log.Finder = br.ReadString();
                                        log.GeocacheCode = br.ReadString();
                                        log.ID = br.ReadString();
                                        log.LogType = Utils.DataAccess.GetLogType(Core.LogTypes, br.ReadInt32());
                                    }
                                    catch
                                    {
                                    }
                                    log.EndUpdate();

                                    if (newLog && logComplete && htInsertedLogs[ri.ID] != null)
                                    {
                                        //check if geocache is present
                                        if (htInsertedGeocaches[log.GeocacheCode ?? ""] != null || Utils.DataAccess.GetGeocache(Core.Geocaches, log.GeocacheCode ?? "") != null)
                                        {
                                            Core.Logs.Add(log);
                                        }
                                        htInsertedLogs.Remove(ri.ID);
                                    }

                                    index++;
                                    if (DateTime.Now >= nextUpdateTime)
                                    {
                                        progress.UpdateProgress(STR_LOADING, STR_LOADINGLOGS, logCount, index);
                                        nextUpdateTime = DateTime.Now.AddSeconds(1);
                                    }
                                }
                            }
                        }


                        using (Utils.ProgressBlock progress = new ProgressBlock(this, STR_LOADING, STR_LOADINGWAYPOINTS, wptCount, 0))
                        {
                            int index = 0;
                            int procStep = 0;

                            using (FileStream fs = File.Open(fc.WaypointsFilename, FileMode.OpenOrCreate, FileAccess.Read))
                            {
                                fs.Position = 0;
                                long eof = fs.Length;
                                while (fs.Position < eof)
                                {
                                    ri.Offset = fs.Position;
                                    fs.Read(memBuffer, 0, lsize + 1);
                                    ms.Position = 0;
                                    ri.Length = br.ReadInt64();
                                    if (memBuffer[lsize] == 0)
                                    {
                                        //free
                                        fs.Position = ri.Offset + ri.Length;
                                    }
                                    else
                                    {
                                        //read
                                        fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                        ms.Position = 0;
                                        ri.ID = br.ReadString();

                                        bool newWp;
                                        Framework.Data.Waypoint wp = Utils.DataAccess.GetWaypoint(Core.Waypoints, ri.ID);
                                        if (wp == null)
                                        {
                                            newWp = true;
                                            wp = new Framework.Data.Waypoint();
                                        }
                                        else
                                        {
                                            if (overwrite)
                                            {
                                                newWp = false;
                                            }
                                            else
                                            {
                                                continue;
                                            }
                                        }

                                        wp.BeginUpdate();
                                        try
                                        {
                                            wp.Code = ri.ID;
                                            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(Core.WaypointTypes, br.ReadInt32());
                                        }
                                        catch
                                        {
                                        }
                                        wp.EndUpdate();
                                        if (newWp)
                                        {
                                            if (Utils.DataAccess.GetGeocache(Core.Geocaches, wp.GeocacheCode ?? "") != null)
                                            {
                                                Core.Waypoints.Add(wp);
                                            }
                                        }

                                        index++;
                                        procStep++;
                                        if (procStep >= 1000)
                                        {
                                            progress.UpdateProgress(STR_LOADING, STR_LOADINGWAYPOINTS, wptCount, index);
                                            procStep = 0;
                                        }
                                    }
                                }
                            }
                        }

                        using (Utils.ProgressBlock progress = new ProgressBlock(this, STR_LOADING, STR_LOADINGLOGIMAGES, logimgCount, 0))
                        {
                            int index = 0;
                            int procStep = 0;

                            using (FileStream fs = File.Open(fc.LogImagesFilename, FileMode.OpenOrCreate, FileAccess.Read))
                            {
                                fs.Position = 0;
                                long eof = fs.Length;
                                while (fs.Position < eof)
                                {
                                    ri.Offset = fs.Position;
                                    fs.Read(memBuffer, 0, lsize + 1);
                                    ms.Position = 0;
                                    ri.Length = br.ReadInt64();
                                    if (memBuffer[lsize] == 0)
                                    {
                                        //free
                                        fs.Position = ri.Offset + ri.Length;
                                    }
                                    else
                                    {
                                        //read
                                        fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                        ms.Position = 0;
                                        ri.ID = br.ReadString();

                                        bool newWp;
                                        Framework.Data.LogImage li = Utils.DataAccess.GetLogImage(Core.LogImages, ri.ID);
                                        if (li == null)
                                        {
                                            newWp = true;
                                            li = new Framework.Data.LogImage();
                                        }
                                        else
                                        {
                                            if (overwrite)
                                            {
                                                newWp = false;
                                            }
                                            else
                                            {
                                                continue;
                                            }
                                        }

                                        li.BeginUpdate();
                                        try
                                        {
                                            li.ID = ri.ID;
                                            li.DataFromDate = DateTime.Parse(br.ReadString());
                                            li.LogID = br.ReadString();
                                            li.Name = br.ReadString();
                                            li.Url = br.ReadString();
                                        }
                                        catch
                                        {
                                        }
                                        li.EndUpdate();

                                        if (newWp)
                                        {
                                            if (Utils.DataAccess.GetLog(Core.Logs, li.LogID ?? "") != null)
                                            {
                                                Core.LogImages.Add(li);
                                            }
                                        }

                                        index++;
                                        procStep++;
                                        if (procStep >= 1000)
                                        {
                                            progress.UpdateProgress(STR_LOADING, STR_LOADINGLOGIMAGES, logimgCount, index);
                                            procStep = 0;
                                        }
                                    }
                                }
                            }
                        }

                        using (Utils.ProgressBlock progress = new ProgressBlock(this, STR_LOADING, STR_LOADINGGEOCACHEIMAGES, geocacheimgCount, 0))
                        {
                            int index = 0;
                            int procStep = 0;

                            using (FileStream fs = File.Open(fc.GeocacheImagesFilename, FileMode.OpenOrCreate, FileAccess.Read))
                            {
                                fs.Position = 0;
                                long eof = fs.Length;
                                while (fs.Position < eof)
                                {
                                    ri.Offset = fs.Position;
                                    fs.Read(memBuffer, 0, lsize + 1);
                                    ms.Position = 0;
                                    ri.Length = br.ReadInt64();
                                    if (memBuffer[lsize] == 0)
                                    {
                                        //free
                                        fs.Position = ri.Offset + ri.Length;
                                    }
                                    else
                                    {
                                        //read
                                        fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                        ms.Position = 0;
                                        ri.ID = br.ReadString();

                                        bool newWp;
                                        Framework.Data.GeocacheImage wp = Utils.DataAccess.GetGeocacheImage(Core.GeocacheImages, ri.ID);
                                        if (wp == null)
                                        {
                                            newWp = true;
                                            wp = new Framework.Data.GeocacheImage();
                                        }
                                        else
                                        {
                                            if (overwrite)
                                            {
                                                newWp = false;
                                            }
                                            else
                                            {
                                                continue;
                                            }
                                        }

                                        wp.BeginUpdate();
                                        try
                                        {
                                            wp.ID = ri.ID;
                                            wp.DataFromDate = DateTime.Parse(br.ReadString());
                                            wp.GeocacheCode = br.ReadString();
                                            wp.Description = br.ReadString();
                                            wp.Name = br.ReadString();
                                            wp.Url = br.ReadString();
                                            wp.MobileUrl = br.ReadString();
                                            wp.ThumbUrl = br.ReadString();
                                        }
                                        catch
                                        {
                                        }
                                        wp.EndUpdate();
                                        if (newWp)
                                        {
                                            if (Utils.DataAccess.GetGeocache(Core.Geocaches, wp.GeocacheCode ?? "") != null)
                                            {
                                                Core.GeocacheImages.Add(wp);
                                            }
                                        }

                                        index++;
                                        procStep++;
                                        if (procStep >= 1000)
                                        {
                                            progress.UpdateProgress(STR_LOADING, STR_LOADINGGEOCACHEIMAGES, geocacheimgCount, index);
                                            procStep = 0;
                                        }
                                    }
                                }
                            }
                        }

                        {
                            using (FileStream fs = File.Open(fc.UserWaypointsFilename, FileMode.OpenOrCreate, FileAccess.Read))
                            {
                                fs.Position = 0;
                                long eof = fs.Length;
                                while (fs.Position < eof)
                                {
                                    ri.Offset = fs.Position;
                                    fs.Read(memBuffer, 0, lsize + 1);
                                    ms.Position = 0;
                                    ri.Length = br.ReadInt64();
                                    if (memBuffer[lsize] == 0)
                                    {
                                        //free
                                        fs.Position = ri.Offset + ri.Length;
                                    }
                                    else
                                    {
                                        //read
                                        fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                        ms.Position = 0;
                                        ri.ID = br.ReadString();

                                        bool newWp;
                                        Framework.Data.UserWaypoint wp = Utils.DataAccess.GetUserWaypoint(Core.UserWaypoints, int.Parse(ri.ID));
                                        if (wp == null)
                                        {
                                            newWp = true;
                                            wp = new Framework.Data.UserWaypoint();
                                        }
                                        else
                                        {
                                            if (overwrite)
                                            {
                                                newWp = false;
                                            }
                                            else
                                            {
                                                continue;
                                            }
                                        }

                                        wp.BeginUpdate();
                                        try
                                        {
                                            wp.ID = int.Parse(ri.ID);
                                            wp.Description = br.ReadString();
                                            wp.GeocacheCode = br.ReadString();
                                            wp.Lat = br.ReadDouble();
                                            wp.Lon = br.ReadDouble();
                                            wp.Date = DateTime.Parse(br.ReadString());
                                        }
                                        catch
                                        {
                                        }
                                        if (newWp)
                                        {
                                            if (Utils.DataAccess.GetGeocache(Core.Geocaches, wp.GeocacheCode ?? "") != null)
                                            {
                                                Core.UserWaypoints.Add(wp);
                                            }
                                        }

                                        wp.EndUpdate();
                                    }
                                }
                            }
                        }
                    }
                }
                result = true;
            }
            catch
            {
            }
            return result;
        }
Ejemplo n.º 27
0
        public bool RepairV1()
        {
            bool result = true;

            if (File.Exists(_fileCollection.DatabaseInfoFilename))
            {
                int lsize = sizeof(long);
                byte[] memBuffer = new byte[10 * 1024 * 1024];
                using (MemoryStream ms = new MemoryStream(memBuffer))
                using (BinaryReader br = new BinaryReader(ms))
                {

                    int gcCount = 0;
                    int logCount = 0;
                    int logimgCount = 0;
                    int wptCount = 0;
                    int usrwptCount = 0;

                    XmlDocument doc = new XmlDocument();
                    doc.Load(_fileCollection.DatabaseInfoFilename);
                    XmlElement root = doc.DocumentElement;
                    gcCount = int.Parse(root.SelectSingleNode("GeocacheCount").InnerText);
                    logCount = int.Parse(root.SelectSingleNode("LogCount").InnerText);
                    logimgCount = int.Parse(root.SelectSingleNode("LogImagesCount").InnerText);
                    wptCount = int.Parse(root.SelectSingleNode("WaypointCount").InnerText);
                    usrwptCount = int.Parse(root.SelectSingleNode("UserWaypointCount").InnerText);

                    DateTime nextUpdateTime = DateTime.MinValue;
                    using (Utils.ProgressBlock fixscr = new Utils.ProgressBlock(this, STR_LOADING, STR_LOADINGDATA, 1, 0))
                    {
                        using (Utils.ProgressBlock progress = new Utils.ProgressBlock(this, STR_LOADING, STR_LOADINGGEOCACHES, gcCount, 0))
                        {
                            int index = 0;

                            FileStream fs = _fileCollection._fsGeocaches;
                            fs.Position = 0;
                            long eof = fs.Length;
                            long lastOKPosition = 0;
                            RecordInfo ri = new RecordInfo();
                            try
                            {
                                while (fs.Position < eof)
                                {
                                    lastOKPosition = fs.Position;
                                    ri.Offset = fs.Position;
                                    fs.Read(memBuffer, 0, lsize + 1);
                                    ms.Position = 0;
                                    ri.Length = br.ReadInt64();
                                    if (memBuffer[lsize] == 0)
                                    {
                                        //free
                                        ri.FreeSlot = true;
                                        ri.ID = string.Concat("_", ri.Offset.ToString());
                                        fs.Position = ri.Offset + ri.Length;
                                    }
                                    else if (memBuffer[lsize] == 2)
                                    {
                                        ri.FreeSlot = false;
                                        fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                        ms.Position = 0;
                                        ri.ID = br.ReadString();
                                        br.ReadString();
                                        br.ReadBoolean();
                                        br.ReadString();
                                        br.ReadBoolean();
                                    }
                                    else
                                    {
                                        //read
                                        ri.FreeSlot = false;
                                        Framework.Data.Geocache gc = new Framework.Data.Geocache();

                                        fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                        ms.Position = 0;
                                        gc.Code = br.ReadString();
                                        ri.ID = gc.Code;

                                        gc.Archived = br.ReadBoolean();
                                        gc.AttributeIds = ReadIntegerArray(br);
                                        gc.Available = br.ReadBoolean();
                                        gc.City = br.ReadString();
                                        gc.Container = Utils.DataAccess.GetGeocacheContainer(Core.GeocacheContainers, br.ReadInt32());
                                        gc.CustomCoords = 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();
                                        gc.GeocacheType = Utils.DataAccess.GetGeocacheType(Core.GeocacheTypes, br.ReadInt32());
                                        gc.ID = br.ReadString();
                                        gc.Lat = br.ReadDouble();
                                        gc.Lon = br.ReadDouble();
                                        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.PersonaleNote = br.ReadString();
                                        gc.PlacedBy = br.ReadString();
                                        gc.PublishedTime = DateTime.Parse(br.ReadString());
                                        gc.State = br.ReadString();
                                        gc.Terrain = br.ReadDouble();
                                        gc.Title = br.ReadString();
                                        gc.Url = br.ReadString();
                                        gc.DataFromDate = DateTime.Parse(br.ReadString());
                                        gc.Locked = br.ReadBoolean();

                                        gc.Saved = true;
                                        gc.IsDataChanged = false;

                                        index++;
                                        if (DateTime.Now >= nextUpdateTime)
                                        {
                                            progress.UpdateProgress(STR_LOADING, STR_LOADINGGEOCACHES, gcCount, index);
                                            nextUpdateTime = DateTime.Now.AddSeconds(1);
                                        }
                                    }

                                    checkUniqueID(ri, fs);
                                }
                            }
                            catch
                            {
                                //error in file after lastOKPosition
                                fs.SetLength(lastOKPosition);
                            }
                        }

                        _uniqueCheckList.Clear();
                        using (Utils.ProgressBlock progress = new ProgressBlock(this, STR_LOADING, STR_LOADINGLOGS, logCount, 0))
                        {
                            int index = 0;
                            nextUpdateTime = DateTime.MinValue;

                            FileStream fs = _fileCollection._fsLogs;
                            fs.Position = 0;
                            long lastOKPosition = 0;
                            long eof = fs.Length;
                            RecordInfo ri = new RecordInfo();
                            try
                            {
                                while (fs.Position < eof)
                                {
                                    lastOKPosition = fs.Position;
                                    ri.Offset = fs.Position;
                                    fs.Read(memBuffer, 0, lsize + 1);
                                    ms.Position = 0;
                                    ri.Length = br.ReadInt64();
                                    if (memBuffer[lsize] == 0)
                                    {
                                        //free
                                        ri.FreeSlot = true;
                                        ri.ID = string.Concat("_", ri.Offset.ToString());
                                        fs.Position = ri.Offset + ri.Length;
                                    }
                                    else if (memBuffer[lsize] == 2)
                                    {
                                        ri.FreeSlot = false;
                                        fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                        ms.Position = 0;
                                        ri.ID = br.ReadString();
                                        br.ReadString();
                                        br.ReadString();
                                        br.ReadString();
                                        br.ReadBoolean();
                                    }
                                    else
                                    {
                                        //read
                                        ri.FreeSlot = false;
                                        Framework.Data.Log log = new Framework.Data.Log();

                                        fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                        ms.Position = 0;
                                        log.ID = br.ReadString();
                                        ri.ID = log.ID;

                                        log.DataFromDate = DateTime.Parse(br.ReadString());
                                        log.Date = DateTime.Parse(br.ReadString());
                                        log.Finder = br.ReadString();
                                        log.GeocacheCode = br.ReadString();
                                        log.ID = br.ReadString();
                                        log.LogType = Utils.DataAccess.GetLogType(Core.LogTypes, br.ReadInt32());

                                        log.Saved = true;
                                        log.IsDataChanged = false;

                                        index++;
                                        if (DateTime.Now >= nextUpdateTime)
                                        {
                                            progress.UpdateProgress(STR_LOADING, STR_LOADINGLOGS, logCount, index);
                                            nextUpdateTime = DateTime.Now.AddSeconds(1);
                                        }
                                    }
                                    checkUniqueID(ri, fs);
                                }
                            }
                            catch
                            {
                                //error in file after lastOKPosition
                                fs.SetLength(lastOKPosition);
                            }
                        }

                        _uniqueCheckList.Clear();
                        using (Utils.ProgressBlock progress = new ProgressBlock(this, STR_LOADING, STR_LOADINGWAYPOINTS, wptCount, 0))
                        {
                            int index = 0;
                            int procStep = 0;

                            using (FileStream fs = File.Open(_fileCollection.WaypointsFilename, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                            {
                                fs.Position = 0;
                                long eof = fs.Length;
                                long lastOKPosition = 0;
                                RecordInfo ri = new RecordInfo();
                                try
                                {
                                    while (fs.Position < eof)
                                    {
                                        lastOKPosition = fs.Position;
                                        ri.Offset = fs.Position;
                                        fs.Read(memBuffer, 0, lsize + 1);
                                        ms.Position = 0;
                                        ri.Length = br.ReadInt64();
                                        if (memBuffer[lsize] == 0)
                                        {
                                            //free
                                            ri.FreeSlot = true;
                                            ri.ID = string.Concat("_", ri.Offset.ToString());
                                            fs.Position = ri.Offset + ri.Length;
                                        }
                                        else
                                        {
                                            //read
                                            ri.FreeSlot = false;
                                            Framework.Data.Waypoint wp = new Framework.Data.Waypoint();

                                            fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                            ms.Position = 0;
                                            wp.Code = br.ReadString();
                                            ri.ID = wp.Code;

                                            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(Core.WaypointTypes, br.ReadInt32());

                                            wp.Saved = true;
                                            wp.IsDataChanged = false;

                                            index++;
                                            procStep++;
                                            if (procStep >= 1000)
                                            {
                                                progress.UpdateProgress(STR_LOADING, STR_LOADINGWAYPOINTS, wptCount, index);
                                                procStep = 0;
                                            }
                                        }
                                        checkUniqueID(ri, fs);
                                    }
                                }
                                catch
                                {
                                    //error in file after lastOKPosition
                                    fs.SetLength(lastOKPosition);
                                }
                            }
                        }

                        _uniqueCheckList.Clear();
                        using (Utils.ProgressBlock progress = new ProgressBlock(this, STR_LOADING, STR_LOADINGLOGIMAGES, logimgCount, 0))
                        {
                            int index = 0;
                            int procStep = 0;

                            using (FileStream fs = File.Open(_fileCollection.LogImagesFilename, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                            {
                                fs.Position = 0;
                                long eof = fs.Length;
                                long lastOKPosition = 0;
                                RecordInfo ri = new RecordInfo();
                                try
                                {
                                    while (fs.Position < eof)
                                    {
                                        lastOKPosition = fs.Position;
                                        ri.Offset = fs.Position;
                                        fs.Read(memBuffer, 0, lsize + 1);
                                        ms.Position = 0;
                                        ri.Length = br.ReadInt64();
                                        if (memBuffer[lsize] == 0)
                                        {
                                            //free
                                            ri.FreeSlot = true;
                                            ri.ID = string.Concat("_", ri.Offset.ToString());
                                            fs.Position = ri.Offset + ri.Length;
                                        }
                                        else
                                        {
                                            //read
                                            ri.FreeSlot = false;
                                            Framework.Data.LogImage li = new Framework.Data.LogImage();

                                            fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                            ms.Position = 0;
                                            li.ID = br.ReadString();
                                            ri.ID = li.ID;

                                            li.DataFromDate = DateTime.Parse(br.ReadString());
                                            li.LogID = br.ReadString();
                                            li.Name = br.ReadString();
                                            li.Url = br.ReadString();

                                            li.Saved = true;
                                            li.IsDataChanged = false;

                                            index++;
                                            procStep++;
                                            if (procStep >= 1000)
                                            {
                                                progress.UpdateProgress(STR_LOADING, STR_LOADINGLOGIMAGES, logimgCount, index);
                                                procStep = 0;
                                            }
                                        }
                                        checkUniqueID(ri, fs);
                                    }
                                }
                                catch
                                {
                                    //error in file after lastOKPosition
                                    fs.SetLength(lastOKPosition);
                                }
                            }
                        }

                        {
                            int index = 0;
                            _uniqueCheckList.Clear();
                            using (FileStream fs = File.Open(_fileCollection.UserWaypointsFilename, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                            {
                                fs.Position = 0;
                                long eof = fs.Length;
                                long lastOKPosition = 0;
                                RecordInfo ri = new RecordInfo();
                                try
                                {
                                    while (fs.Position < eof)
                                    {
                                        lastOKPosition = fs.Position;
                                        ri.Offset = fs.Position;
                                        fs.Read(memBuffer, 0, lsize + 1);
                                        ms.Position = 0;
                                        ri.Length = br.ReadInt64();
                                        if (memBuffer[lsize] == 0)
                                        {
                                            //free
                                            ri.FreeSlot = true;
                                            ri.ID = string.Concat("_", ri.Offset.ToString());
                                            fs.Position = ri.Offset + ri.Length;
                                        }
                                        else
                                        {
                                            //read
                                            ri.FreeSlot = false;
                                            Framework.Data.UserWaypoint wp = new Framework.Data.UserWaypoint();

                                            fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                            ms.Position = 0;
                                            ri.ID = br.ReadString();
                                            wp.ID = int.Parse(ri.ID);

                                            wp.Description = br.ReadString();
                                            wp.GeocacheCode = br.ReadString();
                                            wp.Lat = br.ReadDouble();
                                            wp.Lon = br.ReadDouble();
                                            wp.Date = DateTime.Parse(br.ReadString());

                                            wp.Saved = true;
                                            wp.IsDataChanged = false;

                                            index++;
                                        }
                                        checkUniqueID(ri, fs);
                                    }
                                }
                                catch
                                {
                                    //error in file after lastOKPosition
                                    fs.SetLength(lastOKPosition);
                                }
                            }
                        }
                    }
                }
            }

            return result;
        }
Ejemplo n.º 28
0
        public void ProcessGeocachingComGPXCaches(ResultData data, 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
                            //check if it is a child waypoint by checking the name
                            Framework.Data.Geocache parentGeocache = null;
                            string wpname = wp.SelectSingleNode("x:name", nsmgr).InnerText.Substring(2);
                            parentGeocache = (from g in data.Geocaches where g.Code.Substring(2) == wpname select g).FirstOrDefault();
                            if (parentGeocache == null)
                            {
                                parentGeocache = (from Framework.Data.Geocache g in _core.Geocaches where g.Code.Substring(2) == wpname select g).FirstOrDefault();
                                if (parentGeocache == null)
                                {
                                    //parent is not available
                                    //now what?
                                    continue;
                                }
                            }

                            Framework.Data.Waypoint cwp = new Framework.Data.Waypoint();

                            cwp.Code         = wp.SelectSingleNode("x:name", nsmgr).InnerText;
                            cwp.GeocacheCode = string.Concat(parentGeocache.Code.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      = DataAccess.GetWaypointType(_core.WaypointTypes, wp.SelectSingleNode("x:sym", nsmgr).InnerText);

                            data.Waypoints.Add(cwp);

                            continue;
                        }

                        Framework.Data.Geocache gc = new Framework.Data.Geocache();

                        gc.Lat           = Utils.Conversion.StringToDouble(SafeAttributeInnerText(wp, "lat", "0.0"));
                        gc.Lon           = Utils.Conversion.StringToDouble(SafeAttributeInnerText(wp, "lon", "0.0"));
                        gc.Code          = wp.SelectSingleNode("x:name", nsmgr).InnerText;
                        gc.Title         = n.SelectSingleNode("y:name", nsmgr).InnerText;
                        gc.DataFromDate  = _gpxDataTime;
                        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.Available = bool.Parse(n.Attributes["available"].InnerText);
                        gc.Archived  = bool.Parse(SafeInnerText(n.Attributes["archived"], "False"));
                        gc.Country   = SafeInnerText(n.SelectSingleNode("y:country", nsmgr), "");
                        gc.State     = SafeInnerText(n.SelectSingleNode("y:state", nsmgr), "");
                        gc.ID        = SafeInnerText(n.Attributes["id"], "");
                        gc.OwnerId   = SafeAttributeInnerText(n.SelectSingleNode("y:owner", nsmgr), "id", "");
                        if (_cachesGpxVersion >= V102)
                        {
                            gc.GeocacheType  = DataAccess.GetGeocacheType(_core.GeocacheTypes, int.Parse(SafeAttributeInnerText(n.SelectSingleNode("y:type", nsmgr), "id", "-1")));
                            gc.Container     = DataAccess.GetGeocacheContainer(_core.GeocacheContainers, 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.PersonaleNote = 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 = DataAccess.GetGeocacheType(_core.GeocacheTypes, srchTxt);
                            if (isGeoSpy)
                            {
                                gc.Container = DataAccess.GetGeocacheContainer(_core.GeocacheContainers, "Virtual");
                            }
                            else
                            {
                                gc.Container = DataAccess.GetGeocacheContainer(_core.GeocacheContainers, 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                = Conversion.StringToDouble(SafeInnerText(n.SelectSingleNode("y:terrain", nsmgr), "1"));
                        gc.Difficulty             = 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), "");

                        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);
                                }
                            }
                        }

                        Calculus.SetDistanceAndAngleGeocacheFromLocation(gc, _core.CenterLocation);

                        data.Geocaches.Add(gc);

                        //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)
                                {
                                    Framework.Data.Log lg = new Framework.Data.Log();

                                    lg.GeocacheCode = gc.Code;
                                    string lid = SafeAttributeInnerText(l, "id", "");

                                    if (lid.StartsWith("GL"))
                                    {
                                        lg.ID = lid;
                                    }
                                    else
                                    {
                                        if (string.IsNullOrEmpty(lid) || lid.StartsWith("-"))
                                        {
                                            continue;
                                        }
                                        try
                                        {
                                            lg.ID = string.Concat("GL", Utils.Conversion.GetCacheCodeFromCacheID(int.Parse(lid)).Substring(2));
                                        }
                                        catch
                                        {
                                            continue;
                                        }
                                    }

                                    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 = 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 = DataAccess.GetLogType(_core.LogTypes, int.Parse(l.SelectSingleNode("y:type", nsmgr).Attributes["id"].InnerText));
                                    }
                                    else
                                    {
                                        lg.LogType = DataAccess.GetLogType(_core.LogTypes, l.SelectSingleNode("y:type", nsmgr).InnerText);
                                    }

                                    data.Logs.Add(lg);

                                    //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)
                                            {
                                                Framework.Data.LogImage lgi = new Framework.Data.LogImage();

                                                lgi.Url          = li.SelectSingleNode("y:url", nsmgr).InnerText;
                                                lgi.ID           = lgi.Url;
                                                lgi.LogID        = lg.ID;
                                                lgi.Name         = li.SelectSingleNode("y:name", nsmgr).InnerText;
                                                lgi.DataFromDate = _gpxDataTime;

                                                data.LogImages.Add(lgi);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch
            {
            }
        }
Ejemplo n.º 29
0
        public override bool LoadWaypoints(List<Framework.Data.Waypoint> wps, List<Framework.Data.UserWaypoint> usrwps)
        {
            bool result = false;
            MySqlConnection dbcon = GetMySqlConnection(Properties.Settings.Default.SqlServer, Properties.Settings.Default.Database, Properties.Settings.Default.SqlServerPwd, Properties.Settings.Default.SqlServerUsername, Properties.Settings.Default.SqlServerUseIS, Properties.Settings.Default.SqlServerPort);
            if (dbcon != null && InitDatabase(dbcon, Properties.Settings.Default.Database))
            {
                int wptCount = 0;
                using (MySqlCommand cmd = new MySqlCommand("select waypoint from counter", dbcon))
                using (MySqlDataReader dr = cmd.ExecuteReader())
                    if (dr.Read())
                    {
                        wptCount = (int)dr["waypoint"];
                    }

                int index = 0;
                int procStep = 0;
                using (MySqlCommand cmd = new MySqlCommand("select * from waypoint", dbcon))
                {
                    cmd.CommandTimeout = 0;
                    using (MySqlDataReader dr = cmd.ExecuteReader())
                        while (dr.Read())
                        {
                            Framework.Data.Waypoint wp = new Framework.Data.Waypoint();

                            wp.ID = (string)dr["id"];
                            wp.Code = (string)dr["code"];
                            wp.Url = (string)dr["url"];
                            wp.UrlName = (string)dr["urlname"];
                            wp.Name = (string)dr["name"];
                            wp.DataFromDate = DateTime.Parse((string)dr["datafromdate"]).ToLocalTime();
                            wp.Comment = (string)dr["comment"];
                            wp.GeocacheCode = (string)dr["geocachecode"];
                            wp.Description = (string)dr["description"];
                            wp.WPType = Utils.DataAccess.GetWaypointType(Core.WaypointTypes, (int)dr["wptype"]);
                            object o = dr["lat"];
                            if (o == null || o.GetType() == typeof(DBNull))
                            {
                                wp.Lat = null;
                            }
                            else
                            {
                                wp.Lat = (double?)(float)o;
                            }
                            o = dr["lon"];
                            if (o == null || o.GetType() == typeof(DBNull))
                            {
                                wp.Lon = null;
                            }
                            else
                            {
                                wp.Lon = (double?)(float)o;
                            }
                            wp.Time = DateTime.Parse((string)dr["time"]).ToLocalTime();

                            wp.Saved = true;
                            wp.IsDataChanged = false;

                            _wptsInDB[wp.Code] = wp.Code;
                            wps.Add(wp);

                            index++;
                            procStep++;
                            if (procStep >= 20000)
                            {
                                UpdateLoadingInBackgroundProgress(STR_LOADING_WAYPOINTS_BG, wptCount, index);
                                procStep = 0;
                            }
                        }
                }

                using (MySqlCommand cmd = new MySqlCommand("select * from userwaypoint", dbcon))
                {
                    cmd.CommandTimeout = 0;
                    using (MySqlDataReader dr = cmd.ExecuteReader())
                        while (dr.Read())
                        {
                            Framework.Data.UserWaypoint wp = new Framework.Data.UserWaypoint();

                            wp.ID = (int)dr["id"];
                            wp.GeocacheCode = (string)dr["geocachecode"];
                            wp.Description = (string)dr["description"];
                            wp.Lat = (double)(float)dr["lat"];
                            wp.Lon = (double)(float)dr["lon"];
                            wp.Date = DateTime.Parse((string)dr["time"]).ToLocalTime();

                            wp.Saved = true;
                            wp.IsDataChanged = false;

                            _usrwptsInDB[wp.ID] = wp.ID;
                            usrwps.Add(wp);
                        }
                }

                dbcon.Dispose();
                dbcon = null;
            }
            return result;
        }
Ejemplo n.º 30
0
        private void readWaypointData(RecordInfo ri, MemoryStream ms, BinaryReader br)
        {
            _wptsInDB.Add(ri.ID, ri);
            Framework.Data.Waypoint wp = new Framework.Data.Waypoint();
            wp.Code = ri.ID;
            //wp.GeocacheCode = ri.SubID;

            wp.DataFromDate = DateTimeFromLong(br.ReadInt64());
            if (br.ReadBoolean())
            {
                wp.Lat = br.ReadDouble();
            }
            else
            {
                wp.Lat = null;
                br.ReadDouble();
            }
            if (br.ReadBoolean())
            {
                wp.Lon = br.ReadDouble();
            }
            else
            {
                wp.Lon = null;
                br.ReadDouble();
            }
            wp.Time = DateTimeFromLong(br.ReadInt64());
            wp.WPType = Utils.DataAccess.GetWaypointType(Core.WaypointTypes, br.ReadInt32());

            ms.Position = 200;
            wp.GeocacheCode = br.ReadString();
            ms.Position = 240;
            wp.Code = br.ReadString();
            ms.Position = 280;
            wp.Description = br.ReadString();
            ms.Position = 500;
            wp.Name = br.ReadString();
            ms.Position = 600;
            wp.Url = br.ReadString();
            ms.Position = 700;
            wp.UrlName = br.ReadString();
            ms.Position = 800;
            wp.Comment = br.ReadString();

            wp.Saved = true;
            wp.IsDataChanged = false;
            Core.Waypoints.Add(wp);
        }
Ejemplo n.º 31
0
        public override bool Load(bool geocachesOnly)
        {
            bool result = true;

            if (File.Exists(_fileCollection.DatabaseInfoFilename))
            {
                int lsize = sizeof(long);
                byte[] memBuffer = new byte[10 * 1024 * 1024];
                using (MemoryStream ms = new MemoryStream(memBuffer))
                using (BinaryReader br = new BinaryReader(ms))
                {

                    int gcCount = 0;
                    int logCount = 0;
                    int logimgCount = 0;
                    int geocacheimgCount = 0;
                    int wptCount = 0;
                    int usrwptCount = 0;

                    XmlDocument doc = new XmlDocument();
                    doc.Load(_fileCollection.DatabaseInfoFilename);
                    XmlElement root = doc.DocumentElement;
                    gcCount = int.Parse(root.SelectSingleNode("GeocacheCount").InnerText);
                    logCount = int.Parse(root.SelectSingleNode("LogCount").InnerText);
                    logimgCount = int.Parse(root.SelectSingleNode("LogImagesCount").InnerText);
                    wptCount = int.Parse(root.SelectSingleNode("WaypointCount").InnerText);
                    usrwptCount = int.Parse(root.SelectSingleNode("UserWaypointCount").InnerText);
                    if (root.SelectSingleNode("GeocacheImagesCount") != null)
                    {
                        geocacheimgCount = int.Parse(root.SelectSingleNode("GeocacheImagesCount").InnerText);
                    }

                    DateTime nextUpdateTime = DateTime.MinValue;
                    using (Utils.ProgressBlock fixscr = new Utils.ProgressBlock(this, STR_LOADING, STR_LOADINGDATA, 1, 0))
                    {
                        using (Utils.ProgressBlock progress = new Utils.ProgressBlock(this, STR_LOADING, STR_LOADINGGEOCACHES, gcCount, 0))
                        {
                            int index = 0;
                            //int procStep = 0;
                            List<Framework.Data.Geocache> gcList = new List<Framework.Data.Geocache>();

                            FileStream fs = _fileCollection._fsGeocaches;
                            fs.Position = 0;
                            long eof = fs.Length;
                            while (fs.Position < eof)
                            {
                                RecordInfo ri = new RecordInfo();
                                ri.Offset = fs.Position;
                                fs.Read(memBuffer, 0, lsize + 1);
                                ms.Position = 0;
                                ri.Length = br.ReadInt64();
                                if (memBuffer[lsize] == 0)
                                {
                                    //free
                                    ri.FreeSlot = true;
                                    ri.ID = string.Concat("_", ri.Offset.ToString());
                                    fs.Position = ri.Offset + ri.Length;
                                }
                                else if (memBuffer[lsize] == 2)
                                {
                                    //lazy loading
                                    ri.FreeSlot = false;
                                    int readCount = Math.Min(32, (int)(ri.Length - lsize - 1));
                                    fs.Read(memBuffer, 0, readCount);
                                    ms.Position = 0;
                                    ri.ID = br.ReadString();
                                    fs.Position = ri.Offset + ri.Length;
                                }
                                else
                                {
                                    //read
                                    ri.FreeSlot = false;
                                    Framework.Data.Geocache gc = new Framework.Data.Geocache();

                                    fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                    ms.Position = 0;
                                    gc.Code = br.ReadString();
                                    ri.ID = gc.Code;

                                    gc.Archived = br.ReadBoolean();
                                    gc.AttributeIds = ReadIntegerArray(br);
                                    gc.Available = br.ReadBoolean();
                                    gc.City = br.ReadString();
                                    gc.Container = Utils.DataAccess.GetGeocacheContainer(Core.GeocacheContainers, br.ReadInt32());
                                    gc.CustomCoords = 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();
                                    gc.GeocacheType = Utils.DataAccess.GetGeocacheType(Core.GeocacheTypes, br.ReadInt32());
                                    gc.ID = br.ReadString();
                                    gc.Lat = br.ReadDouble();
                                    gc.Lon = br.ReadDouble();
                                    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.PersonaleNote = br.ReadString();
                                    gc.PlacedBy = br.ReadString();
                                    gc.PublishedTime = DateTime.Parse(br.ReadString());
                                    gc.State = br.ReadString();
                                    gc.Terrain = br.ReadDouble();
                                    gc.Title = br.ReadString();
                                    gc.Url = br.ReadString();
                                    gc.DataFromDate = DateTime.Parse(br.ReadString());
                                    gc.Locked = br.ReadBoolean();

                                    Calculus.SetDistanceAndAngleGeocacheFromLocation(gc, Core.CenterLocation);
                                    gc.Saved = true;
                                    gc.IsDataChanged = false;
                                    //gcList.Add(gc);
                                    Core.Geocaches.Add(gc);

                                    index++;
                                    //procStep++;
                                    //if (procStep >= 1000)
                                    if (DateTime.Now>=nextUpdateTime)
                                    {
                                        progress.UpdateProgress(STR_LOADING, STR_LOADINGGEOCACHES, gcCount, index);
                                        nextUpdateTime = DateTime.Now.AddSeconds(1);
                                        //procStep = 0;
                                    }
                                }
                                _fileCollection._geocachesInDB.Add(ri.ID, ri);
                            }
                            //Core.Geocaches.Add(gcList);
                        }

                        if (!geocachesOnly)
                        {
                            using (Utils.ProgressBlock progress = new ProgressBlock(this, STR_LOADING, STR_LOADINGLOGS, logCount, 0))
                            {
                                int index = 0;
                                nextUpdateTime = DateTime.MinValue;

                                List<Framework.Data.Log> lgList = new List<Framework.Data.Log>();

                                FileStream fs = _fileCollection._fsLogs;
                                fs.Position = 0;
                                long eof = fs.Length;
                                while (fs.Position < eof)
                                {
                                    RecordInfo ri = new RecordInfo();
                                    ri.Offset = fs.Position;
                                    fs.Read(memBuffer, 0, lsize + 1);
                                    ms.Position = 0;
                                    ri.Length = br.ReadInt64();
                                    if (memBuffer[lsize] == 0)
                                    {
                                        //free
                                        ri.FreeSlot = true;
                                        ri.ID = string.Concat("_", ri.Offset.ToString());
                                        fs.Position = ri.Offset + ri.Length;
                                    }
                                    else if (memBuffer[lsize] == 2)
                                    {
                                        //lazy loading
                                        ri.FreeSlot = false;
                                        int readCount = Math.Min(32, (int)(ri.Length - lsize - 1));
                                        fs.Read(memBuffer, 0, readCount);
                                        ms.Position = 0;
                                        ri.ID = br.ReadString();
                                        fs.Position = ri.Offset + ri.Length;
                                    }
                                    else
                                    {
                                        //read
                                        ri.FreeSlot = false;
                                        Framework.Data.Log log = new Framework.Data.Log();

                                        fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                        ms.Position = 0;
                                        log.ID = br.ReadString();
                                        ri.ID = log.ID;

                                        log.DataFromDate = DateTime.Parse(br.ReadString());
                                        log.Date = DateTime.Parse(br.ReadString());
                                        log.Finder = br.ReadString();
                                        log.GeocacheCode = br.ReadString();
                                        log.ID = br.ReadString();
                                        log.LogType = Utils.DataAccess.GetLogType(Core.LogTypes, br.ReadInt32());

                                        log.Saved = true;
                                        log.IsDataChanged = false;
                                        lgList.Add(log);

                                        index++;
                                        //procStep++;
                                        //if (procStep >= 1000)
                                        if (DateTime.Now >= nextUpdateTime)
                                        {
                                            progress.UpdateProgress(STR_LOADING, STR_LOADINGLOGS, logCount, index);
                                            nextUpdateTime = DateTime.Now.AddSeconds(1);
                                            //procStep = 0;
                                        }
                                    }
                                    _fileCollection._logsInDB.Add(ri.ID, ri);
                                }
                                Core.Logs.Add(lgList);
                            }


                            using (Utils.ProgressBlock progress = new ProgressBlock(this, STR_LOADING, STR_LOADINGWAYPOINTS, wptCount, 0))
                            {
                                int index = 0;
                                int procStep = 0;

                                List<Framework.Data.Waypoint> wptList = new List<Framework.Data.Waypoint>();

                                using (FileStream fs = File.Open(_fileCollection.WaypointsFilename, FileMode.OpenOrCreate, FileAccess.Read))
                                {
                                    fs.Position = 0;
                                    long eof = fs.Length;
                                    while (fs.Position < eof)
                                    {
                                        RecordInfo ri = new RecordInfo();
                                        ri.Offset = fs.Position;
                                        fs.Read(memBuffer, 0, lsize + 1);
                                        ms.Position = 0;
                                        ri.Length = br.ReadInt64();
                                        if (memBuffer[lsize] == 0)
                                        {
                                            //free
                                            ri.FreeSlot = true;
                                            ri.ID = string.Concat("_", ri.Offset.ToString());
                                            fs.Position = ri.Offset + ri.Length;
                                        }
                                        else
                                        {
                                            //read
                                            ri.FreeSlot = false;
                                            Framework.Data.Waypoint wp = new Framework.Data.Waypoint();

                                            fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                            ms.Position = 0;
                                            wp.Code = br.ReadString();
                                            ri.ID = wp.Code;

                                            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(Core.WaypointTypes, br.ReadInt32());

                                            wp.Saved = true;
                                            wp.IsDataChanged = false;
                                            wptList.Add(wp);

                                            index++;
                                            procStep++;
                                            if (procStep >= 1000)
                                            {
                                                progress.UpdateProgress(STR_LOADING, STR_LOADINGWAYPOINTS, wptCount, index);
                                                procStep = 0;
                                            }
                                        }
                                        _fileCollection._wptsInDB.Add(ri.ID, ri);
                                    }
                                }
                                Core.Waypoints.Add(wptList);
                            }

                            using (Utils.ProgressBlock progress = new ProgressBlock(this, STR_LOADING, STR_LOADINGLOGIMAGES, logimgCount, 0))
                            {
                                int index = 0;
                                int procStep = 0;

                                List<Framework.Data.LogImage> lgiList = new List<Framework.Data.LogImage>();

                                using (FileStream fs = File.Open(_fileCollection.LogImagesFilename, FileMode.OpenOrCreate, FileAccess.Read))
                                {
                                    fs.Position = 0;
                                    long eof = fs.Length;
                                    while (fs.Position < eof)
                                    {
                                        RecordInfo ri = new RecordInfo();
                                        ri.Offset = fs.Position;
                                        fs.Read(memBuffer, 0, lsize + 1);
                                        ms.Position = 0;
                                        ri.Length = br.ReadInt64();
                                        if (memBuffer[lsize] == 0)
                                        {
                                            //free
                                            ri.FreeSlot = true;
                                            ri.ID = string.Concat("_", ri.Offset.ToString());
                                            fs.Position = ri.Offset + ri.Length;
                                        }
                                        else
                                        {
                                            //read
                                            ri.FreeSlot = false;
                                            Framework.Data.LogImage li = new Framework.Data.LogImage();

                                            fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                            ms.Position = 0;
                                            li.ID = br.ReadString();
                                            ri.ID = li.ID;

                                            li.DataFromDate = DateTime.Parse(br.ReadString());
                                            li.LogID = br.ReadString();
                                            li.Name = br.ReadString();
                                            li.Url = br.ReadString();

                                            li.Saved = true;
                                            li.IsDataChanged = false;
                                            lgiList.Add(li);

                                            index++;
                                            procStep++;
                                            if (procStep >= 1000)
                                            {
                                                progress.UpdateProgress(STR_LOADING, STR_LOADINGLOGIMAGES, logimgCount, index);
                                                procStep = 0;
                                            }
                                        }
                                        _fileCollection._logimgsInDB.Add(ri.ID, ri);
                                    }
                                }
                                Core.LogImages.Add(lgiList);
                            }

                            using (Utils.ProgressBlock progress = new ProgressBlock(this, STR_LOADING, STR_LOADINGGEOCACHEIMAGES, geocacheimgCount, 0))
                            {
                                int index = 0;
                                int procStep = 0;

                                List<Framework.Data.GeocacheImage> lgiList = new List<Framework.Data.GeocacheImage>();

                                using (FileStream fs = File.Open(_fileCollection.GeocacheImagesFilename, FileMode.OpenOrCreate, FileAccess.Read))
                                {
                                    fs.Position = 0;
                                    long eof = fs.Length;
                                    while (fs.Position < eof)
                                    {
                                        RecordInfo ri = new RecordInfo();
                                        ri.Offset = fs.Position;
                                        fs.Read(memBuffer, 0, lsize + 1);
                                        ms.Position = 0;
                                        ri.Length = br.ReadInt64();
                                        if (memBuffer[lsize] == 0)
                                        {
                                            //free
                                            ri.FreeSlot = true;
                                            ri.ID = string.Concat("_", ri.Offset.ToString());
                                            fs.Position = ri.Offset + ri.Length;
                                        }
                                        else
                                        {
                                            //read
                                            ri.FreeSlot = false;
                                            Framework.Data.GeocacheImage li = new Framework.Data.GeocacheImage();

                                            fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                            ms.Position = 0;
                                            li.ID = br.ReadString();
                                            ri.ID = li.ID;

                                            li.DataFromDate = DateTime.Parse(br.ReadString());
                                            li.GeocacheCode = br.ReadString();
                                            li.Description = br.ReadString();
                                            li.Name = br.ReadString();
                                            li.Url = br.ReadString();
                                            li.MobileUrl = br.ReadString();
                                            li.ThumbUrl = br.ReadString();

                                            li.Saved = true;
                                            li.IsDataChanged = false;
                                            lgiList.Add(li);

                                            index++;
                                            procStep++;
                                            if (procStep >= 1000)
                                            {
                                                progress.UpdateProgress(STR_LOADING, STR_LOADINGGEOCACHEIMAGES, geocacheimgCount, index);
                                                procStep = 0;
                                            }
                                        }
                                        _fileCollection._geocacheimgsInDB.Add(ri.ID, ri);
                                    }
                                }
                                Core.GeocacheImages.Add(lgiList);
                            }

                            //using (Utils.ProgressBlock progress = new ProgressBlock(this, STR_LOADING, STR_LOADINGLOGIMAGES, logimgCount, 0))
                            {
                                int index = 0;
                                int procStep = 0;

                                List<Framework.Data.UserWaypoint> uwplList = new List<Framework.Data.UserWaypoint>();

                                using (FileStream fs = File.Open(_fileCollection.UserWaypointsFilename, FileMode.OpenOrCreate, FileAccess.Read))
                                {
                                    fs.Position = 0;
                                    long eof = fs.Length;
                                    while (fs.Position < eof)
                                    {
                                        RecordInfo ri = new RecordInfo();
                                        ri.Offset = fs.Position;
                                        fs.Read(memBuffer, 0, lsize + 1);
                                        ms.Position = 0;
                                        ri.Length = br.ReadInt64();
                                        if (memBuffer[lsize] == 0)
                                        {
                                            //free
                                            ri.FreeSlot = true;
                                            ri.ID = string.Concat("_", ri.Offset.ToString());
                                            fs.Position = ri.Offset + ri.Length;
                                        }
                                        else
                                        {
                                            //read
                                            ri.FreeSlot = false;
                                            Framework.Data.UserWaypoint wp = new Framework.Data.UserWaypoint();

                                            fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                            ms.Position = 0;
                                            ri.ID = br.ReadString();
                                            wp.ID = int.Parse(ri.ID);

                                            wp.Description = br.ReadString();
                                            wp.GeocacheCode = br.ReadString();
                                            wp.Lat = br.ReadDouble();
                                            wp.Lon = br.ReadDouble();
                                            wp.Date = DateTime.Parse(br.ReadString());

                                            wp.Saved = true;
                                            wp.IsDataChanged = false;
                                            uwplList.Add(wp);

                                            index++;
                                            procStep++;
                                            if (procStep >= 1000)
                                            {
                                                //progress.UpdateProgress(STR_LOADING, STR_LOADINGLOGIMAGES, logimgCount, index);
                                                procStep = 0;
                                            }
                                        }
                                        _fileCollection._usrwptsInDB.Add(ri.ID, ri);
                                    }
                                }
                                Core.UserWaypoints.Add(uwplList);
                            }

                        }
                    }
                }
            }

            return result;
        }
Ejemplo n.º 32
0
        public override bool LoadWaypoints(List<Framework.Data.Waypoint> wps, List<Framework.Data.UserWaypoint> usrwps)
        {
            bool result = true;
            int index = 0;
            int procStep = 0;

            if (File.Exists(_fileCollection.DatabaseInfoFilename))
            {

                int lsize = sizeof(long);
                byte[] memBuffer = new byte[5 * 1024];
                using (MemoryStream ms = new MemoryStream(memBuffer))
                using (BinaryReader br = new BinaryReader(ms))
                {

                    int wptCount = 0;

                    XmlDocument doc = new XmlDocument();
                    doc.Load(_fileCollection.DatabaseInfoFilename);
                    XmlElement root = doc.DocumentElement;
                    wptCount = int.Parse(root.SelectSingleNode("WaypointCount").InnerText);

                    using (FileStream fs = File.Open(_fileCollection.WaypointsFilename, FileMode.OpenOrCreate, FileAccess.Read))
                    {
                        fs.Position = 0;
                        long eof = fs.Length;
                        while (fs.Position < eof)
                        {
                            RecordInfo ri = new RecordInfo();
                            ri.Offset = fs.Position;
                            fs.Read(memBuffer, 0, lsize + 1);
                            ms.Position = 0;
                            ri.Length = br.ReadInt64();
                            if (memBuffer[lsize] == 0)
                            {
                                //free
                                ri.FreeSlot = true;
                                ri.ID = string.Concat("_", ri.Offset.ToString());
                                fs.Position = ri.Offset + ri.Length;
                            }
                            else
                            {
                                //read
                                ri.FreeSlot = false;
                                Framework.Data.Waypoint wp = new Framework.Data.Waypoint();

                                fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                ms.Position = 0;
                                wp.Code = br.ReadString();
                                ri.ID = wp.Code;

                                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(Core.WaypointTypes, br.ReadInt32());

                                wp.Saved = true;
                                wp.IsDataChanged = false;
                                wps.Add(wp);

                                index++;
                                procStep++;
                                if (procStep >= 20000)
                                {
                                    UpdateLoadingInBackgroundProgress(STR_LOADING_WAYPOINTS_BG, wptCount, index);
                                    procStep = 0;
                                }
                            }
                            _fileCollection._wptsInDB.Add(ri.ID, ri);
                        }
                    }

                    using (FileStream fs = File.Open(_fileCollection.UserWaypointsFilename, FileMode.OpenOrCreate, FileAccess.Read))
                    {
                        fs.Position = 0;
                        long eof = fs.Length;
                        while (fs.Position < eof)
                        {
                            RecordInfo ri = new RecordInfo();
                            ri.Offset = fs.Position;
                            fs.Read(memBuffer, 0, lsize + 1);
                            ms.Position = 0;
                            ri.Length = br.ReadInt64();
                            if (memBuffer[lsize] == 0)
                            {
                                //free
                                ri.FreeSlot = true;
                                ri.ID = string.Concat("_", ri.Offset.ToString());
                                fs.Position = ri.Offset + ri.Length;
                            }
                            else
                            {
                                //read
                                ri.FreeSlot = false;
                                Framework.Data.UserWaypoint wp = new Framework.Data.UserWaypoint();

                                fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                ms.Position = 0;
                                ri.ID = br.ReadString();
                                wp.ID = int.Parse(ri.ID);

                                wp.Description = br.ReadString();
                                wp.GeocacheCode = br.ReadString();
                                wp.Lat = br.ReadDouble();
                                wp.Lon = br.ReadDouble();
                                wp.Date = DateTime.Parse(br.ReadString());

                                wp.Saved = true;
                                wp.IsDataChanged = false;
                                usrwps.Add(wp);
                            }
                            _fileCollection._usrwptsInDB.Add(ri.ID, ri);
                        }
                    }

                }
            }

            return result;
        }
Ejemplo n.º 33
0
 public bool AddImportedWaypoint(Framework.Data.Waypoint wp)
 {
     return(base.AddWaypoint(wp));
 }
Ejemplo n.º 34
0
        public bool RepairV1()
        {
            bool result = true;

            if (File.Exists(_fileCollection.DatabaseInfoFilename))
            {
                int    lsize     = sizeof(long);
                byte[] memBuffer = new byte[10 * 1024 * 1024];
                using (MemoryStream ms = new MemoryStream(memBuffer))
                    using (BinaryReader br = new BinaryReader(ms))
                    {
                        int gcCount     = 0;
                        int logCount    = 0;
                        int logimgCount = 0;
                        int wptCount    = 0;
                        int usrwptCount = 0;

                        XmlDocument doc = new XmlDocument();
                        doc.Load(_fileCollection.DatabaseInfoFilename);
                        XmlElement root = doc.DocumentElement;
                        gcCount     = int.Parse(root.SelectSingleNode("GeocacheCount").InnerText);
                        logCount    = int.Parse(root.SelectSingleNode("LogCount").InnerText);
                        logimgCount = int.Parse(root.SelectSingleNode("LogImagesCount").InnerText);
                        wptCount    = int.Parse(root.SelectSingleNode("WaypointCount").InnerText);
                        usrwptCount = int.Parse(root.SelectSingleNode("UserWaypointCount").InnerText);

                        DateTime nextUpdateTime = DateTime.MinValue;
                        using (Utils.ProgressBlock fixscr = new Utils.ProgressBlock(this, STR_LOADING, STR_LOADINGDATA, 1, 0))
                        {
                            using (Utils.ProgressBlock progress = new Utils.ProgressBlock(this, STR_LOADING, STR_LOADINGGEOCACHES, gcCount, 0))
                            {
                                int index = 0;

                                FileStream fs = _fileCollection._fsGeocaches;
                                fs.Position = 0;
                                long       eof            = fs.Length;
                                long       lastOKPosition = 0;
                                RecordInfo ri             = new RecordInfo();
                                try
                                {
                                    while (fs.Position < eof)
                                    {
                                        lastOKPosition = fs.Position;
                                        ri.Offset      = fs.Position;
                                        fs.Read(memBuffer, 0, lsize + 1);
                                        ms.Position = 0;
                                        ri.Length   = br.ReadInt64();
                                        if (memBuffer[lsize] == 0)
                                        {
                                            //free
                                            ri.FreeSlot = true;
                                            ri.ID       = string.Concat("_", ri.Offset.ToString());
                                            fs.Position = ri.Offset + ri.Length;
                                        }
                                        else if (memBuffer[lsize] == 2)
                                        {
                                            ri.FreeSlot = false;
                                            fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                            ms.Position = 0;
                                            ri.ID       = br.ReadString();
                                            br.ReadString();
                                            br.ReadBoolean();
                                            br.ReadString();
                                            br.ReadBoolean();
                                        }
                                        else
                                        {
                                            //read
                                            ri.FreeSlot = false;
                                            Framework.Data.Geocache gc = new Framework.Data.Geocache();

                                            fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                            ms.Position = 0;
                                            gc.Code     = br.ReadString();
                                            ri.ID       = gc.Code;

                                            gc.Archived     = br.ReadBoolean();
                                            gc.AttributeIds = ReadIntegerArray(br);
                                            gc.Available    = br.ReadBoolean();
                                            gc.City         = br.ReadString();
                                            gc.Container    = Utils.DataAccess.GetGeocacheContainer(Core.GeocacheContainers, br.ReadInt32());
                                            gc.CustomCoords = 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();
                                            gc.GeocacheType  = Utils.DataAccess.GetGeocacheType(Core.GeocacheTypes, br.ReadInt32());
                                            gc.ID            = br.ReadString();
                                            gc.Lat           = br.ReadDouble();
                                            gc.Lon           = br.ReadDouble();
                                            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.PersonaleNote = br.ReadString();
                                            gc.PlacedBy      = br.ReadString();
                                            gc.PublishedTime = DateTime.Parse(br.ReadString());
                                            gc.State         = br.ReadString();
                                            gc.Terrain       = br.ReadDouble();
                                            gc.Title         = br.ReadString();
                                            gc.Url           = br.ReadString();
                                            gc.DataFromDate  = DateTime.Parse(br.ReadString());
                                            gc.Locked        = br.ReadBoolean();

                                            gc.Saved         = true;
                                            gc.IsDataChanged = false;

                                            index++;
                                            if (DateTime.Now >= nextUpdateTime)
                                            {
                                                progress.UpdateProgress(STR_LOADING, STR_LOADINGGEOCACHES, gcCount, index);
                                                nextUpdateTime = DateTime.Now.AddSeconds(1);
                                            }
                                        }

                                        checkUniqueID(ri, fs);
                                    }
                                }
                                catch
                                {
                                    //error in file after lastOKPosition
                                    fs.SetLength(lastOKPosition);
                                }
                            }

                            _uniqueCheckList.Clear();
                            using (Utils.ProgressBlock progress = new ProgressBlock(this, STR_LOADING, STR_LOADINGLOGS, logCount, 0))
                            {
                                int index = 0;
                                nextUpdateTime = DateTime.MinValue;

                                FileStream fs = _fileCollection._fsLogs;
                                fs.Position = 0;
                                long       lastOKPosition = 0;
                                long       eof            = fs.Length;
                                RecordInfo ri             = new RecordInfo();
                                try
                                {
                                    while (fs.Position < eof)
                                    {
                                        lastOKPosition = fs.Position;
                                        ri.Offset      = fs.Position;
                                        fs.Read(memBuffer, 0, lsize + 1);
                                        ms.Position = 0;
                                        ri.Length   = br.ReadInt64();
                                        if (memBuffer[lsize] == 0)
                                        {
                                            //free
                                            ri.FreeSlot = true;
                                            ri.ID       = string.Concat("_", ri.Offset.ToString());
                                            fs.Position = ri.Offset + ri.Length;
                                        }
                                        else if (memBuffer[lsize] == 2)
                                        {
                                            ri.FreeSlot = false;
                                            fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                            ms.Position = 0;
                                            ri.ID       = br.ReadString();
                                            br.ReadString();
                                            br.ReadString();
                                            br.ReadString();
                                            br.ReadBoolean();
                                        }
                                        else
                                        {
                                            //read
                                            ri.FreeSlot = false;
                                            Framework.Data.Log log = new Framework.Data.Log();

                                            fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                            ms.Position = 0;
                                            log.ID      = br.ReadString();
                                            ri.ID       = log.ID;

                                            log.DataFromDate = DateTime.Parse(br.ReadString());
                                            log.Date         = DateTime.Parse(br.ReadString());
                                            log.Finder       = br.ReadString();
                                            log.GeocacheCode = br.ReadString();
                                            log.ID           = br.ReadString();
                                            log.LogType      = Utils.DataAccess.GetLogType(Core.LogTypes, br.ReadInt32());

                                            log.Saved         = true;
                                            log.IsDataChanged = false;

                                            index++;
                                            if (DateTime.Now >= nextUpdateTime)
                                            {
                                                progress.UpdateProgress(STR_LOADING, STR_LOADINGLOGS, logCount, index);
                                                nextUpdateTime = DateTime.Now.AddSeconds(1);
                                            }
                                        }
                                        checkUniqueID(ri, fs);
                                    }
                                }
                                catch
                                {
                                    //error in file after lastOKPosition
                                    fs.SetLength(lastOKPosition);
                                }
                            }

                            _uniqueCheckList.Clear();
                            using (Utils.ProgressBlock progress = new ProgressBlock(this, STR_LOADING, STR_LOADINGWAYPOINTS, wptCount, 0))
                            {
                                int index    = 0;
                                int procStep = 0;

                                using (FileStream fs = File.Open(_fileCollection.WaypointsFilename, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                                {
                                    fs.Position = 0;
                                    long       eof            = fs.Length;
                                    long       lastOKPosition = 0;
                                    RecordInfo ri             = new RecordInfo();
                                    try
                                    {
                                        while (fs.Position < eof)
                                        {
                                            lastOKPosition = fs.Position;
                                            ri.Offset      = fs.Position;
                                            fs.Read(memBuffer, 0, lsize + 1);
                                            ms.Position = 0;
                                            ri.Length   = br.ReadInt64();
                                            if (memBuffer[lsize] == 0)
                                            {
                                                //free
                                                ri.FreeSlot = true;
                                                ri.ID       = string.Concat("_", ri.Offset.ToString());
                                                fs.Position = ri.Offset + ri.Length;
                                            }
                                            else
                                            {
                                                //read
                                                ri.FreeSlot = false;
                                                Framework.Data.Waypoint wp = new Framework.Data.Waypoint();

                                                fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                                ms.Position = 0;
                                                wp.Code     = br.ReadString();
                                                ri.ID       = wp.Code;

                                                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(Core.WaypointTypes, br.ReadInt32());

                                                wp.Saved         = true;
                                                wp.IsDataChanged = false;

                                                index++;
                                                procStep++;
                                                if (procStep >= 1000)
                                                {
                                                    progress.UpdateProgress(STR_LOADING, STR_LOADINGWAYPOINTS, wptCount, index);
                                                    procStep = 0;
                                                }
                                            }
                                            checkUniqueID(ri, fs);
                                        }
                                    }
                                    catch
                                    {
                                        //error in file after lastOKPosition
                                        fs.SetLength(lastOKPosition);
                                    }
                                }
                            }

                            _uniqueCheckList.Clear();
                            using (Utils.ProgressBlock progress = new ProgressBlock(this, STR_LOADING, STR_LOADINGLOGIMAGES, logimgCount, 0))
                            {
                                int index    = 0;
                                int procStep = 0;

                                using (FileStream fs = File.Open(_fileCollection.LogImagesFilename, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                                {
                                    fs.Position = 0;
                                    long       eof            = fs.Length;
                                    long       lastOKPosition = 0;
                                    RecordInfo ri             = new RecordInfo();
                                    try
                                    {
                                        while (fs.Position < eof)
                                        {
                                            lastOKPosition = fs.Position;
                                            ri.Offset      = fs.Position;
                                            fs.Read(memBuffer, 0, lsize + 1);
                                            ms.Position = 0;
                                            ri.Length   = br.ReadInt64();
                                            if (memBuffer[lsize] == 0)
                                            {
                                                //free
                                                ri.FreeSlot = true;
                                                ri.ID       = string.Concat("_", ri.Offset.ToString());
                                                fs.Position = ri.Offset + ri.Length;
                                            }
                                            else
                                            {
                                                //read
                                                ri.FreeSlot = false;
                                                Framework.Data.LogImage li = new Framework.Data.LogImage();

                                                fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                                ms.Position = 0;
                                                li.ID       = br.ReadString();
                                                ri.ID       = li.ID;

                                                li.DataFromDate = DateTime.Parse(br.ReadString());
                                                li.LogID        = br.ReadString();
                                                li.Name         = br.ReadString();
                                                li.Url          = br.ReadString();

                                                li.Saved         = true;
                                                li.IsDataChanged = false;

                                                index++;
                                                procStep++;
                                                if (procStep >= 1000)
                                                {
                                                    progress.UpdateProgress(STR_LOADING, STR_LOADINGLOGIMAGES, logimgCount, index);
                                                    procStep = 0;
                                                }
                                            }
                                            checkUniqueID(ri, fs);
                                        }
                                    }
                                    catch
                                    {
                                        //error in file after lastOKPosition
                                        fs.SetLength(lastOKPosition);
                                    }
                                }
                            }

                            {
                                int index = 0;
                                _uniqueCheckList.Clear();
                                using (FileStream fs = File.Open(_fileCollection.UserWaypointsFilename, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                                {
                                    fs.Position = 0;
                                    long       eof            = fs.Length;
                                    long       lastOKPosition = 0;
                                    RecordInfo ri             = new RecordInfo();
                                    try
                                    {
                                        while (fs.Position < eof)
                                        {
                                            lastOKPosition = fs.Position;
                                            ri.Offset      = fs.Position;
                                            fs.Read(memBuffer, 0, lsize + 1);
                                            ms.Position = 0;
                                            ri.Length   = br.ReadInt64();
                                            if (memBuffer[lsize] == 0)
                                            {
                                                //free
                                                ri.FreeSlot = true;
                                                ri.ID       = string.Concat("_", ri.Offset.ToString());
                                                fs.Position = ri.Offset + ri.Length;
                                            }
                                            else
                                            {
                                                //read
                                                ri.FreeSlot = false;
                                                Framework.Data.UserWaypoint wp = new Framework.Data.UserWaypoint();

                                                fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                                ms.Position = 0;
                                                ri.ID       = br.ReadString();
                                                wp.ID       = int.Parse(ri.ID);

                                                wp.Description  = br.ReadString();
                                                wp.GeocacheCode = br.ReadString();
                                                wp.Lat          = br.ReadDouble();
                                                wp.Lon          = br.ReadDouble();
                                                wp.Date         = DateTime.Parse(br.ReadString());

                                                wp.Saved         = true;
                                                wp.IsDataChanged = false;

                                                index++;
                                            }
                                            checkUniqueID(ri, fs);
                                        }
                                    }
                                    catch
                                    {
                                        //error in file after lastOKPosition
                                        fs.SetLength(lastOKPosition);
                                    }
                                }
                            }
                        }
                    }
            }

            return(result);
        }
Ejemplo n.º 35
0
        private bool insertFromDatabase(bool overwrite)
        {
            bool result = false;

            try
            {
                int    lsize     = sizeof(long);
                byte[] memBuffer = new byte[10 * 1024 * 1024];
                using (MemoryStream ms = new MemoryStream(memBuffer))
                    using (BinaryReader br = new BinaryReader(ms))
                        using (FileCollection fc = new FileCollection(_selectedInsertFromFilename))
                        {
                            int gcCount          = 0;
                            int logCount         = 0;
                            int logimgCount      = 0;
                            int geocacheimgCount = 0;
                            int wptCount         = 0;
                            int usrwptCount      = 0;

                            Hashtable htInsertedGeocaches = new Hashtable();
                            Hashtable htInsertedLogs      = new Hashtable();

                            XmlDocument doc = new XmlDocument();
                            doc.Load(fc.DatabaseInfoFilename);
                            XmlElement root = doc.DocumentElement;
                            gcCount     = int.Parse(root.SelectSingleNode("GeocacheCount").InnerText);
                            logCount    = int.Parse(root.SelectSingleNode("LogCount").InnerText);
                            logimgCount = int.Parse(root.SelectSingleNode("LogImagesCount").InnerText);
                            wptCount    = int.Parse(root.SelectSingleNode("WaypointCount").InnerText);
                            usrwptCount = int.Parse(root.SelectSingleNode("UserWaypointCount").InnerText);
                            if (root.SelectSingleNode("GeocacheImagesCount") != null)
                            {
                                geocacheimgCount = int.Parse(root.SelectSingleNode("GeocacheImagesCount").InnerText);
                            }

                            DateTime   nextUpdateTime = DateTime.MinValue;
                            RecordInfo ri             = new RecordInfo();
                            using (Utils.ProgressBlock fixscr = new Utils.ProgressBlock(this, STR_LOADING, STR_LOADINGDATA, 1, 0))
                            {
                                using (Utils.ProgressBlock progress = new Utils.ProgressBlock(this, STR_LOADING, STR_LOADINGGEOCACHES, gcCount, 0))
                                {
                                    int index = 0;

                                    FileStream fs = fc._fsGeocaches;
                                    fs.Position = 0;
                                    long eof = fs.Length;
                                    while (fs.Position < eof)
                                    {
                                        ri.Offset = fs.Position;
                                        fs.Read(memBuffer, 0, lsize + 1);
                                        ms.Position = 0;
                                        ri.Length   = br.ReadInt64();
                                        if (memBuffer[lsize] == 0)
                                        {
                                            //free
                                            //ignore
                                            fs.Position = ri.Offset + ri.Length;
                                        }
                                        else if (memBuffer[lsize] == 2)
                                        {
                                            //read
                                            fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                            ms.Position = 0;
                                            ri.ID       = br.ReadString().Substring(2);
                                            bool newGeocache;
                                            Framework.Data.Geocache gc = Utils.DataAccess.GetGeocache(Core.Geocaches, ri.ID);
                                            if (gc == null)
                                            {
                                                gc = new Framework.Data.Geocache();
                                                htInsertedGeocaches.Add(ri.ID, gc);
                                                newGeocache = true;
                                            }
                                            else
                                            {
                                                if (overwrite || htInsertedGeocaches[ri.ID] != null)
                                                {
                                                    newGeocache = false;
                                                }
                                                else
                                                {
                                                    continue;
                                                }
                                            }
                                            gc.BeginUpdate();
                                            try
                                            {
                                                gc.Code                   = ri.ID;
                                                gc.ShortDescription       = br.ReadString();
                                                gc.ShortDescriptionInHtml = br.ReadBoolean();
                                                gc.LongDescription        = br.ReadString();
                                                gc.LongDescriptionInHtml  = br.ReadBoolean();
                                            }
                                            catch
                                            {
                                            }
                                            gc.EndUpdate();
                                            if (newGeocache)
                                            {
                                                Core.Geocaches.Add(gc);
                                            }
                                        }
                                        else
                                        {
                                            //read

                                            fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                            ms.Position = 0;
                                            ri.ID       = br.ReadString();

                                            bool newGeocache;
                                            Framework.Data.Geocache gc = Utils.DataAccess.GetGeocache(Core.Geocaches, ri.ID);
                                            if (gc == null)
                                            {
                                                gc = new Framework.Data.Geocache();
                                                htInsertedGeocaches.Add(ri.ID, gc);
                                                newGeocache = true;
                                            }
                                            else
                                            {
                                                if (overwrite || htInsertedGeocaches[ri.ID] != null)
                                                {
                                                    newGeocache = false;
                                                }
                                                else
                                                {
                                                    continue;
                                                }
                                            }
                                            gc.BeginUpdate();
                                            try
                                            {
                                                gc.Code         = ri.ID;
                                                gc.Archived     = br.ReadBoolean();
                                                gc.AttributeIds = ReadIntegerArray(br);
                                                gc.Available    = br.ReadBoolean();
                                                gc.City         = br.ReadString();
                                                gc.Container    = Utils.DataAccess.GetGeocacheContainer(Core.GeocacheContainers, br.ReadInt32());
                                                gc.CustomCoords = 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();
                                                gc.GeocacheType  = Utils.DataAccess.GetGeocacheType(Core.GeocacheTypes, br.ReadInt32());
                                                gc.ID            = br.ReadString();
                                                gc.Lat           = br.ReadDouble();
                                                gc.Lon           = br.ReadDouble();
                                                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.PersonaleNote = br.ReadString();
                                                gc.PlacedBy      = br.ReadString();
                                                gc.PublishedTime = DateTime.Parse(br.ReadString());
                                                gc.State         = br.ReadString();
                                                gc.Terrain       = br.ReadDouble();
                                                gc.Title         = br.ReadString();
                                                gc.Url           = br.ReadString();
                                                gc.DataFromDate  = DateTime.Parse(br.ReadString());
                                                gc.Locked        = br.ReadBoolean();

                                                Calculus.SetDistanceAndAngleGeocacheFromLocation(gc, Core.CenterLocation);
                                            }
                                            catch
                                            {
                                            }
                                            gc.EndUpdate();

                                            if (newGeocache)
                                            {
                                                Core.Geocaches.Add(gc);
                                            }

                                            index++;
                                            if (DateTime.Now >= nextUpdateTime)
                                            {
                                                progress.UpdateProgress(STR_LOADING, STR_LOADINGGEOCACHES, gcCount, index);
                                                nextUpdateTime = DateTime.Now.AddSeconds(1);
                                            }
                                        }
                                    }
                                }

                                using (Utils.ProgressBlock progress = new ProgressBlock(this, STR_LOADING, STR_LOADINGLOGS, logCount, 0))
                                {
                                    int index = 0;
                                    nextUpdateTime = DateTime.MinValue;

                                    FileStream fs = fc._fsLogs;
                                    fs.Position = 0;
                                    long eof = fs.Length;
                                    while (fs.Position < eof)
                                    {
                                        ri.Offset = fs.Position;
                                        fs.Read(memBuffer, 0, lsize + 1);
                                        ms.Position = 0;
                                        ri.Length   = br.ReadInt64();
                                        if (memBuffer[lsize] == 0)
                                        {
                                            //free
                                            fs.Position = ri.Offset + ri.Length;
                                        }
                                        else if (memBuffer[lsize] == 2)
                                        {
                                            //read
                                            fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                            ms.Position = 0;
                                            ri.ID       = br.ReadString().Substring(2);
                                            fs.Position = ri.Offset + ri.Length;

                                            bool newLog;
                                            bool logComplete       = true;
                                            Framework.Data.Log log = Utils.DataAccess.GetLog(Core.Logs, ri.ID);
                                            if (log == null)
                                            {
                                                newLog = true;
                                                log    = htInsertedLogs[ri.ID] as Framework.Data.Log;
                                                if (log == null)
                                                {
                                                    log = new Framework.Data.Log();
                                                    htInsertedLogs.Add(ri.ID, log);
                                                    logComplete = false;
                                                }
                                                else
                                                {
                                                    logComplete = true;
                                                }
                                            }
                                            else
                                            {
                                                if (overwrite)
                                                {
                                                    newLog = false;
                                                }
                                                else
                                                {
                                                    continue;
                                                }
                                            }
                                            log.BeginUpdate();
                                            try
                                            {
                                                log.ID       = ri.ID;
                                                log.TBCode   = br.ReadString();
                                                log.FinderId = br.ReadString();
                                                log.Text     = br.ReadString();
                                                log.Encoded  = br.ReadBoolean();
                                            }
                                            catch
                                            {
                                            }
                                            log.EndUpdate();
                                            if (newLog && logComplete && htInsertedLogs[ri.ID] != null)
                                            {
                                                if (htInsertedGeocaches[log.GeocacheCode ?? ""] != null || Utils.DataAccess.GetGeocache(Core.Geocaches, log.GeocacheCode ?? "") != null)
                                                {
                                                    Core.Logs.Add(log);
                                                }
                                                htInsertedLogs.Remove(ri.ID);
                                            }
                                        }
                                        else
                                        {
                                            //read
                                            fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                            ms.Position = 0;
                                            ri.ID       = br.ReadString();

                                            bool newLog;
                                            bool logComplete       = true;
                                            Framework.Data.Log log = Utils.DataAccess.GetLog(Core.Logs, ri.ID);
                                            if (log == null)
                                            {
                                                newLog = true;
                                                log    = htInsertedLogs[ri.ID] as Framework.Data.Log;
                                                if (log == null)
                                                {
                                                    log = new Framework.Data.Log();
                                                    htInsertedLogs.Add(ri.ID, log);
                                                    logComplete = false;
                                                }
                                                else
                                                {
                                                    logComplete = true;
                                                }
                                            }
                                            else
                                            {
                                                if (overwrite)
                                                {
                                                    newLog = false;
                                                }
                                                else
                                                {
                                                    continue;
                                                }
                                            }
                                            log.BeginUpdate();
                                            try
                                            {
                                                log.ID           = ri.ID;
                                                log.DataFromDate = DateTime.Parse(br.ReadString());
                                                log.Date         = DateTime.Parse(br.ReadString());
                                                log.Finder       = br.ReadString();
                                                log.GeocacheCode = br.ReadString();
                                                log.ID           = br.ReadString();
                                                log.LogType      = Utils.DataAccess.GetLogType(Core.LogTypes, br.ReadInt32());
                                            }
                                            catch
                                            {
                                            }
                                            log.EndUpdate();

                                            if (newLog && logComplete && htInsertedLogs[ri.ID] != null)
                                            {
                                                //check if geocache is present
                                                if (htInsertedGeocaches[log.GeocacheCode ?? ""] != null || Utils.DataAccess.GetGeocache(Core.Geocaches, log.GeocacheCode ?? "") != null)
                                                {
                                                    Core.Logs.Add(log);
                                                }
                                                htInsertedLogs.Remove(ri.ID);
                                            }

                                            index++;
                                            if (DateTime.Now >= nextUpdateTime)
                                            {
                                                progress.UpdateProgress(STR_LOADING, STR_LOADINGLOGS, logCount, index);
                                                nextUpdateTime = DateTime.Now.AddSeconds(1);
                                            }
                                        }
                                    }
                                }


                                using (Utils.ProgressBlock progress = new ProgressBlock(this, STR_LOADING, STR_LOADINGWAYPOINTS, wptCount, 0))
                                {
                                    int index    = 0;
                                    int procStep = 0;

                                    using (FileStream fs = File.Open(fc.WaypointsFilename, FileMode.OpenOrCreate, FileAccess.Read))
                                    {
                                        fs.Position = 0;
                                        long eof = fs.Length;
                                        while (fs.Position < eof)
                                        {
                                            ri.Offset = fs.Position;
                                            fs.Read(memBuffer, 0, lsize + 1);
                                            ms.Position = 0;
                                            ri.Length   = br.ReadInt64();
                                            if (memBuffer[lsize] == 0)
                                            {
                                                //free
                                                fs.Position = ri.Offset + ri.Length;
                                            }
                                            else
                                            {
                                                //read
                                                fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                                ms.Position = 0;
                                                ri.ID       = br.ReadString();

                                                bool newWp;
                                                Framework.Data.Waypoint wp = Utils.DataAccess.GetWaypoint(Core.Waypoints, ri.ID);
                                                if (wp == null)
                                                {
                                                    newWp = true;
                                                    wp    = new Framework.Data.Waypoint();
                                                }
                                                else
                                                {
                                                    if (overwrite)
                                                    {
                                                        newWp = false;
                                                    }
                                                    else
                                                    {
                                                        continue;
                                                    }
                                                }

                                                wp.BeginUpdate();
                                                try
                                                {
                                                    wp.Code         = ri.ID;
                                                    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(Core.WaypointTypes, br.ReadInt32());
                                                }
                                                catch
                                                {
                                                }
                                                wp.EndUpdate();
                                                if (newWp)
                                                {
                                                    if (Utils.DataAccess.GetGeocache(Core.Geocaches, wp.GeocacheCode ?? "") != null)
                                                    {
                                                        Core.Waypoints.Add(wp);
                                                    }
                                                }

                                                index++;
                                                procStep++;
                                                if (procStep >= 1000)
                                                {
                                                    progress.UpdateProgress(STR_LOADING, STR_LOADINGWAYPOINTS, wptCount, index);
                                                    procStep = 0;
                                                }
                                            }
                                        }
                                    }
                                }

                                using (Utils.ProgressBlock progress = new ProgressBlock(this, STR_LOADING, STR_LOADINGLOGIMAGES, logimgCount, 0))
                                {
                                    int index    = 0;
                                    int procStep = 0;

                                    using (FileStream fs = File.Open(fc.LogImagesFilename, FileMode.OpenOrCreate, FileAccess.Read))
                                    {
                                        fs.Position = 0;
                                        long eof = fs.Length;
                                        while (fs.Position < eof)
                                        {
                                            ri.Offset = fs.Position;
                                            fs.Read(memBuffer, 0, lsize + 1);
                                            ms.Position = 0;
                                            ri.Length   = br.ReadInt64();
                                            if (memBuffer[lsize] == 0)
                                            {
                                                //free
                                                fs.Position = ri.Offset + ri.Length;
                                            }
                                            else
                                            {
                                                //read
                                                fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                                ms.Position = 0;
                                                ri.ID       = br.ReadString();

                                                bool newWp;
                                                Framework.Data.LogImage li = Utils.DataAccess.GetLogImage(Core.LogImages, ri.ID);
                                                if (li == null)
                                                {
                                                    newWp = true;
                                                    li    = new Framework.Data.LogImage();
                                                }
                                                else
                                                {
                                                    if (overwrite)
                                                    {
                                                        newWp = false;
                                                    }
                                                    else
                                                    {
                                                        continue;
                                                    }
                                                }

                                                li.BeginUpdate();
                                                try
                                                {
                                                    li.ID           = ri.ID;
                                                    li.DataFromDate = DateTime.Parse(br.ReadString());
                                                    li.LogID        = br.ReadString();
                                                    li.Name         = br.ReadString();
                                                    li.Url          = br.ReadString();
                                                }
                                                catch
                                                {
                                                }
                                                li.EndUpdate();

                                                if (newWp)
                                                {
                                                    if (Utils.DataAccess.GetLog(Core.Logs, li.LogID ?? "") != null)
                                                    {
                                                        Core.LogImages.Add(li);
                                                    }
                                                }

                                                index++;
                                                procStep++;
                                                if (procStep >= 1000)
                                                {
                                                    progress.UpdateProgress(STR_LOADING, STR_LOADINGLOGIMAGES, logimgCount, index);
                                                    procStep = 0;
                                                }
                                            }
                                        }
                                    }
                                }

                                using (Utils.ProgressBlock progress = new ProgressBlock(this, STR_LOADING, STR_LOADINGGEOCACHEIMAGES, geocacheimgCount, 0))
                                {
                                    int index    = 0;
                                    int procStep = 0;

                                    using (FileStream fs = File.Open(fc.GeocacheImagesFilename, FileMode.OpenOrCreate, FileAccess.Read))
                                    {
                                        fs.Position = 0;
                                        long eof = fs.Length;
                                        while (fs.Position < eof)
                                        {
                                            ri.Offset = fs.Position;
                                            fs.Read(memBuffer, 0, lsize + 1);
                                            ms.Position = 0;
                                            ri.Length   = br.ReadInt64();
                                            if (memBuffer[lsize] == 0)
                                            {
                                                //free
                                                fs.Position = ri.Offset + ri.Length;
                                            }
                                            else
                                            {
                                                //read
                                                fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                                ms.Position = 0;
                                                ri.ID       = br.ReadString();

                                                bool newWp;
                                                Framework.Data.GeocacheImage wp = Utils.DataAccess.GetGeocacheImage(Core.GeocacheImages, ri.ID);
                                                if (wp == null)
                                                {
                                                    newWp = true;
                                                    wp    = new Framework.Data.GeocacheImage();
                                                }
                                                else
                                                {
                                                    if (overwrite)
                                                    {
                                                        newWp = false;
                                                    }
                                                    else
                                                    {
                                                        continue;
                                                    }
                                                }

                                                wp.BeginUpdate();
                                                try
                                                {
                                                    wp.ID           = ri.ID;
                                                    wp.DataFromDate = DateTime.Parse(br.ReadString());
                                                    wp.GeocacheCode = br.ReadString();
                                                    wp.Description  = br.ReadString();
                                                    wp.Name         = br.ReadString();
                                                    wp.Url          = br.ReadString();
                                                    wp.MobileUrl    = br.ReadString();
                                                    wp.ThumbUrl     = br.ReadString();
                                                }
                                                catch
                                                {
                                                }
                                                wp.EndUpdate();
                                                if (newWp)
                                                {
                                                    if (Utils.DataAccess.GetGeocache(Core.Geocaches, wp.GeocacheCode ?? "") != null)
                                                    {
                                                        Core.GeocacheImages.Add(wp);
                                                    }
                                                }

                                                index++;
                                                procStep++;
                                                if (procStep >= 1000)
                                                {
                                                    progress.UpdateProgress(STR_LOADING, STR_LOADINGGEOCACHEIMAGES, geocacheimgCount, index);
                                                    procStep = 0;
                                                }
                                            }
                                        }
                                    }
                                }

                                {
                                    using (FileStream fs = File.Open(fc.UserWaypointsFilename, FileMode.OpenOrCreate, FileAccess.Read))
                                    {
                                        fs.Position = 0;
                                        long eof = fs.Length;
                                        while (fs.Position < eof)
                                        {
                                            ri.Offset = fs.Position;
                                            fs.Read(memBuffer, 0, lsize + 1);
                                            ms.Position = 0;
                                            ri.Length   = br.ReadInt64();
                                            if (memBuffer[lsize] == 0)
                                            {
                                                //free
                                                fs.Position = ri.Offset + ri.Length;
                                            }
                                            else
                                            {
                                                //read
                                                fs.Read(memBuffer, 0, (int)(ri.Length - lsize - 1));
                                                ms.Position = 0;
                                                ri.ID       = br.ReadString();

                                                bool newWp;
                                                Framework.Data.UserWaypoint wp = Utils.DataAccess.GetUserWaypoint(Core.UserWaypoints, int.Parse(ri.ID));
                                                if (wp == null)
                                                {
                                                    newWp = true;
                                                    wp    = new Framework.Data.UserWaypoint();
                                                }
                                                else
                                                {
                                                    if (overwrite)
                                                    {
                                                        newWp = false;
                                                    }
                                                    else
                                                    {
                                                        continue;
                                                    }
                                                }

                                                wp.BeginUpdate();
                                                try
                                                {
                                                    wp.ID           = int.Parse(ri.ID);
                                                    wp.Description  = br.ReadString();
                                                    wp.GeocacheCode = br.ReadString();
                                                    wp.Lat          = br.ReadDouble();
                                                    wp.Lon          = br.ReadDouble();
                                                    wp.Date         = DateTime.Parse(br.ReadString());
                                                }
                                                catch
                                                {
                                                }
                                                if (newWp)
                                                {
                                                    if (Utils.DataAccess.GetGeocache(Core.Geocaches, wp.GeocacheCode ?? "") != null)
                                                    {
                                                        Core.UserWaypoints.Add(wp);
                                                    }
                                                }

                                                wp.EndUpdate();
                                            }
                                        }
                                    }
                                }
                            }
                        }
                result = true;
            }
            catch
            {
            }
            return(result);
        }