123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- using MediaBrowser.Model.Entities;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace MediaBrowser.Controller.Entities
- {
- public class Folder : BaseItem
- {
- public override bool IsFolder
- {
- get
- {
- return true;
- }
- }
- public bool IsRoot { get; set; }
- public bool IsVirtualFolder
- {
- get
- {
- return Parent != null && Parent.IsRoot;
- }
- }
- public IEnumerable<BaseItem> Children { get; set; }
- /// <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;
- }
- }
- }
- }
- /// <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)
- {
- var counts = new ItemSpecialCounts();
- IEnumerable<BaseItem> recursiveChildren = GetParentalAllowedRecursiveChildren(user);
- var recentlyAddedItems = GetRecentlyAddedItems(recursiveChildren, user);
- counts.RecentlyAddedItemCount = recentlyAddedItems.Count;
- counts.RecentlyAddedUnPlayedItemCount = GetRecentlyAddedUnplayedItems(recentlyAddedItems, user).Count;
- counts.InProgressItemCount = GetInProgressItems(recursiveChildren, user).Count;
- counts.PlayedPercentage = GetPlayedPercentage(recursiveChildren, user);
- return counts;
- }
- /// <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)));
- }
- /// <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 =>
- {
- UserItemData data = c.GetUserData(user, false);
- if (data != null)
- {
- return data.IsFavorite;
- }
- return false;
- });
- }
- /// <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)
- {
- return c.People.ContainsKey(person);
- }
- return false;
- });
- }
- /// <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>
- public IEnumerable<BaseItem> GetItemsWithPerson(string person, string personType, User user)
- {
- return GetParentalAllowedRecursiveChildren(user).Where(c =>
- {
- if (c.People != null)
- {
- return c.People.ContainsKey(person) && c.People[person].Type.Equals(personType, StringComparison.OrdinalIgnoreCase);
- }
- return false;
- });
- }
- /// <summary>
- /// Gets all recently added items (recursive) within a folder, based on configuration and parental settings
- /// </summary>
- public List<BaseItem> GetRecentlyAddedItems(User user)
- {
- return GetRecentlyAddedItems(GetParentalAllowedRecursiveChildren(user), user);
- }
- /// <summary>
- /// Gets all recently added unplayed items (recursive) within a folder, based on configuration and parental settings
- /// </summary>
- public List<BaseItem> GetRecentlyAddedUnplayedItems(User user)
- {
- return GetRecentlyAddedUnplayedItems(GetParentalAllowedRecursiveChildren(user), user);
- }
- /// <summary>
- /// Gets all in-progress items (recursive) within a folder
- /// </summary>
- public List<BaseItem> GetInProgressItems(User user)
- {
- return GetInProgressItems(GetParentalAllowedRecursiveChildren(user), user);
- }
- /// <summary>
- /// Takes a list of items and returns the ones that are recently added
- /// </summary>
- private static List<BaseItem> GetRecentlyAddedItems(IEnumerable<BaseItem> itemSet, User user)
- {
- var list = new List<BaseItem>();
- foreach (var item in itemSet)
- {
- if (!item.IsFolder && item.IsRecentlyAdded(user))
- {
- list.Add(item);
- }
- }
- return list;
- }
- /// <summary>
- /// Takes a list of items and returns the ones that are recently added and unplayed
- /// </summary>
- private static List<BaseItem> GetRecentlyAddedUnplayedItems(IEnumerable<BaseItem> itemSet, User user)
- {
- var list = new List<BaseItem>();
- foreach (var item in itemSet)
- {
- if (!item.IsFolder && item.IsRecentlyAdded(user))
- {
- var userdata = item.GetUserData(user, false);
- if (userdata == null || userdata.PlayCount == 0)
- {
- list.Add(item);
- }
- }
- }
- return list;
- }
- /// <summary>
- /// Takes a list of items and returns the ones that are in progress
- /// </summary>
- private static List<BaseItem> GetInProgressItems(IEnumerable<BaseItem> itemSet, User user)
- {
- var list = new List<BaseItem>();
- foreach (var item in itemSet)
- {
- if (!item.IsFolder)
- {
- var userdata = item.GetUserData(user, false);
- if (userdata != null && userdata.PlaybackPositionTicks > 0)
- {
- list.Add(item);
- }
- }
- }
- return list;
- }
- /// <summary>
- /// Gets the total played percentage for a set of items
- /// </summary>
- private static decimal GetPlayedPercentage(IEnumerable<BaseItem> itemSet, User user)
- {
- itemSet = itemSet.Where(i => !(i.IsFolder));
- decimal totalPercent = 0;
- int count = 0;
- foreach (BaseItem item in itemSet)
- {
- count++;
-
- UserItemData data = item.GetUserData(user, false);
- 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;
- }
- }
- if (count == 0)
- {
- return 0;
- }
- return totalPercent / count;
- }
- /// <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);
- }
- }
- /// <summary>
- /// Finds an item by ID, recursively
- /// </summary>
- public override BaseItem FindItemById(Guid id)
- {
- var result = base.FindItemById(id);
- if (result != null)
- {
- return result;
- }
- foreach (BaseItem item in Children)
- {
- result = item.FindItemById(id);
- if (result != null)
- {
- return result;
- }
- }
- 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)
- {
- var folder = item as Folder;
- if (folder != null)
- {
- var foundItem = folder.FindByPath(path);
- if (foundItem != null)
- {
- return foundItem;
- }
- }
- else if (item.Path.Equals(path, StringComparison.OrdinalIgnoreCase))
- {
- return item;
- }
- }
- return null;
- }
- }
- }
|