ItemUpdateNotification.xaml.cs 7.8 KB

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