jellyfin/MediaBrowser.Controller/Playlists/Playlist.cs

158 lines
4.9 KiB
C#
Raw Normal View History

2014-08-02 02:34:45 +00:00
using MediaBrowser.Controller.Entities;
2014-08-19 01:42:53 +00:00
using MediaBrowser.Controller.Entities.Audio;
2014-08-24 15:48:06 +00:00
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Querying;
using System;
2014-08-02 02:34:45 +00:00
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
2014-08-02 02:34:45 +00:00
namespace MediaBrowser.Controller.Playlists
{
public class Playlist : Folder
{
public string OwnerUserId { get; set; }
2014-08-02 02:34:45 +00:00
2014-08-14 13:24:30 +00:00
protected override bool FilterLinkedChildrenPerUser
{
get
{
return true;
}
}
public override IEnumerable<BaseItem> GetChildren(User user, bool includeLinkedChildren)
2014-08-02 02:34:45 +00:00
{
return GetPlayableItems(user);
2014-08-02 02:34:45 +00:00
}
public override IEnumerable<BaseItem> GetRecursiveChildren(User user, bool includeLinkedChildren = true)
{
return GetPlayableItems(user);
}
2014-08-11 23:41:11 +00:00
public IEnumerable<Tuple<LinkedChild, BaseItem>> GetManageableItems()
{
2014-08-11 23:41:11 +00:00
return GetLinkedChildrenInfos();
}
private IEnumerable<BaseItem> GetPlayableItems(User user)
{
return GetPlaylistItems(MediaType, base.GetChildren(user, true), user);
}
public static IEnumerable<BaseItem> GetPlaylistItems(string playlistMediaType, IEnumerable<BaseItem> inputItems, User user)
{
2014-08-14 13:24:30 +00:00
if (user != null)
{
inputItems = inputItems.Where(i => i.IsVisible(user));
}
2014-08-19 01:42:53 +00:00
return inputItems.SelectMany(i => GetPlaylistItems(i, user))
2014-08-22 02:24:38 +00:00
.Where(m => string.Equals(m.MediaType, playlistMediaType, StringComparison.OrdinalIgnoreCase));
2014-08-19 01:42:53 +00:00
}
private static IEnumerable<BaseItem> GetPlaylistItems(BaseItem i, User user)
{
var musicGenre = i as MusicGenre;
if (musicGenre != null)
{
2014-08-22 02:24:38 +00:00
var items = user == null
? LibraryManager.RootFolder.GetRecursiveChildren()
: user.RootFolder.GetRecursiveChildren(user, true);
var songs = items
2014-08-19 01:42:53 +00:00
.OfType<Audio>()
.Where(a => a.Genres.Contains(musicGenre.Name, StringComparer.OrdinalIgnoreCase));
2014-08-22 02:24:38 +00:00
return LibraryManager.Sort(songs, user, new[] { ItemSortBy.AlbumArtist, ItemSortBy.Album, ItemSortBy.SortName }, SortOrder.Ascending);
2014-08-19 01:42:53 +00:00
}
2014-08-07 02:51:09 +00:00
2014-08-19 01:42:53 +00:00
var musicArtist = i as MusicArtist;
if (musicArtist != null)
{
2014-08-22 02:24:38 +00:00
var items = user == null
? LibraryManager.RootFolder.GetRecursiveChildren()
: user.RootFolder.GetRecursiveChildren(user, true);
var songs = items
2014-08-19 01:42:53 +00:00
.OfType<Audio>()
.Where(a => a.HasArtist(musicArtist.Name));
2014-08-22 02:24:38 +00:00
return LibraryManager.Sort(songs, user, new[] { ItemSortBy.AlbumArtist, ItemSortBy.Album, ItemSortBy.SortName }, SortOrder.Ascending);
2014-08-19 01:42:53 +00:00
}
2014-08-22 02:24:38 +00:00
2014-08-24 15:48:06 +00:00
// Grab these explicitly to avoid the sorting that will happen below
var collection = i as BoxSet;
if (collection != null)
{
var items = user == null
? collection.Children
: collection.GetChildren(user, true);
return items
.Where(m => !m.IsFolder);
}
// Grab these explicitly to avoid the sorting that will happen below
var season = i as Season;
if (season != null)
{
var items = user == null
? season.Children
: season.GetChildren(user, true);
return items
.Where(m => !m.IsFolder);
}
2014-08-19 01:42:53 +00:00
var folder = i as Folder;
2014-08-19 01:42:53 +00:00
if (folder != null)
{
var items = user == null
? folder.GetRecursiveChildren()
: folder.GetRecursiveChildren(user, true);
items = items
.Where(m => !m.IsFolder);
2014-08-24 15:48:06 +00:00
return LibraryManager.Sort(items, user, new[] { ItemSortBy.SortName }, SortOrder.Ascending);
2014-08-19 01:42:53 +00:00
}
2014-08-14 13:24:30 +00:00
2014-08-19 01:42:53 +00:00
return new[] { i };
}
[IgnoreDataMember]
public override bool IsPreSorted
{
get
{
return true;
}
}
public string PlaylistMediaType { get; set; }
public override string MediaType
{
get
{
return PlaylistMediaType;
}
}
public void SetMediaType(string value)
{
PlaylistMediaType = value;
}
public override bool IsVisible(User user)
2014-08-02 02:34:45 +00:00
{
return base.IsVisible(user) && string.Equals(user.Id.ToString("N"), OwnerUserId);
2014-08-02 02:34:45 +00:00
}
}
}