1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-12 11:15:43 +02:00
Sonarr/NzbDrone.Core/Providers/IndexerProvider.cs

85 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 03:10:33 +03:00
using PetaPoco;
namespace NzbDrone.Core.Providers
{
public class IndexerProvider
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
2011-06-18 03:10:33 +03:00
private readonly IDatabase _database;
private IList<IndexerBase> _indexers = new List<IndexerBase>();
[Inject]
2011-06-18 03:10:33 +03:00
public IndexerProvider(IDatabase database)
{
2011-06-18 03:10:33 +03:00
_database = database;
}
public IndexerProvider()
{
}
public virtual IList<IndexerBase> GetEnabledIndexers()
{
var all = GetAllISettings();
return _indexers.Where(i => all.Exists(c => c.IndexProviderType == i.GetType().ToString() && c.Enable)).ToList();
}
public virtual List<IndexerSetting> GetAllISettings()
{
2011-06-18 03:10:33 +03:00
return _database.Fetch<IndexerSetting>();
}
public virtual void SaveSettings(IndexerSetting settings)
{
if (settings.Id == 0)
{
Logger.Debug("Adding Indexer settings for {0}", settings.Name);
2011-06-18 03:10:33 +03:00
_database.Insert(settings);
}
else
{
Logger.Debug("Updating Indexer settings for {0}", settings.Name);
2011-06-18 03:10:33 +03:00
_database.Update(settings);
}
}
public virtual IndexerSetting GetSettings(Type type)
{
return _database.Single<IndexerSetting>("WHERE IndexProviderType = @0", type.ToString());
}
public virtual void InitializeIndexers(IList<IndexerBase> indexers)
{
Logger.Info("Initializing indexers. Count {0}", indexers.Count);
_indexers = indexers;
var currentIndexers = GetAllISettings();
foreach (var feedProvider in indexers)
{
IndexerBase indexerLocal = feedProvider;
if (!currentIndexers.Exists(c => c.IndexProviderType == indexerLocal.GetType().ToString()))
{
var settings = new IndexerSetting
{
Enable = false,
IndexProviderType = indexerLocal.GetType().ToString(),
Name = indexerLocal.Name
};
SaveSettings(settings);
}
}
}
}
2011-04-10 05:44:01 +03:00
}