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