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