Removed dependeny to JMC.h and use NSInvocation instead.

This commit is contained in:
Thomas Dohmke 2011-12-03 16:19:14 +01:00
parent f3f6f2172f
commit d8bb3b07eb
2 changed files with 91 additions and 24 deletions

View File

@ -20,7 +20,10 @@
// THE SOFTWARE. // THE SOFTWARE.
@interface CNSHockeyManager : NSObject @interface CNSHockeyManager : NSObject {
@private
NSString *appIdentifier;
}
+ (CNSHockeyManager *)sharedHockeyManager; + (CNSHockeyManager *)sharedHockeyManager;

View File

@ -23,15 +23,9 @@
#import "BWQuincyManager.h" #import "BWQuincyManager.h"
#import "BWHockeyManager.h" #import "BWHockeyManager.h"
#ifdef JMC_PRESENT
#import "JMC.h"
#endif
@interface CNSHockeyManager () @interface CNSHockeyManager ()
#ifdef JMC_PRESENT
- (void)configureJMC; - (void)configureJMC;
#endif
@end @end
@ -65,7 +59,62 @@
} }
#endif #endif
- (void)configureWithIdentifier:(NSString *)appIdentifier delegate:(id)delegate { + (BOOL)isJMCActive {
id jmcClass = NSClassFromString(@"JMC");
id jmcInstance = [jmcClass performSelector:@selector(instance)];
return (jmcInstance) && ([jmcInstance performSelector:@selector(url)]);
}
+ (BOOL)isJMCPresent {
id jmcClass = NSClassFromString(@"JMC");
return (jmcClass) && ([jmcClass respondsToSelector:@selector(instance)]);
}
+ (void)disableJMCCrashReporter {
id jmcClass = NSClassFromString(@"JMC");
id jmcInstance = [jmcClass performSelector:@selector(instance)];
id jmcOptions = [jmcInstance performSelector:@selector(options)];
SEL crashReporterSelector = @selector(setCrashReportingEnabled:);
BOOL value = NO;
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[jmcOptions methodSignatureForSelector:crashReporterSelector]];
invocation.target = jmcOptions;
invocation.selector = crashReporterSelector;
[invocation setArgument:&value atIndex:2];
[invocation invoke];
}
+ (BOOL)checkJMCConfiguration:(NSDictionary *)configuration {
return (([[configuration valueForKey:@"enabled"] boolValue]) &&
([[configuration valueForKey:@"url"] length] > 0) &&
([[configuration valueForKey:@"key"] length] > 0) &&
([[configuration valueForKey:@"project"] length] > 0));
}
+ (void)applyJMCConfiguration:(NSDictionary *)configuration {
id jmcClass = NSClassFromString(@"JMC");
id jmcInstance = [jmcClass performSelector:@selector(instance)];
SEL configureSelector = @selector(configureJiraConnect:projectKey:apiKey:);
NSString *url = [configuration valueForKey:@"url"];
NSString *project = [configuration valueForKey:@"project"];
NSString *key = [configuration valueForKey:@"key"];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[jmcInstance methodSignatureForSelector:configureSelector]];
invocation.target = jmcInstance;
invocation.selector = configureSelector;
[invocation setArgument:&url atIndex:2];
[invocation setArgument:&project atIndex:3];
[invocation setArgument:&key atIndex:4];
[invocation invoke];
}
- (void)configureWithIdentifier:(NSString *)newAppIdentifier delegate:(id)delegate {
[appIdentifier release];
appIdentifier = [newAppIdentifier copy];
// Crash Reporting // Crash Reporting
[[BWQuincyManager sharedQuincyManager] setAppIdentifier:appIdentifier]; [[BWQuincyManager sharedQuincyManager] setAppIdentifier:appIdentifier];
@ -73,38 +122,53 @@
[[BWHockeyManager sharedHockeyManager] setAppIdentifier:appIdentifier]; [[BWHockeyManager sharedHockeyManager] setAppIdentifier:appIdentifier];
[[BWHockeyManager sharedHockeyManager] setCheckForTracker:YES]; [[BWHockeyManager sharedHockeyManager] setCheckForTracker:YES];
#ifdef JMC_PRESENT
// JMC // JMC
[[[JMC instance] options] setCrashReportingEnabled:NO]; if ([[self class] isJMCPresent]) {
[[BWHockeyManager sharedHockeyManager] addObserver:self forKeyPath:@"trackerConfig" options:0 context:nil]; [[BWHockeyManager sharedHockeyManager] addObserver:self forKeyPath:@"trackerConfig" options:0 context:nil];
[[self class] disableJMCCrashReporter];
[self performSelector:@selector(configureJMC) withObject:nil afterDelay:0]; [self performSelector:@selector(configureJMC) withObject:nil afterDelay:0];
#endif }
} }
#ifdef JMC_PRESENT
- (void)configureJMC { - (void)configureJMC {
// Return if JMC is already configured // Return if JMC is already configured
if ([[JMC instance] url]) { if ([[self class] isJMCActive]) {
return;
}
// Return if app id is nil
if (!appIdentifier) {
return; return;
} }
// Configure JMC from user defaults // Configure JMC from user defaults
NSDictionary *config = [[NSUserDefaults standardUserDefaults] valueForKey:@"CNSTrackerConfig"]; NSDictionary *configurations = [[NSUserDefaults standardUserDefaults] valueForKey:@"CNSTrackerConfigurations"];
if (([[config valueForKey:@"enabled"] boolValue]) && NSDictionary *configuration = [configurations valueForKey:appIdentifier];
([[config valueForKey:@"url"] length] > 0) && if ([[self class] checkJMCConfiguration:configuration]) {
([[config valueForKey:@"key"] length] > 0) && [[self class] applyJMCConfiguration:configuration];
([[config valueForKey:@"project"] length] > 0)) {
[[JMC instance] configureJiraConnect:[config valueForKey:@"url"] projectKey:[config valueForKey:@"project"] apiKey:[config valueForKey:@"key"]];
} }
} }
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([object trackerConfig]) { if ([object trackerConfig]) {
[[NSUserDefaults standardUserDefaults] setValue:[object trackerConfig] forKey:@"CNSTrackerConfig"]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[[NSUserDefaults standardUserDefaults] synchronize]; NSMutableDictionary *trackerConfig = [[defaults valueForKey:@"CNSTrackerConfigurations"] mutableCopy];
if (!trackerConfig) {
trackerConfig = [[NSMutableDictionary dictionaryWithCapacity:1] retain];
}
[trackerConfig setValue:[object trackerConfig] forKey:appIdentifier];
[defaults setValue:trackerConfig forKey:@"CNSTrackerConfigurations"];
[trackerConfig release];
[defaults synchronize];
[self configureJMC]; [self configureJMC];
} }
} }
#endif
- (void)dealloc {
[appIdentifier release], appIdentifier = nil;
[super dealloc];
}
@end @end