/
SettingsController.m
666 lines (560 loc) · 25.8 KB
/
SettingsController.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
//
// SettingsController.m
// MobileOrg
//
// Created by Richard Moreland on 10/6/09.
// Copyright 2009 Richard Moreland.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
#ifndef __GNUC__
#define __asm__ asm
#endif
__asm__(".weak_reference _OBJC_CLASS_$_NSURL");
#import "SettingsController.h"
#import "Settings.h"
#import "DataUtils.h"
#import "GlobalUtils.h"
#import "SearchController.h"
#import "OutlineViewController.h"
#import "SessionManager.h"
#import "MobileOrgAppDelegate.h"
#import "DropboxTransferManager.h"
@implementation SettingsController
@synthesize dropboxPassword;
- (void)onSyncComplete {
[[Settings instance] setLastSync:[NSDate date]];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self setTitle:@"Settings"];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(onSyncComplete)
name:@"SyncComplete"
object:nil];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[self tableView] reloadData];
[[self tableView] setNeedsDisplay];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 5;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *title = nil;
switch (section) {
case 0:
title = NSLocalizedString(@"Server Config", @"Server configuration title");
break;
case 1:
break;
case 2:
title = NSLocalizedString(@"App Info", @"App info title");
break;
case 3:
title = NSLocalizedString(@"Settings", @"App settings");
break;
case 4:
title = NSLocalizedString(@"Credits", @"Credits title");
break;
default:
break;
}
return title;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
switch (section) {
case 0:
return 1;
break;
case 1:
if ([[Settings instance] serverMode] == ServerModeDropbox) {
if ([[DropboxTransferManager instance] isLinked]) {
return 2;
} else {
return 4;
}
} else {
return 3;
}
break;
case 2:
return 2;
break;
case 3:
return 1;
break;
case 4:
return 5;
break;
default:
break;
}
return 0;
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
if (section == 1) {
return @"For help on configuration, visit http://mobileorg.ncogni.to";
} else {
return @"";
}
}
// From Nick @ http://iphoneincubator.com/blog/windows-views/how-to-create-a-data-entry-screen
- (NSArray*)entryFields {
NSMutableArray *ret = [NSMutableArray arrayWithCapacity:3];
NSInteger tag = 1;
UIView *aView;
while (aView = [self.view viewWithTag:tag]) {
if (aView && [[aView class] isSubclassOfClass:[UIResponder class]]) {
[ret addObject:aView];
}
tag++;
}
return ret;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
static NSString *CellIdentifier = @"ServerModeConfigurationSell";
UISegmentedControl *modeSwitch;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
NSMutableArray *options = [[NSMutableArray alloc] initWithCapacity:2];
[options addObject:@"WebDAV"];
[options addObject:@"Dropbox"];
modeSwitch = [[[UISegmentedControl alloc] initWithItems:options] autorelease];
// TODO: Make this resize when the orientation changes
if (IsIpad())
modeSwitch.frame = CGRectMake(44, 0, 680, 48);
else
modeSwitch.frame = CGRectMake(9, 0, 302, 48);
[options release];
[cell addSubview:modeSwitch];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
} else {
modeSwitch = (UISegmentedControl*)[cell.contentView viewWithTag:1];
}
[modeSwitch addTarget:self action:@selector(modeSwitchChanged:) forControlEvents:UIControlEventValueChanged];
[modeSwitch setSelectedSegmentIndex:[[Settings instance] serverMode]-1];
return cell;
} else if (indexPath.section == 1) {
if ([[Settings instance] serverMode] == ServerModeWebDav) {
static NSString *CellIdentifier = @"SettingsConfigurationCell";
UITextField *newLabel;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
if (IsIpad())
newLabel = [[[UITextField alloc] initWithFrame:CGRectMake(130,13,200,25)] autorelease];
else
newLabel = [[[UITextField alloc] initWithFrame:CGRectMake(100,13,200,25)] autorelease];
[newLabel setAdjustsFontSizeToFitWidth:YES];
[newLabel setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleRightMargin];
[newLabel setAutocorrectionType:UITextAutocorrectionTypeNo];
[newLabel setClearButtonMode:UITextFieldViewModeWhileEditing];
[newLabel setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[cell addSubview:newLabel];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
CGRect detailFrame = [[cell detailTextLabel] frame];
detailFrame.origin.y -= 1;
[[cell detailTextLabel] setFrame:detailFrame];
} else {
newLabel = (UITextField*)[cell.contentView viewWithTag:1];
}
// Set up the cell...
switch (indexPath.row) {
case 0:
[newLabel addTarget:self action:@selector(serverUrlChanged:) forControlEvents:(UIControlEventValueChanged | UIControlEventEditingDidEnd)];
[newLabel setKeyboardType:UIKeyboardTypeURL];
[newLabel setDelegate:self];
[newLabel setTag:1];
[[cell textLabel] setText:@"URL"];
newLabel.text = [[[Settings instance] indexUrl] absoluteString];
break;
case 1:
[newLabel addTarget:self action:@selector(usernameChanged:) forControlEvents:(UIControlEventEditingDidEnd | UIControlEventEditingDidEnd)];
[newLabel setDelegate:self];
[newLabel setTag:2];
[[cell textLabel] setText:@"Username"];
newLabel.text = [[Settings instance] username];
break;
case 2:
[newLabel addTarget:self action:@selector(passwordChanged:) forControlEvents:(UIControlEventEditingDidEnd | UIControlEventEditingDidEnd)];
[newLabel setDelegate:self];
[newLabel setTag:3];
[[cell textLabel] setText:@"Password"];
[newLabel setSecureTextEntry:YES];
newLabel.text = [[Settings instance] password];
break;
default:
break;
}
return cell;
} else if ([[Settings instance] serverMode] == ServerModeDropbox) {
if ((![[DropboxTransferManager instance] isLinked] && indexPath.row < 3) || indexPath.row == 0) {
static NSString *CellIdentifier = @"SettingsDropboxConfigurationCell";
UITextField *newLabel;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
if (IsIpad())
newLabel = [[[UITextField alloc] initWithFrame:CGRectMake(130,13,200,25)] autorelease];
else
newLabel = [[[UITextField alloc] initWithFrame:CGRectMake(100,13,200,25)] autorelease];
[newLabel setAdjustsFontSizeToFitWidth:YES];
[newLabel setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleRightMargin];
[newLabel setAutocorrectionType:UITextAutocorrectionTypeNo];
[newLabel setClearButtonMode:UITextFieldViewModeWhileEditing];
[newLabel setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[cell addSubview:newLabel];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
CGRect detailFrame = [[cell detailTextLabel] frame];
detailFrame.origin.y -= 1;
[[cell detailTextLabel] setFrame:detailFrame];
} else {
newLabel = (UITextField*)[cell.contentView viewWithTag:1];
}
// Set up the cell...
switch (indexPath.row) {
case 0:
[newLabel addTarget:self action:@selector(dropboxIndexChanged:) forControlEvents:(UIControlEventValueChanged | UIControlEventEditingDidEnd)];
[newLabel setKeyboardType:UIKeyboardTypeURL];
[newLabel setDelegate:self];
[newLabel setTag:1];
[[cell textLabel] setText:@"Index File"];
newLabel.text = [[Settings instance] dropboxIndex];
break;
case 1:
[newLabel addTarget:self action:@selector(dropboxEmailChanged:) forControlEvents:(UIControlEventEditingDidEnd | UIControlEventEditingDidEnd)];
[newLabel setDelegate:self];
[newLabel setTag:2];
[newLabel setKeyboardType:UIKeyboardTypeEmailAddress];
[[cell textLabel] setText:@"Email"];
newLabel.text = [[Settings instance] dropboxEmail];
break;
case 2:
[newLabel addTarget:self action:@selector(dropboxPasswordChanged:) forControlEvents:(UIControlEventEditingChanged | UIControlEventEditingDidEnd)];
[newLabel setDelegate:self];
[newLabel setTag:3];
[[cell textLabel] setText:@"Password"];
[newLabel setSecureTextEntry:YES];
if ([[DropboxTransferManager instance] isLinked]) {
newLabel.text = @"";
}
break;
default:
break;
}
return cell;
} else {
static NSString *CellIdentifier = @"SettingsDropboxButtonCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
} else {
}
if ([[DropboxTransferManager instance] isLinked]) {
cell.textLabel.text = @"Unlink from Dropbox";
[cell.textLabel setTextColor:[UIColor colorWithRed:0.543 green:0.306 blue:0.435 alpha:1.0]];
} else {
cell.textLabel.text = @"Log in to Dropbox";
[cell.textLabel setTextColor:[UIColor colorWithRed:0.243 green:0.306 blue:0.435 alpha:1.0]];
}
[cell.textLabel setTextAlignment:UITextAlignmentCenter];
return cell;
}
}
} else if (indexPath.section == 2) {
static NSString *CellIdentifier = @"SettingsSimpleCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
[[cell detailTextLabel] setAdjustsFontSizeToFitWidth:YES];
[[cell detailTextLabel] setFont:[UIFont systemFontOfSize:14.0]];
[[cell detailTextLabel] setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleRightMargin];
}
// Set up the cell...
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
if (indexPath.row == 0) {
[[cell textLabel] setText:@"Version"];
#ifdef FOR_APP_STORE
[cell.detailTextLabel setText:[NSString stringWithFormat:@"MobileOrg %@", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]]];
#else
[cell.detailTextLabel setText:[NSString stringWithFormat:@"MobileOrg %@ (build %@)", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"], [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]]];
#endif
} else if (indexPath.row == 1) {
[[cell textLabel] setText:@"Last Sync"];
NSDate *last_sync = [[Settings instance] lastSync];
if (last_sync) {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY-MM-dd EEE HH:mm"];
[cell.detailTextLabel setText:[formatter stringFromDate:last_sync]];
[formatter release];
} else {
[cell.detailTextLabel setText:@"Not yet synced"];
}
}
return cell;
} else if (indexPath.section == 3) {
switch (indexPath.row) {
case 0:
{
static NSString *CellIdentifier = @"SettingsAppBadgeCell";
UISwitch *appBadgeSwitch = nil;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
if (IsIpad())
appBadgeSwitch = [[[UISwitch alloc] initWithFrame:CGRectMake(620,10,200,25)] autorelease];
else
appBadgeSwitch = [[[UISwitch alloc] initWithFrame:CGRectMake(200,10,200,25)] autorelease];
[cell addSubview:appBadgeSwitch];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[[cell textLabel] setFont:[UIFont boldSystemFontOfSize:15.0]];
} else {
appBadgeSwitch = (UISwitch*)[cell.contentView viewWithTag:1];
}
[appBadgeSwitch addTarget:self action:@selector(appBadgeSwitchChanged:) forControlEvents:UIControlEventValueChanged];
[[cell textLabel] setText:@"Show app badge"];
[appBadgeSwitch setOn:([[Settings instance] appBadgeMode] == AppBadgeModeTotal)];
return cell;
}
default:
break;
}
} else if (indexPath.section == 4) {
static NSString *CellIdentifier = @"SettingsCreditsCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
[[cell textLabel] setFont:[UIFont boldSystemFontOfSize:14.0]];
[[cell detailTextLabel] setFont:[UIFont systemFontOfSize:12.0]];
}
// Set up the cell...
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
switch (indexPath.row) {
case 0:
[[cell textLabel] setText:@"Richard Moreland"];
[[cell detailTextLabel] setText:@"Design and development"];
break;
case 1:
[[cell textLabel] setText:@"Carsten Dominik"];
[[cell detailTextLabel] setText:@"Design and Emacs integration"];
break;
case 2:
[[cell textLabel] setText:@"Greg Newman"];
[[cell detailTextLabel] setText:@"Updated app icon"];
break;
case 3:
[[cell textLabel] setText:@"Christophe Bataillon"];
[[cell detailTextLabel] setText:@"Original app icon"];
break;
case 4:
[[cell textLabel] setText:@"Joseph Wain of glyphish.com"];
[[cell detailTextLabel] setText:@"Creative Commons Attribution icons"];
break;
}
return cell;
}
return nil;
}
- (void)resetAppData {
// Session. Clear the saved state
[[SessionManager instance] reset];
// Clear search
[[AppInstance() searchController] reset];
// Delete all nodes
DeleteAllNodes();
// Clear outline view
[[AppInstance() rootOutlineController] reset];
// Get rid of custom todo state, tags, etc
[[Settings instance] resetPrimaryTagsAndTodoStates];
[[Settings instance] resetAllTags];
// Reset last sync time
[[Settings instance] setLastSync:nil];
}
- (void)applyNewServerUrl:(NSString*)url {
// Store the new URL
[[Settings instance] setIndexUrl:[NSURL URLWithString:url]];
[self resetAppData];
}
// This is the callback for when we are asking the user if they want to proceed with changing the URL
// If we use more of these, add some sort of state to the class so we can determine what handler we are.
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
switch(buttonIndex) {
case 0:
urlTextField.text = [[[Settings instance] indexUrl] absoluteString];
urlTextField = nil;
break;
case 1:
[self applyNewServerUrl:pendingNewIndexUrl];
break;
}
}
- (void)serverUrlChanged:(id)sender {
UITextField *textField = (UITextField*)sender;
if ([[textField text] rangeOfRegex:@"http.*\\.(?:org|txt)$"].location == NSNotFound) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Invalid path"
message:@"This setting should be the complete URL to a .org file on a WebDAV server. For instance, http://www.example.com/private/org/index.org"
delegate:nil
cancelButtonTitle:@"Cancel"
otherButtonTitles:nil];
[alert show];
[alert autorelease];
}
if (![[textField text] isEqualToString:[[[Settings instance] indexUrl] absoluteString]]) {
if ([[textField text] length] > 0) {
// The user just changed URLs. Let's see if they had any local changes.
// We need to warn them that that the changes they have made will likely
// not apply to the new data.
if (CountLocalEditActions() > 0) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Proceed with Change?"
message:@"Changing the URL to another set of files may invalidate the local changes you have made. You may want to sync with the old URL first instead.\n\nProceed to change URL?"
delegate:self
cancelButtonTitle:@"No"
otherButtonTitles:@"Yes", nil];
[alert show];
[alert autorelease];
[pendingNewIndexUrl release];
pendingNewIndexUrl = [textField.text copy];
urlTextField = textField;
return;
}
}
[self applyNewServerUrl:textField.text];
}
}
- (void)usernameChanged:(id)sender {
UITextField *textField = (UITextField*)sender;
[[Settings instance] setUsername:textField.text];
}
- (void)passwordChanged:(id)sender {
UITextField *textField = (UITextField*)sender;
[[Settings instance] setPassword:textField.text];
}
- (void)dropboxIndexChanged:(id)sender {
UITextField *textField = (UITextField*)sender;
[[Settings instance] setDropboxIndex:textField.text];
}
- (void)dropboxEmailChanged:(id)sender {
UITextField *textField = (UITextField*)sender;
[[Settings instance] setDropboxEmail:textField.text];
}
- (void)dropboxPasswordChanged:(id)sender {
self.dropboxPassword = [sender text];
}
- (void)appBadgeSwitchChanged:(id)sender {
UISwitch *appBadgeSwitch = (UISwitch*)sender;
if ([appBadgeSwitch isOn]) {
[[Settings instance] setAppBadgeMode:AppBadgeModeTotal];
} else {
[[Settings instance] setAppBadgeMode:AppBadgeModeNone];
}
}
- (void)modeSwitchChanged:(id)sender {
UISegmentedControl *modeSwitch = (UISegmentedControl*)sender;
if ([[Settings instance] serverMode] != (1 + [modeSwitch selectedSegmentIndex])) {
[[Settings instance] setServerMode:(1 + [modeSwitch selectedSegmentIndex])];
[self resetAppData];
}
[[self tableView] reloadData];
[[self tableView] setNeedsDisplay];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 46;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
// [self.navigationController pushViewController:anotherViewController];
// [anotherViewController release];
if (indexPath.section == 1 && [[Settings instance] serverMode] == ServerModeDropbox) {
bool linked = [[DropboxTransferManager instance] isLinked];
if ((!linked && indexPath.row == 3) || (linked && indexPath.row == 1)) {
if (dropboxLoggingIn) {
[[[self tableView] cellForRowAtIndexPath:indexPath] setSelected:NO animated:YES];
return;
}
[[[self tableView] cellForRowAtIndexPath:indexPath] setSelected:YES animated:YES];
if ([[DropboxTransferManager instance] isLinked]) {
[[DropboxTransferManager instance] unlink];
[[self tableView] reloadData];
[[self tableView] setNeedsDisplay];
} else {
[[DropboxTransferManager instance] setLoginDelegate:self];
[[DropboxTransferManager instance] login:[[Settings instance] dropboxEmail] andPassword:dropboxPassword];
dropboxLoggingIn = true;
[[self tableView] cellForRowAtIndexPath:indexPath].textLabel.text = @"Logging in...";
}
[[[self tableView] cellForRowAtIndexPath:indexPath] setSelected:NO animated:YES];
}
}
}
- (void)loginSuccess {
dropboxLoggingIn = false;
[[self tableView] reloadData];
[[self tableView] setNeedsDisplay];
}
- (void)loginFailedWithError:(NSString*)message {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Dropbox Error"
message:message
delegate:nil
cancelButtonTitle:@"Cancel"
otherButtonTitles:nil];
[alert show];
[alert autorelease];
dropboxLoggingIn = false;
[[self tableView] reloadData];
[[self tableView] setNeedsDisplay];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self forKeyPath:@"SyncComplete"];
[pendingNewIndexUrl release];
[dropboxPassword release];
[super dealloc];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
bool resign = true;
// Find the next entry field
for (UIView *view in [self entryFields]) {
if (view.tag == (textField.tag + 1)) {
[view becomeFirstResponder];
resign = false;
break;
}
}
if (resign)
[textField resignFirstResponder];
return YES;
}
@end