Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 6 additions & 23 deletions Source/TimeMachineDestinationAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@

#import "Action.h"




@interface TimeMachineDestinationAction : Action <ActionWithLimitedOptions> {
NSDictionary *destinationVolumePath;
NSString *destinationVolumePath;
}

@property (retain) NSDictionary *destinationVolumePath;
@property (retain) NSString *destinationVolumePath;

- (id) initWithDictionary: (NSDictionary *) dict;
- (void) dealloc;
Expand All @@ -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 <NSObject>

@property (nonatomic, readwrite, retain) NSString *currentDestination;
@property (nonatomic, readonly) NSArray *allDestinations;

@end
182 changes: 51 additions & 131 deletions Source/TimeMachineDestinationAction.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
#import <ScriptingBridge/SBApplication.h>
#import "DSLogger.h"

@interface TimeMachineDestinationAction (Private)

+ (id<DOTediumApp>) getTediumApp;

@end

@implementation TimeMachineDestinationAction

@synthesize destinationVolumePath;
Expand All @@ -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;

Expand All @@ -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];
}

Expand All @@ -59,8 +73,13 @@ - (NSString *)description

- (BOOL) execute: (NSString **) errorString {
@try {


id<DOTediumApp> 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");
Expand All @@ -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<DOTediumApp> 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<DOTediumApp>) 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