MultiItemUpdateNotification.xaml.cs 4.1 KB

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