2010-09-30 12:27:22 -05:00
|
|
|
//
|
|
|
|
// MGMSIPUser.m
|
|
|
|
// VoiceMob
|
|
|
|
//
|
|
|
|
// Created by Mr. Gecko on 9/24/10.
|
2013-07-20 06:56:17 -05:00
|
|
|
// Copyright (c) 2011 Mr. Gecko's Media (James Coleman). http://mrgeckosmedia.com/
|
2010-09-30 12:27:22 -05:00
|
|
|
//
|
|
|
|
|
2013-07-20 06:56:17 -05:00
|
|
|
#if MGMSIPENABLED
|
2010-09-30 12:27:22 -05:00
|
|
|
#import "MGMSIPUser.h"
|
2013-07-20 06:56:17 -05:00
|
|
|
#import "MGMSIPCallView.h"
|
|
|
|
#import "MGMSIPPad.h"
|
|
|
|
#import "MGMSIPContacts.h"
|
|
|
|
#import "MGMSIPInbox.h"
|
|
|
|
#import "MGMSIPRecordings.h"
|
|
|
|
#import "MGMAccountController.h"
|
|
|
|
#import "MGMController.h"
|
|
|
|
#import "MGMVoiceUser.h"
|
|
|
|
#import "MGMProgressView.h"
|
2010-09-30 12:27:22 -05:00
|
|
|
#import "MGMVMAddons.h"
|
|
|
|
#import <MGMUsers/MGMUsers.h>
|
2010-10-01 22:03:15 -05:00
|
|
|
#import <VoiceBase/VoiceBase.h>
|
2010-09-30 12:27:22 -05:00
|
|
|
|
2013-07-20 06:56:17 -05:00
|
|
|
const int MGMSIPKeypadTabIndex = 0;
|
|
|
|
const int MGMSIPContactsTabIndex = 1;
|
|
|
|
const int MGMSIPInboxTabIndex = 2;
|
|
|
|
const int MGMSIPRecordingsTabIndex = 3;
|
|
|
|
|
2010-09-30 12:27:22 -05:00
|
|
|
NSString * const MGMSIPUserAreaCode = @"MGMVSIPUserAreaCode";
|
2013-07-20 06:56:17 -05:00
|
|
|
NSString * const MGMSIPCurrentTab = @"MGMSIPCurrentTab";
|
2010-09-30 12:27:22 -05:00
|
|
|
|
|
|
|
@implementation MGMSIPUser
|
|
|
|
+ (id)SIPUser:(MGMUser *)theUser accountController:(MGMAccountController *)theAccountController {
|
|
|
|
return [[[self alloc] initWithUser:theUser accountController:theAccountController] autorelease];
|
|
|
|
}
|
|
|
|
- (id)initWithUser:(MGMUser *)theUser accountController:(MGMAccountController *)theAccountController {
|
2013-07-20 06:56:17 -05:00
|
|
|
if ((self = [super init])) {
|
2010-09-30 12:27:22 -05:00
|
|
|
accountController = theAccountController;
|
|
|
|
user = [theUser retain];
|
2013-07-20 06:56:17 -05:00
|
|
|
[self registerSettings];
|
|
|
|
|
|
|
|
if ([user isStarted]) {
|
|
|
|
account = [[MGMSIPAccount alloc] initWithSettings:[user settings]];
|
|
|
|
[account setDelegate:self];
|
|
|
|
calls = [NSMutableArray new];
|
|
|
|
loggingIn = NO;
|
|
|
|
acountRegistered = NO;
|
|
|
|
contacts = [[MGMContacts contactsWithClass:NSClassFromString([user settingForKey:MGMSContactsSourceKey]) delegate:self] retain];
|
|
|
|
[contacts updateContacts];
|
|
|
|
|
|
|
|
currentTab = [[user settingForKey:MGMSIPCurrentTab] intValue];
|
|
|
|
tabObjects = [NSMutableArray new];
|
|
|
|
[tabObjects addObject:[MGMSIPPad tabWithSIPUser:self]];
|
|
|
|
[tabObjects addObject:[MGMSIPContacts tabWithSIPUser:self]];
|
|
|
|
[tabObjects addObject:[MGMSIPInbox tabWithSIPUser:self]];
|
|
|
|
[tabObjects addObject:[MGMSIPRecordings tabWithSIPUser:self]];
|
|
|
|
|
|
|
|
loggingIn = YES;
|
|
|
|
[NSThread detachNewThreadSelector:@selector(login) toTarget:account withObject:nil];
|
|
|
|
}
|
2010-09-30 12:27:22 -05:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
- (void)dealloc {
|
2013-07-20 06:56:17 -05:00
|
|
|
#if releaseDebug
|
|
|
|
NSLog(@"%s Releasing", __PRETTY_FUNCTION__);
|
|
|
|
#endif
|
2010-09-30 12:27:22 -05:00
|
|
|
[self releaseView];
|
2013-07-20 06:56:17 -05:00
|
|
|
[tabObjects removeAllObjects];
|
|
|
|
[tabObjects release];
|
|
|
|
[SIPRegistrationTimeout invalidate];
|
|
|
|
[SIPRegistrationTimeout release];
|
|
|
|
[calls removeAllObjects];
|
|
|
|
[calls release];
|
|
|
|
[account setDelegate:nil];
|
|
|
|
[account logout];
|
|
|
|
[account release];
|
|
|
|
[contacts stop];
|
|
|
|
[contacts setDelegate:nil];
|
|
|
|
[contacts release];
|
|
|
|
[user release];
|
|
|
|
[callToAwnswer release];
|
|
|
|
[optionsNumber release];
|
|
|
|
[callToAwnswer release];
|
2010-09-30 12:27:22 -05:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2013-07-20 06:56:17 -05:00
|
|
|
- (void)registerSettings {
|
|
|
|
NSMutableDictionary *settings = [NSMutableDictionary dictionary];
|
|
|
|
[settings setObject:NSStringFromClass([MGMAddressBook class]) forKey:MGMSContactsSourceKey];
|
|
|
|
[settings setObject:[NSNumber numberWithInt:MGMSIPKeypadTabIndex] forKey:MGMSIPCurrentTab];
|
|
|
|
[user registerSettings:settings];
|
|
|
|
}
|
|
|
|
|
2010-09-30 12:27:22 -05:00
|
|
|
- (MGMAccountController *)accountController {
|
|
|
|
return accountController;
|
|
|
|
}
|
|
|
|
- (MGMUser *)user {
|
|
|
|
return user;
|
|
|
|
}
|
2013-07-20 06:56:17 -05:00
|
|
|
- (MGMContacts *)contacts {
|
|
|
|
return contacts;
|
|
|
|
}
|
|
|
|
- (NSArray *)calls {
|
|
|
|
return calls;
|
|
|
|
}
|
2010-09-30 12:27:22 -05:00
|
|
|
- (NSString *)title {
|
|
|
|
if ([user settingForKey:MGMSIPAccountFullName]!=nil && ![[user settingForKey:MGMSIPAccountFullName] isEqual:@""])
|
|
|
|
return [user settingForKey:MGMSIPAccountFullName];
|
2013-07-20 06:56:17 -05:00
|
|
|
NSString *userName = [user settingForKey:MGMUserName];
|
|
|
|
if ([userName isPhoneComplete])
|
|
|
|
userName = [userName readableNumber];
|
|
|
|
return userName;
|
|
|
|
}
|
|
|
|
- (NSString *)areaCode {
|
|
|
|
return [user settingForKey:MGMSIPUserAreaCode];
|
|
|
|
}
|
|
|
|
- (NSString *)password {
|
|
|
|
return [user password];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)registrationChanged {
|
|
|
|
[SIPRegistrationTimeout invalidate];
|
|
|
|
[SIPRegistrationTimeout release];
|
|
|
|
SIPRegistrationTimeout = nil;
|
|
|
|
if (!acountRegistered) {
|
|
|
|
if (![account isRegistered]) {
|
|
|
|
UIAlertView *alert = [[UIAlertView new] autorelease];
|
|
|
|
[alert setTitle:@"Unable to Register with Server. Please check your credentials."];
|
|
|
|
[alert setMessage:[account lastError]];
|
|
|
|
[alert addButtonWithTitle:MGMOkButtonTitle];
|
|
|
|
[alert show];
|
|
|
|
}
|
|
|
|
acountRegistered = YES;
|
|
|
|
[self performSelectorOnMainThread:@selector(removeLoginProgress) withObject:nil waitUntilDone:NO];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
- (void)loggedIn {
|
|
|
|
loggingIn = NO;
|
|
|
|
[self performSelectorOnMainThread:@selector(startRegistrationTimeoutTimer) withObject:nil waitUntilDone:NO];
|
|
|
|
}
|
|
|
|
- (void)startRegistrationTimeoutTimer {
|
|
|
|
if (!acountRegistered)
|
|
|
|
SIPRegistrationTimeout = [[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(SIPTimeout) userInfo:nil repeats:NO] retain];
|
|
|
|
}
|
|
|
|
- (void)SIPTimeout {
|
|
|
|
[SIPRegistrationTimeout invalidate];
|
|
|
|
[SIPRegistrationTimeout release];
|
|
|
|
SIPRegistrationTimeout = nil;
|
|
|
|
[account setLastError:@"Registration Timeout."];
|
|
|
|
[self loginErrored];
|
|
|
|
}
|
|
|
|
- (void)removeLoginProgress {
|
|
|
|
if (progressView!=nil) {
|
|
|
|
[progressView stopProgess];
|
|
|
|
[progressView setNeedsDisplay];
|
|
|
|
|
|
|
|
[UIView beginAnimations:nil context:nil];
|
|
|
|
[UIView setAnimationDuration:1.0];
|
|
|
|
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
|
|
|
|
[UIView setAnimationDelegate:self];
|
|
|
|
[UIView setAnimationDidStopSelector:@selector(progressFadeAnimationDidStop:finished:context:)];
|
|
|
|
[progressView setAlpha:0.0];
|
|
|
|
[UIView commitAnimations];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
- (void)progressFadeAnimationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
|
|
|
|
[progressView removeFromSuperview];
|
|
|
|
[progressView release];
|
|
|
|
progressView = nil;
|
|
|
|
}
|
|
|
|
- (void)loginErrored {
|
|
|
|
loggingIn = NO;
|
|
|
|
UIAlertView *alert = [[UIAlertView new] autorelease];
|
|
|
|
[alert setTitle:@"Error logging in"];
|
|
|
|
[alert setMessage:[account lastError]];
|
|
|
|
[alert addButtonWithTitle:MGMOkButtonTitle];
|
|
|
|
[alert show];
|
|
|
|
|
|
|
|
[progressView stopProgess];
|
|
|
|
[progressView removeFromSuperview];
|
|
|
|
[progressView release];
|
|
|
|
progressView = nil;
|
|
|
|
[self performSelectorOnMainThread:@selector(removeLoginProgress) withObject:nil waitUntilDone:NO];
|
|
|
|
}
|
|
|
|
- (void)logoutErrored {
|
|
|
|
UIAlertView *alert = [[UIAlertView new] autorelease];
|
|
|
|
[alert setTitle:@"Error logging out"];
|
|
|
|
[alert setMessage:[account lastError]];
|
|
|
|
[alert addButtonWithTitle:MGMOkButtonTitle];
|
|
|
|
[alert show];
|
2010-09-30 12:27:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
- (UIView *)view {
|
|
|
|
if (view==nil) {
|
|
|
|
if (![[NSBundle mainBundle] loadNibNamed:[[UIDevice currentDevice] appendDeviceSuffixToString:@"SIPUser"] owner:self options:nil]) {
|
|
|
|
NSLog(@"Unable to load SIP User");
|
|
|
|
} else {
|
2013-07-20 06:56:17 -05:00
|
|
|
[tabView addSubview:[[tabObjects objectAtIndex:currentTab] view]];
|
|
|
|
[tabBar setSelectedItem:[[tabBar items] objectAtIndex:currentTab]];
|
|
|
|
if (![account isRegistered]) {
|
|
|
|
CGSize contentSize = [view frame].size;
|
|
|
|
progressView = [[MGMProgressView alloc] initWithFrame:CGRectMake(0, 0, contentSize.width, contentSize.height)];
|
|
|
|
[progressView setProgressTitle:@"Logging In"];
|
|
|
|
[view addSubview:progressView];
|
|
|
|
[progressView startProgess];
|
|
|
|
[progressView becomeFirstResponder];
|
|
|
|
}
|
2010-09-30 12:27:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return view;
|
|
|
|
}
|
2013-07-20 06:56:17 -05:00
|
|
|
- (NSArray *)tabObjects {
|
|
|
|
return tabObjects;
|
|
|
|
}
|
|
|
|
- (UIView *)tabView {
|
|
|
|
return tabView;
|
|
|
|
}
|
|
|
|
- (UITabBar *)tabBar {
|
|
|
|
return tabBar;
|
|
|
|
}
|
2010-09-30 12:27:22 -05:00
|
|
|
- (void)releaseView {
|
2013-07-20 06:56:17 -05:00
|
|
|
#if releaseDebug
|
|
|
|
NSLog(@"%s Releasing", __PRETTY_FUNCTION__);
|
|
|
|
#endif
|
|
|
|
[[tabObjects objectAtIndex:currentTab] releaseView];
|
|
|
|
[view release];
|
|
|
|
view = nil;
|
|
|
|
[tabView release];
|
|
|
|
tabView = nil;
|
|
|
|
[tabBar release];
|
|
|
|
tabBar = nil;
|
|
|
|
[progressView stopProgess];
|
|
|
|
[progressView release];
|
|
|
|
progressView = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updatedContacts {
|
|
|
|
[[tabObjects objectAtIndex:MGMSIPContactsTabIndex] updatedContacts];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)call:(NSString *)theNumber {
|
|
|
|
[[tabObjects objectAtIndex:MGMSIPInboxTabIndex] addPhoneNumber:theNumber type:MGMIPlacedType];
|
|
|
|
[account makeCallToNumber:theNumber];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)phoneCalling {
|
|
|
|
for (int i=0; i<[[accountController contactsControllers] count]; i++) {
|
|
|
|
if ([[[accountController contactsControllers] objectAtIndex:i] isKindOfClass:[MGMVoiceUser class]] && [[[accountController contactsControllers] objectAtIndex:i] isPlacingCall]) {
|
|
|
|
MGMVoiceUser *voiceUser = [[accountController contactsControllers] objectAtIndex:i];
|
|
|
|
[voiceUser donePlacingCall];
|
|
|
|
return [voiceUser currentPhoneNumber];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
- (void)gotNewCall:(MGMSIPCall *)theCall {
|
|
|
|
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
|
|
|
|
if ([[accountController controller] isInBackground]) {
|
|
|
|
[self performSelectorOnMainThread:@selector(showNotificationForCall:) withObject:theCall waitUntilDone:YES];
|
|
|
|
} else {
|
|
|
|
#endif
|
|
|
|
[self performSelectorOnMainThread:@selector(mainGotNewCall:) withObject:theCall waitUntilDone:NO];
|
|
|
|
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
- (void)answerCall {
|
|
|
|
if (callToAwnswer!=nil) {
|
|
|
|
if ([callToAwnswer state]!=MGMSIPCallDisconnectedState) {
|
|
|
|
[callToAwnswer answer];
|
|
|
|
[self performSelectorOnMainThread:@selector(mainGotNewCall:) withObject:callToAwnswer waitUntilDone:NO];
|
|
|
|
}
|
|
|
|
[callToAwnswer release];
|
|
|
|
callToAwnswer = nil;
|
2010-09-30 12:27:22 -05:00
|
|
|
}
|
|
|
|
}
|
2013-07-20 06:56:17 -05:00
|
|
|
- (void)clearCall {
|
|
|
|
[callToAwnswer release];
|
|
|
|
callToAwnswer = nil;
|
|
|
|
}
|
|
|
|
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
|
|
|
|
- (void)showNotificationForCall:(MGMSIPCall *)theCall {
|
|
|
|
UILocalNotification *alert = [[[UILocalNotification alloc] init] autorelease];
|
|
|
|
if (alert!=nil) {
|
|
|
|
[callToAwnswer release];
|
|
|
|
callToAwnswer = [theCall retain];
|
|
|
|
[alert setRepeatInterval:0];
|
|
|
|
NSString *name = [[theCall remoteURL] userName];
|
|
|
|
if ([name isPhone])
|
|
|
|
name = [contacts nameForNumber:[name phoneFormatWithAreaCode:[self areaCode]]];
|
|
|
|
[alert setAlertBody:[NSString stringWithFormat:@"Call from %@", name]];
|
|
|
|
[alert setAlertAction:@"Answer"];
|
|
|
|
|
|
|
|
[[UIApplication sharedApplication] presentLocalNotificationNow:alert];
|
|
|
|
//[theCall sendRingingNotification];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
- (void)mainGotNewCall:(MGMSIPCall *)theCall {
|
|
|
|
MGMSIPCallView *callView = [MGMSIPCallView viewWithCall:theCall SIPUser:self];
|
|
|
|
[calls addObject:callView];
|
|
|
|
[[accountController controller] showCallView:callView];
|
|
|
|
}
|
|
|
|
- (void)callDone:(MGMSIPCallView *)theCall {
|
|
|
|
if ([[theCall call] isIncoming])
|
|
|
|
[[tabObjects objectAtIndex:MGMSIPInboxTabIndex] addPhoneNumber:[[[theCall call] remoteURL] userName] type:([theCall didAnswer] ? MGMIReceivedType : MGMIMissedType)];
|
|
|
|
[[accountController controller] performSelectorOnMainThread:@selector(dismissCallView:) withObject:theCall waitUntilDone:NO];
|
|
|
|
[calls removeObject:theCall];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)isUserDone:(MGMUser *)theUser {
|
|
|
|
return !loggingIn;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)tabBar:(UITabBar *)theTabBar didSelectItem:(UITabBarItem *)item {
|
|
|
|
int tabIndex = [[tabBar items] indexOfObject:item];
|
|
|
|
if (tabIndex==currentTab)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (tabIndex!=MGMSIPRecordingsTabIndex)
|
|
|
|
[accountController setItems:[accountController accountItems] animated:YES];
|
|
|
|
|
|
|
|
id tab = [tabObjects objectAtIndex:currentTab];
|
|
|
|
currentTab = tabIndex;
|
|
|
|
[user setSetting:[NSNumber numberWithInt:currentTab] forKey:MGMSIPCurrentTab];
|
|
|
|
id newTab = [tabObjects objectAtIndex:currentTab];
|
|
|
|
CGRect tabFrame = [[newTab view] frame];
|
|
|
|
tabFrame.size = [tabView frame].size;
|
|
|
|
[[newTab view] setFrame:tabFrame];
|
|
|
|
[tabView addSubview:[newTab view]];
|
|
|
|
[[tab view] removeFromSuperview];
|
|
|
|
[tab releaseView];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)showOptionsForNumber:(NSString *)theNumber {
|
|
|
|
optionsNumber = [theNumber copy];
|
|
|
|
UIActionSheet *theAction = [[UIActionSheet new] autorelease];
|
|
|
|
[theAction addButtonWithTitle:@"Call"];
|
|
|
|
[theAction addButtonWithTitle:@"Reverse Lookup"];
|
|
|
|
[theAction addButtonWithTitle:@"Cancel"];
|
|
|
|
[theAction setCancelButtonIndex:2];
|
|
|
|
[theAction setDelegate:self];
|
|
|
|
[theAction showInView:view];
|
|
|
|
}
|
|
|
|
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
|
|
|
|
if (buttonIndex==0)
|
|
|
|
[self call:optionsNumber];
|
|
|
|
else if (buttonIndex==1)
|
|
|
|
[[accountController controller] showReverseLookupWithNumber:optionsNumber];
|
|
|
|
[optionsNumber release];
|
|
|
|
optionsNumber = nil;
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
#endif
|