root/apps/iphone/my.tel/trunk/Classes/ProfileRecordsViewController.m
@
657
| Revision 651, 27.9 kB (checked in by henri, 3 years ago) | |
|---|---|
|
|
| Line | |
|---|---|
| 1 | // |
| 2 | // ProfileRecordsViewController.m |
| 3 | // My.tel |
| 4 | // |
| 5 | // Created by Henri Asseily on 11/18/08. |
| 6 | // Copyright 2008 Telnic Ltd.. All rights reserved. |
| 7 | // |
| 8 | |
| 9 | #import "ProfileRecordsViewController.h" |
| 10 | |
| 11 | #ifndef kRowHeight |
| 12 | #define kRowHeight 52.0 |
| 13 | #endif |
| 14 | |
| 15 | #define kNINFOMaxLength 255 // NINFO max length |
| 16 | |
| 17 | @interface ProfileRecordsViewController (PrivateMethods) |
| 18 | - (void)setDidPreload:(BOOL)preload; |
| 19 | @end |
| 20 | |
| 21 | @implementation ProfileRecordsViewController |
| 22 | |
| 23 | @synthesize skeysDict; |
| 24 | @synthesize liDict; |
| 25 | |
| 26 | @synthesize theProfile; |
| 27 | @synthesize ninfoApiId; |
| 28 | @synthesize recordsArray; |
| 29 | @synthesize uiArray; |
| 30 | |
| 31 | @synthesize showDisabledRecords; |
| 32 | |
| 33 | @synthesize tableHeaderView; |
| 34 | @synthesize ninfoCell; |
| 35 | @synthesize ninfoLabel; |
| 36 | |
| 37 | @synthesize delegate; |
| 38 | |
| 39 | #pragma mark - |
| 40 | #pragma mark Designated initializer |
| 41 | |
| 42 | + (ProfileRecordsViewController *)controllerWithProfile:(NSDictionary *)aProfile |
| 43 | delegate:(id <TelControllerDelegate>)aDelegate |
| 44 | preload:(BOOL)preload { |
| 45 | ProfileRecordsViewController *theC = [[[ProfileRecordsViewController alloc] initWithNibName:@"ProfileRecords" bundle:nil] autorelease]; |
| 46 | theC.delegate = aDelegate; |
| 47 | theC.skeysDict = [NSMutableDictionary dictionaryWithContentsOfFile: |
| 48 | [[NSBundle mainBundle] pathForResource:@"ServiceTypes" ofType:@"plist"]]; |
| 49 | theC.liDict = [NSMutableDictionary dictionaryWithContentsOfFile: |
| 50 | [[NSBundle mainBundle] pathForResource:@"LocationIndicators" ofType:@"plist"]]; |
| 51 | theC.recordsArray = [NSMutableArray arrayWithCapacity:20]; |
| 52 | theC.uiArray = [NSMutableArray arrayWithCapacity:20]; |
| 53 | theC.theProfile = aProfile; |
| 54 | theC.showDisabledRecords = NO; |
| 55 | |
| 56 | if (preload) { |
| 57 | [theC getRecords]; |
| 58 | [theC getTextHeader]; |
| 59 | } |
| 60 | [theC setDidPreload:preload]; |
| 61 | return theC; |
| 62 | } |
| 63 | |
| 64 | - (void)setDidPreload:(BOOL)preload { |
| 65 | didPreload = preload; |
| 66 | } |
| 67 | |
| 68 | #pragma mark ------ Standard View Controller Methods |
| 69 | |
| 70 | - (void)viewDidLoad { |
| 71 | [super viewDidLoad]; |
| 72 | self.title = [self.theProfile objectForKey:@"name"]; |
| 73 | self.navigationItem.rightBarButtonItem = self.editButtonItem; |
| 74 | recordTableHeight = kRowHeight; |
| 75 | } |
| 76 | |
| 77 | - (void)viewWillAppear:(BOOL)animated { |
| 78 | [super viewWillAppear:animated]; |
| 79 | if (didPreload) { |
| 80 | // Don't get the data if we preloaded |
| 81 | [self.tableView reloadData]; |
| 82 | } else { |
| 83 | [self getRecords]; |
| 84 | [self getTextHeader]; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | - (void)dealloc { |
| 89 | self.skeysDict = nil; |
| 90 | self.liDict = nil; |
| 91 | self.ninfoApiId = nil; |
| 92 | self.theProfile = nil; |
| 93 | self.recordsArray = nil; |
| 94 | self.uiArray = nil; |
| 95 | [super dealloc]; |
| 96 | } |
| 97 | |
| 98 | #pragma mark ------ TableView Delegate and DataSource Methods |
| 99 | |
| 100 | - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { |
| 101 | return kKProfileTableViewSectionsCount; |
| 102 | } |
| 103 | |
| 104 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
| 105 | if (section == kKProfileTableViewSectionRecords) { |
| 106 | return [uiArray count]; |
| 107 | } |
| 108 | if (! self.editing) { |
| 109 | if (section == kKProfileTableViewSectionRename) |
| 110 | return 0; |
| 111 | if (section == kKProfileTableViewSectionDelete) |
| 112 | return 0; |
| 113 | } |
| 114 | return 1; |
| 115 | } |
| 116 | |
| 117 | - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { |
| 118 | if (section == kKProfileTableViewSectionNINFO) { |
| 119 | return @"Header Status"; |
| 120 | } else if (section == kKProfileTableViewSectionRecords) { |
| 121 | return @"Contact items"; |
| 122 | } |
| 123 | return nil; |
| 124 | } |
| 125 | |
| 126 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
| 127 | static NSString *CellIdentifier = @"RecordCellIdentifier"; |
| 128 | static NSString *ButtonCellIdentifier = @"ButtonCellIdentifier"; |
| 129 | |
| 130 | if (indexPath.section == kKProfileTableViewSectionNINFO) { |
| 131 | return self.ninfoCell; |
| 132 | } |
| 133 | |
| 134 | if (indexPath.section == kKProfileTableViewSectionRecords) { |
| 135 | // UI Item keys: |
| 136 | // apiId (string), terminal (bool), enabled (bool), imageName (string), |
| 137 | // private (bool), service (string), label (string), value (string) |
| 138 | |
| 139 | RecordCell *cell; |
| 140 | cell = (RecordCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
| 141 | if (cell == nil) { |
| 142 | cell = [[[RecordCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease]; |
| 143 | cell.canBeDisabled = TRUE; // When handling profiles, allow enable/disable |
| 144 | cell.accessoryType = UITableViewCellAccessoryNone; |
| 145 | cell.shouldIndentWhileEditing = NO; |
| 146 | } |
| 147 | NSMutableDictionary *uiItem = [[uiArray objectAtIndex:indexPath.row] retain]; |
| 148 | [cell.labelService setText:[NSString stringWithFormat:@"%@", [uiItem objectForKey:@"service"]]]; |
| 149 | [cell.labelLabel setText:[NSString stringWithFormat:@"%@", [uiItem objectForKey:@"longLabel"]]]; |
| 150 | [cell.labelValue setText:[NSString stringWithFormat:@"%@", [uiItem objectForKey:@"value"]]]; |
| 151 | |
| 152 | // Set the icon image |
| 153 | [cell.labelImage setImage:[UIImage imageNamed:(NSString *)[uiItem objectForKey:@"imageName"]]]; |
| 154 | |
| 155 | cell.enabledInProfile = [[uiItem valueForKey:@"enabled"] boolValue]; |
| 156 | if (cell.enabledInProfile) { |
| 157 | cell.editingAccessoryType = UITableViewCellAccessoryCheckmark; |
| 158 | } else { |
| 159 | cell.editingAccessoryType = UITableViewCellAccessoryNone; |
| 160 | } |
| 161 | |
| 162 | if (tableView.editing) { |
| 163 | cell.selectionStyle = UITableViewCellSelectionStyleBlue; |
| 164 | } else { |
| 165 | cell.selectionStyle = UITableViewCellSelectionStyleNone; |
| 166 | } |
| 167 | |
| 168 | [uiItem release]; |
| 169 | return cell; |
| 170 | } |
| 171 | |
| 172 | // Manage the button cells |
| 173 | UITableViewCell *cell = nil; |
| 174 | switch (indexPath.section) { |
| 175 | case kKProfileTableViewSectionActivate: |
| 176 | cell = [tableView dequeueReusableCellWithIdentifier:ButtonCellIdentifier]; |
| 177 | if (cell == nil) { |
| 178 | cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault |
| 179 | reuseIdentifier:ButtonCellIdentifier] autorelease]; |
| 180 | cell.accessoryType = cell.editingAccessoryType = UITableViewCellAccessoryNone; |
| 181 | } |
| 182 | if ([[self.theProfile valueForKey:@"isActive"] integerValue] == 1) { |
| 183 | cell.textLabel.textColor = [UIColor darkGrayColor]; |
| 184 | cell.textLabel.textAlignment = UITextAlignmentCenter; |
| 185 | cell.textLabel.text = @"Currently Active Profile"; |
| 186 | } else { |
| 187 | cell.textLabel.textColor = [UIColor blueColor]; |
| 188 | cell.textLabel.textAlignment = UITextAlignmentCenter; |
| 189 | cell.textLabel.text = @"Activate Profile"; |
| 190 | } |
| 191 | break; |
| 192 | case kKProfileTableViewSectionRename: |
| 193 | cell = [tableView dequeueReusableCellWithIdentifier:ButtonCellIdentifier]; |
| 194 | if (cell == nil) { |
| 195 | cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault |
| 196 | reuseIdentifier:ButtonCellIdentifier] autorelease]; |
| 197 | cell.accessoryType = cell.editingAccessoryType = UITableViewCellAccessoryNone; |
| 198 | } |
| 199 | cell.textLabel.textColor = [UIColor blueColor]; |
| 200 | cell.textLabel.textAlignment = UITextAlignmentCenter; |
| 201 | cell.textLabel.text = @"Rename Profile"; |
| 202 | break; |
| 203 | case kKProfileTableViewSectionDelete: |
| 204 | cell = [tableView dequeueReusableCellWithIdentifier:ButtonCellIdentifier]; |
| 205 | if (cell == nil) { |
| 206 | cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault |
| 207 | reuseIdentifier:ButtonCellIdentifier] autorelease]; |
| 208 | cell.accessoryType = cell.editingAccessoryType = UITableViewCellAccessoryNone; |
| 209 | } |
| 210 | cell.textLabel.textColor = [UIColor redColor]; |
| 211 | cell.textLabel.textAlignment = UITextAlignmentCenter; |
| 212 | cell.textLabel.text = @"Delete Profile"; |
| 213 | break; |
| 214 | } |
| 215 | return cell; |
| 216 | |
| 217 | } |
| 218 | |
| 219 | - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
| 220 | // we only want to allow selection for the profile records when editing |
| 221 | if (indexPath.section == kKProfileTableViewSectionRecords) { |
| 222 | if (! self.editing) { |
| 223 | return nil; |
| 224 | } |
| 225 | } |
| 226 | // don't allow selection of the activation row if the profile is the active profile |
| 227 | if (indexPath.section == kKProfileTableViewSectionActivate) { |
| 228 | if ([[self.theProfile valueForKey:@"isActive"] integerValue] == 1) { |
| 229 | return nil; |
| 230 | } |
| 231 | } |
| 232 | return indexPath; |
| 233 | } |
| 234 | |
| 235 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
| 236 | [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
| 237 | if (indexPath.section == kKProfileTableViewSectionNINFO) { |
| 238 | NSString *ninfoEditTitle = @"Header Status"; |
| 239 | NSString *ninfoPrompt = nil; |
| 240 | HATextViewEditController *teC = [HATextViewEditController controllerWithDelegate:self |
| 241 | title:ninfoEditTitle |
| 242 | prompt:ninfoPrompt]; |
| 243 | teC.textContent = self.ninfoLabel.text; |
| 244 | teC.maxCharSize = kNINFOMaxLength; |
| 245 | teC.returnKeyType = UIReturnKeyDefault; |
| 246 | [self.navigationController pushViewController:teC animated:YES]; |
| 247 | } else if (indexPath.section == kKProfileTableViewSectionRecords) { |
| 248 | NSMutableDictionary *uiItem = [[uiArray objectAtIndex:indexPath.row] retain]; |
| 249 | if ([[uiItem valueForKey:@"enabled"] integerValue] == 0) { |
| 250 | [uiItem setValue:@"1" forKey:@"enabled"]; |
| 251 | #ifdef DEBUG |
| 252 | NSLog(@"%@", [uiItem description]); |
| 253 | #endif |
| 254 | } else { |
| 255 | [uiItem setValue:@"0" forKey:@"enabled"]; |
| 256 | } |
| 257 | [uiItem release]; |
| 258 | [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; |
| 259 | } else if (indexPath.section == kKProfileTableViewSectionActivate) { |
| 260 | [self didClickActivate:nil]; |
| 261 | [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]; |
| 262 | } else if (indexPath.section == kKProfileTableViewSectionRename) { |
| 263 | [self didClickRename:nil]; |
| 264 | } else if (indexPath.section == kKProfileTableViewSectionDelete) { |
| 265 | [self.delegate objectWillGetDeletedInController:self]; |
| 266 | [self.navigationController popViewControllerAnimated:YES]; |
| 267 | return; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | - (void)setEditing:(BOOL)editing animated:(BOOL)animated { |
| 272 | //Toggles the receiver into and out of editing mode. |
| 273 | [super setEditing:editing animated:animated]; |
| 274 | self.showDisabledRecords = editing; |
| 275 | if (editing) { |
| 276 | [self displayDisabledRecords:YES]; |
| 277 | buttonEditCount = 2; |
| 278 | self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel |
| 279 | target:self |
| 280 | action:@selector(didClickCancel:)] |
| 281 | autorelease]; |
| 282 | } else { |
| 283 | self.navigationItem.leftBarButtonItem = nil; |
| 284 | [self enableRecords]; |
| 285 | [self getRecords]; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { |
| 290 | return UITableViewCellEditingStyleNone; |
| 291 | } |
| 292 | |
| 293 | - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { |
| 294 | // Disable swipe-to-delete: don't allow row edit if table not in edit mode |
| 295 | if (indexPath.section == kKProfileTableViewSectionRecords) |
| 296 | return self.editing; |
| 297 | return NO; |
| 298 | } |
| 299 | |
| 300 | - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { |
| 301 | return NO; |
| 302 | } |
| 303 | |
| 304 | - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
| 305 | switch (indexPath.section) { |
| 306 | case kKProfileTableViewSectionNINFO: |
| 307 | return self.ninfoCell.frame.size.height; |
| 308 | break; |
| 309 | case kKProfileTableViewSectionRecords: |
| 310 | return recordTableHeight; |
| 311 | default: |
| 312 | return self.tableView.rowHeight; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | - (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath { |
| 317 | return NO; |
| 318 | } |
| 319 | |
| 320 | #pragma mark ------ UI Methods and Json Delegates |
| 321 | |
| 322 | - (IBAction)didClickCancel:(id)sender { |
| 323 | self.navigationItem.leftBarButtonItem = nil; |
| 324 | self.editing = NO; |
| 325 | [self displayDisabledRecords:NO]; |
| 326 | } |
| 327 | |
| 328 | - (IBAction)didClickActivate:(id)sender { |
| 329 | [self activateProfile]; |
| 330 | } |
| 331 | |
| 332 | - (IBAction)didClickRename:(id)sender { |
| 333 | AlertRenameView *alert = [[AlertRenameView alloc] initWithTitle:@"Edit Profile Name" |
| 334 | message:@"\n\n" |
| 335 | delegate:self |
| 336 | cancelButtonTitle:@"Cancel" |
| 337 | otherButtonTitles:@"OK", nil]; |
| 338 | alert.alertId = [NSNumber numberWithInt:0]; |
| 339 | alert.uiStringField.text = [self.theProfile objectForKey:@"name"]; |
| 340 | [alert show]; |
| 341 | [alert release]; |
| 342 | } |
| 343 | |
| 344 | - (void)updateEditButtonAndDoNothing:(NSDictionary *)parsedJson { |
| 345 | // hack to ensure: |
| 346 | // 1- that all enable-disable actions are done in serial order |
| 347 | // 2- that the user cannot re-edit the table before all is done |
| 348 | // call enableRecords, and all goes from there automatically |
| 349 | if (parsedJson) { |
| 350 | [[MyTelConnect sharedInstance] doNothing:parsedJson]; |
| 351 | } |
| 352 | switch (buttonEditCount) { |
| 353 | case 2: // This case should be skipped if you start by calling enableRecords |
| 354 | [self enableRecords]; |
| 355 | break; |
| 356 | case 1: |
| 357 | [self disableRecords]; |
| 358 | break; |
| 359 | case 0: |
| 360 | self.navigationItem.rightBarButtonItem = self.editButtonItem; |
| 361 | break; |
| 362 | default: |
| 363 | buttonEditCount = 0; |
| 364 | self.navigationItem.rightBarButtonItem = self.editButtonItem; |
| 365 | break; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | - (void)displayDisabledRecords:(BOOL)showDisabled { |
| 370 | if (showDisabled) { |
| 371 | if ([self.uiArray count] == [self.recordsArray count]) return; |
| 372 | [self.uiArray removeAllObjects]; |
| 373 | for (NSDictionary *aRec in self.recordsArray) { |
| 374 | [self.uiArray addObject:[self uiItemFromJsonItem:aRec]]; |
| 375 | } |
| 376 | } else { |
| 377 | if ([self.uiArray count] < [self.recordsArray count]) return; |
| 378 | NSArray *tempUiArray = [NSArray arrayWithArray:self.uiArray]; |
| 379 | for (NSDictionary *theRow in tempUiArray) { |
| 380 | if ([[theRow valueForKey:@"enabled"] integerValue] == 0) { |
| 381 | [self.uiArray removeObject:theRow]; |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | // Reload all relevant sections |
| 386 | NSMutableIndexSet *sectionsToReload = [[[NSMutableIndexSet alloc] init] autorelease]; |
| 387 | [sectionsToReload addIndex:kKProfileTableViewSectionRecords]; |
| 388 | [sectionsToReload addIndex:kKProfileTableViewSectionRename]; |
| 389 | [sectionsToReload addIndex:kKProfileTableViewSectionDelete]; |
| 390 | [self.tableView reloadSections:sectionsToReload |
| 391 | withRowAnimation:UITableViewRowAnimationFade]; |
| 392 | } |
| 393 | |
| 394 | - (void)updateUIHeaderWithJson:(NSDictionary *)parsedJson { |
| 395 | #ifdef DEBUG |
| 396 | NSLog(@"%@", [parsedJson descriptionInStringsFileFormat]); |
| 397 | #endif |
| 398 | |
| 399 | if ([[parsedJson valueForKey:@"success"] integerValue] == 1) { |
| 400 | self.ninfoApiId = [parsedJson valueForKey:@"apiId"]; |
| 401 | self.ninfoLabel.text = (NSString *)[parsedJson valueForKey:@"text"]; |
| 402 | } else { |
| 403 | self.ninfoApiId = @"0"; |
| 404 | self.ninfoLabel.text = @""; |
| 405 | NSString *errString = [[(NSArray *)[parsedJson valueForKey:@"actionErrors"] objectAtIndex:0] description]; |
| 406 | if ([errString hasPrefix:@"The current profile has no text header set"]) { |
| 407 | // Bypass this error, it's not useful to show to the user |
| 408 | return; |
| 409 | } |
| 410 | [TextHeader throwJsonErrorAlert:parsedJson]; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | - (void)updateUITableWithJson:(NSDictionary *)parsedJson { |
| 415 | #ifdef DEBUG |
| 416 | NSLog(@"%@", [parsedJson descriptionInStringsFileFormat]); |
| 417 | #endif |
| 418 | |
| 419 | [self.recordsArray removeAllObjects]; |
| 420 | [self.uiArray removeAllObjects]; |
| 421 | if ([[parsedJson valueForKey:@"success"] integerValue] == 1) { |
| 422 | [self.recordsArray setArray:(NSArray *)[parsedJson valueForKey:@"recordList"]]; |
| 423 | [self.delegate dataDidChangeInController:self]; |
| 424 | } else { |
| 425 | [Record throwJsonErrorAlert:parsedJson]; |
| 426 | } |
| 427 | for (NSDictionary *aRec in self.recordsArray) { |
| 428 | [self.uiArray addObject:[self uiItemFromJsonItem:aRec]]; |
| 429 | } |
| 430 | [self displayDisabledRecords:self.showDisabledRecords]; |
| 431 | } |
| 432 | |
| 433 | - (NSMutableDictionary *)uiItemFromJsonItem:(NSDictionary *)jsonItem { |
| 434 | // Method that creates a dictionary to display in a cell row |
| 435 | // Sample jsonItem: |
| 436 | // {apiId: 23, terminal: true, |
| 437 | // serviceKeys: ["voice", "fax"], value: "+34.34343443", |
| 438 | // label: "my home number", groups: [1, 3], |
| 439 | // profiles: [12, 23], global: false, |
| 440 | // locations: ["x-home", "x-mobile"], editable: true} |
| 441 | #ifdef DEBUG |
| 442 | NSLog(@"Naptr Rec in JSON: %@", [jsonItem descriptionInStringsFileFormat]); |
| 443 | #endif |
| 444 | |
| 445 | NSMutableDictionary *uiItem; |
| 446 | if (!jsonItem) { |
| 447 | return NULL; |
| 448 | } |
| 449 | uiItem = [NSMutableDictionary dictionaryWithCapacity:7]; |
| 450 | |
| 451 | // set apiId |
| 452 | [uiItem setObject:(NSString *)[jsonItem objectForKey:@"apiId"] forKey:@"apiId"]; |
| 453 | |
| 454 | // set naptr value |
| 455 | if ([[jsonItem objectForKey:@"value"] isKindOfClass:[NSNull class]]) |
| 456 | [uiItem setObject:@"" forKey:@"value"]; |
| 457 | else |
| 458 | [uiItem setObject:(NSString *)[jsonItem objectForKey:@"value"] forKey:@"value"]; |
| 459 | |
| 460 | // set label |
| 461 | if ([[jsonItem objectForKey:@"longLabel"] isKindOfClass:[NSNull class]]) |
| 462 | [uiItem setObject:@"" forKey:@"longLabel"]; |
| 463 | else |
| 464 | [uiItem setObject:(NSString *)[jsonItem objectForKey:@"longLabel"] forKey:@"longLabel"]; |
| 465 | |
| 466 | // set public/private |
| 467 | if ([(NSArray *)[jsonItem objectForKey:@"groups"] count] > 0) |
| 468 | [uiItem setValue:@"1" forKey:@"private"]; |
| 469 | else |
| 470 | [uiItem setValue:@"0" forKey:@"private"]; |
| 471 | |
| 472 | // set enabled/disabled |
| 473 | [uiItem setValue:@"0" forKey:@"enabled"]; // default to disabled |
| 474 | NSUInteger selectedProfileId = [[self.theProfile objectForKey:@"id"] integerValue]; |
| 475 | if ([[jsonItem objectForKey:@"global"] integerValue] == 0) { |
| 476 | NSArray *profileArray = (NSArray *)[jsonItem objectForKey:@"profiles"]; |
| 477 | id aProfile; |
| 478 | for (aProfile in profileArray) { |
| 479 | if ([aProfile integerValue] == selectedProfileId) { |
| 480 | [uiItem setValue:@"1" forKey:@"enabled"]; |
| 481 | break; |
| 482 | } |
| 483 | } |
| 484 | } else { |
| 485 | [uiItem setValue:@"1" forKey:@"enabled"]; |
| 486 | } |
| 487 | |
| 488 | |
| 489 | // test nonterminal and return |
| 490 | if ([jsonItem objectForKey:@"terminal"] && [[jsonItem objectForKey:@"terminal"] integerValue] == 0) { //nonterminal |
| 491 | [uiItem setValue:@"0" forKey:@"terminal"]; |
| 492 | [uiItem setObject:[self.skeysDict objectForKey:@"ntn"] forKey:@"service"]; |
| 493 | [uiItem setObject:(NSString *)[[RecordViewController imageMappings] objectForKey:@"ntn"] forKey:@"imageName"]; |
| 494 | return uiItem; |
| 495 | } |
| 496 | [uiItem setValue:@"1" forKey:@"terminal"]; |
| 497 | |
| 498 | // set service + LI string, and image as well |
| 499 | NSMutableString *sPart = [NSMutableString stringWithString:@""]; |
| 500 | NSMutableString *liPart = [NSMutableString stringWithString:@""]; |
| 501 | BOOL couldbeMobilePhone = NO; |
| 502 | if ([jsonItem objectForKey:@"serviceKeys"]) { |
| 503 | NSUInteger i, count = [[jsonItem objectForKey:@"serviceKeys"] count]; |
| 504 | for (i = 0; i < count; i++) { |
| 505 | NSString *anS = [[jsonItem objectForKey:@"serviceKeys"] objectAtIndex:i]; |
| 506 | if (i == 0) { |
| 507 | // set the icon image to the first service key |
| 508 | if ([[RecordViewController imageMappings] objectForKey:anS]) { |
| 509 | [uiItem setObject:[[RecordViewController imageMappings] objectForKey:anS] forKey:@"imageName"]; |
| 510 | } else { |
| 511 | [uiItem setObject:[[RecordViewController imageMappings] objectForKey:@"unknown"] forKey:@"imageName"]; |
| 512 | } |
| 513 | // Check for mobile phones (yes mobile phones SHOULD have been their own |
| 514 | // type, but blame the IETF for this ridiculous state of things) |
| 515 | if ([anS isEqualToString:@"voice"]) |
| 516 | couldbeMobilePhone = YES; // Could be a mobile. see below |
| 517 | } else { |
| 518 | [sPart appendString:@" & "]; |
| 519 | } |
| 520 | if ([self.skeysDict objectForKey:anS]) { |
| 521 | [sPart appendString:[self.skeysDict objectForKey:anS]]; |
| 522 | } else { |
| 523 | [sPart appendString:anS]; |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | if ([jsonItem objectForKey:@"locations"]) { |
| 528 | NSUInteger i, count = [[jsonItem objectForKey:@"locations"] count]; |
| 529 | for (i = 0; i < count; i++) { |
| 530 | if (i > 0) |
| 531 | [liPart appendString:@" & "]; |
| 532 | NSString *anLI = (NSString *)[[jsonItem objectForKey:@"locations"] objectAtIndex:i]; |
| 533 | [liPart appendString:[self.liDict objectForKey:anLI]]; |
| 534 | if ([anLI isEqualToString:@"x-mobile"] && couldbeMobilePhone) { |
| 535 | [uiItem setObject:[[RecordViewController imageMappings] objectForKey:@"mobile"] forKey:@"imageName"]; |
| 536 | } |
| 537 | } |
| 538 | } |
| 539 | [uiItem setObject:[NSString stringWithFormat:@"%@ %@", liPart, sPart] forKey:@"service"]; |
| 540 | |
| 541 | return uiItem; |
| 542 | } |
| 543 | |
| 544 | #pragma mark - |
| 545 | #pragma mark AlertRenameView delegate methods |
| 546 | |
| 547 | - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { |
| 548 | AlertRenameView *aV = (AlertRenameView *)alertView; |
| 549 | if (buttonIndex == aV.cancelButtonIndex) { |
| 550 | // Cancel |
| 551 | return; |
| 552 | } |
| 553 | if ([aV.uiStringField.text length] == 0) { |
| 554 | return; |
| 555 | } |
| 556 | [self renameProfile:aV.uiStringField.text]; |
| 557 | return; |
| 558 | } |
| 559 | |
| 560 | #pragma mark HATextViewDelegate Methods |
| 561 | |
| 562 | - (void)didChangeContentOfTextView:(UITextView *)textView { |
| 563 | self.ninfoLabel.text = textView.text; |
| 564 | [self storeTextHeader:self.ninfoLabel.text]; |
| 565 | } |
| 566 | |
| 567 | - (void)didDismissTextView:(UITextView *)textView { |
| 568 | } |
| 569 | |
| 570 | #pragma mark - |
| 571 | #pragma mark Data management |
| 572 | |
| 573 | - (NSInteger)activateProfile { |
| 574 | // inputProfile = { |
| 575 | // domainName: "cartman.tel", |
| 576 | // profileId: 23 |
| 577 | // }; |
| 578 | // |
| 579 | // successResult = { |
| 580 | // success: true, |
| 581 | // actionMessages: ["profile activated", |
| 582 | // "2nd message here", |
| 583 | // "3rd message here"] |
| 584 | // }; |
| 585 | |
| 586 | Profile *conn = [[[Profile alloc] init] autorelease]; |
| 587 | [conn setTheDelegate:[MyTelConnect sharedInstance]]; |
| 588 | [conn setActionSel:@selector(doNothing:)]; |
| 589 | [conn setConnectionUrl:[conn urlFromAction:@"activateprofile"]]; |
| 590 | |
| 591 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:2] retain]; |
| 592 | MyTelConnect *cMyTel = [MyTelConnect sharedInstance]; |
| 593 | [requestData setObject:[cMyTel topDomainFromDomain:[self.delegate domain]] forKey:@"domainName"]; |
| 594 | [requestData setObject:[self.theProfile objectForKey:@"id"] forKey:@"profileId"]; |
| 595 | |
| 596 | [conn setPayload:requestData]; |
| 597 | NSDictionary *parsedJson = [conn performLookup:FALSE]; |
| 598 | [requestData release]; |
| 599 | NSInteger result = [[parsedJson valueForKey:@"success"] integerValue]; |
| 600 | if (result == 0) { |
| 601 | [Profile throwJsonErrorAlert:parsedJson]; |
| 602 | } else { |
| 603 | [delegate dataDidChangeInController:self]; |
| 604 | } |
| 605 | return result; |
| 606 | } |
| 607 | |
| 608 | - (NSInteger)renameProfile:(NSString *)aProfName { |
| 609 | // Creates new profile or updates existing profile if id is passed in |
| 610 | // inputProfile = { |
| 611 | // domainName: "cartman.tel", |
| 612 | // profileId: 13, |
| 613 | // newProfileName: "holiday" |
| 614 | // }; |
| 615 | // |
| 616 | // successResult = { |
| 617 | // success: true, |
| 618 | // profileId: 13, |
| 619 | // actionMessages: ["profile stored" |
| 620 | // "2nd message here", |
| 621 | // "3rd message here"] |
| 622 | // }; |
| 623 | |
| 624 | if (!aProfName) { |
| 625 | return 0; |
| 626 | } |
| 627 | [aProfName retain]; |
| 628 | Profile *conn = [[[Profile alloc] init] autorelease]; |
| 629 | [conn setTheDelegate:self]; |
| 630 | [conn setActionSel:@selector(doNothing:)]; |
| 631 | [conn setConnectionUrl:[conn urlFromAction:@"storeprofile"]]; |
| 632 | |
| 633 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:3] retain]; |
| 634 | MyTelConnect *cMyTel = [MyTelConnect sharedInstance]; |
| 635 | [requestData setObject:[cMyTel topDomainFromDomain:[self.delegate domain]] forKey:@"domainName"]; |
| 636 | [requestData setObject:[self.theProfile objectForKey:@"id"] forKey:@"profileId"]; |
| 637 | [requestData setObject:aProfName forKey:@"newProfileName"]; |
| 638 | |
| 639 | [conn setPayload:requestData]; |
| 640 | NSDictionary *parsedJson = [conn performLookup:FALSE]; |
| 641 | [requestData release]; |
| 642 | NSInteger result = [[parsedJson valueForKey:@"success"] integerValue]; |
| 643 | if (result == 0) { |
| 644 | [Profile throwJsonErrorAlert:parsedJson]; |
| 645 | } else { |
| 646 | NSMutableDictionary *newProfile = [NSMutableDictionary dictionaryWithDictionary:self.theProfile]; |
| 647 | [newProfile setObject:aProfName forKey:@"name"]; |
| 648 | self.theProfile = newProfile; |
| 649 | [delegate dataDidChangeInController:self]; |
| 650 | } |
| 651 | [aProfName release]; |
| 652 | return result; |
| 653 | } |
| 654 | |
| 655 | - (void)getTextHeader { |
| 656 | // inputTextHeader = { |
| 657 | // domainName: "cartman.tel", |
| 658 | // profileId: 23 |
| 659 | // }; |
| 660 | |
| 661 | TextHeader *conn = [[[TextHeader alloc] init] autorelease]; |
| 662 | [conn setTheDelegate:self]; |
| 663 | [conn setActionSel:@selector(updateUIHeaderWithJson:)]; |
| 664 | [conn setConnectionUrl:[conn urlFromAction:@"gettextheader"]]; |
| 665 | |
| 666 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:2] retain]; |
| 667 | [requestData setObject:[self.delegate domain] forKey:@"domainName"]; |
| 668 | [requestData setObject:[self.theProfile objectForKey:@"id"] forKey:@"profileId"]; |
| 669 | |
| 670 | [conn setPayload:requestData]; |
| 671 | [conn performLookup:TRUE]; |
| 672 | [requestData release]; |
| 673 | } |
| 674 | |
| 675 | - (void)storeTextHeader:(NSString *)newHeader { |
| 676 | //inputTextHeader = { |
| 677 | //domainName: "cartman.tel", |
| 678 | //apiId: 23, /** may be 0 if text header is not yet set */ |
| 679 | //profileId: 2, /** only needed if apiId is 0, else ignored */ |
| 680 | //text: "lorem ipsum dolor sit amet consectateur..." |
| 681 | //}; |
| 682 | |
| 683 | if (newHeader == nil) return; |
| 684 | if (!self.ninfoApiId) { |
| 685 | self.ninfoApiId = @"0"; |
| 686 | } |
| 687 | |
| 688 | TextHeader *conn = [[[TextHeader alloc] init] autorelease]; |
| 689 | [conn setTheDelegate:[MyTelConnect sharedInstance]]; |
| 690 | [conn setActionSel:@selector(doNothing:)]; |
| 691 | [conn setConnectionUrl:[conn urlFromAction:@"storetextheader"]]; |
| 692 | |
| 693 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:4] retain]; |
| 694 | [requestData setObject:[self.delegate domain] forKey:@"domainName"]; |
| 695 | [requestData setObject:[self.theProfile objectForKey:@"id"] forKey:@"profileId"]; |
| 696 | [requestData setObject:self.ninfoApiId forKey:@"apiId"]; |
| 697 | [requestData setObject:newHeader forKey:@"text"]; |
| 698 | |
| 699 | [conn setPayload:requestData]; |
| 700 | [conn performLookup:TRUE]; |
| 701 | [requestData release]; |
| 702 | } |
| 703 | |
| 704 | - (void)getRecords { |
| 705 | //inputRecord = { |
| 706 | //domainName: "stan.cartman.tel", |
| 707 | //profileId: 23, /** use -1 to get all Records of the domain */ |
| 708 | //includeNonTerminals: true |
| 709 | //}; |
| 710 | |
| 711 | // successResult = { |
| 712 | // success: true, |
| 713 | // domainId: 12, |
| 714 | // recordList: [{apiId: 23, terminal: true, |
| 715 | // serviceKeys: ["voice", "fax"], value: "+34.34343443", |
| 716 | // label: "my home number", groups: [1, 3], |
| 717 | // profiles: [12, 23], global: false, |
| 718 | // locations: ["x-home", "x-mobile"], editable: true}, |
| 719 | // {apiId: 12, terminal: true, serviceKeys: ["web"],, |
| 720 | // value: "www.telnic.org", label: "my homepage", |
| 721 | // groups: [3], profiles: [], global: true, |
| 722 | // locations: ["x-home"], editable: true}, |
| 723 | // {apiId: 12, terminal: false, value: "stan.cartman.tel.", |
| 724 | // groups: [3], profiles: [], global: true}], |
| 725 | // actionMessages: ["request successful", |
| 726 | // "2nd message here", |
| 727 | // "3rd message here"] |
| 728 | // }; |
| 729 | // |
| 730 | Record *conn = [[[Record alloc] init] autorelease]; |
| 731 | [conn setTheDelegate:self]; |
| 732 | [conn setActionSel:@selector(updateUITableWithJson:)]; |
| 733 | [conn setConnectionUrl:[conn urlFromAction:@"getrecordlist"]]; |
| 734 | |
| 735 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:3] retain]; |
| 736 | [requestData setObject:[self.delegate domain] forKey:@"domainName"]; |
| 737 | [requestData setObject:@"-1" forKey:@"profileId"]; |
| 738 | // Use the below to only request this profile's records |
| 739 | // [requestData setObject:[self.theProfile objectForKey:@"id"] forKey:@"profileId"]; |
| 740 | |
| 741 | [requestData setObject:@"true" forKey:@"includeNonTerminals"]; |
| 742 | |
| 743 | [conn setPayload:requestData]; |
| 744 | [conn performLookup:TRUE]; |
| 745 | [requestData release]; |
| 746 | } |
| 747 | |
| 748 | - (void)enableRecords { |
| 749 | // inputRecords = { |
| 750 | // domainName: "cartman.tel", |
| 751 | // profiles: [23, 35], |
| 752 | // apiIds: [23, 2, 22] |
| 753 | // }; |
| 754 | // |
| 755 | // successResult = { |
| 756 | // success: true, |
| 757 | // actionMessages: ["records enabled" |
| 758 | // "2nd message here", |
| 759 | // "3rd message here"] |
| 760 | // }; |
| 761 | |
| 762 | NSMutableArray *apiIdArray = [NSMutableArray arrayWithCapacity:[uiArray count]]; |
| 763 | for (NSDictionary *theRow in uiArray) { |
| 764 | if ([[theRow valueForKey:@"enabled"] integerValue] == 1) { |
| 765 | if ([[theRow valueForKey:@"terminal"] integerValue] == 1) { |
| 766 | // Only handle terminal records |
| 767 | [apiIdArray addObject:[theRow objectForKey:@"apiId"]]; |
| 768 | } |
| 769 | } |
| 770 | } |
| 771 | if ([apiIdArray count] == 0) { |
| 772 | buttonEditCount--; |
| 773 | [self performSelector:@selector(updateEditButtonAndDoNothing:) withObject:nil]; |
| 774 | return; |
| 775 | } |
| 776 | |
| 777 | Record *conn = [[[Record alloc] init] autorelease]; |
| 778 | [conn setTheDelegate:self]; |
| 779 | [conn setActionSel:@selector(updateEditButtonAndDoNothing:)]; |
| 780 | [conn setConnectionUrl:[conn urlFromAction:@"enablerecords"]]; |
| 781 | |
| 782 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:3] retain]; |
| 783 | [requestData setObject:[self.delegate domain] forKey:@"domainName"]; |
| 784 | NSArray *profiles = [NSArray arrayWithObject:[self.theProfile objectForKey:@"id"]]; |
| 785 | [requestData setObject:profiles forKey:@"profiles"]; |
| 786 | |
| 787 | [requestData setObject:apiIdArray forKey:@"apiIds"]; |
| 788 | |
| 789 | [conn setPayload:requestData]; |
| 790 | [conn performLookup:TRUE]; |
| 791 | [requestData release]; |
| 792 | buttonEditCount--; |
| 793 | } |
| 794 | |
| 795 | - (void)disableRecords { |
| 796 | // inputRecords = { |
| 797 | // domainName: "cartman.tel", |
| 798 | // profiles: [23, 35], |
| 799 | // apiIds: [23, 2, 22] |
| 800 | // }; |
| 801 | // |
| 802 | // successResult = { |
| 803 | // success: true, |
| 804 | // actionMessages: ["records disabled" |
| 805 | // "2nd message here", |
| 806 | // "3rd message here"] |
| 807 | // }; |
| 808 | |
| 809 | NSMutableArray *apiIdArray = [NSMutableArray arrayWithCapacity:[uiArray count]]; |
| 810 | for (NSDictionary *theRow in uiArray) { |
| 811 | if ([[theRow valueForKey:@"enabled"] integerValue] == 0) { |
| 812 | if ([[theRow valueForKey:@"terminal"] integerValue] == 1) { |
| 813 | // Only handle terminal records |
| 814 | [apiIdArray addObject:[theRow objectForKey:@"apiId"]]; |
| 815 | } |
| 816 | } |
| 817 | } |
| 818 | if ([apiIdArray count] == 0) { |
| 819 | buttonEditCount--; |
| 820 | [self performSelector:@selector(updateEditButtonAndDoNothing:) withObject:nil]; |
| 821 | return; |
| 822 | } |
| 823 | |
| 824 | Record *conn = [[[Record alloc] init] autorelease]; |
| 825 | [conn setTheDelegate:self]; |
| 826 | [conn setActionSel:@selector(updateEditButtonAndDoNothing:)]; |
| 827 | [conn setConnectionUrl:[conn urlFromAction:@"disablerecords"]]; |
| 828 | |
| 829 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:3] retain]; |
| 830 | [requestData setObject:[self.delegate domain] forKey:@"domainName"]; |
| 831 | NSArray *profiles = [NSArray arrayWithObject:[self.theProfile objectForKey:@"id"]]; |
| 832 | [requestData setObject:profiles forKey:@"profiles"]; |
| 833 | |
| 834 | [requestData setObject:apiIdArray forKey:@"apiIds"]; |
| 835 | |
| 836 | [conn setPayload:requestData]; |
| 837 | [conn performLookup:TRUE]; |
| 838 | [requestData release]; |
| 839 | buttonEditCount--; |
| 840 | } |
| 841 | |
| 842 | @end |
| 843 |
Note: See TracBrowser
for help on using the browser.








