ItemUpdateNotification.xaml.cs 7.7 KB

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