2022-09-22 00:18:20 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2022-08-16 22:45:36 +00:00
|
|
|
|
using System.Linq;
|
2022-12-15 11:03:54 +00:00
|
|
|
|
using System.Runtime.InteropServices;
|
2022-03-25 21:19:00 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2022-12-15 11:03:54 +00:00
|
|
|
|
using Flow.Launcher.Plugin.Explorer.Exceptions;
|
2022-09-10 15:45:41 +00:00
|
|
|
|
using Flow.Launcher.Plugin.Explorer.Search.IProvider;
|
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; }
|
2022-09-10 15:45:41 +00:00
|
|
|
|
|
2022-12-15 11:03:54 +00:00
|
|
|
|
private QueryConstructor QueryConstructor { 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
|
|
|
|
|
2022-12-01 12:02:43 +00:00
|
|
|
|
private IAsyncEnumerable<SearchResult> WindowsIndexFileContentSearchAsync(
|
|
|
|
|
|
ReadOnlySpan<char> querySearchString,
|
2022-03-25 21:19:00 +00:00
|
|
|
|
CancellationToken token)
|
|
|
|
|
|
{
|
2022-09-22 00:18:20 +00:00
|
|
|
|
if (querySearchString.IsEmpty)
|
2022-08-16 22:45:36 +00:00
|
|
|
|
return AsyncEnumerable.Empty<SearchResult>();
|
2022-03-25 21:19:00 +00:00
|
|
|
|
|
2022-12-15 11:03:54 +00:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
return WindowsIndex.WindowsIndexSearchAsync(
|
|
|
|
|
|
QueryConstructor.CreateQueryHelper().ConnectionString,
|
|
|
|
|
|
QueryConstructor.FileContent(querySearchString),
|
|
|
|
|
|
token);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (COMException)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Occurs when the Windows Indexing (WSearch) is turned off in services and unable to be used by Explorer plugin
|
|
|
|
|
|
// Thrown by QueryConstructor.CreateQueryHelper()
|
|
|
|
|
|
return HandledEngineNotAvailableExceptionAsync();
|
|
|
|
|
|
}
|
2022-03-25 21:19:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-01 12:02:43 +00:00
|
|
|
|
private IAsyncEnumerable<SearchResult> WindowsIndexFilesAndFoldersSearchAsync(
|
|
|
|
|
|
ReadOnlySpan<char> querySearchString,
|
2022-09-10 15:45:41 +00:00
|
|
|
|
CancellationToken token = default)
|
2022-03-25 21:19:00 +00:00
|
|
|
|
{
|
2022-12-15 11:03:54 +00:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
return WindowsIndex.WindowsIndexSearchAsync(
|
|
|
|
|
|
QueryConstructor.CreateQueryHelper().ConnectionString,
|
|
|
|
|
|
QueryConstructor.FilesAndFolders(querySearchString),
|
|
|
|
|
|
token);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (COMException)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Occurs when the Windows Indexing (WSearch) is turned off in services and unable to be used by Explorer plugin
|
|
|
|
|
|
// Thrown by QueryConstructor.CreateQueryHelper()
|
|
|
|
|
|
return HandledEngineNotAvailableExceptionAsync();
|
|
|
|
|
|
}
|
2022-03-25 21:19:00 +00:00
|
|
|
|
}
|
2022-08-16 22:45:36 +00:00
|
|
|
|
|
2022-12-01 12:02:43 +00:00
|
|
|
|
private IAsyncEnumerable<SearchResult> WindowsIndexTopLevelFolderSearchAsync(
|
|
|
|
|
|
ReadOnlySpan<char> search,
|
2022-09-22 00:18:20 +00:00
|
|
|
|
ReadOnlySpan<char> path,
|
2022-09-10 15:45:41 +00:00
|
|
|
|
bool recursive,
|
2022-03-25 21:19:00 +00:00
|
|
|
|
CancellationToken token)
|
|
|
|
|
|
{
|
2022-12-15 11:03:54 +00:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
return WindowsIndex.WindowsIndexSearchAsync(
|
|
|
|
|
|
QueryConstructor.CreateQueryHelper().ConnectionString,
|
|
|
|
|
|
QueryConstructor.Directory(path, search, recursive),
|
|
|
|
|
|
token);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (COMException)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Occurs when the Windows Indexing (WSearch) is turned off in services and unable to be used by Explorer plugin
|
|
|
|
|
|
// Thrown by QueryConstructor.CreateQueryHelper()
|
|
|
|
|
|
return HandledEngineNotAvailableExceptionAsync();
|
|
|
|
|
|
}
|
2022-03-25 21:19:00 +00:00
|
|
|
|
}
|
2022-09-24 19:23:59 +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-09-24 19:23:59 +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-09-24 19:23:59 +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-12-15 11:03:54 +00:00
|
|
|
|
|
|
|
|
|
|
private IAsyncEnumerable<SearchResult> HandledEngineNotAvailableExceptionAsync()
|
|
|
|
|
|
{
|
2023-01-19 06:47:42 +00:00
|
|
|
|
if (!Settings.WarnWindowsSearchServiceOff)
|
2022-12-15 11:03:54 +00:00
|
|
|
|
return AsyncEnumerable.Empty<SearchResult>();
|
|
|
|
|
|
|
2023-01-19 06:47:42 +00:00
|
|
|
|
var api = Main.Context.API;
|
2022-12-15 11:03:54 +00:00
|
|
|
|
|
|
|
|
|
|
throw new EngineNotAvailableException(
|
|
|
|
|
|
"Windows Index",
|
|
|
|
|
|
api.GetTranslation("plugin_explorer_windowsSearchServiceFix"),
|
|
|
|
|
|
api.GetTranslation("plugin_explorer_windowsSearchServiceNotRunning"),
|
2023-01-19 06:48:35 +00:00
|
|
|
|
Constants.WindowsIndexErrorImagePath,
|
2022-12-15 11:03:54 +00:00
|
|
|
|
c =>
|
|
|
|
|
|
{
|
2023-01-19 06:47:42 +00:00
|
|
|
|
Settings.WarnWindowsSearchServiceOff = false;
|
2022-12-15 11:03:54 +00:00
|
|
|
|
|
|
|
|
|
|
// Clears the warning message so user is not mistaken that it has not worked
|
|
|
|
|
|
api.ChangeQuery(string.Empty);
|
|
|
|
|
|
|
|
|
|
|
|
return ValueTask.FromResult(false);
|
2023-01-19 06:48:35 +00:00
|
|
|
|
});
|
2022-12-15 11:03:54 +00:00
|
|
|
|
}
|
2022-03-25 21:19:00 +00:00
|
|
|
|
}
|
2022-09-10 15:45:41 +00:00
|
|
|
|
}
|