Season.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. using MediaBrowser.Controller.Library;
  2. using MediaBrowser.Controller.Localization;
  3. using MediaBrowser.Model.Entities;
  4. using MediaBrowser.Model.Querying;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Runtime.Serialization;
  10. namespace MediaBrowser.Controller.Entities.TV
  11. {
  12. /// <summary>
  13. /// Class Season
  14. /// </summary>
  15. public class Season : Folder
  16. {
  17. /// <summary>
  18. /// Seasons are just containers
  19. /// </summary>
  20. /// <value><c>true</c> if [include in index]; otherwise, <c>false</c>.</value>
  21. [IgnoreDataMember]
  22. public override bool IncludeInIndex
  23. {
  24. get
  25. {
  26. return false;
  27. }
  28. }
  29. /// <summary>
  30. /// We want to group into our Series
  31. /// </summary>
  32. /// <value><c>true</c> if [group in index]; otherwise, <c>false</c>.</value>
  33. [IgnoreDataMember]
  34. public override bool GroupInIndex
  35. {
  36. get
  37. {
  38. return true;
  39. }
  40. }
  41. /// <summary>
  42. /// Override this to return the folder that should be used to construct a container
  43. /// for this item in an index. GroupInIndex should be true as well.
  44. /// </summary>
  45. /// <value>The index container.</value>
  46. [IgnoreDataMember]
  47. public override Folder IndexContainer
  48. {
  49. get
  50. {
  51. return Series;
  52. }
  53. }
  54. // Genre, Rating and Stuido will all be the same
  55. protected override IEnumerable<string> GetIndexByOptions()
  56. {
  57. return new List<string> {
  58. {LocalizedStrings.Instance.GetString("NoneDispPref")},
  59. {LocalizedStrings.Instance.GetString("PerformerDispPref")},
  60. {LocalizedStrings.Instance.GetString("DirectorDispPref")},
  61. {LocalizedStrings.Instance.GetString("YearDispPref")},
  62. };
  63. }
  64. /// <summary>
  65. /// Gets the user data key.
  66. /// </summary>
  67. /// <returns>System.String.</returns>
  68. public override string GetUserDataKey()
  69. {
  70. if (Series != null)
  71. {
  72. var seasonNo = IndexNumber ?? 0;
  73. return Series.GetUserDataKey() + seasonNo.ToString("000");
  74. }
  75. return base.GetUserDataKey();
  76. }
  77. /// <summary>
  78. /// The _series
  79. /// </summary>
  80. private Series _series;
  81. /// <summary>
  82. /// This Episode's Series Instance
  83. /// </summary>
  84. /// <value>The series.</value>
  85. [IgnoreDataMember]
  86. public Series Series
  87. {
  88. get { return _series ?? (_series = FindParent<Series>()); }
  89. }
  90. [IgnoreDataMember]
  91. public string SeriesPath
  92. {
  93. get
  94. {
  95. var series = Series;
  96. if (series != null)
  97. {
  98. return series.Path;
  99. }
  100. return System.IO.Path.GetDirectoryName(Path);
  101. }
  102. }
  103. /// <summary>
  104. /// Our rating comes from our series
  105. /// </summary>
  106. [IgnoreDataMember]
  107. public override string OfficialRatingForComparison
  108. {
  109. get { return Series != null ? Series.OfficialRatingForComparison : base.OfficialRatingForComparison; }
  110. }
  111. /// <summary>
  112. /// Our rating comes from our series
  113. /// </summary>
  114. [IgnoreDataMember]
  115. public override string CustomRatingForComparison
  116. {
  117. get { return Series != null ? Series.CustomRatingForComparison : base.CustomRatingForComparison; }
  118. }
  119. /// <summary>
  120. /// Add files from the metadata folder to ResolveArgs
  121. /// </summary>
  122. /// <param name="args">The args.</param>
  123. public static void AddMetadataFiles(ItemResolveArgs args)
  124. {
  125. var folder = args.GetFileSystemEntryByName("metadata");
  126. if (folder != null)
  127. {
  128. args.AddMetadataFiles(new DirectoryInfo(folder.FullName).EnumerateFiles());
  129. }
  130. }
  131. /// <summary>
  132. /// Creates ResolveArgs on demand
  133. /// </summary>
  134. /// <param name="pathInfo">The path info.</param>
  135. /// <returns>ItemResolveArgs.</returns>
  136. protected internal override ItemResolveArgs CreateResolveArgs(FileSystemInfo pathInfo = null)
  137. {
  138. var args = base.CreateResolveArgs(pathInfo);
  139. AddMetadataFiles(args);
  140. return args;
  141. }
  142. /// <summary>
  143. /// Creates the name of the sort.
  144. /// </summary>
  145. /// <returns>System.String.</returns>
  146. protected override string CreateSortName()
  147. {
  148. return IndexNumber != null ? IndexNumber.Value.ToString("0000") : Name;
  149. }
  150. private IEnumerable<Episode> GetEpisodes()
  151. {
  152. var series = Series;
  153. if (series != null && series.ContainsEpisodesWithoutSeasonFolders)
  154. {
  155. var seasonNumber = IndexNumber;
  156. if (seasonNumber.HasValue)
  157. {
  158. return series.RecursiveChildren.OfType<Episode>()
  159. .Where(i => i.ParentIndexNumber.HasValue && i.ParentIndexNumber.Value == seasonNumber.Value);
  160. }
  161. }
  162. return Children.OfType<Episode>();
  163. }
  164. [IgnoreDataMember]
  165. public bool IsMissingSeason
  166. {
  167. get { return LocationType == LocationType.Virtual && GetEpisodes().All(i => i.IsMissingEpisode); }
  168. }
  169. [IgnoreDataMember]
  170. public bool IsUnaired
  171. {
  172. get { return GetEpisodes().All(i => i.IsUnaired); }
  173. }
  174. [IgnoreDataMember]
  175. public bool IsVirtualUnaired
  176. {
  177. get { return LocationType == LocationType.Virtual && IsUnaired; }
  178. }
  179. [IgnoreDataMember]
  180. public bool IsMissingOrVirtualUnaired
  181. {
  182. get { return LocationType == LocationType.Virtual && GetEpisodes().All(i => i.IsVirtualUnaired || i.IsMissingEpisode); }
  183. }
  184. [IgnoreDataMember]
  185. public bool IsSpecialSeason
  186. {
  187. get { return (IndexNumber ?? -1) == 0; }
  188. }
  189. /// <summary>
  190. /// Gets the episodes.
  191. /// </summary>
  192. /// <param name="user">The user.</param>
  193. /// <returns>IEnumerable{Episode}.</returns>
  194. public IEnumerable<Episode> GetEpisodes(User user)
  195. {
  196. var config = user.Configuration;
  197. return GetEpisodes(user, config.DisplayMissingEpisodes, config.DisplayUnairedEpisodes);
  198. }
  199. public IEnumerable<Episode> GetEpisodes(User user, bool includeMissingEpisodes, bool includeVirtualUnairedEpisodes)
  200. {
  201. if (IndexNumber.HasValue)
  202. {
  203. var series = Series;
  204. if (series != null)
  205. {
  206. return series.GetEpisodes(user, IndexNumber.Value, includeMissingEpisodes, includeVirtualUnairedEpisodes);
  207. }
  208. }
  209. var episodes = GetRecursiveChildren(user)
  210. .OfType<Episode>();
  211. if (!includeMissingEpisodes)
  212. {
  213. episodes = episodes.Where(i => !i.IsMissingEpisode);
  214. }
  215. if (!includeVirtualUnairedEpisodes)
  216. {
  217. episodes = episodes.Where(i => !i.IsVirtualUnaired);
  218. }
  219. return LibraryManager
  220. .Sort(episodes, user, new[] { ItemSortBy.SortName }, SortOrder.Ascending)
  221. .Cast<Episode>();
  222. }
  223. public override IEnumerable<BaseItem> GetChildren(User user, bool includeLinkedChildren)
  224. {
  225. return GetEpisodes(user);
  226. }
  227. }
  228. }