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