tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2015, Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #import "ARDMainViewController.h" |
| 29 | |
| 30 | #import "ARDAppClient.h" |
| 31 | #import "ARDMainView.h" |
| 32 | #import "ARDVideoCallViewController.h" |
| 33 | |
| 34 | @interface ARDMainViewController () <ARDMainViewDelegate> |
| 35 | @end |
| 36 | |
| 37 | @implementation ARDMainViewController |
| 38 | |
| 39 | - (void)loadView { |
| 40 | ARDMainView *mainView = [[ARDMainView alloc] initWithFrame:CGRectZero]; |
| 41 | mainView.delegate = self; |
| 42 | self.view = mainView; |
| 43 | } |
| 44 | |
| 45 | - (void)applicationWillResignActive:(UIApplication *)application { |
| 46 | // Terminate any calls when we aren't active. |
| 47 | [self dismissViewControllerAnimated:NO completion:nil]; |
| 48 | } |
| 49 | |
| 50 | #pragma mark - ARDMainViewDelegate |
| 51 | |
| 52 | - (void)mainView:(ARDMainView *)mainView didInputRoom:(NSString *)room { |
| 53 | if (!room.length) { |
| 54 | return; |
| 55 | } |
| 56 | // Trim whitespaces. |
| 57 | NSCharacterSet *whitespaceSet = [NSCharacterSet whitespaceCharacterSet]; |
| 58 | NSString *trimmedRoom = [room stringByTrimmingCharactersInSet:whitespaceSet]; |
| 59 | |
| 60 | // Check that room name is valid. |
| 61 | NSError *error = nil; |
| 62 | NSRegularExpressionOptions options = NSRegularExpressionCaseInsensitive; |
| 63 | NSRegularExpression *regex = |
| 64 | [NSRegularExpression regularExpressionWithPattern:@"\\w+" |
| 65 | options:options |
| 66 | error:&error]; |
| 67 | if (error) { |
| 68 | [self showAlertWithMessage:error.localizedDescription]; |
| 69 | return; |
| 70 | } |
| 71 | NSRange matchRange = |
| 72 | [regex rangeOfFirstMatchInString:trimmedRoom |
| 73 | options:0 |
| 74 | range:NSMakeRange(0, trimmedRoom.length)]; |
| 75 | if (matchRange.location == NSNotFound || |
| 76 | matchRange.length != trimmedRoom.length) { |
| 77 | [self showAlertWithMessage:@"Invalid room name."]; |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | // Kick off the video call. |
| 82 | ARDVideoCallViewController *videoCallViewController = |
| 83 | [[ARDVideoCallViewController alloc] initForRoom:trimmedRoom]; |
| 84 | videoCallViewController.modalTransitionStyle = |
| 85 | UIModalTransitionStyleCrossDissolve; |
| 86 | [self presentViewController:videoCallViewController |
| 87 | animated:YES |
| 88 | completion:nil]; |
| 89 | } |
| 90 | |
| 91 | #pragma mark - Private |
| 92 | |
| 93 | - (void)showAlertWithMessage:(NSString*)message { |
| 94 | UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:nil |
| 95 | message:message |
| 96 | delegate:nil |
| 97 | cancelButtonTitle:@"OK" |
| 98 | otherButtonTitles:nil]; |
| 99 | [alertView show]; |
| 100 | } |
| 101 | |
| 102 | @end |