2011-03-06 20:45:35 +00:00
|
|
|
|
using System;
|
2011-04-29 06:06:13 +00:00
|
|
|
|
using System.Collections.Generic;
|
2011-06-14 01:23:04 +00:00
|
|
|
|
using Ninject;
|
2011-03-06 20:45:35 +00:00
|
|
|
|
using NLog;
|
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
|
|
|
|
|
2011-06-14 01:23:04 +00:00
|
|
|
|
[Inject]
|
2011-06-17 19:18:50 +00:00
|
|
|
|
public ExternalNotificationProvider(IDatabase database)
|
2011-03-06 20:45:35 +00:00
|
|
|
|
{
|
2011-06-17 19:18:50 +00:00
|
|
|
|
_database = database;
|
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-04-29 06:06:13 +00:00
|
|
|
|
public virtual List<ExternalNotificationSetting> All()
|
2011-03-06 20:45:35 +00:00
|
|
|
|
{
|
2011-06-17 19:18:50 +00:00
|
|
|
|
return _database.Fetch<ExternalNotificationSetting>();
|
2011-04-29 06:06:13 +00:00
|
|
|
|
}
|
2011-03-06 20:45:35 +00:00
|
|
|
|
|
2011-04-29 06:06:13 +00:00
|
|
|
|
public virtual void SaveSettings(ExternalNotificationSetting settings)
|
|
|
|
|
{
|
|
|
|
|
if (settings.Id == 0)
|
2011-03-06 20:45:35 +00:00
|
|
|
|
{
|
2011-04-29 06:06:13 +00:00
|
|
|
|
Logger.Debug("Adding External Notification settings 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
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Updating External Notification settings 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-04-29 06:06:13 +00:00
|
|
|
|
public virtual ExternalNotificationSetting GetSettings(Type type)
|
|
|
|
|
{
|
2011-06-17 19:18:50 +00:00
|
|
|
|
return _database.SingleOrDefault<ExternalNotificationSetting>("WHERE NotifierName = @0", type.ToString());
|
2011-03-06 20:45:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-04-29 06:06:13 +00:00
|
|
|
|
public virtual ExternalNotificationSetting GetSettings(int id)
|
2011-03-06 20:45:35 +00:00
|
|
|
|
{
|
2011-06-17 19:18:50 +00:00
|
|
|
|
return _database.SingleOrDefault<ExternalNotificationSetting>(id);
|
2011-04-29 06:06:13 +00:00
|
|
|
|
}
|
2011-03-06 20:45:35 +00:00
|
|
|
|
|
2011-04-29 06:06:13 +00:00
|
|
|
|
public virtual void InitializeNotifiers(IList<ExternalNotificationProviderBase> notifiers)
|
|
|
|
|
{
|
|
|
|
|
Logger.Info("Initializing notifiers. Count {0}", notifiers.Count);
|
2011-03-06 20:45:35 +00:00
|
|
|
|
|
2011-04-29 06:06:13 +00:00
|
|
|
|
var currentNotifiers = All();
|
2011-03-06 20:45:35 +00:00
|
|
|
|
|
2011-04-29 06:06:13 +00:00
|
|
|
|
foreach (var feedProvider in notifiers)
|
2011-03-06 20:45:35 +00:00
|
|
|
|
{
|
2011-04-29 06:06:13 +00:00
|
|
|
|
ExternalNotificationProviderBase externalNotificationProviderLocal = feedProvider;
|
|
|
|
|
if (!currentNotifiers.Exists(c => c.NotifierName == externalNotificationProviderLocal.GetType().ToString()))
|
|
|
|
|
{
|
|
|
|
|
var settings = new ExternalNotificationSetting()
|
|
|
|
|
{
|
|
|
|
|
Enabled = false,
|
|
|
|
|
NotifierName = externalNotificationProviderLocal.GetType().ToString(),
|
|
|
|
|
Name = externalNotificationProviderLocal.Name
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SaveSettings(settings);
|
|
|
|
|
}
|
2011-03-06 20:45:35 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-04-10 02:44:01 +00:00
|
|
|
|
}
|