changed plugin APIs for entryAcitonPlugins and customAttributePlugins.

Internal housekeeping on MPPluginHost to allow for better access to certain plugins
Still pending: menu action to plugin delegation
This commit is contained in:
Michael Starke
2017-12-06 20:02:22 +01:00
parent 4fa52e23ef
commit 91508f0dde
3 changed files with 38 additions and 5 deletions

View File

@@ -65,13 +65,13 @@ NS_ASSUME_NONNULL_BEGIN
MacPass will call you back via -[MPPlugin performActionFroMenuItem:withEntries:] MacPass will call you back via -[MPPlugin performActionFroMenuItem:withEntries:]
*/ */
@protocol MPEntryActionPlugin <NSObject> @protocol MPEntryActionPlugin <NSObject>
@optional @required
- (NSMenuItem * _Nullable)menuItemForEntry; - (NSArray<NSMenuItem *> *)menuItemsForEntries:(NSArray< KPKEntry *>*)entries;
- (void)performActionForMenuItem:(NSMenuItem *)item withEntries:(NSArray <KPKEntry *>*)entries; - (void)performActionForMenuItem:(NSMenuItem *)item withEntries:(NSArray <KPKEntry *>*)entries;
@end @end
@protocol MPCustomAttributePlugin <NSObject> @protocol MPCustomAttributePlugin <NSObject>
@optional @required
/* Supply a list of attribute keys that will get suggested for autocompletion as well as added to the extend add for custom fields */ /* Supply a list of attribute keys that will get suggested for autocompletion as well as added to the extend add for custom fields */
@property (nonatomic,copy) NSArray<NSString *>* attributeKeys; @property (nonatomic,copy) NSArray<NSString *>* attributeKeys;
/* /*

View File

@@ -30,6 +30,7 @@ FOUNDATION_EXPORT NSString *const MPPluginHostDidLoadPlugin;
FOUNDATION_EXPORT NSString *const MPPluginHostPluginBundleIdentifiyerKey; FOUNDATION_EXPORT NSString *const MPPluginHostPluginBundleIdentifiyerKey;
@class MPPlugin; @class MPPlugin;
@class KPKEntry;
@interface MPPluginHost : NSObject @interface MPPluginHost : NSObject
@@ -50,4 +51,5 @@ FOUNDATION_EXPORT NSString *const MPPluginHostPluginBundleIdentifiyerKey;
- (NSArray <MPPlugin __kindof*>*)autotypePlugins; - (NSArray <MPPlugin __kindof*>*)autotypePlugins;
- (NSArray <MPPlugin __kindof*>*)entryContextMenuPlugins; - (NSArray <MPPlugin __kindof*>*)entryContextMenuPlugins;
*/ */
- (NSArray *)menuItemsForEntries:(NSArray <KPKEntry *>*)entries;
@end @end

View File

@@ -40,6 +40,10 @@ NSString *const MPPluginHostPluginBundleIdentifiyerKey = @"MPPluginHostPluginBun
@interface MPPluginHost () @interface MPPluginHost ()
@property (strong) NSMutableArray<MPPlugin __kindof *> *mutablePlugins; @property (strong) NSMutableArray<MPPlugin __kindof *> *mutablePlugins;
@property (strong) NSPointerArray *entryActionPlugins;
@property (strong) NSPointerArray *customAttributePlugins;
@property (nonatomic) BOOL loadUnsecurePlugins; @property (nonatomic) BOOL loadUnsecurePlugins;
@property (copy) NSArray<NSString *> *disabledPlugins; @property (copy) NSArray<NSString *> *disabledPlugins;
@@ -70,6 +74,9 @@ NSString *const MPPluginHostPluginBundleIdentifiyerKey = @"MPPluginHostPluginBun
_mutablePlugins = [[NSMutableArray alloc] init]; _mutablePlugins = [[NSMutableArray alloc] init];
_disabledPlugins = [[NSUserDefaults standardUserDefaults] arrayForKey:kMPSettingsKeyLoadUnsecurePlugins]; _disabledPlugins = [[NSUserDefaults standardUserDefaults] arrayForKey:kMPSettingsKeyLoadUnsecurePlugins];
_loadUnsecurePlugins = [[NSUserDefaults standardUserDefaults] boolForKey:kMPSettingsKeyLoadUnsecurePlugins]; _loadUnsecurePlugins = [[NSUserDefaults standardUserDefaults] boolForKey:kMPSettingsKeyLoadUnsecurePlugins];
_entryActionPlugins = [NSPointerArray weakObjectsPointerArray];
_customAttributePlugins = [NSPointerArray weakObjectsPointerArray];
[self _loadPlugins]; [self _loadPlugins];
[self bind:NSStringFromSelector(@selector(loadUnsecurePlugins)) [self bind:NSStringFromSelector(@selector(loadUnsecurePlugins))
@@ -205,7 +212,7 @@ NSString *const MPPluginHostPluginBundleIdentifiyerKey = @"MPPluginHostPluginBun
[[NSNotificationCenter defaultCenter] postNotificationName:MPPluginHostWillLoadPlugin [[NSNotificationCenter defaultCenter] postNotificationName:MPPluginHostWillLoadPlugin
object:self object:self
userInfo:@{ MPPluginHostPluginBundleIdentifiyerKey : plugin.identifier }]; userInfo:@{ MPPluginHostPluginBundleIdentifiyerKey : plugin.identifier }];
[self.mutablePlugins addObject:plugin]; [self _addPlugin:plugin];
[[NSNotificationCenter defaultCenter] postNotificationName:MPPluginHostDidLoadPlugin [[NSNotificationCenter defaultCenter] postNotificationName:MPPluginHostDidLoadPlugin
object:self object:self
userInfo:@{ MPPluginHostPluginBundleIdentifiyerKey : plugin.identifier }]; userInfo:@{ MPPluginHostPluginBundleIdentifiyerKey : plugin.identifier }];
@@ -222,7 +229,7 @@ NSString *const MPPluginHostPluginBundleIdentifiyerKey = @"MPPluginHostPluginBun
plugin.bundle = bundle; plugin.bundle = bundle;
plugin.enabled = NO; plugin.enabled = NO;
plugin.errorMessage = errorMessage; plugin.errorMessage = errorMessage;
[self.mutablePlugins addObject:plugin]; [self _addPlugin:plugin];
} }
- (BOOL)_validateUniqueBundle:(NSBundle *)bundle { - (BOOL)_validateUniqueBundle:(NSBundle *)bundle {
@@ -286,4 +293,28 @@ NSString *const MPPluginHostPluginBundleIdentifiyerKey = @"MPPluginHostPluginBun
return NO; return NO;
} }
- (void)_addPlugin:(MPPlugin *)plugin {
[self.mutablePlugins addObject:plugin];
if([plugin conformsToProtocol:@protocol(MPEntryActionPlugin)]) {
[self.entryActionPlugins addPointer:(__bridge void * _Nullable)(plugin)];
}
if([plugin conformsToProtocol:@protocol(MPCustomAttributePlugin)]) {
[self.customAttributePlugins addPointer:(__bridge void * _Nullable)(plugin)];
}
}
#pragma mark Action Plugins
- (NSArray *)menuItemsForEntries:(NSArray<KPKEntry *> *)entries {
NSMutableArray *items = [[NSMutableArray alloc] init];
for(id<MPEntryActionPlugin> plugin in self.entryActionPlugins) {
if(plugin) {
[items addObjectsFromArray:[plugin menuItemsForEntries:entries]];
}
}
return [items copy];
}
@end @end