Fixing stylecop violations (#5629)

This commit is contained in:
Clint Rutkas 2020-08-06 10:10:44 -07:00 committed by GitHub
parent 0f90af3c7d
commit ed36447bdf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 653 additions and 607 deletions

View File

@ -0,0 +1,17 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace Microsoft.Plugin.Indexer
{
public class ContextMenu
{
public string Name { get; set; }
public string Command { get; set; }
public string Argument { get; set; }
public string ImagePath { get; set; }
}
}

View File

@ -1,15 +1,19 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows;
using Wox.Infrastructure.Logger;
using Wox.Plugin;
using Microsoft.Plugin.Indexer.SearchHelper;
using System.Windows.Input;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Microsoft.Plugin.Indexer.SearchHelper;
using Wox.Infrastructure;
using Wox.Infrastructure.Logger;
using Wox.Plugin;
namespace Microsoft.Plugin.Indexer
{
@ -20,7 +24,7 @@ namespace Microsoft.Plugin.Indexer
public enum ResultType
{
Folder,
File
File,
}
// Extensions for adding run as admin context menu item for applications
@ -73,7 +77,7 @@ namespace Microsoft.Plugin.Indexer
_context.API.ShowMsg(message);
return false;
}
}
},
});
contextMenus.Add(new ContextMenuResult
{
@ -104,7 +108,7 @@ namespace Microsoft.Plugin.Indexer
Log.Exception($"|Microsoft.Plugin.Indexer.ContextMenuLoader.LoadContextMenus| Failed to open {record.Path} in console, {e.Message}", e);
return false;
}
}
},
});
}
@ -122,7 +126,7 @@ namespace Microsoft.Plugin.Indexer
Glyph = "\xE7EF",
FontFamily = "Segoe MDL2 Assets",
AcceleratorKey = Key.Enter,
AcceleratorModifiers = (ModifierKeys.Control | ModifierKeys.Shift),
AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift,
Action = _ =>
{
try
@ -135,8 +139,7 @@ namespace Microsoft.Plugin.Indexer
Log.Exception($"|Microsoft.Plugin.Indexer.ContextMenu| Failed to run {record.Path} as admin, {e.Message}", e);
return false;
}
}
},
};
}
@ -151,6 +154,7 @@ namespace Microsoft.Plugin.Indexer
return true;
}
}
return false;
}
@ -164,7 +168,7 @@ namespace Microsoft.Plugin.Indexer
Glyph = "\xE838",
FontFamily = "Segoe MDL2 Assets",
AcceleratorKey = Key.E,
AcceleratorModifiers = (ModifierKeys.Control | ModifierKeys.Shift),
AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift,
Action = _ =>
{
try
@ -189,5 +193,4 @@ namespace Microsoft.Plugin.Indexer
Log.Exception($"|Microsoft.Plugin.Folder.ContextMenu|{message}", e);
}
}
}

View File

@ -1,9 +1,15 @@
namespace Microsoft.Plugin.Indexer.DriveDetection
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace Microsoft.Plugin.Indexer.DriveDetection
{
public class IndexerDriveDetection
{
private bool IsEnhancedModeEnabled { get; set; } = false;
private IRegistryWrapper _registryHelper;
private readonly IRegistryWrapper _registryHelper;
public bool IsDriveDetectionWarningCheckBoxSelected { get; set; } = false;
public IndexerDriveDetection(IRegistryWrapper registryHelper)
@ -18,7 +24,7 @@
return !(IsDriveDetectionWarningCheckBoxSelected || IsEnhancedModeEnabled);
}
// To look up the registry entry for
// To look up the registry entry for enhanced search
private void GetEnhancedModeStatus()
{
string registryLocation = @"Software\Microsoft\Windows Search\Gather\Windows\SystemIndex";

View File

@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Input;
using Microsoft.Plugin.Indexer;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using Microsoft.Win32;
namespace Microsoft.Plugin.Indexer.DriveDetection
@ -16,13 +16,14 @@ namespace Microsoft.Plugin.Indexer.DriveDetection
{
if (regKey != null)
{
Object value = regKey.GetValue(valueName);
object value = regKey.GetValue(valueName);
if (value != null)
{
return (int)value;
}
}
}
return 0;
}
}

View File

@ -1,23 +1,17 @@
using System;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Microsoft.Plugin.Indexer
{
public class IndexerSettings
{
public List<ContextMenu> ContextMenus { get; } = new List<ContextMenu>();
public int MaxSearchCount { get; set; } = 100;
public bool UseLocationAsWorkingDir { get; set; } = false;
}
public class ContextMenu
{
public string Name { get; set; }
public string Command { get; set; }
public string Argument { get; set; }
public string ImagePath { get; set; }
}
}

View File

@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace Microsoft.Plugin.Indexer
{

View File

@ -1,5 +1,9 @@
using Microsoft.Plugin.Indexer.SearchHelper;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using Microsoft.Plugin.Indexer.SearchHelper;
namespace Microsoft.Plugin.Indexer
{

View File

@ -1,27 +1,27 @@
using System;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
using Wox.Plugin;
using System.IO;
using System.ComponentModel;
using Wox.Infrastructure.Storage;
using Microsoft.Plugin.Indexer.SearchHelper;
using Microsoft.Search.Interop;
using Microsoft.PowerToys.Settings.UI.Lib;
using System.Windows.Controls;
using Wox.Infrastructure.Logger;
using System.Text.RegularExpressions;
using Microsoft.Plugin.Indexer.DriveDetection;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Controls;
using Microsoft.Plugin.Indexer.DriveDetection;
using Microsoft.Plugin.Indexer.SearchHelper;
using Microsoft.PowerToys.Settings.UI.Lib;
using Wox.Infrastructure.Logger;
using Wox.Infrastructure.Storage;
using Wox.Plugin;
namespace Microsoft.Plugin.Indexer
{
internal class Main : ISettingProvider, IPlugin, ISavable, IPluginI18n, IContextMenu, IDisposable
{
// This variable contains metadata about the Plugin
private PluginInitContext _context;
@ -39,8 +39,10 @@ namespace Microsoft.Plugin.Indexer
private readonly IndexerDriveDetection _driveDetection = new IndexerDriveDetection(new RegistryWrapper());
// Reserved keywords in oleDB
private string ReservedStringPattern = @"^[\/\\\$\%]+$";
private readonly string reservedStringPattern = @"^[\/\\\$\%]+$";
private string WarningIconPath { get; set; }
private IContextMenu _contextMenuLoader;
private bool disposedValue;
@ -50,7 +52,6 @@ namespace Microsoft.Plugin.Indexer
_storage.Save();
}
// This function uses the Windows indexer and returns the list of results obtained
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "We want to keep the process alive but will log the exception")]
public List<Result> Query(Query query)
@ -65,13 +66,13 @@ namespace Microsoft.Plugin.Indexer
_settings.MaxSearchCount = 50;
}
var regexMatch = Regex.Match(searchQuery, ReservedStringPattern);
var regexMatch = Regex.Match(searchQuery, reservedStringPattern);
if (!regexMatch.Success)
{
try
{
if(_driveDetection.DisplayWarning())
if (_driveDetection.DisplayWarning())
{
results.Add(new Result
{
@ -88,8 +89,9 @@ namespace Microsoft.Plugin.Indexer
{
Log.Exception("Microsoft.Plugin.Indexer", $"Unable to launch Windows Search Settings: {ex.Message}", ex, "Query");
}
return true;
}
},
});
}
@ -101,7 +103,9 @@ namespace Microsoft.Plugin.Indexer
var toolTipText = string.Format(CultureInfo.CurrentCulture, "{0} : {1}", _context.API.GetTranslation("Microsoft_plugin_indexer_path"), path);
string workingDir = null;
if (_settings.UseLocationAsWorkingDir)
{
workingDir = Path.GetDirectoryName(path);
}
Result r = new Result();
r.Title = searchResult.Title;
@ -117,7 +121,7 @@ namespace Microsoft.Plugin.Indexer
{
FileName = path,
UseShellExecute = true,
WorkingDirectory = workingDir
WorkingDirectory = workingDir,
});
hide = true;
}
@ -128,11 +132,12 @@ namespace Microsoft.Plugin.Indexer
_context.API.ShowMsg(name, msg, string.Empty);
hide = false;
}
return hide;
};
r.ContextData = searchResult;
//If the result is a directory, then it's display should show a directory.
// If the result is a directory, then it's display should show a directory.
if (Directory.Exists(path))
{
r.QueryTextDisplay = path;
@ -143,8 +148,8 @@ namespace Microsoft.Plugin.Indexer
}
catch (InvalidOperationException)
{
//The connection has closed, internal error of ExecuteReader()
//Not showing this exception to the users
// The connection has closed, internal error of ExecuteReader()
// Not showing this exception to the users
}
catch (Exception ex)
{
@ -180,7 +185,7 @@ namespace Microsoft.Plugin.Indexer
}
}
private void OnThemeChanged(Theme _, Theme newTheme)
private void OnThemeChanged(Theme currentTheme, Theme newTheme)
{
UpdateIconPath(newTheme);
}
@ -203,11 +208,13 @@ namespace Microsoft.Plugin.Indexer
{
return _contextMenuLoader.LoadContextMenus(selectedResult);
}
public void UpdateSettings(PowerLauncherSettings settings)
{
_settings.MaxSearchCount = settings.Properties.MaximumNumberOfResults;
_driveDetection.IsDriveDetectionWarningCheckBoxSelected = settings.Properties.DisableDriveDetectionWarning;
}
public Control CreateSettingPanel()
{
throw new NotImplementedException();
@ -219,7 +226,7 @@ namespace Microsoft.Plugin.Indexer
var ps = new ProcessStartInfo("ms-settings:cortana-windowssearch")
{
UseShellExecute = true,
Verb = "open"
Verb = "open",
};
return ps;

View File

@ -115,5 +115,19 @@
<_Parameter1>Wox.Test</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\..\..\codeAnalysis\GlobalSuppressions.cs">
<Link>GlobalSuppressions.cs</Link>
</Compile>
<AdditionalFiles Include="..\..\..\..\codeAnalysis\StyleCop.json">
<Link>StyleCop.json</Link>
</AdditionalFiles>
</ItemGroup>
<ItemGroup>
<PackageReference Include="StyleCop.Analyzers">
<Version>1.1.118</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
</Project>

View File

@ -1,8 +1,8 @@
using System;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Data.Common;
using System.Data.OleDb;
using System.Text;
namespace Microsoft.Plugin.Indexer.SearchHelper
{

View File

@ -1,4 +1,8 @@
using System;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Data.OleDb;
@ -8,12 +12,13 @@ namespace Microsoft.Plugin.Indexer.SearchHelper
{
private OleDbCommand command;
private OleDbConnection conn;
private OleDbDataReader WDSResults;
private OleDbDataReader wDSResults;
private bool disposedValue;
[System.Diagnostics.CodeAnalysis.SuppressMessage("Security", "CA2100:Review SQL queries for security vulnerabilities",
Justification = "sqlQuery does not come from user input but is generated via the ISearchQueryHelper::GenerateSqlFromUserQuery " +
" see: https://docs.microsoft.com/en-us/windows/win32/search/-search-3x-wds-qryidx-searchqueryhelper#using-the-generatesqlfromuserquery-method")]
[System.Diagnostics.CodeAnalysis.SuppressMessage(
"Security",
"CA2100:Review SQL queries for security vulnerabilities",
Justification = "sqlQuery does not come from user input but is generated via the ISearchQueryHelper::GenerateSqlFromUserQuery see: https://docs.microsoft.com/en-us/windows/win32/search/-search-3x-wds-qryidx-searchqueryhelper#using-the-generatesqlfromuserquery-method")]
public List<OleDBResult> Query(string connectionString, string sqlQuery)
{
List<OleDBResult> result = new List<OleDBResult>();
@ -26,17 +31,18 @@ namespace Microsoft.Plugin.Indexer.SearchHelper
// now create an OleDB command object with the query we built above and the connection we just opened.
using (command = new OleDbCommand(sqlQuery, conn))
{
using (WDSResults = command.ExecuteReader())
using (wDSResults = command.ExecuteReader())
{
if (WDSResults.HasRows)
if (wDSResults.HasRows)
{
while (WDSResults.Read())
while (wDSResults.Read())
{
List<Object> fieldData = new List<object>();
for (int i = 0; i < WDSResults.FieldCount; i++)
List<object> fieldData = new List<object>();
for (int i = 0; i < wDSResults.FieldCount; i++)
{
fieldData.Add(WDSResults.GetValue(i));
fieldData.Add(wDSResults.GetValue(i));
}
result.Add(new OleDBResult(fieldData));
}
}
@ -47,7 +53,7 @@ namespace Microsoft.Plugin.Indexer.SearchHelper
return result;
}
/// Checks if all the variables related to database connection have been properly disposed
// Checks if all the variables related to database connection have been properly disposed
public bool HaveAllDisposableItemsBeenDisposed()
{
bool commandDisposed = false;
@ -65,7 +71,7 @@ namespace Microsoft.Plugin.Indexer.SearchHelper
try
{
WDSResults.Read();
wDSResults.Read();
}
catch (InvalidOperationException)
{
@ -88,7 +94,7 @@ namespace Microsoft.Plugin.Indexer.SearchHelper
{
command?.Dispose();
conn?.Dispose();
WDSResults?.Dispose();
wDSResults?.Dispose();
}
// TODO: free unmanaged resources (unmanaged objects) and override finalizer
@ -97,13 +103,6 @@ namespace Microsoft.Plugin.Indexer.SearchHelper
}
}
// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
// ~OleDBSearch()
// {
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
// Dispose(disposing: false);
// }
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method

View File

@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace Microsoft.Plugin.Indexer.SearchHelper
{
@ -13,6 +11,5 @@ namespace Microsoft.Plugin.Indexer.SearchHelper
// Contains the Title of the file or folder
public string Title { get; set; }
}
}

View File

@ -1,4 +1,8 @@
using System;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using Microsoft.Search.Interop;
@ -8,30 +12,30 @@ namespace Microsoft.Plugin.Indexer.SearchHelper
{
public bool DisplayHiddenFiles { get; set; }
private readonly ISearch WindowsIndexerSearch;
private readonly ISearch windowsIndexerSearch;
private readonly object _lock = new object();
private readonly UInt32 FILE_ATTRIBUTE_HIDDEN = 0x2;
private const uint _fileAttributeHidden = 0x2;
public WindowsSearchAPI(ISearch windowsIndexerSearch, bool displayHiddenFiles = false)
{
this.WindowsIndexerSearch = windowsIndexerSearch;
this.DisplayHiddenFiles = displayHiddenFiles;
this.windowsIndexerSearch = windowsIndexerSearch;
DisplayHiddenFiles = displayHiddenFiles;
}
public List<SearchResult> ExecuteQuery(ISearchQueryHelper queryHelper, string keyword)
{
if(queryHelper == null)
if (queryHelper == null)
{
throw new ArgumentNullException(paramName: nameof(queryHelper));
}
List<SearchResult> _Result = new List<SearchResult>();
List<SearchResult> results = new List<SearchResult>();
// Generate SQL from our parameters, converting the userQuery from AQS->WHERE clause
string sqlQuery = queryHelper.GenerateSQLFromUserQuery(keyword);
// execute the command, which returns the results as an OleDBResults.
List<OleDBResult> oleDBResults = WindowsIndexerSearch.Query(queryHelper.ConnectionString, sqlQuery);
List<OleDBResult> oleDBResults = windowsIndexerSearch.Query(queryHelper.ConnectionString, sqlQuery);
// Loop over all records from the database
foreach (OleDBResult oleDBResult in oleDBResults)
@ -41,8 +45,8 @@ namespace Microsoft.Plugin.Indexer.SearchHelper
continue;
}
UInt32 fileAttributes = (UInt32)((Int64)oleDBResult.FieldData[2]);
bool isFileHidden = (fileAttributes & FILE_ATTRIBUTE_HIDDEN) == FILE_ATTRIBUTE_HIDDEN;
uint fileAttributes = (uint)((long)oleDBResult.FieldData[2]);
bool isFileHidden = (fileAttributes & _fileAttributeHidden) == _fileAttributeHidden;
if (DisplayHiddenFiles || !isFileHidden)
{
@ -50,19 +54,19 @@ namespace Microsoft.Plugin.Indexer.SearchHelper
var result = new SearchResult
{
Path = uri_path.LocalPath,
Title = (string)oleDBResult.FieldData[1]
Title = (string)oleDBResult.FieldData[1],
};
_Result.Add(result);
results.Add(result);
}
}
return _Result;
return results;
}
public static void ModifyQueryHelper(ref ISearchQueryHelper queryHelper, string pattern)
{
if(pattern == null)
if (pattern == null)
{
throw new ArgumentNullException(paramName: nameof(pattern));
}