ItemUpdateNotification.xaml.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. using System.Linq;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Entities.TV;
  4. using MediaBrowser.Model.Entities;
  5. using MediaBrowser.Model.Logging;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Media;
  12. namespace MediaBrowser.ServerApplication.Controls
  13. {
  14. /// <summary>
  15. /// Interaction logic for ItemUpdateNotification.xaml
  16. /// </summary>
  17. public partial class ItemUpdateNotification : UserControl
  18. {
  19. /// <summary>
  20. /// The logger
  21. /// </summary>
  22. private readonly ILogger Logger;
  23. /// <summary>
  24. /// Gets the children changed event args.
  25. /// </summary>
  26. /// <value>The children changed event args.</value>
  27. private BaseItem Item
  28. {
  29. get { return DataContext as BaseItem; }
  30. }
  31. /// <summary>
  32. /// Initializes a new instance of the <see cref="ItemUpdateNotification" /> class.
  33. /// </summary>
  34. public ItemUpdateNotification(ILogger logger)
  35. {
  36. if (logger == null)
  37. {
  38. throw new ArgumentNullException("logger");
  39. }
  40. Logger = logger;
  41. InitializeComponent();
  42. Loaded += ItemUpdateNotification_Loaded;
  43. }
  44. /// <summary>
  45. /// Handles the Loaded event of the ItemUpdateNotification control.
  46. /// </summary>
  47. /// <param name="sender">The source of the event.</param>
  48. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  49. void ItemUpdateNotification_Loaded(object sender, RoutedEventArgs e)
  50. {
  51. DisplayItem(Item);
  52. }
  53. /// <summary>
  54. /// Gets the display name.
  55. /// </summary>
  56. /// <param name="item">The item.</param>
  57. /// <param name="includeParentName">if set to <c>true</c> [include parent name].</param>
  58. /// <returns>System.String.</returns>
  59. internal static string GetDisplayName(BaseItem item, bool includeParentName)
  60. {
  61. var name = item.Name;
  62. if (item.ProductionYear.HasValue && !(item is Episode))
  63. {
  64. name += string.Format(" ({0})", item.ProductionYear);
  65. }
  66. var episode = item as Episode;
  67. if (episode != null)
  68. {
  69. var indexNumbers = new List<int>();
  70. if (episode.Season.IndexNumber.HasValue)
  71. {
  72. indexNumbers.Add(episode.Season.IndexNumber.Value);
  73. }
  74. if (episode.IndexNumber.HasValue)
  75. {
  76. indexNumbers.Add(episode.IndexNumber.Value);
  77. }
  78. var indexNumber = string.Join(".", indexNumbers.ToArray());
  79. name = string.Format("{0} - {1}", indexNumber, name);
  80. if (includeParentName)
  81. {
  82. name = episode.Series.Name + " - " + name;
  83. }
  84. }
  85. if (includeParentName)
  86. {
  87. var season = item as Season;
  88. if (season != null)
  89. {
  90. name = season.Series.Name + " - " + name;
  91. }
  92. }
  93. return name;
  94. }
  95. /// <summary>
  96. /// Displays the parent title.
  97. /// </summary>
  98. /// <param name="item">The item.</param>
  99. private void DisplayParentTitle(BaseItem item)
  100. {
  101. if (!(item is Episode || item is Season))
  102. {
  103. txtParentName.Visibility = Visibility.Collapsed;
  104. imgParentLogo.Visibility = Visibility.Collapsed;
  105. return;
  106. }
  107. var series = item is Episode ? (item as Episode).Series : (item as Season).Series;
  108. var logo = series.GetImage(ImageType.Logo);
  109. if (string.IsNullOrEmpty(logo))
  110. {
  111. imgParentLogo.Visibility = Visibility.Collapsed;
  112. txtParentName.Visibility = Visibility.Visible;
  113. }
  114. else
  115. {
  116. imgParentLogo.Visibility = Visibility.Visible;
  117. txtParentName.Visibility = Visibility.Collapsed;
  118. imgParentLogo.Source = App.Instance.GetBitmapImage(logo);
  119. }
  120. txtParentName.Text = series.Name;
  121. }
  122. /// <summary>
  123. /// Displays the title.
  124. /// </summary>
  125. /// <param name="item">The item.</param>
  126. private void DisplayTitle(BaseItem item)
  127. {
  128. txtName.Text = GetDisplayName(item, false);
  129. }
  130. /// <summary>
  131. /// Displays the item.
  132. /// </summary>
  133. /// <param name="item">The item.</param>
  134. private void DisplayItem(BaseItem item)
  135. {
  136. DisplayParentTitle(item);
  137. DisplayTitle(item);
  138. DisplayRating(item);
  139. var path = GetImagePath(item);
  140. if (string.IsNullOrEmpty(path))
  141. {
  142. img.Visibility = Visibility.Collapsed;
  143. }
  144. else
  145. {
  146. img.Visibility = Visibility.Visible;
  147. try
  148. {
  149. img.Source = App.Instance.GetBitmapImage(path);
  150. }
  151. catch (FileNotFoundException)
  152. {
  153. Logger.Error("Image file not found {0}", path);
  154. }
  155. }
  156. if (string.IsNullOrEmpty(item.Overview))
  157. {
  158. txtOverview.Visibility = Visibility.Collapsed;
  159. }
  160. else
  161. {
  162. txtOverview.Visibility = Visibility.Visible;
  163. txtOverview.Text = item.Overview;
  164. }
  165. if (item.Taglines == null || item.Taglines.Count == 0)
  166. {
  167. txtTagline.Visibility = Visibility.Collapsed;
  168. }
  169. else
  170. {
  171. txtTagline.Visibility = Visibility.Visible;
  172. txtTagline.Text = item.Taglines[0];
  173. }
  174. if (!item.PremiereDate.HasValue)
  175. {
  176. txtPremeireDate.Visibility = Visibility.Collapsed;
  177. }
  178. else
  179. {
  180. txtPremeireDate.Visibility = Visibility.Visible;
  181. txtPremeireDate.Text = "Premiered " + item.PremiereDate.Value.ToLocalTime().ToShortDateString();
  182. }
  183. }
  184. /// <summary>
  185. /// Gets the image path.
  186. /// </summary>
  187. /// <param name="item">The item.</param>
  188. /// <returns>System.String.</returns>
  189. internal static string GetImagePath(BaseItem item)
  190. {
  191. // Try our best to find an image
  192. var path = item.PrimaryImagePath;
  193. if (string.IsNullOrEmpty(path) && item.BackdropImagePaths != null)
  194. {
  195. path = item.BackdropImagePaths.FirstOrDefault();
  196. }
  197. if (string.IsNullOrEmpty(path))
  198. {
  199. path = item.GetImage(ImageType.Thumb);
  200. }
  201. if (string.IsNullOrEmpty(path))
  202. {
  203. path = item.GetImage(ImageType.Art);
  204. }
  205. if (string.IsNullOrEmpty(path))
  206. {
  207. path = item.GetImage(ImageType.Logo);
  208. }
  209. if (string.IsNullOrEmpty(path))
  210. {
  211. path = item.GetImage(ImageType.Disc);
  212. }
  213. return path;
  214. }
  215. /// <summary>
  216. /// Displays the rating.
  217. /// </summary>
  218. /// <param name="item">The item.</param>
  219. private void DisplayRating(BaseItem item)
  220. {
  221. if (!item.CommunityRating.HasValue)
  222. {
  223. pnlRating.Visibility = Visibility.Collapsed;
  224. return;
  225. }
  226. pnlRating.Children.Clear();
  227. pnlRating.Visibility = Visibility.Visible;
  228. var rating = item.CommunityRating.Value;
  229. for (var i = 0; i < 10; i++)
  230. {
  231. Image image;
  232. if (rating < i - 1)
  233. {
  234. image = App.Instance.GetImage(new Uri("../Resources/Images/starEmpty.png", UriKind.Relative));
  235. }
  236. else if (rating < i)
  237. {
  238. image = App.Instance.GetImage(new Uri("../Resources/Images/starHalf.png", UriKind.Relative));
  239. }
  240. else
  241. {
  242. image = App.Instance.GetImage(new Uri("../Resources/Images/starFull.png", UriKind.Relative));
  243. }
  244. RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.Fant);
  245. image.Stretch = Stretch.Uniform;
  246. image.Height = 16;
  247. pnlRating.Children.Add(image);
  248. }
  249. }
  250. }
  251. }