Beispiel #1
0
 public static bool AddLog(Database db, LogData gd)
 {
     bool result = true;
     if (!Core.Settings.Default.GeocacheCodeIgnored(gd.GeocacheCode))
     {
         Log gc = db.LogCollection.GetLog(gd.ID);
         if (gc == null)
         {
             gc = new Log(db, gd);
         }
         else
         {
             if (gc.DataFromDate < gd.DataFromDate)
             {
                 gc.BeginUpdate();
                 LogData.Copy(gd, gc);
                 gc.EndUpdate();
             }
         }
     }
     else
     {
         result = false;
     }
     return result;
 }
Beispiel #2
0
 public static bool AddWaypoint(Database db, WaypointData gd)
 {
     bool result = true;
     if (!Core.Settings.Default.GeocacheCodeIgnored(gd.GeocacheCode))
     {
         Waypoint gc = db.WaypointCollection.GetWaypoint(gd.ID);
         if (gc == null)
         {
             gc = new Waypoint(db, gd);
         }
         else
         {
             if (gc.DataFromDate < gd.DataFromDate)
             {
                 gc.BeginUpdate();
                 WaypointData.Copy(gd, gc);
                 gc.EndUpdate();
             }
         }
     }
     else
     {
         result = false;
     }
     return result;
 }
Beispiel #3
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;
 }
Beispiel #4
0
 async public static Task<bool> PerformAction(Database database, string filename)
 {
     bool result = false;
     using (DataUpdater upd = new DataUpdater(database))
     {
         await Task.Run(() => { result = Import(database, filename); });
     }
     return result;
 }
Beispiel #5
0
        private void blockUpdates(Database db)
        {
            db.GeocacheCollection.BeginUpdate();
            db.LogCollection.BeginUpdate();
            db.WaypointCollection.BeginUpdate();
            db.UserWaypointCollection.BeginUpdate();
            db.LogImageCollection.BeginUpdate();
            db.GeocacheImageCollection.BeginUpdate();

            db.CheckAutoBackup();
        }
Beispiel #6
0
        private void unblockUpdates(Database db)
        {
            db.GeocacheImageCollection.EndUpdate();
            db.LogImageCollection.EndUpdate();
            db.UserWaypointCollection.EndUpdate();
            db.WaypointCollection.EndUpdate();
            db.LogCollection.EndUpdate();
            db.GeocacheCollection.EndUpdate();

            if (!_readOnly)
            {
                db.Flush();
            }
        }
Beispiel #7
0
 public static bool AddWaypoint(Database db, WaypointData gd)
 {
     bool result = true;
     Waypoint gc = db.WaypointCollection.GetWaypoint(gd.ID);
     if (gc == null)
     {
         gc = new Waypoint(db, gd);
     }
     else
     {
         if (gc.DataFromDate < gd.DataFromDate)
         {
             gc.BeginUpdate();
             WaypointData.Copy(gd, gc);
             gc.EndUpdate();
         }
     }
     return result;
 }
Beispiel #8
0
 public DataUpdater(Database db)
 {
     _dbList = new List<Database>();
     if (db != null)
     {
         _dbList.Add(db);
     }
     else
     {
         foreach (Database d in ApplicationData.Instance.Databases)
         {
             _dbList.Add(d);
         }
     }
     foreach (Database d in _dbList)
     {
         blockUpdates(d);
     }
 }
Beispiel #9
0
 public static bool AddLogImage(Database db,LogImageData gd)
 {
     bool result = true;
     LogImage gc = db.LogImageCollection.GetLogImage(gd.ID);
     if (gc == null)
     {
         gc = new LogImage(db, gd);
     }
     else
     {
         if (gc.DataFromDate < gd.DataFromDate)
         {
             gc.BeginUpdate();
             LogImageData.Copy(gd, gc);
             gc.EndUpdate();
         }
     }
     return result;
 }
Beispiel #10
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;
 }
Beispiel #11
0
 public static bool AddUserWaypoint(Database db, UserWaypointData gd)
 {
     bool result = true;
     UserWaypoint gc = db.UserWaypointCollection.GetUserWaypoint(gd.ID);
     if (gc == null)
     {
         gc = new UserWaypoint(db, gd);
     }
     else
     {
         gc.BeginUpdate();
         UserWaypointData.Copy(gd, gc);
         gc.EndUpdate();
     }
     return result;
 }
Beispiel #12
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;
        }
Beispiel #13
0
 public static void DeleteLog(Database db, Core.Data.Log lg)
 {
     List<Core.Data.LogImage> gcil = GetLogImages(db, lg.ID);
     foreach (var wp in gcil)
     {
         wp.DeleteRecord();
         db.LogImageCollection.Remove(wp);
     }
     lg.DeleteRecord();
     db.LogCollection.Remove(lg);
 }
Beispiel #14
0
        public static List<string> GetImagesOfGeocache(Database db, string geocacheCode, bool notInDescriptionOnly)
        {
            List<string> bresult = new List<string>();
            List<string> result;

            Core.Data.Geocache gc = db.GeocacheCollection.GetGeocache(geocacheCode);

            if (gc != null)
            {
                StringBuilder sb = new StringBuilder();
                if (gc.ShortDescriptionInHtml && gc.ShortDescription != null)
                {
                    sb.Append(gc.ShortDescription);
                }
                if (gc.LongDescriptionInHtml && gc.LongDescription != null)
                {
                    sb.Append(gc.LongDescription);
                }
                if (sb.Length > 0)
                {

                    Regex r = new Regex(@"</?\w+\s+[^>]*>", RegexOptions.Multiline);
                    MatchCollection mc = r.Matches(sb.ToString());
                    foreach (Match m in mc)
                    {
                        string s = m.Value.Substring(1).Replace('\r', ' ').Replace('\n', ' ').Trim();
                        if (s.StartsWith("img ", StringComparison.OrdinalIgnoreCase))
                        {
                            int pos = s.IndexOf(" src", StringComparison.OrdinalIgnoreCase);
                            if (pos >= 0)
                            {
                                pos = s.IndexOfAny(new char[] { '\'', '"' }, pos);
                                if (pos >= 0)
                                {
                                    int pos2 = s.IndexOfAny(new char[] { '\'', '"' }, pos + 1);
                                    bresult.Add(s.Substring(pos + 1, pos2 - pos - 1));
                                }
                                else
                                {
                                    Core.ApplicationData.Instance.Logger.AddLog(new DataAccess(), Logger.Level.Warning, string.Format("url parse error: {0}", s));
                                }
                            }
                            else
                            {
                                Core.ApplicationData.Instance.Logger.AddLog(new DataAccess(), Logger.Level.Warning, string.Format("url parse error: {0}", s));
                            }
                        }
                    }
                }
                if (!notInDescriptionOnly)
                {
                    result = bresult;
                }
                else
                {
                    result = new List<string>();
                }
                List<Core.Data.GeocacheImage> imgList = DataAccess.GetGeocacheImages(db, geocacheCode);
                if (imgList != null)
                {
                    foreach (Core.Data.GeocacheImage img in imgList)
                    {
                        if (!bresult.Contains(img.Url))
                        {
                            result.Add(img.Url);
                        }
                    }
                }
            }
            else
            {
                result = bresult;
            }
            return result;
        }
Beispiel #15
0
 public static List<string> GetImagesOfGeocache(Database db, string geocacheCode)
 {
     return GetImagesOfGeocache(db, geocacheCode, false);
 }
Beispiel #16
0
 public static List<Core.Data.LogImage> GetLogImages(Database db, string logId)
 {
     return db.LogImageCollection.GetLogImages(logId);
 }
Beispiel #17
0
 public static List<Core.Data.Log> GetLogs(Database db, string geocacheCode)
 {
     return db.LogCollection.GetLogs(geocacheCode);
 }
Beispiel #18
0
 public static List<Core.Data.Waypoint> GetWaypointsFromGeocache(Database db, string geocacheCode)
 {
     return db.WaypointCollection.GetWaypoints(geocacheCode);
 }
Beispiel #19
0
 public static List<GAPPSF.Core.Data.GeocacheImage> GetGeocacheImages(Database db, string GeocacheCode)
 {
     //grouping
     return db.GeocacheImageCollection.GetGeocacheImages(GeocacheCode);
 }
Beispiel #20
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;
        }
Beispiel #21
0
 private void blockUpdates(Database db)
 {
     db.GeocacheCollection.BeginUpdate();
     db.LogCollection.BeginUpdate();
 }
Beispiel #22
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;
        }
Beispiel #23
0
 public DataUpdater(Database db)
     : this(db, false)
 {
 }
Beispiel #24
0
 private void unblockUpdates(Database db)
 {
     db.LogCollection.EndUpdate();
     db.GeocacheCollection.EndUpdate();
 }
Beispiel #25
0
        async private Task OpenDatabase()
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = ""; // Default file name
            dlg.DefaultExt = ".gsf"; // Default file extension
            dlg.Filter = "GAPP SF (.gsf)|*.gsf"; // Filter files by extension 

            // Show open file dialog box
            Nullable<bool> result = dlg.ShowDialog();

            // Process open file dialog box results 
            if (result == true)
            {
                // Open document 
                string filename = dlg.FileName;
                Core.Storage.Database db = new Core.Storage.Database(filename);
                await db.InitializeAsync();
                Core.ApplicationData.Instance.Databases.Add(db);
                Core.ApplicationData.Instance.ActiveDatabase = db;
            }
        }