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-05-27 05:12:28 +03:00
|
|
|
private IList<IndexerBase> _indexers = new List<IndexerBase>();
|
|
|
|
|
2011-06-14 04:23:04 +03:00
|
|
|
[Inject]
|
2011-06-18 03:10:33 +03:00
|
|
|
public IndexerProvider(IDatabase database)
|
2011-01-29 08:10:22 +02:00
|
|
|
{
|
2011-06-18 03:10:33 +03:00
|
|
|
_database = database;
|
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()
|
|
|
|
{
|
|
|
|
var all = GetAllISettings();
|
|
|
|
return _indexers.Where(i => all.Exists(c => c.IndexProviderType == i.GetType().ToString() && c.Enable)).ToList();
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual List<IndexerSetting> GetAllISettings()
|
2011-04-19 03:12:06 +03:00
|
|
|
{
|
2011-06-18 03:10:33 +03:00
|
|
|
return _database.Fetch<IndexerSetting>();
|
2011-04-19 03:12:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2011-04-19 03:12:06 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Logger.Debug("Updating Indexer settings for {0}", settings.Name);
|
2011-06-18 03:10:33 +03:00
|
|
|
_database.Update(settings);
|
2011-04-19 03:12:06 +03:00
|
|
|
}
|
2011-01-29 08:10:22 +02:00
|
|
|
}
|
|
|
|
|
2011-04-19 03:12:06 +03:00
|
|
|
public virtual IndexerSetting GetSettings(Type type)
|
2011-01-29 08:10:22 +02:00
|
|
|
{
|
2011-06-19 02:03:58 +03:00
|
|
|
return _database.Single<IndexerSetting>("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
|
|
|
{
|
2011-04-19 03:12:06 +03:00
|
|
|
Logger.Info("Initializing indexers. Count {0}", indexers.Count);
|
|
|
|
|
2011-05-27 05:12:28 +03:00
|
|
|
_indexers = indexers;
|
|
|
|
|
|
|
|
var currentIndexers = GetAllISettings();
|
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-05-27 05:12:28 +03:00
|
|
|
var settings = new IndexerSetting
|
2011-04-19 03:12:06 +03:00
|
|
|
{
|
|
|
|
Enable = false,
|
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
|
|
|
}
|