Ejemplo n.º 1
0
 public async Task GetAllYourFavoriteGeocachesAsync(Core.Storage.Database db, bool importMissing)
 {
     using (Utils.DataUpdater upd = new Utils.DataUpdater(db))
     {
         await Task.Run(() =>
         {
             try
             {
                 List<string> gcList = null;
                 using (var api = new LiveAPI.GeocachingLiveV6())
                 {
                     var respC = api.Client.GetCacheIdsFavoritedByUser(api.Token);
                     if (respC.Status.StatusCode == 0)
                     {
                         gcList = (from s in respC.CacheCodes select s).ToList();
                         Manager.Instance.AddFavoritedGeocaches(gcList);
                     }
                     else
                     {
                         Core.ApplicationData.Instance.Logger.AddLog(this, new Exception(respC.Status.StatusMessage));
                     }
                 }
                 if (gcList != null && gcList.Count > 0 && importMissing)
                 {
                     List<string> missingList = (from a in gcList where db.GeocacheCollection.GetGeocache(a) == null select a).ToList();
                     LiveAPI.Import.ImportGeocaches(db, gcList);
                 }
             }
             catch (Exception e)
             {
                 Core.ApplicationData.Instance.Logger.AddLog(this, e);
             }
         });
     }
 }
Ejemplo n.º 2
0
 public async Task RemoveFavoriteGeocacheAsync(Core.Data.Geocache gc)
 {
     await Task.Run(() =>
     {
         try
         {
             using (var api = new LiveAPI.GeocachingLiveV6())
             {
                 var resp = api.Client.RemoveFavoritePointFromCache(api.Token, gc.Code);
                 if (resp.Status.StatusCode == 0)
                 {
                     Manager.Instance.RemoveFavoritedGeocache(gc.Code);
                 }
                 else
                 {
                     Core.ApplicationData.Instance.Logger.AddLog(this, new Exception(resp.Status.StatusMessage));
                 }
             }
         }
         catch (Exception e)
         {
             Core.ApplicationData.Instance.Logger.AddLog(this, e);
         }
     });
 }
Ejemplo n.º 3
0
        public List <string> UpdateBookmarkList(Bookmark bm)
        {
            List <string> result = null;

            try
            {
                using (LiveAPI.GeocachingLiveV6 api = new LiveAPI.GeocachingLiveV6())
                {
                    Guid guid = Guid.Parse(bm.Guid);

                    var req = new LiveAPI.LiveV6.GetBookmarkListByGuidRequest();
                    req.AccessToken      = api.Token;
                    req.BookmarkListGuid = guid;
                    var resp = api.Client.GetBookmarkListByGuid(req);
                    if (resp.Status.StatusCode == 0)
                    {
                        result = (from c in resp.BookmarkList select c.CacheCode).ToList();

                        Core.Settings.Default.SaveGCComBookmarkGeocaches(bm, result);
                    }
                    else
                    {
                        Core.ApplicationData.Instance.Logger.AddLog(this, new Exception(resp.Status.StatusMessage));
                    }
                }
            }
            catch (Exception e)
            {
                Core.ApplicationData.Instance.Logger.AddLog(this, e);
            }

            return(result);
        }
Ejemplo n.º 4
0
 public async Task RemoveFavoriteGeocacheAsync(Core.Data.Geocache gc)
 {
     await Task.Run(() =>
     {
         try
         {
             using (var api = new LiveAPI.GeocachingLiveV6())
             {
                 var resp = api.Client.RemoveFavoritePointFromCache(api.Token, gc.Code);
                 if (resp.Status.StatusCode == 0)
                 {
                     Manager.Instance.RemoveFavoritedGeocache(gc.Code);
                 }
                 else
                 {
                     Core.ApplicationData.Instance.Logger.AddLog(this, new Exception(resp.Status.StatusMessage));
                 }
             }
         }
         catch (Exception e)
         {
             Core.ApplicationData.Instance.Logger.AddLog(this, e);
         }
     });
 }
Ejemplo n.º 5
0
 public async Task GetAllYourFavoriteGeocachesAsync(Core.Storage.Database db, bool importMissing)
 {
     using (Utils.DataUpdater upd = new Utils.DataUpdater(db))
     {
         await Task.Run(() =>
         {
             try
             {
                 List <string> gcList = null;
                 using (var api = new LiveAPI.GeocachingLiveV6())
                 {
                     var respC = api.Client.GetCacheIdsFavoritedByUser(api.Token);
                     if (respC.Status.StatusCode == 0)
                     {
                         gcList = (from s in respC.CacheCodes select s).ToList();
                         Manager.Instance.AddFavoritedGeocaches(gcList);
                     }
                     else
                     {
                         Core.ApplicationData.Instance.Logger.AddLog(this, new Exception(respC.Status.StatusMessage));
                     }
                 }
                 if (gcList != null && gcList.Count > 0 && importMissing)
                 {
                     List <string> missingList = (from a in gcList where db.GeocacheCollection.GetGeocache(a) == null select a).ToList();
                     LiveAPI.Import.ImportGeocaches(db, gcList);
                 }
             }
             catch (Exception e)
             {
                 Core.ApplicationData.Instance.Logger.AddLog(this, e);
             }
         });
     }
 }
Ejemplo n.º 6
0
        public ImportPQWindow()
        {
            PQDataCollection = new ObservableCollection<PQData>();
            _downloadedPqs = new List<DownloadedPQInfo>();

            DataContext = this;
            InitializeComponent();

            Dispatcher.BeginInvoke(new Action(async ()=>{
                Core.ApplicationData.Instance.BeginActiviy();
                try
                {
                    if (!string.IsNullOrEmpty(Core.Settings.Default.LiveAPIDownloadedPQs))
                    {
                        string[] lines = Core.Settings.Default.LiveAPIDownloadedPQs.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                        foreach(string l in lines)
                        {
                            string[] parts = l.Split('|');
                            DownloadedPQInfo dp = new DownloadedPQInfo();
                            dp.Guid = Guid.Parse(parts[0]);
                            dp.DownloadedAt = DateTime.Parse(parts[1]);
                            if (dp.DownloadedAt >= DateTime.Now.AddDays(-7))
                            {
                                _downloadedPqs.Add(dp);
                            }
                        }
                        setLiveAPIDownloadedPQs();
                    }
                    using (var api = new LiveAPI.GeocachingLiveV6())
                    {
                        var resp = await api.Client.GetPocketQueryListAsync(api.Token);
                        if (resp.Status.StatusCode==0)
                        {
                            foreach(var r in resp.PocketQueryList)
                            {
                                PQDataCollection.Add(new PQData(r));
                            }
                        }
                        else
                        {
                            Core.ApplicationData.Instance.Logger.AddLog(this, Core.Logger.Level.Error, resp.Status.StatusMessage);
                        }
                    }
                    foreach(var p in PQDataCollection)
                    {
                        if ((from a in _downloadedPqs where a.Guid == p.LiveAPIData.GUID select a).FirstOrDefault() == null)
                        {
                            listItems.SelectedItems.Add(p);
                        }
                    }
                }
                catch(Exception e)
                {
                    Core.ApplicationData.Instance.Logger.AddLog(this, e);
                }
                Core.ApplicationData.Instance.EndActiviy();
            }));
        }
Ejemplo n.º 7
0
        public ImportPQWindow()
        {
            PQDataCollection = new ObservableCollection <PQData>();
            _downloadedPqs   = new List <DownloadedPQInfo>();

            DataContext = this;
            InitializeComponent();

            Dispatcher.BeginInvoke(new Action(async() => {
                Core.ApplicationData.Instance.BeginActiviy();
                try
                {
                    if (!string.IsNullOrEmpty(Core.Settings.Default.LiveAPIDownloadedPQs))
                    {
                        string[] lines = Core.Settings.Default.LiveAPIDownloadedPQs.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                        foreach (string l in lines)
                        {
                            string[] parts      = l.Split('|');
                            DownloadedPQInfo dp = new DownloadedPQInfo();
                            dp.Guid             = Guid.Parse(parts[0]);
                            dp.DownloadedAt     = DateTime.Parse(parts[1]);
                            if (dp.DownloadedAt >= DateTime.Now.AddDays(-7))
                            {
                                _downloadedPqs.Add(dp);
                            }
                        }
                        setLiveAPIDownloadedPQs();
                    }
                    using (var api = new LiveAPI.GeocachingLiveV6())
                    {
                        var resp = await api.Client.GetPocketQueryListAsync(api.Token);
                        if (resp.Status.StatusCode == 0)
                        {
                            foreach (var r in resp.PocketQueryList)
                            {
                                PQDataCollection.Add(new PQData(r));
                            }
                        }
                        else
                        {
                            Core.ApplicationData.Instance.Logger.AddLog(this, Core.Logger.Level.Error, resp.Status.StatusMessage);
                        }
                    }
                    foreach (var p in PQDataCollection)
                    {
                        if ((from a in _downloadedPqs where a.Guid == p.LiveAPIData.GUID select a).FirstOrDefault() == null)
                        {
                            listItems.SelectedItems.Add(p);
                        }
                    }
                }
                catch (Exception e)
                {
                    Core.ApplicationData.Instance.Logger.AddLog(this, e);
                }
                Core.ApplicationData.Instance.EndActiviy();
            }));
        }
Ejemplo n.º 8
0
        public void AddOwnTrackables(TrackableGroup grp)
        {
            bool canceled = false;

            using (Utils.ProgressBlock progr = new Utils.ProgressBlock("GetTrackableData", "GetTrackableData", 1, 0, true))
                using (var api = new LiveAPI.GeocachingLiveV6())
                {
                    List <string> trkCodes = new List <string>();

                    var req = new LiveAPI.LiveV6.GetTrackablesByOwnerRequest();
                    req.AccessToken        = api.Token;
                    req.TrackableLogsCount = 0;
                    req.StartIndex         = 0;
                    req.MaxPerPage         = Core.Settings.Default.LiveAPIGetOwnedTrackablesBatchSize;
                    int total = 0;
                    while (true)
                    {
                        var resp = api.Client.GetOwnedTrackables(req);
                        if (resp.Status.StatusCode == 0)
                        {
                            if (resp.Trackables != null)
                            {
                                foreach (var t in resp.Trackables)
                                {
                                    trkCodes.Add(t.Code);
                                    Core.Settings.Default.AddUpdateTrackable(grp, GetTrackableItemFromLiveAPI(t));
                                    total++;
                                }
                                if (!progr.Update("GetTrackableData", total, 2 * total))
                                {
                                    canceled = true;
                                    break;
                                }
                            }
                            if (resp.Trackables.Count() < req.MaxPerPage)
                            {
                                break;
                            }
                            else
                            {
                                req.StartIndex = total;
                                System.Threading.Thread.Sleep(Core.Settings.Default.LiveAPIDelayGetOwnedTrackables);
                            }
                        }
                        else
                        {
                            Core.ApplicationData.Instance.Logger.AddLog(this, Core.Logger.Level.Error, resp.Status.StatusMessage);
                            break;
                        }
                    }

                    if (!canceled)
                    {
                        progr.Update("GetTrackableData", total, total);
                        AddUpdateTrackables(grp, trkCodes);
                    }
                }
        }
Ejemplo n.º 9
0
        public void AddOwnTrackables(TrackableGroup grp)
        {
            bool canceled = false;
            using (Utils.ProgressBlock progr = new Utils.ProgressBlock("GetTrackableData", "GetTrackableData", 1, 0, true))
            using (var api = new LiveAPI.GeocachingLiveV6())
            {
                List<string> trkCodes = new List<string>();

                var req = new LiveAPI.LiveV6.GetTrackablesByOwnerRequest();
                req.AccessToken = api.Token;
                req.TrackableLogsCount = 0;
                req.StartIndex = 0;
                req.MaxPerPage = Core.Settings.Default.LiveAPIGetOwnedTrackablesBatchSize;
                int total = 0;
                while (true)
                {
                    var resp = api.Client.GetOwnedTrackables(req);
                    if (resp.Status.StatusCode == 0)
                    {
                        if (resp.Trackables != null)
                        {
                            foreach (var t in resp.Trackables)
                            {
                                trkCodes.Add(t.Code);
                                Core.Settings.Default.AddUpdateTrackable(grp, GetTrackableItemFromLiveAPI(t));
                                total++;
                            }
                            if (!progr.Update("GetTrackableData", total, 2 * total))
                            {
                                canceled = true;
                                break;
                            }
                        }
                        if (resp.Trackables.Count() < req.MaxPerPage)
                        {
                            break;
                        }
                        else
                        {
                            req.StartIndex = total;
                            System.Threading.Thread.Sleep(Core.Settings.Default.LiveAPIDelayGetOwnedTrackables);
                        }
                    }
                    else
                    {
                        Core.ApplicationData.Instance.Logger.AddLog(this, Core.Logger.Level.Error, resp.Status.StatusMessage);
                        break;
                    }
                }

                if (!canceled)
                {
                    progr.Update("GetTrackableData", total, total);
                    AddUpdateTrackables(grp, trkCodes);
                }
            }
        }
Ejemplo n.º 10
0
        public async Task<List<LogInfo>> LogGeocachesAsync(List<LogInfo> logInfos)
        {
            List<LogInfo> result = new List<LogInfo>();
            Utils.DataUpdater upd = null;
            if (Core.ApplicationData.Instance.ActiveDatabase != null)
            {
                upd = new Utils.DataUpdater(Core.ApplicationData.Instance.ActiveDatabase);
            }
            using (Utils.ProgressBlock prog = new Utils.ProgressBlock("LogGeocache", "Logging", logInfos.Count, 0, true))
            {
                using (var api = new LiveAPI.GeocachingLiveV6())
                {
                    foreach (LogInfo li in logInfos)
                    {
                        int index = 0;
                        List<LiveAPI.LiveV6.Trackable> dropTbs = null;
                        List<string> retrieveTbs = null;

                        //todo: check if trackable dialog is needed
                        //fetch in background

                        bool ok = false;
                        await Task.Run(() =>
                        {
                            if (index > 0 && dropTbs == null && retrieveTbs == null)
                            {
                                System.Threading.Thread.Sleep(Core.Settings.Default.LiveAPIDelayCreateFieldNoteAndPublish);
                            }
                            ok = LogGeocache(api, li, dropTbs, retrieveTbs);
                        });
                        if (ok)
                        {
                            result.Add(li);
                            index++;
                            if (!prog.Update("Logging", logInfos.Count, index))
                            {
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
            if (upd!=null)
            {
                upd.Dispose();
                upd = null;
            }
            return result;
        }
Ejemplo n.º 11
0
        public List <LiveAPI.LiveV6.GeocacheLog> GetLogsOfUser(string userName, List <Core.Data.LogType> logTypes)
        {
            List <LiveAPI.LiveV6.GeocacheLog> result = new List <LiveAPI.LiveV6.GeocacheLog>();

            using (var api = new LiveAPI.GeocachingLiveV6())
            {
                using (Utils.ProgressBlock progress = new Utils.ProgressBlock("ImportingLogs", "ImportingLogs", 100, 0, true))
                {
                    var req = new LiveAPI.LiveV6.GetUsersGeocacheLogsRequest();
                    req.AccessToken     = api.Token;
                    req.ExcludeArchived = false;
                    req.MaxPerPage      = Core.Settings.Default.LiveAPIGetUsersGeocacheLogsBatchSize;
                    req.StartIndex      = 0;
                    req.LogTypes        = (from a in logTypes select(long) a.ID).ToArray();
                    var resp = api.Client.GetUsersGeocacheLogs(req);
                    while (resp.Status.StatusCode == 0)
                    {
                        //logs.AddRange(resp.Logs);
                        //if (resp.Logs.Count() >= req.MaxPerPage)
                        if (resp.Logs.Count() > 0)
                        {
                            result.AddRange(resp.Logs);
                            req.StartIndex = result.Count;
                            if (!progress.Update("ImportingLogs", result.Count + req.MaxPerPage, result.Count))
                            {
                                _cancelled = true;
                                break;
                            }
                            System.Threading.Thread.Sleep(Core.Settings.Default.LiveAPIDelayGetUsersGeocacheLogs);
                            resp = api.Client.GetUsersGeocacheLogs(req);
                        }
                        else
                        {
                            break;
                        }
                    }
                    if (resp.Status.StatusCode != 0)
                    {
                        Core.ApplicationData.Instance.Logger.AddLog(this, Core.Logger.Level.Error, resp.Status.StatusMessage);
                        _cancelled = true;
                    }
                }
            }
            return(result);
        }
Ejemplo n.º 12
0
 public List<LiveAPI.LiveV6.GeocacheLog> GetLogsOfUser(string userName, List<Core.Data.LogType> logTypes)
 {
     List<LiveAPI.LiveV6.GeocacheLog> result = new List<LiveAPI.LiveV6.GeocacheLog>();
     using (var api = new LiveAPI.GeocachingLiveV6())
     {
         using (Utils.ProgressBlock progress = new Utils.ProgressBlock("ImportingLogs", "ImportingLogs", 100, 0, true))
         {
             var req = new LiveAPI.LiveV6.GetUsersGeocacheLogsRequest();
             req.AccessToken = api.Token;
             req.ExcludeArchived = false;
             req.MaxPerPage = Core.Settings.Default.LiveAPIGetUsersGeocacheLogsBatchSize;
             req.StartIndex = 0;
             req.LogTypes = (from a in logTypes select (long)a.ID).ToArray();
             var resp = api.Client.GetUsersGeocacheLogs(req);
             while (resp.Status.StatusCode == 0)
             {
                 //logs.AddRange(resp.Logs);
                 //if (resp.Logs.Count() >= req.MaxPerPage)
                 if (resp.Logs.Count() > 0)
                 {
                     result.AddRange(resp.Logs);
                     req.StartIndex = result.Count;
                     if (!progress.Update("ImportingLogs", result.Count + req.MaxPerPage, result.Count))
                     {
                         _cancelled = true;
                         break;
                     }
                     System.Threading.Thread.Sleep(Core.Settings.Default.LiveAPIDelayGetUsersGeocacheLogs);
                     resp = api.Client.GetUsersGeocacheLogs(req);
                 }
                 else
                 {
                     break;
                 }
             }
             if (resp.Status.StatusCode != 0)
             {
                 Core.ApplicationData.Instance.Logger.AddLog(this, Core.Logger.Level.Error, resp.Status.StatusMessage);
                 _cancelled = true;
             }
         }
     }
     return result;
 }
Ejemplo n.º 13
0
        private List <LiveAPI.LiveV6.Trackable> getOwnedTrackables(LiveAPI.GeocachingLiveV6 api)
        {
            List <LiveAPI.LiveV6.Trackable> result = new List <LiveAPI.LiveV6.Trackable>();

            try
            {
                var req = new LiveAPI.LiveV6.GetTrackablesByUserRequest();
                req.AccessToken        = api.Token;
                req.MaxPerPage         = 10;
                req.StartIndex         = 0;
                req.TrackableLogsCount = 0;
                var resp = api.Client.GetUsersTrackables(req);
                while (resp.Status.StatusCode == 0)
                {
                    if (resp.Trackables != null)
                    {
                        foreach (LiveAPI.LiveV6.Trackable tb in resp.Trackables)
                        {
                            result.Add(tb);
                        }
                        if (resp.Trackables.Count() < req.MaxPerPage)
                        {
                            break;
                        }
                        else
                        {
                            req.StartIndex += req.MaxPerPage;
                            resp            = api.Client.GetUsersTrackables(req);
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                Core.ApplicationData.Instance.Logger.AddLog(this, e);
            }
            return(result);
        }
Ejemplo n.º 14
0
 public void AddUpdateTrackables(TrackableGroup grp, List <string> trkList)
 {
     using (Utils.ProgressBlock progr = new Utils.ProgressBlock("GetTrackableData", "GetTrackableData", trkList.Count, 0, true))
         using (var api = new LiveAPI.GeocachingLiveV6())
         {
             int index = 0;
             while (index < trkList.Count && AddUpdateTrackable(api, grp, trkList[index]))
             {
                 index++;
                 if (!progr.Update("GetTrackableData", trkList.Count, index))
                 {
                     break;
                 }
                 else if (index < trkList.Count)
                 {
                     System.Threading.Thread.Sleep(Core.Settings.Default.LiveAPIDelayGetTrackablesByTBCode);
                 }
             }
         }
 }
Ejemplo n.º 15
0
        public static void ImportGeocacheLogs(Core.Storage.Database db, List<Core.Data.Geocache> gcList)
        {
            try
            {
                using (Utils.ProgressBlock progress = new Utils.ProgressBlock("UpdatingGeocaches", "UpdatingGeocache", gcList.Count, 0, true))
                {
                    int totalcount = gcList.Count;
                    using (LiveAPI.GeocachingLiveV6 client = new LiveAPI.GeocachingLiveV6())
                    {
                        int index = 0;

                        while (gcList.Count > 0)
                        {
                            int logCount = 0;
                            int maxPerPage = Core.Settings.Default.LiveAPIGetGeocacheLogsByCacheCodeBatchSize;
                            bool done = false;

                            if (Core.Settings.Default.LiveAPIUpdateGeocacheLogsMax > 0 && Core.Settings.Default.LiveAPIUpdateGeocacheLogsMax < 30)
                            {
                                maxPerPage = Core.Settings.Default.LiveAPIUpdateGeocacheLogsMax;
                            }
                            List<string> ids = new List<string>();

                            if (index > 0)
                            {
                                Thread.Sleep(Core.Settings.Default.LiveAPIDelayGetGeocacheLogsByCacheCode);
                            }
                            var resp = client.Client.GetGeocacheLogsByCacheCode(client.Token, gcList[0].Code, logCount, maxPerPage);
                            while (resp.Status.StatusCode == 0 && resp.Logs != null && resp.Logs.Count() > 0 && !done)
                            {
                                foreach (var lg in resp.Logs)
                                {
                                    if (!lg.IsArchived)
                                    {
                                        Core.Data.Log gcLog = ImportLog(db, lg);
                                        if (Core.Settings.Default.LiveAPIUpdateGeocacheLogsMax == 0 && gcLog != null)
                                        {
                                            ids.Add(gcLog.ID);
                                        }
                                    }
                                }

                                logCount += resp.Logs.Count();
                                if (Core.Settings.Default.LiveAPIUpdateGeocacheLogsMax > 0)
                                {
                                    int left = Core.Settings.Default.LiveAPIUpdateGeocacheLogsMax - logCount;
                                    if (left < maxPerPage)
                                    {
                                        maxPerPage = left;
                                    }
                                }
                                if (maxPerPage > 0)
                                {
                                    Thread.Sleep(Core.Settings.Default.LiveAPIDelayGetGeocacheLogsByCacheCode);
                                    resp = client.Client.GetGeocacheLogsByCacheCode(client.Token, gcList[0].Code, logCount, maxPerPage);
                                }
                                else
                                {
                                    done = true;
                                }
                            }
                            if (resp.Status.StatusCode != 0)
                            {
                                Core.ApplicationData.Instance.Logger.AddLog(new Import(), Core.Logger.Level.Error, resp.Status.StatusMessage);
                                break;
                            }
                            else
                            {
                                if (Core.Settings.Default.LiveAPIDeselectAfterUpdate)
                                {
                                    gcList[0].Selected = false;
                                }
                                if (Core.Settings.Default.LiveAPIUpdateGeocacheLogsMax == 0)
                                {
                                    List<Core.Data.Log> allLogs = Utils.DataAccess.GetLogs(db, gcList[0].Code);
                                    foreach (Core.Data.Log gim in allLogs)
                                    {
                                        if (!ids.Contains(gim.ID))
                                        {
                                            gim.DeleteRecord();
                                            db.LogCollection.Remove(gim);
                                        }
                                    }
                                }
                            }

                            index++;
                            if (!progress.Update("UpdatingGeocache", totalcount, index))
                            {
                                break;
                            }
                            gcList.RemoveAt(0);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Core.ApplicationData.Instance.Logger.AddLog(new Import(), e);
            }
        }
Ejemplo n.º 16
0
        public void ImportNotes(Core.Storage.Database db, bool importMissing)
        {
            List<LiveAPI.LiveV6.CacheNote> missingGeocaches = new List<LiveAPI.LiveV6.CacheNote>();
            bool error = false;
            try
            {
                using (Utils.ProgressBlock progress = new Utils.ProgressBlock("ImportingGeocachingcomFieldNotes", "ImportingGeocachingcomFieldNotes", 1, 0))
                {
                    //clear all notes
                    foreach (var gc in db.GeocacheCollection)
                    {
                        gc.PersonalNote = "";
                    }

                    using (LiveAPI.GeocachingLiveV6 client = new LiveAPI.GeocachingLiveV6(false))
                    {
                        int maxPerRequest = 100;
                        int startIndex = 0;
                        var resp = client.Client.GetUsersCacheNotes(client.Token, startIndex, maxPerRequest);
                        while (resp.Status.StatusCode == 0)
                        {
                            foreach (var n in resp.CacheNotes)
                            {
                                var gc = db.GeocacheCollection.GetGeocache(n.CacheCode);
                                if (gc != null)
                                {
                                    string s = n.Note ?? "";
                                    s = s.Replace("\r", "");
                                    s = s.Replace("\n", "\r\n");
                                    gc.PersonalNote = s;
                                }
                                else
                                {
                                    missingGeocaches.Add(n);
                                }
                            }
                            if (resp.CacheNotes.Count() >= maxPerRequest)
                            {
                                startIndex += resp.CacheNotes.Count();
                                resp = client.Client.GetUsersCacheNotes(client.Token, startIndex, maxPerRequest);
                            }
                            else
                            {
                                break;
                            }
                        }
                        if (resp.Status.StatusCode != 0)
                        {
                            error = true;
                            if (!string.IsNullOrEmpty(resp.Status.StatusMessage))
                            {
                                Core.ApplicationData.Instance.Logger.AddLog(this, Core.Logger.Level.Error, resp.Status.StatusMessage);
                            }
                        }
                    }
                }
                if (!error && missingGeocaches.Count > 0)
                {
                    List<string> gcList = (from a in missingGeocaches select a.CacheCode).ToList();
                    if (importMissing)
                    {
                        LiveAPI.Import.ImportGeocaches(db, gcList);
                        foreach (var n in missingGeocaches)
                        {
                            var gc = db.GeocacheCollection.GetGeocache(n.CacheCode);
                            if (gc != null)
                            {
                                string s = n.Note ?? "";
                                s = s.Replace("\r", "");
                                s = s.Replace("\n", "\r\n");
                                gc.PersonalNote = s;
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Core.ApplicationData.Instance.Logger.AddLog(this, e);
            }
        }
Ejemplo n.º 17
0
        public async Task <List <LogInfo> > LogGeocachesAsync(List <LogInfo> logInfos)
        {
            List <LogInfo> result = new List <LogInfo>();

            try
            {
                Utils.DataUpdater upd = null;
                if (Core.ApplicationData.Instance.ActiveDatabase != null)
                {
                    upd = new Utils.DataUpdater(Core.ApplicationData.Instance.ActiveDatabase);
                }
                using (Utils.ProgressBlock prog = new Utils.ProgressBlock("LogGeocache", "Logging", logInfos.Count, 0, true))
                {
                    using (var api = new LiveAPI.GeocachingLiveV6())
                    {
                        foreach (LogInfo li in logInfos)
                        {
                            int index = 0;
                            List <LiveAPI.LiveV6.Trackable> dropTbs = null;
                            List <string> retrieveTbs = (from a in li.TrackableRetrieve.Split(new char[] { ' ', ',', '\t' }, StringSplitOptions.RemoveEmptyEntries) select a.ToUpper()).ToList();

                            //check if trackable dialog is needed
                            if (li.TrackableDrop)
                            {
                                //fetch in background
                                List <LiveAPI.LiveV6.Trackable> tbList = null;
                                await Task.Run(() =>
                                {
                                    tbList = getOwnedTrackables(api);
                                });

                                if (tbList == null || tbList.Count == 0)
                                {
                                    Core.ApplicationData.Instance.Logger.AddLog(this, Core.Logger.Level.Error, "NoTrackablesToDrop");
                                    break;
                                }
                                Dialogs.SelectTrackablesWindow dlg = new Dialogs.SelectTrackablesWindow(tbList);
                                if (dlg.ShowDialog() == true)
                                {
                                    dropTbs = dlg.SelectedTrackables;
                                    if (dropTbs == null || dropTbs.Count == 0)
                                    {
                                        Core.ApplicationData.Instance.Logger.AddLog(this, Core.Logger.Level.Error, "NoTrackablesToDrop");
                                        break;
                                    }
                                }
                                else
                                {
                                    Core.ApplicationData.Instance.Logger.AddLog(this, Core.Logger.Level.Error, "NoTrackablesToDrop");
                                    break;
                                }
                            }

                            bool ok = false;
                            await Task.Run(() =>
                            {
                                if (index > 0 && dropTbs == null && retrieveTbs == null)
                                {
                                    System.Threading.Thread.Sleep(Core.Settings.Default.LiveAPIDelayCreateFieldNoteAndPublish);
                                }
                                ok = LogGeocache(api, li, dropTbs, retrieveTbs);
                            });

                            if (ok)
                            {
                                result.Add(li);
                                index++;
                                if (!prog.Update("Logging", logInfos.Count, index))
                                {
                                    break;
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
                if (upd != null)
                {
                    upd.Dispose();
                    upd = null;
                }
            }
            catch (Exception e)
            {
                Core.ApplicationData.Instance.Logger.AddLog(this, e);
            }
            return(result);
        }
Ejemplo n.º 18
0
        public async Task DownloadSelectedPQ()
        {
            List <LiveAPI.LiveV6.PQData> pqs = new List <LiveAPI.LiveV6.PQData>();

            foreach (PQData p in listItems.SelectedItems)
            {
                pqs.Add(p.LiveAPIData);
            }

            using (Utils.DataUpdater upd = new Utils.DataUpdater(Core.ApplicationData.Instance.ActiveDatabase))
            {
                await Task.Run(new Action(() =>
                {
                    try
                    {
                        using (Utils.ProgressBlock progress = new Utils.ProgressBlock("DownloadingPQ", "DownloadingPQ", pqs.Count, 0, true))
                        {
                            int index = 0;
                            try
                            {
                                using (var api = new LiveAPI.GeocachingLiveV6())
                                {
                                    Import imp = new Import();
                                    foreach (LiveAPI.LiveV6.PQData pq in pqs)
                                    {
                                        if (progress.Update(pq.Name, pqs.Count, index))
                                        {
                                            LiveAPI.LiveV6.GetPocketQueryZippedFileResponse resp = api.Client.GetPocketQueryZippedFile(api.Token, pq.GUID);
                                            if (resp.Status.StatusCode == 0)
                                            {
                                                using (System.IO.TemporaryFile tf = new System.IO.TemporaryFile(true))
                                                {
                                                    System.IO.File.WriteAllBytes(tf.Path, Convert.FromBase64String(resp.ZippedFile));
                                                    imp.ImportFile(tf.Path, true);
                                                    updateProcessedPq(pq.GUID);
                                                }
                                            }
                                            else
                                            {
                                                Core.ApplicationData.Instance.Logger.AddLog(this, Core.Logger.Level.Error, resp.Status.StatusMessage);
                                                break;
                                            }
                                            index++;
                                        }
                                        else
                                        {
                                            break;
                                        }
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                Core.ApplicationData.Instance.Logger.AddLog(this, e);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Core.ApplicationData.Instance.Logger.AddLog(this, e);
                    }
                }));
            }
            Close();
        }
Ejemplo n.º 19
0
        public List<string> UpdateBookmarkList(Bookmark bm)
        {
            List<string> result = null;

            try
            {
                using (LiveAPI.GeocachingLiveV6 api = new LiveAPI.GeocachingLiveV6())
                {
                    Guid guid = Guid.Parse(bm.Guid);

                    var req = new LiveAPI.LiveV6.GetBookmarkListByGuidRequest();
                    req.AccessToken = api.Token;
                    req.BookmarkListGuid = guid;
                    var resp = api.Client.GetBookmarkListByGuid(req);
                    if (resp.Status.StatusCode == 0)
                    {
                        result = (from c in resp.BookmarkList select c.CacheCode).ToList();

                        Core.Settings.Default.SaveGCComBookmarkGeocaches(bm, result);
                    }
                    else
                    {
                        Core.ApplicationData.Instance.Logger.AddLog(this, new Exception(resp.Status.StatusMessage));
                    }
                }
            }
            catch (Exception e)
            {
                Core.ApplicationData.Instance.Logger.AddLog(this, e);
            }

            return result;
        }
Ejemplo n.º 20
0
        public void ImportNotes(Core.Storage.Database db, bool importMissing)
        {
            List <LiveAPI.LiveV6.CacheNote> missingGeocaches = new List <LiveAPI.LiveV6.CacheNote>();
            bool error = false;

            try
            {
                using (Utils.ProgressBlock progress = new Utils.ProgressBlock("ImportingGeocachingcomFieldNotes", "ImportingGeocachingcomFieldNotes", 1, 0))
                {
                    //clear all notes
                    foreach (var gc in db.GeocacheCollection)
                    {
                        gc.PersonalNote = "";
                    }

                    using (LiveAPI.GeocachingLiveV6 client = new LiveAPI.GeocachingLiveV6(false))
                    {
                        int maxPerRequest = 30;
                        int startIndex    = 0;
                        var resp          = client.Client.GetUsersCacheNotes(client.Token, startIndex, maxPerRequest);
                        while (resp.Status.StatusCode == 0)
                        {
                            foreach (var n in resp.CacheNotes)
                            {
                                var gc = db.GeocacheCollection.GetGeocache(n.CacheCode);
                                if (gc != null)
                                {
                                    string s = n.Note ?? "";
                                    s = s.Replace("\r", "");
                                    s = s.Replace("\n", "\r\n");
                                    gc.PersonalNote = s;
                                }
                                else
                                {
                                    missingGeocaches.Add(n);
                                }
                            }
                            if (resp.CacheNotes.Count() >= maxPerRequest)
                            {
                                startIndex += resp.CacheNotes.Count();
                                Thread.Sleep(2100);
                                resp = client.Client.GetUsersCacheNotes(client.Token, startIndex, maxPerRequest);
                            }
                            else
                            {
                                break;
                            }
                        }
                        if (resp.Status.StatusCode != 0)
                        {
                            error = true;
                            if (!string.IsNullOrEmpty(resp.Status.StatusMessage))
                            {
                                Core.ApplicationData.Instance.Logger.AddLog(this, Core.Logger.Level.Error, resp.Status.StatusMessage);
                            }
                        }
                    }
                }
                if (!error && missingGeocaches.Count > 0)
                {
                    List <string> gcList = (from a in missingGeocaches select a.CacheCode).ToList();
                    if (importMissing)
                    {
                        LiveAPI.Import.ImportGeocaches(db, gcList);
                        foreach (var n in missingGeocaches)
                        {
                            var gc = db.GeocacheCollection.GetGeocache(n.CacheCode);
                            if (gc != null)
                            {
                                string s = n.Note ?? "";
                                s = s.Replace("\r", "");
                                s = s.Replace("\n", "\r\n");
                                gc.PersonalNote = s;
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Core.ApplicationData.Instance.Logger.AddLog(this, e);
            }
        }
Ejemplo n.º 21
0
        public async Task<List<LogInfo>> LogGeocachesAsync(List<LogInfo> logInfos)
        {
            List<LogInfo> result = new List<LogInfo>();
            try
            {
                Utils.DataUpdater upd = null;
                if (Core.ApplicationData.Instance.ActiveDatabase != null)
                {
                    upd = new Utils.DataUpdater(Core.ApplicationData.Instance.ActiveDatabase);
                }
                using (Utils.ProgressBlock prog = new Utils.ProgressBlock("LogGeocache", "Logging", logInfos.Count, 0, true))
                {
                    using (var api = new LiveAPI.GeocachingLiveV6())
                    {
                        foreach (LogInfo li in logInfos)
                        {
                            int index = 0;
                            List<LiveAPI.LiveV6.Trackable> dropTbs = null;
                            List<string> retrieveTbs = (from a in li.TrackableRetrieve.Split(new char[]{' ',',','\t'}, StringSplitOptions.RemoveEmptyEntries) select a.ToUpper()).ToList();

                            //check if trackable dialog is needed
                            if (li.TrackableDrop)
                            {
                                //fetch in background
                                List<LiveAPI.LiveV6.Trackable> tbList = null;
                                await Task.Run(() =>
                                {
                                    tbList = getOwnedTrackables(api);
                                });
                                if (tbList==null || tbList.Count==0)
                                {
                                    Core.ApplicationData.Instance.Logger.AddLog(this, Core.Logger.Level.Error, "NoTrackablesToDrop");
                                    break;
                                }
                                Dialogs.SelectTrackablesWindow dlg = new Dialogs.SelectTrackablesWindow(tbList);
                                if (dlg.ShowDialog() == true)
                                {
                                    dropTbs = dlg.SelectedTrackables;
                                    if (dropTbs == null || dropTbs.Count == 0)
                                    {
                                        Core.ApplicationData.Instance.Logger.AddLog(this, Core.Logger.Level.Error, "NoTrackablesToDrop");
                                        break;
                                    }
                                }
                                else
                                {
                                    Core.ApplicationData.Instance.Logger.AddLog(this, Core.Logger.Level.Error, "NoTrackablesToDrop");
                                    break;
                                }
                            }

                            bool ok = false;
                            await Task.Run(() =>
                            {
                                if (index > 0 && dropTbs == null && retrieveTbs == null)
                                {
                                    System.Threading.Thread.Sleep(Core.Settings.Default.LiveAPIDelayCreateFieldNoteAndPublish);
                                }
                                ok = LogGeocache(api, li, dropTbs, retrieveTbs);
                            });
                            if (ok)
                            {
                                result.Add(li);
                                index++;
                                if (!prog.Update("Logging", logInfos.Count, index))
                                {
                                    break;
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
                if (upd != null)
                {
                    upd.Dispose();
                    upd = null;
                }
            }
            catch(Exception e)
            {
                Core.ApplicationData.Instance.Logger.AddLog(this, e);
            }
            return result;
        }
Ejemplo n.º 22
0
 public void AddUpdateTrackables(TrackableGroup grp, List<string> trkList)
 {
     using (Utils.ProgressBlock progr = new Utils.ProgressBlock("GetTrackableData", "GetTrackableData", trkList.Count, 0, true))
     using (var api = new LiveAPI.GeocachingLiveV6())
     {
         int index = 0;
         while (index < trkList.Count && AddUpdateTrackable(api, grp, trkList[index]))
         {
             index++;
             if (!progr.Update("GetTrackableData", trkList.Count, index))
             {
                 break;
             }
             else if (index < trkList.Count)
             {
                 System.Threading.Thread.Sleep(Core.Settings.Default.LiveAPIDelayGetTrackablesByTBCode);
             }
         }
     }
 }
Ejemplo n.º 23
0
        public static void ImportGeocacheImages(Core.Storage.Database db, List <Core.Data.Geocache> gcList)
        {
            try
            {
                using (Utils.ProgressBlock progress = new Utils.ProgressBlock("UpdatingGeocaches", "UpdatingGeocache", gcList.Count, 0, true))
                {
                    int totalcount = gcList.Count;
                    using (LiveAPI.GeocachingLiveV6 client = new LiveAPI.GeocachingLiveV6())
                    {
                        int index = 0;
                        while (gcList.Count > 0)
                        {
                            if (index > 0)
                            {
                                Thread.Sleep(Core.Settings.Default.LiveAPIDelayGetImagesForGeocache);
                            }
                            var resp = client.Client.GetImagesForGeocache(client.Token, gcList[0].Code);
                            if (resp.Status.StatusCode == 0)
                            {
                                if (resp.Images != null)
                                {
                                    List <string> ids = new List <string>();
                                    foreach (var img in resp.Images)
                                    {
                                        if (img.Url.IndexOf("/cache/log/") < 0)
                                        {
                                            Core.Data.GeocacheImage gcImg = ImportGeocacheImage(db, img, gcList[0].Code);
                                            if (gcImg != null)
                                            {
                                                ids.Add(gcImg.ID);
                                            }
                                        }
                                    }
                                    List <Core.Data.GeocacheImage> allImages = Utils.DataAccess.GetGeocacheImages(db, gcList[0].Code);
                                    foreach (Core.Data.GeocacheImage gim in allImages)
                                    {
                                        if (!ids.Contains(gim.ID))
                                        {
                                            gim.DeleteRecord();
                                            db.GeocacheImageCollection.Remove(gim);
                                        }
                                    }
                                }

                                if (Core.Settings.Default.LiveAPIDeselectAfterUpdate)
                                {
                                    gcList[0].Selected = false;
                                }

                                index++;
                                if (!progress.Update("UpdatingGeocache", totalcount, index))
                                {
                                    break;
                                }
                                gcList.RemoveAt(0);
                            }
                            else
                            {
                                Core.ApplicationData.Instance.Logger.AddLog(new Import(), Core.Logger.Level.Error, resp.Status.StatusMessage);
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Core.ApplicationData.Instance.Logger.AddLog(new Import(), e);
            }
        }
Ejemplo n.º 24
0
        public async Task ImportLogsOfUsers(Core.Storage.Database db,
                                            List <string> usrs,
                                            bool betweenDates,
                                            DateTime minDate,
                                            DateTime maxDate,
                                            List <Core.Data.LogType> logTypes,
                                            bool importMissingGeocaches)
        {
            using (Utils.DataUpdater upd = new Utils.DataUpdater(db))
            {
                await Task.Run(() =>
                {
                    try
                    {
                        bool cancelled = false;
                        using (var api = new LiveAPI.GeocachingLiveV6())
                        {
                            using (Utils.ProgressBlock prog = new Utils.ProgressBlock("ImportingLogs", usrs[0], usrs.Count, 0, true))
                            {
                                for (int i = 0; !cancelled && (i < usrs.Count); i++)
                                {
                                    prog.Update(usrs[i], usrs.Count, i);

                                    List <LiveAPI.LiveV6.GeocacheLog> logs = new List <LiveAPI.LiveV6.GeocacheLog>();
                                    using (Utils.ProgressBlock progress = new Utils.ProgressBlock("ImportingLogs", 100, 0))
                                    {
                                        var req             = new LiveAPI.LiveV6.GetUsersGeocacheLogsRequest();
                                        req.Username        = usrs[i];
                                        req.AccessToken     = api.Token;
                                        req.ExcludeArchived = false;
                                        req.MaxPerPage      = Core.Settings.Default.LiveAPIGetUsersGeocacheLogsBatchSize;
                                        req.StartIndex      = 0;
                                        req.LogTypes        = (from a in logTypes select(long) a.ID).ToArray();
                                        if (betweenDates)
                                        {
                                            req.Range           = new LiveAPI.LiveV6.DateRange();
                                            req.Range.StartDate = minDate < maxDate ? minDate : maxDate;
                                            req.Range.EndDate   = maxDate > minDate ? maxDate : minDate;
                                        }
                                        var resp = api.Client.GetUsersGeocacheLogs(req);
                                        while (resp.Status.StatusCode == 0)
                                        {
                                            //logs.AddRange(resp.Logs);
                                            //if (resp.Logs.Count() >= req.MaxPerPage)
                                            if (resp.Logs.Count() > 0)
                                            {
                                                logs.AddRange(resp.Logs);
                                                req.StartIndex = logs.Count;
                                                if (!progress.Update("ImportingLogs", logs.Count + req.MaxPerPage, logs.Count))
                                                {
                                                    cancelled = true;
                                                    break;
                                                }
                                                System.Threading.Thread.Sleep(Core.Settings.Default.LiveAPIDelayGetUsersGeocacheLogs);
                                                resp = api.Client.GetUsersGeocacheLogs(req);
                                            }
                                            else
                                            {
                                                break;
                                            }
                                        }
                                        if (resp.Status.StatusCode != 0)
                                        {
                                            Core.ApplicationData.Instance.Logger.AddLog(this, Core.Logger.Level.Error, resp.Status.StatusMessage);
                                            cancelled = true;
                                        }
                                    }
                                    if (!cancelled && importMissingGeocaches && logs.Count > 0)
                                    {
                                        List <string> gcList = new List <string>();
                                        foreach (var l in logs)
                                        {
                                            if (!gcList.Contains(l.CacheCode) && db.GeocacheCollection.GetGeocache(l.CacheCode) == null)
                                            {
                                                gcList.Add(l.CacheCode);
                                            }
                                        }
                                        LiveAPI.Import.ImportGeocaches(db, gcList);
                                    }

                                    cancelled = !prog.Update(usrs[i], usrs.Count, i);
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Core.ApplicationData.Instance.Logger.AddLog(this, e);
                    }
                });
            }
        }
Ejemplo n.º 25
0
        private bool AddUpdateTrackable(LiveAPI.GeocachingLiveV6 api, TrackableGroup grp, string trkCode)
        {
            bool result = true;

            if (trkCode.ToUpper().StartsWith("TB"))
            {
                try
                {
                    var resp = api.Client.GetTrackablesByTBCode(api.Token, trkCode.ToUpper(), 0);
                    if (resp.Status.StatusCode == 0)
                    {
                        if (resp.Trackables != null)
                        {
                            foreach (var t in resp.Trackables)
                            {
                                TrackableItem trk = GetTrackableItemFromLiveAPI(t);
                                Core.Settings.Default.AddUpdateTrackable(grp, trk);

                                var resp2 = api.Client.GetTrackableTravelList(api.Token, trk.Code);
                                if (resp2.Status.StatusCode == 0)
                                {
                                    if (resp2.TrackableTravels != null)
                                    {
                                        List <TravelItem> travelList = new List <TravelItem>();
                                        foreach (var tt in resp2.TrackableTravels)
                                        {
                                            if (tt.Latitude != null && tt.Longitude != null)
                                            {
                                                TravelItem ti = new TravelItem();
                                                ti.TrackableCode = trk.Code;
                                                if (tt.CacheID != null)
                                                {
                                                    ti.GeocacheCode = Utils.Conversion.GetCacheCodeFromCacheID((int)tt.CacheID);
                                                }
                                                else
                                                {
                                                    ti.GeocacheCode = "";
                                                }
                                                ti.DateLogged = tt.DateLogged;
                                                ti.Lat        = (double)tt.Latitude;
                                                ti.Lon        = (double)tt.Longitude;
                                                travelList.Add(ti);
                                            }
                                        }
                                        Core.Settings.Default.UpdateTrackableTravels(trk, travelList);
                                    }

                                    //get all logs
                                    List <LogItem> logs        = new List <LogItem>();
                                    int            maxPageSize = Core.Settings.Default.LiveAPIGetTrackableLogsByTBCodeBatchSize;
                                    while (true)
                                    {
                                        var resp3 = api.Client.GetTrackableLogsByTBCode(api.Token, trk.Code, logs.Count, maxPageSize);
                                        if (resp3.Status.StatusCode == 0)
                                        {
                                            if (resp3.TrackableLogs != null)
                                            {
                                                foreach (var tl in resp3.TrackableLogs)
                                                {
                                                    LogItem li = new LogItem();
                                                    li.TrackableCode = trk.Code;
                                                    if (tl.CacheID != null)
                                                    {
                                                        li.GeocacheCode = Utils.Conversion.GetCacheCodeFromCacheID((int)tl.CacheID);
                                                    }
                                                    else
                                                    {
                                                        li.GeocacheCode = "";
                                                    }
                                                    li.LogCode       = tl.Code;
                                                    li.ID            = tl.ID;
                                                    li.IsArchived    = tl.IsArchived;
                                                    li.LoggedBy      = tl.LoggedBy == null ? "" : tl.LoggedBy.UserName;
                                                    li.LogGuid       = tl.LogGuid.ToString();
                                                    li.LogIsEncoded  = tl.LogIsEncoded;
                                                    li.LogText       = tl.LogText;
                                                    li.WptLogTypeId  = tl.LogType == null ? -1 : (int)tl.LogType.WptLogTypeId;
                                                    li.Url           = tl.Url;
                                                    li.UTCCreateDate = tl.UTCCreateDate;
                                                    li.VisitDate     = tl.VisitDate;
                                                    logs.Add(li);
                                                }
                                                if (resp3.TrackableLogs.Count() < maxPageSize)
                                                {
                                                    break;
                                                }
                                                System.Threading.Thread.Sleep(Core.Settings.Default.LiveAPIDelayGetTrackableLogsByTBCode);
                                            }
                                            else
                                            {
                                                break;
                                            }
                                        }
                                        else
                                        {
                                            logs   = null;
                                            result = false;
                                            Core.ApplicationData.Instance.Logger.AddLog(this, Core.Logger.Level.Error, resp3.Status.StatusMessage);
                                            break;
                                        }
                                    }
                                    if (logs != null)
                                    {
                                        Core.Settings.Default.UpdateTrackableLogs(trk, logs);
                                    }
                                }
                                else
                                {
                                    result = false;
                                    Core.ApplicationData.Instance.Logger.AddLog(this, Core.Logger.Level.Error, resp2.Status.StatusMessage);
                                }
                            }
                        }
                    }
                    else
                    {
                        result = false;
                        Core.ApplicationData.Instance.Logger.AddLog(this, Core.Logger.Level.Error, resp.Status.StatusMessage);
                    }
                }
                catch (Exception e)
                {
                    result = false;
                    Core.ApplicationData.Instance.Logger.AddLog(this, e);
                }
            }
            return(result);
        }
Ejemplo n.º 26
0
        public static void ImportGeocacheImages(Core.Storage.Database db, List<Core.Data.Geocache> gcList)
        {
            try
            {
                using (Utils.ProgressBlock progress = new Utils.ProgressBlock("UpdatingGeocaches", "UpdatingGeocache", gcList.Count, 0, true))
                {
                    int totalcount = gcList.Count;
                    using (LiveAPI.GeocachingLiveV6 client = new LiveAPI.GeocachingLiveV6())
                    {
                        int index = 0;
                        while (gcList.Count > 0)
                        {
                            if (index > 0)
                            {
                                Thread.Sleep(Core.Settings.Default.LiveAPIDelayGetImagesForGeocache);
                            }
                            var resp = client.Client.GetImagesForGeocache(client.Token, gcList[0].Code);
                            if (resp.Status.StatusCode == 0)
                            {
                                if (resp.Images != null)
                                {
                                    List<string> ids = new List<string>();
                                    foreach (var img in resp.Images)
                                    {
                                        if (img.Url.IndexOf("/cache/log/") < 0)
                                        {
                                            Core.Data.GeocacheImage gcImg = ImportGeocacheImage(db, img, gcList[0].Code);
                                            if (gcImg != null)
                                            {
                                                ids.Add(gcImg.ID);
                                            }
                                        }
                                    }
                                    List<Core.Data.GeocacheImage> allImages = Utils.DataAccess.GetGeocacheImages(db, gcList[0].Code);
                                    foreach (Core.Data.GeocacheImage gim in allImages)
                                    {
                                        if (!ids.Contains(gim.ID))
                                        {
                                            gim.DeleteRecord();
                                            db.GeocacheImageCollection.Remove(gim);
                                        }
                                    }
                                }

                                if (Core.Settings.Default.LiveAPIDeselectAfterUpdate)
                                {
                                    gcList[0].Selected = false;
                                }

                                index++;
                                if (!progress.Update("UpdatingGeocache", totalcount, index))
                                {
                                    break;
                                }
                                gcList.RemoveAt(0);
                            }
                            else
                            {
                                Core.ApplicationData.Instance.Logger.AddLog(new Import(),  Core.Logger.Level.Error, resp.Status.StatusMessage);
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Core.ApplicationData.Instance.Logger.AddLog(new Import(), e);
            }
        }
Ejemplo n.º 27
0
        public async Task DownloadSelectedPQ()
        {
            List<LiveAPI.LiveV6.PQData> pqs = new List<LiveAPI.LiveV6.PQData>();
            foreach (PQData p in listItems.SelectedItems)
            {
                pqs.Add(p.LiveAPIData);
            }

            using (Utils.DataUpdater upd = new Utils.DataUpdater(Core.ApplicationData.Instance.ActiveDatabase))
            {
                await Task.Run(new Action(() =>
                {
                    try
                    {
                        using (Utils.ProgressBlock progress = new Utils.ProgressBlock("DownloadingPQ", "DownloadingPQ", pqs.Count, 0, true))
                        {
                            int index = 0;
                            try
                            {
                                using (var api = new LiveAPI.GeocachingLiveV6())
                                {
                                    Import imp = new Import();
                                    foreach (LiveAPI.LiveV6.PQData pq in pqs)
                                    {
                                        if (progress.Update(pq.Name, pqs.Count, index))
                                        {
                                            LiveAPI.LiveV6.GetPocketQueryZippedFileResponse resp = api.Client.GetPocketQueryZippedFile(api.Token, pq.GUID);
                                            if (resp.Status.StatusCode == 0)
                                            {
                                                using (System.IO.TemporaryFile tf = new System.IO.TemporaryFile(true))
                                                {
                                                    System.IO.File.WriteAllBytes(tf.Path, Convert.FromBase64String(resp.ZippedFile));
                                                    imp.ImportFile(tf.Path);
                                                    updateProcessedPq(pq.GUID);
                                                }
                                            }
                                            else
                                            {
                                                Core.ApplicationData.Instance.Logger.AddLog(this, Core.Logger.Level.Error, resp.Status.StatusMessage);
                                                break;
                                            }
                                            index++;
                                        }
                                        else
                                        {
                                            break;
                                        }
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                Core.ApplicationData.Instance.Logger.AddLog(this, e);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Core.ApplicationData.Instance.Logger.AddLog(this, e);
                    }
                }));
            }
            Close();
        }
Ejemplo n.º 28
0
        public bool LogGeocache(LiveAPI.GeocachingLiveV6 api, LogInfo logInfo, List <LiveAPI.LiveV6.Trackable> dropTbs, List <string> retrieveTbs)
        {
            bool result = false;

            try
            {
                var req = new LiveAPI.LiveV6.CreateFieldNoteAndPublishRequest();
                req.AccessToken       = api.Token;
                req.CacheCode         = logInfo.GeocacheCode;
                req.EncryptLogText    = false;
                req.FavoriteThisCache = logInfo.AddToFavorites;
                req.Note          = logInfo.LogText;
                req.PromoteToLog  = true;
                req.WptLogTypeId  = logInfo.LogType.ID;
                req.UTCDateLogged = logInfo.VisitDate.Date.AddHours(12).ToUniversalTime();
                var resp = api.Client.CreateFieldNoteAndPublish(req);
                if (resp.Status.StatusCode == 0)
                {
                    bool error = false;

                    if (Core.ApplicationData.Instance.ActiveDatabase != null)
                    {
                        var gc = Core.ApplicationData.Instance.ActiveDatabase.GeocacheCollection.GetGeocache(logInfo.GeocacheCode);
                        if (gc != null)
                        {
                            if (logInfo.LogType.AsFound)
                            {
                                gc.Found = true;
                            }
                        }
                        LiveAPI.Import.ImportLog(Core.ApplicationData.Instance.ActiveDatabase, resp.Log);
                        if (gc != null)
                        {
                            gc.ResetCachedLogData();
                        }
                    }

                    //log trackables (14=drop off, 13=retrieve from cache, 75=visited)
                    if (dropTbs != null && dropTbs.Count > 0)
                    {
                        var reqT = new LiveAPI.LiveV6.CreateTrackableLogRequest();
                        reqT.AccessToken   = api.Token;
                        reqT.LogType       = 14;
                        reqT.UTCDateLogged = logInfo.VisitDate.Date.AddHours(12).ToUniversalTime();
                        foreach (var tb in dropTbs)
                        {
                            reqT.TrackingNumber = tb.TrackingCode;
                            reqT.CacheCode      = logInfo.GeocacheCode;
                            var resp2 = api.Client.CreateTrackableLog(reqT);
                            if (resp2.Status.StatusCode != 0)
                            {
                                Core.ApplicationData.Instance.Logger.AddLog(this, Core.Logger.Level.Error, resp2.Status.StatusMessage);
                                error = true;
                                //break;
                            }
                        }
                    }

                    if (retrieveTbs != null && retrieveTbs.Count > 0)
                    {
                        var reqT = new LiveAPI.LiveV6.CreateTrackableLogRequest();
                        reqT.AccessToken   = api.Token;
                        reqT.LogType       = 13;
                        reqT.UTCDateLogged = logInfo.VisitDate.Date.AddHours(12).ToUniversalTime();
                        foreach (var tb in retrieveTbs)
                        {
                            reqT.TrackingNumber = tb;
                            reqT.CacheCode      = logInfo.GeocacheCode;
                            var resp2 = api.Client.CreateTrackableLog(reqT);
                            if (resp2.Status.StatusCode != 0)
                            {
                                Core.ApplicationData.Instance.Logger.AddLog(this, Core.Logger.Level.Error, resp2.Status.StatusMessage);
                                error = true;
                                //break;
                            }
                        }
                    }

                    //add log images
                    foreach (var li in logInfo.Images)
                    {
                        var uplReq = new LiveAPI.LiveV6.UploadImageToGeocacheLogRequest();
                        uplReq.AccessToken               = api.Token;
                        uplReq.LogGuid                   = resp.Log.Guid;
                        uplReq.ImageData                 = new LiveAPI.LiveV6.UploadImageData();
                        uplReq.ImageData.FileCaption     = li.Caption;
                        uplReq.ImageData.FileDescription = li.Description;
                        uplReq.ImageData.FileName        = li.Uri;

                        using (System.IO.TemporaryFile tmpFile = new System.IO.TemporaryFile(true))
                        {
                            if (Utils.ResourceHelper.ScaleImage(li.Uri, tmpFile.Path, Core.Settings.Default.LiveAPILogGeocachesMaxImageWidth, Core.Settings.Default.LiveAPILogGeocachesMaxImageHeight, Core.Settings.Default.LiveAPILogGeocachesMaxImageSizeMB, Core.Settings.Default.LiveAPILogGeocachesImageQuality, li.RotationDeg))
                            {
                                uplReq.ImageData.base64ImageData = System.Convert.ToBase64String(System.IO.File.ReadAllBytes(tmpFile.Path));
                            }
                        }
                        if (!string.IsNullOrEmpty(uplReq.ImageData.base64ImageData))
                        {
                            var resp2 = api.Client.UploadImageToGeocacheLog(uplReq);
                            if (resp2.Status.StatusCode != 0)
                            {
                                Core.ApplicationData.Instance.Logger.AddLog(this, Core.Logger.Level.Error, resp2.Status.StatusMessage);
                                error = true;
                                //break;
                            }
                        }
                        else
                        {
                            error = true;
                        }
                    }

                    if (logInfo.AddToFavorites)
                    {
                        Favorites.Manager.Instance.AddFavoritedGeocache(logInfo.GeocacheCode);
                    }

                    result = true;
                }
                else
                {
                    Core.ApplicationData.Instance.Logger.AddLog(this, Core.Logger.Level.Error, resp.Status.StatusMessage);
                }
            }
            catch (Exception e)
            {
                Core.ApplicationData.Instance.Logger.AddLog(this, e);
            }
            return(result);
        }
Ejemplo n.º 29
0
        public static void ImportGeocacheLogs(Core.Storage.Database db, List <Core.Data.Geocache> gcList)
        {
            try
            {
                using (Utils.ProgressBlock progress = new Utils.ProgressBlock("UpdatingGeocaches", "UpdatingGeocache", gcList.Count, 0, true))
                {
                    int totalcount = gcList.Count;
                    using (LiveAPI.GeocachingLiveV6 client = new LiveAPI.GeocachingLiveV6())
                    {
                        int index = 0;

                        while (gcList.Count > 0)
                        {
                            int  logCount   = 0;
                            int  maxPerPage = Core.Settings.Default.LiveAPIGetGeocacheLogsByCacheCodeBatchSize;
                            bool done       = false;

                            if (Core.Settings.Default.LiveAPIUpdateGeocacheLogsMax > 0 && Core.Settings.Default.LiveAPIUpdateGeocacheLogsMax < 30)
                            {
                                maxPerPage = Core.Settings.Default.LiveAPIUpdateGeocacheLogsMax;
                            }
                            List <string> ids = new List <string>();

                            if (index > 0)
                            {
                                Thread.Sleep(Core.Settings.Default.LiveAPIDelayGetGeocacheLogsByCacheCode);
                            }
                            var resp = client.Client.GetGeocacheLogsByCacheCode(client.Token, gcList[0].Code, logCount, maxPerPage);
                            while (resp.Status.StatusCode == 0 && resp.Logs != null && resp.Logs.Count() > 0 && !done)
                            {
                                foreach (var lg in resp.Logs)
                                {
                                    if (!lg.IsArchived)
                                    {
                                        Core.Data.Log gcLog = ImportLog(db, lg);
                                        if (Core.Settings.Default.LiveAPIUpdateGeocacheLogsMax == 0 && gcLog != null)
                                        {
                                            ids.Add(gcLog.ID);
                                        }
                                    }
                                }

                                logCount += resp.Logs.Count();
                                if (Core.Settings.Default.LiveAPIUpdateGeocacheLogsMax > 0)
                                {
                                    int left = Core.Settings.Default.LiveAPIUpdateGeocacheLogsMax - logCount;
                                    if (left < maxPerPage)
                                    {
                                        maxPerPage = left;
                                    }
                                }
                                if (maxPerPage > 0)
                                {
                                    Thread.Sleep(Core.Settings.Default.LiveAPIDelayGetGeocacheLogsByCacheCode);
                                    resp = client.Client.GetGeocacheLogsByCacheCode(client.Token, gcList[0].Code, logCount, maxPerPage);
                                }
                                else
                                {
                                    done = true;
                                }
                            }
                            if (resp.Status.StatusCode != 0)
                            {
                                Core.ApplicationData.Instance.Logger.AddLog(new Import(), Core.Logger.Level.Error, resp.Status.StatusMessage);
                                break;
                            }
                            else
                            {
                                if (Core.Settings.Default.LiveAPIDeselectAfterUpdate)
                                {
                                    gcList[0].Selected = false;
                                }
                                if (Core.Settings.Default.LiveAPIUpdateGeocacheLogsMax == 0)
                                {
                                    List <Core.Data.Log> allLogs = Utils.DataAccess.GetLogs(db, gcList[0].Code);
                                    foreach (Core.Data.Log gim in allLogs)
                                    {
                                        if (!ids.Contains(gim.ID))
                                        {
                                            gim.DeleteRecord();
                                            db.LogCollection.Remove(gim);
                                        }
                                    }
                                }
                            }

                            index++;
                            if (!progress.Update("UpdatingGeocache", totalcount, index))
                            {
                                break;
                            }
                            gcList.RemoveAt(0);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Core.ApplicationData.Instance.Logger.AddLog(new Import(), e);
            }
        }