Пример #1
0
 public static bool AddGeocache(Database db, GeocacheData gd)
 {
     bool result = true;
     if (!Core.Settings.Default.GeocacheIgnored(gd))
     {
         Geocache gc = db.GeocacheCollection.GetGeocache(gd.Code);
         if (gc == null)
         {
             gc = new Geocache(db, gd);
             gc.Selected = Core.Settings.Default.AutoSelectNewGeocaches;
         }
         else
         {
             if (gc.DataFromDate < gd.DataFromDate)
             {
                 gc.BeginUpdate();
                 GeocacheData.Copy(gd, gc);
                 gc.EndUpdate();
             }
         }
         Utils.Calculus.SetDistanceAndAngleGeocacheFromLocation(gc, ApplicationData.Instance.CenterLocation);
     }
     else
     {
         result = false;
     }
     return result;
 }
Пример #2
0
        private void checkDescriptionsFit(string sd, string ld)
        {
            string s = string.Concat(sd, ld);

            if (!checkStringFits(s, 3010)) //add bytes for extra string length of long description
            {
                //oeps: ran out of space
                //mark this record for deletion
                Storage.Database db = RecordInfo.Database;
                this.DeleteRecord();
                GeocacheData gd = new GeocacheData();
                GeocacheData.Copy(this, gd);
                gd.ShortDescription = sd ?? "";
                gd.LongDescription  = ld ?? "";
                createRecord(db, gd);
            }
        }
Пример #3
0
 public static bool AddGeocache(Database db, GeocacheData gd)
 {
     bool result = true;
     Geocache gc = db.GeocacheCollection.GetGeocache(gd.Code);
     if (gc == null)
     {
         gc = new Geocache(db, gd);
     }
     else
     {
         if (gc.DataFromDate<gd.DataFromDate)
         {
             gc.BeginUpdate();
             GeocacheData.Copy(gd, gc);
             gc.EndUpdate();
         }
     }
     Utils.Calculus.SetDistanceAndAngleGeocacheFromLocation(gc, ApplicationData.Instance.CenterLocation);
     return result;
 }
Пример #4
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("Importing database", "Importing...", 6, 0))
                {
                    List<RecordInfo> records = new List<RecordInfo>();
                    Hashtable f_records = new Hashtable();

                    using (Utils.ProgressBlock subProg = new ProgressBlock("Importing geocaches...", 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("Importing geocaches...", gcCount, index);
                                    nextUpdateTime = DateTime.Now.AddSeconds(1);
                                }
                            }
                        }
                    }
                    prog.Update("Importing...", 6, 1);

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

                    using (Utils.ProgressBlock subProg = new ProgressBlock("Importing logs...", 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("Importing geocaches...", gcCount, index);
                                    nextUpdateTime = DateTime.Now.AddSeconds(1);
                                }
                            }
                        }
                    }
                    prog.Update("Importing...", 6, 2);

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

                    using (Utils.ProgressBlock subProg = new ProgressBlock("Importing waypoints...", logCount, 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("Importing waypoints...", gcCount, 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("Importing log images...", 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("Importing log images...", 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("Importing geocache images...", 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("Importing geocache images...", 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("Importing user waypoints...", 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("Importing user waypoints...", usrwptCount, index);
                                        nextUpdateTime = DateTime.Now.AddSeconds(1);
                                    }

                                }
                                fs.Position = ri.Offset + ri.Length;
                            }
                        }
                    }
                    prog.Update("Importing...", 6, 6);

                    result = true;
                }
            }
            catch
            {
            }
            return result;
        }
Пример #5
0
        private static bool Import(Database database, string filename)
        {
            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("Enable Listing", 23);
                logTypes.Add("Publish Listing", 24);
                logTypes.Add("Retract Listing", 25);
                logTypes.Add("Needs Maintenance", 45);
                logTypes.Add("Owner Maintenance", 46);
                logTypes.Add("Update Coordinates", 47);
                logTypes.Add("Post Reviewer Note", 68);
                logTypes.Add("Announcement", 74);

                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("Import GSAK database", "Importing...", 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, 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"];
                            gc.DataFromDate = DateTime.Parse((string)dr["LastGPXDate"]);
                            gc.Available = ((String)dr["Status"]).Equals("A");
                            gc.Archived = (int)dr["archived"] != 0;
                            gc.Country = (string)dr["country"];
                            gc.State = (string)dr["state"];

                            gc.GeocacheType = Utils.DataAccess.GetGeocacheType(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("Importing", gcCount, index);
                                progShow = DateTime.Now.AddSeconds(1);
                            }
                        }
                        dr.Close();

                    }
                }
            }
            return true;
        }
Пример #6
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("Enable Listing", 23);
                        logTypes.Add("Publish Listing", 24);
                        logTypes.Add("Retract Listing", 25);
                        logTypes.Add("Needs Maintenance", 45);
                        logTypes.Add("Owner Maintenance", 46);
                        logTypes.Add("Update Coordinates", 47);
                        logTypes.Add("Post Reviewer Note", 68);
                        logTypes.Add("Announcement", 74);

                        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, 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.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;
        }