BaseItem.cs 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Controller.Channels;
  3. using MediaBrowser.Controller.Configuration;
  4. using MediaBrowser.Controller.Dto;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Controller.Persistence;
  7. using MediaBrowser.Controller.Providers;
  8. using MediaBrowser.Model.Configuration;
  9. using MediaBrowser.Model.Dto;
  10. using MediaBrowser.Model.Entities;
  11. using MediaBrowser.Model.Library;
  12. using MediaBrowser.Model.Users;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Globalization;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Threading;
  19. using System.Threading.Tasks;
  20. using MediaBrowser.Controller.Extensions;
  21. using MediaBrowser.Controller.Sorting;
  22. using MediaBrowser.Model.Extensions;
  23. using MediaBrowser.Model.Globalization;
  24. using MediaBrowser.Model.IO;
  25. using MediaBrowser.Model.LiveTv;
  26. using MediaBrowser.Model.Providers;
  27. using MediaBrowser.Model.Serialization;
  28. using MediaBrowser.Model.MediaInfo;
  29. using Microsoft.Extensions.Logging;
  30. namespace MediaBrowser.Controller.Entities
  31. {
  32. /// <summary>
  33. /// Class BaseItem
  34. /// </summary>
  35. public abstract class BaseItem : IHasProviderIds, IHasLookupInfo<ItemLookupInfo>
  36. {
  37. protected static MetadataFields[] EmptyMetadataFieldsArray = Array.Empty<MetadataFields>();
  38. protected static MediaUrl[] EmptyMediaUrlArray = Array.Empty<MediaUrl>();
  39. protected static ItemImageInfo[] EmptyItemImageInfoArray = Array.Empty<ItemImageInfo>();
  40. public static readonly LinkedChild[] EmptyLinkedChildArray = Array.Empty<LinkedChild>();
  41. protected BaseItem()
  42. {
  43. ThemeSongIds = Array.Empty<Guid>();
  44. ThemeVideoIds = Array.Empty<Guid>();
  45. Tags = Array.Empty<string>();
  46. Genres = Array.Empty<string>();
  47. Studios = Array.Empty<string>();
  48. ProviderIds = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  49. LockedFields = EmptyMetadataFieldsArray;
  50. ImageInfos = EmptyItemImageInfoArray;
  51. ProductionLocations = Array.Empty<string>();
  52. RemoteTrailers = Array.Empty<MediaUrl>();
  53. ExtraIds = Array.Empty<Guid>();
  54. }
  55. public static readonly char[] SlugReplaceChars = { '?', '/', '&' };
  56. public static char SlugChar = '-';
  57. /// <summary>
  58. /// The supported image extensions
  59. /// </summary>
  60. public static readonly string[] SupportedImageExtensions = { ".png", ".jpg", ".jpeg", ".tbn", ".gif" };
  61. /// <summary>
  62. /// The trailer folder name
  63. /// </summary>
  64. public static string TrailerFolderName = "trailers";
  65. public static string ThemeSongsFolderName = "theme-music";
  66. public static string ThemeSongFilename = "theme";
  67. public static string ThemeVideosFolderName = "backdrops";
  68. [IgnoreDataMember]
  69. public Guid[] ThemeSongIds { get; set; }
  70. [IgnoreDataMember]
  71. public Guid[] ThemeVideoIds { get; set; }
  72. [IgnoreDataMember]
  73. public string PreferredMetadataCountryCode { get; set; }
  74. [IgnoreDataMember]
  75. public string PreferredMetadataLanguage { get; set; }
  76. public long? Size { get; set; }
  77. public string Container { get; set; }
  78. [IgnoreDataMember]
  79. public string Tagline { get; set; }
  80. [IgnoreDataMember]
  81. public virtual ItemImageInfo[] ImageInfos { get; set; }
  82. [IgnoreDataMember]
  83. public bool IsVirtualItem { get; set; }
  84. /// <summary>
  85. /// Gets or sets the album.
  86. /// </summary>
  87. /// <value>The album.</value>
  88. [IgnoreDataMember]
  89. public string Album { get; set; }
  90. /// <summary>
  91. /// Gets or sets the channel identifier.
  92. /// </summary>
  93. /// <value>The channel identifier.</value>
  94. [IgnoreDataMember]
  95. public Guid ChannelId { get; set; }
  96. [IgnoreDataMember]
  97. public virtual bool SupportsAddingToPlaylist
  98. {
  99. get
  100. {
  101. return false;
  102. }
  103. }
  104. [IgnoreDataMember]
  105. public virtual bool AlwaysScanInternalMetadataPath
  106. {
  107. get { return false; }
  108. }
  109. /// <summary>
  110. /// Gets a value indicating whether this instance is in mixed folder.
  111. /// </summary>
  112. /// <value><c>true</c> if this instance is in mixed folder; otherwise, <c>false</c>.</value>
  113. [IgnoreDataMember]
  114. public bool IsInMixedFolder { get; set; }
  115. [IgnoreDataMember]
  116. public virtual bool SupportsPlayedStatus
  117. {
  118. get
  119. {
  120. return false;
  121. }
  122. }
  123. [IgnoreDataMember]
  124. public virtual bool SupportsPositionTicksResume
  125. {
  126. get
  127. {
  128. return false;
  129. }
  130. }
  131. [IgnoreDataMember]
  132. public virtual bool SupportsRemoteImageDownloading
  133. {
  134. get
  135. {
  136. return true;
  137. }
  138. }
  139. private string _name;
  140. /// <summary>
  141. /// Gets or sets the name.
  142. /// </summary>
  143. /// <value>The name.</value>
  144. [IgnoreDataMember]
  145. public virtual string Name
  146. {
  147. get
  148. {
  149. return _name;
  150. }
  151. set
  152. {
  153. _name = value;
  154. // lazy load this again
  155. _sortName = null;
  156. }
  157. }
  158. [IgnoreDataMember]
  159. public bool IsUnaired
  160. {
  161. get { return PremiereDate.HasValue && PremiereDate.Value.ToLocalTime().Date >= DateTime.Now.Date; }
  162. }
  163. [IgnoreDataMember]
  164. public int? TotalBitrate { get; set; }
  165. [IgnoreDataMember]
  166. public ExtraType? ExtraType { get; set; }
  167. [IgnoreDataMember]
  168. public bool IsThemeMedia
  169. {
  170. get
  171. {
  172. return ExtraType.HasValue && (ExtraType.Value == Model.Entities.ExtraType.ThemeSong || ExtraType.Value == Model.Entities.ExtraType.ThemeVideo);
  173. }
  174. }
  175. [IgnoreDataMember]
  176. public string OriginalTitle { get; set; }
  177. /// <summary>
  178. /// Gets or sets the id.
  179. /// </summary>
  180. /// <value>The id.</value>
  181. [IgnoreDataMember]
  182. public Guid Id { get; set; }
  183. [IgnoreDataMember]
  184. public Guid OwnerId { get; set; }
  185. /// <summary>
  186. /// Gets or sets the audio.
  187. /// </summary>
  188. /// <value>The audio.</value>
  189. [IgnoreDataMember]
  190. public ProgramAudio? Audio { get; set; }
  191. /// <summary>
  192. /// Return the id that should be used to key display prefs for this item.
  193. /// Default is based on the type for everything except actual generic folders.
  194. /// </summary>
  195. /// <value>The display prefs id.</value>
  196. [IgnoreDataMember]
  197. public virtual Guid DisplayPreferencesId
  198. {
  199. get
  200. {
  201. var thisType = GetType();
  202. return thisType == typeof(Folder) ? Id : thisType.FullName.GetMD5();
  203. }
  204. }
  205. /// <summary>
  206. /// Gets or sets the path.
  207. /// </summary>
  208. /// <value>The path.</value>
  209. [IgnoreDataMember]
  210. public virtual string Path { get; set; }
  211. [IgnoreDataMember]
  212. public virtual SourceType SourceType
  213. {
  214. get
  215. {
  216. if (!ChannelId.Equals(Guid.Empty))
  217. {
  218. return SourceType.Channel;
  219. }
  220. return SourceType.Library;
  221. }
  222. }
  223. /// <summary>
  224. /// Returns the folder containing the item.
  225. /// If the item is a folder, it returns the folder itself
  226. /// </summary>
  227. [IgnoreDataMember]
  228. public virtual string ContainingFolderPath
  229. {
  230. get
  231. {
  232. if (IsFolder)
  233. {
  234. return Path;
  235. }
  236. return FileSystem.GetDirectoryName(Path);
  237. }
  238. }
  239. /// <summary>
  240. /// Gets or sets the name of the service.
  241. /// </summary>
  242. /// <value>The name of the service.</value>
  243. [IgnoreDataMember]
  244. public string ServiceName { get; set; }
  245. /// <summary>
  246. /// If this content came from an external service, the id of the content on that service
  247. /// </summary>
  248. [IgnoreDataMember]
  249. public string ExternalId { get; set; }
  250. [IgnoreDataMember]
  251. public string ExternalSeriesId { get; set; }
  252. /// <summary>
  253. /// Gets or sets the etag.
  254. /// </summary>
  255. /// <value>The etag.</value>
  256. [IgnoreDataMember]
  257. public string ExternalEtag { get; set; }
  258. [IgnoreDataMember]
  259. public virtual bool IsHidden
  260. {
  261. get
  262. {
  263. return false;
  264. }
  265. }
  266. public BaseItem GetOwner()
  267. {
  268. var ownerId = OwnerId;
  269. return ownerId.Equals(Guid.Empty) ? null : LibraryManager.GetItemById(ownerId);
  270. }
  271. /// <summary>
  272. /// Gets or sets the type of the location.
  273. /// </summary>
  274. /// <value>The type of the location.</value>
  275. [IgnoreDataMember]
  276. public virtual LocationType LocationType
  277. {
  278. get
  279. {
  280. //if (IsOffline)
  281. //{
  282. // return LocationType.Offline;
  283. //}
  284. var path = Path;
  285. if (string.IsNullOrEmpty(path))
  286. {
  287. if (SourceType == SourceType.Channel)
  288. {
  289. return LocationType.Remote;
  290. }
  291. return LocationType.Virtual;
  292. }
  293. return FileSystem.IsPathFile(path) ? LocationType.FileSystem : LocationType.Remote;
  294. }
  295. }
  296. [IgnoreDataMember]
  297. public MediaProtocol? PathProtocol
  298. {
  299. get
  300. {
  301. var path = Path;
  302. if (string.IsNullOrEmpty(path))
  303. {
  304. return null;
  305. }
  306. return MediaSourceManager.GetPathProtocol(path);
  307. }
  308. }
  309. public bool IsPathProtocol(MediaProtocol protocol)
  310. {
  311. var current = PathProtocol;
  312. return current.HasValue && current.Value == protocol;
  313. }
  314. [IgnoreDataMember]
  315. public bool IsFileProtocol
  316. {
  317. get
  318. {
  319. return IsPathProtocol(MediaProtocol.File);
  320. }
  321. }
  322. [IgnoreDataMember]
  323. public bool HasPathProtocol
  324. {
  325. get
  326. {
  327. return PathProtocol.HasValue;
  328. }
  329. }
  330. [IgnoreDataMember]
  331. public virtual bool SupportsLocalMetadata
  332. {
  333. get
  334. {
  335. if (SourceType == SourceType.Channel)
  336. {
  337. return false;
  338. }
  339. return IsFileProtocol;
  340. }
  341. }
  342. [IgnoreDataMember]
  343. public virtual string FileNameWithoutExtension
  344. {
  345. get
  346. {
  347. if (IsFileProtocol)
  348. {
  349. return System.IO.Path.GetFileNameWithoutExtension(Path);
  350. }
  351. return null;
  352. }
  353. }
  354. [IgnoreDataMember]
  355. public virtual bool EnableAlphaNumericSorting
  356. {
  357. get
  358. {
  359. return true;
  360. }
  361. }
  362. private List<Tuple<StringBuilder, bool>> GetSortChunks(string s1)
  363. {
  364. var list = new List<Tuple<StringBuilder, bool>>();
  365. int thisMarker = 0, thisNumericChunk = 0;
  366. while (thisMarker < s1.Length)
  367. {
  368. if (thisMarker >= s1.Length)
  369. {
  370. break;
  371. }
  372. char thisCh = s1[thisMarker];
  373. StringBuilder thisChunk = new StringBuilder();
  374. while ((thisMarker < s1.Length) && (thisChunk.Length == 0 || SortHelper.InChunk(thisCh, thisChunk[0])))
  375. {
  376. thisChunk.Append(thisCh);
  377. thisMarker++;
  378. if (thisMarker < s1.Length)
  379. {
  380. thisCh = s1[thisMarker];
  381. }
  382. }
  383. var isNumeric = thisChunk.Length > 0 && char.IsDigit(thisChunk[0]);
  384. list.Add(new Tuple<StringBuilder, bool>(thisChunk, isNumeric));
  385. }
  386. return list;
  387. }
  388. /// <summary>
  389. /// This is just a helper for convenience
  390. /// </summary>
  391. /// <value>The primary image path.</value>
  392. [IgnoreDataMember]
  393. public string PrimaryImagePath
  394. {
  395. get { return this.GetImagePath(ImageType.Primary); }
  396. }
  397. public bool IsMetadataFetcherEnabled(LibraryOptions libraryOptions, string name)
  398. {
  399. if (SourceType == SourceType.Channel)
  400. {
  401. // hack alert
  402. return !EnableMediaSourceDisplay;
  403. }
  404. var typeOptions = libraryOptions.GetTypeOptions(GetType().Name);
  405. if (typeOptions != null)
  406. {
  407. return typeOptions.MetadataFetchers.Contains(name, StringComparer.OrdinalIgnoreCase);
  408. }
  409. if (!libraryOptions.EnableInternetProviders)
  410. {
  411. return false;
  412. }
  413. var itemConfig = ConfigurationManager.Configuration.MetadataOptions.FirstOrDefault(i => string.Equals(i.ItemType, GetType().Name, StringComparison.OrdinalIgnoreCase));
  414. return itemConfig == null || !itemConfig.DisabledMetadataFetchers.Contains(name, StringComparer.OrdinalIgnoreCase);
  415. }
  416. public bool IsImageFetcherEnabled(LibraryOptions libraryOptions, string name)
  417. {
  418. if (this is Channel)
  419. {
  420. // hack alert
  421. return true;
  422. }
  423. if (SourceType == SourceType.Channel)
  424. {
  425. // hack alert
  426. return !EnableMediaSourceDisplay;
  427. }
  428. var typeOptions = libraryOptions.GetTypeOptions(GetType().Name);
  429. if (typeOptions != null)
  430. {
  431. return typeOptions.ImageFetchers.Contains(name, StringComparer.OrdinalIgnoreCase);
  432. }
  433. if (!libraryOptions.EnableInternetProviders)
  434. {
  435. return false;
  436. }
  437. var itemConfig = ConfigurationManager.Configuration.MetadataOptions.FirstOrDefault(i => string.Equals(i.ItemType, GetType().Name, StringComparison.OrdinalIgnoreCase));
  438. return itemConfig == null || !itemConfig.DisabledImageFetchers.Contains(name, StringComparer.OrdinalIgnoreCase);
  439. }
  440. public virtual bool CanDelete()
  441. {
  442. if (SourceType == SourceType.Channel)
  443. {
  444. return ChannelManager.CanDelete(this);
  445. }
  446. return IsFileProtocol;
  447. }
  448. public virtual bool IsAuthorizedToDelete(User user, List<Folder> allCollectionFolders)
  449. {
  450. if (user.Policy.EnableContentDeletion)
  451. {
  452. return true;
  453. }
  454. var allowed = user.Policy.EnableContentDeletionFromFolders;
  455. if (SourceType == SourceType.Channel)
  456. {
  457. return allowed.Contains(ChannelId.ToString(""), StringComparer.OrdinalIgnoreCase);
  458. }
  459. else
  460. {
  461. var collectionFolders = LibraryManager.GetCollectionFolders(this, allCollectionFolders);
  462. foreach (var folder in collectionFolders)
  463. {
  464. if (allowed.Contains(folder.Id.ToString("N"), StringComparer.OrdinalIgnoreCase))
  465. {
  466. return true;
  467. }
  468. }
  469. }
  470. return false;
  471. }
  472. public bool CanDelete(User user, List<Folder> allCollectionFolders)
  473. {
  474. return CanDelete() && IsAuthorizedToDelete(user, allCollectionFolders);
  475. }
  476. public bool CanDelete(User user)
  477. {
  478. var allCollectionFolders = LibraryManager.GetUserRootFolder().Children.OfType<Folder>().ToList();
  479. return CanDelete(user, allCollectionFolders);
  480. }
  481. public virtual bool CanDownload()
  482. {
  483. return false;
  484. }
  485. public virtual bool IsAuthorizedToDownload(User user)
  486. {
  487. return user.Policy.EnableContentDownloading;
  488. }
  489. public bool CanDownload(User user)
  490. {
  491. return CanDownload() && IsAuthorizedToDownload(user);
  492. }
  493. /// <summary>
  494. /// Gets or sets the date created.
  495. /// </summary>
  496. /// <value>The date created.</value>
  497. [IgnoreDataMember]
  498. public DateTime DateCreated { get; set; }
  499. /// <summary>
  500. /// Gets or sets the date modified.
  501. /// </summary>
  502. /// <value>The date modified.</value>
  503. [IgnoreDataMember]
  504. public DateTime DateModified { get; set; }
  505. [IgnoreDataMember]
  506. public DateTime DateLastSaved { get; set; }
  507. [IgnoreDataMember]
  508. public DateTime DateLastRefreshed { get; set; }
  509. /// <summary>
  510. /// The logger
  511. /// </summary>
  512. public static ILogger Logger { get; set; }
  513. public static ILibraryManager LibraryManager { get; set; }
  514. public static IServerConfigurationManager ConfigurationManager { get; set; }
  515. public static IProviderManager ProviderManager { get; set; }
  516. public static ILocalizationManager LocalizationManager { get; set; }
  517. public static IItemRepository ItemRepository { get; set; }
  518. public static IFileSystem FileSystem { get; set; }
  519. public static IUserDataManager UserDataManager { get; set; }
  520. public static IChannelManager ChannelManager { get; set; }
  521. public static IMediaSourceManager MediaSourceManager { get; set; }
  522. /// <summary>
  523. /// Returns a <see cref="System.String" /> that represents this instance.
  524. /// </summary>
  525. /// <returns>A <see cref="System.String" /> that represents this instance.</returns>
  526. public override string ToString()
  527. {
  528. return Name;
  529. }
  530. [IgnoreDataMember]
  531. public bool IsLocked { get; set; }
  532. /// <summary>
  533. /// Gets or sets the locked fields.
  534. /// </summary>
  535. /// <value>The locked fields.</value>
  536. [IgnoreDataMember]
  537. public MetadataFields[] LockedFields { get; set; }
  538. /// <summary>
  539. /// Gets the type of the media.
  540. /// </summary>
  541. /// <value>The type of the media.</value>
  542. [IgnoreDataMember]
  543. public virtual string MediaType
  544. {
  545. get
  546. {
  547. return null;
  548. }
  549. }
  550. [IgnoreDataMember]
  551. public virtual string[] PhysicalLocations
  552. {
  553. get
  554. {
  555. if (!IsFileProtocol)
  556. {
  557. return new string[] { };
  558. }
  559. return new[] { Path };
  560. }
  561. }
  562. private string _forcedSortName;
  563. /// <summary>
  564. /// Gets or sets the name of the forced sort.
  565. /// </summary>
  566. /// <value>The name of the forced sort.</value>
  567. [IgnoreDataMember]
  568. public string ForcedSortName
  569. {
  570. get { return _forcedSortName; }
  571. set { _forcedSortName = value; _sortName = null; }
  572. }
  573. private string _sortName;
  574. /// <summary>
  575. /// Gets the name of the sort.
  576. /// </summary>
  577. /// <value>The name of the sort.</value>
  578. [IgnoreDataMember]
  579. public string SortName
  580. {
  581. get
  582. {
  583. if (_sortName == null)
  584. {
  585. if (!string.IsNullOrEmpty(ForcedSortName))
  586. {
  587. // Need the ToLower because that's what CreateSortName does
  588. _sortName = ModifySortChunks(ForcedSortName).ToLower();
  589. }
  590. else
  591. {
  592. _sortName = CreateSortName();
  593. }
  594. }
  595. return _sortName;
  596. }
  597. set
  598. {
  599. _sortName = value;
  600. }
  601. }
  602. public string GetInternalMetadataPath()
  603. {
  604. var basePath = ConfigurationManager.ApplicationPaths.InternalMetadataPath;
  605. return GetInternalMetadataPath(basePath);
  606. }
  607. protected virtual string GetInternalMetadataPath(string basePath)
  608. {
  609. if (SourceType == SourceType.Channel)
  610. {
  611. return System.IO.Path.Combine(basePath, "channels", ChannelId.ToString("N"), Id.ToString("N"));
  612. }
  613. var idString = Id.ToString("N");
  614. basePath = System.IO.Path.Combine(basePath, "library");
  615. return System.IO.Path.Combine(basePath, idString.Substring(0, 2), idString);
  616. }
  617. /// <summary>
  618. /// Creates the name of the sort.
  619. /// </summary>
  620. /// <returns>System.String.</returns>
  621. protected virtual string CreateSortName()
  622. {
  623. if (Name == null) return null; //some items may not have name filled in properly
  624. if (!EnableAlphaNumericSorting)
  625. {
  626. return Name.TrimStart();
  627. }
  628. var sortable = Name.Trim().ToLower();
  629. foreach (var removeChar in ConfigurationManager.Configuration.SortRemoveCharacters)
  630. {
  631. sortable = sortable.Replace(removeChar, string.Empty);
  632. }
  633. foreach (var replaceChar in ConfigurationManager.Configuration.SortReplaceCharacters)
  634. {
  635. sortable = sortable.Replace(replaceChar, " ");
  636. }
  637. foreach (var search in ConfigurationManager.Configuration.SortRemoveWords)
  638. {
  639. // Remove from beginning if a space follows
  640. if (sortable.StartsWith(search + " "))
  641. {
  642. sortable = sortable.Remove(0, search.Length + 1);
  643. }
  644. // Remove from middle if surrounded by spaces
  645. sortable = sortable.Replace(" " + search + " ", " ");
  646. // Remove from end if followed by a space
  647. if (sortable.EndsWith(" " + search))
  648. {
  649. sortable = sortable.Remove(sortable.Length - (search.Length + 1));
  650. }
  651. }
  652. return ModifySortChunks(sortable);
  653. }
  654. private string ModifySortChunks(string name)
  655. {
  656. var chunks = GetSortChunks(name);
  657. var builder = new StringBuilder();
  658. foreach (var chunk in chunks)
  659. {
  660. var chunkBuilder = chunk.Item1;
  661. // This chunk is numeric
  662. if (chunk.Item2)
  663. {
  664. while (chunkBuilder.Length < 10)
  665. {
  666. chunkBuilder.Insert(0, '0');
  667. }
  668. }
  669. builder.Append(chunkBuilder);
  670. }
  671. //logger.LogDebug("ModifySortChunks Start: {0} End: {1}", name, builder.ToString());
  672. return builder.ToString().RemoveDiacritics();
  673. }
  674. [IgnoreDataMember]
  675. public bool EnableMediaSourceDisplay
  676. {
  677. get
  678. {
  679. if (SourceType == SourceType.Channel)
  680. {
  681. return ChannelManager.EnableMediaSourceDisplay(this);
  682. }
  683. return true;
  684. }
  685. }
  686. [IgnoreDataMember]
  687. public Guid ParentId { get; set; }
  688. /// <summary>
  689. /// Gets or sets the parent.
  690. /// </summary>
  691. /// <value>The parent.</value>
  692. [IgnoreDataMember]
  693. public Folder Parent
  694. {
  695. get { return GetParent() as Folder; }
  696. set
  697. {
  698. }
  699. }
  700. public void SetParent(Folder parent)
  701. {
  702. ParentId = parent == null ? Guid.Empty : parent.Id;
  703. }
  704. public BaseItem GetParent()
  705. {
  706. var parentId = ParentId;
  707. if (!parentId.Equals(Guid.Empty))
  708. {
  709. return LibraryManager.GetItemById(parentId);
  710. }
  711. return null;
  712. }
  713. public IEnumerable<BaseItem> GetParents()
  714. {
  715. var parent = GetParent();
  716. while (parent != null)
  717. {
  718. yield return parent;
  719. parent = parent.GetParent();
  720. }
  721. }
  722. /// <summary>
  723. /// Finds a parent of a given type
  724. /// </summary>
  725. /// <typeparam name="T"></typeparam>
  726. /// <returns>``0.</returns>
  727. public T FindParent<T>()
  728. where T : Folder
  729. {
  730. foreach (var parent in GetParents())
  731. {
  732. var item = parent as T;
  733. if (item != null)
  734. {
  735. return item;
  736. }
  737. }
  738. return null;
  739. }
  740. [IgnoreDataMember]
  741. public virtual Guid DisplayParentId
  742. {
  743. get
  744. {
  745. var parentId = ParentId;
  746. return parentId;
  747. }
  748. }
  749. [IgnoreDataMember]
  750. public BaseItem DisplayParent
  751. {
  752. get
  753. {
  754. var id = DisplayParentId;
  755. if (id.Equals(Guid.Empty))
  756. {
  757. return null;
  758. }
  759. return LibraryManager.GetItemById(id);
  760. }
  761. }
  762. /// <summary>
  763. /// When the item first debuted. For movies this could be premiere date, episodes would be first aired
  764. /// </summary>
  765. /// <value>The premiere date.</value>
  766. [IgnoreDataMember]
  767. public DateTime? PremiereDate { get; set; }
  768. /// <summary>
  769. /// Gets or sets the end date.
  770. /// </summary>
  771. /// <value>The end date.</value>
  772. [IgnoreDataMember]
  773. public DateTime? EndDate { get; set; }
  774. /// <summary>
  775. /// Gets or sets the official rating.
  776. /// </summary>
  777. /// <value>The official rating.</value>
  778. [IgnoreDataMember]
  779. public string OfficialRating { get; set; }
  780. [IgnoreDataMember]
  781. public int InheritedParentalRatingValue { get; set; }
  782. /// <summary>
  783. /// Gets or sets the critic rating.
  784. /// </summary>
  785. /// <value>The critic rating.</value>
  786. [IgnoreDataMember]
  787. public float? CriticRating { get; set; }
  788. /// <summary>
  789. /// Gets or sets the custom rating.
  790. /// </summary>
  791. /// <value>The custom rating.</value>
  792. [IgnoreDataMember]
  793. public string CustomRating { get; set; }
  794. /// <summary>
  795. /// Gets or sets the overview.
  796. /// </summary>
  797. /// <value>The overview.</value>
  798. [IgnoreDataMember]
  799. public string Overview { get; set; }
  800. /// <summary>
  801. /// Gets or sets the studios.
  802. /// </summary>
  803. /// <value>The studios.</value>
  804. [IgnoreDataMember]
  805. public string[] Studios { get; set; }
  806. /// <summary>
  807. /// Gets or sets the genres.
  808. /// </summary>
  809. /// <value>The genres.</value>
  810. [IgnoreDataMember]
  811. public string[] Genres { get; set; }
  812. /// <summary>
  813. /// Gets or sets the tags.
  814. /// </summary>
  815. /// <value>The tags.</value>
  816. [IgnoreDataMember]
  817. public string[] Tags { get; set; }
  818. [IgnoreDataMember]
  819. public string[] ProductionLocations { get; set; }
  820. /// <summary>
  821. /// Gets or sets the home page URL.
  822. /// </summary>
  823. /// <value>The home page URL.</value>
  824. [IgnoreDataMember]
  825. public string HomePageUrl { get; set; }
  826. /// <summary>
  827. /// Gets or sets the community rating.
  828. /// </summary>
  829. /// <value>The community rating.</value>
  830. [IgnoreDataMember]
  831. public float? CommunityRating { get; set; }
  832. /// <summary>
  833. /// Gets or sets the run time ticks.
  834. /// </summary>
  835. /// <value>The run time ticks.</value>
  836. [IgnoreDataMember]
  837. public long? RunTimeTicks { get; set; }
  838. /// <summary>
  839. /// Gets or sets the production year.
  840. /// </summary>
  841. /// <value>The production year.</value>
  842. [IgnoreDataMember]
  843. public int? ProductionYear { get; set; }
  844. /// <summary>
  845. /// If the item is part of a series, this is it's number in the series.
  846. /// This could be episode number, album track number, etc.
  847. /// </summary>
  848. /// <value>The index number.</value>
  849. [IgnoreDataMember]
  850. public int? IndexNumber { get; set; }
  851. /// <summary>
  852. /// For an episode this could be the season number, or for a song this could be the disc number.
  853. /// </summary>
  854. /// <value>The parent index number.</value>
  855. [IgnoreDataMember]
  856. public int? ParentIndexNumber { get; set; }
  857. [IgnoreDataMember]
  858. public virtual bool HasLocalAlternateVersions
  859. {
  860. get { return false; }
  861. }
  862. [IgnoreDataMember]
  863. public string OfficialRatingForComparison
  864. {
  865. get
  866. {
  867. var officialRating = OfficialRating;
  868. if (!string.IsNullOrEmpty(officialRating))
  869. {
  870. return officialRating;
  871. }
  872. var parent = DisplayParent;
  873. if (parent != null)
  874. {
  875. return parent.OfficialRatingForComparison;
  876. }
  877. return null;
  878. }
  879. }
  880. [IgnoreDataMember]
  881. public string CustomRatingForComparison
  882. {
  883. get
  884. {
  885. var customRating = CustomRating;
  886. if (!string.IsNullOrEmpty(customRating))
  887. {
  888. return customRating;
  889. }
  890. var parent = DisplayParent;
  891. if (parent != null)
  892. {
  893. return parent.CustomRatingForComparison;
  894. }
  895. return null;
  896. }
  897. }
  898. /// <summary>
  899. /// Gets the play access.
  900. /// </summary>
  901. /// <param name="user">The user.</param>
  902. /// <returns>PlayAccess.</returns>
  903. public PlayAccess GetPlayAccess(User user)
  904. {
  905. if (!user.Policy.EnableMediaPlayback)
  906. {
  907. return PlayAccess.None;
  908. }
  909. //if (!user.IsParentalScheduleAllowed())
  910. //{
  911. // return PlayAccess.None;
  912. //}
  913. return PlayAccess.Full;
  914. }
  915. public virtual List<MediaStream> GetMediaStreams()
  916. {
  917. return MediaSourceManager.GetMediaStreams(new MediaStreamQuery
  918. {
  919. ItemId = Id
  920. });
  921. }
  922. protected virtual bool IsActiveRecording()
  923. {
  924. return false;
  925. }
  926. public virtual List<MediaSourceInfo> GetMediaSources(bool enablePathSubstitution)
  927. {
  928. if (SourceType == SourceType.Channel)
  929. {
  930. var sources = ChannelManager.GetStaticMediaSources(this, CancellationToken.None)
  931. .ToList();
  932. if (sources.Count > 0)
  933. {
  934. return sources;
  935. }
  936. }
  937. var list = GetAllItemsForMediaSources();
  938. var result = list.Select(i => GetVersionInfo(enablePathSubstitution, i.Item1, i.Item2)).ToList();
  939. if (IsActiveRecording())
  940. {
  941. foreach (var mediaSource in result)
  942. {
  943. mediaSource.Type = MediaSourceType.Placeholder;
  944. }
  945. }
  946. return result.OrderBy(i =>
  947. {
  948. if (i.VideoType == VideoType.VideoFile)
  949. {
  950. return 0;
  951. }
  952. return 1;
  953. }).ThenBy(i => i.Video3DFormat.HasValue ? 1 : 0)
  954. .ThenByDescending(i =>
  955. {
  956. var stream = i.VideoStream;
  957. return stream == null || stream.Width == null ? 0 : stream.Width.Value;
  958. })
  959. .ToList();
  960. }
  961. protected virtual List<Tuple<BaseItem, MediaSourceType>> GetAllItemsForMediaSources()
  962. {
  963. return new List<Tuple<BaseItem, MediaSourceType>>();
  964. }
  965. private MediaSourceInfo GetVersionInfo(bool enablePathSubstitution, BaseItem item, MediaSourceType type)
  966. {
  967. if (item == null)
  968. {
  969. throw new ArgumentNullException("media");
  970. }
  971. var protocol = item.PathProtocol;
  972. var info = new MediaSourceInfo
  973. {
  974. Id = item.Id.ToString("N"),
  975. Protocol = protocol ?? MediaProtocol.File,
  976. MediaStreams = MediaSourceManager.GetMediaStreams(item.Id),
  977. Name = GetMediaSourceName(item),
  978. Path = enablePathSubstitution ? GetMappedPath(item, item.Path, protocol) : item.Path,
  979. RunTimeTicks = item.RunTimeTicks,
  980. Container = item.Container,
  981. Size = item.Size,
  982. Type = type
  983. };
  984. if (string.IsNullOrEmpty(info.Path))
  985. {
  986. info.Type = MediaSourceType.Placeholder;
  987. }
  988. if (info.Protocol == MediaProtocol.File)
  989. {
  990. info.ETag = item.DateModified.Ticks.ToString(CultureInfo.InvariantCulture).GetMD5().ToString("N");
  991. }
  992. var video = item as Video;
  993. if (video != null)
  994. {
  995. info.IsoType = video.IsoType;
  996. info.VideoType = video.VideoType;
  997. info.Video3DFormat = video.Video3DFormat;
  998. info.Timestamp = video.Timestamp;
  999. if (video.IsShortcut)
  1000. {
  1001. info.IsRemote = true;
  1002. info.Path = video.ShortcutPath;
  1003. info.Protocol = MediaSourceManager.GetPathProtocol(info.Path);
  1004. }
  1005. if (string.IsNullOrEmpty(info.Container))
  1006. {
  1007. if (video.VideoType == VideoType.VideoFile || video.VideoType == VideoType.Iso)
  1008. {
  1009. if (protocol.HasValue && protocol.Value == MediaProtocol.File)
  1010. {
  1011. info.Container = System.IO.Path.GetExtension(item.Path).TrimStart('.');
  1012. }
  1013. }
  1014. }
  1015. }
  1016. if (string.IsNullOrEmpty(info.Container))
  1017. {
  1018. if (protocol.HasValue && protocol.Value == MediaProtocol.File)
  1019. {
  1020. info.Container = System.IO.Path.GetExtension(item.Path).TrimStart('.');
  1021. }
  1022. }
  1023. if (info.SupportsDirectStream && !string.IsNullOrEmpty(info.Path))
  1024. {
  1025. info.SupportsDirectStream = MediaSourceManager.SupportsDirectStream(info.Path, info.Protocol);
  1026. }
  1027. if (video != null && video.VideoType != VideoType.VideoFile)
  1028. {
  1029. info.SupportsDirectStream = false;
  1030. }
  1031. info.Bitrate = item.TotalBitrate;
  1032. info.InferTotalBitrate();
  1033. return info;
  1034. }
  1035. private string GetMediaSourceName(BaseItem item)
  1036. {
  1037. var terms = new List<string>();
  1038. var path = item.Path;
  1039. if (item.IsFileProtocol && !string.IsNullOrEmpty(path))
  1040. {
  1041. if (HasLocalAlternateVersions)
  1042. {
  1043. var displayName = System.IO.Path.GetFileNameWithoutExtension(path)
  1044. .Replace(System.IO.Path.GetFileName(ContainingFolderPath), string.Empty, StringComparison.OrdinalIgnoreCase)
  1045. .TrimStart(new char[] { ' ', '-' });
  1046. if (!string.IsNullOrEmpty(displayName))
  1047. {
  1048. terms.Add(displayName);
  1049. }
  1050. }
  1051. if (terms.Count == 0)
  1052. {
  1053. var displayName = System.IO.Path.GetFileNameWithoutExtension(path);
  1054. terms.Add(displayName);
  1055. }
  1056. }
  1057. if (terms.Count == 0)
  1058. {
  1059. terms.Add(item.Name);
  1060. }
  1061. var video = item as Video;
  1062. if (video != null)
  1063. {
  1064. if (video.Video3DFormat.HasValue)
  1065. {
  1066. terms.Add("3D");
  1067. }
  1068. if (video.VideoType == VideoType.BluRay)
  1069. {
  1070. terms.Add("Bluray");
  1071. }
  1072. else if (video.VideoType == VideoType.Dvd)
  1073. {
  1074. terms.Add("DVD");
  1075. }
  1076. else if (video.VideoType == VideoType.Iso)
  1077. {
  1078. if (video.IsoType.HasValue)
  1079. {
  1080. if (video.IsoType.Value == Model.Entities.IsoType.BluRay)
  1081. {
  1082. terms.Add("Bluray");
  1083. }
  1084. else if (video.IsoType.Value == Model.Entities.IsoType.Dvd)
  1085. {
  1086. terms.Add("DVD");
  1087. }
  1088. }
  1089. else
  1090. {
  1091. terms.Add("ISO");
  1092. }
  1093. }
  1094. }
  1095. return string.Join("/", terms.ToArray());
  1096. }
  1097. /// <summary>
  1098. /// Loads the theme songs.
  1099. /// </summary>
  1100. /// <returns>List{Audio.Audio}.</returns>
  1101. private static Audio.Audio[] LoadThemeSongs(List<FileSystemMetadata> fileSystemChildren, IDirectoryService directoryService)
  1102. {
  1103. var files = fileSystemChildren.Where(i => i.IsDirectory)
  1104. .Where(i => string.Equals(i.Name, ThemeSongsFolderName, StringComparison.OrdinalIgnoreCase))
  1105. .SelectMany(i => FileSystem.GetFiles(i.FullName))
  1106. .ToList();
  1107. // Support plex/xbmc convention
  1108. files.AddRange(fileSystemChildren
  1109. .Where(i => !i.IsDirectory && string.Equals(FileSystem.GetFileNameWithoutExtension(i), ThemeSongFilename, StringComparison.OrdinalIgnoreCase))
  1110. );
  1111. return LibraryManager.ResolvePaths(files, directoryService, null, new LibraryOptions())
  1112. .OfType<Audio.Audio>()
  1113. .Select(audio =>
  1114. {
  1115. // Try to retrieve it from the db. If we don't find it, use the resolved version
  1116. var dbItem = LibraryManager.GetItemById(audio.Id) as Audio.Audio;
  1117. if (dbItem != null)
  1118. {
  1119. audio = dbItem;
  1120. }
  1121. else
  1122. {
  1123. // item is new
  1124. audio.ExtraType = MediaBrowser.Model.Entities.ExtraType.ThemeSong;
  1125. }
  1126. return audio;
  1127. // Sort them so that the list can be easily compared for changes
  1128. }).OrderBy(i => i.Path).ToArray();
  1129. }
  1130. /// <summary>
  1131. /// Loads the video backdrops.
  1132. /// </summary>
  1133. /// <returns>List{Video}.</returns>
  1134. private static Video[] LoadThemeVideos(IEnumerable<FileSystemMetadata> fileSystemChildren, IDirectoryService directoryService)
  1135. {
  1136. var files = fileSystemChildren.Where(i => i.IsDirectory)
  1137. .Where(i => string.Equals(i.Name, ThemeVideosFolderName, StringComparison.OrdinalIgnoreCase))
  1138. .SelectMany(i => FileSystem.GetFiles(i.FullName));
  1139. return LibraryManager.ResolvePaths(files, directoryService, null, new LibraryOptions())
  1140. .OfType<Video>()
  1141. .Select(item =>
  1142. {
  1143. // Try to retrieve it from the db. If we don't find it, use the resolved version
  1144. var dbItem = LibraryManager.GetItemById(item.Id) as Video;
  1145. if (dbItem != null)
  1146. {
  1147. item = dbItem;
  1148. }
  1149. else
  1150. {
  1151. // item is new
  1152. item.ExtraType = MediaBrowser.Model.Entities.ExtraType.ThemeVideo;
  1153. }
  1154. return item;
  1155. // Sort them so that the list can be easily compared for changes
  1156. }).OrderBy(i => i.Path).ToArray();
  1157. }
  1158. public Task RefreshMetadata(CancellationToken cancellationToken)
  1159. {
  1160. return RefreshMetadata(new MetadataRefreshOptions(new DirectoryService(Logger, FileSystem)), cancellationToken);
  1161. }
  1162. protected virtual void TriggerOnRefreshStart()
  1163. {
  1164. }
  1165. protected virtual void TriggerOnRefreshComplete()
  1166. {
  1167. }
  1168. /// <summary>
  1169. /// Overrides the base implementation to refresh metadata for local trailers
  1170. /// </summary>
  1171. /// <param name="options">The options.</param>
  1172. /// <param name="cancellationToken">The cancellation token.</param>
  1173. /// <returns>true if a provider reports we changed</returns>
  1174. public async Task<ItemUpdateType> RefreshMetadata(MetadataRefreshOptions options, CancellationToken cancellationToken)
  1175. {
  1176. TriggerOnRefreshStart();
  1177. var requiresSave = false;
  1178. if (SupportsOwnedItems)
  1179. {
  1180. try
  1181. {
  1182. var files = IsFileProtocol ?
  1183. GetFileSystemChildren(options.DirectoryService).ToList() :
  1184. new List<FileSystemMetadata>();
  1185. var ownedItemsChanged = await RefreshedOwnedItems(options, files, cancellationToken).ConfigureAwait(false);
  1186. if (ownedItemsChanged)
  1187. {
  1188. requiresSave = true;
  1189. }
  1190. }
  1191. catch (Exception ex)
  1192. {
  1193. Logger.LogError(ex, "Error refreshing owned items for {path}", Path ?? Name);
  1194. }
  1195. }
  1196. try
  1197. {
  1198. var refreshOptions = requiresSave
  1199. ? new MetadataRefreshOptions(options)
  1200. {
  1201. ForceSave = true
  1202. }
  1203. : options;
  1204. return await ProviderManager.RefreshSingleItem(this, refreshOptions, cancellationToken).ConfigureAwait(false);
  1205. }
  1206. finally
  1207. {
  1208. TriggerOnRefreshComplete();
  1209. }
  1210. }
  1211. [IgnoreDataMember]
  1212. protected virtual bool SupportsOwnedItems
  1213. {
  1214. get { return !ParentId.Equals(Guid.Empty) && IsFileProtocol; }
  1215. }
  1216. [IgnoreDataMember]
  1217. public virtual bool SupportsPeople
  1218. {
  1219. get { return false; }
  1220. }
  1221. [IgnoreDataMember]
  1222. public virtual bool SupportsThemeMedia
  1223. {
  1224. get { return false; }
  1225. }
  1226. /// <summary>
  1227. /// Refreshes owned items such as trailers, theme videos, special features, etc.
  1228. /// Returns true or false indicating if changes were found.
  1229. /// </summary>
  1230. /// <param name="options"></param>
  1231. /// <param name="fileSystemChildren"></param>
  1232. /// <param name="cancellationToken"></param>
  1233. /// <returns></returns>
  1234. protected virtual async Task<bool> RefreshedOwnedItems(MetadataRefreshOptions options, List<FileSystemMetadata> fileSystemChildren, CancellationToken cancellationToken)
  1235. {
  1236. var themeSongsChanged = false;
  1237. var themeVideosChanged = false;
  1238. var localTrailersChanged = false;
  1239. if (IsFileProtocol && SupportsOwnedItems)
  1240. {
  1241. if (SupportsThemeMedia)
  1242. {
  1243. if (!IsInMixedFolder)
  1244. {
  1245. themeSongsChanged = await RefreshThemeSongs(this, options, fileSystemChildren, cancellationToken).ConfigureAwait(false);
  1246. themeVideosChanged = await RefreshThemeVideos(this, options, fileSystemChildren, cancellationToken).ConfigureAwait(false);
  1247. }
  1248. }
  1249. var hasTrailers = this as IHasTrailers;
  1250. if (hasTrailers != null)
  1251. {
  1252. localTrailersChanged = await RefreshLocalTrailers(hasTrailers, options, fileSystemChildren, cancellationToken).ConfigureAwait(false);
  1253. }
  1254. }
  1255. return themeSongsChanged || themeVideosChanged || localTrailersChanged;
  1256. }
  1257. protected virtual FileSystemMetadata[] GetFileSystemChildren(IDirectoryService directoryService)
  1258. {
  1259. var path = ContainingFolderPath;
  1260. return directoryService.GetFileSystemEntries(path);
  1261. }
  1262. private async Task<bool> RefreshLocalTrailers(IHasTrailers item, MetadataRefreshOptions options, List<FileSystemMetadata> fileSystemChildren, CancellationToken cancellationToken)
  1263. {
  1264. var newItems = LibraryManager.FindTrailers(this, fileSystemChildren, options.DirectoryService).ToList();
  1265. var newItemIds = newItems.Select(i => i.Id).ToArray();
  1266. var itemsChanged = !item.LocalTrailerIds.SequenceEqual(newItemIds);
  1267. var ownerId = item.Id;
  1268. var tasks = newItems.Select(i =>
  1269. {
  1270. var subOptions = new MetadataRefreshOptions(options);
  1271. if (!i.ExtraType.HasValue ||
  1272. i.ExtraType.Value != Model.Entities.ExtraType.Trailer ||
  1273. i.OwnerId != ownerId ||
  1274. !i.ParentId.Equals(Guid.Empty))
  1275. {
  1276. i.ExtraType = Model.Entities.ExtraType.Trailer;
  1277. i.OwnerId = ownerId;
  1278. i.ParentId = Guid.Empty;
  1279. subOptions.ForceSave = true;
  1280. }
  1281. return RefreshMetadataForOwnedItem(i, true, subOptions, cancellationToken);
  1282. });
  1283. await Task.WhenAll(tasks).ConfigureAwait(false);
  1284. item.LocalTrailerIds = newItemIds;
  1285. return itemsChanged;
  1286. }
  1287. private async Task<bool> RefreshThemeVideos(BaseItem item, MetadataRefreshOptions options, IEnumerable<FileSystemMetadata> fileSystemChildren, CancellationToken cancellationToken)
  1288. {
  1289. var newThemeVideos = LoadThemeVideos(fileSystemChildren, options.DirectoryService);
  1290. var newThemeVideoIds = newThemeVideos.Select(i => i.Id).ToArray();
  1291. var themeVideosChanged = !item.ThemeVideoIds.SequenceEqual(newThemeVideoIds);
  1292. var ownerId = item.Id;
  1293. var tasks = newThemeVideos.Select(i =>
  1294. {
  1295. var subOptions = new MetadataRefreshOptions(options);
  1296. if (!i.ExtraType.HasValue ||
  1297. i.ExtraType.Value != Model.Entities.ExtraType.ThemeVideo ||
  1298. i.OwnerId != ownerId ||
  1299. !i.ParentId.Equals(Guid.Empty))
  1300. {
  1301. i.ExtraType = Model.Entities.ExtraType.ThemeVideo;
  1302. i.OwnerId = ownerId;
  1303. i.ParentId = Guid.Empty;
  1304. subOptions.ForceSave = true;
  1305. }
  1306. return RefreshMetadataForOwnedItem(i, true, subOptions, cancellationToken);
  1307. });
  1308. await Task.WhenAll(tasks).ConfigureAwait(false);
  1309. item.ThemeVideoIds = newThemeVideoIds;
  1310. return themeVideosChanged;
  1311. }
  1312. /// <summary>
  1313. /// Refreshes the theme songs.
  1314. /// </summary>
  1315. private async Task<bool> RefreshThemeSongs(BaseItem item, MetadataRefreshOptions options, List<FileSystemMetadata> fileSystemChildren, CancellationToken cancellationToken)
  1316. {
  1317. var newThemeSongs = LoadThemeSongs(fileSystemChildren, options.DirectoryService);
  1318. var newThemeSongIds = newThemeSongs.Select(i => i.Id).ToArray();
  1319. var themeSongsChanged = !item.ThemeSongIds.SequenceEqual(newThemeSongIds);
  1320. var ownerId = item.Id;
  1321. var tasks = newThemeSongs.Select(i =>
  1322. {
  1323. var subOptions = new MetadataRefreshOptions(options);
  1324. if (!i.ExtraType.HasValue ||
  1325. i.ExtraType.Value != Model.Entities.ExtraType.ThemeSong ||
  1326. i.OwnerId != ownerId ||
  1327. !i.ParentId.Equals(Guid.Empty))
  1328. {
  1329. i.ExtraType = Model.Entities.ExtraType.ThemeSong;
  1330. i.OwnerId = ownerId;
  1331. i.ParentId = Guid.Empty;
  1332. subOptions.ForceSave = true;
  1333. }
  1334. return RefreshMetadataForOwnedItem(i, true, subOptions, cancellationToken);
  1335. });
  1336. await Task.WhenAll(tasks).ConfigureAwait(false);
  1337. item.ThemeSongIds = newThemeSongIds;
  1338. return themeSongsChanged;
  1339. }
  1340. /// <summary>
  1341. /// Gets or sets the provider ids.
  1342. /// </summary>
  1343. /// <value>The provider ids.</value>
  1344. [IgnoreDataMember]
  1345. public Dictionary<string, string> ProviderIds { get; set; }
  1346. [IgnoreDataMember]
  1347. public virtual Folder LatestItemsIndexContainer
  1348. {
  1349. get { return null; }
  1350. }
  1351. public virtual double GetDefaultPrimaryImageAspectRatio()
  1352. {
  1353. return 0;
  1354. }
  1355. public virtual string CreatePresentationUniqueKey()
  1356. {
  1357. return Id.ToString("N");
  1358. }
  1359. [IgnoreDataMember]
  1360. public string PresentationUniqueKey { get; set; }
  1361. public string GetPresentationUniqueKey()
  1362. {
  1363. return PresentationUniqueKey ?? CreatePresentationUniqueKey();
  1364. }
  1365. public virtual bool RequiresRefresh()
  1366. {
  1367. return false;
  1368. }
  1369. public virtual List<string> GetUserDataKeys()
  1370. {
  1371. var list = new List<string>();
  1372. if (SourceType == SourceType.Channel)
  1373. {
  1374. if (!string.IsNullOrEmpty(ExternalId))
  1375. {
  1376. list.Add(ExternalId);
  1377. }
  1378. }
  1379. list.Add(Id.ToString());
  1380. return list;
  1381. }
  1382. internal virtual ItemUpdateType UpdateFromResolvedItem(BaseItem newItem)
  1383. {
  1384. var updateType = ItemUpdateType.None;
  1385. if (IsInMixedFolder != newItem.IsInMixedFolder)
  1386. {
  1387. IsInMixedFolder = newItem.IsInMixedFolder;
  1388. updateType |= ItemUpdateType.MetadataImport;
  1389. }
  1390. return updateType;
  1391. }
  1392. public void AfterMetadataRefresh()
  1393. {
  1394. _sortName = null;
  1395. }
  1396. /// <summary>
  1397. /// Gets the preferred metadata language.
  1398. /// </summary>
  1399. /// <returns>System.String.</returns>
  1400. public string GetPreferredMetadataLanguage()
  1401. {
  1402. string lang = PreferredMetadataLanguage;
  1403. if (string.IsNullOrEmpty(lang))
  1404. {
  1405. lang = GetParents()
  1406. .Select(i => i.PreferredMetadataLanguage)
  1407. .FirstOrDefault(i => !string.IsNullOrEmpty(i));
  1408. }
  1409. if (string.IsNullOrEmpty(lang))
  1410. {
  1411. lang = LibraryManager.GetCollectionFolders(this)
  1412. .Select(i => i.PreferredMetadataLanguage)
  1413. .FirstOrDefault(i => !string.IsNullOrEmpty(i));
  1414. }
  1415. if (string.IsNullOrEmpty(lang))
  1416. {
  1417. lang = LibraryManager.GetLibraryOptions(this).PreferredMetadataLanguage;
  1418. }
  1419. if (string.IsNullOrEmpty(lang))
  1420. {
  1421. lang = ConfigurationManager.Configuration.PreferredMetadataLanguage;
  1422. }
  1423. return lang;
  1424. }
  1425. /// <summary>
  1426. /// Gets the preferred metadata language.
  1427. /// </summary>
  1428. /// <returns>System.String.</returns>
  1429. public string GetPreferredMetadataCountryCode()
  1430. {
  1431. string lang = PreferredMetadataCountryCode;
  1432. if (string.IsNullOrEmpty(lang))
  1433. {
  1434. lang = GetParents()
  1435. .Select(i => i.PreferredMetadataCountryCode)
  1436. .FirstOrDefault(i => !string.IsNullOrEmpty(i));
  1437. }
  1438. if (string.IsNullOrEmpty(lang))
  1439. {
  1440. lang = LibraryManager.GetCollectionFolders(this)
  1441. .Select(i => i.PreferredMetadataCountryCode)
  1442. .FirstOrDefault(i => !string.IsNullOrEmpty(i));
  1443. }
  1444. if (string.IsNullOrEmpty(lang))
  1445. {
  1446. lang = LibraryManager.GetLibraryOptions(this).MetadataCountryCode;
  1447. }
  1448. if (string.IsNullOrEmpty(lang))
  1449. {
  1450. lang = ConfigurationManager.Configuration.MetadataCountryCode;
  1451. }
  1452. return lang;
  1453. }
  1454. public virtual bool IsSaveLocalMetadataEnabled()
  1455. {
  1456. if (SourceType == SourceType.Channel)
  1457. {
  1458. return false;
  1459. }
  1460. var libraryOptions = LibraryManager.GetLibraryOptions(this);
  1461. return libraryOptions.SaveLocalMetadata;
  1462. }
  1463. /// <summary>
  1464. /// Determines if a given user has access to this item
  1465. /// </summary>
  1466. /// <param name="user">The user.</param>
  1467. /// <returns><c>true</c> if [is parental allowed] [the specified user]; otherwise, <c>false</c>.</returns>
  1468. /// <exception cref="System.ArgumentNullException">user</exception>
  1469. public bool IsParentalAllowed(User user)
  1470. {
  1471. if (user == null)
  1472. {
  1473. throw new ArgumentNullException("user");
  1474. }
  1475. if (!IsVisibleViaTags(user))
  1476. {
  1477. return false;
  1478. }
  1479. var maxAllowedRating = user.Policy.MaxParentalRating;
  1480. if (maxAllowedRating == null)
  1481. {
  1482. return true;
  1483. }
  1484. var rating = CustomRatingForComparison;
  1485. if (string.IsNullOrEmpty(rating))
  1486. {
  1487. rating = OfficialRatingForComparison;
  1488. }
  1489. if (string.IsNullOrEmpty(rating))
  1490. {
  1491. return !GetBlockUnratedValue(user.Policy);
  1492. }
  1493. var value = LocalizationManager.GetRatingLevel(rating);
  1494. // Could not determine the integer value
  1495. if (!value.HasValue)
  1496. {
  1497. var isAllowed = !GetBlockUnratedValue(user.Policy);
  1498. if (!isAllowed)
  1499. {
  1500. Logger.LogDebug("{0} has an unrecognized parental rating of {1}.", Name, rating);
  1501. }
  1502. return isAllowed;
  1503. }
  1504. return value.Value <= maxAllowedRating.Value;
  1505. }
  1506. public int? GetParentalRatingValue()
  1507. {
  1508. var rating = CustomRating;
  1509. if (string.IsNullOrEmpty(rating))
  1510. {
  1511. rating = OfficialRating;
  1512. }
  1513. if (string.IsNullOrEmpty(rating))
  1514. {
  1515. return null;
  1516. }
  1517. return LocalizationManager.GetRatingLevel(rating);
  1518. }
  1519. public int? GetInheritedParentalRatingValue()
  1520. {
  1521. var rating = CustomRatingForComparison;
  1522. if (string.IsNullOrEmpty(rating))
  1523. {
  1524. rating = OfficialRatingForComparison;
  1525. }
  1526. if (string.IsNullOrEmpty(rating))
  1527. {
  1528. return null;
  1529. }
  1530. return LocalizationManager.GetRatingLevel(rating);
  1531. }
  1532. public List<string> GetInheritedTags()
  1533. {
  1534. var list = new List<string>();
  1535. list.AddRange(Tags);
  1536. foreach (var parent in GetParents())
  1537. {
  1538. list.AddRange(parent.Tags);
  1539. }
  1540. return list.Distinct(StringComparer.OrdinalIgnoreCase).ToList();
  1541. }
  1542. private bool IsVisibleViaTags(User user)
  1543. {
  1544. var policy = user.Policy;
  1545. if (policy.BlockedTags.Any(i => Tags.Contains(i, StringComparer.OrdinalIgnoreCase)))
  1546. {
  1547. return false;
  1548. }
  1549. return true;
  1550. }
  1551. protected virtual bool IsAllowTagFilterEnforced()
  1552. {
  1553. return true;
  1554. }
  1555. public virtual UnratedItem GetBlockUnratedType()
  1556. {
  1557. if (SourceType == SourceType.Channel)
  1558. {
  1559. return UnratedItem.ChannelContent;
  1560. }
  1561. return UnratedItem.Other;
  1562. }
  1563. /// <summary>
  1564. /// Gets the block unrated value.
  1565. /// </summary>
  1566. /// <param name="config">The configuration.</param>
  1567. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  1568. protected virtual bool GetBlockUnratedValue(UserPolicy config)
  1569. {
  1570. // Don't block plain folders that are unrated. Let the media underneath get blocked
  1571. // Special folders like series and albums will override this method.
  1572. if (IsFolder)
  1573. {
  1574. return false;
  1575. }
  1576. if (this is IItemByName)
  1577. {
  1578. return false;
  1579. }
  1580. return config.BlockUnratedItems.Contains(GetBlockUnratedType());
  1581. }
  1582. /// <summary>
  1583. /// Determines if this folder should be visible to a given user.
  1584. /// Default is just parental allowed. Can be overridden for more functionality.
  1585. /// </summary>
  1586. /// <param name="user">The user.</param>
  1587. /// <returns><c>true</c> if the specified user is visible; otherwise, <c>false</c>.</returns>
  1588. /// <exception cref="System.ArgumentNullException">user</exception>
  1589. public virtual bool IsVisible(User user)
  1590. {
  1591. if (user == null)
  1592. {
  1593. throw new ArgumentNullException("user");
  1594. }
  1595. return IsParentalAllowed(user);
  1596. }
  1597. public virtual bool IsVisibleStandalone(User user)
  1598. {
  1599. if (SourceType == SourceType.Channel)
  1600. {
  1601. return IsVisibleStandaloneInternal(user, false) && Channel.IsChannelVisible(this, user);
  1602. }
  1603. return IsVisibleStandaloneInternal(user, true);
  1604. }
  1605. [IgnoreDataMember]
  1606. public virtual bool SupportsInheritedParentImages
  1607. {
  1608. get { return false; }
  1609. }
  1610. protected bool IsVisibleStandaloneInternal(User user, bool checkFolders)
  1611. {
  1612. if (!IsVisible(user))
  1613. {
  1614. return false;
  1615. }
  1616. if (GetParents().Any(i => !i.IsVisible(user)))
  1617. {
  1618. return false;
  1619. }
  1620. if (checkFolders)
  1621. {
  1622. var topParent = GetParents().LastOrDefault() ?? this;
  1623. if (string.IsNullOrEmpty(topParent.Path))
  1624. {
  1625. return true;
  1626. }
  1627. var itemCollectionFolders = LibraryManager.GetCollectionFolders(this).Select(i => i.Id).ToList();
  1628. if (itemCollectionFolders.Count > 0)
  1629. {
  1630. var userCollectionFolders = LibraryManager.GetUserRootFolder().GetChildren(user, true).Select(i => i.Id).ToList();
  1631. if (!itemCollectionFolders.Any(userCollectionFolders.Contains))
  1632. {
  1633. return false;
  1634. }
  1635. }
  1636. }
  1637. return true;
  1638. }
  1639. /// <summary>
  1640. /// Gets a value indicating whether this instance is folder.
  1641. /// </summary>
  1642. /// <value><c>true</c> if this instance is folder; otherwise, <c>false</c>.</value>
  1643. [IgnoreDataMember]
  1644. public virtual bool IsFolder
  1645. {
  1646. get
  1647. {
  1648. return false;
  1649. }
  1650. }
  1651. [IgnoreDataMember]
  1652. public virtual bool IsDisplayedAsFolder
  1653. {
  1654. get
  1655. {
  1656. return false;
  1657. }
  1658. }
  1659. public virtual string GetClientTypeName()
  1660. {
  1661. if (IsFolder && SourceType == SourceType.Channel && !(this is Channel))
  1662. {
  1663. return "ChannelFolderItem";
  1664. }
  1665. return GetType().Name;
  1666. }
  1667. /// <summary>
  1668. /// Gets the linked child.
  1669. /// </summary>
  1670. /// <param name="info">The info.</param>
  1671. /// <returns>BaseItem.</returns>
  1672. protected BaseItem GetLinkedChild(LinkedChild info)
  1673. {
  1674. // First get using the cached Id
  1675. if (info.ItemId.HasValue)
  1676. {
  1677. if (info.ItemId.Value.Equals(Guid.Empty))
  1678. {
  1679. return null;
  1680. }
  1681. var itemById = LibraryManager.GetItemById(info.ItemId.Value);
  1682. if (itemById != null)
  1683. {
  1684. return itemById;
  1685. }
  1686. }
  1687. var item = FindLinkedChild(info);
  1688. // If still null, log
  1689. if (item == null)
  1690. {
  1691. // Don't keep searching over and over
  1692. info.ItemId = Guid.Empty;
  1693. }
  1694. else
  1695. {
  1696. // Cache the id for next time
  1697. info.ItemId = item.Id;
  1698. }
  1699. return item;
  1700. }
  1701. private BaseItem FindLinkedChild(LinkedChild info)
  1702. {
  1703. var path = info.Path;
  1704. if (!string.IsNullOrEmpty(path))
  1705. {
  1706. path = FileSystem.MakeAbsolutePath(ContainingFolderPath, path);
  1707. var itemByPath = LibraryManager.FindByPath(path, null);
  1708. if (itemByPath == null)
  1709. {
  1710. //Logger.LogWarning("Unable to find linked item at path {0}", info.Path);
  1711. }
  1712. return itemByPath;
  1713. }
  1714. if (!string.IsNullOrEmpty(info.LibraryItemId))
  1715. {
  1716. var item = LibraryManager.GetItemById(info.LibraryItemId);
  1717. if (item == null)
  1718. {
  1719. //Logger.LogWarning("Unable to find linked item at path {0}", info.Path);
  1720. }
  1721. return item;
  1722. }
  1723. return null;
  1724. }
  1725. [IgnoreDataMember]
  1726. public virtual bool EnableRememberingTrackSelections
  1727. {
  1728. get
  1729. {
  1730. return true;
  1731. }
  1732. }
  1733. /// <summary>
  1734. /// Adds a studio to the item
  1735. /// </summary>
  1736. /// <param name="name">The name.</param>
  1737. /// <exception cref="System.ArgumentNullException"></exception>
  1738. public void AddStudio(string name)
  1739. {
  1740. if (string.IsNullOrEmpty(name))
  1741. {
  1742. throw new ArgumentNullException("name");
  1743. }
  1744. var current = Studios;
  1745. if (!current.Contains(name, StringComparer.OrdinalIgnoreCase))
  1746. {
  1747. if (current.Length == 0)
  1748. {
  1749. Studios = new[] { name };
  1750. }
  1751. else
  1752. {
  1753. var list =
  1754. Studios = current.Concat(new [] { name }).ToArray();
  1755. }
  1756. }
  1757. }
  1758. public void SetStudios(IEnumerable<string> names)
  1759. {
  1760. Studios = names.Distinct().ToArray();
  1761. }
  1762. /// <summary>
  1763. /// Adds a genre to the item
  1764. /// </summary>
  1765. /// <param name="name">The name.</param>
  1766. /// <exception cref="System.ArgumentNullException"></exception>
  1767. public void AddGenre(string name)
  1768. {
  1769. if (string.IsNullOrEmpty(name))
  1770. {
  1771. throw new ArgumentNullException("name");
  1772. }
  1773. var genres = Genres;
  1774. if (!genres.Contains(name, StringComparer.OrdinalIgnoreCase))
  1775. {
  1776. var list = genres.ToList();
  1777. list.Add(name);
  1778. Genres = list.ToArray();
  1779. }
  1780. }
  1781. /// <summary>
  1782. /// Marks the played.
  1783. /// </summary>
  1784. /// <param name="user">The user.</param>
  1785. /// <param name="datePlayed">The date played.</param>
  1786. /// <param name="resetPosition">if set to <c>true</c> [reset position].</param>
  1787. /// <returns>Task.</returns>
  1788. /// <exception cref="System.ArgumentNullException"></exception>
  1789. public virtual void MarkPlayed(User user,
  1790. DateTime? datePlayed,
  1791. bool resetPosition)
  1792. {
  1793. if (user == null)
  1794. {
  1795. throw new ArgumentNullException();
  1796. }
  1797. var data = UserDataManager.GetUserData(user, this);
  1798. if (datePlayed.HasValue)
  1799. {
  1800. // Increment
  1801. data.PlayCount++;
  1802. }
  1803. // Ensure it's at least one
  1804. data.PlayCount = Math.Max(data.PlayCount, 1);
  1805. if (resetPosition)
  1806. {
  1807. data.PlaybackPositionTicks = 0;
  1808. }
  1809. data.LastPlayedDate = datePlayed ?? data.LastPlayedDate ?? DateTime.UtcNow;
  1810. data.Played = true;
  1811. UserDataManager.SaveUserData(user.Id, this, data, UserDataSaveReason.TogglePlayed, CancellationToken.None);
  1812. }
  1813. /// <summary>
  1814. /// Marks the unplayed.
  1815. /// </summary>
  1816. /// <param name="user">The user.</param>
  1817. /// <returns>Task.</returns>
  1818. /// <exception cref="System.ArgumentNullException"></exception>
  1819. public virtual void MarkUnplayed(User user)
  1820. {
  1821. if (user == null)
  1822. {
  1823. throw new ArgumentNullException();
  1824. }
  1825. var data = UserDataManager.GetUserData(user, this);
  1826. //I think it is okay to do this here.
  1827. // if this is only called when a user is manually forcing something to un-played
  1828. // then it probably is what we want to do...
  1829. data.PlayCount = 0;
  1830. data.PlaybackPositionTicks = 0;
  1831. data.LastPlayedDate = null;
  1832. data.Played = false;
  1833. UserDataManager.SaveUserData(user.Id, this, data, UserDataSaveReason.TogglePlayed, CancellationToken.None);
  1834. }
  1835. /// <summary>
  1836. /// Do whatever refreshing is necessary when the filesystem pertaining to this item has changed.
  1837. /// </summary>
  1838. /// <returns>Task.</returns>
  1839. public virtual void ChangedExternally()
  1840. {
  1841. ProviderManager.QueueRefresh(Id, new MetadataRefreshOptions(new DirectoryService(Logger, FileSystem))
  1842. {
  1843. }, RefreshPriority.High);
  1844. }
  1845. /// <summary>
  1846. /// Gets an image
  1847. /// </summary>
  1848. /// <param name="type">The type.</param>
  1849. /// <param name="imageIndex">Index of the image.</param>
  1850. /// <returns><c>true</c> if the specified type has image; otherwise, <c>false</c>.</returns>
  1851. /// <exception cref="System.ArgumentException">Backdrops should be accessed using Item.Backdrops</exception>
  1852. public bool HasImage(ImageType type, int imageIndex)
  1853. {
  1854. return GetImageInfo(type, imageIndex) != null;
  1855. }
  1856. public void SetImage(ItemImageInfo image, int index)
  1857. {
  1858. if (image.Type == ImageType.Chapter)
  1859. {
  1860. throw new ArgumentException("Cannot set chapter images using SetImagePath");
  1861. }
  1862. var existingImage = GetImageInfo(image.Type, index);
  1863. if (existingImage != null)
  1864. {
  1865. existingImage.Path = image.Path;
  1866. existingImage.DateModified = image.DateModified;
  1867. existingImage.Width = image.Width;
  1868. existingImage.Height = image.Height;
  1869. }
  1870. else
  1871. {
  1872. var currentCount = ImageInfos.Length;
  1873. ImageInfos = ImageInfos.Concat(new [] { image }) .ToArray();
  1874. }
  1875. }
  1876. public void SetImagePath(ImageType type, int index, FileSystemMetadata file)
  1877. {
  1878. if (type == ImageType.Chapter)
  1879. {
  1880. throw new ArgumentException("Cannot set chapter images using SetImagePath");
  1881. }
  1882. var image = GetImageInfo(type, index);
  1883. if (image == null)
  1884. {
  1885. ImageInfos = ImageInfos.Concat(new [] { GetImageInfo(file, type) }) .ToArray();
  1886. }
  1887. else
  1888. {
  1889. var imageInfo = GetImageInfo(file, type);
  1890. image.Path = file.FullName;
  1891. image.DateModified = imageInfo.DateModified;
  1892. // reset these values
  1893. image.Width = 0;
  1894. image.Height = 0;
  1895. }
  1896. }
  1897. /// <summary>
  1898. /// Deletes the image.
  1899. /// </summary>
  1900. /// <param name="type">The type.</param>
  1901. /// <param name="index">The index.</param>
  1902. /// <returns>Task.</returns>
  1903. public void DeleteImage(ImageType type, int index)
  1904. {
  1905. var info = GetImageInfo(type, index);
  1906. if (info == null)
  1907. {
  1908. // Nothing to do
  1909. return;
  1910. }
  1911. // Remove it from the item
  1912. RemoveImage(info);
  1913. if (info.IsLocalFile)
  1914. {
  1915. FileSystem.DeleteFile(info.Path);
  1916. }
  1917. UpdateToRepository(ItemUpdateType.ImageUpdate, CancellationToken.None);
  1918. }
  1919. public void RemoveImage(ItemImageInfo image)
  1920. {
  1921. RemoveImages(new List<ItemImageInfo> { image });
  1922. }
  1923. public void RemoveImages(List<ItemImageInfo> deletedImages)
  1924. {
  1925. ImageInfos = ImageInfos.Except(deletedImages).ToArray();
  1926. }
  1927. public virtual void UpdateToRepository(ItemUpdateType updateReason, CancellationToken cancellationToken)
  1928. {
  1929. LibraryManager.UpdateItem(this, GetParent(), updateReason, cancellationToken);
  1930. }
  1931. /// <summary>
  1932. /// Validates that images within the item are still on the file system
  1933. /// </summary>
  1934. public bool ValidateImages(IDirectoryService directoryService)
  1935. {
  1936. var allFiles = ImageInfos
  1937. .Where(i => i.IsLocalFile)
  1938. .Select(i => FileSystem.GetDirectoryName(i.Path))
  1939. .Distinct(StringComparer.OrdinalIgnoreCase)
  1940. .SelectMany(i => directoryService.GetFilePaths(i))
  1941. .ToList();
  1942. var deletedImages = ImageInfos
  1943. .Where(image => image.IsLocalFile && !allFiles.Contains(image.Path, StringComparer.OrdinalIgnoreCase))
  1944. .ToList();
  1945. if (deletedImages.Count > 0)
  1946. {
  1947. ImageInfos = ImageInfos.Except(deletedImages).ToArray();
  1948. }
  1949. return deletedImages.Count > 0;
  1950. }
  1951. /// <summary>
  1952. /// Gets the image path.
  1953. /// </summary>
  1954. /// <param name="imageType">Type of the image.</param>
  1955. /// <param name="imageIndex">Index of the image.</param>
  1956. /// <returns>System.String.</returns>
  1957. /// <exception cref="System.InvalidOperationException">
  1958. /// </exception>
  1959. /// <exception cref="System.ArgumentNullException">item</exception>
  1960. public string GetImagePath(ImageType imageType, int imageIndex)
  1961. {
  1962. var info = GetImageInfo(imageType, imageIndex);
  1963. return info == null ? null : info.Path;
  1964. }
  1965. /// <summary>
  1966. /// Gets the image information.
  1967. /// </summary>
  1968. /// <param name="imageType">Type of the image.</param>
  1969. /// <param name="imageIndex">Index of the image.</param>
  1970. /// <returns>ItemImageInfo.</returns>
  1971. public ItemImageInfo GetImageInfo(ImageType imageType, int imageIndex)
  1972. {
  1973. if (imageType == ImageType.Chapter)
  1974. {
  1975. var chapter = ItemRepository.GetChapter(this, imageIndex);
  1976. if (chapter == null)
  1977. {
  1978. return null;
  1979. }
  1980. var path = chapter.ImagePath;
  1981. if (string.IsNullOrEmpty(path))
  1982. {
  1983. return null;
  1984. }
  1985. return new ItemImageInfo
  1986. {
  1987. Path = path,
  1988. DateModified = chapter.ImageDateModified,
  1989. Type = imageType
  1990. };
  1991. }
  1992. return GetImages(imageType)
  1993. .ElementAtOrDefault(imageIndex);
  1994. }
  1995. public IEnumerable<ItemImageInfo> GetImages(ImageType imageType)
  1996. {
  1997. if (imageType == ImageType.Chapter)
  1998. {
  1999. throw new ArgumentException("No image info for chapter images");
  2000. }
  2001. return ImageInfos.Where(i => i.Type == imageType);
  2002. }
  2003. /// <summary>
  2004. /// Adds the images.
  2005. /// </summary>
  2006. /// <param name="imageType">Type of the image.</param>
  2007. /// <param name="images">The images.</param>
  2008. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  2009. /// <exception cref="System.ArgumentException">Cannot call AddImages with chapter images</exception>
  2010. public bool AddImages(ImageType imageType, List<FileSystemMetadata> images)
  2011. {
  2012. if (imageType == ImageType.Chapter)
  2013. {
  2014. throw new ArgumentException("Cannot call AddImages with chapter images");
  2015. }
  2016. var existingImages = GetImages(imageType)
  2017. .ToList();
  2018. var newImageList = new List<FileSystemMetadata>();
  2019. var imageAdded = false;
  2020. var imageUpdated = false;
  2021. foreach (var newImage in images)
  2022. {
  2023. if (newImage == null)
  2024. {
  2025. throw new ArgumentException("null image found in list");
  2026. }
  2027. var existing = existingImages
  2028. .FirstOrDefault(i => string.Equals(i.Path, newImage.FullName, StringComparison.OrdinalIgnoreCase));
  2029. if (existing == null)
  2030. {
  2031. newImageList.Add(newImage);
  2032. imageAdded = true;
  2033. }
  2034. else
  2035. {
  2036. if (existing.IsLocalFile)
  2037. {
  2038. var newDateModified = FileSystem.GetLastWriteTimeUtc(newImage);
  2039. // If date changed then we need to reset saved image dimensions
  2040. if (existing.DateModified != newDateModified && (existing.Width > 0 || existing.Height > 0))
  2041. {
  2042. existing.Width = 0;
  2043. existing.Height = 0;
  2044. imageUpdated = true;
  2045. }
  2046. existing.DateModified = newDateModified;
  2047. }
  2048. }
  2049. }
  2050. if (imageAdded || images.Count != existingImages.Count)
  2051. {
  2052. var newImagePaths = images.Select(i => i.FullName).ToList();
  2053. var deleted = existingImages
  2054. .Where(i => i.IsLocalFile && !newImagePaths.Contains(i.Path, StringComparer.OrdinalIgnoreCase) && !FileSystem.FileExists(i.Path))
  2055. .ToList();
  2056. if (deleted.Count > 0)
  2057. {
  2058. ImageInfos = ImageInfos.Except(deleted).ToArray();
  2059. }
  2060. }
  2061. if (newImageList.Count > 0)
  2062. {
  2063. ImageInfos = ImageInfos.Concat(newImageList.Select(i => GetImageInfo(i, imageType))).ToArray();
  2064. }
  2065. return imageUpdated || newImageList.Count > 0;
  2066. }
  2067. private ItemImageInfo GetImageInfo(FileSystemMetadata file, ImageType type)
  2068. {
  2069. return new ItemImageInfo
  2070. {
  2071. Path = file.FullName,
  2072. Type = type,
  2073. DateModified = FileSystem.GetLastWriteTimeUtc(file)
  2074. };
  2075. }
  2076. /// <summary>
  2077. /// Gets the file system path to delete when the item is to be deleted
  2078. /// </summary>
  2079. /// <returns></returns>
  2080. public virtual IEnumerable<FileSystemMetadata> GetDeletePaths()
  2081. {
  2082. return new[] {
  2083. new FileSystemMetadata
  2084. {
  2085. FullName = Path,
  2086. IsDirectory = IsFolder
  2087. }
  2088. }.Concat(GetLocalMetadataFilesToDelete());
  2089. }
  2090. protected List<FileSystemMetadata> GetLocalMetadataFilesToDelete()
  2091. {
  2092. if (IsFolder || !IsInMixedFolder)
  2093. {
  2094. return new List<FileSystemMetadata>();
  2095. }
  2096. var filename = System.IO.Path.GetFileNameWithoutExtension(Path);
  2097. var extensions = new List<string> { ".nfo", ".xml", ".srt", ".vtt", ".sub", ".idx", ".txt", ".edl", ".bif", ".smi", ".ttml" };
  2098. extensions.AddRange(SupportedImageExtensions);
  2099. return FileSystem.GetFiles(FileSystem.GetDirectoryName(Path), extensions.ToArray(), false, false)
  2100. .Where(i => System.IO.Path.GetFileNameWithoutExtension(i.FullName).StartsWith(filename, StringComparison.OrdinalIgnoreCase))
  2101. .ToList();
  2102. }
  2103. public bool AllowsMultipleImages(ImageType type)
  2104. {
  2105. return type == ImageType.Backdrop || type == ImageType.Screenshot || type == ImageType.Chapter;
  2106. }
  2107. public void SwapImages(ImageType type, int index1, int index2)
  2108. {
  2109. if (!AllowsMultipleImages(type))
  2110. {
  2111. throw new ArgumentException("The change index operation is only applicable to backdrops and screenshots");
  2112. }
  2113. var info1 = GetImageInfo(type, index1);
  2114. var info2 = GetImageInfo(type, index2);
  2115. if (info1 == null || info2 == null)
  2116. {
  2117. // Nothing to do
  2118. return;
  2119. }
  2120. if (!info1.IsLocalFile || !info2.IsLocalFile)
  2121. {
  2122. // TODO: Not supported yet
  2123. return;
  2124. }
  2125. var path1 = info1.Path;
  2126. var path2 = info2.Path;
  2127. FileSystem.SwapFiles(path1, path2);
  2128. // Refresh these values
  2129. info1.DateModified = FileSystem.GetLastWriteTimeUtc(info1.Path);
  2130. info2.DateModified = FileSystem.GetLastWriteTimeUtc(info2.Path);
  2131. info1.Width = 0;
  2132. info1.Height = 0;
  2133. info2.Width = 0;
  2134. info2.Height = 0;
  2135. UpdateToRepository(ItemUpdateType.ImageUpdate, CancellationToken.None);
  2136. }
  2137. public virtual bool IsPlayed(User user)
  2138. {
  2139. var userdata = UserDataManager.GetUserData(user, this);
  2140. return userdata != null && userdata.Played;
  2141. }
  2142. public bool IsFavoriteOrLiked(User user)
  2143. {
  2144. var userdata = UserDataManager.GetUserData(user, this);
  2145. return userdata != null && (userdata.IsFavorite || (userdata.Likes ?? false));
  2146. }
  2147. public virtual bool IsUnplayed(User user)
  2148. {
  2149. if (user == null)
  2150. {
  2151. throw new ArgumentNullException("user");
  2152. }
  2153. var userdata = UserDataManager.GetUserData(user, this);
  2154. return userdata == null || !userdata.Played;
  2155. }
  2156. ItemLookupInfo IHasLookupInfo<ItemLookupInfo>.GetLookupInfo()
  2157. {
  2158. return GetItemLookupInfo<ItemLookupInfo>();
  2159. }
  2160. protected T GetItemLookupInfo<T>()
  2161. where T : ItemLookupInfo, new()
  2162. {
  2163. return new T
  2164. {
  2165. MetadataCountryCode = GetPreferredMetadataCountryCode(),
  2166. MetadataLanguage = GetPreferredMetadataLanguage(),
  2167. Name = GetNameForMetadataLookup(),
  2168. ProviderIds = ProviderIds,
  2169. IndexNumber = IndexNumber,
  2170. ParentIndexNumber = ParentIndexNumber,
  2171. Year = ProductionYear,
  2172. PremiereDate = PremiereDate
  2173. };
  2174. }
  2175. protected virtual string GetNameForMetadataLookup()
  2176. {
  2177. return Name;
  2178. }
  2179. /// <summary>
  2180. /// This is called before any metadata refresh and returns true or false indicating if changes were made
  2181. /// </summary>
  2182. public virtual bool BeforeMetadataRefresh(bool replaceAllMetdata)
  2183. {
  2184. _sortName = null;
  2185. var hasChanges = false;
  2186. if (string.IsNullOrEmpty(Name) && !string.IsNullOrEmpty(Path))
  2187. {
  2188. Name = FileSystem.GetFileNameWithoutExtension(Path);
  2189. hasChanges = true;
  2190. }
  2191. return hasChanges;
  2192. }
  2193. protected static string GetMappedPath(BaseItem item, string path, MediaProtocol? protocol)
  2194. {
  2195. if (protocol.HasValue && protocol.Value == MediaProtocol.File)
  2196. {
  2197. return LibraryManager.GetPathAfterNetworkSubstitution(path, item);
  2198. }
  2199. return path;
  2200. }
  2201. public virtual void FillUserDataDtoValues(UserItemDataDto dto, UserItemData userData, BaseItemDto itemDto, User user, DtoOptions fields)
  2202. {
  2203. if (RunTimeTicks.HasValue)
  2204. {
  2205. double pct = RunTimeTicks.Value;
  2206. if (pct > 0)
  2207. {
  2208. pct = userData.PlaybackPositionTicks / pct;
  2209. if (pct > 0)
  2210. {
  2211. dto.PlayedPercentage = 100 * pct;
  2212. }
  2213. }
  2214. }
  2215. }
  2216. protected Task RefreshMetadataForOwnedItem(BaseItem ownedItem, bool copyTitleMetadata, MetadataRefreshOptions options, CancellationToken cancellationToken)
  2217. {
  2218. var newOptions = new MetadataRefreshOptions(options);
  2219. newOptions.SearchResult = null;
  2220. var item = this;
  2221. if (copyTitleMetadata)
  2222. {
  2223. // Take some data from the main item, for querying purposes
  2224. if (!item.Genres.SequenceEqual(ownedItem.Genres, StringComparer.Ordinal))
  2225. {
  2226. newOptions.ForceSave = true;
  2227. ownedItem.Genres = item.Genres;
  2228. }
  2229. if (!item.Studios.SequenceEqual(ownedItem.Studios, StringComparer.Ordinal))
  2230. {
  2231. newOptions.ForceSave = true;
  2232. ownedItem.Studios = item.Studios;
  2233. }
  2234. if (!item.ProductionLocations.SequenceEqual(ownedItem.ProductionLocations, StringComparer.Ordinal))
  2235. {
  2236. newOptions.ForceSave = true;
  2237. ownedItem.ProductionLocations = item.ProductionLocations;
  2238. }
  2239. if (item.CommunityRating != ownedItem.CommunityRating)
  2240. {
  2241. ownedItem.CommunityRating = item.CommunityRating;
  2242. newOptions.ForceSave = true;
  2243. }
  2244. if (item.CriticRating != ownedItem.CriticRating)
  2245. {
  2246. ownedItem.CriticRating = item.CriticRating;
  2247. newOptions.ForceSave = true;
  2248. }
  2249. if (!string.Equals(item.Overview, ownedItem.Overview, StringComparison.Ordinal))
  2250. {
  2251. ownedItem.Overview = item.Overview;
  2252. newOptions.ForceSave = true;
  2253. }
  2254. if (!string.Equals(item.OfficialRating, ownedItem.OfficialRating, StringComparison.Ordinal))
  2255. {
  2256. ownedItem.OfficialRating = item.OfficialRating;
  2257. newOptions.ForceSave = true;
  2258. }
  2259. if (!string.Equals(item.CustomRating, ownedItem.CustomRating, StringComparison.Ordinal))
  2260. {
  2261. ownedItem.CustomRating = item.CustomRating;
  2262. newOptions.ForceSave = true;
  2263. }
  2264. }
  2265. return ownedItem.RefreshMetadata(newOptions, cancellationToken);
  2266. }
  2267. protected Task RefreshMetadataForOwnedVideo(MetadataRefreshOptions options, bool copyTitleMetadata, string path, CancellationToken cancellationToken)
  2268. {
  2269. var newOptions = new MetadataRefreshOptions(options);
  2270. newOptions.SearchResult = null;
  2271. var id = LibraryManager.GetNewItemId(path, typeof(Video));
  2272. // Try to retrieve it from the db. If we don't find it, use the resolved version
  2273. var video = LibraryManager.GetItemById(id) as Video;
  2274. if (video == null)
  2275. {
  2276. video = LibraryManager.ResolvePath(FileSystem.GetFileSystemInfo(path)) as Video;
  2277. newOptions.ForceSave = true;
  2278. }
  2279. //var parentId = Id;
  2280. //if (!video.IsOwnedItem || video.ParentId != parentId)
  2281. //{
  2282. // video.IsOwnedItem = true;
  2283. // video.ParentId = parentId;
  2284. // newOptions.ForceSave = true;
  2285. //}
  2286. if (video == null)
  2287. {
  2288. return Task.FromResult(true);
  2289. }
  2290. return RefreshMetadataForOwnedItem(video, copyTitleMetadata, newOptions, cancellationToken);
  2291. }
  2292. public string GetEtag(User user)
  2293. {
  2294. var list = GetEtagValues(user);
  2295. return string.Join("|", list.ToArray()).GetMD5().ToString("N");
  2296. }
  2297. protected virtual List<string> GetEtagValues(User user)
  2298. {
  2299. return new List<string>
  2300. {
  2301. DateLastSaved.Ticks.ToString(CultureInfo.InvariantCulture)
  2302. };
  2303. }
  2304. public virtual IEnumerable<Guid> GetAncestorIds()
  2305. {
  2306. return GetParents().Select(i => i.Id).Concat(LibraryManager.GetCollectionFolders(this).Select(i => i.Id));
  2307. }
  2308. public BaseItem GetTopParent()
  2309. {
  2310. if (IsTopParent)
  2311. {
  2312. return this;
  2313. }
  2314. foreach (var parent in GetParents())
  2315. {
  2316. if (parent.IsTopParent)
  2317. {
  2318. return parent;
  2319. }
  2320. }
  2321. return null;
  2322. }
  2323. [IgnoreDataMember]
  2324. public virtual bool IsTopParent
  2325. {
  2326. get
  2327. {
  2328. if (this is BasePluginFolder || this is Channel)
  2329. {
  2330. return true;
  2331. }
  2332. var view = this as IHasCollectionType;
  2333. if (view != null)
  2334. {
  2335. if (string.Equals(view.CollectionType, CollectionType.LiveTv, StringComparison.OrdinalIgnoreCase))
  2336. {
  2337. return true;
  2338. }
  2339. }
  2340. if (GetParent() is AggregateFolder)
  2341. {
  2342. return true;
  2343. }
  2344. return false;
  2345. }
  2346. }
  2347. [IgnoreDataMember]
  2348. public virtual bool SupportsAncestors
  2349. {
  2350. get
  2351. {
  2352. return true;
  2353. }
  2354. }
  2355. [IgnoreDataMember]
  2356. public virtual bool StopRefreshIfLocalMetadataFound
  2357. {
  2358. get
  2359. {
  2360. return true;
  2361. }
  2362. }
  2363. public virtual IEnumerable<Guid> GetIdsForAncestorQuery()
  2364. {
  2365. return new[] { Id };
  2366. }
  2367. public virtual List<ExternalUrl> GetRelatedUrls()
  2368. {
  2369. return new List<ExternalUrl>();
  2370. }
  2371. public virtual double? GetRefreshProgress()
  2372. {
  2373. return null;
  2374. }
  2375. public virtual ItemUpdateType OnMetadataChanged()
  2376. {
  2377. var updateType = ItemUpdateType.None;
  2378. var item = this;
  2379. var inheritedParentalRatingValue = item.GetInheritedParentalRatingValue() ?? 0;
  2380. if (inheritedParentalRatingValue != item.InheritedParentalRatingValue)
  2381. {
  2382. item.InheritedParentalRatingValue = inheritedParentalRatingValue;
  2383. updateType |= ItemUpdateType.MetadataImport;
  2384. }
  2385. return updateType;
  2386. }
  2387. /// <summary>
  2388. /// Updates the official rating based on content and returns true or false indicating if it changed.
  2389. /// </summary>
  2390. /// <returns></returns>
  2391. public bool UpdateRatingToItems(IList<BaseItem> children)
  2392. {
  2393. var currentOfficialRating = OfficialRating;
  2394. // Gather all possible ratings
  2395. var ratings = children
  2396. .Select(i => i.OfficialRating)
  2397. .Where(i => !string.IsNullOrEmpty(i))
  2398. .Distinct(StringComparer.OrdinalIgnoreCase)
  2399. .Select(i => new Tuple<string, int?>(i, LocalizationManager.GetRatingLevel(i)))
  2400. .OrderBy(i => i.Item2 ?? 1000)
  2401. .Select(i => i.Item1);
  2402. OfficialRating = ratings.FirstOrDefault() ?? currentOfficialRating;
  2403. return !string.Equals(currentOfficialRating ?? string.Empty, OfficialRating ?? string.Empty,
  2404. StringComparison.OrdinalIgnoreCase);
  2405. }
  2406. public IEnumerable<BaseItem> GetThemeSongs()
  2407. {
  2408. return ThemeVideoIds.Select(LibraryManager.GetItemById).Where(i => i.ExtraType.Equals(Model.Entities.ExtraType.ThemeSong)).OrderBy(i => i.SortName);
  2409. }
  2410. public IEnumerable<BaseItem> GetThemeVideos()
  2411. {
  2412. return ThemeVideoIds.Select(LibraryManager.GetItemById).Where(i => i.ExtraType.Equals(Model.Entities.ExtraType.ThemeVideo)).OrderBy(i => i.SortName);
  2413. }
  2414. /// <summary>
  2415. /// Gets or sets the remote trailers.
  2416. /// </summary>
  2417. /// <value>The remote trailers.</value>
  2418. public MediaUrl[] RemoteTrailers { get; set; }
  2419. public IEnumerable<BaseItem> GetExtras()
  2420. {
  2421. return ThemeVideoIds.Select(LibraryManager.GetItemById).Where(i => i.ExtraType.Equals(Model.Entities.ExtraType.ThemeVideo)).OrderBy(i => i.SortName);
  2422. }
  2423. public IEnumerable<BaseItem> GetExtras(ExtraType[] unused)
  2424. {
  2425. return GetExtras();
  2426. }
  2427. public IEnumerable<BaseItem> GetDisplayExtras()
  2428. {
  2429. return GetExtras();
  2430. }
  2431. public virtual bool IsHD {
  2432. get
  2433. {
  2434. return Height >= 720;
  2435. }
  2436. }
  2437. public bool IsShortcut{ get; set;}
  2438. public string ShortcutPath{ get; set;}
  2439. public int Width { get; set; }
  2440. public int Height { get; set; }
  2441. public Guid[] ExtraIds { get; set; }
  2442. public virtual long GetRunTimeTicksForPlayState()
  2443. {
  2444. return RunTimeTicks ?? 0;
  2445. }
  2446. // what does this do?
  2447. public static ExtraType[] DisplayExtraTypes = new[] {Model.Entities.ExtraType.ThemeSong, Model.Entities.ExtraType.ThemeVideo };
  2448. public virtual bool SupportsExternalTransfer
  2449. {
  2450. get {
  2451. return false;
  2452. }
  2453. }
  2454. }
  2455. }