using MediaBrowser.Common.Events;
using MediaBrowser.Common.Kernel;
using MediaBrowser.Common.Plugins;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Plugins;
using MediaBrowser.Controller.Updates;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Updates;
using System;
namespace MediaBrowser.ServerApplication
{
///
/// Class WebSocketEvents
///
public class WebSocketEvents : IServerEntryPoint
{
///
/// The _server manager
///
private readonly IServerManager _serverManager;
///
/// The _logger
///
private readonly ILogger _logger;
///
/// The _user manager
///
private readonly IUserManager _userManager;
///
/// The _library manager
///
private readonly ILibraryManager _libraryManager;
///
/// The _installation manager
///
private readonly IInstallationManager _installationManager;
///
/// The _kernel
///
private readonly IKernel _kernel;
///
/// Initializes a new instance of the class.
///
/// The server manager.
/// The logger.
/// The user manager.
public WebSocketEvents(IServerManager serverManager, IKernel kernel, ILogger logger, IUserManager userManager, ILibraryManager libraryManager, IInstallationManager installationManager)
{
_serverManager = serverManager;
_logger = logger;
_userManager = userManager;
_libraryManager = libraryManager;
_installationManager = installationManager;
_kernel = kernel;
}
public void Run()
{
_userManager.UserDeleted += userManager_UserDeleted;
_userManager.UserUpdated += userManager_UserUpdated;
_libraryManager.LibraryChanged += libraryManager_LibraryChanged;
_kernel.HasPendingRestartChanged += kernel_HasPendingRestartChanged;
_installationManager.PluginUninstalled += InstallationManager_PluginUninstalled;
_installationManager.PackageInstalling += installationManager_PackageInstalling;
_installationManager.PackageInstallationCancelled += installationManager_PackageInstallationCancelled;
_installationManager.PackageInstallationCompleted += installationManager_PackageInstallationCompleted;
_installationManager.PackageInstallationFailed += installationManager_PackageInstallationFailed;
}
///
/// Installations the manager_ package installation failed.
///
/// The sender.
/// The e.
void installationManager_PackageInstallationFailed(object sender, GenericEventArgs e)
{
_serverManager.SendWebSocketMessage("PackageInstallationFailed", e.Argument);
}
///
/// Installations the manager_ package installation completed.
///
/// The sender.
/// The e.
void installationManager_PackageInstallationCompleted(object sender, GenericEventArgs e)
{
_serverManager.SendWebSocketMessage("PackageInstallationCompleted", e.Argument);
}
///
/// Installations the manager_ package installation cancelled.
///
/// The sender.
/// The e.
void installationManager_PackageInstallationCancelled(object sender, GenericEventArgs e)
{
_serverManager.SendWebSocketMessage("PackageInstallationCancelled", e.Argument);
}
///
/// Installations the manager_ package installing.
///
/// The sender.
/// The e.
void installationManager_PackageInstalling(object sender, GenericEventArgs e)
{
_serverManager.SendWebSocketMessage("PackageInstalling", e.Argument);
}
///
/// 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)
{
_serverManager.SendWebSocketMessage("LibraryChanged", () => DtoBuilder.GetLibraryUpdateInfo(e));
}
///
/// Installations the manager_ plugin uninstalled.
///
/// The sender.
/// The e.
void InstallationManager_PluginUninstalled(object sender, GenericEventArgs e)
{
_serverManager.SendWebSocketMessage("PluginUninstalled", e.Argument.GetPluginInfo());
}
///
/// Handles the HasPendingRestartChanged event of the kernel control.
///
/// The source of the event.
/// The instance containing the event data.
void kernel_HasPendingRestartChanged(object sender, EventArgs e)
{
var kernel = (IKernel)sender;
_serverManager.SendWebSocketMessage("HasPendingRestartChanged", kernel.GetSystemInfo());
}
///
/// Users the manager_ user updated.
///
/// The sender.
/// The e.
void userManager_UserUpdated(object sender, GenericEventArgs e)
{
_serverManager.SendWebSocketMessage("UserUpdated", new DtoBuilder(_logger).GetDtoUser(e.Argument));
}
///
/// Users the manager_ user deleted.
///
/// The sender.
/// The e.
void userManager_UserDeleted(object sender, GenericEventArgs e)
{
_serverManager.SendWebSocketMessage("UserDeleted", e.Argument.Id.ToString());
}
///
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
///
public void Dispose()
{
Dispose(true);
}
///
/// Releases unmanaged and - optionally - managed resources.
///
/// true to release both managed and unmanaged resources; false to release only unmanaged resources.
protected virtual void Dispose(bool dispose)
{
if (dispose)
{
_userManager.UserDeleted -= userManager_UserDeleted;
_userManager.UserUpdated -= userManager_UserUpdated;
_libraryManager.LibraryChanged -= libraryManager_LibraryChanged;
_installationManager.PluginUninstalled -= InstallationManager_PluginUninstalled;
_installationManager.PackageInstalling -= installationManager_PackageInstalling;
_installationManager.PackageInstallationCancelled -= installationManager_PackageInstallationCancelled;
_installationManager.PackageInstallationCompleted -= installationManager_PackageInstallationCompleted;
_installationManager.PackageInstallationFailed -= installationManager_PackageInstallationFailed;
_kernel.HasPendingRestartChanged -= kernel_HasPendingRestartChanged;
}
}
}
}