From 6481688d2acc23b789dab6e8ad04a96e48c46064 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 3 May 2013 00:10:11 -0400 Subject: fixes #223 - New Content Localhost Popups Repeat 'Old' 'New Content' on Media Changes --- .../Controls/ItemUpdateNotification.xaml | 63 ----- .../Controls/ItemUpdateNotification.xaml.cs | 294 --------------------- .../EntryPoints/NewItemNotifier.cs | 135 ---------- .../EntryPoints/WebSocketEvents.cs | 95 ++++++- .../MediaBrowser.ServerApplication.csproj | 8 - 5 files changed, 83 insertions(+), 512 deletions(-) delete mode 100644 MediaBrowser.ServerApplication/Controls/ItemUpdateNotification.xaml delete mode 100644 MediaBrowser.ServerApplication/Controls/ItemUpdateNotification.xaml.cs delete mode 100644 MediaBrowser.ServerApplication/EntryPoints/NewItemNotifier.cs (limited to 'MediaBrowser.ServerApplication') diff --git a/MediaBrowser.ServerApplication/Controls/ItemUpdateNotification.xaml b/MediaBrowser.ServerApplication/Controls/ItemUpdateNotification.xaml deleted file mode 100644 index 22a3ea696c..0000000000 --- a/MediaBrowser.ServerApplication/Controls/ItemUpdateNotification.xaml +++ /dev/null @@ -1,63 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/MediaBrowser.ServerApplication/Controls/ItemUpdateNotification.xaml.cs b/MediaBrowser.ServerApplication/Controls/ItemUpdateNotification.xaml.cs deleted file mode 100644 index 30e39f6b02..0000000000 --- a/MediaBrowser.ServerApplication/Controls/ItemUpdateNotification.xaml.cs +++ /dev/null @@ -1,294 +0,0 @@ -using System.Linq; -using MediaBrowser.Controller.Entities; -using MediaBrowser.Controller.Entities.TV; -using MediaBrowser.Model.Entities; -using MediaBrowser.Model.Logging; -using System; -using System.Collections.Generic; -using System.IO; -using System.Windows; -using System.Windows.Controls; -using System.Windows.Media; - -namespace MediaBrowser.ServerApplication.Controls -{ - /// - /// Interaction logic for ItemUpdateNotification.xaml - /// - public partial class ItemUpdateNotification : UserControl - { - /// - /// The logger - /// - private readonly ILogger Logger; - - /// - /// Gets the children changed event args. - /// - /// The children changed event args. - private BaseItem Item - { - get { return DataContext as BaseItem; } - } - - /// - /// Initializes a new instance of the class. - /// - public ItemUpdateNotification(ILogger logger) - { - if (logger == null) - { - throw new ArgumentNullException("logger"); - } - - Logger = logger; - - InitializeComponent(); - - Loaded += ItemUpdateNotification_Loaded; - } - - /// - /// Handles the Loaded event of the ItemUpdateNotification control. - /// - /// The source of the event. - /// The instance containing the event data. - void ItemUpdateNotification_Loaded(object sender, RoutedEventArgs e) - { - DisplayItem(Item); - } - - /// - /// Gets the display name. - /// - /// The item. - /// if set to true [include parent name]. - /// System.String. - internal static string GetDisplayName(BaseItem item, bool includeParentName) - { - var name = item.Name; - - if (item.ProductionYear.HasValue && !(item is Episode)) - { - name += string.Format(" ({0})", item.ProductionYear); - } - - var episode = item as Episode; - if (episode != null) - { - var indexNumbers = new List(); - - if (episode.Season.IndexNumber.HasValue) - { - indexNumbers.Add(episode.Season.IndexNumber.Value); - } - if (episode.IndexNumber.HasValue) - { - indexNumbers.Add(episode.IndexNumber.Value); - } - var indexNumber = string.Join(".", indexNumbers.ToArray()); - - name = string.Format("{0} - {1}", indexNumber, name); - - if (includeParentName) - { - name = episode.Series.Name + " - " + name; - } - } - - if (includeParentName) - { - var season = item as Season; - - if (season != null) - { - name = season.Series.Name + " - " + name; - } - } - - return name; - } - - /// - /// Displays the parent title. - /// - /// The item. - private void DisplayParentTitle(BaseItem item) - { - if (!(item is Episode || item is Season)) - { - txtParentName.Visibility = Visibility.Collapsed; - imgParentLogo.Visibility = Visibility.Collapsed; - return; - } - - var series = item is Episode ? (item as Episode).Series : (item as Season).Series; - - var logo = series.GetImage(ImageType.Logo); - - if (string.IsNullOrEmpty(logo)) - { - imgParentLogo.Visibility = Visibility.Collapsed; - txtParentName.Visibility = Visibility.Visible; - } - else - { - imgParentLogo.Visibility = Visibility.Visible; - txtParentName.Visibility = Visibility.Collapsed; - imgParentLogo.Source = App.Instance.GetBitmapImage(logo); - } - - txtParentName.Text = series.Name; - } - - /// - /// Displays the title. - /// - /// The item. - private void DisplayTitle(BaseItem item) - { - txtName.Text = GetDisplayName(item, false); - } - - /// - /// Displays the item. - /// - /// The item. - private void DisplayItem(BaseItem item) - { - DisplayParentTitle(item); - DisplayTitle(item); - DisplayRating(item); - - var path = GetImagePath(item); - - if (string.IsNullOrEmpty(path)) - { - img.Visibility = Visibility.Collapsed; - } - else - { - img.Visibility = Visibility.Visible; - - try - { - img.Source = App.Instance.GetBitmapImage(path); - } - catch (FileNotFoundException) - { - Logger.Error("Image file not found {0}", path); - } - } - - if (string.IsNullOrEmpty(item.Overview)) - { - txtOverview.Visibility = Visibility.Collapsed; - } - else - { - txtOverview.Visibility = Visibility.Visible; - txtOverview.Text = item.Overview; - } - - if (item.Taglines == null || item.Taglines.Count == 0) - { - txtTagline.Visibility = Visibility.Collapsed; - } - else - { - txtTagline.Visibility = Visibility.Visible; - txtTagline.Text = item.Taglines[0]; - } - - if (!item.PremiereDate.HasValue) - { - txtPremeireDate.Visibility = Visibility.Collapsed; - } - else - { - txtPremeireDate.Visibility = Visibility.Visible; - txtPremeireDate.Text = "Premiered " + item.PremiereDate.Value.ToLocalTime().ToShortDateString(); - } - } - - /// - /// Gets the image path. - /// - /// The item. - /// System.String. - internal static string GetImagePath(BaseItem item) - { - // Try our best to find an image - var path = item.PrimaryImagePath; - - if (string.IsNullOrEmpty(path) && item.BackdropImagePaths != null) - { - path = item.BackdropImagePaths.FirstOrDefault(); - } - - if (string.IsNullOrEmpty(path)) - { - path = item.GetImage(ImageType.Thumb); - } - - if (string.IsNullOrEmpty(path)) - { - path = item.GetImage(ImageType.Art); - } - - if (string.IsNullOrEmpty(path)) - { - path = item.GetImage(ImageType.Logo); - } - - if (string.IsNullOrEmpty(path)) - { - path = item.GetImage(ImageType.Disc); - } - - return path; - } - - /// - /// Displays the rating. - /// - /// The item. - private void DisplayRating(BaseItem item) - { - if (!item.CommunityRating.HasValue) - { - pnlRating.Visibility = Visibility.Collapsed; - return; - } - - pnlRating.Children.Clear(); - pnlRating.Visibility = Visibility.Visible; - - var rating = item.CommunityRating.Value; - - for (var i = 0; i < 10; i++) - { - Image image; - if (rating < i - 1) - { - image = App.Instance.GetImage(new Uri("../Resources/Images/starEmpty.png", UriKind.Relative)); - } - else if (rating < i) - { - image = App.Instance.GetImage(new Uri("../Resources/Images/starHalf.png", UriKind.Relative)); - } - else - { - image = App.Instance.GetImage(new Uri("../Resources/Images/starFull.png", UriKind.Relative)); - } - - RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.Fant); - - image.Stretch = Stretch.Uniform; - image.Height = 16; - - pnlRating.Children.Add(image); - } - } - } -} diff --git a/MediaBrowser.ServerApplication/EntryPoints/NewItemNotifier.cs b/MediaBrowser.ServerApplication/EntryPoints/NewItemNotifier.cs deleted file mode 100644 index da39520c9a..0000000000 --- a/MediaBrowser.ServerApplication/EntryPoints/NewItemNotifier.cs +++ /dev/null @@ -1,135 +0,0 @@ -using MediaBrowser.Controller.Entities; -using MediaBrowser.Controller.Library; -using MediaBrowser.Controller.Plugins; -using MediaBrowser.Model.Logging; -using MediaBrowser.ServerApplication.Controls; -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Windows; -using System.Windows.Controls.Primitives; - -namespace MediaBrowser.ServerApplication.EntryPoints -{ - /// - /// Class NewItemNotifier - /// - public class NewItemNotifier - { - /// - /// Holds the list of new items to display when the NewItemTimer expires - /// - private readonly List _newlyAddedItems = new List(); - - /// - /// The amount of time to wait before showing a new item notification - /// This allows us to group items together into one notification - /// - private const int NewItemDelay = 60000; - - /// - /// The current new item timer - /// - /// The new item timer. - private Timer NewItemTimer { get; set; } - - /// - /// The _library manager - /// - private readonly ILibraryManager _libraryManager; - - /// - /// The _logger - /// - private readonly ILogger _logger; - - /// - /// Initializes a new instance of the class. - /// - /// The library manager. - /// The log manager. - public NewItemNotifier(ILibraryManager libraryManager, ILogManager logManager) - { - _logger = logManager.GetLogger("NewItemNotifier"); - _libraryManager = libraryManager; - } - - /// - /// Runs this instance. - /// - public void Run() - { - _libraryManager.LibraryChanged += libraryManager_LibraryChanged; - } - - /// - /// Handles the LibraryChanged event of the libraryManager control. - /// - /// The source of the event. - /// The instance containing the event data. - void libraryManager_LibraryChanged(object sender, ChildrenChangedEventArgs e) - { - var newItems = e.ItemsAdded.Where(i => !i.IsFolder).ToList(); - - // Use a timer to prevent lots of these notifications from showing in a short period of time - if (newItems.Count > 0) - { - lock (_newlyAddedItems) - { - _newlyAddedItems.AddRange(newItems); - - if (NewItemTimer == null) - { - NewItemTimer = new Timer(NewItemTimerCallback, null, NewItemDelay, Timeout.Infinite); - } - else - { - NewItemTimer.Change(NewItemDelay, Timeout.Infinite); - } - } - } - } - - /// - /// Called when the new item timer expires - /// - /// The state. - private void NewItemTimerCallback(object state) - { - List newItems; - - // Lock the list and release all resources - lock (_newlyAddedItems) - { - newItems = _newlyAddedItems.ToList(); - _newlyAddedItems.Clear(); - - NewItemTimer.Dispose(); - NewItemTimer = null; - } - - // Show the notification - if (newItems.Count > 0) - { - Application.Current.Dispatcher.InvokeAsync(() => - { - var window = Application.Current.Windows.OfType().First(); - - window.Dispatcher.InvokeAsync(() => window.MbTaskbarIcon.ShowCustomBalloon(new ItemUpdateNotification(_logger) - { - DataContext = newItems[0] - - }, PopupAnimation.Slide, 6000)); - }); - } - } - - /// - /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. - /// - public void Dispose() - { - _libraryManager.LibraryChanged -= libraryManager_LibraryChanged; - } - } -} diff --git a/MediaBrowser.ServerApplication/EntryPoints/WebSocketEvents.cs b/MediaBrowser.ServerApplication/EntryPoints/WebSocketEvents.cs index a6d505d198..e80c5cc1c9 100644 --- a/MediaBrowser.ServerApplication/EntryPoints/WebSocketEvents.cs +++ b/MediaBrowser.ServerApplication/EntryPoints/WebSocketEvents.cs @@ -101,10 +101,12 @@ namespace MediaBrowser.ServerApplication.EntryPoints _userManager.UserDeleted += userManager_UserDeleted; _userManager.UserUpdated += userManager_UserUpdated; - _libraryManager.LibraryChanged += libraryManager_LibraryChanged; - _appHost.HasPendingRestartChanged += kernel_HasPendingRestartChanged; + _libraryManager.ItemAdded += libraryManager_ItemAdded; + _libraryManager.ItemUpdated += libraryManager_ItemUpdated; + _libraryManager.ItemRemoved += libraryManager_ItemRemoved; + _installationManager.PluginUninstalled += InstallationManager_PluginUninstalled; _installationManager.PackageInstalling += installationManager_PackageInstalling; _installationManager.PackageInstallationCancelled += installationManager_PackageInstallationCancelled; @@ -122,7 +124,7 @@ namespace MediaBrowser.ServerApplication.EntryPoints void _taskManager_TaskExecuting(object sender, EventArgs e) { - var task = (IScheduledTask) sender; + var task = (IScheduledTask)sender; _serverManager.SendWebSocketMessage("ScheduledTaskStarted", task.Name); } @@ -167,11 +169,77 @@ namespace MediaBrowser.ServerApplication.EntryPoints } /// - /// Handles the LibraryChanged event of the libraryManager control. + /// Handles the ItemAdded event of the libraryManager control. + /// + /// The source of the event. + /// The instance containing the event data. + void libraryManager_ItemAdded(object sender, ItemChangeEventArgs e) + { + lock (_libraryChangedSyncLock) + { + if (LibraryUpdateInfo == null) + { + LibraryUpdateInfo = new LibraryUpdateInfo(); + } + + if (LibraryUpdateTimer == null) + { + LibraryUpdateTimer = new Timer(LibraryUpdateTimerCallback, null, LibraryUpdateDuration, + Timeout.Infinite); + } + else + { + LibraryUpdateTimer.Change(LibraryUpdateDuration, Timeout.Infinite); + } + + if (e.Item.Parent != null) + { + LibraryUpdateInfo.Folders.Add(e.Item.Parent.Id); + } + + LibraryUpdateInfo.ItemsAdded.Add(e.Item.Id); + } + } + + /// + /// Handles the ItemUpdated event of the libraryManager control. + /// + /// The source of the event. + /// The instance containing the event data. + void libraryManager_ItemUpdated(object sender, ItemChangeEventArgs e) + { + lock (_libraryChangedSyncLock) + { + if (LibraryUpdateInfo == null) + { + LibraryUpdateInfo = new LibraryUpdateInfo(); + } + + if (LibraryUpdateTimer == null) + { + LibraryUpdateTimer = new Timer(LibraryUpdateTimerCallback, null, LibraryUpdateDuration, + Timeout.Infinite); + } + else + { + LibraryUpdateTimer.Change(LibraryUpdateDuration, Timeout.Infinite); + } + + if (e.Item.Parent != null) + { + LibraryUpdateInfo.Folders.Add(e.Item.Parent.Id); + } + + LibraryUpdateInfo.ItemsUpdated.Add(e.Item.Id); + } + } + + /// + /// Handles the ItemRemoved event of the libraryManager control. /// /// The source of the event. - /// The instance containing the event data. - void libraryManager_LibraryChanged(object sender, ChildrenChangedEventArgs e) + /// The instance containing the event data. + void libraryManager_ItemRemoved(object sender, ItemChangeEventArgs e) { lock (_libraryChangedSyncLock) { @@ -190,11 +258,12 @@ namespace MediaBrowser.ServerApplication.EntryPoints LibraryUpdateTimer.Change(LibraryUpdateDuration, Timeout.Infinite); } - LibraryUpdateInfo.Folders.Add(e.Folder.Id); + if (e.Item.Parent != null) + { + LibraryUpdateInfo.Folders.Add(e.Item.Parent.Id); + } - LibraryUpdateInfo.ItemsAdded.AddRange(e.ItemsAdded.Select(i => i.Id)); - LibraryUpdateInfo.ItemsUpdated.AddRange(e.ItemsUpdated.Select(i => i.Id)); - LibraryUpdateInfo.ItemsRemoved.AddRange(e.ItemsRemoved.Select(i => i.Id)); + LibraryUpdateInfo.ItemsRemoved.Add(e.Item.Id); } } @@ -281,12 +350,14 @@ namespace MediaBrowser.ServerApplication.EntryPoints LibraryUpdateTimer.Dispose(); LibraryUpdateTimer = null; } + + _libraryManager.ItemAdded -= libraryManager_ItemAdded; + _libraryManager.ItemUpdated -= libraryManager_ItemUpdated; + _libraryManager.ItemRemoved -= libraryManager_ItemRemoved; _userManager.UserDeleted -= userManager_UserDeleted; _userManager.UserUpdated -= userManager_UserUpdated; - _libraryManager.LibraryChanged -= libraryManager_LibraryChanged; - _installationManager.PluginUninstalled -= InstallationManager_PluginUninstalled; _installationManager.PackageInstalling -= installationManager_PackageInstalling; _installationManager.PackageInstallationCancelled -= installationManager_PackageInstallationCancelled; diff --git a/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj b/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj index 7fd8463a85..7199b5dd24 100644 --- a/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj +++ b/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj @@ -170,7 +170,6 @@ - @@ -181,10 +180,6 @@ MSBuild:Compile Designer - - Designer - MSBuild:Compile - Designer MSBuild:Compile @@ -205,9 +200,6 @@ Code - - ItemUpdateNotification.xaml - LibraryExplorer.xaml -- cgit v1.2.3