Exemple #1
0
        private void doStartupCheck()
        {
            XElement[] elements = new XElement[] { CHDBElement, InWorkElement };
            foreach (XElement element in elements)
            {
                IEnumerable <XElement> queryPathsNeedChecking =
                    // loop on videos
                    from vid in element.Descendants("VIDEO")
                    // where they either have never been seen
                    where (string)vid.Attribute("Exists") == null
                    // or the last time they were checked is unknown
                    || (string)vid.Attribute("LastChecked") == null
                    // or the last time they where checked was more than two days agp
                    || DateTime.Now.Subtract((DateTime)vid.Attribute("LastChecked")).TotalDays > 2
                    // get the filename
                    select vid.Element("Filename");

                // Check for their existence.
                foreach (var file in queryPathsNeedChecking)
                {
                    if (PlayerHelper.CheckFileExists(file.Value))
                    {
                        file.Parent.SetAttributeValue("Exists", true);
                        file.Parent.SetAttributeValue("LastChecked", DateTime.Now);
                        Console.WriteLine(file.ToString() + " exists");
                    }
                    else
                    {
                        file.Parent.Attributes("Exists").Remove();
                        file.Parent.Attributes("LastChecked").Remove();
                        Console.WriteLine(file.ToString() + " does not exist");
                    }
                }
            }


            // sync up tag DB
            // using "select new" to only retain tag + value
            // so tags will look the same in the union

            var TagIEqualityComparer = new FuncEqualityComparer <XElement>(
                (x1, x2) =>
            {
                if (x1 == null)
                {
                    return(x2 == null);
                }
                if (x2 == null)
                {
                    return(x1 == null);
                }
                return(x1.Value.Equals(x2.Value));
            },
                (x) =>
            {
                if (x == null)
                {
                    return(0);
                }
                return(x.Value.GetHashCode());
            });

            var allTags = from tag in CHDBElement.Descendants("Tag") select new XElement(tag.Name, tag.Value);

            allTags = allTags.Union(from tag in InWorkElement.Descendants("Tag") select new XElement(tag.Name, tag.Value), TagIEqualityComparer);
            var tagsInTagDB = from tag in TagDBElement.Descendants("Tag") select new XElement(tag.Name, tag.Value);
            // allTags = allTags.Union(tagsInTagDB); is it worth doing this??
            var needToAddTags = allTags.Except(tagsInTagDB, TagIEqualityComparer);

            // since at this point we should have parentless tags, and should have only tags not already in tagDB
            // ... add them
            TagDBElement.Add(needToAddTags);
        }