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