Skip to content

Commit

Permalink
Handle calls made before app finished starting
Browse files Browse the repository at this point in the history
Issue #41
  • Loading branch information
eofster committed Apr 8, 2016
1 parent fcde62f commit 88ec5d2
Showing 1 changed file with 50 additions and 30 deletions.
80 changes: 50 additions & 30 deletions Telephone/AppController.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,15 @@
// Dynamic store callback for DNS changes.
static void NameserversChanged(SCDynamicStoreRef store, CFArrayRef changedKeys, void *info);

NS_ASSUME_NONNULL_BEGIN

@interface AppController () <AKSIPUserAgentDelegate, NSUserNotificationCenterDelegate, PreferencesControllerDelegate>

@property(nonatomic, readonly) CompositionRoot *compositionRoot;
@property(nonatomic, readonly) PreferencesController *preferencesController;
@property(nonatomic, readonly) id<RingtonePlaybackInteractor> ringtonePlayback;
@property(nonatomic, getter=isFinishedLaunching) BOOL finishedLaunching;
@property(nonatomic, copy) NSString *destinationToCall;

// Installs Address Book plug-ins.
- (void)installAddressBookPlugIns;
Expand All @@ -75,6 +79,8 @@ - (void)updateCallsShouldDisplayAccountInfo;

@end

NS_ASSUME_NONNULL_END


@implementation AppController

Expand Down Expand Up @@ -227,6 +233,7 @@ - (instancetype)init {
[[self userAgent] setDelegate:self];
_preferencesController = _compositionRoot.preferencesController;
_ringtonePlayback = _compositionRoot.ringtonePlayback;
_destinationToCall = @"";
_accountControllers = [[NSMutableArray alloc] init];
[self setShouldRegisterAllAccounts:NO];
[self setShouldRestartUserAgentASAP:NO];
Expand Down Expand Up @@ -1349,6 +1356,10 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[accountController setAccountRegistered:YES];
}
}

[self makeCallAfterLaunchIfNeeded];

[self setFinishedLaunching:YES];
}

// Reopen all account windows when the user clicks the dock icon.
Expand Down Expand Up @@ -1517,65 +1528,74 @@ - (void)workspaceSessionDidBecomeActive:(NSNotification *)notification {
// change ActiveAccountViewController's
// tokenField:representedObjectForEditingString:.
- (void)addressBookDidDialCallDestination:(NSNotification *)notification {
if (![self canMakeCall]) {
return;
}

NSDictionary *userInfo = [notification userInfo];

[NSApp activateIgnoringOtherApps:YES];
[self makeCallOrRememberDestination:[self callDestinationWithAddressBookDidDialNotification:notification]];
}

- (NSString *)callDestinationWithAddressBookDidDialNotification:(NSNotification *)notification {
NSString *SIPAddressOrNumber = nil;
if ([[notification name] isEqualToString:AKAddressBookDidDialPhoneNumberNotification]) {
SIPAddressOrNumber = userInfo[@"AKPhoneNumber"];
SIPAddressOrNumber = notification.userInfo[@"AKPhoneNumber"];
} else if ([[notification name] isEqualToString:AKAddressBookDidDialSIPAddressNotification]) {
SIPAddressOrNumber = userInfo[@"AKSIPAddress"];
SIPAddressOrNumber = notification.userInfo[@"AKSIPAddress"];
}

NSString *name = userInfo[@"AKFullName"];

[NSApp activateIgnoringOtherApps:YES];

NSString *destination;

NSString *name = notification.userInfo[@"AKFullName"];

NSString *result;
if ([name length] > 0) {
destination = [NSString stringWithFormat:@"%@ <%@>", name, SIPAddressOrNumber];
result = [NSString stringWithFormat:@"%@ <%@>", name, SIPAddressOrNumber];
} else {
destination = SIPAddressOrNumber;
result = SIPAddressOrNumber;
}

[self.enabledAccountControllers[0] makeCallToDestinationRegisteringAccountIfNeeded:destination];
return result;
}


#pragma mark -
#pragma mark Apple event handler for URLs support

- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent {
if ([self canMakeCall]) {
[self.enabledAccountControllers[0] makeCallToDestinationRegisteringAccountIfNeeded:
[[event paramDescriptorForKeyword:keyDirectObject] stringValue]];
}
[self makeCallOrRememberDestination:[[event paramDescriptorForKeyword:keyDirectObject] stringValue]];
}


#pragma mark -
#pragma mark Service Provider

- (void)makeCallFromTextService:(NSPasteboard *)pboard userData:(NSString *)userData error:(NSString **)error {
if (![self canMakeCall]) {
return;
}

NSArray *classes = @[[NSString class]];
NSDictionary *options = @{};
if ([NSPasteboard instancesRespondToSelector:@selector(canReadObjectForClasses:options:)] &&
![pboard canReadObjectForClasses:classes options:options]) {
![pboard canReadObjectForClasses:@[[NSString class]] options:@{}]) {
NSLog(@"Could not make call, pboard couldn't give string.");
return;
}

[self.enabledAccountControllers[0] makeCallToDestinationRegisteringAccountIfNeeded:[pboard stringForType:NSPasteboardTypeString]];
[self makeCallOrRememberDestination:[pboard stringForType:NSPasteboardTypeString]];
}

#pragma mark -

- (void)makeCallAfterLaunchIfNeeded {
if (self.destinationToCall.length > 0) {
[self makeCallTo:self.destinationToCall];
self.destinationToCall = @"";
}
}

- (void)makeCallOrRememberDestination:(NSString *)destination {
if (self.isFinishedLaunching) {
[self makeCallTo:destination];
} else {
self.destinationToCall = destination;
}
}

- (void)makeCallTo:(NSString *)destination {
if ([self canMakeCall]) {
[self.enabledAccountControllers[0] makeCallToDestinationRegisteringAccountIfNeeded:destination];
}
}

- (BOOL)canMakeCall {
return NSApp.modalWindow == nil && self.enabledAccountControllers.count > 0;
}
Expand Down

0 comments on commit 88ec5d2

Please sign in to comment.