MultiItemUpdateNotification.xaml.cs 4.1 KB

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