diff --git a/Telephone/AppController.m b/Telephone/AppController.m index 199b91d60..5b1fda220 100644 --- a/Telephone/AppController.m +++ b/Telephone/AppController.m @@ -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 () @property(nonatomic, readonly) CompositionRoot *compositionRoot; @property(nonatomic, readonly) PreferencesController *preferencesController; @property(nonatomic, readonly) id ringtonePlayback; +@property(nonatomic, getter=isFinishedLaunching) BOOL finishedLaunching; +@property(nonatomic, copy) NSString *destinationToCall; // Installs Address Book plug-ins. - (void)installAddressBookPlugIns; @@ -75,6 +79,8 @@ - (void)updateCallsShouldDisplayAccountInfo; @end +NS_ASSUME_NONNULL_END + @implementation AppController @@ -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]; @@ -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. @@ -1517,31 +1528,28 @@ - (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; } @@ -1549,10 +1557,7 @@ - (void)addressBookDidDialCallDestination:(NSNotification *)notification { #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]]; } @@ -1560,22 +1565,37 @@ - (void)handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppl #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; }