prototyped plugin repository browsing

This commit is contained in:
Michael Starke
2017-12-04 16:03:50 +01:00
parent fc496189b6
commit 5879de1e4d
6 changed files with 136 additions and 3 deletions

View File

@@ -39,4 +39,5 @@ FOUNDATION_EXPORT NSString *const MPPluginUTI;
*/
FOUNDATION_EXPORT NSString *const MPBundleHelpURLKey; // MPHelpURL
FOUNDATION_EXPORT NSString *const MPBundlePluginRepositoryURLKey; // MPPluginRepositoryURL
FOUNDATION_EXPORT NSString *const MPBundlePluginCompaibilityURLKey; // MPPluginCompaibilityURL
#endif

View File

@@ -27,6 +27,7 @@ NSString *const MPKdbDocumentUTI = @"com.hicknhack.macpass.kdb";
NSString *const MPKdbxDocumentUTI = @"com.hicknhack.macpass.kdbx";
NSString *const MPPluginUTI = @"com.hicknhack.macpass.plugin";
NSString *const MPBundleHelpURLKey = @"MPHelpURL";
NSString *const MPBundlePluginRepositoryURLKey = @"MPPluginRepositoryURL"
NSString *const MPBundleHelpURLKey = @"MPHelpURL";
NSString *const MPBundlePluginRepositoryURLKey = @"MPPluginRepositoryURL";
NSString *const MPBundlePluginCompaibilityURLKey = @"MPPluginCompaibilityURL";

View File

@@ -0,0 +1,31 @@
//
// MPPluginRepository.h
// MacPass
//
// Created by Michael Starke on 04.12.17.
// Copyright © 2017 HicknHack Software GmbH. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface MPPluginRespositoryItem : NSObject
@property (copy) NSString *name;
@property (copy) NSString *version;
@property (copy) NSString *descriptionText;
@property (copy) NSURL *sourceURL;
@property (copy) NSURL *downloadURL;
@property (readonly, nonatomic, getter=isVaid) BOOL valid;
+ (instancetype)pluginItemFromDictionary:(NSDictionary *)dict;
- (instancetype)initWithDictionary:(NSDictionary *)dict;
@end
@interface MPPluginRepository : NSObject
@property (nonatomic, copy) NSArray<MPPluginRespositoryItem *> *availablePlugins;
+ (instancetype)sharedRespoitory;
@end

View File

@@ -0,0 +1,94 @@
//
// MPPluginRepository.m
// MacPass
//
// Created by Michael Starke on 04.12.17.
// Copyright © 2017 HicknHack Software GmbH. All rights reserved.
//
#import "MPPluginRepository.h"
#import "MPConstants.h"
NSString *const MPPluginItemNameKey = @"name";
NSString *const MPPluginItemDescriptionKey = @"description";
NSString *const MPPluginItemDownloadURLKey = @"download";
NSString *const MPPluginItemSourceURLKey = @"source";
NSString *const MPPluginItemVersionKey = @"version";
@implementation MPPluginRespositoryItem
@dynamic valid;
+ (instancetype)pluginItemFromDictionary:(NSDictionary *)dict {
return [[MPPluginRespositoryItem alloc] initWithDictionary:dict];
}
- (instancetype)initWithDictionary:(NSDictionary *)dict {
self = [super init];
if(self) {
self.name = dict[MPPluginItemNameKey];
self.descriptionText = dict[MPPluginItemDescriptionKey];
self.downloadURL = [NSURL URLWithString:dict[MPPluginItemDownloadURLKey]];
self.sourceURL = [NSURL URLWithString:dict[MPPluginItemSourceURLKey]];
self.version = dict[MPPluginItemVersionKey];
}
return self;
}
- (BOOL)isVaid {
/* name and download seems ok */
return (self.name.length > 0 && self.downloadURL);
}
@end
@implementation MPPluginRepository
@dynamic availablePlugins;
+ (instancetype)sharedRespoitory {
static MPPluginRepository *instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[MPPluginRepository alloc] init];
});
return instance;
}
- (instancetype)init {
self = [super init];
return self;
}
- (NSArray<MPPluginRespositoryItem *> *)availablePlugins {
NSString *urlString = NSBundle.mainBundle.infoDictionary[MPBundlePluginRepositoryURLKey];
if(!urlString) {
return @[];
}
NSURL *jsonURL = [NSURL URLWithString:urlString];
if(!jsonURL) {
return @[];
}
NSError *error;
NSData *jsonData = [NSData dataWithContentsOfURL:jsonURL options:0 error:&error];
if(!jsonData) {
return @[];
}
id jsonRoot = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
if(!jsonRoot || ![jsonRoot isKindOfClass:NSArray.class]) {
return @[];
}
NSMutableArray *items = [[NSMutableArray alloc] init];
for(id item in jsonRoot) {
if(![item isKindOfClass:NSDictionary.class]) {
continue;
}
MPPluginRespositoryItem *pluginItem = [MPPluginRespositoryItem pluginItemFromDictionary:item];
if(pluginItem.isVaid) {
[items addObject:pluginItem];
}
}
return [items copy];
}
@end

View File

@@ -68,7 +68,7 @@
<key>MPHelpURL</key>
<string>https://github.com/mstarke/MacPass</string>
<key>MPPluginRepositoryURL</key>
<string>file:///Users/michael/Projekte/GitHub/MacPassPlugins/plugins.json</string>
<string>https://macpass.github.io/data/plugins.json</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2012-2017 HicknHack Software GmbH. All rights reserved.</string>
<key>NSMainNibFile</key>