1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #if !defined(__has_feature) || !__has_feature(objc_arc)
6 #error "This file requires ARC support."
9 #import "remoting/ios/ui/host_list_view_controller.h"
11 #import "remoting/ios/authorize.h"
12 #import "remoting/ios/host.h"
13 #import "remoting/ios/host_cell.h"
14 #import "remoting/ios/host_refresh.h"
15 #import "remoting/ios/utility.h"
16 #import "remoting/ios/ui/host_view_controller.h"
18 @interface HostListViewController (Private)
19 - (void)refreshHostList;
20 - (void)checkUserAndRefreshHostList;
23 // Callback from [Authorize createLoginController...]
24 - (void)viewController:(UIViewController*)viewController
25 finishedWithAuth:(GTMOAuth2Authentication*)authResult
26 error:(NSError*)error;
29 @implementation HostListViewController
31 @synthesize userEmail = _userEmail;
32 @synthesize authorization = _authorization;
34 // Override default setter
35 - (void)setAuthorization:(GTMOAuth2Authentication*)authorization {
36 _authorization = authorization;
37 if (_authorization.canAuthorize) {
38 _userEmail = _authorization.userEmail;
43 NSString* userName = _userEmail;
45 if (userName == nil) {
46 userName = @"Not logged in";
49 [_btnAccount setTitle:userName forState:UIControlStateNormal];
51 [self refreshHostList];
54 // Override UIViewController
55 // Create google+ service for google authentication and oAuth2 authorization.
59 [_tableHostList setDataSource:self];
60 [_tableHostList setDelegate:self];
62 _versionInfo.title = [Utility appVersionNumberDisplayString];
65 // Override UIViewController
66 - (void)viewWillAppear:(BOOL)animated {
67 [super viewWillAppear:animated];
68 [self.navigationController setNavigationBarHidden:NO animated:NO];
69 [self setAuthorization:[Authorize getAnyExistingAuthorization]];
72 // Override UIViewController
73 // Cancel segue when host status is not online
74 - (BOOL)shouldPerformSegueWithIdentifier:(NSString*)identifier
76 if ([identifier isEqualToString:@"ConnectToHost"]) {
77 Host* host = [self hostAtIndex:[_tableHostList indexPathForCell:sender]];
78 if (![host.status isEqualToString:@"ONLINE"]) {
85 // Override UIViewController
86 // check for segues defined in the storyboard by identifier, and set a few
87 // properties before transitioning
88 - (void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender {
89 if ([segue.identifier isEqualToString:@"ConnectToHost"]) {
90 // the designationViewController type is defined by the storyboard
91 HostViewController* hostView =
92 static_cast<HostViewController*>(segue.destinationViewController);
95 [_authorization.parameters valueForKey:@"access_token"];
97 if (authToken == nil) {
98 authToken = _authorization.authorizationTokenKey;
101 [hostView setHostDetails:[self hostAtIndex:[_tableHostList
102 indexPathForCell:sender]]
104 authorizationToken:authToken];
108 // @protocol HostRefreshDelegate, remember received host list for the table
109 // view to refresh from
110 - (void)hostListRefresh:(NSArray*)hostList
111 errorMessage:(NSString*)errorMessage {
112 if (hostList != nil) {
113 _hostList = hostList;
114 [_tableHostList reloadData];
116 [_refreshActivityIndicator stopAnimating];
117 if (errorMessage != nil) {
118 [Utility showAlert:@"Host Refresh Failed" message:errorMessage];
122 // @protocol UITableViewDataSource
123 // Only have 1 section and it contains all the hosts
124 - (NSInteger)tableView:(UITableView*)tableView
125 numberOfRowsInSection:(NSInteger)section {
126 return [_hostList count];
129 // @protocol UITableViewDataSource
130 // Convert a host entry to a table row
131 - (HostCell*)tableView:(UITableView*)tableView
132 cellForRowAtIndexPath:(NSIndexPath*)indexPath {
133 static NSString* CellIdentifier = @"HostStatusCell";
135 HostCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
136 forIndexPath:indexPath];
138 Host* host = [self hostAtIndex:indexPath];
139 cell.labelHostName.text = host.hostName;
140 cell.labelStatus.text = host.status;
142 UIColor* statColor = nil;
143 if ([host.status isEqualToString:@"ONLINE"]) {
144 statColor = [[UIColor alloc] initWithRed:0 green:1 blue:0 alpha:1];
146 statColor = [[UIColor alloc] initWithRed:1 green:0 blue:0 alpha:1];
148 [cell.labelStatus setTextColor:statColor];
153 // @protocol UITableViewDataSource
154 // Rows are not editable via standard UI mechanisms
155 - (BOOL)tableView:(UITableView*)tableView
156 canEditRowAtIndexPath:(NSIndexPath*)indexPath {
160 - (IBAction)btnRefreshHostListPressed:(id)sender {
161 [self refreshHostList];
164 - (IBAction)btnAccountPressed:(id)sender {
168 - (void)refreshHostList {
169 [_refreshActivityIndicator startAnimating];
170 _hostList = [[NSArray alloc] init];
171 [_tableHostList reloadData];
173 // Insert a small delay so the user is well informed that something is
174 // happening by the animating activity indicator
175 [self performSelector:@selector(checkUserAndRefreshHostList)
180 // Most likely you want to call refreshHostList
181 - (void)checkUserAndRefreshHostList {
182 if (![self isSignedIn]) {
185 HostRefresh* hostRefresh = [[HostRefresh alloc] init];
186 [hostRefresh refreshHostList:_authorization delegate:self];
191 return (_userEmail != nil);
194 // Launch the google.com authentication and authorization process. If a user is
195 // already signed in, begin by signing out so another account could be
198 [self presentViewController:
199 [Authorize createLoginController:self
200 finishedSelector:@selector(viewController:
207 // Callback from [Authorize createLoginController...]
208 // Handle completion of the authentication process, and updates the service
209 // with the new credentials.
210 - (void)viewController:(UIViewController*)viewController
211 finishedWithAuth:(GTMOAuth2Authentication*)authResult
212 error:(NSError*)error {
213 [viewController.presentingViewController dismissViewControllerAnimated:NO
217 [Utility showAlert:@"Authentication Error"
218 message:error.localizedDescription];
219 [self setAuthorization:nil];
221 [self setAuthorization:authResult];
225 - (Host*)hostAtIndex:(NSIndexPath*)indexPath {
226 return [_hostList objectAtIndex:indexPath.row];