Swapper.NET Logo

Source Code Interfaces ~ ISearchEngine.cs


using System;
using System.Collections;
using System.Diagnostics;
using RevolutionaryStuff.JBT;
using RevolutionaryStuff.JBT.Filters;

namespace RevolutionaryStuff.P2P.Basics
{
	public interface ISearchEngine
	{
		ISearchHandle Search(ISearchParams searchParams);
	}

	[Flags]
	public enum SearchPerimeterFlags
	{
		Local = 1,
		Intranet = 2,
		Internet = 4|Intranet,
		All = Local|Intranet|Internet
	}

	public interface ISearchParams
	{
		string DisplayName {get;}
		Guid Id {get;}
		SearchPerimeterFlags SearchPermimeter {get;}
	}

	public interface IFileSourceSearchParams : ISearchParams
	{
		string KeyPhrase { get; }
		bool IsMatch(IFileSource fs);
	}

	public interface ISearchHandle
	{
		event SearchResultsFoundEventHandler ResultsFound;
		void Start();
		void Abort();
	}

	public delegate void SearchResultsFoundEventHandler(object sender, SearchResultsFoundEventArgs e);

	public class SearchResultsFoundEventArgs : EventArgs 
	{
		public readonly ICollection Items;

		public SearchResultsFoundEventArgs(ICollection items)
		{
			this.Items = items;
		}			
	}

	#region Search Params Implementation

	public abstract class BaseSearchParams : ISearchParams
	{
		protected BaseSearchParams(){}

		#region ISearchParams Members

		public virtual string DisplayName 
		{
			[DebuggerStepThrough]
			get { return DisplayName_p; }
			[DebuggerStepThrough]
			set { DisplayName_p = value; }
		}
		private string DisplayName_p;

		public Guid Id
		{
			[DebuggerStepThrough]
			get { return this.Id_p; }
			[DebuggerStepThrough]
			set { this.Id_p = value; }
		}
		private Guid Id_p;

		public SearchPerimeterFlags SearchPermimeter
		{	
			[DebuggerStepThrough]
			get { return this.SearchPermimeter_p; }
			[DebuggerStepThrough]
			set { this.SearchPermimeter_p = value; }
		}
		private SearchPerimeterFlags SearchPermimeter_p = SearchPerimeterFlags.All;

		public object Tag
		{
			[DebuggerStepThrough]
			get { return this.Tag_p; }
			[DebuggerStepThrough]
			set { this.Tag_p = value; }
		}
		private object Tag_p;

		#endregion
	}

	public abstract class BaseFileSourceSearchParams : BaseSearchParams, IFileSourceSearchParams
	{
		protected BaseFileSourceSearchParams(string keyPhrase)
		{
			this.KeyPhrase_p = keyPhrase;		
		}

		public abstract bool IsMatch(IFileSource fs);

		public string KeyPhrase
		{
			get { return this.KeyPhrase_p; }
		}
		private readonly string KeyPhrase_p;
	}

	public class SimpleFileSourceSearchParams : BaseFileSourceSearchParams
	{
		protected readonly long SizeMin;
		protected readonly long SizeMax;
		protected readonly IFilter KeyPhraseFilter;
		protected readonly string Urn;
		protected readonly string MimeExpression;
		private readonly System.Text.RegularExpressions.Regex MimeRegex;
		public IList FileSourceFilters
		{
			get 
			{
				if (null==FileSourceFilters_p)
				{
					FileSourceFilters_p = new ArrayListWithEvents(typeof(IFilter));
				}
				return FileSourceFilters_p; 
			}
		}
		private IList FileSourceFilters_p;

		#region Constructors
		public SimpleFileSourceSearchParams(string keyPhrase, string urn) : this(keyPhrase, urn, null, 0, long.MaxValue)
		{}

		public SimpleFileSourceSearchParams(string keyPhrase, string urn, string mimeExpression, long sizeMin, long sizeMax) : base(keyPhrase)
		{
			if (sizeMin<0) throw new ArgumentException("sizeMin");
			if (sizeMax0)
						{
							parts.Add( string.Format("Size>={0}", Format.FileSize(this.SizeMin)) );
						}
						if (this.SizeMax0)
					{
						dn = dn + Format.List(parts, null, " AND {0}");
					}
					this.DisplayName = dn;
				}
				return DisplayName_p;
			}
			[DebuggerStepThrough]
			set { this.DisplayName_p = value; }
		}
		private string DisplayName_p = null;

		public override bool IsMatch(IFileSource fs)
		{
			if (null==fs) return false;
			try
			{
				if (fs.Sizethis.SizeMax) return false;				
				string name = fs.Name;
				if (this.MimeRegex!=null)
				{
					string mime = ContentTypeStuff.ContentTypeFromFileName(name) as string;
					if (mime!=null)
					{
						if (!this.MimeRegex.IsMatch(mime)) return false;
					}
				}
				if (this.KeyPhraseFilter.IsMatch(name))
				{
					if (this.FileSourceFilters_p!=null)
					{
						foreach (IFilter f in this.FileSourceFilters_p)
						{
							if (!f.IsMatch(fs)) return false;
						}
					}
					return true;	
				}
			}
			catch (Exception) {}
			return false;			
		}
	}

	#endregion
}