root/apps/iphone/my.tel/trunk/Classes/RecordViewController.m
@
426
| Revision 426, 25.7 kB (checked in by henri, 4 years ago) |
|---|
| Line | |
|---|---|
| 1 | // |
| 2 | // RecordViewController.m |
| 3 | // VIP.tel |
| 4 | // |
| 5 | // Created by Henri Asseily on 11/18/08. |
| 6 | // Copyright 2008 Telnic Ltd.. All rights reserved. |
| 7 | // |
| 8 | |
| 9 | #import "RecordViewController.h" |
| 10 | |
| 11 | #define kRowHeight 54.0 |
| 12 | #define kRowHeightEditIncrease 20.0 |
| 13 | |
| 14 | static NSDictionary *imageMappings; |
| 15 | |
| 16 | @implementation RecordViewController |
| 17 | |
| 18 | @synthesize skeysDict; |
| 19 | @synthesize liDict; |
| 20 | |
| 21 | @synthesize apiId; |
| 22 | @synthesize recordsArray; |
| 23 | @synthesize uiArray; |
| 24 | |
| 25 | @synthesize theTable; |
| 26 | @synthesize buttonAdd; |
| 27 | @synthesize buttonEdit; |
| 28 | @synthesize buttonDone; |
| 29 | @synthesize navBar; |
| 30 | @synthesize editController; |
| 31 | |
| 32 | #pragma mark ------ Class variables access |
| 33 | |
| 34 | + (NSDictionary *)imageMappings { |
| 35 | if (!imageMappings) { |
| 36 | imageMappings = [[NSDictionary dictionaryWithObjectsAndKeys: |
| 37 | @"unknown.png", @"unknown", |
| 38 | @"phone.png", @"voice", |
| 39 | @"fax.png", @"fax", |
| 40 | @"blackberry.png", @"mobile", |
| 41 | @"mobile.png", @"mobileOther", |
| 42 | @"sms.png", @"sms-tel", |
| 43 | @"sms.png", @"mms-tel", |
| 44 | @"sms.png", @"ems-tel", |
| 45 | @"email.png", @"email", |
| 46 | @"email.png", @"sms-email", |
| 47 | @"email.png", @"mms-email", |
| 48 | @"email.png", @"ems-email", |
| 49 | @"http.png", @"web", |
| 50 | @"http.png", @"web-ssl", |
| 51 | @"voip.png", @"skype-voice", |
| 52 | @"voip.png", @"aol-voice", |
| 53 | @"voip.png", @"google-voice", |
| 54 | @"voip.png", @"msn-voice", |
| 55 | @"voip.png", @"yahoo-voice", |
| 56 | @"voip.png", @"sip-voice", |
| 57 | @"voip.png", @"h323-voice", |
| 58 | @"im.png", @"icq", |
| 59 | @"im.png", @"skype-im", |
| 60 | @"im.png", @"aol-im", |
| 61 | @"im.png", @"google-im", |
| 62 | @"im.png", @"msn-im", |
| 63 | @"im.png", @"yahoo-im", |
| 64 | @"im.png", @"xmpp", |
| 65 | @"ftp.png", @"ftp", |
| 66 | @"goto.png", @"ntn", |
| 67 | nil] retain]; |
| 68 | } |
| 69 | return imageMappings; |
| 70 | } |
| 71 | |
| 72 | #pragma mark ------ Standard View Controller Methods |
| 73 | |
| 74 | - (void)viewDidLoad { |
| 75 | [super viewDidLoad]; |
| 76 | [self getServiceKeys:NO]; |
| 77 | [self getLocations:NO]; |
| 78 | self.recordsArray = [NSMutableArray arrayWithCapacity:20]; |
| 79 | self.uiArray = [NSMutableArray arrayWithCapacity:20]; |
| 80 | recordTableHeight = kRowHeight; |
| 81 | [[self class] imageMappings]; |
| 82 | } |
| 83 | - (void)viewWillAppear:(BOOL)animated { |
| 84 | // This triggers when switching tabs |
| 85 | self.title = navBar.topItem.title = [VIPConnect sharedInstance].selectedDomain; |
| 86 | [self getRecords]; |
| 87 | } |
| 88 | |
| 89 | - (void)dealloc { |
| 90 | [liDict release]; |
| 91 | [skeysDict release]; |
| 92 | [imageMappings release]; |
| 93 | [super dealloc]; |
| 94 | } |
| 95 | |
| 96 | #pragma mark ------ TableView Delegate and DataSource Methods |
| 97 | |
| 98 | - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { |
| 99 | return 1; |
| 100 | } |
| 101 | |
| 102 | - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { |
| 103 | return [NSString stringWithFormat:@"Profile: %@", [VIPConnect sharedInstance].selectedProfileName]; |
| 104 | } |
| 105 | |
| 106 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
| 107 | return [uiArray count]; |
| 108 | } |
| 109 | |
| 110 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
| 111 | // UI Item keys: |
| 112 | // apiId (string), terminal (bool), enabled (bool), imageName (string), |
| 113 | // private (bool), service (string), label (string), value (string) |
| 114 | |
| 115 | static NSString *CellIdentifier = @"RecordCellIdentifier"; |
| 116 | |
| 117 | RecordCell *cell = (RecordCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
| 118 | if (cell == nil) { |
| 119 | cell = [[[RecordCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; |
| 120 | } |
| 121 | NSMutableDictionary *uiItem = [[uiArray objectAtIndex:indexPath.row] retain]; |
| 122 | [cell.labelService setText:[NSString stringWithFormat:@"%@", [uiItem objectForKey:@"service"]]]; |
| 123 | [cell.labelLabel setText:[NSString stringWithFormat:@"%@", [uiItem objectForKey:@"label"]]]; |
| 124 | [cell.labelValue setText:[NSString stringWithFormat:@"%@", [uiItem objectForKey:@"value"]]]; |
| 125 | |
| 126 | // Set the icon image |
| 127 | [cell.labelImage setImage:[UIImage imageNamed:(NSString *)[uiItem objectForKey:@"imageName"]]]; |
| 128 | //[cell.labelImage setBounds:CGRectMake(33.0, 5.0, 30.0, 30.0)]; |
| 129 | |
| 130 | // Change look and feel if enabled or disabled |
| 131 | if ([[uiItem valueForKey:@"enabled"] integerValue] == 1) { |
| 132 | [cell.labelImage setContentMode:UIViewContentModeTop]; |
| 133 | [cell.labelService setTextColor:[UIColor blackColor]]; |
| 134 | [cell.labelLabel setTextColor:[UIColor blueColor]]; |
| 135 | [cell.labelValue setTextColor:[UIColor blackColor]]; |
| 136 | [cell.switchEnabled setOn:TRUE]; |
| 137 | } else { |
| 138 | [cell.labelImage setContentMode:UIViewContentModeBottom]; |
| 139 | [cell.labelService setTextColor:[UIColor grayColor]]; |
| 140 | [cell.labelLabel setTextColor:[UIColor grayColor]]; |
| 141 | [cell.labelValue setTextColor:[UIColor grayColor]]; |
| 142 | [cell.switchEnabled setOn:FALSE]; |
| 143 | } |
| 144 | if ([[uiItem valueForKey:@"terminal"] integerValue] == 1) { |
| 145 | [cell.switchEnabled setEnabled:TRUE]; |
| 146 | } else { |
| 147 | [cell.switchEnabled setEnabled:FALSE]; |
| 148 | } |
| 149 | // TODO: Overlay a small padlock image if the record is private |
| 150 | |
| 151 | [uiItem release]; |
| 152 | |
| 153 | return cell; |
| 154 | } |
| 155 | |
| 156 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
| 157 | if (tableView.editing) { |
| 158 | // Don't go to the details screen for a record if we're editing the table |
| 159 | [tableView deselectRowAtIndexPath:indexPath animated:NO]; |
| 160 | } else { |
| 161 | [tableView deselectRowAtIndexPath:indexPath animated:YES]; |
| 162 | NSDictionary *theRecord = [self selectRecordInSetUsingId:[[uiArray objectAtIndex:indexPath.row] |
| 163 | objectForKey:@"apiId"]]; |
| 164 | [self displayEditingForRecord:theRecord]; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
| 169 | |
| 170 | if (editingStyle == UITableViewCellEditingStyleDelete) { |
| 171 | if (tableView.editing) { |
| 172 | // delete immediately, do not cache deletion because we can't tell a single row swipe from multiple deletes |
| 173 | [self deleteRecords:[uiArray objectAtIndex:indexPath.row]]; |
| 174 | } |
| 175 | [uiArray removeObjectAtIndex:indexPath.row]; |
| 176 | [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; |
| 177 | } |
| 178 | if (editingStyle == UITableViewCellEditingStyleInsert) { |
| 179 | // Not supported here |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { |
| 184 | // Disable swipe-to-delete: don't allow row edit if table not in edit mode |
| 185 | return (tableView.editing); |
| 186 | } |
| 187 | |
| 188 | - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView |
| 189 | editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { |
| 190 | return UITableViewCellEditingStyleDelete; |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { |
| 195 | return YES; |
| 196 | } |
| 197 | */ |
| 198 | |
| 199 | - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath |
| 200 | toIndexPath:(NSIndexPath *)toIndexPath { |
| 201 | |
| 202 | NSDictionary *recordToMove = [[uiArray objectAtIndex:fromIndexPath.row] retain]; |
| 203 | [uiArray removeObjectAtIndex:fromIndexPath.row]; |
| 204 | [uiArray insertObject:recordToMove atIndex:toIndexPath.row]; |
| 205 | [recordToMove release]; |
| 206 | } |
| 207 | |
| 208 | - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
| 209 | return recordTableHeight; |
| 210 | } |
| 211 | |
| 212 | #pragma mark ------ Open Record Edit view |
| 213 | |
| 214 | - (void)displayEditingForRecord:(NSDictionary *)aRec { |
| 215 | if (!editNavController) { |
| 216 | NSArray *nibItems = [[NSBundle mainBundle] loadNibNamed:@"RecordEdit" owner:self options:nil]; |
| 217 | editNavController = [nibItems objectAtIndex:0]; |
| 218 | [editNavController retain]; |
| 219 | editController = (RecordEditController *)editNavController.topViewController; |
| 220 | [editController setPickerCol1:skeysDict col2:liDict]; |
| 221 | } |
| 222 | editController.delegate = self; |
| 223 | [editController presentViewForRecord:aRec]; |
| 224 | } |
| 225 | |
| 226 | #pragma mark ------ cell enable/disable Method |
| 227 | |
| 228 | - (IBAction)switchedEnabling:(id)sender { |
| 229 | // a switch for a cell was toggled |
| 230 | // Go up the views: UISwitch > Content View > RecordCell |
| 231 | RecordCell *theCell = (RecordCell *)[[sender superview] superview]; |
| 232 | NSIndexPath *thePath = [self.theTable indexPathForCell:theCell]; |
| 233 | NSMutableDictionary *uiItem = [[uiArray objectAtIndex:thePath.row] retain]; |
| 234 | if ([[uiItem valueForKey:@"enabled"] integerValue] == 0) { |
| 235 | [uiItem setValue:@"1" forKey:@"enabled"]; |
| 236 | #ifdef DEBUG |
| 237 | NSLog([uiItem description]); |
| 238 | #endif |
| 239 | } else { |
| 240 | [uiItem setValue:@"0" forKey:@"enabled"]; |
| 241 | } |
| 242 | [uiItem release]; |
| 243 | } |
| 244 | |
| 245 | #pragma mark ------ RecordEditDelegate Methods |
| 246 | |
| 247 | - (void)didStoreRecord:(NSDictionary *)aRec { |
| 248 | [self getRecords]; |
| 249 | } |
| 250 | |
| 251 | #pragma mark ------ UI Methods and Json Delegates |
| 252 | - (IBAction)addRecord:(id)sender { |
| 253 | [self displayEditingForRecord:nil]; |
| 254 | } |
| 255 | |
| 256 | - (IBAction)editTable:(id)sender { |
| 257 | recordTableHeight = kRowHeight + kRowHeightEditIncrease; |
| 258 | navBar.topItem.rightBarButtonItem = buttonDone; |
| 259 | [theTable setEditing:YES animated:YES]; |
| 260 | [theTable reloadData]; |
| 261 | } |
| 262 | |
| 263 | - (IBAction)doneEditingTable:(id)sender { |
| 264 | buttonEditCount = 3; |
| 265 | UIActivityIndicatorView *aiV = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]; |
| 266 | [aiV startAnimating]; |
| 267 | UIBarButtonItem *savingButton = [[UIBarButtonItem alloc] initWithCustomView:aiV]; |
| 268 | [aiV release]; |
| 269 | navBar.topItem.rightBarButtonItem = savingButton; |
| 270 | [self orderRecords]; |
| 271 | |
| 272 | [theTable setEditing:NO animated:YES]; |
| 273 | recordTableHeight = kRowHeight; |
| 274 | [theTable reloadData]; |
| 275 | } |
| 276 | |
| 277 | - (NSDictionary *)selectRecordInSetUsingId:(NSString *)anId { |
| 278 | // selects the correct full record in the set from an id |
| 279 | for (NSDictionary *theRec in recordsArray) { |
| 280 | if ([[theRec valueForKey:@"apiId"] integerValue] == [anId integerValue]) { |
| 281 | return theRec; |
| 282 | } |
| 283 | } |
| 284 | return nil; |
| 285 | } |
| 286 | |
| 287 | - (void)updateEditButtonAndDoNothing:(NSDictionary *)parsedJson { |
| 288 | // hack to ensure: |
| 289 | // 1- that all order-enable-disable actions are done in serial order |
| 290 | // 2- that the user cannot re-edit the table before all is done |
| 291 | // call orderRecords, and all goes from there automatically |
| 292 | if (parsedJson) { |
| 293 | [[VIPConnect sharedInstance] doNothing:parsedJson]; |
| 294 | } |
| 295 | switch (buttonEditCount) { |
| 296 | case 2: |
| 297 | [self enableRecords]; |
| 298 | break; |
| 299 | case 1: |
| 300 | [self disableRecords]; |
| 301 | break; |
| 302 | case 0: |
| 303 | navBar.topItem.rightBarButtonItem = buttonEdit; |
| 304 | break; |
| 305 | default: |
| 306 | buttonEditCount = 0; |
| 307 | navBar.topItem.rightBarButtonItem = buttonEdit; |
| 308 | break; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | - (void)updateUITableWithJson:(NSDictionary *)parsedJson { |
| 313 | #ifdef DEBUG |
| 314 | NSLog([parsedJson descriptionInStringsFileFormat]); |
| 315 | #endif |
| 316 | |
| 317 | [self.recordsArray removeAllObjects]; |
| 318 | [self.uiArray removeAllObjects]; |
| 319 | if ([[parsedJson valueForKey:@"success"] integerValue] == 1) { |
| 320 | [self.recordsArray setArray:(NSArray *)[parsedJson valueForKey:@"recordList"]]; |
| 321 | for (NSDictionary *aRec in self.recordsArray) { |
| 322 | [self.uiArray addObject:[self uiItemFromJsonItem:aRec]]; |
| 323 | } |
| 324 | [self.theTable reloadData]; |
| 325 | } else { |
| 326 | [self.theTable reloadData]; |
| 327 | [Record throwJsonErrorAlert:parsedJson]; |
| 328 | } |
| 329 | |
| 330 | } |
| 331 | |
| 332 | - (NSMutableDictionary *)uiItemFromJsonItem:(NSDictionary *)jsonItem { |
| 333 | // Method that creates a dictionary to display in a cell row |
| 334 | // Sample jsonItem: |
| 335 | // {apiId: 23, terminal: true, |
| 336 | // serviceKeys: ["voice", "fax"], value: "+34.34343443", |
| 337 | // label: "my home number", groups: [1, 3], |
| 338 | // profiles: [12, 23], global: false, |
| 339 | // locations: ["x-home", "x-mobile"], editable: true} |
| 340 | #ifdef DEBUG |
| 341 | NSLog(@"Naptr Rec in JSON: %@", [jsonItem descriptionInStringsFileFormat]); |
| 342 | #endif |
| 343 | |
| 344 | NSMutableDictionary *uiItem; |
| 345 | if (!jsonItem) { |
| 346 | return NULL; |
| 347 | } |
| 348 | uiItem = [NSMutableDictionary dictionaryWithCapacity:7]; |
| 349 | |
| 350 | // set apiId |
| 351 | [uiItem setObject:(NSString *)[jsonItem objectForKey:@"apiId"] forKey:@"apiId"]; |
| 352 | |
| 353 | // set naptr value |
| 354 | if ([[jsonItem objectForKey:@"value"] isKindOfClass:[NSNull class]]) |
| 355 | [uiItem setObject:@"" forKey:@"value"]; |
| 356 | else |
| 357 | [uiItem setObject:(NSString *)[jsonItem objectForKey:@"value"] forKey:@"value"]; |
| 358 | |
| 359 | // set label |
| 360 | if ([[jsonItem objectForKey:@"label"] isKindOfClass:[NSNull class]]) |
| 361 | [uiItem setObject:@"" forKey:@"label"]; |
| 362 | else |
| 363 | [uiItem setObject:(NSString *)[jsonItem objectForKey:@"label"] forKey:@"label"]; |
| 364 | |
| 365 | // set public/private |
| 366 | if ([(NSArray *)[jsonItem objectForKey:@"groups"] count] > 0) |
| 367 | [uiItem setValue:@"1" forKey:@"private"]; |
| 368 | else |
| 369 | [uiItem setValue:@"0" forKey:@"private"]; |
| 370 | |
| 371 | // set enabled/disabled |
| 372 | [uiItem setValue:@"0" forKey:@"enabled"]; // default to disabled |
| 373 | if ([[jsonItem objectForKey:@"global"] integerValue] == 0) { |
| 374 | NSArray *profileArray = (NSArray *)[jsonItem objectForKey:@"profiles"]; |
| 375 | id aProfile; |
| 376 | for (aProfile in profileArray) { |
| 377 | if ([aProfile integerValue] == [[VIPConnect sharedInstance].selectedProfileId integerValue]) { |
| 378 | [uiItem setValue:@"1" forKey:@"enabled"]; |
| 379 | break; |
| 380 | } |
| 381 | } |
| 382 | } else { |
| 383 | [uiItem setValue:@"1" forKey:@"enabled"]; |
| 384 | } |
| 385 | |
| 386 | |
| 387 | // test nonterminal and return |
| 388 | if ([jsonItem objectForKey:@"terminal"] && [[jsonItem objectForKey:@"terminal"] integerValue] == 0) { //nonterminal |
| 389 | [uiItem setValue:@"0" forKey:@"terminal"]; |
| 390 | [uiItem setObject:[self.skeysDict objectForKey:@"ntn"] forKey:@"service"]; |
| 391 | [uiItem setObject:(NSString *)[[RecordViewController imageMappings] objectForKey:@"ntn"] forKey:@"imageName"]; |
| 392 | return uiItem; |
| 393 | } |
| 394 | [uiItem setValue:@"1" forKey:@"terminal"]; |
| 395 | |
| 396 | // set service + LI string, and image as well |
| 397 | NSMutableString *sPart = [NSMutableString stringWithString:@""]; |
| 398 | NSMutableString *liPart = [NSMutableString stringWithString:@""]; |
| 399 | BOOL couldbeMobilePhone = NO; |
| 400 | if ([jsonItem objectForKey:@"serviceKeys"]) { |
| 401 | NSUInteger i, count = [[jsonItem objectForKey:@"serviceKeys"] count]; |
| 402 | for (i = 0; i < count; i++) { |
| 403 | NSString *anS = [[jsonItem objectForKey:@"serviceKeys"] objectAtIndex:i]; |
| 404 | if (i == 0) { |
| 405 | // set the icon image to the first service key |
| 406 | if ([imageMappings objectForKey:anS]) { |
| 407 | [uiItem setObject:[[RecordViewController imageMappings] objectForKey:anS] forKey:@"imageName"]; |
| 408 | } else { |
| 409 | [uiItem setObject:[[RecordViewController imageMappings] objectForKey:@"unknown"] forKey:@"imageName"]; |
| 410 | } |
| 411 | // Check for mobile phones (yes mobile phones SHOULD have been their own |
| 412 | // type, but blame the IETF for this ridiculous state of things) |
| 413 | if ([anS isEqualToString:@"voice"]) |
| 414 | couldbeMobilePhone = YES; // Could be a mobile. see below |
| 415 | } else { |
| 416 | [sPart appendString:@" & "]; |
| 417 | } |
| 418 | if ([self.skeysDict objectForKey:anS]) { |
| 419 | [sPart appendString:[self.skeysDict objectForKey:anS]]; |
| 420 | } else { |
| 421 | [sPart appendString:anS]; |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | if ([jsonItem objectForKey:@"locations"]) { |
| 426 | NSUInteger i, count = [[jsonItem objectForKey:@"locations"] count]; |
| 427 | for (i = 0; i < count; i++) { |
| 428 | if (i > 0) |
| 429 | [liPart appendString:@" & "]; |
| 430 | NSString *anLI = (NSString *)[[jsonItem objectForKey:@"locations"] objectAtIndex:i]; |
| 431 | [liPart appendString:[self.liDict objectForKey:anLI]]; |
| 432 | if ([anLI isEqualToString:@"x-mobile"] && couldbeMobilePhone) { |
| 433 | [uiItem setObject:[[RecordViewController imageMappings] objectForKey:@"mobile"] forKey:@"imageName"]; |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | [uiItem setObject:[NSString stringWithFormat:@"%@ %@", liPart, sPart] forKey:@"service"]; |
| 438 | |
| 439 | return uiItem; |
| 440 | } |
| 441 | |
| 442 | - (void)fillServicesDict:(NSDictionary *)parsedJson { |
| 443 | // Fills in the Services dictionary |
| 444 | if (skeysDict) { |
| 445 | [skeysDict release]; |
| 446 | } |
| 447 | if (!parsedJson) { |
| 448 | // We're loading the data statically (not from the network) |
| 449 | skeysDict = [NSMutableDictionary dictionaryWithContentsOfFile: |
| 450 | [[NSBundle mainBundle] pathForResource:@"ServiceTypes" ofType:@"plist"]]; |
| 451 | } else { |
| 452 | // load from a json response, and reduce some of the string sizes |
| 453 | skeysDict = [[NSMutableDictionary dictionaryWithCapacity:30] retain]; |
| 454 | NSArray *ary = [parsedJson objectForKey:@"serviceKeys"]; |
| 455 | if (!ary) |
| 456 | return; |
| 457 | NSUInteger i, count = [ary count]; |
| 458 | for (i = 0; i < count; i = i+2) { |
| 459 | [skeysDict setObject:[[ary objectAtIndex:i+1] stringByReplacingOccurrencesOfString:@"Instant Messenger" |
| 460 | withString:@"IM"] |
| 461 | forKey:[ary objectAtIndex:i]]; |
| 462 | } |
| 463 | // Add the nonterminal naptr type |
| 464 | //if (![skeysDict objectForKey:@"ntn"]) |
| 465 | // [skeysDict setObject:@".tel Link" forKey:@"ntn"]; |
| 466 | } |
| 467 | [skeysDict retain]; |
| 468 | return; |
| 469 | } |
| 470 | |
| 471 | - (void)fillLocationsDict:(NSDictionary *)parsedJson { |
| 472 | // Fills in the Locations dictionary |
| 473 | if (liDict) { |
| 474 | [liDict release]; |
| 475 | } |
| 476 | if (!parsedJson) { |
| 477 | // We're loading the data statically (not from the network) |
| 478 | liDict = [NSMutableDictionary dictionaryWithContentsOfFile: |
| 479 | [[NSBundle mainBundle] pathForResource:@"LocationIndicators" ofType:@"plist"]]; |
| 480 | } else { |
| 481 | // load from a json response |
| 482 | liDict = [[NSMutableDictionary dictionaryWithCapacity:30] retain]; |
| 483 | NSArray *ary = [parsedJson objectForKey:@"locations"]; |
| 484 | if (!ary) |
| 485 | return; |
| 486 | NSUInteger i, count = [ary count]; |
| 487 | for (i = 0; i < count; i = i+2) { |
| 488 | [liDict setObject:[ary objectAtIndex:i+1] forKey:[ary objectAtIndex:i]]; |
| 489 | } |
| 490 | } |
| 491 | [liDict retain]; |
| 492 | return; |
| 493 | } |
| 494 | |
| 495 | #pragma mark ------ Data management |
| 496 | |
| 497 | - (void)getRecords { |
| 498 | //inputRecord = { |
| 499 | //domainName: "stan.cartman.tel", |
| 500 | //profileId: 23, /** use -1 to get all Records of the domain */ |
| 501 | //includeNonTerminals: true |
| 502 | //}; |
| 503 | |
| 504 | // successResult = { |
| 505 | // success: true, |
| 506 | // domainId: 12, |
| 507 | // recordList: [{apiId: 23, terminal: true, |
| 508 | // serviceKeys: ["voice", "fax"], value: "+34.34343443", |
| 509 | // label: "my home number", groups: [1, 3], |
| 510 | // profiles: [12, 23], global: false, |
| 511 | // locations: ["x-home", "x-mobile"], editable: true}, |
| 512 | // {apiId: 12, terminal: true, serviceKeys: ["web"],, |
| 513 | // value: "www.telnic.org", label: "my homepage", |
| 514 | // groups: [3], profiles: [], global: true, |
| 515 | // locations: ["x-home"], editable: true}, |
| 516 | // {apiId: 12, terminal: false, value: "stan.cartman.tel.", |
| 517 | // groups: [3], profiles: [], global: true}], |
| 518 | // actionMessages: ["request successful", |
| 519 | // "2nd message here", |
| 520 | // "3rd message here"] |
| 521 | // }; |
| 522 | // |
| 523 | Record *conn = [[[Record alloc] init] autorelease]; |
| 524 | [conn setTheDelegate:self]; |
| 525 | [conn setActionSel:@selector(updateUITableWithJson:)]; |
| 526 | [conn setConnectionUrl:[conn urlFromAction:@"getrecordlist"]]; |
| 527 | |
| 528 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:3] retain]; |
| 529 | [requestData setObject:[VIPConnect sharedInstance].selectedDomain forKey:@"domainName"]; |
| 530 | [requestData setObject:@"-1" forKey:@"profileId"]; |
| 531 | [requestData setObject:@"true" forKey:@"includeNonTerminals"]; |
| 532 | |
| 533 | [conn setPayload:requestData]; |
| 534 | [conn performLookup:TRUE]; |
| 535 | [requestData release]; |
| 536 | } |
| 537 | |
| 538 | - (void)storeRecord:(NSDictionary *)aRec { |
| 539 | // inputRecord = { |
| 540 | // domainName: "cartman.tel", |
| 541 | // apiId: 0, |
| 542 | // serviceKeys: ["voice", "fax"], |
| 543 | // value: "+34.34343443", |
| 544 | // label: "my home number", |
| 545 | // groups: [1, 2, 3], |
| 546 | // profiles: [12, 23, 47], |
| 547 | // locations: ["x-home", "x-mobile"] |
| 548 | // }; |
| 549 | // |
| 550 | // successResult = { |
| 551 | // success: "true", |
| 552 | // actionMessages: ["record stored successfully", |
| 553 | // "2nd message here", |
| 554 | // "3dmessage here"], |
| 555 | // apiId: 17 |
| 556 | // }; |
| 557 | |
| 558 | Record *conn = [[[Record alloc] init] autorelease]; |
| 559 | [conn setTheDelegate:self]; |
| 560 | [conn setActionSel:@selector(getRecords)]; |
| 561 | [conn setConnectionUrl:[conn urlFromAction:@"storerecord"]]; |
| 562 | |
| 563 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithDictionary:aRec] retain]; |
| 564 | |
| 565 | [conn setPayload:requestData]; |
| 566 | [conn performLookup:TRUE]; |
| 567 | [requestData release]; |
| 568 | } |
| 569 | |
| 570 | - (void)deleteRecords:(NSDictionary *)aRec { |
| 571 | // inputRecords = { |
| 572 | // domainName: "cartman.tel", |
| 573 | // apiIds: [23, 2, 22] |
| 574 | // }; |
| 575 | // |
| 576 | // successResult = { |
| 577 | // success: true, |
| 578 | // actionMessages: ["records deleted" |
| 579 | // "2nd message here", |
| 580 | // "3rd message here"] |
| 581 | // }; |
| 582 | |
| 583 | if (!aRec) |
| 584 | return; |
| 585 | NSArray *idsToDelete; |
| 586 | idsToDelete = [NSArray arrayWithObject:[aRec objectForKey:@"apiId"]]; |
| 587 | if ([idsToDelete count] == 0) // Nothing to delete |
| 588 | return; |
| 589 | [idsToDelete retain]; |
| 590 | Record *conn = [[[Record alloc] init] autorelease]; |
| 591 | [conn setTheDelegate:[VIPConnect sharedInstance]]; |
| 592 | [conn setActionSel:@selector(doNothing:)]; |
| 593 | [conn setConnectionUrl:[conn urlFromAction:@"deleterecords"]]; |
| 594 | |
| 595 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:2] retain]; |
| 596 | [requestData setObject:[VIPConnect sharedInstance].selectedDomain forKey:@"domainName"]; |
| 597 | [requestData setObject:idsToDelete forKey:@"apiIds"]; |
| 598 | |
| 599 | [conn setPayload:requestData]; |
| 600 | NSDictionary *parsedJson = [conn performLookup:FALSE]; |
| 601 | [requestData release]; |
| 602 | [idsToDelete release]; |
| 603 | if ([[parsedJson valueForKey:@"success"] integerValue] == 0) { |
| 604 | [Record throwJsonErrorAlert:parsedJson]; |
| 605 | } |
| 606 | [self getRecords]; |
| 607 | } |
| 608 | |
| 609 | |
| 610 | - (void)orderRecords { |
| 611 | // inputRecords = { |
| 612 | // domainName: "cartman.tel", |
| 613 | // includeNonTerminals: true |
| 614 | // apiIds: [23, 31, 1, 45, 2, 22] |
| 615 | // }; |
| 616 | // |
| 617 | // successResult = { |
| 618 | // success: true, |
| 619 | // actionMessages: ["records reordered" |
| 620 | // "2nd message here", |
| 621 | // "3rd message here"] |
| 622 | // }; |
| 623 | if ([uiArray count] == 0) { |
| 624 | buttonEditCount--; |
| 625 | [self performSelector:@selector(updateEditButtonAndDoNothing:) withObject:nil]; |
| 626 | return; |
| 627 | } |
| 628 | Record *conn = [[[Record alloc] init] autorelease]; |
| 629 | [conn setTheDelegate:self]; |
| 630 | [conn setActionSel:@selector(updateEditButtonAndDoNothing:)]; |
| 631 | [conn setConnectionUrl:[conn urlFromAction:@"orderrecords"]]; |
| 632 | |
| 633 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:3] retain]; |
| 634 | [requestData setObject:[VIPConnect sharedInstance].selectedDomain forKey:@"domainName"]; |
| 635 | [requestData setObject:@"true" forKey:@"includeNonTerminals"]; |
| 636 | |
| 637 | NSMutableArray *apiIdArray = [NSMutableArray arrayWithCapacity:[uiArray count]]; |
| 638 | for (NSDictionary *theRow in uiArray) { |
| 639 | [apiIdArray addObject:[theRow objectForKey:@"apiId"]]; |
| 640 | } |
| 641 | [requestData setObject:apiIdArray forKey:@"apiIds"]; |
| 642 | |
| 643 | [conn setPayload:requestData]; |
| 644 | [conn performLookup:TRUE]; |
| 645 | [requestData release]; |
| 646 | buttonEditCount--; |
| 647 | } |
| 648 | |
| 649 | - (void)enableRecords { |
| 650 | // inputRecords = { |
| 651 | // domainName: "cartman.tel", |
| 652 | // profiles: [23, 35], |
| 653 | // apiIds: [23, 2, 22] |
| 654 | // }; |
| 655 | // |
| 656 | // successResult = { |
| 657 | // success: true, |
| 658 | // actionMessages: ["records enabled" |
| 659 | // "2nd message here", |
| 660 | // "3rd message here"] |
| 661 | // }; |
| 662 | |
| 663 | NSMutableArray *apiIdArray = [NSMutableArray arrayWithCapacity:[uiArray count]]; |
| 664 | for (NSDictionary *theRow in uiArray) { |
| 665 | if ([[theRow valueForKey:@"enabled"] integerValue] == 1) { |
| 666 | if ([[theRow valueForKey:@"terminal"] integerValue] == 1) { |
| 667 | // Only handle terminal records |
| 668 | [apiIdArray addObject:[theRow objectForKey:@"apiId"]]; |
| 669 | } |
| 670 | } |
| 671 | } |
| 672 | if ([apiIdArray count] == 0) { |
| 673 | buttonEditCount--; |
| 674 | [self performSelector:@selector(updateEditButtonAndDoNothing:) withObject:nil]; |
| 675 | return; |
| 676 | } |
| 677 | |
| 678 | Record *conn = [[[Record alloc] init] autorelease]; |
| 679 | [conn setTheDelegate:self]; |
| 680 | [conn setActionSel:@selector(updateEditButtonAndDoNothing:)]; |
| 681 | [conn setConnectionUrl:[conn urlFromAction:@"enablerecords"]]; |
| 682 | |
| 683 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:3] retain]; |
| 684 | [requestData setObject:[VIPConnect sharedInstance].selectedDomain forKey:@"domainName"]; |
| 685 | NSArray *profiles = [NSArray arrayWithObject:[VIPConnect sharedInstance].selectedProfileId]; |
| 686 | [requestData setObject:profiles forKey:@"profiles"]; |
| 687 | |
| 688 | [requestData setObject:apiIdArray forKey:@"apiIds"]; |
| 689 | |
| 690 | [conn setPayload:requestData]; |
| 691 | [conn performLookup:TRUE]; |
| 692 | [requestData release]; |
| 693 | buttonEditCount--; |
| 694 | } |
| 695 | |
| 696 | - (void)disableRecords { |
| 697 | // inputRecords = { |
| 698 | // domainName: "cartman.tel", |
| 699 | // profiles: [23, 35], |
| 700 | // apiIds: [23, 2, 22] |
| 701 | // }; |
| 702 | // |
| 703 | // successResult = { |
| 704 | // success: true, |
| 705 | // actionMessages: ["records disabled" |
| 706 | // "2nd message here", |
| 707 | // "3rd message here"] |
| 708 | // }; |
| 709 | |
| 710 | NSMutableArray *apiIdArray = [NSMutableArray arrayWithCapacity:[uiArray count]]; |
| 711 | for (NSDictionary *theRow in uiArray) { |
| 712 | if ([[theRow valueForKey:@"enabled"] integerValue] == 0) { |
| 713 | if ([[theRow valueForKey:@"terminal"] integerValue] == 1) { |
| 714 | // Only handle terminal records |
| 715 | [apiIdArray addObject:[theRow objectForKey:@"apiId"]]; |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | if ([apiIdArray count] == 0) { |
| 720 | buttonEditCount--; |
| 721 | [self performSelector:@selector(updateEditButtonAndDoNothing:) withObject:nil]; |
| 722 | return; |
| 723 | } |
| 724 | |
| 725 | Record *conn = [[[Record alloc] init] autorelease]; |
| 726 | [conn setTheDelegate:self]; |
| 727 | [conn setActionSel:@selector(updateEditButtonAndDoNothing:)]; |
| 728 | [conn setConnectionUrl:[conn urlFromAction:@"disablerecords"]]; |
| 729 | |
| 730 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:3] retain]; |
| 731 | [requestData setObject:[VIPConnect sharedInstance].selectedDomain forKey:@"domainName"]; |
| 732 | NSArray *profiles = [NSArray arrayWithObject:[VIPConnect sharedInstance].selectedProfileId]; |
| 733 | [requestData setObject:profiles forKey:@"profiles"]; |
| 734 | |
| 735 | [requestData setObject:apiIdArray forKey:@"apiIds"]; |
| 736 | |
| 737 | [conn setPayload:requestData]; |
| 738 | [conn performLookup:TRUE]; |
| 739 | [requestData release]; |
| 740 | buttonEditCount--; |
| 741 | } |
| 742 | |
| 743 | - (void)getLocations:(BOOL)fromNetwork { |
| 744 | // successResult = { |
| 745 | // success: true, |
| 746 | // locations: ["x-home", "Home", "x-work", "Work", "x-main", |
| 747 | // "Main Contact"], |
| 748 | // actionMessages: "request successful", |
| 749 | // "2nd message here", |
| 750 | // "3rd message here"] |
| 751 | // }; |
| 752 | |
| 753 | if (fromNetwork) { |
| 754 | Record *conn = [[[Record alloc] init] autorelease]; |
| 755 | [conn setTheDelegate:[VIPConnect sharedInstance]]; |
| 756 | [conn setActionSel:@selector(doNothing:)]; |
| 757 | [conn setConnectionUrl:[conn urlFromAction:@"getlocations"]]; |
| 758 | |
| 759 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:1] retain]; |
| 760 | [requestData setObject:[VIPConnect sharedInstance].selectedDomain forKey:@"domainName"]; |
| 761 | |
| 762 | [conn setPayload:requestData]; |
| 763 | NSDictionary *parsedJson = [conn performLookup:FALSE]; |
| 764 | #ifdef DEBUG |
| 765 | NSLog([parsedJson descriptionInStringsFileFormat]); |
| 766 | #endif |
| 767 | [self fillLocationsDict:parsedJson]; |
| 768 | [requestData release]; |
| 769 | } else { |
| 770 | // Not from network |
| 771 | [self fillLocationsDict:nil]; |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | - (void)getServiceKeys:(BOOL)fromNetwork { |
| 776 | // successResult = { |
| 777 | // success: true, |
| 778 | // serviceKeys: ["voice", "Voice Call", "fax", "Fax", "web", "Web Page"], |
| 779 | // actionMessages: "request successful", |
| 780 | // "2nd message here", |
| 781 | // "3rd message here"] |
| 782 | // }; |
| 783 | |
| 784 | if (fromNetwork) { |
| 785 | Record *conn = [[[Record alloc] init] autorelease]; |
| 786 | [conn setTheDelegate:[VIPConnect sharedInstance]]; |
| 787 | [conn setActionSel:@selector(doNothing:)]; |
| 788 | [conn setConnectionUrl:[conn urlFromAction:@"getservicekeys"]]; |
| 789 | |
| 790 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:1] retain]; |
| 791 | [requestData setObject:[VIPConnect sharedInstance].selectedDomain forKey:@"domainName"]; |
| 792 | |
| 793 | [conn setPayload:requestData]; |
| 794 | NSDictionary *parsedJson = [conn performLookup:FALSE]; |
| 795 | #ifdef DEBUG |
| 796 | NSLog([parsedJson descriptionInStringsFileFormat]); |
| 797 | #endif |
| 798 | [self fillServicesDict:parsedJson]; |
| 799 | [requestData release]; |
| 800 | } else { |
| 801 | // Not from network |
| 802 | [self fillServicesDict:nil]; |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | @end |
| 807 |
Note: See TracBrowser
for help on using the browser.








