2014-09-04 23:48:53 -04:00
|
|
|
|
using MediaBrowser.Common.IO;
|
2014-07-29 23:31:35 -04:00
|
|
|
|
using MediaBrowser.Controller;
|
2014-06-07 15:46:24 -04:00
|
|
|
|
using MediaBrowser.Controller.Channels;
|
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Entities.Movies;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Controller.LiveTv;
|
|
|
|
|
using MediaBrowser.Controller.Localization;
|
2014-08-01 22:34:45 -04:00
|
|
|
|
using MediaBrowser.Controller.Playlists;
|
2014-06-07 15:46:24 -04:00
|
|
|
|
using MediaBrowser.Model.Channels;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using MediaBrowser.Model.Library;
|
2014-07-13 17:03:57 -04:00
|
|
|
|
using MediaBrowser.Model.Querying;
|
2014-06-07 15:46:24 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.Library
|
|
|
|
|
{
|
|
|
|
|
public class UserViewManager : IUserViewManager
|
|
|
|
|
{
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
private readonly ILocalizationManager _localizationManager;
|
|
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
|
|
|
|
|
|
private readonly IChannelManager _channelManager;
|
|
|
|
|
private readonly ILiveTvManager _liveTvManager;
|
2014-07-29 23:31:35 -04:00
|
|
|
|
private readonly IServerApplicationPaths _appPaths;
|
2014-08-01 22:34:45 -04:00
|
|
|
|
private readonly IPlaylistManager _playlists;
|
2014-06-07 15:46:24 -04:00
|
|
|
|
|
2014-08-01 22:34:45 -04:00
|
|
|
|
public UserViewManager(ILibraryManager libraryManager, ILocalizationManager localizationManager, IFileSystem fileSystem, IUserManager userManager, IChannelManager channelManager, ILiveTvManager liveTvManager, IServerApplicationPaths appPaths, IPlaylistManager playlists)
|
2014-06-07 15:46:24 -04:00
|
|
|
|
{
|
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
_localizationManager = localizationManager;
|
|
|
|
|
_fileSystem = fileSystem;
|
|
|
|
|
_userManager = userManager;
|
|
|
|
|
_channelManager = channelManager;
|
|
|
|
|
_liveTvManager = liveTvManager;
|
2014-07-29 23:31:35 -04:00
|
|
|
|
_appPaths = appPaths;
|
2014-08-01 22:34:45 -04:00
|
|
|
|
_playlists = playlists;
|
2014-06-07 15:46:24 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<Folder>> GetUserViews(UserViewQuery query, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2014-09-14 11:10:51 -04:00
|
|
|
|
var user = _userManager.GetUserById(query.UserId);
|
2014-06-07 15:46:24 -04:00
|
|
|
|
|
|
|
|
|
var folders = user.RootFolder
|
|
|
|
|
.GetChildren(user, true)
|
|
|
|
|
.OfType<Folder>()
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
var list = new List<Folder>();
|
|
|
|
|
|
|
|
|
|
var excludeFolderIds = user.Configuration.ExcludeFoldersFromGrouping.Select(i => new Guid(i)).ToList();
|
|
|
|
|
|
|
|
|
|
var standaloneFolders = folders.Where(i => UserView.IsExcludedFromGrouping(i) || excludeFolderIds.Contains(i.Id)).ToList();
|
|
|
|
|
|
2014-10-11 16:38:13 -04:00
|
|
|
|
var foldersWithViewTypes = folders
|
2014-06-07 15:46:24 -04:00
|
|
|
|
.Except(standaloneFolders)
|
2014-10-11 16:38:13 -04:00
|
|
|
|
.OfType<ICollectionFolder>()
|
2014-06-07 15:46:24 -04:00
|
|
|
|
.ToList();
|
|
|
|
|
|
2014-10-11 16:38:13 -04:00
|
|
|
|
list.AddRange(standaloneFolders);
|
|
|
|
|
|
|
|
|
|
if (foldersWithViewTypes.Any(i => string.Equals(i.CollectionType, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase)) ||
|
|
|
|
|
foldersWithViewTypes.Any(i => string.IsNullOrWhiteSpace(i.CollectionType)))
|
2014-06-07 15:46:24 -04:00
|
|
|
|
{
|
2014-10-29 18:01:02 -04:00
|
|
|
|
list.Add(await GetUserView(CollectionType.TvShows, string.Empty, cancellationToken).ConfigureAwait(false));
|
2014-06-07 15:46:24 -04:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-11 16:38:13 -04:00
|
|
|
|
if (foldersWithViewTypes.Any(i => string.Equals(i.CollectionType, CollectionType.Music, StringComparison.OrdinalIgnoreCase)) ||
|
|
|
|
|
foldersWithViewTypes.Any(i => string.Equals(i.CollectionType, CollectionType.MusicVideos, StringComparison.OrdinalIgnoreCase)))
|
2014-06-07 15:46:24 -04:00
|
|
|
|
{
|
2014-10-29 18:01:02 -04:00
|
|
|
|
list.Add(await GetUserView(CollectionType.Music, string.Empty, cancellationToken).ConfigureAwait(false));
|
2014-06-07 15:46:24 -04:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-11 16:38:13 -04:00
|
|
|
|
if (foldersWithViewTypes.Any(i => string.Equals(i.CollectionType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase)) ||
|
|
|
|
|
foldersWithViewTypes.Any(i => string.IsNullOrWhiteSpace(i.CollectionType)))
|
2014-06-07 15:46:24 -04:00
|
|
|
|
{
|
2014-10-29 18:01:02 -04:00
|
|
|
|
list.Add(await GetUserView(CollectionType.Movies, string.Empty, cancellationToken).ConfigureAwait(false));
|
2014-06-07 15:46:24 -04:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-11 16:38:13 -04:00
|
|
|
|
if (foldersWithViewTypes.Any(i => string.Equals(i.CollectionType, CollectionType.Games, StringComparison.OrdinalIgnoreCase)))
|
2014-06-07 15:46:24 -04:00
|
|
|
|
{
|
2014-10-29 18:01:02 -04:00
|
|
|
|
list.Add(await GetUserView(CollectionType.Games, string.Empty, cancellationToken).ConfigureAwait(false));
|
2014-06-07 15:46:24 -04:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-13 17:03:57 -04:00
|
|
|
|
if (user.Configuration.DisplayCollectionsView &&
|
2014-10-11 16:38:13 -04:00
|
|
|
|
folders
|
|
|
|
|
.Except(standaloneFolders)
|
|
|
|
|
.SelectMany(i => i.GetRecursiveChildren(user, false)).OfType<BoxSet>().Any())
|
2014-06-07 15:46:24 -04:00
|
|
|
|
{
|
2014-10-29 18:01:02 -04:00
|
|
|
|
list.Add(await GetUserView(CollectionType.BoxSets, string.Empty, cancellationToken).ConfigureAwait(false));
|
2014-06-07 15:46:24 -04:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-11 16:38:13 -04:00
|
|
|
|
if (foldersWithViewTypes.Any(i => string.Equals(i.CollectionType, CollectionType.Playlists, StringComparison.OrdinalIgnoreCase)))
|
2014-08-01 22:34:45 -04:00
|
|
|
|
{
|
|
|
|
|
list.Add(_playlists.GetPlaylistsFolder(user.Id.ToString("N")));
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-14 09:24:30 -04:00
|
|
|
|
if (user.Configuration.DisplayFoldersView)
|
|
|
|
|
{
|
2014-10-29 18:01:02 -04:00
|
|
|
|
list.Add(await GetUserView(CollectionType.Folders, "zz_" + CollectionType.Folders, cancellationToken).ConfigureAwait(false));
|
2014-08-14 09:24:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-07 15:46:24 -04:00
|
|
|
|
if (query.IncludeExternalContent)
|
|
|
|
|
{
|
2014-06-10 13:36:06 -04:00
|
|
|
|
var channelResult = await _channelManager.GetChannels(new ChannelQuery
|
2014-06-07 15:46:24 -04:00
|
|
|
|
{
|
|
|
|
|
UserId = query.UserId
|
|
|
|
|
|
2014-06-10 13:36:06 -04:00
|
|
|
|
}, cancellationToken).ConfigureAwait(false);
|
2014-06-07 15:46:24 -04:00
|
|
|
|
|
2014-06-30 15:24:35 -04:00
|
|
|
|
var channels = channelResult.Items;
|
|
|
|
|
|
|
|
|
|
var embeddedChannels = channels
|
|
|
|
|
.Where(i => user.Configuration.DisplayChannelsWithinViews.Contains(i.Id))
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
list.AddRange(embeddedChannels.Select(i => _channelManager.GetChannel(i.Id)));
|
|
|
|
|
|
|
|
|
|
if (channels.Length > embeddedChannels.Count)
|
2014-06-07 15:46:24 -04:00
|
|
|
|
{
|
|
|
|
|
list.Add(await _channelManager.GetInternalChannelFolder(query.UserId, cancellationToken).ConfigureAwait(false));
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-10 13:36:06 -04:00
|
|
|
|
if (_liveTvManager.GetEnabledUsers().Select(i => i.Id.ToString("N")).Contains(query.UserId))
|
2014-06-07 15:46:24 -04:00
|
|
|
|
{
|
|
|
|
|
list.Add(await _liveTvManager.GetInternalLiveTvFolder(query.UserId, cancellationToken).ConfigureAwait(false));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-18 21:42:53 -04:00
|
|
|
|
var sorted = _libraryManager.Sort(list, user, new[] { ItemSortBy.SortName }, SortOrder.Ascending).ToList();
|
|
|
|
|
|
|
|
|
|
var orders = user.Configuration.OrderedViews.ToList();
|
|
|
|
|
|
|
|
|
|
return list
|
|
|
|
|
.OrderBy(i =>
|
|
|
|
|
{
|
|
|
|
|
var index = orders.IndexOf(i.Id.ToString("N"));
|
|
|
|
|
|
|
|
|
|
return index == -1 ? int.MaxValue : index;
|
|
|
|
|
})
|
|
|
|
|
.ThenBy(sorted.IndexOf)
|
|
|
|
|
.ThenBy(i => i.SortName);
|
2014-06-07 15:46:24 -04:00
|
|
|
|
}
|
|
|
|
|
|
2014-11-10 22:41:55 -05:00
|
|
|
|
public Task<UserView> GetUserView(string name, string parentId, string type, User user, string sortName, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return _libraryManager.GetSpecialFolder(user, name, parentId, type, sortName, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-29 18:01:02 -04:00
|
|
|
|
public Task<UserView> GetUserView(string parentId, string type, User user, string sortName, CancellationToken cancellationToken)
|
2014-06-07 15:46:24 -04:00
|
|
|
|
{
|
|
|
|
|
var name = _localizationManager.GetLocalizedString("ViewType" + type);
|
|
|
|
|
|
2014-11-10 22:41:55 -05:00
|
|
|
|
return GetUserView(name, parentId, type, user, sortName, cancellationToken);
|
2014-06-07 15:46:24 -04:00
|
|
|
|
}
|
2014-07-29 23:31:35 -04:00
|
|
|
|
|
2014-10-29 18:01:02 -04:00
|
|
|
|
public Task<UserView> GetUserView(string type, string sortName, CancellationToken cancellationToken)
|
2014-07-29 23:31:35 -04:00
|
|
|
|
{
|
2014-10-29 18:01:02 -04:00
|
|
|
|
var name = _localizationManager.GetLocalizedString("ViewType" + type);
|
|
|
|
|
|
|
|
|
|
return _libraryManager.GetNamedView(name, type, sortName, cancellationToken);
|
2014-07-29 23:31:35 -04:00
|
|
|
|
}
|
2014-06-07 15:46:24 -04:00
|
|
|
|
}
|
|
|
|
|
}
|