Skip to content

Commit

Permalink
[fxcop] Wox.Infrastructure (#7590)
Browse files Browse the repository at this point in the history
* CA1052: Static holder types should be Static or NotInheritable

* CA1041: Provide ObsoleteAttribute message

* CA1062: Validate arguments of public methods

* CA1304: Specify CultureInfo / CA1305: Specify IFormatProvider / CA1307: Specify StringComparison for clarity

* CA1802: Use Literals Where Appropriate

* CA1820: Test for empty strings using string length

* CA1707: Identifiers should not contain underscores

* CA1805: Do not initialize unnecessarily.

* CA1822: Mark members as static

* CA2227: Collection properties should be read only

* CA1054: URI parameters should not be strings

* CA1031: Do not catch general exception types

* CA1060: Move P/Invokes to NativeMethods class

* CA1308: Normalize strings to uppercase

* CA2000: Dispose objects before losing scope / CA2234: Pass System.Uri objects instead of strings

* CA2234: Pass System.Uri objects instead of strings

* CA1044: Properties should not be write only

* CA1716: Identifiers should not match keywords

* CA2007: Do not directly await a Task

* CA2007: Do not directly await a Task (Suppressed)

* CA5350: Do Not Use Weak Cryptographic Algorithms (Suppressed)

* CA1724: Type names should not match namespaces (renamed Settings.cs to PowerToysRunSettings.cs)

* CA1033: Interface methods should be callable by child types (Added sealed modifier to class)

* CA1724: Type names should not match namespaces (Renamed Plugin.cs to RunPlugin.cs)

* CA1724: Type names should not match namespaces (Renamed Http.cs to HttpClient.cs)

* CA5364: Do not use deprecated security protocols (Remove unused code)

* Enabled FxCopAnalyzer for Wox.Infrastructure

* fixed comment

* Addressed comments

- Changed Ordinal to InvariantCulture
- Added comments
- Removed unused obsolete code
- Removed unused method (CA2007: Do not directly await a Task)

* Addressed comments - fixed justification for CA1031 suppression

* Addressed comments - Fixed justification for CA1031 suppression in Wox.Core/Wox.Plugin
  • Loading branch information
avneet-kr committed Oct 30, 2020
1 parent 5015642 commit ec8ead8
Show file tree
Hide file tree
Showing 41 changed files with 293 additions and 227 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void IndexPrograms()
var support = Environment.OSVersion.Version.Major >= windows10.Major;

var applications = support ? Programs.UWP.All() : Array.Empty<UWPApplication>();
Set(applications);
SetList(applications);
}

public void Save()
Expand All @@ -91,7 +91,7 @@ public void Save()
public void Load()
{
var items = _storage.TryLoad(Array.Empty<UWPApplication>());
Set(items);
SetList(items);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ private void OnAppChanged(object sender, FileSystemEventArgs e)
public void IndexPrograms()
{
var applications = Programs.Win32Program.All(_settings);
Set(applications);
SetList(applications);
}

public void Save()
Expand All @@ -236,7 +236,7 @@ public void Save()
public void Load()
{
var items = _storage.TryLoad(Array.Empty<Win32Program>());
Set(items);
SetList(items);
}
}
}
4 changes: 2 additions & 2 deletions src/modules/launcher/PowerLauncher/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public partial class App : IDisposable, ISingleInstanceApp

private const string Unique = "PowerLauncher_Unique_Application_Mutex";
private static bool _disposed;
private Settings _settings;
private PowerToysRunSettings _settings;
private MainViewModel _mainVM;
private MainWindow _mainWindow;
private ThemeManager _themeManager;
Expand Down Expand Up @@ -107,7 +107,7 @@ private void OnStartup(object sender, StartupEventArgs e)
Current.MainWindow.Title = Constant.ExeFileName;
// main windows needs initialized before theme change because of blur settings
Http.Proxy = _settings.Proxy;
HttpClient.Proxy = _settings.Proxy;
RegisterExitEvents();
Expand Down
4 changes: 2 additions & 2 deletions src/modules/launcher/PowerLauncher/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ namespace PowerLauncher
{
public partial class MainWindow : IDisposable
{
private readonly Settings _settings;
private readonly PowerToysRunSettings _settings;
private readonly MainViewModel _viewModel;
private bool _isTextSetProgrammatically;
private bool _deletePressed;
private Timer _firstDeleteTimer = new Timer();
private bool _coldStateHotkeyPressed;

public MainWindow(Settings settings, MainViewModel mainVM)
public MainWindow(PowerToysRunSettings settings, MainViewModel mainVM)
: this()
{
DataContext = mainVM;
Expand Down
5 changes: 3 additions & 2 deletions src/modules/launcher/PowerLauncher/SettingsWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ public class SettingsWatcher : BaseModel
private const int MaxRetries = 10;
private static readonly object _watcherSyncObject = new object();
private readonly FileSystemWatcher _watcher;
private readonly Settings _settings;
private readonly PowerToysRunSettings _settings;

private readonly ThemeManager _themeManager;

public SettingsWatcher(Settings settings, ThemeManager themeManager)
public SettingsWatcher(PowerToysRunSettings settings, ThemeManager themeManager)
{
_settingsUtils = new SettingsUtils(new SystemIOProvider());
_settings = settings;
Expand Down
6 changes: 3 additions & 3 deletions src/modules/launcher/PowerLauncher/ViewModel/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class MainViewModel : BaseModel, ISavable, IDisposable
private readonly WoxJsonStorage<QueryHistory> _historyItemsStorage;
private readonly WoxJsonStorage<UserSelectedRecord> _userSelectedRecordStorage;
private readonly WoxJsonStorage<TopMostRecord> _topMostRecordStorage;
private readonly Settings _settings;
private readonly PowerToysRunSettings _settings;
private readonly QueryHistory _history;
private readonly UserSelectedRecord _userSelectedRecord;
private readonly TopMostRecord _topMostRecord;
Expand All @@ -53,7 +53,7 @@ public class MainViewModel : BaseModel, ISavable, IDisposable

internal HotkeyManager HotkeyManager { get; set; }

public MainViewModel(Settings settings)
public MainViewModel(PowerToysRunSettings settings)
{
_saved = false;
_queryTextBeforeLeaveResults = string.Empty;
Expand Down Expand Up @@ -87,7 +87,7 @@ public MainViewModel(Settings settings)
HotkeyManager = new HotkeyManager();
_settings.PropertyChanged += (s, e) =>
{
if (e.PropertyName == nameof(Settings.Hotkey))
if (e.PropertyName == nameof(PowerToysRunSettings.Hotkey))
{
Application.Current.Dispatcher.Invoke(() =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ public class ResultsViewModel : BaseModel
{
private readonly object _collectionLock = new object();

private readonly Settings _settings;
private readonly PowerToysRunSettings _settings;

public ResultsViewModel()
{
Results = new ResultCollection();
BindingOperations.EnableCollectionSynchronization(Results, _collectionLock);
}

public ResultsViewModel(Settings settings)
public ResultsViewModel(PowerToysRunSettings settings)
: this()
{
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ namespace PowerLauncher.ViewModel
{
public class SettingWindowViewModel : BaseModel
{
private readonly WoxJsonStorage<Settings> _storage;
private readonly WoxJsonStorage<PowerToysRunSettings> _storage;

public SettingWindowViewModel()
{
_storage = new WoxJsonStorage<Settings>();
_storage = new WoxJsonStorage<PowerToysRunSettings>();
Settings = _storage.Load();
Settings.PropertyChanged += (s, e) =>
{
Expand All @@ -26,7 +26,7 @@ public SettingWindowViewModel()
};
}

public Settings Settings { get; set; }
public PowerToysRunSettings Settings { get; set; }

public void Save()
{
Expand Down
4 changes: 2 additions & 2 deletions src/modules/launcher/Wox.Core/Plugin/PluginConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static List<PluginMetadata> Parse(string[] pluginDirectories)
return PluginMetadatas;
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "All exception information is being logged")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Suppressing this to enable FxCop. We are logging the exception, and going forward general exceptions should not be caught")]
private static void ParsePluginConfigs(IEnumerable<string> directories)
{
// todo use linq when diable plugin is implemented since parallel.foreach + list is not thread saft
Expand Down Expand Up @@ -60,7 +60,7 @@ private static void ParsePluginConfigs(IEnumerable<string> directories)
}
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "All exception information is being logged")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Suppressing this to enable FxCop. We are logging the exception, and going forward general exceptions should not be caught")]
private static PluginMetadata GetPluginMetadata(string pluginDirectory)
{
string configPath = Path.Combine(pluginDirectory, PluginConfigName);
Expand Down
2 changes: 1 addition & 1 deletion src/modules/launcher/Wox.Core/Plugin/PluginInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ internal static void Install(string path)
}
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "All exception information is being logged")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Suppressing this to enable FxCop. We are logging the exception, and going forward general exceptions should not be caught")]
private static PluginMetadata GetMetadataFromJson(string pluginDirectory)
{
string configPath = Path.Combine(pluginDirectory, "plugin.json");
Expand Down
6 changes: 3 additions & 3 deletions src/modules/launcher/Wox.Core/Plugin/PluginManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public static void LoadPlugins(PluginSettings settings)
/// <summary>
/// Call initialize for all plugins
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "All exception information is being logged")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Suppressing this to enable FxCop. We are logging the exception, and going forward general exceptions should not be caught")]
public static void InitializePlugins(IPublicAPI api)
{
API = api ?? throw new ArgumentNullException(nameof(api));
Expand Down Expand Up @@ -160,7 +160,7 @@ public static List<PluginPair> ValidPluginsForQuery(Query query)
}
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "All exception information is being logged")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Suppressing this to enable FxCop. We are logging the exception, and going forward general exceptions should not be caught")]
public static List<Result> QueryForPlugin(PluginPair pair, Query query, bool delayedExecution = false)
{
if (pair == null)
Expand Down Expand Up @@ -262,7 +262,7 @@ public static IEnumerable<PluginPair> GetPluginsForInterface<T>()
return AllPlugins.Where(p => p.Plugin is T);
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "All exception information is being logged")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Suppressing this to enable FxCop. We are logging the exception, and going forward general exceptions should not be caught")]
public static List<ContextMenuResult> GetContextMenusForPlugin(Result result)
{
var pluginPair = _contextMenuPlugins.FirstOrDefault(o => o.Metadata.ID == result.PluginID);
Expand Down
2 changes: 1 addition & 1 deletion src/modules/launcher/Wox.Core/Plugin/PluginsLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static List<PluginPair> Plugins(List<PluginMetadata> metadatas)
return csharpPlugins;
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "All exception information is being logged")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Suppressing this to enable FxCop. We are logging the exception, and going forward general exceptions should not be caught")]
public static IEnumerable<PluginPair> CSharpPlugins(List<PluginMetadata> source)
{
var plugins = new List<PluginPair>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using Microsoft.Win32;
using Wox.Plugin;
using Wox.Plugin.Logger;

namespace Wox.Infrastructure.Exception
{
public class ExceptionFormatter
public static class ExceptionFormatter
{
public static string FormatException(System.Exception exception)
{
Expand Down Expand Up @@ -110,6 +112,7 @@ private static string CreateExceptionReport(System.Exception ex)
}

// http://msdn.microsoft.com/en-us/library/hh925568%28v=vs.110%29.aspx
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Suppressing this to enable FxCop. We are logging the exception, and going forward general exceptions should not be caught")]
private static List<string> GetFrameworkVersionFromRegistry()
{
try
Expand All @@ -119,25 +122,28 @@ private static List<string> GetFrameworkVersionFromRegistry()
{
foreach (string versionKeyName in ndpKey.GetSubKeyNames())
{
if (versionKeyName.StartsWith("v"))
// Using InvariantCulture since this is internal and involves version key
if (versionKeyName.StartsWith("v", StringComparison.InvariantCulture))
{
RegistryKey versionKey = ndpKey.OpenSubKey(versionKeyName);
string name = (string)versionKey.GetValue("Version", string.Empty);
string sp = versionKey.GetValue("SP", string.Empty).ToString();
string install = versionKey.GetValue("Install", string.Empty).ToString();
if (install != string.Empty)
if (!string.IsNullOrEmpty(install))
{
if (sp != string.Empty && install == "1")
if (!string.IsNullOrEmpty(sp) && install == "1")
{
result.Add(string.Format("{0} {1} SP{2}", versionKeyName, name, sp));
// Using InvariantCulture since this is internal
result.Add(string.Format(CultureInfo.InvariantCulture, "{0} {1} SP{2}", versionKeyName, name, sp));
}
else
{
result.Add(string.Format("{0} {1}", versionKeyName, name));
// Using InvariantCulture since this is internal
result.Add(string.Format(CultureInfo.InvariantCulture, "{0} {1}", versionKeyName, name));
}
}

if (name != string.Empty)
if (!string.IsNullOrEmpty(name))
{
continue;
}
Expand All @@ -146,21 +152,23 @@ private static List<string> GetFrameworkVersionFromRegistry()
{
RegistryKey subKey = versionKey.OpenSubKey(subKeyName);
name = (string)subKey.GetValue("Version", string.Empty);
if (name != string.Empty)
if (!string.IsNullOrEmpty(name))
{
sp = subKey.GetValue("SP", string.Empty).ToString();
}

install = subKey.GetValue("Install", string.Empty).ToString();
if (install != string.Empty)
if (!string.IsNullOrEmpty(install))
{
if (sp != string.Empty && install == "1")
if (!string.IsNullOrEmpty(sp) && install == "1")
{
result.Add(string.Format("{0} {1} {2} SP{3}", versionKeyName, subKeyName, name, sp));
// Using InvariantCulture since this is internal
result.Add(string.Format(CultureInfo.InvariantCulture, "{0} {1} {2} SP{3}", versionKeyName, subKeyName, name, sp));
}
else if (install == "1")
{
result.Add(string.Format("{0} {1} {2}", versionKeyName, subKeyName, name));
// Using InvariantCulture since this is internal
result.Add(string.Format(CultureInfo.InvariantCulture, "{0} {1} {2}", versionKeyName, subKeyName, name));
}
}
}
Expand Down Expand Up @@ -191,8 +199,9 @@ private static List<string> GetFrameworkVersionFromRegistry()

return result;
}
catch (System.Exception)
catch (System.Exception e)
{
Log.Exception("Could not get framework version from registry", e, MethodBase.GetCurrentMethod().DeclaringType);
return new List<string>();
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/modules/launcher/Wox.Infrastructure/FuzzyMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,21 @@ private FuzzyMatcher(string query, MatchOption opt)

public static FuzzyMatcher Create(string query)
{
if (query == null)
{
throw new ArgumentNullException(nameof(query));
}

return new FuzzyMatcher(query, new MatchOption());
}

public static FuzzyMatcher Create(string query, MatchOption opt)
{
if (query == null)
{
throw new ArgumentNullException(nameof(query));
}

return new FuzzyMatcher(query, opt);
}

Expand Down
1 change: 1 addition & 0 deletions src/modules/launcher/Wox.Infrastructure/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public static string Formatted<T>(this T t)
}

// Function to run as admin for context menu items
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Suppressing this to enable FxCop. We are logging the exception, and going forward general exceptions should not be caught")]
public static void RunAsAdmin(string path)
{
var info = new ProcessStartInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ private void Parse(string hotkeyString)
return;
}

List<string> keys = hotkeyString.Replace(" ", string.Empty).Split('+').ToList();
// Using InvariantCulture since this is internal and involves key codes
List<string> keys = hotkeyString.Replace(" ", string.Empty, StringComparison.InvariantCulture).Split('+').ToList();
if (keys.Contains("Alt"))
{
Alt = true;
Expand Down
8 changes: 4 additions & 4 deletions src/modules/launcher/Wox.Infrastructure/Hotkey/KeyEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ public enum KeyEvent
/// <summary>
/// Key down
/// </summary>
WM_KEYDOWN = 256,
WMKEYDOWN = 256,

/// <summary>
/// Key up
/// </summary>
WM_KEYUP = 257,
WMKEYUP = 257,

/// <summary>
/// System key up
/// </summary>
WM_SYSKEYUP = 261,
WMSYSKEYUP = 261,

/// <summary>
/// System key down
/// </summary>
WM_SYSKEYDOWN = 260,
WMSYSKEYDOWN = 260,
}
}

0 comments on commit ec8ead8

Please sign in to comment.