Beispiel #1
0
        private async Task AddToPlaylistInternal(string playlistId, IEnumerable <string> itemIds, User user)
        {
            var playlist = _libraryManager.GetItemById(playlistId) as Playlist;

            if (playlist == null)
            {
                throw new ArgumentException("No Playlist exists with the supplied Id");
            }

            var list = new List <LinkedChild>();

            var items = (await GetPlaylistItems(itemIds, playlist.MediaType, user).ConfigureAwait(false))
                        .Where(i => i.SupportsAddingToPlaylist)
                        .ToList();

            foreach (var item in items)
            {
                list.Add(LinkedChild.Create(item));
            }

            playlist.LinkedChildren.AddRange(list);

            await playlist.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false);

            _providerManager.QueueRefresh(playlist.Id, new MetadataRefreshOptions(_fileSystem)
            {
                ForceSave = true
            });
        }
Beispiel #2
0
        private void AddToPlaylistInternal(string playlistId, IEnumerable <string> itemIds, User user, DtoOptions options)
        {
            var playlist = _libraryManager.GetItemById(playlistId) as Playlist;

            if (playlist == null)
            {
                throw new ArgumentException("No Playlist exists with the supplied Id");
            }

            var list = new List <LinkedChild>();

            var items = (GetPlaylistItems(itemIds, playlist.MediaType, user, options))
                        .Where(i => i.SupportsAddingToPlaylist)
                        .ToList();

            foreach (var item in items)
            {
                list.Add(LinkedChild.Create(item));
            }

            var newList = playlist.LinkedChildren.ToList();

            newList.AddRange(list);
            playlist.LinkedChildren = newList.ToArray(newList.Count);

            playlist.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None);

            _providerManager.QueueRefresh(playlist.Id, new MetadataRefreshOptions(_fileSystem)
            {
                ForceSave = true
            }, RefreshPriority.High);
        }
        public async Task AddToPlaylist(string playlistId, IEnumerable <string> itemIds)
        {
            var playlist = _libraryManager.GetItemById(playlistId) as Playlist;

            if (playlist == null)
            {
                throw new ArgumentException("No Playlist exists with the supplied Id");
            }

            var list     = new List <LinkedChild>();
            var itemList = new List <BaseItem>();

            var items = GetPlaylistItems(itemIds, playlist.MediaType).ToList();

            foreach (var item in items)
            {
                itemList.Add(item);
                list.Add(LinkedChild.Create(item));
            }

            playlist.LinkedChildren.AddRange(list);

            await playlist.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false);

            await playlist.RefreshMetadata(new MetadataRefreshOptions {
                ForceSave = true
            }, CancellationToken.None).ConfigureAwait(false);
        }
        private async Task AddToCollection(Guid collectionId, IEnumerable <Guid> ids, bool fireEvent)
        {
            var collection = _libraryManager.GetItemById(collectionId) as BoxSet;

            if (collection == null)
            {
                throw new ArgumentException("No collection exists with the supplied Id");
            }

            var list     = new List <LinkedChild>();
            var itemList = new List <BaseItem>();
            var currentLinkedChildren = collection.GetLinkedChildren().ToList();

            foreach (var itemId in ids)
            {
                var item = _libraryManager.GetItemById(itemId);

                if (item == null)
                {
                    throw new ArgumentException("No item exists with the supplied Id");
                }

                itemList.Add(item);

                if (currentLinkedChildren.Any(i => i.Id == itemId))
                {
                    throw new ArgumentException("Item already exists in collection");
                }

                list.Add(LinkedChild.Create(item));

                var supportsGrouping = item as ISupportsBoxSetGrouping;

                if (supportsGrouping != null)
                {
                    var boxsetIdList = supportsGrouping.BoxSetIdList.ToList();
                    if (!boxsetIdList.Contains(collectionId))
                    {
                        boxsetIdList.Add(collectionId);
                    }
                    supportsGrouping.BoxSetIdList = boxsetIdList;
                }
            }

            collection.LinkedChildren.AddRange(list);

            await collection.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false);

            await collection.RefreshMetadata(CancellationToken.None).ConfigureAwait(false);

            if (fireEvent)
            {
                EventHelper.FireEventIfNotNull(ItemsAddedToCollection, this, new CollectionModifiedEventArgs
                {
                    Collection   = collection,
                    ItemsChanged = itemList
                }, _logger);
            }
        }
        protected LinkedChild GetLinkedChild(XmlReader reader)
        {
            reader.MoveToContent();

            var linkedItem = new LinkedChild
            {
                Type = LinkedChildType.Manual
            };

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    switch (reader.Name)
                    {
                    case "Name":
                    {
                        linkedItem.ItemName = reader.ReadElementContentAsString();
                        break;
                    }

                    case "Path":
                    {
                        linkedItem.Path = reader.ReadElementContentAsString();
                        break;
                    }

                    case "Type":
                    {
                        linkedItem.ItemType = reader.ReadElementContentAsString();
                        break;
                    }

                    case "Year":
                    {
                        var val = reader.ReadElementContentAsString();

                        if (!string.IsNullOrWhiteSpace(val))
                        {
                            int rval;

                            if (int.TryParse(val, NumberStyles.Integer, _usCulture, out rval))
                            {
                                linkedItem.ItemYear = rval;
                            }
                        }

                        break;
                    }

                    default:
                        reader.Skip();
                        break;
                    }
                }
            }

            return(string.IsNullOrWhiteSpace(linkedItem.ItemName) || string.IsNullOrWhiteSpace(linkedItem.ItemType) ? null : linkedItem);
        }
Beispiel #6
0
        private void AddToCollection(Guid collectionId, IEnumerable <string> ids, bool fireEvent, MetadataRefreshOptions refreshOptions)
        {
            var collection = _libraryManager.GetItemById(collectionId) as BoxSet;

            if (collection == null)
            {
                throw new ArgumentException("No collection exists with the supplied Id");
            }

            var list     = new List <LinkedChild>();
            var itemList = new List <BaseItem>();
            var currentLinkedChildrenIds = collection.GetLinkedChildren().Select(i => i.Id).ToList();

            foreach (var id in ids)
            {
                var guidId = new Guid(id);
                var item   = _libraryManager.GetItemById(guidId);

                if (string.IsNullOrWhiteSpace(item.Path))
                {
                    continue;
                }

                if (item == null)
                {
                    throw new ArgumentException("No item exists with the supplied Id");
                }

                itemList.Add(item);

                if (!currentLinkedChildrenIds.Contains(guidId))
                {
                    list.Add(LinkedChild.Create(item));
                }
            }

            if (list.Count > 0)
            {
                var newList = collection.LinkedChildren.ToList();
                newList.AddRange(list);
                collection.LinkedChildren = newList.ToArray(newList.Count);

                collection.UpdateRatingToContent();

                collection.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None);

                _providerManager.QueueRefresh(collection.Id, refreshOptions, RefreshPriority.High);

                if (fireEvent)
                {
                    EventHelper.FireEventIfNotNull(ItemsAddedToCollection, this, new CollectionModifiedEventArgs
                    {
                        Collection   = collection,
                        ItemsChanged = itemList
                    }, _logger);
                }
            }
        }
Beispiel #7
0
        private async Task AddToCollectionAsync(Guid collectionId, IEnumerable <Guid> ids, bool fireEvent, MetadataRefreshOptions refreshOptions)
        {
            var collection = _libraryManager.GetItemById(collectionId) as BoxSet;

            if (collection == null)
            {
                throw new ArgumentException("No collection exists with the supplied Id");
            }

            var list     = new List <LinkedChild>();
            var itemList = new List <BaseItem>();

            var linkedChildrenList       = collection.GetLinkedChildren();
            var currentLinkedChildrenIds = linkedChildrenList.Select(i => i.Id).ToList();

            foreach (var id in ids)
            {
                var item = _libraryManager.GetItemById(id);

                if (item == null)
                {
                    throw new ArgumentException("No item exists with the supplied Id");
                }

                if (!currentLinkedChildrenIds.Contains(id))
                {
                    itemList.Add(item);

                    list.Add(LinkedChild.Create(item));
                    linkedChildrenList.Add(item);
                }
            }

            if (list.Count > 0)
            {
                var newList = collection.LinkedChildren.ToList();
                newList.AddRange(list);
                collection.LinkedChildren = newList.ToArray();

                collection.UpdateRatingToItems(linkedChildrenList);

                await collection.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false);

                refreshOptions.ForceSave = true;
                _providerManager.QueueRefresh(collection.Id, refreshOptions, RefreshPriority.High);

                if (fireEvent)
                {
                    ItemsAddedToCollection?.Invoke(this, new CollectionModifiedEventArgs
                    {
                        Collection   = collection,
                        ItemsChanged = itemList
                    });
                }
            }
        }
Beispiel #8
0
        private async Task AddToCollection(Guid collectionId, IEnumerable <Guid> ids, bool fireEvent, MetadataRefreshOptions refreshOptions)
        {
            var collection = _libraryManager.GetItemById(collectionId) as BoxSet;

            if (collection == null)
            {
                throw new ArgumentException("No collection exists with the supplied Id");
            }

            var list     = new List <LinkedChild>();
            var itemList = new List <BaseItem>();
            var currentLinkedChildren = collection.GetLinkedChildren().ToList();

            foreach (var itemId in ids)
            {
                var item = _libraryManager.GetItemById(itemId);

                if (item == null)
                {
                    throw new ArgumentException("No item exists with the supplied Id");
                }

                itemList.Add(item);

                if (currentLinkedChildren.All(i => i.Id != itemId))
                {
                    list.Add(LinkedChild.Create(item));
                }
            }

            if (list.Count > 0)
            {
                collection.LinkedChildren.AddRange(list);

                collection.UpdateRatingToContent();

                await collection.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false);

                _providerManager.QueueRefresh(collection.Id, refreshOptions);

                if (fireEvent)
                {
                    EventHelper.FireEventIfNotNull(ItemsAddedToCollection, this, new CollectionModifiedEventArgs
                    {
                        Collection   = collection,
                        ItemsChanged = itemList
                    }, _logger);
                }
            }
        }
Beispiel #9
0
        protected LinkedChild GetLinkedChild(XmlReader reader)
        {
            var linkedItem = new LinkedChild();

            reader.MoveToContent();
            reader.Read();

            // Loop through each element
            while (!reader.EOF && reader.ReadState == ReadState.Interactive)
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    switch (reader.Name)
                    {
                    case "Path":
                    {
                        linkedItem.Path = reader.ReadElementContentAsString();
                        break;
                    }

                    case "ItemId":
                    {
                        var libraryItemId = reader.ReadElementContentAsString();
                        if (!string.IsNullOrEmpty(libraryItemId))
                        {
                            linkedItem.LibraryItemId = new Guid(libraryItemId);
                        }
                        break;
                    }

                    default:
                        reader.Skip();
                        break;
                    }
                }
                else
                {
                    reader.Read();
                }
            }

            // This is valid
            if (!string.IsNullOrWhiteSpace(linkedItem.Path) || !linkedItem.LibraryItemId.Equals(Guid.Empty))
            {
                return(linkedItem);
            }

            return(null);
        }
        protected LinkedChild GetLinkedChild(XmlReader reader)
        {
            var linkedItem = new LinkedChild
            {
                Type = LinkedChildType.Manual
            };

            reader.MoveToContent();
            reader.Read();

            // Loop through each element
            while (!reader.EOF && reader.ReadState == ReadState.Interactive)
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    switch (reader.Name)
                    {
                    case "Path":
                    {
                        linkedItem.Path = reader.ReadElementContentAsString();
                        break;
                    }

                    default:
                        reader.Skip();
                        break;
                    }
                }
                else
                {
                    reader.Read();
                }
            }

            // This is valid
            if (!string.IsNullOrWhiteSpace(linkedItem.Path))
            {
                return(linkedItem);
            }

            return(null);
        }
Beispiel #11
0
        protected LinkedChild GetLinkedChild(XmlReader reader)
        {
            reader.MoveToContent();

            var linkedItem = new LinkedChild
            {
                Type = LinkedChildType.Manual
            };

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    switch (reader.Name)
                    {
                    case "Path":
                    {
                        linkedItem.Path = reader.ReadElementContentAsString();
                        break;
                    }

                    default:
                        reader.Skip();
                        break;
                    }
                }
            }

            // This is valid
            if (!string.IsNullOrWhiteSpace(linkedItem.Path))
            {
                return(linkedItem);
            }

            return(null);
        }
Beispiel #12
0
        private void AddToPlaylistInternal(string playlistId, ICollection <Guid> newItemIds, User user, DtoOptions options)
        {
            // Retrieve the existing playlist
            var playlist = _libraryManager.GetItemById(playlistId) as Playlist
                           ?? throw new ArgumentException("No Playlist exists with Id " + playlistId);

            // Retrieve all the items to be added to the playlist
            var newItems = GetPlaylistItems(newItemIds, playlist.MediaType, user, options)
                           .Where(i => i.SupportsAddingToPlaylist);

            // Filter out duplicate items, if necessary
            if (!_appConfig.DoPlaylistsAllowDuplicates())
            {
                var existingIds = playlist.LinkedChildren.Select(c => c.ItemId).ToHashSet();
                newItems = newItems
                           .Where(i => !existingIds.Contains(i.Id))
                           .Distinct();
            }

            // Create a list of the new linked children to add to the playlist
            var childrenToAdd = newItems
                                .Select(i => LinkedChild.Create(i))
                                .ToList();

            // Log duplicates that have been ignored, if any
            int numDuplicates = newItemIds.Count - childrenToAdd.Count;

            if (numDuplicates > 0)
            {
                _logger.LogWarning("Ignored adding {DuplicateCount} duplicate items to playlist {PlaylistName}.", numDuplicates, playlist.Name);
            }

            // Do nothing else if there are no items to add to the playlist
            if (childrenToAdd.Count == 0)
            {
                return;
            }

            // Create a new array with the updated playlist items
            var newLinkedChildren = new LinkedChild[playlist.LinkedChildren.Length + childrenToAdd.Count];

            playlist.LinkedChildren.CopyTo(newLinkedChildren, 0);
            childrenToAdd.CopyTo(newLinkedChildren, playlist.LinkedChildren.Length);

            // Update the playlist in the repository
            playlist.LinkedChildren = newLinkedChildren;
            playlist.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None);

            // Update the playlist on disk
            if (playlist.IsFile)
            {
                SavePlaylistFile(playlist);
            }

            // Refresh playlist metadata
            _providerManager.QueueRefresh(
                playlist.Id,
                new MetadataRefreshOptions(new DirectoryService(_fileSystem))
            {
                ForceSave = true
            },
                RefreshPriority.High);
        }