2013-02-24 23:47:57 +00:00
|
|
|
|
using System;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Common.Eventing;
|
|
|
|
|
using NzbDrone.Core.Download;
|
2013-04-07 18:13:09 +00:00
|
|
|
|
using NzbDrone.Core.MediaFiles.Events;
|
2013-02-24 23:47:57 +00:00
|
|
|
|
using NzbDrone.Core.Tv;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.ExternalNotification
|
|
|
|
|
{
|
|
|
|
|
public abstract class ExternalNotificationBase
|
|
|
|
|
: IHandle<EpisodeGrabbedEvent>,
|
|
|
|
|
IHandle<EpisodeDownloadedEvent>,
|
|
|
|
|
IHandle<SeriesRenamedEvent>
|
|
|
|
|
{
|
|
|
|
|
private readonly IExternalNotificationRepository _externalNotificationRepository;
|
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
|
|
protected ExternalNotificationBase(IExternalNotificationRepository externalNotificationRepository, Logger logger)
|
|
|
|
|
{
|
|
|
|
|
_externalNotificationRepository = externalNotificationRepository;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public abstract string Name { get; }
|
|
|
|
|
|
|
|
|
|
public bool NotifyOnGrab
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return GetEnableStatus(c => c.OnGrab);
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
SetEnableStatus(c => c.OnGrab = value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool NotifyOnDownload
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return GetEnableStatus(c => c.OnDownload);
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
SetEnableStatus(c => c.OnDownload = value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool NotifyOnRename
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return GetEnableStatus(c => c.OnRename);
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
SetEnableStatus(c => c.OnRename = value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetEnableStatus(Action<ExternalNotificationDefinition> updateAction)
|
|
|
|
|
{
|
|
|
|
|
var def = _externalNotificationRepository.Get(Name) ??
|
|
|
|
|
new ExternalNotificationDefinition { Name = Name };
|
|
|
|
|
|
|
|
|
|
updateAction(def);
|
2013-03-24 04:16:00 +00:00
|
|
|
|
_externalNotificationRepository.Upsert(def);
|
2013-02-24 23:47:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool GetEnableStatus(Func<ExternalNotificationDefinition, bool> readFunction)
|
|
|
|
|
{
|
|
|
|
|
var def = _externalNotificationRepository.Get(Name) ??
|
|
|
|
|
new ExternalNotificationDefinition { Name = Name };
|
|
|
|
|
|
|
|
|
|
return readFunction(def);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Handle(EpisodeGrabbedEvent message)
|
|
|
|
|
{
|
|
|
|
|
if (NotifyOnGrab)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_logger.Trace("Sending grab notification to {0}", Name);
|
2013-04-15 01:41:39 +00:00
|
|
|
|
OnGrab(message.Episode.GetDownloadTitle());
|
2013-02-24 23:47:57 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.WarnException("Couldn't send grab notification to " + Name, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Handle(EpisodeDownloadedEvent message)
|
|
|
|
|
{
|
|
|
|
|
if (NotifyOnDownload)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_logger.Trace("Sending download notification to {0}", Name);
|
2013-04-23 06:14:55 +00:00
|
|
|
|
OnDownload(message.ParsedEpisodeInfo.ToString(), message.Series);
|
2013-02-24 23:47:57 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.WarnException("Couldn't send download notification to " + Name, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Handle(SeriesRenamedEvent message)
|
|
|
|
|
{
|
|
|
|
|
if (NotifyOnRename)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_logger.Trace("Sending rename notification to {0}", Name);
|
|
|
|
|
AfterRename(message.Series);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.WarnException("Couldn't send rename notification to " + Name, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual void OnGrab(string message)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void OnDownload(string message, Series series)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void AfterRename(Series series)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|