コード例 #1
0
ファイル: UpdaterService.cs プロジェクト: iQuarc/QBTracker
        /// <summary>
        /// Reads the Atom feed at <see cref="ReleaseUpdateUri"/> and fills the <see cref="Releases"/>
        /// collection with information. If a release with higher version number than <see cref="ApplicationVersion"/>
        /// is found then the <see cref="ReleaseToUpdate"/> is set to this release.
        /// </summary>
        /// <param name="force"></param>
        /// <returns></returns>
        public async Task <bool> CheckForUpdate(bool force = false)
        {
            EnsureUpdateEntry();
            if (updateEntry.LastCheck == DateTime.Today && !force)
            {
                return(false);
            }
            try
            {
                using (var cl = new HttpClient())
                {
                    var response = await cl.GetAsync(ReleaseUpdateUri);

                    response.EnsureSuccessStatusCode();
                    using (var xmlReader = XmlReader.Create(await response.Content.ReadAsStreamAsync(),
                                                            new XmlReaderSettings
                    {
                        Async = true
                    }))
                    {
                        Releases.Clear();
                        ReleaseToUpdate = null;
                        var feedReader = new AtomFeedReader(xmlReader);

                        while (await feedReader.Read())
                        {
                            switch (feedReader.ElementType)
                            {
                            case SyndicationElementType.Category:
                                await feedReader.ReadCategory();

                                break;

                            case SyndicationElementType.Image:
                                await feedReader.ReadImage();

                                break;

                            case SyndicationElementType.Item:
                                var entry = await feedReader.ReadEntry();

                                var r = new Release();
                                r.DownloadUri = entry.Links.FirstOrDefault(x => x.RelationshipType == "alternate")?.Uri;
                                if (r.DownloadUri != null)
                                {
                                    r.Updated       = entry.LastUpdated.ToUniversalTime().LocalDateTime;
                                    r.ParsedVersion = ParseVersion(r.DownloadUri.AbsolutePath);
                                    if (r.ParsedVersion != null)
                                    {
                                        Releases.Add(r);
                                    }
                                }

                                break;

                            case SyndicationElementType.Link:
                                await feedReader.ReadLink();

                                break;

                            case SyndicationElementType.Person:
                                await feedReader.ReadPerson();

                                break;

                            default:
                                await feedReader.ReadContent();

                                break;
                            }
                        }

                        if (Releases.Count == 0)
                        {
                            return(false);
                        }
                        var top = Releases.OrderByDescending(x => x.ParsedVersion).First();
                        if (top.ParsedVersion > ApplicationVersion)
                        {
                            ReleaseToUpdate = top;
                            return(true);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogException(ex);
                return(false);
            }
            updateEntry.LastCheck = DateTime.Today;
            SaveUpdateEntry();
            return(false);
        }