2020-08-18 01:00:56 +08:00
// 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.Windows.Controls ;
2020-10-01 11:37:46 +08:00
using Microsoft.Plugin.Folder.Sources ;
2020-10-23 00:45:48 +08:00
using Microsoft.PowerToys.Settings.UI.Library ;
2020-08-18 01:00:56 +08:00
using Wox.Infrastructure.Storage ;
using Wox.Plugin ;
namespace Microsoft.Plugin.Folder
{
public class Main : IPlugin , ISettingProvider , IPluginI18n , ISavable , IContextMenu , IDisposable
{
public const string FolderImagePath = "Images\\folder.dark.png" ;
public const string FileImagePath = "Images\\file.dark.png" ;
public const string DeleteFileFolderImagePath = "Images\\delete.dark.png" ;
public const string CopyImagePath = "Images\\copy.dark.png" ;
private static readonly PluginJsonStorage < FolderSettings > _storage = new PluginJsonStorage < FolderSettings > ( ) ;
private static readonly FolderSettings _settings = _storage . Load ( ) ;
2020-10-01 11:37:46 +08:00
private static readonly IQueryInternalDirectory _internalDirectory = new QueryInternalDirectory ( _settings , new QueryFileSystemInfo ( ) ) ;
private static readonly FolderHelper _folderHelper = new FolderHelper ( new DriveInformation ( ) , new FolderLinksSettings ( _settings ) ) ;
private static readonly ICollection < IFolderProcessor > _processors = new IFolderProcessor [ ]
{
new UserFolderProcessor ( _folderHelper ) ,
new InternalDirectoryProcessor ( _folderHelper , _internalDirectory ) ,
} ;
2020-08-18 01:00:56 +08:00
private static PluginInitContext _context ;
private IContextMenu _contextMenuLoader ;
2020-08-22 03:40:31 +08:00
private bool _disposed ;
2020-08-18 01:00:56 +08:00
public void Save ( )
{
_storage . Save ( ) ;
}
public Control CreateSettingPanel ( )
{
2020-09-03 06:24:59 +08:00
throw new NotImplementedException ( ) ;
2020-08-18 01:00:56 +08:00
}
public List < Result > Query ( Query query )
{
if ( query = = null )
{
throw new ArgumentNullException ( paramName : nameof ( query ) ) ;
}
2020-10-01 11:37:46 +08:00
var expandedName = FolderHelper . Expand ( query . Search ) ;
2020-08-18 01:00:56 +08:00
2020-10-01 11:37:46 +08:00
return _processors . SelectMany ( processor = > processor . Results ( query . ActionKeyword , expandedName ) )
. Select ( res = > res . Create ( _context . API ) )
. Select ( AddScore )
. ToList ( ) ;
2020-08-18 01:00:56 +08:00
}
2020-10-01 11:37:46 +08:00
public void Init ( PluginInitContext context )
2020-08-18 01:00:56 +08:00
{
2020-10-01 11:37:46 +08:00
_context = context ? ? throw new ArgumentNullException ( nameof ( context ) ) ;
_contextMenuLoader = new ContextMenuLoader ( context ) ;
2020-08-18 01:00:56 +08:00
2020-10-01 11:37:46 +08:00
_context . API . ThemeChanged + = OnThemeChanged ;
UpdateIconPath ( _context . API . GetCurrentTheme ( ) ) ;
2020-08-18 01:00:56 +08:00
}
2020-10-01 11:37:46 +08:00
public static IEnumerable < Result > GetFolderPluginResults ( Query query )
2020-08-18 01:00:56 +08:00
{
if ( query = = null )
{
throw new ArgumentNullException ( paramName : nameof ( query ) ) ;
}
2020-10-01 11:37:46 +08:00
var expandedName = FolderHelper . Expand ( query . Search ) ;
2020-08-18 01:00:56 +08:00
2020-10-01 11:37:46 +08:00
return _processors . SelectMany ( processor = > processor . Results ( query . ActionKeyword , expandedName ) )
. Select ( res = > res . Create ( _context . API ) )
. Select ( AddScore ) ;
2020-08-18 01:00:56 +08:00
}
2020-10-01 11:37:46 +08:00
private static void UpdateIconPath ( Theme theme )
2020-08-18 01:00:56 +08:00
{
2020-10-01 11:37:46 +08:00
QueryInternalDirectory . SetWarningIcon ( theme ) ;
2020-08-18 01:00:56 +08:00
}
2020-10-01 11:37:46 +08:00
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1313:Parameter names should begin with lower-case letter", Justification = "The parameter is unused")]
private static void OnThemeChanged ( Theme _ , Theme newTheme )
2020-08-18 01:00:56 +08:00
{
2020-10-01 11:37:46 +08:00
UpdateIconPath ( newTheme ) ;
2020-08-18 01:00:56 +08:00
}
2020-10-01 11:37:46 +08:00
// todo why was this hack here?
private static Result AddScore ( Result result )
2020-08-18 01:00:56 +08:00
{
2020-10-01 11:37:46 +08:00
result . Score + = 10 ;
2020-08-18 01:00:56 +08:00
return result ;
}
public string GetTranslatedPluginTitle ( )
{
2020-09-03 06:24:59 +08:00
return Properties . Resources . wox_plugin_folder_plugin_name ;
2020-08-18 01:00:56 +08:00
}
public string GetTranslatedPluginDescription ( )
{
2020-09-03 06:24:59 +08:00
return Properties . Resources . wox_plugin_folder_plugin_description ;
2020-08-18 01:00:56 +08:00
}
public List < ContextMenuResult > LoadContextMenus ( Result selectedResult )
{
return _contextMenuLoader . LoadContextMenus ( selectedResult ) ;
}
public void UpdateSettings ( PowerLauncherSettings settings )
{
}
public void Dispose ( )
{
Dispose ( disposing : true ) ;
GC . SuppressFinalize ( this ) ;
}
protected virtual void Dispose ( bool disposing )
{
if ( ! _disposed )
{
if ( disposing )
{
_context . API . ThemeChanged - = OnThemeChanged ;
_disposed = true ;
}
}
}
}
}