2012-09-11 01:34:02 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using System;
|
2012-08-17 17:37:26 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2012-07-12 06:55:27 +00:00
|
|
|
|
|
2012-09-11 01:34:02 +00:00
|
|
|
|
namespace MediaBrowser.Controller.Entities
|
2012-07-12 06:55:27 +00:00
|
|
|
|
{
|
|
|
|
|
public class Folder : BaseItem
|
|
|
|
|
{
|
2012-08-20 00:14:12 +00:00
|
|
|
|
public override bool IsFolder
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-12 06:55:27 +00:00
|
|
|
|
public bool IsRoot { get; set; }
|
|
|
|
|
|
2012-08-28 16:36:31 +00:00
|
|
|
|
public bool IsVirtualFolder
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return Parent != null && Parent.IsRoot;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-21 01:21:03 +00:00
|
|
|
|
public IEnumerable<BaseItem> Children { get; set; }
|
2012-07-12 06:55:27 +00:00
|
|
|
|
|
2012-08-17 17:37:26 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets allowed children of an item
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IEnumerable<BaseItem> GetParentalAllowedChildren(User user)
|
|
|
|
|
{
|
|
|
|
|
return Children.Where(c => c.IsParentalAllowed(user));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets allowed recursive children of an item
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IEnumerable<BaseItem> GetParentalAllowedRecursiveChildren(User user)
|
|
|
|
|
{
|
|
|
|
|
foreach (var item in GetParentalAllowedChildren(user))
|
|
|
|
|
{
|
|
|
|
|
yield return item;
|
|
|
|
|
|
|
|
|
|
var subFolder = item as Folder;
|
|
|
|
|
|
|
|
|
|
if (subFolder != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var subitem in subFolder.GetParentalAllowedRecursiveChildren(user))
|
|
|
|
|
{
|
|
|
|
|
yield return subitem;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-18 08:22:54 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Since it can be slow to make all of these calculations at once, this method will provide a way to get them all back together
|
|
|
|
|
/// </summary>
|
|
|
|
|
public ItemSpecialCounts GetSpecialCounts(User user)
|
|
|
|
|
{
|
2012-09-11 18:20:12 +00:00
|
|
|
|
var counts = new ItemSpecialCounts();
|
2012-08-18 08:22:54 +00:00
|
|
|
|
|
|
|
|
|
IEnumerable<BaseItem> recursiveChildren = GetParentalAllowedRecursiveChildren(user);
|
|
|
|
|
|
2012-09-15 20:32:41 +00:00
|
|
|
|
var recentlyAddedItems = GetRecentlyAddedItems(recursiveChildren, user);
|
|
|
|
|
|
|
|
|
|
counts.RecentlyAddedItemCount = recentlyAddedItems.Count;
|
|
|
|
|
counts.RecentlyAddedUnPlayedItemCount = GetRecentlyAddedUnplayedItems(recentlyAddedItems, user).Count;
|
|
|
|
|
counts.InProgressItemCount = GetInProgressItems(recursiveChildren, user).Count;
|
2012-08-19 17:00:52 +00:00
|
|
|
|
counts.PlayedPercentage = GetPlayedPercentage(recursiveChildren, user);
|
2012-08-18 08:22:54 +00:00
|
|
|
|
|
|
|
|
|
return counts;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-17 17:37:26 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Finds all recursive items within a top-level parent that contain the given genre and are allowed for the current user
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IEnumerable<BaseItem> GetItemsWithGenre(string genre, User user)
|
|
|
|
|
{
|
|
|
|
|
return GetParentalAllowedRecursiveChildren(user).Where(f => f.Genres != null && f.Genres.Any(s => s.Equals(genre, StringComparison.OrdinalIgnoreCase)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Finds all recursive items within a top-level parent that contain the given year and are allowed for the current user
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IEnumerable<BaseItem> GetItemsWithYear(int year, User user)
|
|
|
|
|
{
|
|
|
|
|
return GetParentalAllowedRecursiveChildren(user).Where(f => f.ProductionYear.HasValue && f.ProductionYear == year);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Finds all recursive items within a top-level parent that contain the given studio and are allowed for the current user
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IEnumerable<BaseItem> GetItemsWithStudio(string studio, User user)
|
|
|
|
|
{
|
|
|
|
|
return GetParentalAllowedRecursiveChildren(user).Where(f => f.Studios != null && f.Studios.Any(s => s.Equals(studio, StringComparison.OrdinalIgnoreCase)));
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-09 18:32:51 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Finds all recursive items within a top-level parent that the user has marked as a favorite
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IEnumerable<BaseItem> GetFavoriteItems(User user)
|
|
|
|
|
{
|
|
|
|
|
return GetParentalAllowedRecursiveChildren(user).Where(c =>
|
|
|
|
|
{
|
2012-09-10 02:30:34 +00:00
|
|
|
|
UserItemData data = c.GetUserData(user, false);
|
2012-09-09 18:32:51 +00:00
|
|
|
|
|
|
|
|
|
if (data != null)
|
|
|
|
|
{
|
|
|
|
|
return data.IsFavorite;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-18 08:22:54 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Finds all recursive items within a top-level parent that contain the given person and are allowed for the current user
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IEnumerable<BaseItem> GetItemsWithPerson(string person, User user)
|
|
|
|
|
{
|
|
|
|
|
return GetParentalAllowedRecursiveChildren(user).Where(c =>
|
|
|
|
|
{
|
|
|
|
|
if (c.People != null)
|
|
|
|
|
{
|
2012-08-24 16:24:29 +00:00
|
|
|
|
return c.People.ContainsKey(person);
|
2012-08-18 08:22:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-17 17:37:26 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Finds all recursive items within a top-level parent that contain the given person and are allowed for the current user
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="personType">Specify this to limit results to a specific PersonType</param>
|
2012-08-18 08:22:54 +00:00
|
|
|
|
public IEnumerable<BaseItem> GetItemsWithPerson(string person, string personType, User user)
|
2012-08-17 17:37:26 +00:00
|
|
|
|
{
|
|
|
|
|
return GetParentalAllowedRecursiveChildren(user).Where(c =>
|
|
|
|
|
{
|
|
|
|
|
if (c.People != null)
|
|
|
|
|
{
|
2012-08-24 16:24:29 +00:00
|
|
|
|
return c.People.ContainsKey(person) && c.People[person].Type.Equals(personType, StringComparison.OrdinalIgnoreCase);
|
2012-08-17 17:37:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets all recently added items (recursive) within a folder, based on configuration and parental settings
|
|
|
|
|
/// </summary>
|
2012-09-15 20:32:41 +00:00
|
|
|
|
public List<BaseItem> GetRecentlyAddedItems(User user)
|
2012-08-17 17:37:26 +00:00
|
|
|
|
{
|
2012-08-18 08:22:54 +00:00
|
|
|
|
return GetRecentlyAddedItems(GetParentalAllowedRecursiveChildren(user), user);
|
2012-08-17 17:37:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets all recently added unplayed items (recursive) within a folder, based on configuration and parental settings
|
|
|
|
|
/// </summary>
|
2012-09-15 20:32:41 +00:00
|
|
|
|
public List<BaseItem> GetRecentlyAddedUnplayedItems(User user)
|
2012-08-17 17:37:26 +00:00
|
|
|
|
{
|
2012-08-18 08:22:54 +00:00
|
|
|
|
return GetRecentlyAddedUnplayedItems(GetParentalAllowedRecursiveChildren(user), user);
|
2012-08-17 17:37:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets all in-progress items (recursive) within a folder
|
|
|
|
|
/// </summary>
|
2012-09-15 20:32:41 +00:00
|
|
|
|
public List<BaseItem> GetInProgressItems(User user)
|
2012-08-17 17:37:26 +00:00
|
|
|
|
{
|
2012-08-18 08:22:54 +00:00
|
|
|
|
return GetInProgressItems(GetParentalAllowedRecursiveChildren(user), user);
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-10 02:30:34 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Takes a list of items and returns the ones that are recently added
|
|
|
|
|
/// </summary>
|
2012-09-15 20:32:41 +00:00
|
|
|
|
private static List<BaseItem> GetRecentlyAddedItems(IEnumerable<BaseItem> itemSet, User user)
|
2012-08-18 08:22:54 +00:00
|
|
|
|
{
|
2012-09-15 20:32:41 +00:00
|
|
|
|
var list = new List<BaseItem>();
|
|
|
|
|
|
|
|
|
|
foreach (var item in itemSet)
|
|
|
|
|
{
|
|
|
|
|
if (!item.IsFolder && item.IsRecentlyAdded(user))
|
|
|
|
|
{
|
|
|
|
|
list.Add(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list;
|
2012-08-18 08:22:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-10 02:30:34 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Takes a list of items and returns the ones that are recently added and unplayed
|
|
|
|
|
/// </summary>
|
2012-09-15 20:32:41 +00:00
|
|
|
|
private static List<BaseItem> GetRecentlyAddedUnplayedItems(IEnumerable<BaseItem> itemSet, User user)
|
2012-08-18 08:22:54 +00:00
|
|
|
|
{
|
2012-09-15 20:32:41 +00:00
|
|
|
|
var list = new List<BaseItem>();
|
|
|
|
|
|
|
|
|
|
foreach (var item in itemSet)
|
2012-08-18 08:22:54 +00:00
|
|
|
|
{
|
2012-09-15 20:32:41 +00:00
|
|
|
|
if (!item.IsFolder && item.IsRecentlyAdded(user))
|
|
|
|
|
{
|
|
|
|
|
var userdata = item.GetUserData(user, false);
|
2012-08-18 08:22:54 +00:00
|
|
|
|
|
2012-09-15 20:32:41 +00:00
|
|
|
|
if (userdata == null || userdata.PlayCount == 0)
|
|
|
|
|
{
|
|
|
|
|
list.Add(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list;
|
2012-08-18 08:22:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-10 02:30:34 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Takes a list of items and returns the ones that are in progress
|
|
|
|
|
/// </summary>
|
2012-09-15 20:32:41 +00:00
|
|
|
|
private static List<BaseItem> GetInProgressItems(IEnumerable<BaseItem> itemSet, User user)
|
2012-08-18 08:22:54 +00:00
|
|
|
|
{
|
2012-09-15 20:32:41 +00:00
|
|
|
|
var list = new List<BaseItem>();
|
|
|
|
|
|
|
|
|
|
foreach (var item in itemSet)
|
2012-08-17 17:37:26 +00:00
|
|
|
|
{
|
2012-09-15 20:32:41 +00:00
|
|
|
|
if (!item.IsFolder)
|
2012-08-17 17:37:26 +00:00
|
|
|
|
{
|
2012-09-15 20:32:41 +00:00
|
|
|
|
var userdata = item.GetUserData(user, false);
|
2012-08-17 17:37:26 +00:00
|
|
|
|
|
2012-09-15 20:32:41 +00:00
|
|
|
|
if (userdata != null && userdata.PlaybackPositionTicks > 0)
|
|
|
|
|
{
|
|
|
|
|
list.Add(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-08-17 17:37:26 +00:00
|
|
|
|
|
2012-09-15 20:32:41 +00:00
|
|
|
|
return list;
|
2012-08-17 17:37:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-10 02:30:34 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the total played percentage for a set of items
|
|
|
|
|
/// </summary>
|
2012-08-19 17:00:52 +00:00
|
|
|
|
private static decimal GetPlayedPercentage(IEnumerable<BaseItem> itemSet, User user)
|
2012-08-18 08:22:54 +00:00
|
|
|
|
{
|
2012-08-20 00:14:12 +00:00
|
|
|
|
itemSet = itemSet.Where(i => !(i.IsFolder));
|
2012-08-18 08:22:54 +00:00
|
|
|
|
|
|
|
|
|
decimal totalPercent = 0;
|
|
|
|
|
|
2012-09-11 14:35:04 +00:00
|
|
|
|
int count = 0;
|
|
|
|
|
|
2012-08-18 08:22:54 +00:00
|
|
|
|
foreach (BaseItem item in itemSet)
|
|
|
|
|
{
|
2012-09-11 14:35:04 +00:00
|
|
|
|
count++;
|
|
|
|
|
|
2012-09-10 02:30:34 +00:00
|
|
|
|
UserItemData data = item.GetUserData(user, false);
|
2012-08-18 08:22:54 +00:00
|
|
|
|
|
|
|
|
|
if (data == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data.PlayCount > 0)
|
|
|
|
|
{
|
|
|
|
|
totalPercent += 100;
|
|
|
|
|
}
|
|
|
|
|
else if (data.PlaybackPositionTicks > 0 && item.RunTimeTicks.HasValue)
|
|
|
|
|
{
|
|
|
|
|
decimal itemPercent = data.PlaybackPositionTicks;
|
|
|
|
|
itemPercent /= item.RunTimeTicks.Value;
|
|
|
|
|
totalPercent += itemPercent;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-11 14:35:04 +00:00
|
|
|
|
if (count == 0)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return totalPercent / count;
|
2012-08-18 08:22:54 +00:00
|
|
|
|
}
|
2012-08-19 17:00:52 +00:00
|
|
|
|
|
2012-09-10 02:30:34 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Marks the item as either played or unplayed
|
|
|
|
|
/// </summary>
|
|
|
|
|
public override void SetPlayedStatus(User user, bool wasPlayed)
|
|
|
|
|
{
|
|
|
|
|
base.SetPlayedStatus(user, wasPlayed);
|
|
|
|
|
|
|
|
|
|
// Now sweep through recursively and update status
|
|
|
|
|
foreach (BaseItem item in GetParentalAllowedChildren(user))
|
|
|
|
|
{
|
|
|
|
|
item.SetPlayedStatus(user, wasPlayed);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-12 06:55:27 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Finds an item by ID, recursively
|
|
|
|
|
/// </summary>
|
2012-08-18 08:22:54 +00:00
|
|
|
|
public override BaseItem FindItemById(Guid id)
|
2012-07-12 06:55:27 +00:00
|
|
|
|
{
|
2012-08-18 08:22:54 +00:00
|
|
|
|
var result = base.FindItemById(id);
|
|
|
|
|
|
|
|
|
|
if (result != null)
|
2012-07-12 06:55:27 +00:00
|
|
|
|
{
|
2012-08-18 08:22:54 +00:00
|
|
|
|
return result;
|
2012-07-12 06:55:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (BaseItem item in Children)
|
|
|
|
|
{
|
2012-08-18 08:22:54 +00:00
|
|
|
|
result = item.FindItemById(id);
|
2012-07-12 06:55:27 +00:00
|
|
|
|
|
2012-08-18 08:22:54 +00:00
|
|
|
|
if (result != null)
|
2012-07-20 02:22:44 +00:00
|
|
|
|
{
|
2012-08-18 08:22:54 +00:00
|
|
|
|
return result;
|
2012-07-12 06:55:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Finds an item by path, recursively
|
|
|
|
|
/// </summary>
|
|
|
|
|
public BaseItem FindByPath(string path)
|
|
|
|
|
{
|
|
|
|
|
if (Path.Equals(path, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (BaseItem item in Children)
|
|
|
|
|
{
|
2012-07-20 02:22:44 +00:00
|
|
|
|
var folder = item as Folder;
|
2012-07-12 06:55:27 +00:00
|
|
|
|
|
2012-07-20 02:22:44 +00:00
|
|
|
|
if (folder != null)
|
|
|
|
|
{
|
|
|
|
|
var foundItem = folder.FindByPath(path);
|
2012-07-12 06:55:27 +00:00
|
|
|
|
|
2012-07-20 02:22:44 +00:00
|
|
|
|
if (foundItem != null)
|
|
|
|
|
{
|
|
|
|
|
return foundItem;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (item.Path.Equals(path, StringComparison.OrdinalIgnoreCase))
|
2012-07-12 06:55:27 +00:00
|
|
|
|
{
|
|
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|