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