Newer
Older
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
//
// TokenViewController.m
// ISDS
//
// Created by Petr Hruška on 6/30/11.
// Copyright 2011 CZ.NIC, z.s.p.o. All rights reserved.
//
#import "TokenViewController.h"
#import "OtpStatus.h"
#import "DownloadCookie.h"
#import "RequestManager.h"
#define CONTINUE_BUTTON_INDEX 0
#define CANCEL_BUTTON_INDEX 1
@implementation TokenViewController
@synthesize data=_data;
@synthesize cell=_cell;
@synthesize tableView=_tableView;
@synthesize doneButton=_doneButton;
@synthesize editing=_editing;
@synthesize delegate;
- (void)cancelNetworkOperations
{
//TODO remove from queue
}
- (void)dealloc
{
[self cancelNetworkOperations];
[_data release];
[_tableView release];
[_doneButton release];
[_editing release];
[super dealloc];
}
- (void)viewDidUnload
{
self.tableView = nil;
self.doneButton = nil;
[super viewDidUnload];
}
- (TokenCell*)cellAtIndex:(NSUInteger)index
{
return (TokenCell*)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:index]];
}
/* return [self.data count] if the focus should not be set */
- (NSUInteger)initialFocusIndex
{
NSUInteger index = 0;
const NSUInteger count = [self.data count];
while (index < count) {
OtpStatus* status = [self.data objectAtIndex:index];
if (status.status == OtpStatusShouldSendSms) {
return count;
} else if (status.status == OtpStatusEnterToken) {
return index;
}
index++;
}
return index;
}
- (void)viewWillAppear:(BOOL)animated
{
self.doneButton.title = NSLocalizedString(@"Done", nil);
self.navigationItem.rightBarButtonItem = self.doneButton;
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
/* set focus */
NSUInteger index = [self initialFocusIndex];
if (index < [self.data count]) {
TokenCell* cell = [self cellAtIndex:index];
[cell setFocus];
}
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[self cancelNetworkOperations];
[super viewWillAppear:animated];
}
- (NSUInteger)indexFromTask:(OtpTask*)task
{
const NSUInteger count = [self.data count];
for (NSUInteger index = 0; index < count; index++) {
OtpStatus* status = [self.data objectAtIndex:index];
if (status.task == task) return index;
}
return count;
}
- (void)setupAccounts:(NSArray*)accounts
{
self.data = [NSMutableArray arrayWithCapacity:[accounts count]];
for (ISDSAccount* account in accounts) {
if (account.authType != ISDSAccountAuthPass) {
OtpStatus* status = [[OtpStatus alloc] initWithAccount:account];
[self.data addObject:status];
[status release];
}
}
}
- (void)configureCell:(TokenCell*)cell withStatus:(OtpStatus*)status
{
cell.boxName = [status.account boxName];
cell.authType = status.account.authType;
switch (status.status) {
case OtpStatusShouldSendSms:
cell.message = NSLocalizedString(@"click to send sms", nil);
break;
case OtpStatusRequestingSms:
cell.message = NSLocalizedString(@"requesting sms", nil);
break;
case OtpStatusEnterToken:
if (status.errorDescription != nil)
cell.message = status.errorDescription;
else
cell.message = NSLocalizedString(@"enter code", nil);
break;
case OtpStatusRequestingCookie:
cell.message = NSLocalizedString(@"requesting cookie", nil);
break;
case OtpStatusHasCookie:
cell.message = NSLocalizedString(@"account authorized", nil);
break;
}
[cell setStatus:status.status];
}
- (IBAction)sendSms:(id)sender
{/* FIXME
TokenCell* cell = (TokenCell*)sender;
NSUInteger index = cell.tag;
OtpStatus* status = [self.data objectAtIndex:index];
status.connection = [[OtpTask alloc] initWithAccount:status.account delegate:self];
[status.connection release];
*/
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString* reuseIdentifier = @"TokenCell";
TokenCell* cell = (TokenCell*)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"TokenCell" owner:self options:nil];
cell = self.cell;
self.cell = nil;
}
OtpStatus* status = [self.data objectAtIndex:[indexPath section]];
[self configureCell:cell withStatus:status];
[cell setupTag:indexPath.section];
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.data count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (OtpTask*)createTask:(OtpStatus*)status
{
OtpTask* t;
switch (status.account.authType) {
case ISDSAccountAuthHotp:
t = [[DownloadCookie alloc] initWithOtpStatus:status];
t.otpDelegate = self;
[t autorelease];
break;
default:
t = nil;
break;
}
return t;
}
- (void)done
{
[self.delegate tokenViewControllerDone:self];
[self.navigationController popViewControllerAnimated:YES];
}
- (void)downloadCookies
{
NSUInteger taskCount = 0;
for (OtpStatus* status in self.data) {
if ([status tokenReady]) taskCount++;
}
if (!taskCount) {
[self done];
return;
}
NSMutableArray* tasks = [NSMutableArray arrayWithCapacity:taskCount];
for (OtpStatus* status in self.data)
if ([status tokenReady]) {
OtpTask* task = [self createTask:status];
status.task = task;
status.status = OtpStatusRequestingCookie;
[tasks addObject:task];
}
RequestManager* rm = [RequestManager requestManager];
[rm addTasks:tasks withPriority:YES];
[self.tableView reloadData];
}
- (void)showActionSheet
{
UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"Some accounts are missing tokens. Do you with to continue?", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) destructiveButtonTitle:NSLocalizedString(@"Continue", nil) otherButtonTitles: nil];
[actionSheet showInView:self.view];
[actionSheet release];
}
- (void)markMissingUnwanted
{
for (OtpStatus* status in self.data) {
if (![status tokenReady] && status.shouldHaveCookie) status.shouldHaveCookie = FALSE;
}
}
- (IBAction)doneTouched
{
if (self.editing)
[self.editing resignFirstResponder];
NSUInteger missingCount = 0;
for (OtpStatus* status in self.data) {
if (![status tokenReady] && status.shouldHaveCookie) missingCount++;
}
if (missingCount)
[self showActionSheet];
else
[self downloadCookies];
}
- (void)reloadSectionForIndex:(NSUInteger)index
{
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:index] withRowAnimation:UITableViewRowAnimationNone];
}
- (BOOL)hasAllWantedCookies
{
for (OtpStatus* status in self.data)
if (status.status != OtpStatusHasCookie && status.shouldHaveCookie) return NO;
return YES;
}
#pragma mark - OtpTaskDelegate
- (void)taskSuccessfullyDone:(ISDSTask*)task
{
NSUInteger index = [self indexFromTask:(OtpTask*)task];
OtpStatus* status = [self.data objectAtIndex:index];
switch (status.status) {
case OtpStatusRequestingSms:
status.status = OtpStatusEnterToken;
[self reloadSectionForIndex:index];
break;
case OtpStatusRequestingCookie:
status.status = OtpStatusHasCookie;
if ([self hasAllWantedCookies])
[self done];
else
[self reloadSectionForIndex:index];
break;
default:
break;
}
}
- (void)taskFailed:(ISDSTask*)task
{
NSUInteger index = [self indexFromTask:(OtpTask*)task];
if (index == [self.data count]) return;
OtpStatus* status = [self.data objectAtIndex:index];
status.status = OtpStatusEnterToken;
status.errorDescription = task.errorDescription;
[self reloadSectionForIndex:index];
}
#pragma mark - UITextFieldDelegate
- (void)setupTokenFromTextField:(UITextField *)textField
{
NSUInteger index = textField.tag;
OtpStatus* status = [self.data objectAtIndex:index];
status.token = textField.text;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
self.editing = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self setupTokenFromTextField:textField];
self.editing = nil;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return NO;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
if ([textField isFirstResponder])
[textField resignFirstResponder];
else
textField.text = @"";
return NO;
}
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
switch (buttonIndex) {
case CONTINUE_BUTTON_INDEX:
// we will not ask next time
[self markMissingUnwanted];
[self downloadCookies];
break;
case CANCEL_BUTTON_INDEX:
break;
}
}
@end