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