using System.Collections.Generic; using System.Linq; using NLog; using NzbDrone.Common.Eventing; using NzbDrone.Core.Lifecycle; namespace NzbDrone.Core.Indexers { public interface IIndexerService { List All(); List GetAvailableIndexers(); void Save(IndexerDefinition indexer); IndexerDefinition Get(string name); } public class IndexerService : IIndexerService, IHandle { private readonly IIndexerRepository _indexerRepository; private readonly Logger _logger; private readonly IList _indexers; public IndexerService(IIndexerRepository indexerRepository, IEnumerable indexers, Logger logger) { _indexerRepository = indexerRepository; _logger = logger; _indexers = indexers.ToList(); } public List All() { return _indexerRepository.All().ToList(); } public List GetAvailableIndexers() { var enabled = All().Where(c => c.Enable).Select(c => c.Name); var configureIndexers = _indexers.Where(c => c.IsConfigured); return configureIndexers.Where(c => enabled.Contains(c.Name)).ToList(); } public void Save(IndexerDefinition indexer) { _indexerRepository.Update(indexer); } public IndexerDefinition Get(string name) { return _indexerRepository.Get(name); } public void Handle(ApplicationStartedEvent message) { _logger.Debug("Initializing indexers. Count {0}", _indexers.Count); var currentIndexers = All(); foreach (var feedProvider in _indexers) { IIndexerBase indexerLocal = feedProvider; if (!currentIndexers.Exists(c => c.Name == indexerLocal.Name)) { var settings = new IndexerDefinition { Enable = indexerLocal.EnabledByDefault, Name = indexerLocal.Name.ToLower() }; _indexerRepository.Insert(settings); } } } } }