2020-08-25 11:40:17 +00:00
|
|
|
|
using Flow.Launcher.Plugin.Explorer.Search.DirectoryInfo;
|
2020-06-06 12:13:22 +00:00
|
|
|
|
using Flow.Launcher.Plugin.Explorer.Search.FolderLinks;
|
2020-05-24 09:09:44 +00:00
|
|
|
|
using Flow.Launcher.Plugin.Explorer.Search.WindowsIndex;
|
2020-05-24 10:14:36 +00:00
|
|
|
|
using Flow.Launcher.Plugin.SharedCommands;
|
2020-05-19 12:36:56 +00:00
|
|
|
|
using System;
|
2020-05-19 10:10:46 +00:00
|
|
|
|
using System.Collections.Generic;
|
2020-06-02 10:12:14 +00:00
|
|
|
|
using System.Linq;
|
2020-05-19 10:10:46 +00:00
|
|
|
|
|
|
|
|
|
|
namespace Flow.Launcher.Plugin.Explorer.Search
|
|
|
|
|
|
{
|
|
|
|
|
|
public class SearchManager
|
|
|
|
|
|
{
|
2020-06-08 04:20:22 +00:00
|
|
|
|
private readonly PluginInitContext context;
|
2020-05-19 12:36:56 +00:00
|
|
|
|
|
2020-06-08 04:20:22 +00:00
|
|
|
|
private readonly IndexSearch indexSearch;
|
2020-05-23 06:40:31 +00:00
|
|
|
|
|
2020-06-08 04:20:22 +00:00
|
|
|
|
private readonly QuickFolderAccess quickFolderAccess = new QuickFolderAccess();
|
|
|
|
|
|
|
|
|
|
|
|
private readonly ResultManager resultManager;
|
|
|
|
|
|
|
|
|
|
|
|
private readonly Settings settings;
|
2020-05-24 22:14:21 +00:00
|
|
|
|
|
2020-05-19 12:36:56 +00:00
|
|
|
|
public SearchManager(Settings settings, PluginInitContext context)
|
|
|
|
|
|
{
|
2020-06-08 04:20:22 +00:00
|
|
|
|
this.context = context;
|
|
|
|
|
|
indexSearch = new IndexSearch(context);
|
|
|
|
|
|
resultManager = new ResultManager(context);
|
|
|
|
|
|
this.settings = settings;
|
2020-05-23 06:40:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-05-24 10:14:36 +00:00
|
|
|
|
internal List<Result> Search(Query query)
|
2020-05-23 06:40:31 +00:00
|
|
|
|
{
|
2020-06-09 10:12:49 +00:00
|
|
|
|
var results = new List<Result>();
|
|
|
|
|
|
|
2020-05-24 09:09:44 +00:00
|
|
|
|
var querySearch = query.Search;
|
|
|
|
|
|
|
2020-08-25 11:38:39 +00:00
|
|
|
|
if (IsFileContentSearch(query.ActionKeyword))
|
|
|
|
|
|
return WindowsIndexFileContentSearch(query, querySearch);
|
2020-05-24 22:14:21 +00:00
|
|
|
|
|
2020-08-25 11:40:17 +00:00
|
|
|
|
// This allows the user to type the assigned action keyword and only see the list of quick folder links
|
|
|
|
|
|
if (settings.QuickFolderAccessLinks.Count > 0
|
|
|
|
|
|
&& query.ActionKeyword == settings.SearchActionKeyword
|
|
|
|
|
|
&& string.IsNullOrEmpty(query.Search))
|
|
|
|
|
|
return quickFolderAccess.FolderListAll(query, settings.QuickFolderAccessLinks, context);
|
2020-07-12 12:43:38 +00:00
|
|
|
|
|
2020-09-21 10:55:39 +00:00
|
|
|
|
var quickFolderLinks = quickFolderAccess.FolderListMatched(query, settings.QuickFolderAccessLinks, context);
|
|
|
|
|
|
|
|
|
|
|
|
if (quickFolderLinks.Count > 0)
|
|
|
|
|
|
results.AddRange(quickFolderLinks);
|
|
|
|
|
|
|
2020-06-20 04:26:13 +00:00
|
|
|
|
var isEnvironmentVariable = EnvironmentVariables.IsEnvironmentVariableSearch(querySearch);
|
2020-05-31 21:53:34 +00:00
|
|
|
|
|
2020-06-20 04:26:13 +00:00
|
|
|
|
if (isEnvironmentVariable)
|
|
|
|
|
|
return EnvironmentVariables.GetEnvironmentStringPathSuggestions(querySearch, query, context);
|
2020-05-24 09:09:44 +00:00
|
|
|
|
|
2020-05-26 11:49:59 +00:00
|
|
|
|
// Query is a location path with a full environment variable, eg. %appdata%\somefolder\
|
2020-06-20 04:26:13 +00:00
|
|
|
|
var isEnvironmentVariablePath = querySearch.Substring(1).Contains("%\\");
|
|
|
|
|
|
|
2020-06-21 19:02:42 +00:00
|
|
|
|
if (!FilesFolders.IsLocationPathString(querySearch) && !isEnvironmentVariablePath)
|
2020-08-25 11:36:26 +00:00
|
|
|
|
{
|
|
|
|
|
|
results.AddRange(WindowsIndexFilesAndFoldersSearch(query, querySearch));
|
|
|
|
|
|
|
|
|
|
|
|
return results;
|
|
|
|
|
|
}
|
2020-06-20 04:26:13 +00:00
|
|
|
|
|
|
|
|
|
|
var locationPath = querySearch;
|
|
|
|
|
|
|
|
|
|
|
|
if (isEnvironmentVariablePath)
|
2020-05-31 21:53:34 +00:00
|
|
|
|
locationPath = EnvironmentVariables.TranslateEnvironmentVariablePath(locationPath);
|
2020-05-25 23:03:27 +00:00
|
|
|
|
|
2020-06-02 10:21:28 +00:00
|
|
|
|
if (!FilesFolders.LocationExists(FilesFolders.ReturnPreviousDirectoryIfIncompleteString(locationPath)))
|
2020-05-31 21:53:34 +00:00
|
|
|
|
return results;
|
2020-05-25 23:03:27 +00:00
|
|
|
|
|
2020-06-02 10:12:14 +00:00
|
|
|
|
var useIndexSearch = UseWindowsIndexForDirectorySearch(locationPath);
|
2020-05-25 23:03:27 +00:00
|
|
|
|
|
2020-06-08 04:20:22 +00:00
|
|
|
|
results.Add(resultManager.CreateOpenCurrentFolderResult(locationPath, useIndexSearch));
|
2020-05-23 06:40:31 +00:00
|
|
|
|
|
2020-06-02 10:04:22 +00:00
|
|
|
|
results.AddRange(TopLevelDirectorySearchBehaviour(WindowsIndexTopLevelFolderSearch,
|
|
|
|
|
|
DirectoryInfoClassSearch,
|
2020-06-02 10:12:14 +00:00
|
|
|
|
useIndexSearch,
|
|
|
|
|
|
query,
|
|
|
|
|
|
locationPath));
|
2020-05-26 11:55:07 +00:00
|
|
|
|
|
|
|
|
|
|
return results;
|
2020-05-23 06:40:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-12 12:43:38 +00:00
|
|
|
|
private List<Result> WindowsIndexFileContentSearch(Query query, string querySearchString)
|
|
|
|
|
|
{
|
|
|
|
|
|
var queryConstructor = new QueryConstructor(settings);
|
|
|
|
|
|
|
2020-07-14 19:52:01 +00:00
|
|
|
|
if (string.IsNullOrEmpty(querySearchString))
|
2020-07-12 12:43:38 +00:00
|
|
|
|
return new List<Result>();
|
|
|
|
|
|
|
2020-07-14 19:52:01 +00:00
|
|
|
|
return indexSearch.WindowsIndexSearch(querySearchString,
|
2020-07-12 12:43:38 +00:00
|
|
|
|
queryConstructor.CreateQueryHelper().ConnectionString,
|
|
|
|
|
|
queryConstructor.QueryForFileContentSearch,
|
|
|
|
|
|
query);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-14 19:52:01 +00:00
|
|
|
|
public bool IsFileContentSearch(string actionKeyword)
|
2020-07-12 12:43:38 +00:00
|
|
|
|
{
|
2020-07-14 19:52:01 +00:00
|
|
|
|
return actionKeyword == settings.FileContentSearchActionKeyword;
|
2020-07-12 12:43:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-05-24 10:14:36 +00:00
|
|
|
|
private List<Result> DirectoryInfoClassSearch(Query query, string querySearch)
|
2020-05-23 06:40:31 +00:00
|
|
|
|
{
|
2020-06-08 04:20:22 +00:00
|
|
|
|
var directoryInfoSearch = new DirectoryInfoSearch(context);
|
2020-05-19 12:36:56 +00:00
|
|
|
|
|
2020-05-24 10:14:36 +00:00
|
|
|
|
return directoryInfoSearch.TopLevelDirectorySearch(query, querySearch);
|
2020-05-19 12:36:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-02 10:04:22 +00:00
|
|
|
|
public List<Result> TopLevelDirectorySearchBehaviour(
|
2020-05-25 08:58:28 +00:00
|
|
|
|
Func<Query, string, List<Result>> windowsIndexSearch,
|
2020-05-24 10:14:36 +00:00
|
|
|
|
Func<Query, string, List<Result>> directoryInfoClassSearch,
|
2020-06-02 10:12:14 +00:00
|
|
|
|
bool useIndexSearch,
|
2020-05-24 10:14:36 +00:00
|
|
|
|
Query query,
|
|
|
|
|
|
string querySearchString)
|
2020-05-19 10:10:46 +00:00
|
|
|
|
{
|
2020-06-02 10:12:14 +00:00
|
|
|
|
if (!useIndexSearch)
|
2020-05-24 10:14:36 +00:00
|
|
|
|
return directoryInfoClassSearch(query, querySearchString);
|
2020-05-19 10:10:46 +00:00
|
|
|
|
|
2020-05-31 22:03:04 +00:00
|
|
|
|
return windowsIndexSearch(query, querySearchString);
|
2020-05-19 10:10:46 +00:00
|
|
|
|
}
|
2020-05-19 12:38:42 +00:00
|
|
|
|
|
2020-05-25 08:58:28 +00:00
|
|
|
|
private List<Result> WindowsIndexFilesAndFoldersSearch(Query query, string querySearchString)
|
2020-05-19 12:38:42 +00:00
|
|
|
|
{
|
2020-06-08 04:20:22 +00:00
|
|
|
|
var queryConstructor = new QueryConstructor(settings);
|
2020-05-19 12:38:42 +00:00
|
|
|
|
|
2020-06-08 04:20:22 +00:00
|
|
|
|
return indexSearch.WindowsIndexSearch(querySearchString,
|
2020-06-02 10:12:14 +00:00
|
|
|
|
queryConstructor.CreateQueryHelper().ConnectionString,
|
|
|
|
|
|
queryConstructor.QueryForAllFilesAndFolders,
|
|
|
|
|
|
query);
|
2020-05-19 12:38:42 +00:00
|
|
|
|
}
|
2020-05-24 10:14:36 +00:00
|
|
|
|
|
2020-05-25 08:58:28 +00:00
|
|
|
|
private List<Result> WindowsIndexTopLevelFolderSearch(Query query, string path)
|
2020-05-23 06:40:31 +00:00
|
|
|
|
{
|
2020-06-08 04:20:22 +00:00
|
|
|
|
var queryConstructor = new QueryConstructor(settings);
|
2020-05-23 06:40:31 +00:00
|
|
|
|
|
2020-06-08 04:20:22 +00:00
|
|
|
|
return indexSearch.WindowsIndexSearch(path,
|
2020-05-31 21:53:34 +00:00
|
|
|
|
queryConstructor.CreateQueryHelper().ConnectionString,
|
|
|
|
|
|
queryConstructor.QueryForTopLevelDirectorySearch,
|
|
|
|
|
|
query);
|
2020-05-25 23:03:27 +00:00
|
|
|
|
}
|
2020-06-02 10:12:14 +00:00
|
|
|
|
|
|
|
|
|
|
private bool UseWindowsIndexForDirectorySearch(string locationPath)
|
|
|
|
|
|
{
|
2020-08-26 04:10:39 +00:00
|
|
|
|
var pathToDirectory = FilesFolders.ReturnPreviousDirectoryIfIncompleteString(locationPath);
|
|
|
|
|
|
|
2020-06-08 04:20:22 +00:00
|
|
|
|
if (!settings.UseWindowsIndexForDirectorySearch)
|
2020-06-02 10:12:14 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
|
2020-06-08 04:20:22 +00:00
|
|
|
|
if (settings.IndexSearchExcludedSubdirectoryPaths
|
2020-08-26 04:10:39 +00:00
|
|
|
|
.Any(x => FilesFolders.ReturnPreviousDirectoryIfIncompleteString(pathToDirectory)
|
2020-06-08 04:47:57 +00:00
|
|
|
|
.StartsWith(x.Path, StringComparison.OrdinalIgnoreCase)))
|
2020-06-02 10:12:14 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
|
2020-08-26 04:10:39 +00:00
|
|
|
|
return indexSearch.PathIsIndexed(pathToDirectory);
|
2020-06-02 10:12:14 +00:00
|
|
|
|
}
|
2020-05-19 10:10:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|