MultiItemUpdateNotification.xaml.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using MediaBrowser.Common.Logging;
  2. using MediaBrowser.Controller.Entities;
  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.Linq;
  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 MultiItemUpdateNotification.xaml
  16. /// </summary>
  17. public partial class MultiItemUpdateNotification : 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 List<BaseItem> Items
  28. {
  29. get { return DataContext as List<BaseItem>; }
  30. }
  31. /// <summary>
  32. /// Initializes a new instance of the <see cref="MultiItemUpdateNotification" /> class.
  33. /// </summary>
  34. public MultiItemUpdateNotification(ILogger logger)
  35. {
  36. if (logger == null)
  37. {
  38. throw new ArgumentNullException("logger");
  39. }
  40. Logger = logger;
  41. InitializeComponent();
  42. Loaded += MultiItemUpdateNotification_Loaded;
  43. }
  44. /// <summary>
  45. /// Handles the Loaded event of the MultiItemUpdateNotification 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 MultiItemUpdateNotification_Loaded(object sender, RoutedEventArgs e)
  50. {
  51. header.Text = string.Format("{0} New Items!", Items.Count);
  52. PopulateItems();
  53. }
  54. /// <summary>
  55. /// Populates the items.
  56. /// </summary>
  57. private void PopulateItems()
  58. {
  59. itemsPanel.Children.Clear();
  60. var items = Items;
  61. const int maxItemsToDisplay = 8;
  62. var index = 0;
  63. foreach (var item in items)
  64. {
  65. if (index >= maxItemsToDisplay)
  66. {
  67. break;
  68. }
  69. // Try our best to find an image
  70. var path = GetImagePath(item);
  71. if (string.IsNullOrEmpty(path))
  72. {
  73. continue;
  74. }
  75. Image img;
  76. try
  77. {
  78. img = App.Instance.GetImage(path);
  79. }
  80. catch (FileNotFoundException)
  81. {
  82. Logger.Error("Image file not found {0}", path);
  83. continue;
  84. }
  85. img.Stretch = Stretch.Uniform;
  86. img.Margin = new Thickness(0, 0, 5, 5);
  87. img.ToolTip = ItemUpdateNotification.GetDisplayName(item, true);
  88. RenderOptions.SetBitmapScalingMode(img, BitmapScalingMode.Fant);
  89. itemsPanel.Children.Add(img);
  90. index++;
  91. }
  92. }
  93. /// <summary>
  94. /// Gets the image path.
  95. /// </summary>
  96. /// <param name="item">The item.</param>
  97. /// <returns>System.String.</returns>
  98. internal static string GetImagePath(BaseItem item)
  99. {
  100. // Try our best to find an image
  101. var path = item.PrimaryImagePath;
  102. if (string.IsNullOrEmpty(path) && item.BackdropImagePaths != null)
  103. {
  104. path = item.BackdropImagePaths.FirstOrDefault();
  105. }
  106. if (string.IsNullOrEmpty(path))
  107. {
  108. path = item.GetImage(ImageType.Thumb);
  109. }
  110. if (string.IsNullOrEmpty(path))
  111. {
  112. path = item.GetImage(ImageType.Art);
  113. }
  114. if (string.IsNullOrEmpty(path))
  115. {
  116. path = item.GetImage(ImageType.Logo);
  117. }
  118. if (string.IsNullOrEmpty(path))
  119. {
  120. path = item.GetImage(ImageType.Disc);
  121. }
  122. return path;
  123. }
  124. }
  125. }