diff --git a/Source/TimeMachineDestinationAction.h b/Source/TimeMachineDestinationAction.h index 59bbb82f3..db551b3d1 100644 --- a/Source/TimeMachineDestinationAction.h +++ b/Source/TimeMachineDestinationAction.h @@ -8,14 +8,11 @@ #import "Action.h" - - - @interface TimeMachineDestinationAction : Action { - NSDictionary *destinationVolumePath; + NSString *destinationVolumePath; } -@property (retain) NSDictionary *destinationVolumePath; +@property (retain) NSString *destinationVolumePath; - (id) initWithDictionary: (NSDictionary *) dict; - (void) dealloc; @@ -27,28 +24,14 @@ + (NSString *) creationHelpText; + (NSArray *) limitedOptions; -- (id) initWithOption: (NSString *) option; - -- (void) didReceiveNotificationResponse:(NSNotification *) notification; @end -@interface TimeMachineDestinationActionSingleton : NSObject { - //@private - NSDictionary *tediumResponse; - NSDistributedNotificationCenter *tediumNotifications; -} - -@property (retain) NSDictionary *tediumResponse; +// Protocol implemented by the Tedium DO -+ (id) sharedSingleton; -- (void) registerForNotifications; -- (void) deRegisterForNotifications; -- (void) didReceiveNotification:(NSNotification *) notification; -- (void) sendAllDestinationsRequest; -- (void) getAllDestinations; -- (void) doGetAllDestinations; -- (void) setDestination:(NSString *)newDestination; +@protocol DOTediumApp +@property (nonatomic, readwrite, retain) NSString *currentDestination; +@property (nonatomic, readonly) NSArray *allDestinations; @end diff --git a/Source/TimeMachineDestinationAction.m b/Source/TimeMachineDestinationAction.m index 0bab8507b..a85100dee 100644 --- a/Source/TimeMachineDestinationAction.m +++ b/Source/TimeMachineDestinationAction.m @@ -10,6 +10,12 @@ #import #import "DSLogger.h" +@interface TimeMachineDestinationAction (Private) + ++ (id) getTediumApp; + +@end + @implementation TimeMachineDestinationAction @synthesize destinationVolumePath; @@ -19,14 +25,13 @@ - (id)init if (!(self = [super init])) return nil; - NSDictionary *destinationVolumePath; + destinationVolumePath = @""; return self; } - (id)initWithDictionary:(NSDictionary *)dict { - DSLog(@"%@",dict); if (!(self = [super initWithDictionary:dict])) return nil; @@ -35,9 +40,18 @@ - (id)initWithDictionary:(NSDictionary *)dict return self; } +- (id)initWithOption:(NSString *)option +{ + if (!(self = [super init])) + return nil; + + destinationVolumePath = [option copy]; + + return self; +} + - (void)dealloc { - [super dealloc]; } @@ -59,8 +73,13 @@ - (NSString *)description - (BOOL) execute: (NSString **) errorString { @try { - - + id tedium = [TimeMachineDestinationAction getTediumApp]; + if (tedium) + tedium.currentDestination = destinationVolumePath; + else + @throw [NSException exceptionWithName: @"ConnectionError" + reason: @"Couldn't connect to Tedium" + userInfo: nil]; } @catch (NSException *e) { DSLog(@"Exception: %@", e); *errorString = NSLocalizedString(@"Couldn't set Time Machine backup destination!", @"In TimeMachineDestinationAction"); @@ -82,144 +101,45 @@ + (NSString *)creationHelpText } + (NSArray *) limitedOptions { - NSMutableArray *opts = nil; + NSMutableArray *opts = [NSMutableArray new]; + NSArray *volumes = nil; - TimeMachineDestinationActionSingleton *tmdah = [TimeMachineDestinationActionSingleton sharedSingleton]; - - - // CP is going to now issue a notification to Tedium to see if it responds - // with a list of configured destinations - - [tmdah getAllDestinations]; - //[NSThread sleepForTimeInterval:2]; - - DSLog(@"tmdah came back with %@", [tmdah tediumResponse]); @try { -/* - TediumApplication *Tedium = [SBApplication applicationWithBundleIdentifier: @"com.dustinrue.Tedium"]; - - NSString *error; - NSPropertyListFormat format; - NSString *destinationsFromTedium = [Tedium allDestinations]; - NSData *theData = [destinationsFromTedium dataUsingEncoding:NSUTF8StringEncoding]; - NSDictionary *destinations = [NSPropertyListSerialization propertyListFromData:theData mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error]; -*/ - opts = [[NSMutableArray alloc] initWithCapacity:[[tmdah tediumResponse] count]]; - - for (NSDictionary *destination in [[tmdah tediumResponse] objectForKey:@"destinations"]) { - DSLog(@"destination is %@", destination); - [opts addObject:[NSDictionary dictionaryWithObjectsAndKeys: - [destination valueForKey:@"destinationVolumePath"], @"option", - [destination valueForKey:@"destinationVolumePath"], @"description", nil]]; - } + // fetch configured destinations + id tedium = [TimeMachineDestinationAction getTediumApp]; + if (tedium) + volumes = tedium.allDestinations; + // error check + if (!opts) + @throw [NSException exceptionWithName: @"ConnectionError" + reason: @"Couldn't connect to Tedium" + userInfo: nil]; } @catch (NSException *e) { DSLog(@"Exception: %@", e); opts = [NSArray array]; } - DSLog(@"leaving limitedOptions"); + // process results + for (NSString *volume in volumes) + [opts addObject: [NSDictionary dictionaryWithObjectsAndKeys: + volume, @"option", + volume, @"description", nil]]; + return opts; -} - - - (void) didReceiveNotificationResponse:(NSNotification *) notification { - DSLog(@"huh %@", [notification userInfo]); - TimeMachineDestinationActionSingleton *tmdas = [TimeMachineDestinationActionSingleton sharedSingleton]; - [tmdas setTediumResponse:[notification userInfo]]; - } -- (id)initWithOption:(NSString *)option -{ - self = [super init]; ++ (id) getTediumApp { + id tedium = nil; - destinationVolumePath = [option copy]; - - return self; -} - -@end - -@implementation TimeMachineDestinationActionSingleton - -static TimeMachineDestinationActionSingleton *_timeMachineDestinationActionSingleton = nil; - -@synthesize tediumResponse; - -+ (id) sharedSingleton { - @synchronized([TimeMachineDestinationActionSingleton class]) { - if (!_timeMachineDestinationActionSingleton) - _timeMachineDestinationActionSingleton = [[self alloc] init]; - - return _timeMachineDestinationActionSingleton; - } - - - return self; -} - -+ (id) alloc { - @synchronized([TimeMachineDestinationActionSingleton class]) { - NSAssert(_timeMachineDestinationActionSingleton == nil, @"Attempted to allocate a second instance of TimeMachineDestinationActionSingleton"); - _timeMachineDestinationActionSingleton = [super alloc]; - return _timeMachineDestinationActionSingleton; - } - - return nil; -} - -- (id) init { - self = [super init]; - NSLog(@"%@", tediumResponse); - if (self != nil) { - tediumResponse = [[NSDictionary alloc] init]; - tediumNotifications = [NSDistributedNotificationCenter defaultCenter]; - } - - return self; -} - -- (void) registerForNotifications { - DSLog(@"registering for notifications"); - [tediumNotifications addObserver:self selector:@selector(didReceiveNotification:) name:@"com.dustinrue.Tedium.allDestinationsResponse" object:nil]; -} - -- (void) deRegisterForNotifications { - [tediumNotifications removeObserver:self name:@"com.dustinrue.Tedium.allDestinationsResponse" object:nil]; -} - -- (void) didReceiveNotification:(NSNotification *) notification { - [self setTediumResponse:[notification userInfo]]; - DSLog(@"got response %@",[self tediumResponse]); - [self deRegisterForNotifications]; -} - -- (void) sendAllDestinationsRequest { - DSLog(@"sending notification"); - - [tediumNotifications postNotificationName:@"com.dustinrue.Tedium.allDestinationsRequest" object:nil userInfo:nil deliverImmediately:YES]; -} - -- (void) getAllDestinations { - [NSThread detachNewThreadSelector:@selector(doGetAllDestinations) toTarget:self withObject:nil]; - -} - -- (void) doGetAllDestinations { - DSLog(@"doing the things"); - [self registerForNotifications]; - [self sendAllDestinationsRequest]; -} - -- (void) setDestination:(NSString *)newDestination { - NSDictionary *args = [[[NSDictionary alloc] initWithObjectsAndKeys:newDestination, @"destinationVolumeName", nil] autorelease]; - [tediumNotifications postNotificationName:@"com.dustinrue.Tedium.setDestination" object:nil userInfo:args deliverImmediately:YES]; + tedium = [NSConnection rootProxyForConnectionWithRegisteredName: @"com.dustinrue.Tedium" host: nil]; + + if (!tedium) + DSLog(@"Couldn't connect to Tedium app"); + else + [tedium setProtocolForProxy: @protocol(DOTediumApp)]; + + return tedium; } - - @end - - - -