Folder.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. using MediaBrowser.Model.Entities;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace MediaBrowser.Controller.Entities
  6. {
  7. public class Folder : BaseItem
  8. {
  9. public override bool IsFolder
  10. {
  11. get
  12. {
  13. return true;
  14. }
  15. }
  16. public bool IsRoot { get; set; }
  17. public bool IsVirtualFolder
  18. {
  19. get
  20. {
  21. return Parent != null && Parent.IsRoot;
  22. }
  23. }
  24. public IEnumerable<BaseItem> Children { get; set; }
  25. /// <summary>
  26. /// Gets allowed children of an item
  27. /// </summary>
  28. public IEnumerable<BaseItem> GetParentalAllowedChildren(User user)
  29. {
  30. return Children.Where(c => c.IsParentalAllowed(user));
  31. }
  32. /// <summary>
  33. /// Gets allowed recursive children of an item
  34. /// </summary>
  35. public IEnumerable<BaseItem> GetParentalAllowedRecursiveChildren(User user)
  36. {
  37. foreach (var item in GetParentalAllowedChildren(user))
  38. {
  39. yield return item;
  40. var subFolder = item as Folder;
  41. if (subFolder != null)
  42. {
  43. foreach (var subitem in subFolder.GetParentalAllowedRecursiveChildren(user))
  44. {
  45. yield return subitem;
  46. }
  47. }
  48. }
  49. }
  50. /// <summary>
  51. /// 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
  52. /// </summary>
  53. public ItemSpecialCounts GetSpecialCounts(User user)
  54. {
  55. var counts = new ItemSpecialCounts();
  56. IEnumerable<BaseItem> recursiveChildren = GetParentalAllowedRecursiveChildren(user);
  57. counts.RecentlyAddedItemCount = GetRecentlyAddedItems(recursiveChildren, user).Count();
  58. counts.RecentlyAddedUnPlayedItemCount = GetRecentlyAddedUnplayedItems(recursiveChildren, user).Count();
  59. counts.InProgressItemCount = GetInProgressItems(recursiveChildren, user).Count();
  60. counts.PlayedPercentage = GetPlayedPercentage(recursiveChildren, user);
  61. return counts;
  62. }
  63. /// <summary>
  64. /// Finds all recursive items within a top-level parent that contain the given genre and are allowed for the current user
  65. /// </summary>
  66. public IEnumerable<BaseItem> GetItemsWithGenre(string genre, User user)
  67. {
  68. return GetParentalAllowedRecursiveChildren(user).Where(f => f.Genres != null && f.Genres.Any(s => s.Equals(genre, StringComparison.OrdinalIgnoreCase)));
  69. }
  70. /// <summary>
  71. /// Finds all recursive items within a top-level parent that contain the given year and are allowed for the current user
  72. /// </summary>
  73. public IEnumerable<BaseItem> GetItemsWithYear(int year, User user)
  74. {
  75. return GetParentalAllowedRecursiveChildren(user).Where(f => f.ProductionYear.HasValue && f.ProductionYear == year);
  76. }
  77. /// <summary>
  78. /// Finds all recursive items within a top-level parent that contain the given studio and are allowed for the current user
  79. /// </summary>
  80. public IEnumerable<BaseItem> GetItemsWithStudio(string studio, User user)
  81. {
  82. return GetParentalAllowedRecursiveChildren(user).Where(f => f.Studios != null && f.Studios.Any(s => s.Equals(studio, StringComparison.OrdinalIgnoreCase)));
  83. }
  84. /// <summary>
  85. /// Finds all recursive items within a top-level parent that the user has marked as a favorite
  86. /// </summary>
  87. public IEnumerable<BaseItem> GetFavoriteItems(User user)
  88. {
  89. return GetParentalAllowedRecursiveChildren(user).Where(c =>
  90. {
  91. UserItemData data = c.GetUserData(user, false);
  92. if (data != null)
  93. {
  94. return data.IsFavorite;
  95. }
  96. return false;
  97. });
  98. }
  99. /// <summary>
  100. /// Finds all recursive items within a top-level parent that contain the given person and are allowed for the current user
  101. /// </summary>
  102. public IEnumerable<BaseItem> GetItemsWithPerson(string person, User user)
  103. {
  104. return GetParentalAllowedRecursiveChildren(user).Where(c =>
  105. {
  106. if (c.People != null)
  107. {
  108. return c.People.ContainsKey(person);
  109. }
  110. return false;
  111. });
  112. }
  113. /// <summary>
  114. /// Finds all recursive items within a top-level parent that contain the given person and are allowed for the current user
  115. /// </summary>
  116. /// <param name="personType">Specify this to limit results to a specific PersonType</param>
  117. public IEnumerable<BaseItem> GetItemsWithPerson(string person, string personType, User user)
  118. {
  119. return GetParentalAllowedRecursiveChildren(user).Where(c =>
  120. {
  121. if (c.People != null)
  122. {
  123. return c.People.ContainsKey(person) && c.People[person].Type.Equals(personType, StringComparison.OrdinalIgnoreCase);
  124. }
  125. return false;
  126. });
  127. }
  128. /// <summary>
  129. /// Gets all recently added items (recursive) within a folder, based on configuration and parental settings
  130. /// </summary>
  131. public IEnumerable<BaseItem> GetRecentlyAddedItems(User user)
  132. {
  133. return GetRecentlyAddedItems(GetParentalAllowedRecursiveChildren(user), user);
  134. }
  135. /// <summary>
  136. /// Gets all recently added unplayed items (recursive) within a folder, based on configuration and parental settings
  137. /// </summary>
  138. public IEnumerable<BaseItem> GetRecentlyAddedUnplayedItems(User user)
  139. {
  140. return GetRecentlyAddedUnplayedItems(GetParentalAllowedRecursiveChildren(user), user);
  141. }
  142. /// <summary>
  143. /// Gets all in-progress items (recursive) within a folder
  144. /// </summary>
  145. public IEnumerable<BaseItem> GetInProgressItems(User user)
  146. {
  147. return GetInProgressItems(GetParentalAllowedRecursiveChildren(user), user);
  148. }
  149. /// <summary>
  150. /// Takes a list of items and returns the ones that are recently added
  151. /// </summary>
  152. private static IEnumerable<BaseItem> GetRecentlyAddedItems(IEnumerable<BaseItem> itemSet, User user)
  153. {
  154. return itemSet.Where(i => !(i.IsFolder) && i.IsRecentlyAdded(user));
  155. }
  156. /// <summary>
  157. /// Takes a list of items and returns the ones that are recently added and unplayed
  158. /// </summary>
  159. private static IEnumerable<BaseItem> GetRecentlyAddedUnplayedItems(IEnumerable<BaseItem> itemSet, User user)
  160. {
  161. return GetRecentlyAddedItems(itemSet, user).Where(i =>
  162. {
  163. var userdata = i.GetUserData(user, false);
  164. return userdata == null || userdata.PlayCount == 0;
  165. });
  166. }
  167. /// <summary>
  168. /// Takes a list of items and returns the ones that are in progress
  169. /// </summary>
  170. private static IEnumerable<BaseItem> GetInProgressItems(IEnumerable<BaseItem> itemSet, User user)
  171. {
  172. return itemSet.Where(i =>
  173. {
  174. if (i.IsFolder)
  175. {
  176. return false;
  177. }
  178. var userdata = i.GetUserData(user, false);
  179. return userdata != null && userdata.PlaybackPositionTicks > 0;
  180. });
  181. }
  182. /// <summary>
  183. /// Gets the total played percentage for a set of items
  184. /// </summary>
  185. private static decimal GetPlayedPercentage(IEnumerable<BaseItem> itemSet, User user)
  186. {
  187. itemSet = itemSet.Where(i => !(i.IsFolder));
  188. decimal totalPercent = 0;
  189. int count = 0;
  190. foreach (BaseItem item in itemSet)
  191. {
  192. count++;
  193. UserItemData data = item.GetUserData(user, false);
  194. if (data == null)
  195. {
  196. continue;
  197. }
  198. if (data.PlayCount > 0)
  199. {
  200. totalPercent += 100;
  201. }
  202. else if (data.PlaybackPositionTicks > 0 && item.RunTimeTicks.HasValue)
  203. {
  204. decimal itemPercent = data.PlaybackPositionTicks;
  205. itemPercent /= item.RunTimeTicks.Value;
  206. totalPercent += itemPercent;
  207. }
  208. }
  209. if (count == 0)
  210. {
  211. return 0;
  212. }
  213. return totalPercent / count;
  214. }
  215. /// <summary>
  216. /// Marks the item as either played or unplayed
  217. /// </summary>
  218. public override void SetPlayedStatus(User user, bool wasPlayed)
  219. {
  220. base.SetPlayedStatus(user, wasPlayed);
  221. // Now sweep through recursively and update status
  222. foreach (BaseItem item in GetParentalAllowedChildren(user))
  223. {
  224. item.SetPlayedStatus(user, wasPlayed);
  225. }
  226. }
  227. /// <summary>
  228. /// Finds an item by ID, recursively
  229. /// </summary>
  230. public override BaseItem FindItemById(Guid id)
  231. {
  232. var result = base.FindItemById(id);
  233. if (result != null)
  234. {
  235. return result;
  236. }
  237. foreach (BaseItem item in Children)
  238. {
  239. result = item.FindItemById(id);
  240. if (result != null)
  241. {
  242. return result;
  243. }
  244. }
  245. return null;
  246. }
  247. /// <summary>
  248. /// Finds an item by path, recursively
  249. /// </summary>
  250. public BaseItem FindByPath(string path)
  251. {
  252. if (Path.Equals(path, StringComparison.OrdinalIgnoreCase))
  253. {
  254. return this;
  255. }
  256. foreach (BaseItem item in Children)
  257. {
  258. var folder = item as Folder;
  259. if (folder != null)
  260. {
  261. var foundItem = folder.FindByPath(path);
  262. if (foundItem != null)
  263. {
  264. return foundItem;
  265. }
  266. }
  267. else if (item.Path.Equals(path, StringComparison.OrdinalIgnoreCase))
  268. {
  269. return item;
  270. }
  271. }
  272. return null;
  273. }
  274. }
  275. }