Beispiel #1
0
        public static IEnumerable <string> GetChildVideos(IFolderMediaLocation location, string[] ignore)
        {
            if (location.Path.EndsWith("$RECYCLE.BIN"))
            {
                yield break;
            }

            foreach (var child in location.Children)
            {
                // MCE plays vobs natively
                if (child.IsVideo() || child.IsVob())
                {
                    yield return(child.Path);
                }
                else if (child is IFolderMediaLocation && Kernel.Instance.ConfigData.EnableNestedMovieFolders)
                {
                    if (ignore != null && ignore.Any(path => path.ToUpper() == child.Name.ToUpper()))
                    {
                        continue;
                    }
                    foreach (var grandChild in GetChildVideos(child as IFolderMediaLocation, null))
                    {
                        yield return(grandChild);
                    }
                }
            }
        }
Beispiel #2
0
        private IEnumerable<IMediaLocation> ChildVideos(IFolderMediaLocation location)
        {
            if (location.ContainsChild(FolderResolver.IGNORE_FOLDER) ) yield break;

            if (location.ContainsChild("mymovies.xml")) yield return null;

            if (location.ContainsChild("movie.xml")) yield return null;

            foreach (var child in location.Children) {

                // nested DVD or BD
                if (GetSpecialMediaType(child.Path.ToUpper()) != null) {
                    yield return null;
                }

                if (child.IsHidden()) continue;

                if (child.IsVideo()) {
                    yield return child;
                }
                var folder = child as IFolderMediaLocation;
                if (folder != null) {

                    if (enableTrailerSupport &&
                       folder.Name.ToUpper() == TrailersPath) {
                        continue;
                    }

                    foreach (var grandChild in ChildVideos(folder)) {
                        yield return grandChild;
                    }
                }
            }
        }
        public static bool IsArtistFolder(this IMediaLocation location)
        {
            IFolderMediaLocation folder = location as IFolderMediaLocation;

            if (folder != null)
            {
                if (Path.HasExtension(folder.Path))
                {
                    return(false);
                }

                if (MusicHelper.IsAlbumFolder(folder.Path))
                {
                    return(false);
                }

                DirectoryInfo directoryInfo = new DirectoryInfo(folder.Path);
                foreach (DirectoryInfo directory in directoryInfo.GetDirectories())
                {
                    if (IsAlbumFolder(directory.FullName))
                    {
                        return(true);
                    }
                }

                return(false);
            }
            return(false);
        }
 public MediaLocation(FileInfo info, IFolderMediaLocation parent) {
     this.Path = info.Path;
     this.Parent = parent;
     this.DateCreated = info.DateCreated;
     this.DateModified = info.DateModified;
     this.Attributes = info.Attributes;
     SetName();
 }
 public MediaLocation(FileInfo info, IFolderMediaLocation parent)
 {
     this.Path         = info.Path;
     this.Parent       = parent;
     this.DateCreated  = info.DateCreated;
     this.DateModified = info.DateModified;
     SetName();
 }
 // special constructor used by the virtual folders (allows for folder relocation)
 public FolderMediaLocation(FileInfo info, IFolderMediaLocation parent, IFolderMediaLocation location)
     : base(info, parent) {
     children = new MediaBrowser.Library.Util.Lazy<IList<IMediaLocation>>(GetChildren);
     index = new MediaBrowser.Library.Util.Lazy<Dictionary<string, IMediaLocation>>(CreateIndex); 
     if (location == null) {
         this.location = this;
     } else {
         this.location = location;
     }
 }
 // special constructor used by the virtual folders (allows for folder relocation)
 internal FolderMediaLocation(string path, IFolderMediaLocation parent, IFolderMediaLocation location)
     : base(path, parent)
 {
     children = new Lazy<IList<IMediaLocation>>(GetChildren);
     if (location == null) {
         this.location = this;
     } else {
         this.location = location;
     }
 }
        public static bool IsBoxSetFolder(this IMediaLocation location)
        {
            IFolderMediaLocation folder = location as IFolderMediaLocation;

            if (folder != null)
            {
                int start = location.Path.TrimEnd('\\').LastIndexOf('\\');
                return(location.Path.Substring(start).ToLower().Contains("[boxset]"));
            }
            return(false);
        }
Beispiel #9
0
 // special constructor used by the virtual folders (allows for folder relocation)
 internal FolderMediaLocation(FileInfo info, IFolderMediaLocation parent, IFolderMediaLocation location)
     : base(info, parent)
 {
     children = new Lazy <IList <IMediaLocation> >(GetChildren);
     index    = new Lazy <Dictionary <string, IMediaLocation> >(CreateIndex);
     if (location == null)
     {
         this.location = this;
     }
     else
     {
         this.location = location;
     }
 }
        private IEnumerable <IMediaLocation> ChildVideos(IFolderMediaLocation location)
        {
            if (location.ContainsChild(FolderResolver.IGNORE_FOLDER))
            {
                yield break;
            }

            if (location.ContainsChild("mymovies.xml"))
            {
                yield return(null);
            }

            if (location.ContainsChild("movie.xml"))
            {
                yield return(null);
            }

            foreach (var child in location.Children)
            {
                // nested DVD or BD
                if (GetSpecialMediaType(child.Path.ToUpper()) != null)
                {
                    yield return(null);
                }

                if (child.IsHidden())
                {
                    continue;
                }

                if (child.IsVideo())
                {
                    yield return(child);
                }
                var folder = child as IFolderMediaLocation;
                if (folder != null)
                {
                    if (enableTrailerSupport &&
                        folder.Name.ToUpper() == TrailersPath)
                    {
                        continue;
                    }

                    foreach (var grandChild in ChildVideos(folder))
                    {
                        yield return(grandChild);
                    }
                }
            }
        }
        private IEnumerable<IMediaLocation> ChildVideos(IFolderMediaLocation location)
        {
            if (location.ContainsChild(FolderResolver.IGNORE_FOLDER)) yield break;

            foreach (var child in location.Children) {
                if (child.IsVideo()) {
                    yield return child;
                }
                var folder = child as IFolderMediaLocation;
                if (folder != null) {
                    foreach (var grandChild in ChildVideos(folder)) {
                        yield return grandChild;
                    }
                }
            }
        }
        public static bool IsSeriesFolder(this IMediaLocation location)
        {
            IFolderMediaLocation folder = location as IFolderMediaLocation;

            if (folder != null)
            {
                if (TVUtils.IsSeasonFolder(folder.Path))
                {
                    return(false);
                }

                int i = 0;

                foreach (IMediaLocation child in folder.Children)
                {
                    if (child is IFolderMediaLocation &&
                        TVUtils.IsSeasonFolder(child.Path))
                    {
                        return(true); // we have found at least one season folder
                    }
                    else
                    {
                        i++;
                    }
                    if (i >= 3)
                    {
                        return(false); // a folder with more than 3 non-season folders in will not be counted as a series
                    }
                }

                foreach (IMediaLocation child in folder.Children)
                {
                    if (!(child is IFolderMediaLocation) &&
                        child.IsVideo() &&
                        TVUtils.EpisodeNumberFromFile(child.Path, false) != null)
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Beispiel #13
0
        IEnumerable <string> GetChildVideos(IFolderMediaLocation location)
        {
            if (location.Path.EndsWith("$RECYCLE.BIN"))
            {
                yield break;
            }

            foreach (var child in location.Children)
            {
                if (child.IsVideo())
                {
                    yield return(child.Path);
                }
                else if (child is IFolderMediaLocation)
                {
                    foreach (var grandChild in GetChildVideos(child as IFolderMediaLocation))
                    {
                        yield return(grandChild);
                    }
                }
            }
        }
Beispiel #14
0
        protected IEnumerable <string> GetChildFiles(IFolderMediaLocation location)
        {
            if (location.Path.EndsWith("$RECYCLE.BIN"))
            {
                yield break;
            }

            foreach (var child in location.Children)
            {
                if (MusicHelper.IsMusic(child.Path))
                {
                    yield return(child.Path);
                }
                else if (child is IFolderMediaLocation)
                {
                    foreach (var grandChild in GetChildFiles(child as IFolderMediaLocation))
                    {
                        yield return(grandChild);
                    }
                }
            }
        }
        private IEnumerable <IMediaLocation> ChildVideos(IFolderMediaLocation location)
        {
            if (location.ContainsChild(FolderResolver.IGNORE_FOLDER))
            {
                yield break;
            }

            foreach (var child in location.Children)
            {
                if (child.IsVideo())
                {
                    yield return(child);
                }
                var folder = child as IFolderMediaLocation;
                if (folder != null)
                {
                    foreach (var grandChild in ChildVideos(folder))
                    {
                        yield return(grandChild);
                    }
                }
            }
        }
        public MBDirectoryWatcher(Folder aFolder, bool watchChanges)
        {
            lastRefresh = System.DateTime.Now.AddMilliseconds(-60000); //initialize this
            this.folder = aFolder;
            IFolderMediaLocation location = folder.FolderMediaLocation;

            if (location is VirtualFolderMediaLocation)
            {
                //virtual folder
                this.watchedFolders = ((VirtualFolderMediaLocation)location).VirtualFolder.Folders.ToArray();
            }
            else
            {
                if (location != null)
                {
                    //regular folder
                    if (Directory.Exists(location.Path))
                    {
                        this.watchedFolders = new string[] { location.Path };
                    }
                    else
                    {
                        this.watchedFolders = new string[0];
                        Logger.ReportInfo("Cannot watch non-folder location " + aFolder.Name);
                    }
                }
                else
                {
                    Logger.ReportInfo("Cannot watch non-folder location " + aFolder.Name);
                    return;
                }
            }

            this.fileSystemWatchers = new List <FileSystemWatcher>();
            InitFileSystemWatcher(this.watchedFolders, watchChanges);
            Microsoft.MediaCenter.UI.Application.DeferredInvoke(_ => { InitTimers(); }); //timers only on app thread
        }
Beispiel #17
0
        IEnumerable<string> GetChildVideos(IFolderMediaLocation location)
        {
            if (location.Path.EndsWith("$RECYCLE.BIN")) yield break;

            foreach (var child in location.Children)
            {
                if (child.IsVideo()) yield return child.Path;
                else if (child is IFolderMediaLocation) {
                    foreach (var grandChild in GetChildVideos(child as IFolderMediaLocation)) {
                        yield return grandChild;
                    }
                }
            }
        }
 public MediaLocation(string path, IFolderMediaLocation parent)
 {
     this.Path = path;
     this.Parent = parent;
     id = new Lazy<Guid>(Path.GetMD5);
 }
        private void DetectFolderWhichIsMovie(IFolderMediaLocation folder, out bool isMovie, out MediaType mediaType, out List <IMediaLocation> volumes)
        {
            int isoCount     = 0;
            var childFolders = new List <IFolderMediaLocation>();

            isMovie   = false;
            mediaType = MediaType.Unknown;

            volumes = new List <IMediaLocation>();

            foreach (var child in folder.Children)
            {
                var pathUpper = child.Path.ToUpper();

                if (pathUpper.EndsWith("VIDEO_TS") || pathUpper.EndsWith(".VOB"))
                {
                    isMovie   = true;
                    mediaType = MediaType.DVD;
                    break;
                }

                if (pathUpper.EndsWith("HVDVD_TS"))
                {
                    isMovie   = true;
                    mediaType = MediaType.HDDVD;
                    break;
                }

                if (pathUpper.EndsWith("BDMV"))
                {
                    isMovie   = true;
                    mediaType = MediaType.BluRay;
                    break;
                }

                if (child.IsIso())
                {
                    mediaType = MediaType.ISO;
                    isoCount++;
                    if (isoCount > 1)
                    {
                        break;
                    }
                }

                if (pathUpper.EndsWith("NOAUTOPLAYLIST"))
                {
                    break;
                }

                var childFolder = child as IFolderMediaLocation;
                if (childFolder != null)
                {
                    childFolders.Add(childFolder);
                }

                if (child.IsVideo())
                {
                    volumes.Add(child);
                    if (volumes.Count > maxVideosPerMovie || isoCount > 0)
                    {
                        break;
                    }
                }
            }

            if (searchForVideosRecursively && isoCount == 0)
            {
                int currentCount = volumes.Count;

                volumes.AddRange(childFolders
                                 .Select(child => ChildVideos(child))
                                 .SelectMany(x => x)
                                 .Take((maxVideosPerMovie - currentCount) + 1));
            }

            if (volumes.Count > 0 && isoCount == 0)
            {
                if (volumes.Count <= maxVideosPerMovie)
                {
                    isMovie = true;
                }
            }

            if (volumes.Count == 0 && isoCount == 1)
            {
                isMovie = true;
            }

            return;
        }
Beispiel #20
0
        void ValidateChildrenImpl()
        {
            location = null;
            // cache a copy of the children
            var childrenCopy = Children;

            var validChildren   = GetChildren(false);
            var currentChildren = new Dictionary <Guid, BaseItem>();

            // in case some how we have a non distinct list
            foreach (var item in childrenCopy)
            {
                currentChildren[item.Id] = item;
            }


            bool changed = false;

            foreach (var item in validChildren)
            {
                BaseItem currentChild;
                if (currentChildren.TryGetValue(item.Id, out currentChild))
                {
                    if (currentChild != null)
                    {
                        changed |= currentChild.AssignFromItem(item);
                        currentChildren[item.Id] = null;
                    }
                }
                else
                {
                    changed = true;
                    lock (ActualChildren) {
                        item.Parent = this;
                        ActualChildren.Add(item);
                    }
                }
            }

            foreach (var item in currentChildren.Values.Where(item => item != null))
            {
                changed = true;
                lock (ActualChildren) {
                    ActualChildren.RemoveAll(current => current.Id == item.Id);
                }
            }

            // this is a rare concurrency bug workaround - which I already fixed (it protects against regressions)
            if (!changed && childrenCopy.Count != validChildren.Count)
            {
                Debug.Assert(false, "For some reason we have duplicate items in our folder, fixing this up!");
                childrenCopy = childrenCopy
                               .Distinct(i => i.Id)
                               .ToList();

                lock (ActualChildren) {
                    ActualChildren.Clear();
                    ActualChildren.AddRange(childrenCopy);
                }

                changed = true;
            }


            if (changed)
            {
                SaveChildren(Children);
                OnChildrenChanged(null);
            }
        }
Beispiel #21
0
        public static IEnumerable<string> GetChildVideos(IFolderMediaLocation location, string[] ignore)
        {
            if (location.Path.EndsWith("$RECYCLE.BIN")) yield break;

            foreach (var child in location.Children)
            {
                // MCE plays vobs natively
                if (child.IsVideo() || child.IsVob()) yield return child.Path;
                else if (child is IFolderMediaLocation && Kernel.Instance.ConfigData.EnableNestedMovieFolders) {
                    if (ignore != null && ignore.Any(path => path.ToUpper() == child.Name.ToUpper())) {
                        continue;
                    }
                    foreach (var grandChild in GetChildVideos(child as IFolderMediaLocation, null)) {
                        yield return grandChild;
                    }
                }
            }
        }
 internal FolderMediaLocation(string path, IFolderMediaLocation parent)
     : this(path, parent, null)
 {
 }
        private void DetectFolderWhichIsMovie(IFolderMediaLocation folder, out bool isMovie, out MediaType mediaType, out List<IMediaLocation> volumes)
        {
            int isoCount = 0;
            var childFolders = new List<IFolderMediaLocation>();
            isMovie = false;
            mediaType = MediaType.Unknown;

            volumes = new List<IMediaLocation>();

            foreach (var child in folder.Children) {
                var pathUpper = child.Path.ToUpper();

                if (pathUpper.EndsWith("VIDEO_TS") || pathUpper.EndsWith(".VOB")) {
                    isMovie = true;
                    mediaType = MediaType.DVD;
                    break;
                }

                if (pathUpper.EndsWith("HVDVD_TS")) {
                    isMovie = true;
                    mediaType = MediaType.HDDVD;
                    break;
                }

                if (pathUpper.EndsWith("BDMV")) {
                    isMovie = true;
                    mediaType = MediaType.BluRay;
                    break;
                }

                if (child.IsIso()) {
                    mediaType = MediaType.ISO;
                    isoCount++;
                    if (isoCount > 1) {
                        break;
                    }
                }

                if (pathUpper.EndsWith("NOAUTOPLAYLIST")) {
                    break;
                }

                var childFolder = child as IFolderMediaLocation;
                if (childFolder != null) {
                    childFolders.Add(childFolder);
                }

                if (child.IsVideo()) {
                    volumes.Add(child);
                    if (volumes.Count > maxVideosPerMovie || isoCount > 0) {
                        break;
                    }
                }
            }

            if (searchForVideosRecursively && isoCount == 0) {

                int currentCount = volumes.Count;

                volumes.AddRange(childFolders
                    .Select(child => ChildVideos(child))
                    .SelectMany(x => x)
                    .Take((maxVideosPerMovie - currentCount) + 1));
            }

            if (volumes.Count > 0 && isoCount == 0) {
                if (volumes.Count <= maxVideosPerMovie) {
                    isMovie = true;
                }
            }

            if (volumes.Count == 0 && isoCount == 1) {
                isMovie = true;
            }

            return;
        }
 public VirtualFolderMediaLocation(string path, IFolderMediaLocation parent)
     : base(path, parent)
 {
     virtualFolder = new VirtualFolder(Contents);
     children = new Lazy<IList<IMediaLocation>>(GetChildren);
 }
Beispiel #25
0
 internal FolderMediaLocation(FileInfo info, IFolderMediaLocation parent)
     : this(info, parent, null)
 {
 }
Beispiel #26
0
 public override void Assign(IMediaLocation location, IEnumerable<InitializationParameter> parameters, Guid id)
 {
     base.Assign(location, parameters, id);
     this.location = location as IFolderMediaLocation;
 }
Beispiel #27
0
 public override void Assign(IMediaLocation location, IEnumerable <InitializationParameter> parameters, Guid id)
 {
     base.Assign(location, parameters, id);
     this.location = location as IFolderMediaLocation;
 }
        private void DetectFolderWhichIsMovie(IFolderMediaLocation folder, out bool isMovie, out MediaType mediaType, out List <IMediaLocation> volumes)
        {
            int isoCount     = 0;
            var childFolders = new List <IFolderMediaLocation>();

            isMovie   = false;
            mediaType = MediaType.Unknown;

            volumes = new List <IMediaLocation>();

            foreach (var child in folder.Children)
            {
                var pathUpper = child.Path.ToUpper();

                var tmpMediaType = GetSpecialMediaType(pathUpper);
                // DVD/ BD or ISO
                if (tmpMediaType != null)
                {
                    mediaType = tmpMediaType.Value;
                    if (tmpMediaType.Value == MediaType.ISO)
                    {
                        isoCount++;
                        if (isoCount > 1)
                        {
                            break;
                        }
                    }
                    else
                    {
                        isMovie = true;
                        break;
                    }
                }

                var childFolder = child as IFolderMediaLocation;
                if (enableTrailerSupport &&
                    childFolder != null &&
                    childFolder.Name.ToUpper() == TrailersPath)
                {
                    continue;
                }


                if (childFolder != null && !childFolder.IsHidden())
                {
                    childFolders.Add(childFolder);
                }

                if (!child.IsHidden() && child.IsVideo())
                {
                    volumes.Add(child);
                    if (volumes.Count > maxVideosPerMovie || isoCount > 0)
                    {
                        break;
                    }
                }
            }

            if (searchForVideosRecursively && isoCount == 0)
            {
                foreach (var location in childFolders
                         .Select(child => ChildVideos(child))
                         .SelectMany(x => x))
                {
                    // this should be refactored, but I prefer this to throwing exceptions
                    if (location == null)
                    {
                        // get out of here, recursive BD / DVD / ETC found
                        return;
                    }
                    else
                    {
                        // another video found
                        volumes.Add(location);
                        if (volumes.Count > maxVideosPerMovie)
                        {
                            break;
                        }
                    }
                }
            }

            if (volumes.Count > 0 && isoCount == 0)
            {
                if (volumes.Count <= maxVideosPerMovie)
                {
                    //figure out media type from file extension (first one will win...)
                    mediaType = volumes[0].GetVideoMediaType();
                    isMovie   = true;
                }
            }

            if (volumes.Count == 0 && isoCount == 1)
            {
                isMovie = true;
            }

            return;
        }
        protected IEnumerable<string> GetChildFiles(IFolderMediaLocation location)
        {
            if (location.Path.EndsWith("$RECYCLE.BIN")) yield break;

            foreach (var child in location.Children)
            {
                if (MusicHelper.IsMusic(child.Path)) yield return child.Path;
                else if (child is IFolderMediaLocation)
                {
                    foreach (var grandChild in GetChildFiles(child as IFolderMediaLocation))
                    {
                        yield return grandChild;
                    }
                }
            }
        }
Beispiel #30
0
        private void DetectFolderWhichIsMovie(IFolderMediaLocation folder, out bool isMovie, out MediaType mediaType, out List<IMediaLocation> volumes)
        {
            int isoCount = 0;
            var childFolders = new List<IFolderMediaLocation>();
            isMovie = false;
            mediaType = MediaType.Unknown;

            volumes = new List<IMediaLocation>();

            foreach (var child in folder.Children) {
                var pathUpper = child.Path.ToUpper();

                var tmpMediaType = GetSpecialMediaType(pathUpper);
                // DVD/ BD or ISO
                if (tmpMediaType != null) {
                    mediaType = tmpMediaType.Value;
                    if (tmpMediaType.Value == MediaType.ISO) {
                        isoCount++;
                        if (isoCount > 1) {
                            break;
                        }
                    } else {
                        isMovie = true;
                        break;
                    }
                }

                var childFolder = child as IFolderMediaLocation;
                if (enableTrailerSupport &&
                    childFolder != null &&
                    childFolder.Name.ToUpper() == TrailersPath) {
                    continue;
                }

                if (childFolder != null && !childFolder.IsHidden()) {
                    childFolders.Add(childFolder);
                }

                if (!child.IsHidden() && child.IsVideo()) {
                    volumes.Add(child);
                    if (volumes.Count > maxVideosPerMovie || isoCount > 0) {
                        break;
                    }
                }
            }

            if (searchForVideosRecursively && isoCount == 0) {

                foreach (var location in childFolders
                    .Select(child => ChildVideos(child))
                    .SelectMany(x => x)) {

                    // this should be refactored, but I prefer this to throwing exceptions
                    if (location == null) {
                        // get out of here, recursive BD / DVD / ETC found
                        return;
                    } else {
                        // another video found
                        volumes.Add(location);
                        if (volumes.Count > maxVideosPerMovie) break;
                    }
                }

            }

            if (volumes.Count > 0 && isoCount == 0) {

                if (volumes.Count <= maxVideosPerMovie) {
                    //figure out media type from file extension (first one will win...)
                    mediaType = volumes[0].GetVideoMediaType();
                    isMovie = true;
                }
            }

            if (volumes.Count == 0 && isoCount == 1) {
                isMovie = true;
            }

            return;
        }
 public VirtualFolderMediaLocation(FileInfo info, IFolderMediaLocation parent)
     : base(info, parent)
 {
     virtualFolder = new VirtualFolderContents(Contents);
 }
Beispiel #32
0
        bool ValidateChildrenImpl()
        {
            location = null;
            int unavailableItems = 0;
            // cache a copy of the children

            var childrenCopy = ActualChildren.ToList(); //changed this to reference actual children so it wouldn't keep mucking up hidden ones -ebr

            var validChildren   = GetChildren(false);
            var currentChildren = new Dictionary <Guid, BaseItem>();

            // in case some how we have a non distinct list
            foreach (var item in childrenCopy)
            {
                currentChildren[item.Id] = item;
            }

            if (currentChildren.Count != validChildren.Count)
            {
                Logger.ReportVerbose("Validating " + this.Name + ". CurrentChildren: " + currentChildren.Count + ". Physical Children: " + validChildren.Count);
            }

            bool changed = false;

            foreach (var item in validChildren)
            {
                BaseItem currentChild;
                if (currentChildren.TryGetValue(item.Id, out currentChild))
                {
                    if (currentChild != null)
                    {
                        bool thisItemChanged = currentChild.AssignFromItem(item);
                        if (thisItemChanged)
                        {
                            item.RefreshMetadata(MediaBrowser.Library.Metadata.MetadataRefreshOptions.Default);
                            Kernel.Instance.ItemRepository.SaveItem(item);
                        }

                        currentChildren[item.Id] = null;
                    }
                }
                else
                {
                    changed = true;
                    Logger.ReportInfo("Adding new item to library: " + item.Name + " (" + item.Path + ")");
                    lock (ActualChildren) {
                        item.Parent = this;
                        ActualChildren.Add(item);
                        item.RefreshMetadata(MediaBrowser.Library.Metadata.MetadataRefreshOptions.Force); //necessary to get it to show up without user intervention
                        Kernel.Instance.ItemRepository.SaveItem(item);

                        // Notify the kernel that a new item was added
                        Kernel.Instance.OnItemAddedToLibrary(item);

                        var folder = item as Folder;

                        if (folder != null)
                        {
                            Logger.ReportVerbose(item.Name + " is folder - validating and refreshing children...");
                            folder.ValidateChildren();
                            foreach (var child in folder.Children)  //necessary to get new sub-items to refresh properly
                            {
                                child.RefreshMetadata(MediaBrowser.Library.Metadata.MetadataRefreshOptions.Force);
                            }
                        }
                    }
                }
            }

            foreach (var item in currentChildren.Values.Where(item => item != null))
            {
                if (FolderMediaLocation != null && FolderMediaLocation.IsUnavailable(item.Path))
                {
                    Logger.ReportInfo("Not removing missing item " + item.Name + " because its location is unavailable.");
                    unavailableItems++;
                }
                else
                {
                    changed = true;
                    Logger.ReportInfo("Removing missing item from library: (" + item.Id + ") " + item.Path);
                    lock (ActualChildren)
                    {
                        ActualChildren.RemoveAll(current => current.Id == item.Id);
                    }
                    // Notify the kernel that an item was removed
                    Kernel.Instance.OnItemRemovedFromLibrary(item);
                }
            }

            // this is a rare concurrency bug workaround - which I already fixed (it protects against regressions)
            if (!changed && childrenCopy.Count != (validChildren.Count + unavailableItems))
            {
                Logger.ReportWarning("For some reason we have duplicate items in folder " + Name + ", fixing this up!");
                Logger.ReportVerbose("ChildrenCopy count: " + childrenCopy.Count + " ValidChildren count: " + (validChildren.Count + unavailableItems));
                //Logger.ReportVerbose("ChildrenCopy contents are: ");
                //foreach (var item in childrenCopy) Logger.ReportVerbose("  --- " + item.Name + " Path: " + item.Path);
                //Logger.ReportVerbose("ValidChildren contents are: ");
                //foreach (var item in validChildren) Logger.ReportVerbose("  --- " + item.Name + " Path: " + item.Path);
                childrenCopy = childrenCopy
                               .Distinct(i => i.Id)
                               .ToList();

                lock (ActualChildren) {
                    ActualChildren.Clear();
                    ActualChildren.AddRange(childrenCopy);
                }

                changed = true;
            }


            if (changed)
            {
                lock (ActualChildren)
                    SaveChildren(ActualChildren);
                //we need to blank out the persisted RAL items for the top level folder
                var item = this;
                while (item != null && item.Parent != Kernel.Instance.RootFolder)
                {
                    item = item.Parent;
                }
                if (item != null)
                {
                    item.RemoveQuicklist();
                }
                OnChildrenChanged(new ChildrenChangedEventArgs {
                    FolderContentChanged = true
                });
            }
            return(changed);
        }
Beispiel #33
0
        void ValidateChildrenImpl()
        {
            location = null;
            // cache a copy of the children
            var childrenCopy = Children;

            var validChildren = GetChildren(false);
            var currentChildren = new Dictionary<Guid, BaseItem>();
            // in case some how we have a non distinct list
            foreach (var item in childrenCopy) {
                currentChildren[item.Id] = item;
            }

            bool changed = false;
            foreach (var item in validChildren) {
                BaseItem currentChild;
                if (currentChildren.TryGetValue(item.Id, out currentChild)) {
                    if (currentChild != null) {
                        changed |= currentChild.AssignFromItem(item);
                        currentChildren[item.Id] = null;
                    }
                } else {
                    changed = true;
                    lock (ActualChildren) {
                        item.Parent = this;
                        ActualChildren.Add(item);
                    }
                }
            }

            foreach (var item in currentChildren.Values.Where(item => item != null)) {
                changed = true;
                lock (ActualChildren) {
                    ActualChildren.RemoveAll(current => current.Id == item.Id);
                }
            }

            // this is a rare concurrency bug workaround - which I already fixed (it protects against regressions)
            if (!changed && childrenCopy.Count != validChildren.Count) {
                Debug.Assert(false,"For some reason we have duplicate items in our folder, fixing this up!");
                childrenCopy = childrenCopy
                    .Distinct(i => i.Id)
                    .ToList();

                lock (ActualChildren) {
                    ActualChildren.Clear();
                    ActualChildren.AddRange(childrenCopy);
                }

                changed = true;
            }

            if (changed) {
                SaveChildren(Children);
                OnChildrenChanged(null);
            }
        }
 internal FolderMediaLocation(FileInfo info, IFolderMediaLocation parent)
     : this(info, parent, null)
 {
 }
Beispiel #35
0
 public VirtualFolderMediaLocation(FileInfo info, IFolderMediaLocation parent)
     : base(info, parent)
 {
     virtualFolder = new VirtualFolderContents(Contents);
 }