2022-03-25 21:19:00 +00:00
|
|
|
|
using System.Collections.Generic;
|
2022-08-16 22:45:36 +00:00
|
|
|
|
using System.Linq;
|
2022-03-25 21:19:00 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2022-09-10 15:45:41 +00:00
|
|
|
|
using Flow.Launcher.Plugin.Explorer.Search.IProvider;
|
|
|
|
|
|
using Microsoft.Search.Interop;
|
2022-03-25 21:19:00 +00:00
|
|
|
|
|
|
|
|
|
|
namespace Flow.Launcher.Plugin.Explorer.Search.WindowsIndex
|
|
|
|
|
|
{
|
2022-09-10 15:45:41 +00:00
|
|
|
|
public class WindowsIndexSearchManager : IIndexProvider, IContentIndexProvider, IPathIndexProvider
|
2022-03-25 21:19:00 +00:00
|
|
|
|
{
|
|
|
|
|
|
private Settings Settings { get; }
|
|
|
|
|
|
private QueryConstructor QueryConstructor { get; }
|
2022-09-10 15:45:41 +00:00
|
|
|
|
|
|
|
|
|
|
private CSearchQueryHelper QueryHelper { get; }
|
2022-03-28 20:28:18 +00:00
|
|
|
|
public WindowsIndexSearchManager(Settings settings)
|
2022-03-25 21:19:00 +00:00
|
|
|
|
{
|
|
|
|
|
|
Settings = settings;
|
|
|
|
|
|
QueryConstructor = new QueryConstructor(Settings);
|
2022-09-10 15:45:41 +00:00
|
|
|
|
QueryHelper = QueryConstructor.CreateQueryHelper();
|
2022-03-25 21:19:00 +00:00
|
|
|
|
}
|
2022-09-10 15:45:41 +00:00
|
|
|
|
|
2022-08-16 22:45:36 +00:00
|
|
|
|
private IAsyncEnumerable<SearchResult> WindowsIndexFileContentSearchAsync(string querySearchString,
|
2022-03-25 21:19:00 +00:00
|
|
|
|
CancellationToken token)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrEmpty(querySearchString))
|
2022-08-16 22:45:36 +00:00
|
|
|
|
return AsyncEnumerable.Empty<SearchResult>();
|
2022-03-25 21:19:00 +00:00
|
|
|
|
|
2022-08-16 22:45:36 +00:00
|
|
|
|
return WindowsIndex.WindowsIndexSearchAsync(
|
2022-09-10 15:45:41 +00:00
|
|
|
|
QueryHelper.ConnectionString,
|
|
|
|
|
|
QueryConstructor.FileContent(querySearchString),
|
2022-08-16 22:45:36 +00:00
|
|
|
|
token);
|
2022-03-25 21:19:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-16 22:45:36 +00:00
|
|
|
|
private IAsyncEnumerable<SearchResult> WindowsIndexFilesAndFoldersSearchAsync(string querySearchString,
|
2022-09-10 15:45:41 +00:00
|
|
|
|
CancellationToken token = default)
|
2022-03-25 21:19:00 +00:00
|
|
|
|
{
|
2022-08-16 22:45:36 +00:00
|
|
|
|
return WindowsIndex.WindowsIndexSearchAsync(
|
2022-09-10 15:45:41 +00:00
|
|
|
|
QueryHelper.ConnectionString,
|
|
|
|
|
|
QueryConstructor.FilesAndFolders(querySearchString),
|
2022-08-16 22:45:36 +00:00
|
|
|
|
token);
|
2022-03-25 21:19:00 +00:00
|
|
|
|
}
|
2022-08-16 22:45:36 +00:00
|
|
|
|
|
2022-09-10 15:45:41 +00:00
|
|
|
|
private IAsyncEnumerable<SearchResult> WindowsIndexTopLevelFolderSearchAsync(string search,
|
|
|
|
|
|
string path,
|
|
|
|
|
|
bool recursive,
|
2022-03-25 21:19:00 +00:00
|
|
|
|
CancellationToken token)
|
|
|
|
|
|
{
|
|
|
|
|
|
var queryConstructor = new QueryConstructor(Settings);
|
|
|
|
|
|
|
2022-08-16 22:45:36 +00:00
|
|
|
|
return WindowsIndex.WindowsIndexSearchAsync(
|
2022-09-10 15:45:41 +00:00
|
|
|
|
QueryConstructor.CreateQueryHelper().ConnectionString,
|
|
|
|
|
|
queryConstructor.Directory(path, search, recursive),
|
2022-08-16 22:45:36 +00:00
|
|
|
|
token);
|
2022-03-25 21:19:00 +00:00
|
|
|
|
}
|
2022-08-16 22:45:36 +00:00
|
|
|
|
public IAsyncEnumerable<SearchResult> SearchAsync(string search, CancellationToken token)
|
2022-03-25 21:19:00 +00:00
|
|
|
|
{
|
2022-09-10 15:45:41 +00:00
|
|
|
|
return WindowsIndexFilesAndFoldersSearchAsync(search, token: token);
|
2022-03-25 21:19:00 +00:00
|
|
|
|
}
|
2022-08-16 22:45:36 +00:00
|
|
|
|
public IAsyncEnumerable<SearchResult> ContentSearchAsync(string plainSearch, string contentSearch, CancellationToken token)
|
2022-03-25 21:19:00 +00:00
|
|
|
|
{
|
2022-08-16 22:45:36 +00:00
|
|
|
|
return WindowsIndexFileContentSearchAsync(contentSearch, token);
|
2022-03-28 20:28:18 +00:00
|
|
|
|
}
|
2022-08-16 22:45:36 +00:00
|
|
|
|
public IAsyncEnumerable<SearchResult> EnumerateAsync(string path, string search, bool recursive, CancellationToken token)
|
2022-03-28 20:28:18 +00:00
|
|
|
|
{
|
2022-09-10 15:45:41 +00:00
|
|
|
|
return WindowsIndexTopLevelFolderSearchAsync(search, path, recursive, token);
|
2022-03-25 21:19:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-09-10 15:45:41 +00:00
|
|
|
|
}
|