2011-11-02 03:08:53 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Growl.Connector;
|
|
|
|
|
using NLog;
|
2013-08-31 01:42:30 +00:00
|
|
|
|
using NzbDrone.Common.Instrumentation;
|
2013-09-14 06:36:07 +00:00
|
|
|
|
using NzbDrone.Core.Messaging.Commands;
|
2013-05-19 23:17:32 +00:00
|
|
|
|
using GrowlNotification = Growl.Connector.Notification;
|
2011-11-02 03:08:53 +00:00
|
|
|
|
|
2013-05-19 23:17:32 +00:00
|
|
|
|
namespace NzbDrone.Core.Notifications.Growl
|
2011-11-02 03:08:53 +00:00
|
|
|
|
{
|
2013-06-13 06:41:26 +00:00
|
|
|
|
public interface IGrowlService
|
|
|
|
|
{
|
|
|
|
|
void SendNotification(string title, string message, string notificationTypeName, string hostname, int port, string password);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class GrowlService : IGrowlService, IExecute<TestGrowlCommand>
|
2011-11-02 03:08:53 +00:00
|
|
|
|
{
|
2013-08-31 01:42:30 +00:00
|
|
|
|
private static readonly Logger Logger = NzbDroneLogger.GetLogger();
|
2011-11-02 03:08:53 +00:00
|
|
|
|
|
|
|
|
|
private readonly Application _growlApplication = new Application("NzbDrone");
|
|
|
|
|
private GrowlConnector _growlConnector;
|
2013-06-13 06:41:26 +00:00
|
|
|
|
private readonly List<NotificationType> _notificationTypes;
|
2011-11-02 03:08:53 +00:00
|
|
|
|
|
2013-06-13 06:41:26 +00:00
|
|
|
|
public GrowlService()
|
2011-11-02 03:08:53 +00:00
|
|
|
|
{
|
|
|
|
|
_notificationTypes = GetNotificationTypes();
|
2013-09-09 15:13:54 +00:00
|
|
|
|
_growlApplication.Icon = "https://raw.github.com/NzbDrone/NzbDrone/master/Logo/64.png";
|
2011-11-02 03:08:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-13 06:41:26 +00:00
|
|
|
|
public void SendNotification(string title, string message, string notificationTypeName, string hostname, int port, string password)
|
2011-11-02 03:08:53 +00:00
|
|
|
|
{
|
|
|
|
|
var notificationType = _notificationTypes.Single(n => n.Name == notificationTypeName);
|
2013-05-19 23:17:32 +00:00
|
|
|
|
var notification = new GrowlNotification("NzbDrone", notificationType.Name, DateTime.Now.Ticks.ToString(), title, message);
|
2011-11-02 03:08:53 +00:00
|
|
|
|
|
|
|
|
|
_growlConnector = new GrowlConnector(password, hostname, port);
|
|
|
|
|
|
|
|
|
|
Logger.Trace("Sending Notification to: {0}:{1}", hostname, port);
|
|
|
|
|
_growlConnector.Notify(notification);
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-13 06:41:26 +00:00
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-02 03:08:53 +00:00
|
|
|
|
private List<NotificationType> GetNotificationTypes()
|
|
|
|
|
{
|
|
|
|
|
var notificationTypes = new List<NotificationType>();
|
|
|
|
|
notificationTypes.Add(new NotificationType("TEST", "Test"));
|
|
|
|
|
notificationTypes.Add(new NotificationType("GRAB", "Episode Grabbed"));
|
|
|
|
|
notificationTypes.Add(new NotificationType("DOWNLOAD", "Episode Complete"));
|
|
|
|
|
|
|
|
|
|
return notificationTypes;
|
|
|
|
|
}
|
2013-06-13 06:41:26 +00:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2011-11-02 03:08:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|