public void OnProgressBlockCompleted(ProgressBlock block)
 {
     if (progressBlock != null)
     {
         progressBlock(block);
     }
 }
Example #2
0
 void Awake()
 {
     states   = new List <InGameState> ();
     progress = new ProgressBlock();
     inGame   = this;
     InitGame();
     CreateDynamics();
     CreateStates();
 }
Example #3
0
 private void EventsManager_progressBlock(ProgressBlock progressBlock)
 {
     if (progressBlock.goldPrize > 0)
     {
         goldField.AddAmount(progressBlock.goldPrize);
     }
     if (progressBlock.tokenPrize > 0)
     {
         tokenField.AddAmount(progressBlock.tokenPrize);
     }
 }
Example #4
0
 private void threadMethod()
 {
     try
     {
         using (ProgressBlock progress = new ProgressBlock(_plugin, "Update archived geocaches", "Downloading data from globalcaching.eu", 1, 0))
         {
             using (System.Net.WebClient wc = new System.Net.WebClient())
             {
                 byte[] data = wc.DownloadData(string.Format("https://www.4geocaching.eu/Service/Archived.aspx?country=Belgium&prefix=GC&token={0}", System.Web.HttpUtility.UrlEncode(_core.GeocachingComAccount.APIToken)));
                 string sdoc = CompressText.UnzipText(data);
                 //_core.DebugLog(DebugLogLevel.Info, _plugin, null, sdoc);
                 XmlDocument doc = new XmlDocument();
                 doc.LoadXml(sdoc);
                 XmlElement  root   = doc.DocumentElement;
                 XmlNodeList strngs = root.SelectNodes("wp");
                 if (strngs != null)
                 {
                     progress.UpdateProgress("Update archived geocaches", "Updating geocaches...", strngs.Count, 0);
                     Geocache gc;
                     int      index = 0;
                     foreach (XmlNode sn in strngs)
                     {
                         gc = DataAccess.GetGeocache(_core.Geocaches, sn.InnerText);
                         if (gc != null)
                         {
                             gc.Archived  = true;
                             gc.Available = false;
                         }
                         index++;
                         if (index % 50 == 0)
                         {
                             //_core.DebugLog(DebugLogLevel.Info, _plugin, null, index.ToString());
                             progress.UpdateProgress("Update archived geocaches", "Updating geocaches...", strngs.Count, index);
                         }
                     }
                 }
             }
         }
     }
     catch (Exception e)
     {
         _errormessage = e.Message;
     }
     _actionReady.Set();
 }
 private void threadMethod()
 {
     try
     {
         using (ProgressBlock progress = new ProgressBlock(_plugin, "Update Favorite for Netherlands, Belgium and Italy", "Downloading data from globalcaching.eu", 1, 0))
         {
             using (System.Net.WebClient wc = new System.Net.WebClient())
             {
                 string doc = wc.DownloadString(string.Format("https://www.4geocaching.eu/Service/CacheFavorites.aspx?token={0}", System.Web.HttpUtility.UrlEncode(_core.GeocachingComAccount.APIToken)));
                 if (doc != null)
                 {
                     string[] lines = doc.Replace("\r", "").Split(new char[] { '\n' });
                     progress.UpdateProgress("Update Favorite of geocaches", "Updating geocaches...", lines.Length, 0);
                     Geocache gc;
                     char[]   sep = new char[] { ',' };
                     string[] parts;
                     foreach (string s in lines)
                     {
                         parts = s.Split(sep);
                         if (parts.Length > 0)
                         {
                             gc = DataAccess.GetGeocache(_core.Geocaches, parts[0]);
                             if (gc != null)
                             {
                                 gc.Favorites = int.Parse(parts[1]);
                             }
                         }
                     }
                 }
             }
         }
     }
     catch (Exception e)
     {
         _errormessage = e.Message;
     }
     _actionReady.Set();
 }
Example #6
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);
        }
    private void threadMethod()
    {
        try
        {
            bool cancel = false;
            using (ProgressBlock progress = new ProgressBlock(_plugin, "Bijwerken van status en nieuwe geocaches", "Download gegevens van globalcaching.eu", 1, 0, true))
            {
                using (System.Net.WebClient wc = new System.Net.WebClient())
                {
                    string doc = wc.DownloadString(string.Format("http://www.globalcaching.eu/Service/GeocacheCodes.aspx?country=Belgium&token={0}", System.Web.HttpUtility.UrlEncode(_core.GeocachingComAccount.APIToken)));
                    if (doc != null)
                    {
                        List <string> gcList = new List <string>();

                        string[] lines = doc.Replace("\r", "").Split(new char[] { '\n' });
                        progress.UpdateProgress("Bijwerken van status en nieuwe geocaches", "Bijwerken van de status...", lines.Length, 0);
                        Geocache gc;
                        char[]   sep = new char[] { ',' };
                        string[] parts;
                        foreach (string s in lines)
                        {
                            parts = s.Split(sep);
                            if (parts.Length > 2)
                            {
                                gc = DataAccess.GetGeocache(_core.Geocaches, parts[0]);
                                if (gc != null)
                                {
                                    gc.Archived  = parts[1] != "0";
                                    gc.Available = parts[2] != "0";
                                }
                                else if (parts[1] == "0") //add only none archived
                                {
                                    gcList.Add(parts[0]);
                                }
                            }
                        }

                        if (gcList.Count == 0)
                        {
                            System.Windows.Forms.MessageBox.Show("Er zijn geen nieuwe geocaches gevonden", "Bericht", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
                        }
                        else
                        {
                            if (System.Windows.Forms.MessageBox.Show(string.Format("Er zijn {0} nieuwe geocaches gevonden\r\nDeze downloaden?", gcList.Count), "Bericht", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Information) == System.Windows.Forms.DialogResult.Yes)
                            {
                                progress.UpdateProgress("Bijwerken van status en nieuwe geocaches", "Importeren van geocaches...", gcList.Count, 0);

                                using (GeocachingLiveV6 client = new GeocachingLiveV6(_core, string.IsNullOrEmpty(_core.GeocachingComAccount.APIToken)))
                                {
                                    int      index = 0;
                                    int      total = gcList.Count;
                                    int      gcupdatecount;
                                    TimeSpan interval = new TimeSpan(0, 0, 0, 2, 100);
                                    DateTime prevCall = DateTime.MinValue;
                                    bool     dodelay;
                                    gcupdatecount = 30;
                                    dodelay       = (gcList.Count > 30);
                                    while (gcList.Count > 0)
                                    {
                                        if (dodelay)
                                        {
                                            TimeSpan ts = DateTime.Now - prevCall;
                                            if (ts < interval)
                                            {
                                                Thread.Sleep(interval - ts);
                                            }
                                        }
                                        GlobalcachingApplication.Utils.API.LiveV6.SearchForGeocachesRequest req = new GlobalcachingApplication.Utils.API.LiveV6.SearchForGeocachesRequest();
                                        req.IsLite               = false;
                                        req.AccessToken          = client.Token;
                                        req.CacheCode            = new GlobalcachingApplication.Utils.API.LiveV6.CacheCodeFilter();
                                        req.CacheCode.CacheCodes = (from a in gcList select a).Take(gcupdatecount).ToArray();
                                        req.MaxPerPage           = gcupdatecount;
                                        req.GeocacheLogCount     = 5;
                                        index += req.CacheCode.CacheCodes.Length;
                                        gcList.RemoveRange(0, req.CacheCode.CacheCodes.Length);
                                        prevCall = DateTime.Now;
                                        var resp = client.Client.SearchForGeocaches(req);
                                        if (resp.Status.StatusCode == 0 && resp.Geocaches != null)
                                        {
                                            Import.AddGeocaches(_core, resp.Geocaches);
                                        }
                                        else
                                        {
                                            _errormessage = resp.Status.StatusMessage;
                                            break;
                                        }
                                        if (!progress.UpdateProgress("Bijwerken van status en nieuwe geocaches", "Importeren van geocaches...", total, index))
                                        {
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
            _errormessage = e.Message;
        }
        _actionReady.Set();
    }
Example #8
0
        private bool LoadDatabaseFile()
        {
            //index file not available
            //create one
            //this is an exception. (should be anyway)
            //first create it in a temporary file and copy to target if finished
            bool result    = false;
            bool cancelled = false;

            try
            {
                string fn = string.Concat(FileName, ".gsx");
                if (File.Exists(fn))
                {
                    File.Delete(fn);
                }
                byte[] buffer = new byte[117];
                using (TemporaryFile tf = new TemporaryFile())
                    using (FileStream fsIdx = File.Open(tf.Path, FileMode.OpenOrCreate, FileAccess.Write))
                        using (MemoryStream ms = new MemoryStream(buffer))
                            using (BinaryWriter bw = new BinaryWriter(ms))
                            {
                                long     max        = this.FileStream.Length;
                                DateTime nextUpdate = DateTime.Now.AddSeconds(1);
                                using (Utils.ProgressBlock prog = new ProgressBlock("LoadingDatabase", "Loading", 100, 0, true))
                                {
                                    this.FileStream.Position = DATABASE_CONTENT_OFFSET;
                                    long eof = this.FileStream.Length;
                                    while (this.FileStream.Position < eof)
                                    {
                                        RecordInfo ri = new RecordInfo();
                                        ri.Database = this;
                                        ri.Offset   = this.FileStream.Position;
                                        this.FileStream.Position = ri.Offset + RECORD_POS_LENGTH;
                                        ri.Length = BinaryReader.ReadInt64();
                                        this.FileStream.Position = ri.Offset + RECORD_POS_FIELDTYPE;
                                        ri.FieldType             = BinaryReader.ReadByte();
                                        if (ri.FieldType == RECORD_EMPTY)
                                        {
                                            _emptyRecords.Add(ri);
                                            _emptyRecordListSorted = false;
                                        }
                                        else
                                        {
                                            this.FileStream.Position = ri.Offset + RECORD_POS_ID;
                                            ri.ID    = BinaryReader.ReadString();
                                            ri.SubID = BinaryReader.ReadString();
                                            switch (ri.FieldType)
                                            {
                                            case RECORD_GEOCACHE:
                                                this.GeocacheCollection.Add(new Data.Geocache(ri));
                                                break;

                                            case RECORD_LOG:
                                                this.LogCollection.Add(new Data.Log(ri));
                                                break;

                                            case RECORD_WAYPOINT:
                                                this.WaypointCollection.Add(new Data.Waypoint(ri));
                                                break;

                                            case RECORD_USERWAYPOINT:
                                                this.UserWaypointCollection.Add(new Data.UserWaypoint(ri));
                                                break;

                                            case RECORD_LOGIMAGE:
                                                this.LogImageCollection.Add(new Data.LogImage(ri));
                                                break;

                                            case RECORD_GEOCACHEIMAGE:
                                                this.GeocacheImageCollection.Add(new Data.GeocacheImage(ri));
                                                break;
                                            }
                                        }
                                        this.FileStream.Position = ri.Offset + ri.Length;

                                        ri.OffsetIdx = fsIdx.Position;
                                        ms.Position  = 0;
                                        bw.Write(ri.Offset);
                                        bw.Write(ri.Length);
                                        bw.Write(ri.FieldType);
                                        if (ri.FieldType != RECORD_EMPTY)
                                        {
                                            bw.Write(ri.ID);
                                            bw.Write(ri.SubID);
                                        }
                                        else
                                        {
                                            bw.Write("");
                                            bw.Write("");
                                        }
                                        fsIdx.Write(buffer, 0, 117);

                                        if (DateTime.Now >= nextUpdate)
                                        {
                                            if (!prog.Update("Loading", 100, (int)(100.0 * (double)this.FileStream.Position / (double)max)))
                                            {
                                                cancelled = true;
                                                break;
                                            }
                                            nextUpdate = DateTime.Now.AddSeconds(1);
                                        }
                                    }
                                    //if all OK and not canceled
                                    fsIdx.Close();
                                    if (!cancelled)
                                    {
                                        File.Copy(tf.Path, fn);
                                        _fileStreamIdx = File.Open(fn, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                                        Utils.Calculus.SetDistanceAndAngleGeocacheFromLocation(this.GeocacheCollection, ApplicationData.Instance.CenterLocation);
                                        result = true;
                                    }
                                }
                            }
            }
            catch (Exception e)
            {
                Core.ApplicationData.Instance.Logger.AddLog(this, e);
            }
            return(result);
        }
Example #9
0
        private bool LoadIndexFile(ref bool cancelled)
        {
            bool   result = false;
            string fn     = string.Concat(FileName, ".gsx");

            try
            {
                //check if file exists
                //if not, it will be created during loading of database file
                if (File.Exists(fn))
                {
                    _fileStreamIdx = File.Open(fn, FileMode.OpenOrCreate, FileAccess.ReadWrite);

                    long     max        = _fileStreamIdx.Length;
                    DateTime nextUpdate = DateTime.Now.AddSeconds(1);
                    using (Utils.ProgressBlock prog = new ProgressBlock("LoadingDatabase", "Loading", 100, 0, true))
                    {
                        int    maxChunks = 1000;
                        int    chunkSize = 117;
                        byte[] buffer    = new byte[maxChunks * chunkSize];
                        using (MemoryStream ms = new MemoryStream(buffer))
                            using (BinaryReader br = new BinaryReader(ms))
                            {
                                while (_fileStreamIdx.Position < max)
                                {
                                    long startPos   = _fileStreamIdx.Position;
                                    int  chunksRead = _fileStreamIdx.Read(buffer, 0, maxChunks * chunkSize) / chunkSize;
                                    for (int i = 0; i < chunksRead; i++)
                                    {
                                        RecordInfo ri = new RecordInfo();
                                        ri.Database  = this;
                                        ri.OffsetIdx = startPos + i * chunkSize;
                                        ms.Position  = i * chunkSize;
                                        ri.Offset    = br.ReadInt64();
                                        ri.Length    = br.ReadInt64();
                                        ri.FieldType = br.ReadByte();
                                        if (ri.FieldType != RECORD_EMPTY)
                                        {
                                            ri.ID    = br.ReadString();
                                            ri.SubID = br.ReadString();
                                        }
                                        switch (ri.FieldType)
                                        {
                                        case RECORD_EMPTY:
                                            //empty
                                            _emptyRecords.Add(ri);
                                            _emptyRecordListSorted = false;
                                            break;

                                        case RECORD_GEOCACHE:
                                            this.GeocacheCollection.Add(new Data.Geocache(ri));
                                            break;

                                        case RECORD_LOG:
                                            this.LogCollection.Add(new Data.Log(ri));
                                            break;

                                        case RECORD_WAYPOINT:
                                            this.WaypointCollection.Add(new Data.Waypoint(ri));
                                            break;

                                        case RECORD_USERWAYPOINT:
                                            this.UserWaypointCollection.Add(new Data.UserWaypoint(ri));
                                            break;

                                        case RECORD_LOGIMAGE:
                                            this.LogImageCollection.Add(new Data.LogImage(ri));
                                            break;

                                        case RECORD_GEOCACHEIMAGE:
                                            this.GeocacheImageCollection.Add(new Data.GeocacheImage(ri));
                                            break;
                                        }

                                        if (DateTime.Now >= nextUpdate)
                                        {
                                            if (!prog.Update("Loading", 100, (int)(100.0 * (double)_fileStreamIdx.Position / (double)max)))
                                            {
                                                cancelled = true;
                                                break;
                                            }
                                            nextUpdate = DateTime.Now.AddSeconds(1);
                                        }
                                    }
                                }
                                if (!cancelled)
                                {
                                    Utils.Calculus.SetDistanceAndAngleGeocacheFromLocation(GeocacheCollection, Core.ApplicationData.Instance.CenterLocation);
                                    result = true;
                                }
                            }
                    }
                }
            }
            catch (Exception e)
            {
                Core.ApplicationData.Instance.Logger.AddLog(this, e);
                try
                {
                    if (_fileStreamIdx != null)
                    {
                        _fileStreamIdx.Dispose();
                        _fileStreamIdx = null;
                    }
                    File.Delete(fn);
                }
                catch
                {
                }
            }
            return(result);
        }
Example #10
0
 public void Backup()
 {
     try
     {
         //file.bak01, file.bak02... file.bakNN where NN is the latest
         string fn = string.Format("{0}.bak{1}", FileName, Core.Settings.Default.DatabaseBackupMaxBackups.ToString("00"));
         if (File.Exists(fn))
         {
             //ok, maximum reached
             //delete the oldest and rename the others
             fn = string.Format("{0}.bak{1}", FileName, 1.ToString("00"));
             if (File.Exists(fn))
             {
                 File.Delete(fn);
             }
             for (int i = 1; i < Core.Settings.Default.DatabaseBackupMaxBackups; i++)
             {
                 string fns = string.Format("{0}.bak{1}", FileName, (i + 1).ToString("00"));
                 string fnd = string.Format("{0}.bak{1}", FileName, i.ToString("00"));
                 if (File.Exists(fns))
                 {
                     File.Move(fns, fnd);
                 }
             }
             fn = string.Format("{0}.bak{1}", FileName, Core.Settings.Default.DatabaseBackupMaxBackups.ToString("00"));
         }
         else
         {
             //look for latest
             int i = 1;
             fn = string.Format("{0}.bak{1}", FileName, i.ToString("00"));
             while (File.Exists(fn))
             {
                 i++;
                 fn = string.Format("{0}.bak{1}", FileName, i.ToString("00"));
             }
         }
         DateTime nextUpdate = DateTime.Now.AddSeconds(1);
         using (Utils.ProgressBlock prog = new ProgressBlock("CreatingBackup", "CreatingBackup", 100, 0))
         {
             using (System.IO.FileStream fs = File.OpenWrite(fn))
             {
                 int    read;
                 byte[] buffer = new byte[10 * 1024 * 1024];
                 fs.SetLength(this.FileStream.Length);
                 this.FileStream.Position = 0;
                 while (this.FileStream.Position < this.FileStream.Length)
                 {
                     read = this.FileStream.Read(buffer, 0, buffer.Length);
                     fs.Write(buffer, 0, read);
                     if (DateTime.Now >= nextUpdate)
                     {
                         prog.Update("Loading", 100, (int)(100.0 * (double)this.FileStream.Position / (double)this.FileStream.Length));
                         nextUpdate = DateTime.Now.AddSeconds(1);
                     }
                 }
             }
             MostRecentBackupDate     = File.GetCreationTime(fn);
             this.FileStream.Position = 0;
         }
     }
     catch (Exception e)
     {
         Core.ApplicationData.Instance.Logger.AddLog(this, e);
     }
 }
Example #11
0
        private static bool Import(Database database, string filename)
        {
            try
            {
                using (Utils.ProgressBlock allprog = new ProgressBlock("ImportGSAKDatabase", "Importing", 6, 0))
                {
                    System.Collections.Hashtable logTypes = new System.Collections.Hashtable();
                    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);

                        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 index   = 0;
                        int gcCount = (int)(long)import.ExecuteScalar();
                        if (gcCount > 0)
                        {
                            DateTime progShow = DateTime.Now.AddSeconds(1);
                            using (Utils.ProgressBlock prog = new ProgressBlock("ImportingGeocaches", 1, 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())
                                {
                                    GeocacheData gc = new GeocacheData();
                                    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(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(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;
                                    }
                                    bool customCoords = (int)dr["HasCorrected"] != 0;
                                    if (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.PersonalNote = (string)dr["gcnote"];
                                    }
                                    else
                                    {
                                        gc.PersonalNote = "";
                                    }
                                    gc.Flagged = (int)dr["UserFlag"] != 0;
                                    gc.Found   = (int)dr["Found"] != 0;

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

                                    DataAccess.AddGeocache(database, gc);

                                    index++;
                                    if (DateTime.Now >= progShow)
                                    {
                                        prog.Update("ImportingGeocaches", gcCount, index);
                                        progShow = DateTime.Now.AddSeconds(1);
                                    }
                                }
                                dr.Close();
                            }
                        }
                        allprog.Update("Importing", 5, 1);

                        import.CommandText = "select count(1) from logs";
                        int logCount = (int)(long)import.ExecuteScalar();
                        if (logCount > 0)
                        {
                            DateTime progShow = DateTime.Now.AddSeconds(1);
                            using (Utils.ProgressBlock progress = new Utils.ProgressBlock("ImportingLogs", logCount, 0))
                            {
                                index = 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())
                                {
                                    Core.Data.LogData lg = new Core.Data.LogData();

                                    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(logType);

                                    DataAccess.AddLog(database, lg);

                                    index++;
                                    if (DateTime.Now >= progShow)
                                    {
                                        progress.Update("ImportingLogs", logCount, index);
                                        progShow = DateTime.Now.AddSeconds(1);
                                    }
                                }
                                dr.Close();
                            }
                        }
                        allprog.Update("Importing", 5, 2);

                        import.CommandText = "select count(1) from logimages";
                        int logimgCount = 0;
                        try
                        {
                            logimgCount = (int)(long)import.ExecuteScalar();
                        }
                        catch
                        {
                            //table does not exists
                        }
                        if (logimgCount > 0)
                        {
                            DateTime progShow = DateTime.Now.AddSeconds(1);
                            using (Utils.ProgressBlock progress = new Utils.ProgressBlock("ImportingLogImages", logimgCount, 0))
                            {
                                index = 0;
                                import.CommandText = "select iCode, iLogId, iImage, iName from logimages";
                                DbDataReader dr = import.ExecuteReader();
                                while (dr.Read())
                                {
                                    Core.Data.LogImageData lg = new Core.Data.LogImageData();

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

                                    DataAccess.AddLogImage(database, lg);

                                    index++;
                                    if (DateTime.Now >= progShow)
                                    {
                                        progress.Update("ImportingLogImages", logimgCount, index);
                                        progShow = DateTime.Now.AddSeconds(1);
                                    }
                                }
                                dr.Close();
                            }
                        }
                        allprog.Update("Importing", 5, 3);

                        //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)
                        {
                            DateTime progShow = DateTime.Now.AddSeconds(1);
                            using (Utils.ProgressBlock progress = new Utils.ProgressBlock("ImportingWaypoints", wptCount, 0))
                            {
                                index = 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())
                                {
                                    Core.Data.WaypointData wp = new Core.Data.WaypointData();

                                    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(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;

                                    DataAccess.AddWaypoint(database, wp);

                                    index++;
                                    if (DateTime.Now >= progShow)
                                    {
                                        progress.Update("ImportingWaypoints", wptCount, index);
                                        progShow = DateTime.Now.AddSeconds(1);
                                    }
                                }
                                dr.Close();
                            }
                        }
                        allprog.Update("Importing", 5, 4);

                        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 ?? "";
                                Core.Data.Geocache gc     = database.GeocacheCollection.GetGeocache(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
                        {
                        }
                        allprog.Update("Importing", 5, 5);
                    }
                }
            }
            catch (Exception e)
            {
                Core.ApplicationData.Instance.Logger.AddLog(new Importer(), e);
            }
            return(true);
        }
Example #12
0
        public async Task DownloadImagesAsync(List <Core.Data.Geocache> gcList, bool updateExisting)
        {
            await Task.Run(async() =>
            {
                bool cancel = false;
                ConcurrentQueue <Core.Data.Geocache> cq = new ConcurrentQueue <Core.Data.Geocache>();
                foreach (var gc in gcList)
                {
                    cq.Enqueue(gc);
                }

                using (Utils.ProgressBlock prog = new ProgressBlock("DownloadingImages", "DownloadingImages", gcList.Count, 0, true))
                {
                    Action actionUpdateProgress = () =>
                    {
                        DateTime updateAt = DateTime.MinValue;
                        int cnt           = cq.Count;
                        while (cnt > 0)
                        {
                            if (DateTime.Now >= updateAt)
                            {
                                if (!prog.Update("DownloadingImages", gcList.Count, gcList.Count - cnt))
                                {
                                    cancel = true;
                                    break;
                                }
                                updateAt = DateTime.Now.AddSeconds(1);
                            }
                            System.Threading.Thread.Sleep(200);
                            cnt = cq.Count;
                        }
                        ;
                    };

                    Action actionDownload = () =>
                    {
                        Core.Data.Geocache gc;
                        using (System.Net.WebClient wc = new System.Net.WebClient())
                        {
                            while (!cancel && cq.TryDequeue(out gc))
                            {
                                string fnp       = System.IO.Path.Combine(_imageFolder, IMG_SUBFOLDER);
                                bool grabOnlyNew = !updateExisting;
                                try
                                {
                                    List <string> linkList;
                                    lock (_lockObject)
                                    {
                                        linkList = DataAccess.GetImagesOfGeocache(gc.Database, gc.Code);
                                    }

                                    foreach (string link in linkList)
                                    {
                                        string fn       = string.Format("{0}.jpg", Guid.NewGuid().ToString("N"));
                                        bool skipInsert = false;
                                        //if it fails, just ignore this image
                                        try
                                        {
                                            //check if link already is in database
                                            //if so, use this filename
                                            lock (_lockObject)
                                            {
                                                object o = _dbcon.ExecuteScalar(string.Format("select local_file from images where gccode='{0}' and org_url='{1}'", gc.Code.Replace("'", "''"), link.Replace("'", "''")));
                                                if (o != null && o.GetType() != typeof(DBNull))
                                                {
                                                    fn         = (string)o;
                                                    skipInsert = true;
                                                }
                                            }
                                            if (grabOnlyNew && skipInsert)
                                            {
                                                if (System.IO.File.Exists(System.IO.Path.Combine(fnp, fn)))
                                                {
                                                    continue;
                                                }
                                            }
                                            using (System.IO.TemporaryFile tmp = new System.IO.TemporaryFile(true))
                                            {
                                                wc.DownloadFile(link, tmp.Path);
                                                if (new FileInfo(tmp.Path).Length < 10 * 1024 * 1024)
                                                {
                                                    using (System.Drawing.Image img = System.Drawing.Image.FromFile(tmp.Path))
                                                    {
                                                        img.Save(System.IO.Path.Combine(fnp, fn), System.Drawing.Imaging.ImageFormat.Jpeg);
                                                        if (!skipInsert)
                                                        {
                                                            lock (_lockObject)
                                                            {
                                                                _dbcon.ExecuteNonQuery(string.Format("insert into images (gccode, org_url, local_file) values ('{0}', '{1}', '{2}')", gc.Code.Replace("'", "''"), link.Replace("'", "''"), fn));
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        catch //(Exception e)
                                        {
                                            //Core.ApplicationData.Instance.Logger.AddLog(this, e);
                                        }
                                    }
                                }
                                catch (Exception e)
                                {
                                    Core.ApplicationData.Instance.Logger.AddLog(this, e);
                                    //skip and go to next one
                                }
                            }
                        };
                    };

                    List <Task> tasks = new List <Task>();
                    tasks.Add(Task.Factory.StartNew(actionUpdateProgress));
                    for (int i = 0; i < 4 && i < gcList.Count; i++)
                    {
                        tasks.Add(Task.Factory.StartNew(actionDownload));
                    }
                    await Task.WhenAll(tasks.ToArray());
                }
            });
        }
    public static bool Run(Plugin plugin, ICore core)
    {
        int[,] DTMatrix = new int[9, 9];
        int numMissing = 9 * 9;
        int count      = 0;
        int prog       = 0;

        List <Geocache> gcList = null;

        gcList = DataAccess.GetFoundGeocaches(core.Geocaches, core.Logs, core.GeocachingComAccount);

        using (ProgressBlock progress = new ProgressBlock(plugin,
                                                          "Creating S/T Matrix", "Seeking for Neo", gcList.Count, 0))
        {
            foreach (Geocache gc in gcList)
            {
                int ixD = (int)(gc.Difficulty * 2 + 0.001) - 2;
                int ixT = (int)(gc.Terrain * 2 + 0.001) - 2;
                //range checking is for cowards!
                if (DTMatrix[ixD, ixT] == 0)
                {
                    numMissing--;
                }
                DTMatrix[ixD, ixT]++;

                prog++;
                if (prog % 10 == 0)
                {
                    progress.UpdateProgress("Creating S/T Matrix", "Seeking for Neo", gcList.Count, prog);
                }
            }
        }

        prog = 0;
        using (ProgressBlock progress = new ProgressBlock(plugin,
                                                          "Selecting Caches", "", core.Geocaches.Count, 0))
        {
            //reset current selection
            foreach (Geocache gc in core.Geocaches)
            {
                int ixD = (int)(gc.Difficulty * 2 + 0.001) - 2;
                int ixT = (int)(gc.Terrain * 2 + 0.001) - 2;

                gc.Selected = (DTMatrix[ixD, ixT] == 0);
                if (DTMatrix[ixD, ixT] == 0)
                {
                    count++;
                }
                prog++;
                if (prog % 50 == 0)
                {
                    progress.UpdateProgress("Selecting Caches", "", core.Geocaches.Count, prog);
                }
            }
        }

        System.Windows.Forms.MessageBox.Show("Selected: " + count.ToString() +
                                             "; Missing combinations in D/T Matrix: " + numMissing.ToString() +
                                             " of " + gcList.Count.ToString() + " found Geocaches.");

        return(true);
    }
    public static bool Run(Plugin plugin, ICore core)
    {
        string baseSite    = "http://xylanthrop.de/";
        string promptValue = MyURLPrompt.ShowDialog(baseSite, "2 Select a count(r)y to update the archived caches:", "Update Archived Caches from xylanthrop.de");

//        System.Windows.Forms.MessageBox.Show("promptValue: "+promptValue);
        if (promptValue != "")
        {
            //get page
            using (System.Net.WebClient wc = new System.Net.WebClient()) {
                string html = wc.DownloadString(baseSite + promptValue);
                int    prog;
                int    numgc  = 0;
                int    updc   = 0;
                int    foundc = 0;
                int    nonarc = 0;//counter for caches "not" archived
                //parse GC-Codes from links
                Regex           rgc = new Regex(@"<waypoint>(GC[A-Z0-9]*)<.*?<archived>([^<]*)<", RegexOptions.Singleline);
                MatchCollection mc  = rgc.Matches(html);
                StringBuilder   gcs = new StringBuilder();

                prog = 0;
                using (ProgressBlock progress = new ProgressBlock(plugin,
                                                                  "Retrieving GC-Codes", "", mc.Count, 0))
                {
                    foreach (Match m in mc)
                    {
                        if (m.Groups[2].Value == "archived")
                        {
                            gcs.Append(m.Groups[1].Value);
                            gcs.Append(" ");
                        }
                        else
                        {
                            nonarc++;
                        }
                        prog++;
                        if (prog % 10 == 0)
                        {
                            progress.UpdateProgress("Retrieving GC-Codes", "", mc.Count, prog);
                        }
                    }
                    numgc = prog;
                }

                if (numgc > 0)
                {
                    string gcstr = gcs.ToString();//searchable string of gc-codes
                    prog = 0;
                    core.Geocaches.BeginUpdate();

                    using (ProgressBlock progress = new ProgressBlock(plugin,
                                                                      "Updating Caches", "", core.Geocaches.Count, 0))
                    {
                        foreach (Geocache gc in core.Geocaches)
                        {
                            if (gc.Code != "")
                            {
                                if (gcstr.IndexOf(gc.Code + " ") >= 0)
                                {
                                    foundc++;
                                    if (!gc.Archived)
                                    {
                                        updc++;
                                        gc.Archived  = true;//Set Cache to Archived state
                                        gc.Available = false;
                                    }
                                }
                            }
                            prog++;
                            if (prog % 100 == 0)
                            {
                                progress.UpdateProgress("Updating Caches", String.Format("Found: {0}", updc), core.Geocaches.Count, prog);
                            }
                        }
                    }
                    core.Geocaches.EndUpdate();
                }
                System.Windows.Forms.MessageBox.Show(
                    "Update completed:\r" +
                    "Source URL: " + promptValue + "\r" +
                    String.Format("#Caches on page: {0}\r", numgc) +
                    String.Format("#Caches found in DB: {0}\r", foundc) +
                    String.Format("#Caches updated: {0}\r", updc) +
                    String.Format("#Caches non archived in src: {0}", nonarc));
                //System.Windows.Forms.MessageBox.Show("Val: "+html);
            }
        }
        //System.Windows.Forms.MessageBox.Show("Val: "+promptValue);
        return(true);
    }
Example #15
0
        private static bool Import(Database database, string filename)
        {
            bool result = false;

            try
            {
                byte[] memBuffer = new byte[10 * 1024 * 1024];

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

                XmlDocument doc = new XmlDocument();
                doc.Load(filename);
                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.Now.AddSeconds(1);

                using (Utils.ProgressBlock prog = new ProgressBlock("ImportingDatabase", "Importing", 6, 0))
                {
                    List <RecordInfo> records   = new List <RecordInfo>();
                    Hashtable         f_records = new Hashtable();

                    using (Utils.ProgressBlock subProg = new ProgressBlock("ImportingGeocaches", gcCount, 0))
                    {
                        //GEOCACHES
                        //first all record
                        using (FileStream fs = File.Open(Path.Combine(Path.GetDirectoryName(filename), string.Format("{0}.cch", Path.GetFileNameWithoutExtension(filename))), FileMode.OpenOrCreate, FileAccess.Read))
                            using (BinaryReader br = new BinaryReader(fs))
                            {
                                fs.Position = 0;
                                long eof = fs.Length;
                                while (fs.Position < eof)
                                {
                                    RecordInfo ri = new RecordInfo();
                                    ri.Offset = fs.Position;
                                    ri.Length = br.ReadInt64();
                                    byte slotType = br.ReadByte();
                                    if (slotType == 0)
                                    {
                                        //free
                                    }
                                    else
                                    {
                                        //read
                                        ri.ID = br.ReadString();
                                        if (slotType == 1)
                                        {
                                            records.Add(ri);
                                        }
                                        else
                                        {
                                            f_records.Add(ri.ID, ri);
                                        }
                                    }
                                    fs.Position = ri.Offset + ri.Length;
                                }
                                foreach (RecordInfo ri in records)
                                {
                                    GeocacheData gc = new GeocacheData();
                                    gc.Code = ri.ID;

                                    fs.Position = ri.Offset + 9;

                                    string dummyString = br.ReadString(); //id
                                    gc.Archived     = br.ReadBoolean();
                                    gc.AttributeIds = ReadIntegerArray(br);
                                    gc.Available    = br.ReadBoolean();
                                    gc.City         = br.ReadString();
                                    gc.Container    = Utils.DataAccess.GetGeocacheContainer(br.ReadInt32());
                                    bool dummyBool = 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(br.ReadInt32());
                                    dummyString      = 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.PersonalNote  = br.ReadString();
                                    gc.PlacedBy      = br.ReadString();
                                    gc.PublishedTime = DateTime.Parse(br.ReadString());
                                    gc.State         = br.ReadString();
                                    gc.Terrain       = br.ReadDouble();
                                    gc.Name          = br.ReadString();
                                    gc.Url           = br.ReadString();
                                    gc.DataFromDate  = DateTime.Parse(br.ReadString());
                                    gc.Locked        = br.ReadBoolean();

                                    RecordInfo rf = f_records[string.Format("F_{0}", ri.ID)] as RecordInfo;
                                    if (rf != null)
                                    {
                                        fs.Position = rf.Offset + 9;

                                        br.ReadString(); //id
                                        gc.ShortDescription       = br.ReadString();
                                        gc.ShortDescriptionInHtml = br.ReadBoolean();
                                        gc.LongDescription        = br.ReadString();
                                        gc.LongDescriptionInHtml  = br.ReadBoolean();
                                    }
                                    DataAccess.AddGeocache(database, gc);
                                    index++;
                                    if (DateTime.Now >= nextUpdateTime)
                                    {
                                        subProg.Update("ImportingGeocaches", gcCount, index);
                                        nextUpdateTime = DateTime.Now.AddSeconds(1);
                                    }
                                }
                            }
                    }
                    prog.Update("Importing...", 6, 1);

                    records.Clear();
                    f_records.Clear();

                    using (Utils.ProgressBlock subProg = new ProgressBlock("ImportingLogs", logCount, 0))
                    {
                        index = 0;
                        //LOGS
                        //first all record
                        using (FileStream fs = File.Open(Path.Combine(Path.GetDirectoryName(filename), string.Format("{0}.lgs", Path.GetFileNameWithoutExtension(filename))), FileMode.OpenOrCreate, FileAccess.Read))
                            using (BinaryReader br = new BinaryReader(fs))
                            {
                                fs.Position = 0;
                                long eof = fs.Length;
                                while (fs.Position < eof)
                                {
                                    RecordInfo ri = new RecordInfo();
                                    ri.Offset = fs.Position;
                                    ri.Length = br.ReadInt64();
                                    byte slotType = br.ReadByte();
                                    if (slotType == 0)
                                    {
                                        //free
                                    }
                                    else
                                    {
                                        //read
                                        ri.ID = br.ReadString();
                                        if (slotType == 1)
                                        {
                                            records.Add(ri);
                                        }
                                        else
                                        {
                                            f_records.Add(ri.ID, ri);
                                        }
                                    }
                                    fs.Position = ri.Offset + ri.Length;
                                }
                                foreach (RecordInfo ri in records)
                                {
                                    LogData gc = new LogData();
                                    gc.ID = ri.ID;

                                    fs.Position = ri.Offset + 9;
                                    string dummyString = br.ReadString(); //id
                                    gc.DataFromDate = DateTime.Parse(br.ReadString());
                                    gc.Date         = DateTime.Parse(br.ReadString());
                                    gc.Finder       = br.ReadString();
                                    gc.GeocacheCode = br.ReadString();
                                    gc.ID           = br.ReadString();
                                    gc.LogType      = Utils.DataAccess.GetLogType(br.ReadInt32());

                                    RecordInfo rf = f_records[string.Format("F_{0}", ri.ID)] as RecordInfo;
                                    if (rf != null)
                                    {
                                        fs.Position = rf.Offset + 9;

                                        br.ReadString(); //id
                                        gc.TBCode   = br.ReadString();
                                        gc.FinderId = br.ReadString();
                                        gc.Text     = br.ReadString();
                                        gc.Encoded  = br.ReadBoolean();
                                    }
                                    DataAccess.AddLog(database, gc);
                                    index++;
                                    if (DateTime.Now >= nextUpdateTime)
                                    {
                                        subProg.Update("ImportingLogs", logCount, index);
                                        nextUpdateTime = DateTime.Now.AddSeconds(1);
                                    }
                                }
                            }
                    }
                    prog.Update("Importing", 6, 2);

                    records.Clear();
                    f_records.Clear();

                    using (Utils.ProgressBlock subProg = new ProgressBlock("ImportingWaypoints", wptCount, 0))
                    {
                        index = 0;
                        //Waypoints
                        using (FileStream fs = File.Open(Path.Combine(Path.GetDirectoryName(filename), string.Format("{0}.wpt", Path.GetFileNameWithoutExtension(filename))), FileMode.OpenOrCreate, FileAccess.Read))
                            using (BinaryReader br = new BinaryReader(fs))
                            {
                                fs.Position = 0;
                                long       eof = fs.Length;
                                RecordInfo ri  = new RecordInfo();
                                while (fs.Position < eof)
                                {
                                    ri.Offset = fs.Position;
                                    ri.Length = br.ReadInt64();
                                    byte slotType = br.ReadByte();
                                    if (slotType == 0)
                                    {
                                        //free
                                    }
                                    else
                                    {
                                        //read
                                        Core.Data.WaypointData wp = new Core.Data.WaypointData();

                                        wp.Code         = br.ReadString();
                                        wp.Comment      = br.ReadString();
                                        wp.DataFromDate = DateTime.Parse(br.ReadString());
                                        wp.Description  = br.ReadString();
                                        wp.GeocacheCode = br.ReadString();
                                        wp.ID           = br.ReadString();
                                        if (br.ReadBoolean())
                                        {
                                            wp.Lat = br.ReadDouble();
                                            wp.Lon = br.ReadDouble();
                                        }
                                        wp.Name    = br.ReadString();
                                        wp.Time    = DateTime.Parse(br.ReadString());
                                        wp.Url     = br.ReadString();
                                        wp.UrlName = br.ReadString();
                                        wp.WPType  = Utils.DataAccess.GetWaypointType(br.ReadInt32());

                                        DataAccess.AddWaypoint(database, wp);
                                        index++;
                                        if (DateTime.Now >= nextUpdateTime)
                                        {
                                            subProg.Update("ImportingWaypoints", wptCount, index);
                                            nextUpdateTime = DateTime.Now.AddSeconds(1);
                                        }
                                    }
                                    fs.Position = ri.Offset + ri.Length;
                                }
                            }
                    }
                    prog.Update("Importing", 6, 3);

                    records.Clear();
                    f_records.Clear();

                    using (Utils.ProgressBlock subProg = new ProgressBlock("ImportingLogImages", logimgCount, 0))
                    {
                        index = 0;
                        using (FileStream fs = File.Open(Path.Combine(Path.GetDirectoryName(filename), string.Format("{0}.lmg", Path.GetFileNameWithoutExtension(filename))), FileMode.OpenOrCreate, FileAccess.Read))
                            using (BinaryReader br = new BinaryReader(fs))
                            {
                                fs.Position = 0;
                                long       eof = fs.Length;
                                RecordInfo ri  = new RecordInfo();
                                while (fs.Position < eof)
                                {
                                    ri.Offset = fs.Position;
                                    ri.Length = br.ReadInt64();
                                    byte slotType = br.ReadByte();
                                    if (slotType == 0)
                                    {
                                        //free
                                    }
                                    else
                                    {
                                        //read
                                        Core.Data.LogImageData li = new Core.Data.LogImageData();

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

                                        DataAccess.AddLogImage(database, li);
                                        index++;
                                        if (DateTime.Now >= nextUpdateTime)
                                        {
                                            subProg.Update("ImportingLogImages", logimgCount, index);
                                            nextUpdateTime = DateTime.Now.AddSeconds(1);
                                        }
                                    }
                                    fs.Position = ri.Offset + ri.Length;
                                }
                            }
                    }
                    prog.Update("Importing...", 6, 4);

                    records.Clear();
                    f_records.Clear();

                    using (Utils.ProgressBlock subProg = new ProgressBlock("ImportingGeocacheImages", geocacheimgCount, 0))
                    {
                        index = 0;
                        using (FileStream fs = File.Open(Path.Combine(Path.GetDirectoryName(filename), string.Format("{0}.gmg", Path.GetFileNameWithoutExtension(filename))), FileMode.OpenOrCreate, FileAccess.Read))
                            using (BinaryReader br = new BinaryReader(fs))
                            {
                                fs.Position = 0;
                                long       eof = fs.Length;
                                RecordInfo ri  = new RecordInfo();
                                while (fs.Position < eof)
                                {
                                    ri.Offset = fs.Position;
                                    ri.Length = br.ReadInt64();
                                    byte slotType = br.ReadByte();
                                    if (slotType == 0)
                                    {
                                        //free
                                    }
                                    else
                                    {
                                        //read
                                        Core.Data.GeocacheImageData li = new Core.Data.GeocacheImageData();

                                        li.ID           = br.ReadString();
                                        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();

                                        DataAccess.AddGeocacheImage(database, li);
                                        index++;
                                        if (DateTime.Now >= nextUpdateTime)
                                        {
                                            subProg.Update("ImportingGeocacheImages", geocacheimgCount, index);
                                            nextUpdateTime = DateTime.Now.AddSeconds(1);
                                        }
                                    }
                                    fs.Position = ri.Offset + ri.Length;
                                }
                            }
                    }
                    prog.Update("Importing", 6, 5);

                    records.Clear();
                    f_records.Clear();

                    using (Utils.ProgressBlock subProg = new ProgressBlock("ImportingUserWaypoints", usrwptCount, 0))
                    {
                        index = 0;
                        using (FileStream fs = File.Open(Path.Combine(Path.GetDirectoryName(filename), string.Format("{0}.uwp", Path.GetFileNameWithoutExtension(filename))), FileMode.OpenOrCreate, FileAccess.Read))
                            using (BinaryReader br = new BinaryReader(fs))
                            {
                                fs.Position = 0;
                                long       eof = fs.Length;
                                RecordInfo ri  = new RecordInfo();
                                while (fs.Position < eof)
                                {
                                    ri.Offset = fs.Position;
                                    ri.Length = br.ReadInt64();
                                    byte slotType = br.ReadByte();
                                    if (slotType == 0)
                                    {
                                        //free
                                    }
                                    else
                                    {
                                        //read
                                        Core.Data.UserWaypointData wp = new Core.Data.UserWaypointData();

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

                                        DataAccess.AddUserWaypoint(database, wp);
                                        index++;
                                        if (DateTime.Now >= nextUpdateTime)
                                        {
                                            subProg.Update("ImportingUserWaypoints", usrwptCount, index);
                                            nextUpdateTime = DateTime.Now.AddSeconds(1);
                                        }
                                    }
                                    fs.Position = ri.Offset + ri.Length;
                                }
                            }
                    }
                    prog.Update("Importing", 6, 6);

                    result = true;
                }
            }
            catch (Exception e)
            {
                Core.ApplicationData.Instance.Logger.AddLog(new Importer(), e);
            }
            return(result);
        }
Example #16
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);
        }