1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-11 13:32:42 +01:00
Radarr/NzbDrone.Core/Providers/IndexerProvider.cs

86 lines
2.6 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using Ninject;
using NLog;
using NzbDrone.Core.Providers.Indexer;
using NzbDrone.Core.Repository;
2011-06-18 02:10:33 +02:00
using PetaPoco;
namespace NzbDrone.Core.Providers
{
public class IndexerProvider
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
2011-06-18 02:10:33 +02:00
private readonly IDatabase _database;
2011-06-22 03:12:20 +02:00
private IEnumerable<IndexerBase> _indexers;
[Inject]
2011-06-22 03:12:20 +02:00
public IndexerProvider(IDatabase database, IEnumerable<IndexerBase> indexers)
{
2011-06-18 02:10:33 +02:00
_database = database;
2011-06-22 03:12:20 +02:00
_indexers = indexers;
}
public IndexerProvider()
{
}
public virtual IList<IndexerBase> GetEnabledIndexers()
{
2011-07-08 07:41:08 +02:00
var all = All();
return _indexers.Where(i => all.Exists(c => c.IndexProviderType == i.GetType().ToString() && c.Enable)).ToList();
}
2011-07-08 07:41:08 +02:00
public virtual List<IndexerDefinition> All()
{
2011-07-08 05:27:11 +02:00
return _database.Fetch<IndexerDefinition>();
}
2011-07-08 05:27:11 +02:00
public virtual void SaveSettings(IndexerDefinition definitions)
{
2011-07-08 05:27:11 +02:00
if (definitions.Id == 0)
{
2011-07-08 05:27:11 +02:00
Logger.Debug("Adding Indexer definitions for {0}", definitions.Name);
_database.Insert(definitions);
}
else
{
2011-07-08 05:27:11 +02:00
Logger.Debug("Updating Indexer definitions for {0}", definitions.Name);
_database.Update(definitions);
}
}
2011-07-08 05:27:11 +02:00
public virtual IndexerDefinition GetSettings(Type type)
{
2011-07-08 05:27:11 +02:00
return _database.Single<IndexerDefinition>("WHERE IndexProviderType = @0", type.ToString());
}
public virtual void InitializeIndexers(IList<IndexerBase> indexers)
{
Logger.Debug("Initializing indexers. Count {0}", indexers.Count);
_indexers = indexers;
2011-07-08 07:41:08 +02:00
var currentIndexers = All();
foreach (var feedProvider in indexers)
{
IndexerBase indexerLocal = feedProvider;
if (!currentIndexers.Exists(c => c.IndexProviderType == indexerLocal.GetType().ToString()))
{
2011-07-08 05:27:11 +02:00
var settings = new IndexerDefinition
{
Enable = indexerLocal.EnabledByDefault,
IndexProviderType = indexerLocal.GetType().ToString(),
Name = indexerLocal.Name
};
SaveSettings(settings);
}
}
}
}
2011-04-10 04:44:01 +02:00
}