1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-12 11:15:43 +02:00
Sonarr/NzbDrone.Core/Providers/ExternalNotificationProvider.cs

112 lines
3.8 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using Ninject;
using NLog;
using NzbDrone.Core.Model;
using NzbDrone.Core.Providers.ExternalNotification;
using NzbDrone.Core.Repository;
using PetaPoco;
namespace NzbDrone.Core.Providers
{
2011-04-08 19:24:17 +03:00
public class ExternalNotificationProvider
{
2011-04-10 05:44:01 +03:00
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly IDatabase _database;
private IEnumerable<ExternalNotificationBase> _notifiers;
[Inject]
public ExternalNotificationProvider(IDatabase database, IEnumerable<ExternalNotificationBase> notifiers)
{
_database = database;
_notifiers = notifiers;
}
public ExternalNotificationProvider()
{
}
public virtual List<ExternalNotificationDefinition> All()
{
return _database.Fetch<ExternalNotificationDefinition>();
}
public virtual void SaveSettings(ExternalNotificationDefinition settings)
{
if (settings.Id == 0)
{
Logger.Debug("Adding External Notification definition for {0}", settings.Name);
_database.Insert(settings);
}
else
{
Logger.Debug("Updating External Notification definition for {0}", settings.Name);
_database.Update(settings);
}
}
public virtual ExternalNotificationDefinition GetSettings(Type type)
{
return _database.SingleOrDefault<ExternalNotificationDefinition>("WHERE ExternalNotificationProviderType = @0", type.ToString());
}
public virtual IList<ExternalNotificationBase> GetEnabledExternalNotifiers()
{
var all = All();
return _notifiers.Where(i => all.Exists(c => c.ExternalNotificationProviderType == i.GetType().ToString() && c.Enable)).ToList();
}
public virtual void InitializeNotifiers(IList<ExternalNotificationBase> notifiers)
{
Logger.Info("Initializing notifiers. Count {0}", notifiers.Count);
_notifiers = notifiers;
var currentNotifiers = All();
foreach (var notificationProvider in notifiers)
{
ExternalNotificationBase externalNotificationProviderLocal = notificationProvider;
if (!currentNotifiers.Exists(c => c.ExternalNotificationProviderType == externalNotificationProviderLocal.GetType().ToString()))
{
var settings = new ExternalNotificationDefinition
{
Enable = false,
ExternalNotificationProviderType = externalNotificationProviderLocal.GetType().ToString(),
Name = externalNotificationProviderLocal.Name
};
SaveSettings(settings);
}
}
}
public virtual void OnGrab(string message)
{
foreach (var notifier in _notifiers.Where(i => GetSettings(i.GetType()).Enable))
{
notifier.OnGrab(message);
}
}
public virtual void OnDownload(string message, Series series)
{
foreach (var notifier in _notifiers.Where(i => GetSettings(i.GetType()).Enable))
{
notifier.OnDownload(message, series);
}
}
public virtual void OnRename(string message, Series series)
{
foreach (var notifier in _notifiers.Where(i => GetSettings(i.GetType()).Enable))
{
notifier.OnRename(message, series);
}
}
}
2011-04-10 05:44:01 +03:00
}