Season.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. using MediaBrowser.Controller.Library;
  2. using MediaBrowser.Controller.Localization;
  3. using MediaBrowser.Controller.Providers;
  4. using MediaBrowser.Model.Configuration;
  5. using MediaBrowser.Model.Entities;
  6. using MediaBrowser.Model.Querying;
  7. using System.Collections.Generic;
  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, IHasSeries, IHasLookupInfo<SeasonInfo>
  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. [IgnoreDataMember]
  30. public override bool SupportsAddingToPlaylist
  31. {
  32. get { return true; }
  33. }
  34. [IgnoreDataMember]
  35. public override bool IsPreSorted
  36. {
  37. get
  38. {
  39. return true;
  40. }
  41. }
  42. [IgnoreDataMember]
  43. public override BaseItem DisplayParent
  44. {
  45. get { return Series ?? Parent; }
  46. }
  47. /// <summary>
  48. /// We want to group into our Series
  49. /// </summary>
  50. /// <value><c>true</c> if [group in index]; otherwise, <c>false</c>.</value>
  51. [IgnoreDataMember]
  52. public override bool GroupInIndex
  53. {
  54. get
  55. {
  56. return true;
  57. }
  58. }
  59. /// <summary>
  60. /// Override this to return the folder that should be used to construct a container
  61. /// for this item in an index. GroupInIndex should be true as well.
  62. /// </summary>
  63. /// <value>The index container.</value>
  64. [IgnoreDataMember]
  65. public override Folder IndexContainer
  66. {
  67. get
  68. {
  69. return Series;
  70. }
  71. }
  72. // Genre, Rating and Stuido will all be the same
  73. protected override IEnumerable<string> GetIndexByOptions()
  74. {
  75. return new List<string> {
  76. {LocalizedStrings.Instance.GetString("NoneDispPref")},
  77. {LocalizedStrings.Instance.GetString("PerformerDispPref")},
  78. {LocalizedStrings.Instance.GetString("DirectorDispPref")},
  79. {LocalizedStrings.Instance.GetString("YearDispPref")},
  80. };
  81. }
  82. /// <summary>
  83. /// Gets the user data key.
  84. /// </summary>
  85. /// <returns>System.String.</returns>
  86. public override string GetUserDataKey()
  87. {
  88. if (Series != null)
  89. {
  90. var seasonNo = IndexNumber ?? 0;
  91. return Series.GetUserDataKey() + seasonNo.ToString("000");
  92. }
  93. return base.GetUserDataKey();
  94. }
  95. /// <summary>
  96. /// The _series
  97. /// </summary>
  98. private Series _series;
  99. /// <summary>
  100. /// This Episode's Series Instance
  101. /// </summary>
  102. /// <value>The series.</value>
  103. [IgnoreDataMember]
  104. public Series Series
  105. {
  106. get { return _series ?? (_series = FindParent<Series>()); }
  107. }
  108. [IgnoreDataMember]
  109. public string SeriesPath
  110. {
  111. get
  112. {
  113. var series = Series;
  114. if (series != null)
  115. {
  116. return series.Path;
  117. }
  118. return System.IO.Path.GetDirectoryName(Path);
  119. }
  120. }
  121. /// <summary>
  122. /// Our rating comes from our series
  123. /// </summary>
  124. [IgnoreDataMember]
  125. public override string OfficialRatingForComparison
  126. {
  127. get
  128. {
  129. var series = Series;
  130. return series != null ? series.OfficialRatingForComparison : base.OfficialRatingForComparison;
  131. }
  132. }
  133. /// <summary>
  134. /// Creates the name of the sort.
  135. /// </summary>
  136. /// <returns>System.String.</returns>
  137. protected override string CreateSortName()
  138. {
  139. return IndexNumber != null ? IndexNumber.Value.ToString("0000") : Name;
  140. }
  141. private IEnumerable<Episode> GetEpisodes()
  142. {
  143. var series = Series;
  144. if (series != null && series.ContainsEpisodesWithoutSeasonFolders)
  145. {
  146. var seasonNumber = IndexNumber;
  147. if (seasonNumber.HasValue)
  148. {
  149. return series.RecursiveChildren.OfType<Episode>()
  150. .Where(i => i.ParentIndexNumber.HasValue && i.ParentIndexNumber.Value == seasonNumber.Value);
  151. }
  152. }
  153. return Children.OfType<Episode>();
  154. }
  155. [IgnoreDataMember]
  156. public bool IsMissingSeason
  157. {
  158. get { return LocationType == LocationType.Virtual && GetEpisodes().All(i => i.IsMissingEpisode); }
  159. }
  160. [IgnoreDataMember]
  161. public bool IsUnaired
  162. {
  163. get { return GetEpisodes().All(i => i.IsUnaired); }
  164. }
  165. [IgnoreDataMember]
  166. public bool IsVirtualUnaired
  167. {
  168. get { return LocationType == LocationType.Virtual && IsUnaired; }
  169. }
  170. [IgnoreDataMember]
  171. public bool IsMissingOrVirtualUnaired
  172. {
  173. get { return LocationType == LocationType.Virtual && GetEpisodes().All(i => i.IsVirtualUnaired || i.IsMissingEpisode); }
  174. }
  175. [IgnoreDataMember]
  176. public bool IsSpecialSeason
  177. {
  178. get { return (IndexNumber ?? -1) == 0; }
  179. }
  180. /// <summary>
  181. /// Gets the episodes.
  182. /// </summary>
  183. /// <param name="user">The user.</param>
  184. /// <returns>IEnumerable{Episode}.</returns>
  185. public IEnumerable<Episode> GetEpisodes(User user)
  186. {
  187. var config = user.Configuration;
  188. return GetEpisodes(user, config.DisplayMissingEpisodes, config.DisplayUnairedEpisodes);
  189. }
  190. public IEnumerable<Episode> GetEpisodes(User user, bool includeMissingEpisodes, bool includeVirtualUnairedEpisodes)
  191. {
  192. var episodes = GetRecursiveChildren(user)
  193. .OfType<Episode>();
  194. if (IndexNumber.HasValue)
  195. {
  196. var series = Series;
  197. if (series != null)
  198. {
  199. return series.GetEpisodes(user, IndexNumber.Value, includeMissingEpisodes, includeVirtualUnairedEpisodes, episodes);
  200. }
  201. }
  202. if (!includeMissingEpisodes)
  203. {
  204. episodes = episodes.Where(i => !i.IsMissingEpisode);
  205. }
  206. if (!includeVirtualUnairedEpisodes)
  207. {
  208. episodes = episodes.Where(i => !i.IsVirtualUnaired);
  209. }
  210. return LibraryManager
  211. .Sort(episodes, user, new[] { ItemSortBy.SortName }, SortOrder.Ascending)
  212. .Cast<Episode>();
  213. }
  214. public override IEnumerable<BaseItem> GetChildren(User user, bool includeLinkedChildren)
  215. {
  216. return GetEpisodes(user);
  217. }
  218. protected override bool GetBlockUnratedValue(UserConfiguration config)
  219. {
  220. // Don't block. Let either the entire series rating or episode rating determine it
  221. return false;
  222. }
  223. [IgnoreDataMember]
  224. public string SeriesName
  225. {
  226. get
  227. {
  228. var series = Series;
  229. return series == null ? null : series.Name;
  230. }
  231. }
  232. /// <summary>
  233. /// Gets the lookup information.
  234. /// </summary>
  235. /// <returns>SeasonInfo.</returns>
  236. public SeasonInfo GetLookupInfo()
  237. {
  238. var id = GetItemLookupInfo<SeasonInfo>();
  239. var series = Series;
  240. if (series != null)
  241. {
  242. id.SeriesProviderIds = series.ProviderIds;
  243. id.AnimeSeriesIndex = series.AnimeSeriesIndex;
  244. }
  245. return id;
  246. }
  247. /// <summary>
  248. /// This is called before any metadata refresh and returns true or false indicating if changes were made
  249. /// </summary>
  250. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  251. public override bool BeforeMetadataRefresh()
  252. {
  253. var hasChanges = base.BeforeMetadataRefresh();
  254. var locationType = LocationType;
  255. if (locationType == LocationType.FileSystem || locationType == LocationType.Offline)
  256. {
  257. if (!IndexNumber.HasValue && !string.IsNullOrEmpty(Path))
  258. {
  259. IndexNumber = IndexNumber ?? LibraryManager.GetSeasonNumberFromPath(Path);
  260. // If a change was made record it
  261. if (IndexNumber.HasValue)
  262. {
  263. hasChanges = true;
  264. }
  265. }
  266. }
  267. return hasChanges;
  268. }
  269. }
  270. }