forked from supersaiyen/iSynergyClient
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathConfigurationViewController.m
executable file
·367 lines (318 loc) · 11.8 KB
/
ConfigurationViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
* Copyright (C) 2009 by Matthias Ringwald
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#import "ConfigurationViewController.h"
#import "SynergyClient.h"
#import "BackgroundApplication.h"
@implementation ConfigurationViewController
@synthesize synergyClient;
@synthesize serverAddress;
@synthesize clientName;
@synthesize activeSwitch;
@synthesize homeButtonSwitch;
@synthesize activityIndicator;
@synthesize autoConnectOnLoad;
NSString *keyAutoConnect = @"AutoConnect";
#pragma mark -
#pragma mark Initialization
/*
- (id)initWithStyle:(UITableViewStyle)style {
// Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
if ((self = [super initWithStyle:style])) {
}
return self;
}
*/
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
NSLog(@"WillRotate!!!!");
}
// Pickup changes to settings outside of app
- (void)defaultsChanged:(NSNotification *)notification
{
[self.tableView reloadData];
// CLIENT_STATE state = [synergyClient clientState];
// if (state == STATE_NOT_CONNECTED) {
// //reload table to show changed data
// //only if we're not connected
// [self.tableView reloadData];
// }
}
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(defaultsChanged:) name:NSUserDefaultsDidChangeNotification object:Nil];
}
- (void)viewDidUnload {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewWillDisappear:(BOOL)animated {
NSLog(@"viewWillDisappear...");
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return YES;
}
//iOS 6 Orientation fix.
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskAll;
}
-(BOOL)shouldAutorotate{
return YES;
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 2;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
static NSString * titles[] = { @"Server Address", @"Client Name", @"Special Keys", @"Activation", @"Status", @"Keyboard Support"};
return titles[section];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = @"Cell";
if ([indexPath row] == 1) {
cellIdentifier = @"Explanation";
} else {
cellIdentifier = [NSString stringWithFormat:@"Field-%u", [indexPath indexAtPosition:0]];
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
CGRect textFrame;
// Configure the cell...
int inset = 7;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
UITextField * textField;
if ([indexPath row] == 0){
switch ([indexPath section]){
case 0:
case 1:
textFrame = CGRectMake(inset, inset, cell.contentView.bounds.size.width-(2*inset),
cell.contentView.bounds.size.height-(2*inset));
textField = [[UITextField alloc] initWithFrame:textFrame];
textField.borderStyle = UITextBorderStyleBezel;
textField.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
textField.returnKeyType = UIReturnKeyDone;
textField.delegate = self;
[cell.contentView addSubview:textField];
if ([indexPath indexAtPosition:0] == 0){
textField.text = [synergyClient serverAddress];
serverAddress = textField;
} else {
textField.text = [synergyClient clientName];
textField.placeholder = [synergyClient hostName];
clientName = textField;
}
break;
case 2:
cell.textLabel.text = @"Home Button Hot Key";
homeButtonSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
homeButtonSwitch.on = [synergyClient homeButtonHotkey];
cell.accessoryView = homeButtonSwitch;
break;
case 3:
cell.textLabel.text = @"Mouse";
activeSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
activeSwitch.on = [synergyClient enabled];
[activeSwitch addTarget:self action:@selector(switchToggled) forControlEvents:UIControlEventValueChanged];
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
cell.accessoryView = activeSwitch;
break;
case 4:
cell.textLabel.text = @"Purchase BTstack Keyboard from Cydia Store";
// keyboard support installed?
// click goes to cydia store
break;
}
}
}
if ([indexPath row] == 0){
switch ([indexPath section]){
case 0:
serverAddress.text = [synergyClient serverAddress];
break;
case 1:
clientName.text = [synergyClient clientName];
break;
case 2:
homeButtonSwitch.on = [synergyClient homeButtonHotkey];
break;
case 3:
break;
case 4:
break;
}
}
if ([indexPath indexAtPosition:1] == 1){
NSString * helpTexts[] = {
@"IP address of Synergy server, either as dot notation like 192.168.3.2 or server.home.com",
@"Client name as configured on server, leave empty for local hostname",
@"Use Command + Shift + H to simulate pressing the Home button?",
@"Start/stop connection to server",
@"Current status, indicates if connection was established successfully",
@"To enter text, the BTstack Keyboard package is required"
};
if ([indexPath indexAtPosition:0] == 3) {
cell.textLabel.text = [NSString stringWithFormat:@"Current status: %@", [synergyClient connectionStatus]];
CLIENT_STATE state = [synergyClient clientState];
switch (state) {
case STATE_CONNECTED:
case STATE_LIVE:
cell.textLabel.textColor = [UIColor greenColor];
cell.accessoryView = nil;
[activityIndicator stopAnimating];
break;
case STATE_CONNECTING:
cell.textLabel.textColor = [UIColor redColor];
cell.accessoryView = activityIndicator;
[activityIndicator startAnimating];
break;
default:
cell.textLabel.textColor = [UIColor redColor];
cell.accessoryView = nil;
[activityIndicator stopAnimating];
break;
}
} else {
cell.textLabel.text = helpTexts[[indexPath indexAtPosition:0]];
}
cell.textLabel.font = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if ([indexPath indexAtPosition:1] == 1){
return 30;
} else {
return tableView.rowHeight;
}
}
#pragma mark TextField delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return NO;
}
#pragma mark -
#pragma mark Table view delegate
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// if ( [indexPath indexAtPosition:1] == 1) {
// return nil;
//}
//return indexPath;
return nil;
}
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// // Navigation logic may go here. Create and push another view controller.
// /*
// <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// // ...
// // Pass the selected object to the new view controller.
// [self.navigationController pushViewController:detailViewController animated:YES];
// [detailViewController release];
// */
// [tableView deselectRowAtIndexPath:indexPath animated:NO];
//}
- (void) updateDefaultsFromView {
[synergyClient setClientName:clientName.text];
[synergyClient setServerAddress:serverAddress.text];
[synergyClient setHomeButtonHotKey:homeButtonSwitch.on];
[synergyClient setEnabled:activeSwitch.on];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)connectionStateChanged {
// NSLog(@"Connection changed %@, %u", [synergyClient connectionStatus], [synergyClient clientState]);
[self.tableView reloadData];
}
- (void)updateAfterDisconnect {
[BackgroundApplication setRunInBackground:NO];
serverAddress.enabled = YES;
clientName.enabled = YES;
homeButtonSwitch.enabled = YES;
serverAddress.borderStyle = UITextBorderStyleBezel;
clientName.borderStyle = UITextBorderStyleBezel;
}
- (void) remoteDisconnected {
activeSwitch.on = NO;
[self.tableView reloadData];
[self updateAfterDisconnect];
}
- (void) connectionFailed{
// get error
NSString *error = [synergyClient connectionError];
// NSLog(@"Error: %@", error);
UIAlertView * connectionErrorAlert = [[UIAlertView alloc] initWithTitle:@"Connection Error" message:error delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles:nil];
[connectionErrorAlert show];
[self remoteDisconnected];
}
-(void) switchToggled{
// NSLog(@"Switch toggled!");
[self updateDefaultsFromView];
if (activeSwitch.on){
serverAddress.borderStyle = 0;
clientName.borderStyle = 0;
serverAddress.enabled = NO;
clientName.enabled = NO;
homeButtonSwitch.enabled = NO;
[self.tableView reloadData];
[BackgroundApplication setRunInBackground:YES];
[NSThread detachNewThreadSelector:@selector(startOpeningConnection) toTarget:synergyClient withObject:nil];
} else {
[synergyClient stopConnection];
[self updateAfterDisconnect];
}
}
- (BOOL) shouldAutoConnect {
return [[NSUserDefaults standardUserDefaults] boolForKey:keyAutoConnect];
}
- (void) viewDidAppear:(BOOL)animated {
// toggle switch and launch connection if auto connect key is set
if ([self shouldAutoConnect]) {
[[self activeSwitch] setOn:YES animated:NO];
[self switchToggled];
}
[self.tableView reloadData];
}
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc that aren't in use.
}
@end