@ -3,43 +3,33 @@ using System.Collections.Generic;
using System.Linq ;
using System.Linq ;
using Growl.Connector ;
using Growl.Connector ;
using NLog ;
using NLog ;
using NzbDrone.Common.Messaging ;
using GrowlNotification = Growl . Connector . Notification ;
using GrowlNotification = Growl . Connector . Notification ;
namespace NzbDrone.Core.Notifications.Growl
namespace NzbDrone.Core.Notifications.Growl
{
{
public class GrowlProvider
public interface IGrowlService
{
void SendNotification ( string title , string message , string notificationTypeName , string hostname , int port , string password ) ;
}
public class GrowlService : IGrowlService , IExecute < TestGrowlCommand >
{
{
private static readonly Logger Logger = LogManager . GetCurrentClassLogger ( ) ;
private static readonly Logger Logger = LogManager . GetCurrentClassLogger ( ) ;
private readonly Application _growlApplication = new Application ( "NzbDrone" ) ;
private readonly Application _growlApplication = new Application ( "NzbDrone" ) ;
private GrowlConnector _growlConnector ;
private GrowlConnector _growlConnector ;
private List < NotificationType > _notificationTypes ;
private readonly List < NotificationType > _notificationTypes ;
public Growl Provider ( )
public Growl Service ( )
{
{
_notificationTypes = GetNotificationTypes ( ) ;
_notificationTypes = GetNotificationTypes ( ) ;
_growlApplication . Icon = "https://github.com/NzbDrone/NzbDrone/raw/master/NzbDrone.Core/NzbDrone.jpg" ;
_growlApplication . Icon = "https://github.com/NzbDrone/NzbDrone/raw/master/NzbDrone.Core/NzbDrone.jpg" ;
}
}
public virtual void Register ( string hostname , int port , string password )
public void SendNotification ( string title , string message , string notificationTypeName , string hostname , int port , string password )
{
Logger . Trace ( "Registering NzbDrone with Growl host: {0}:{1}" , hostname , port ) ;
_growlConnector = new GrowlConnector ( password , hostname , port ) ;
_growlConnector . Register ( _growlApplication , _notificationTypes . ToArray ( ) ) ;
}
public virtual void TestNotification ( string hostname , int port , string password )
{
const string title = "Test Notification" ;
const string message = "This is a test message from NzbDrone" ;
SendNotification ( title , message , "TEST" , hostname , port , password ) ;
}
public virtual void SendNotification ( string title , string message , string notificationTypeName , string hostname , int port , string password )
{
{
var notificationType = _notificationTypes . Single ( n = > n . Name = = notificationTypeName ) ;
var notificationType = _notificationTypes . Single ( n = > n . Name = = notificationTypeName ) ;
var notification = new GrowlNotification ( "NzbDrone" , notificationType . Name , DateTime . Now . Ticks . ToString ( ) , title , message ) ;
var notification = new GrowlNotification ( "NzbDrone" , notificationType . Name , DateTime . Now . Ticks . ToString ( ) , title , message ) ;
_growlConnector = new GrowlConnector ( password , hostname , port ) ;
_growlConnector = new GrowlConnector ( password , hostname , port ) ;
@ -48,6 +38,13 @@ namespace NzbDrone.Core.Notifications.Growl
_growlConnector . Notify ( notification ) ;
_growlConnector . Notify ( notification ) ;
}
}
private void Register ( string host , int port , string password )
{
Logger . Trace ( "Registering NzbDrone with Growl host: {0}:{1}" , host , port ) ;
_growlConnector = new GrowlConnector ( password , host , port ) ;
_growlConnector . Register ( _growlApplication , _notificationTypes . ToArray ( ) ) ;
}
private List < NotificationType > GetNotificationTypes ( )
private List < NotificationType > GetNotificationTypes ( )
{
{
var notificationTypes = new List < NotificationType > ( ) ;
var notificationTypes = new List < NotificationType > ( ) ;
@ -57,5 +54,15 @@ namespace NzbDrone.Core.Notifications.Growl
return notificationTypes ;
return notificationTypes ;
}
}
public void Execute ( TestGrowlCommand message )
{
Register ( message . Host , message . Port , message . Password ) ;
const string title = "Test Notification" ;
const string body = "This is a test message from NzbDrone" ;
SendNotification ( title , body , "TEST" , message . Host , message . Port , message . Password ) ;
}
}
}
}
}