FreeRDP
CredentialsInputController.m
1 /*
2  Credentials input controller
3 
4  Copyright 2013 Thincast Technologies GmbH, Author: Martin Fleisz
5 
6  This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
7  If a copy of the MPL was not distributed with this file, You can obtain one at
8  http://mozilla.org/MPL/2.0/.
9  */
10 
11 #import "CredentialsInputController.h"
12 #import "RDPSession.h"
13 #import "Utils.h"
14 
15 @implementation CredentialsInputController
16 
17 - (id)initWithNibName:(NSString *)nibNameOrNil
18  bundle:(NSBundle *)nibBundleOrNil
19  session:(RDPSession *)session
20  params:(NSMutableDictionary *)params
21 {
22  self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
23  if (self)
24  {
25  _session = session;
26  _params = params;
27  [self setModalPresentationStyle:UIModalPresentationFormSheet];
28 
29  // on iphone we have the problem that the buttons are hidden by the keyboard
30  // we solve this issue by registering keyboard notification handlers and adjusting the
31  // scrollview accordingly
32  if (IsPhone())
33  {
34  [[NSNotificationCenter defaultCenter] addObserver:self
35  selector:@selector(keyboardWillShow:)
36  name:UIKeyboardWillShowNotification
37  object:nil];
38  [[NSNotificationCenter defaultCenter] addObserver:self
39  selector:@selector(keyboardWillHide:)
40  name:UIKeyboardWillHideNotification
41  object:nil];
42  }
43  }
44  return self;
45 }
46 
47 - (void)viewDidLoad
48 {
49  [super viewDidLoad];
50 
51  // set localized strings
52  [_lbl_message
53  setText:NSLocalizedString(
54  @"Please provide the missing user information in order to proceed and login.",
55  @"Credentials input view message")];
56  [_textfield_username
57  setPlaceholder:NSLocalizedString(@"Username", @"Credentials Input Username hint")];
58  [_textfield_password
59  setPlaceholder:NSLocalizedString(@"Password", @"Credentials Input Password hint")];
60  [_textfield_domain
61  setPlaceholder:NSLocalizedString(@"Domain", @"Credentials Input Domain hint")];
62  [_btn_login setTitle:NSLocalizedString(@"Login", @"Login Button")
63  forState:UIControlStateNormal];
64  [_btn_cancel setTitle:NSLocalizedString(@"Cancel", @"Cancel Button")
65  forState:UIControlStateNormal];
66 
67  // init scrollview content size
68  [_scroll_view setContentSize:[_scroll_view frame].size];
69 
70  // set params in the view
71  [_textfield_username setText:[_params valueForKey:@"username"]];
72  [_textfield_password setText:[_params valueForKey:@"password"]];
73  [_textfield_domain setText:[_params valueForKey:@"domain"]];
74 }
75 
76 - (void)viewDidUnload
77 {
78  [super viewDidUnload];
79 }
80 
81 - (void)viewDidDisappear:(BOOL)animated
82 {
83  [super viewDidDisappear:animated];
84  // set signal
85  [[_session uiRequestCompleted] signal];
86 }
87 
88 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
89 {
90  return YES;
91 }
92 
93 - (void)dealloc
94 {
95  [super dealloc];
96  [[NSNotificationCenter defaultCenter] removeObserver:self];
97 }
98 
99 #pragma mark -
100 #pragma mark iOS Keyboard Notification Handlers
101 
102 - (void)keyboardWillShow:(NSNotification *)notification
103 {
104  CGRect keyboardEndFrame =
105  [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
106  CGRect keyboardFrame = [[self view] convertRect:keyboardEndFrame toView:nil];
107 
108  [UIView beginAnimations:nil context:NULL];
109  [UIView setAnimationCurve:[[[notification userInfo]
110  objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
111  [UIView
112  setAnimationDuration:[[[notification userInfo]
113  objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
114  CGRect frame = [_scroll_view frame];
115  frame.size.height -= keyboardFrame.size.height;
116  [_scroll_view setFrame:frame];
117  [UIView commitAnimations];
118 }
119 
120 - (void)keyboardWillHide:(NSNotification *)notification
121 {
122  CGRect keyboardEndFrame =
123  [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
124  CGRect keyboardFrame = [[self view] convertRect:keyboardEndFrame toView:nil];
125 
126  [UIView beginAnimations:nil context:NULL];
127  [UIView setAnimationCurve:[[[notification userInfo]
128  objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
129  [UIView
130  setAnimationDuration:[[[notification userInfo]
131  objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
132  CGRect frame = [_scroll_view frame];
133  frame.size.height += keyboardFrame.size.height;
134  [_scroll_view setFrame:frame];
135  [UIView commitAnimations];
136 }
137 
138 #pragma mark - Action handlers
139 
140 - (IBAction)loginPressed:(id)sender
141 {
142  // read input back in
143  [_params setValue:[_textfield_username text] forKey:@"username"];
144  [_params setValue:[_textfield_password text] forKey:@"password"];
145  [_params setValue:[_textfield_domain text] forKey:@"domain"];
146  [_params setValue:[NSNumber numberWithBool:YES] forKey:@"result"];
147 
148  // dismiss controller
149  [self dismissModalViewControllerAnimated:YES];
150 }
151 
152 - (IBAction)cancelPressed:(id)sender
153 {
154  [_params setValue:[NSNumber numberWithBool:NO] forKey:@"result"];
155 
156  // dismiss controller
157  [self dismissModalViewControllerAnimated:YES];
158 }
159 
160 @end