@ -1,7 +1,9 @@
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 ;
@ -13,10 +15,13 @@ namespace NzbDrone.Core.Providers
private static readonly Logger Logger = LogManager . GetCurrentClassLogger ( ) ;
private readonly IDatabase _database ;
[Inject]
public ExternalNotificationProvider ( IDatabase database )
private IEnumerable < ExternalNotificationBase > _notifiers ;
[Inject]
public ExternalNotificationProvider ( IDatabase database , IEnumerable < ExternalNotificationBase > notifiers )
{
_database = database ;
_notifiers = notifiers ;
}
public ExternalNotificationProvider ( )
@ -24,51 +29,54 @@ namespace NzbDrone.Core.Providers
}
public virtual List < ExternalNotification Setting > All ( )
public virtual List < ExternalNotification Definition > All ( )
{
return _database . Fetch < ExternalNotification Setting > ( ) ;
return _database . Fetch < ExternalNotification Definition > ( ) ;
}
public virtual void SaveSettings ( ExternalNotification Setting settings )
public virtual void SaveSettings ( ExternalNotification Definition settings )
{
if ( settings . Id = = 0 )
{
Logger . Debug ( "Adding External Notification settings for {0}", settings . Name ) ;
Logger . Debug ( "Adding External Notification definition for {0}", settings . Name ) ;
_database . Insert ( settings ) ;
}
else
{
Logger . Debug ( "Updating External Notification settings for {0}", settings . Name ) ;
Logger . Debug ( "Updating External Notification definition for {0}", settings . Name ) ;
_database . Update ( settings ) ;
}
}
public virtual ExternalNotification Setting GetSettings ( Type type )
public virtual ExternalNotification Definition GetSettings ( Type type )
{
return _database . SingleOrDefault < ExternalNotification Setting> ( "WHERE NotifierNam e = @0", type . ToString ( ) ) ;
return _database . SingleOrDefault < ExternalNotification Definition> ( "WHERE ExternalNotificationProviderTyp e = @0", type . ToString ( ) ) ;
}
public virtual ExternalNotificationSetting GetSettings ( int id )
public virtual IList< ExternalNotificationBase > GetEnabledExternalNotifiers ( )
{
return _database . SingleOrDefault < ExternalNotificationSetting > ( id ) ;
var all = All ( ) ;
return _notifiers . Where ( i = > all . Exists ( c = > c . ExternalNotificationProviderType = = i . GetType ( ) . ToString ( ) & & c . Enable ) ) . ToList ( ) ;
}
public virtual void InitializeNotifiers ( IList < ExternalNotification Provider Base> notifiers )
public virtual void InitializeNotifiers ( IList < ExternalNotification Base> notifiers )
{
Logger . Info ( "Initializing notifiers. Count {0}" , notifiers . Count ) ;
_notifiers = notifiers ;
var currentNotifiers = All ( ) ;
foreach ( var feed Provider in notifiers )
foreach ( var notification Provider in notifiers )
{
ExternalNotification Provider Base externalNotificationProviderLocal = feed Provider;
if ( ! currentNotifiers . Exists ( c = > c . NotifierNam e = = externalNotificationProviderLocal . GetType ( ) . ToString ( ) ) )
ExternalNotification Base externalNotificationProviderLocal = notification Provider;
if ( ! currentNotifiers . Exists ( c = > c . ExternalNotificationProviderTyp e = = externalNotificationProviderLocal . GetType ( ) . ToString ( ) ) )
{
var settings = new ExternalNotification Setting( )
var settings = new ExternalNotification Definition
{
Enable d = false ,
NotifierNam e = externalNotificationProviderLocal . GetType ( ) . ToString ( ) ,
Enable = false ,
ExternalNotificationProviderTyp e = externalNotificationProviderLocal . GetType ( ) . ToString ( ) ,
Name = externalNotificationProviderLocal . Name
} ;
@ -76,5 +84,29 @@ namespace NzbDrone.Core.Providers
}
}
}
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 ) ;
}
}
}
}