root/apps/iphone/my.tel/trunk/Classes/DomainViewController.m
@
781
| Revision 781, 31.9 kB (checked in by henri, 2 years ago) |
|---|
| Line | |
|---|---|
| 1 | // |
| 2 | // DomainViewController.m |
| 3 | // My.tel |
| 4 | // |
| 5 | // Created by Henri Asseily on 10/12/09. |
| 6 | // Copyright 2009 Telnic Ltd. All rights reserved. |
| 7 | // |
| 8 | |
| 9 | #import "DomainViewController.h" |
| 10 | #import "HAImageView.h" |
| 11 | #import "Account.h" |
| 12 | #import "Subdomain.h" |
| 13 | |
| 14 | #define kDDSMaxLength 255 // DDS max length |
| 15 | |
| 16 | @interface DomainViewController (PrivateMethods) |
| 17 | - (NSInteger)sectionForIndex:(NSInteger)sectionIndex; |
| 18 | @end |
| 19 | |
| 20 | |
| 21 | @implementation DomainViewController |
| 22 | |
| 23 | @synthesize domain; |
| 24 | @synthesize ddsTitle; |
| 25 | @synthesize subdomainsArray; |
| 26 | @dynamic activeProfile; |
| 27 | @synthesize activeProfileCell; |
| 28 | @synthesize recordVC, keywordVC, locationVC, profileVC, groupVC, prVC; |
| 29 | |
| 30 | + (DomainViewController *)controllerForDomain:(NSString *)aDomain { |
| 31 | if (!aDomain) return nil; |
| 32 | DomainViewController *theC = [[[DomainViewController alloc] initWithStyle:UITableViewStyleGrouped] autorelease]; |
| 33 | theC.title = theC.domain = aDomain; |
| 34 | theC.ddsTitle = nil; |
| 35 | theC.subdomainsArray = [NSMutableArray arrayWithCapacity:10]; |
| 36 | theC.activeProfile = nil; |
| 37 | |
| 38 | // Set up the active profile cell |
| 39 | theC.activeProfileCell = [[[NSBundle mainBundle] loadNibNamed:@"ActiveProfileHeaderCell" owner:self options:nil] objectAtIndex:0]; |
| 40 | // Set the default image to be the image that is already there in the nib |
| 41 | // TODO: Build an IB plugin for HAImageView so that defaultImage can be set in IB? |
| 42 | HAImageView *photoView = (HAImageView *)[theC.activeProfileCell viewWithTag:1]; |
| 43 | photoView.defaultImage = photoView.image; |
| 44 | UILabel *textLabel = (UILabel *)[theC.activeProfileCell viewWithTag:2]; |
| 45 | textLabel.text = LocStr(@"Loading Profile Header..."); |
| 46 | |
| 47 | [theC getSubFolderList]; |
| 48 | return theC; |
| 49 | } |
| 50 | |
| 51 | /* |
| 52 | - (id)initWithStyle:(UITableViewStyle)style { |
| 53 | // Override initWithStyle: if you create the controller programmatically and want to perform customization |
| 54 | // that is not appropriate for viewDidLoad. |
| 55 | if (self = [super initWithStyle:style]) { |
| 56 | } |
| 57 | return self; |
| 58 | } |
| 59 | */ |
| 60 | |
| 61 | - (void)viewDidLoad { |
| 62 | [super viewDidLoad]; |
| 63 | locCell = [[LocationCell alloc] initWithStyle:UITableViewCellStyleValue1 |
| 64 | reuseIdentifier:@"locCell"]; |
| 65 | |
| 66 | self.navigationItem.rightBarButtonItem = [self editButtonItem]; |
| 67 | self.tableView.allowsSelectionDuringEditing = YES; |
| 68 | if (! self.activeProfileCell) { |
| 69 | self.activeProfileCell = [[[NSBundle mainBundle] loadNibNamed:@"ActiveProfileHeaderCell" owner:self options:nil] objectAtIndex:0]; |
| 70 | } |
| 71 | [self getDisplayString]; |
| 72 | self.profileVC = [ProfileViewController controllerWithDelegate:self preload:YES]; |
| 73 | self.locationVC = [LocationViewController controllerWithDelegate:self preload:YES]; |
| 74 | self.recordVC = [RecordViewController controllerWithDelegate:self preload:YES]; |
| 75 | self.keywordVC = [KeywordViewController controllerWithDelegate:self preload:YES]; |
| 76 | self.groupVC = [GroupsViewController controllerWithDelegate:self preload:YES]; |
| 77 | prVC = nil; // wait for profileVC to load |
| 78 | [locationVC addObserver:self forKeyPath:@"markerTel" options:NSKeyValueObservingOptionInitial context:NULL]; |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | - (void)viewWillAppear:(BOOL)animated { |
| 83 | [super viewWillAppear:animated]; |
| 84 | } |
| 85 | */ |
| 86 | |
| 87 | - (void)viewDidAppear:(BOOL)animated { |
| 88 | [super viewDidAppear:animated]; |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | - (void)viewWillDisappear:(BOOL)animated { |
| 93 | [super viewWillDisappear:animated]; |
| 94 | } |
| 95 | */ |
| 96 | /* |
| 97 | - (void)viewDidDisappear:(BOOL)animated { |
| 98 | [super viewDidDisappear:animated]; |
| 99 | } |
| 100 | */ |
| 101 | |
| 102 | /* |
| 103 | // Override to allow orientations other than the default portrait orientation. |
| 104 | - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
| 105 | // Return YES for supported orientations |
| 106 | return (interfaceOrientation == UIInterfaceOrientationPortrait); |
| 107 | } |
| 108 | */ |
| 109 | |
| 110 | - (void)didReceiveMemoryWarning { |
| 111 | // Releases the view if it doesn't have a superview. |
| 112 | [super didReceiveMemoryWarning]; |
| 113 | |
| 114 | // Release any cached data, images, etc that aren't in use. |
| 115 | } |
| 116 | |
| 117 | - (void)viewDidUnload { |
| 118 | [locationVC removeObserver:self forKeyPath:@"markerTel"]; |
| 119 | self.ddsTitle = nil; |
| 120 | self.recordVC = nil; |
| 121 | self.keywordVC = nil; |
| 122 | self.locationVC = nil; |
| 123 | self.profileVC = nil; |
| 124 | self.groupVC = nil; |
| 125 | [subdomainsArray release]; |
| 126 | self.activeProfile = nil; |
| 127 | self.activeProfileCell = nil; |
| 128 | [locCell release]; |
| 129 | locCell = nil; |
| 130 | [super viewDidUnload]; |
| 131 | } |
| 132 | |
| 133 | - (void)dealloc { |
| 134 | [locationVC removeObserver:self forKeyPath:@"markerTel"]; |
| 135 | self.ddsTitle = nil; |
| 136 | self.recordVC = nil; |
| 137 | self.keywordVC = nil; |
| 138 | self.locationVC = nil; |
| 139 | self.profileVC = nil; |
| 140 | self.groupVC = nil; |
| 141 | [subdomainsArray release]; |
| 142 | self.activeProfile = nil; |
| 143 | self.activeProfileCell = nil; |
| 144 | [locCell release]; |
| 145 | locCell = nil; |
| 146 | [super dealloc]; |
| 147 | } |
| 148 | |
| 149 | #pragma mark - |
| 150 | #pragma mark Table view methods |
| 151 | |
| 152 | - (NSInteger)sectionForIndex:(NSInteger)sectionIndex { |
| 153 | // maps the proper section index based on editing mode |
| 154 | switch (sectionIndex) { |
| 155 | case kKDomainTableViewSectionTitle: |
| 156 | return (self.editing ? -1 : kKDomainTableViewSectionTitle); |
| 157 | break; |
| 158 | case kKDomainTableViewSectionProfile: |
| 159 | return (self.editing ? -1 : kKDomainTableViewSectionProfile); |
| 160 | break; |
| 161 | case kKDomainTableViewSectionInfo: |
| 162 | return (self.editing ? -1 : kKDomainTableViewSectionInfo); |
| 163 | break; |
| 164 | case kKDomainTableViewSectionFolders: |
| 165 | return (self.editing ? 1 : kKDomainTableViewSectionFolders); |
| 166 | break; |
| 167 | case kKDomainTableViewSectionAddFolder: |
| 168 | return (self.editing ? 0 : -1); |
| 169 | break; |
| 170 | case kKDomainTableViewSectionsCount: |
| 171 | return (self.editing ? 2 : kKDomainTableViewSectionsCount); |
| 172 | break; |
| 173 | default: |
| 174 | break; |
| 175 | } |
| 176 | return -1; |
| 177 | } |
| 178 | |
| 179 | - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { |
| 180 | return [self sectionForIndex:kKDomainTableViewSectionsCount]; |
| 181 | } |
| 182 | |
| 183 | // Customize the number of rows in the table view. |
| 184 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
| 185 | if (section == [self sectionForIndex:kKDomainTableViewSectionTitle]) |
| 186 | return (self.editing ? 0 : 1); |
| 187 | if (section == [self sectionForIndex:kKDomainTableViewSectionProfile]) |
| 188 | // active profile controller cell + profiles controller cell |
| 189 | return 2; |
| 190 | if (section == [self sectionForIndex:kKDomainTableViewSectionFolders]) |
| 191 | return [self.subdomainsArray count]; |
| 192 | if (section == [self sectionForIndex:kKDomainTableViewSectionInfo]) |
| 193 | // contacts, keywords, location, advanced |
| 194 | return (self.editing ? 0 : 4); |
| 195 | if (section == [self sectionForIndex:kKDomainTableViewSectionAddFolder]) |
| 196 | return (self.editing ? 1 : 0); |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
| 201 | if (indexPath.section == [self sectionForIndex:kKDomainTableViewSectionProfile]) { |
| 202 | if (indexPath.row == 0) { |
| 203 | return self.activeProfileCell.frame.size.height; |
| 204 | } |
| 205 | } |
| 206 | // TODO: Make a location cell with the map |
| 207 | if (indexPath.section == [self sectionForIndex:kKDomainTableViewSectionInfo]) { |
| 208 | if (indexPath.row == 2) { // location |
| 209 | return locCell.height; |
| 210 | } |
| 211 | } |
| 212 | return tableView.rowHeight; |
| 213 | } |
| 214 | |
| 215 | |
| 216 | - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { |
| 217 | if (section == [self sectionForIndex:kKDomainTableViewSectionFolders]) { |
| 218 | if ([self tableView:tableView numberOfRowsInSection:section] == 0) |
| 219 | return nil; |
| 220 | return @"Subfolders"; |
| 221 | } |
| 222 | if (section == [self sectionForIndex:kKDomainTableViewSectionInfo]) |
| 223 | return @"Information"; |
| 224 | if (section == [self sectionForIndex:kKDomainTableViewSectionProfile]) |
| 225 | if (self.activeProfile == nil) { |
| 226 | return @"Active Profile"; |
| 227 | } else { |
| 228 | return ([@"Active Profile" stringByAppendingFormat:@": %@", [self.activeProfile valueForKey:@"name"]]); |
| 229 | } |
| 230 | return nil; |
| 231 | } |
| 232 | |
| 233 | // Customize the appearance of table view cells. |
| 234 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
| 235 | |
| 236 | static NSString *TitleCellIdentifier = @"TitleCell"; |
| 237 | static NSString *FoldersCellIdentifier = @"FoldersCell"; |
| 238 | static NSString *AddFolderCellIdentifier = @"AddFolderCell"; |
| 239 | static NSString *InfoCellIdentifier = @"InfoCell"; |
| 240 | |
| 241 | UITableViewCell *cell = nil; |
| 242 | |
| 243 | if (indexPath.section == [self sectionForIndex:kKDomainTableViewSectionTitle]) { |
| 244 | cell = [tableView dequeueReusableCellWithIdentifier:TitleCellIdentifier]; |
| 245 | if (cell == nil) { |
| 246 | cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 |
| 247 | reuseIdentifier:TitleCellIdentifier] autorelease]; |
| 248 | cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
| 249 | cell.textLabel.text = @"Page Title"; |
| 250 | } |
| 251 | if (self.ddsTitle != nil) { |
| 252 | cell.detailTextLabel.text = self.ddsTitle; |
| 253 | } |
| 254 | } else if (indexPath.section == [self sectionForIndex:kKDomainTableViewSectionProfile]) { |
| 255 | switch (indexPath.row) { |
| 256 | case 0: |
| 257 | cell = self.activeProfileCell; |
| 258 | break; |
| 259 | case 1: |
| 260 | cell = [tableView dequeueReusableCellWithIdentifier:InfoCellIdentifier]; |
| 261 | if (cell == nil) { |
| 262 | cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 |
| 263 | reuseIdentifier:InfoCellIdentifier] autorelease]; |
| 264 | cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
| 265 | } |
| 266 | cell.textLabel.text = @"All Profiles"; |
| 267 | break; |
| 268 | default: |
| 269 | break; |
| 270 | } |
| 271 | } else if (indexPath.section == [self sectionForIndex:kKDomainTableViewSectionFolders]) { |
| 272 | cell = [tableView dequeueReusableCellWithIdentifier:FoldersCellIdentifier]; |
| 273 | if (cell == nil) { |
| 274 | cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault |
| 275 | reuseIdentifier:FoldersCellIdentifier] autorelease]; |
| 276 | cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
| 277 | cell.editingAccessoryType = UITableViewCellAccessoryNone; |
| 278 | } |
| 279 | NSString *subDom = [[self.subdomainsArray objectAtIndex:indexPath.row] valueForKey:@"name"]; |
| 280 | // remove the postfix domain title from the subdomain, including the "." delimiter |
| 281 | // For offline testing usage, make sure we don't go over the index length |
| 282 | if ([subDom length] > [self.title length]) { |
| 283 | cell.textLabel.text = [subDom substringToIndex:([subDom length] - [self.title length] - 1)]; |
| 284 | } else { |
| 285 | cell.textLabel.text = subDom; |
| 286 | } |
| 287 | } else if (indexPath.section == [self sectionForIndex:kKDomainTableViewSectionInfo]) { |
| 288 | // contacts, keywords, location, profiles |
| 289 | // TODO: Add "Advanced" row for SMTP and other stuff |
| 290 | |
| 291 | // Special case for location |
| 292 | if (indexPath.row == 2) { |
| 293 | cell = locCell; |
| 294 | } else { |
| 295 | cell = [tableView dequeueReusableCellWithIdentifier:InfoCellIdentifier]; |
| 296 | if (cell == nil) { |
| 297 | cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 |
| 298 | reuseIdentifier:InfoCellIdentifier] autorelease]; |
| 299 | cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
| 300 | } |
| 301 | |
| 302 | } |
| 303 | switch (indexPath.row) { |
| 304 | case 0: |
| 305 | cell.textLabel.text = @"Contact Items"; |
| 306 | break; |
| 307 | case 1: |
| 308 | cell.textLabel.text = @"Keywords"; |
| 309 | break; |
| 310 | case 2: |
| 311 | cell.textLabel.text = @"Location"; |
| 312 | break; |
| 313 | case 3: |
| 314 | cell.textLabel.text = @"Privacy Groups"; |
| 315 | break; |
| 316 | default: |
| 317 | break; |
| 318 | } |
| 319 | } else if (indexPath.section == [self sectionForIndex:kKDomainTableViewSectionAddFolder]) { |
| 320 | cell = [tableView dequeueReusableCellWithIdentifier:AddFolderCellIdentifier]; |
| 321 | if (cell == nil) { |
| 322 | cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault |
| 323 | reuseIdentifier:AddFolderCellIdentifier] autorelease]; |
| 324 | cell.accessoryType = cell.editingAccessoryType = UITableViewCellAccessoryNone; |
| 325 | cell.textLabel.text = @"Add new folder"; |
| 326 | cell.textLabel.textColor = [UIColor blueColor]; |
| 327 | cell.textLabel.textAlignment = UITextAlignmentCenter; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | return cell; |
| 332 | } |
| 333 | |
| 334 | //- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
| 335 | // // Don't allow selection of folders when in editing mode |
| 336 | // if (self.editing && (indexPath.section == [self sectionForIndex:kKDomainTableViewSectionFolders])) { |
| 337 | // return nil; |
| 338 | // } |
| 339 | // return indexPath; |
| 340 | //} |
| 341 | |
| 342 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
| 343 | [tableView deselectRowAtIndexPath:indexPath animated:YES]; |
| 344 | |
| 345 | if (indexPath.section == [self sectionForIndex:kKDomainTableViewSectionTitle]) { |
| 346 | NSString *ddsEditTitle = @"Page Title"; |
| 347 | NSString *ddsPrompt = @"Page Title for the domain"; |
| 348 | |
| 349 | HATextViewEditController *teC = [HATextViewEditController controllerWithDelegate:self |
| 350 | title:ddsEditTitle |
| 351 | prompt:ddsPrompt]; |
| 352 | teC.textContent = self.ddsTitle; |
| 353 | teC.maxCharSize = kDDSMaxLength; |
| 354 | teC.returnKeyType = UIReturnKeyDone; |
| 355 | [self.navigationController pushViewController:teC animated:YES]; |
| 356 | } else if (indexPath.section == [self sectionForIndex:kKDomainTableViewSectionProfile]) { |
| 357 | switch (indexPath.row) { |
| 358 | case 0: // active profile |
| 359 | if (self.activeProfile != nil) { |
| 360 | // prVC is now instanciated |
| 361 | [self.navigationController pushViewController:prVC animated:YES]; |
| 362 | } |
| 363 | break; |
| 364 | case 1: //"Profiles"; |
| 365 | [self.navigationController pushViewController:self.profileVC animated:YES]; |
| 366 | break; |
| 367 | default: |
| 368 | break; |
| 369 | } |
| 370 | } else if (indexPath.section == [self sectionForIndex:kKDomainTableViewSectionFolders]) { |
| 371 | if (self.editing) { |
| 372 | // disallowed |
| 373 | return; |
| 374 | } |
| 375 | NSString *selectedSubDomain = [tableView cellForRowAtIndexPath:indexPath].textLabel.text; |
| 376 | NSString *fqdn = [NSString stringWithFormat:@"%@.%@", selectedSubDomain, self.domain]; |
| 377 | DomainViewController *newC = [DomainViewController controllerForDomain:fqdn]; |
| 378 | [self.navigationController pushViewController:newC animated:YES]; |
| 379 | } else if (indexPath.section == [self sectionForIndex:kKDomainTableViewSectionInfo]) { |
| 380 | switch (indexPath.row) { |
| 381 | case 0: //"Contacts"; |
| 382 | [self.navigationController pushViewController:self.recordVC animated:YES]; |
| 383 | break; |
| 384 | case 1: //"Keywords"; |
| 385 | [self.navigationController pushViewController:self.keywordVC animated:YES]; |
| 386 | break; |
| 387 | case 2: //"Location"; |
| 388 | [self.navigationController pushViewController:self.locationVC animated:YES]; |
| 389 | break; |
| 390 | case 3: //"Privacy Groups"; |
| 391 | [self.navigationController pushViewController:self.groupVC animated:YES]; |
| 392 | break; |
| 393 | default: |
| 394 | break; |
| 395 | } |
| 396 | } else if (indexPath.section == [self sectionForIndex:kKDomainTableViewSectionAddFolder]) { |
| 397 | [tableView.dataSource tableView:tableView |
| 398 | commitEditingStyle:UITableViewCellEditingStyleInsert forRowAtIndexPath:indexPath]; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | - (void)setEditing:(BOOL)editing animated:(BOOL)animated { |
| 403 | [super setEditing:editing animated:animated]; |
| 404 | UITableView *tv = self.tableView; |
| 405 | [tv beginUpdates]; |
| 406 | NSMutableIndexSet *sectionsToInsert = [[[NSMutableIndexSet alloc] init] autorelease]; |
| 407 | NSMutableIndexSet *sectionsToDelete = [[[NSMutableIndexSet alloc] init] autorelease]; |
| 408 | if (editing) { |
| 409 | [sectionsToDelete addIndex:0]; // kKDomainTableViewSectionTitle |
| 410 | [sectionsToDelete addIndex:1]; // kKDomainTableViewSectionProfile |
| 411 | [sectionsToDelete addIndex:2]; // kKDomainTableViewSectionInfo |
| 412 | [sectionsToInsert addIndex:0]; // kKDomainTableViewSectionAddFolder |
| 413 | } else { |
| 414 | [sectionsToDelete addIndex:0]; // kKDomainTableViewSectionAddFolder |
| 415 | [sectionsToInsert addIndex:0]; // kKDomainTableViewSectionTitle |
| 416 | [sectionsToInsert addIndex:1]; // kKDomainTableViewSectionProfile |
| 417 | [sectionsToInsert addIndex:2]; // kKDomainTableViewSectionInfo |
| 418 | } |
| 419 | [tv deleteSections:sectionsToDelete withRowAnimation:UITableViewRowAnimationFade]; |
| 420 | [tv insertSections:sectionsToInsert withRowAnimation:UITableViewRowAnimationFade]; |
| 421 | [tv endUpdates]; |
| 422 | } |
| 423 | |
| 424 | - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { |
| 425 | if (self.editing && (indexPath.section == [self sectionForIndex:kKDomainTableViewSectionFolders])) { |
| 426 | return UITableViewCellEditingStyleDelete; |
| 427 | } |
| 428 | return UITableViewCellEditingStyleNone; |
| 429 | } |
| 430 | |
| 431 | |
| 432 | - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { |
| 433 | if (indexPath.section == [self sectionForIndex:kKDomainTableViewSectionFolders]) { |
| 434 | return YES; |
| 435 | } |
| 436 | return NO; |
| 437 | } |
| 438 | |
| 439 | |
| 440 | - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
| 441 | // Edit the tableview (only the folders are supported for editing at this point) |
| 442 | |
| 443 | if (editingStyle == UITableViewCellEditingStyleDelete) { |
| 444 | // Delete the row from the data source |
| 445 | if (indexPath.section == [self sectionForIndex:kKDomainTableViewSectionFolders]) { |
| 446 | AlertRenameView *alert = [[AlertRenameView alloc] initWithTitle:@"Confirm Deletion" |
| 447 | message:@"All information contained in this folder will be permanently deleted!" |
| 448 | delegate:self |
| 449 | cancelButtonTitle:@"Cancel" |
| 450 | otherButtonTitles:@"Delete", nil]; |
| 451 | alert.alertId = [NSNumber numberWithInt:kKDomainTableViewAlertDelete]; |
| 452 | alert.refObject = indexPath; |
| 453 | ((UITextField *)alert.uiStringField).hidden = YES; |
| 454 | [alert show]; |
| 455 | [alert release]; |
| 456 | } |
| 457 | } else if (editingStyle == UITableViewCellEditingStyleInsert) { |
| 458 | if (indexPath.section == [self sectionForIndex:kKDomainTableViewSectionAddFolder]) { |
| 459 | AlertRenameView *alert = [[AlertRenameView alloc] initWithTitle:@"New Folder Name" |
| 460 | message:@"\n\n" |
| 461 | delegate:self |
| 462 | cancelButtonTitle:@"Cancel" |
| 463 | otherButtonTitles:@"OK", nil]; |
| 464 | alert.alertId = [NSNumber numberWithInt:kKDomainTableViewAlertAdd]; |
| 465 | alert.uiStringField.text = @""; |
| 466 | [alert show]; |
| 467 | [alert release]; |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | |
| 473 | /* |
| 474 | // Override to support rearranging the table view. |
| 475 | - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { |
| 476 | } |
| 477 | */ |
| 478 | |
| 479 | |
| 480 | - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { |
| 481 | return NO; |
| 482 | } |
| 483 | |
| 484 | #pragma mark - |
| 485 | #pragma mark Data Methods |
| 486 | |
| 487 | - (void)updateFoldersSectionWithJson:(NSDictionary *)parsedJson { |
| 488 | #ifdef DEBUG |
| 489 | NSLog(@"%@", [parsedJson descriptionInStringsFileFormat]); |
| 490 | #endif |
| 491 | |
| 492 | [subdomainsArray removeAllObjects]; |
| 493 | if ([[parsedJson valueForKey:@"success"] integerValue] == 1) { |
| 494 | [subdomainsArray setArray:(NSArray *)[parsedJson valueForKey:@"folderList"]]; |
| 495 | } else { |
| 496 | [Subdomain throwJsonErrorAlert:parsedJson]; |
| 497 | } |
| 498 | NSIndexSet *iSet = [NSIndexSet indexSetWithIndex:[self sectionForIndex:kKDomainTableViewSectionFolders]]; |
| 499 | [self.tableView reloadSections:iSet withRowAnimation:UITableViewRowAnimationTop]; |
| 500 | |
| 501 | } |
| 502 | |
| 503 | - (void)updateDisplayStringWithJson:(NSDictionary *)parsedJson { |
| 504 | #ifdef DEBUG |
| 505 | NSLog(@"%@", [parsedJson descriptionInStringsFileFormat]); |
| 506 | #endif |
| 507 | if ([[parsedJson valueForKey:@"success"] integerValue] == 1) { |
| 508 | self.ddsTitle = [parsedJson valueForKey:@"displayString"]; |
| 509 | if (self.ddsTitle == (NSString *)[NSNull null]) self.ddsTitle = nil; |
| 510 | } else { |
| 511 | [Subdomain throwJsonErrorAlert:parsedJson]; |
| 512 | } |
| 513 | NSIndexSet *iSet = [NSIndexSet indexSetWithIndex:[self sectionForIndex:kKDomainTableViewSectionTitle]]; |
| 514 | if (!self.editing) { |
| 515 | [self.tableView reloadSections:iSet withRowAnimation:UITableViewRowAnimationFade]; |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | - (void)getSubFolderList { |
| 520 | // input = { |
| 521 | // domainName: "tel", /** gets a list of all folders */ |
| 522 | // recursive: true |
| 523 | // }; |
| 524 | // |
| 525 | // successResult = { |
| 526 | // success: true, |
| 527 | // folderList: [{id: 12, name: "cartman.tel", soConnection: "Active"}, |
| 528 | // {id: 23, name: "stan.cartman.tel", soConnection: "Uninitialised"}, |
| 529 | // {id: 14, name: "henry.tel", soConnection: "Incomplete"}], |
| 530 | // actionMessages: ["request successful", |
| 531 | // |
| 532 | // |
| 533 | // "2nd message here", |
| 534 | // "3rd message here"] |
| 535 | // }; |
| 536 | Subdomain *conn = [[[Subdomain alloc] init] autorelease]; |
| 537 | [conn setTheDelegate:self]; |
| 538 | [conn setActionSel:@selector(updateFoldersSectionWithJson:)]; |
| 539 | [conn setConnectionUrl:[conn urlFromAction:@"getsubfolderlist"]]; |
| 540 | |
| 541 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:1] retain]; |
| 542 | [requestData setObject:self.title forKey:@"domainName"]; |
| 543 | [requestData setObject:@"false" forKey:@"recursive"]; |
| 544 | |
| 545 | [conn setPayload:requestData]; |
| 546 | [conn performLookup:TRUE]; |
| 547 | [requestData release]; |
| 548 | |
| 549 | } |
| 550 | |
| 551 | - (void)storeFolder:(NSString *)aFolderName { |
| 552 | // inputFolder = { |
| 553 | // domainName: "stan.cartman.tel" |
| 554 | // }; |
| 555 | // |
| 556 | // successResult = { |
| 557 | // success: true, |
| 558 | // domainId: 25, |
| 559 | // actionMessages: ["folder added", |
| 560 | // "2nd message here", |
| 561 | // "3rd message here"] |
| 562 | // }; |
| 563 | |
| 564 | if (!aFolderName) { |
| 565 | return; |
| 566 | } |
| 567 | [aFolderName retain]; |
| 568 | Subdomain *conn = [[[Subdomain alloc] init] autorelease]; |
| 569 | [conn setTheDelegate:self]; |
| 570 | [conn setActionSel:@selector(doNothing:)]; |
| 571 | [conn setConnectionUrl:[conn urlFromAction:@"storefolder"]]; |
| 572 | |
| 573 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:3] retain]; |
| 574 | [requestData setObject:aFolderName forKey:@"domainName"]; |
| 575 | |
| 576 | [conn setPayload:requestData]; |
| 577 | NSDictionary *parsedJson = [conn performLookup:FALSE]; |
| 578 | [requestData release]; |
| 579 | if ([[parsedJson valueForKey:@"success"] integerValue] == 0) { |
| 580 | [Subdomain throwJsonErrorAlert:parsedJson]; |
| 581 | } else { |
| 582 | NSDictionary *newSub = [NSDictionary dictionaryWithObjectsAndKeys:[parsedJson valueForKey:@"domainId"], @"id", |
| 583 | aFolderName, @"name", |
| 584 | @"Uninitialised", @"soConnection", |
| 585 | nil]; |
| 586 | [self.subdomainsArray addObject:newSub]; |
| 587 | } |
| 588 | [aFolderName release]; |
| 589 | return; |
| 590 | } |
| 591 | |
| 592 | - (void)renameFolder:(NSDictionary *)aSub withString:(NSString *)newFolderName { |
| 593 | // inputFolder = { |
| 594 | // domainName: "stan.cartman.tel" |
| 595 | // newDomainName: "john.cartman.tel" |
| 596 | // }; |
| 597 | // |
| 598 | // successResult = { |
| 599 | // success: true, |
| 600 | // actionMessages: ["folder renamed", |
| 601 | // "2nd message here", |
| 602 | // "3rd message here"] |
| 603 | // }; |
| 604 | |
| 605 | if ((!aSub) || (!newFolderName)) { |
| 606 | return; |
| 607 | } |
| 608 | [newFolderName retain]; |
| 609 | Subdomain *conn = [[[Subdomain alloc] init] autorelease]; |
| 610 | [conn setTheDelegate:self]; |
| 611 | [conn setActionSel:@selector(doNothing:)]; |
| 612 | [conn setConnectionUrl:[conn urlFromAction:@"renamefolder"]]; |
| 613 | |
| 614 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:3] retain]; |
| 615 | [requestData setObject:[aSub objectForKey:@"name"] forKey:@"domainName"]; |
| 616 | [requestData setObject:newFolderName forKey:@"newDomainName"]; |
| 617 | |
| 618 | [conn setPayload:requestData]; |
| 619 | NSDictionary *parsedJson = [conn performLookup:FALSE]; |
| 620 | [requestData release]; |
| 621 | if ([[parsedJson valueForKey:@"success"] integerValue] == 0) { |
| 622 | [Subdomain throwJsonErrorAlert:parsedJson]; |
| 623 | } else { |
| 624 | NSMutableDictionary *newSub = [NSMutableDictionary dictionaryWithDictionary:aSub]; |
| 625 | [newSub setObject:newFolderName forKey:@"domainName"]; |
| 626 | [self.subdomainsArray replaceObjectAtIndex:[self.subdomainsArray indexOfObject:aSub] withObject:newSub]; |
| 627 | } |
| 628 | [newFolderName release]; |
| 629 | return; |
| 630 | } |
| 631 | |
| 632 | - (void)deleteFolder:(NSDictionary *)aSub atIndex:(NSIndexPath *)indexPath{ |
| 633 | // inputFolder = { |
| 634 | // domainName: "test.cartman.tel" |
| 635 | // }; |
| 636 | // |
| 637 | // successResult = { |
| 638 | // success: true, |
| 639 | // actionMessages: ["folder deleted" |
| 640 | // "2nd message here", |
| 641 | // "3rd message here"] |
| 642 | // }; |
| 643 | |
| 644 | if (!aSub) { |
| 645 | return; |
| 646 | } |
| 647 | [aSub retain]; |
| 648 | Subdomain *conn = [[[Subdomain alloc] init] autorelease]; |
| 649 | [conn setTheDelegate:[MyTelConnect sharedInstance]]; |
| 650 | [conn setActionSel:@selector(doNothing:)]; |
| 651 | [conn setConnectionUrl:[conn urlFromAction:@"deletefolder"]]; |
| 652 | |
| 653 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:1] retain]; |
| 654 | [requestData setObject:[aSub objectForKey:@"name"] forKey:@"domainName"]; |
| 655 | |
| 656 | [conn setPayload:requestData]; |
| 657 | NSDictionary *parsedJson = [conn performLookup:FALSE]; |
| 658 | [requestData release]; |
| 659 | if ([[parsedJson valueForKey:@"success"] integerValue] == 0) { |
| 660 | [Subdomain throwJsonErrorAlert:parsedJson]; |
| 661 | } else { |
| 662 | [self.subdomainsArray removeObjectAtIndex:indexPath.row]; |
| 663 | [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; |
| 664 | } |
| 665 | [aSub release]; |
| 666 | } |
| 667 | |
| 668 | - (void)getDisplayString { |
| 669 | // inputFolder = { |
| 670 | // domainName: "stan.cartman.tel" |
| 671 | // }; |
| 672 | // |
| 673 | // successResult = { |
| 674 | // success: true, |
| 675 | // displayString: "Stan's domain", |
| 676 | // actionMessages: ["domain display string retrieved", |
| 677 | // "2nd message here", |
| 678 | // "3rd message here"] |
| 679 | // }; |
| 680 | // |
| 681 | Subdomain *conn = [[[Subdomain alloc] init] autorelease]; |
| 682 | [conn setTheDelegate:self]; |
| 683 | [conn setActionSel:@selector(updateDisplayStringWithJson:)]; |
| 684 | [conn setConnectionUrl:[conn urlFromAction:@"getdisplaystring"]]; |
| 685 | |
| 686 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:1] retain]; |
| 687 | [requestData setObject:self.domain forKey:@"domainName"]; |
| 688 | |
| 689 | [conn setPayload:requestData]; |
| 690 | [conn performLookup:TRUE]; |
| 691 | [requestData release]; |
| 692 | } |
| 693 | |
| 694 | |
| 695 | - (void)setDisplayString:(NSString *)aDisplayString { |
| 696 | // inputFolder = { |
| 697 | // domainName: "stan.cartman.tel", |
| 698 | // displayString: "Stan Cartman: My domain" |
| 699 | // }; |
| 700 | // |
| 701 | // successResult = { |
| 702 | // success: true, |
| 703 | // actionMessages: ["domain display string set", |
| 704 | // "2nd message here", |
| 705 | // "3rd message here"] |
| 706 | // }; |
| 707 | |
| 708 | Subdomain *conn = [[[Subdomain alloc] init] autorelease]; |
| 709 | [conn setTheDelegate:self]; |
| 710 | [conn setActionSel:@selector(getDisplayString)]; |
| 711 | [conn setConnectionUrl:[conn urlFromAction:@"setdisplaystring"]]; |
| 712 | |
| 713 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:2] retain]; |
| 714 | [requestData setObject:self.domain forKey:@"domainName"]; |
| 715 | [requestData setObject:aDisplayString forKey:@"displayString"]; |
| 716 | |
| 717 | [conn setPayload:requestData]; |
| 718 | [conn performLookup:TRUE]; |
| 719 | [requestData release]; |
| 720 | } |
| 721 | |
| 722 | - (void)setLocalLanguage:(NSString *)aLanguage { |
| 723 | // input = { |
| 724 | // domainName: "cartman.tel", |
| 725 | // localLanguage: "de" |
| 726 | // }; |
| 727 | // |
| 728 | // successresult = { |
| 729 | // success: true, |
| 730 | // actionmessages: ["default object names successfully updated with local language.", |
| 731 | // "default object names do not need to be updated.", |
| 732 | // "3rd message here"] |
| 733 | // }; |
| 734 | |
| 735 | /* |
| 736 | NOTE: Do not use setLocalLanguage, as it will change the name of the default privacy groups |
| 737 | among other things. Localization is automatically achieved in JsonConnection via the use of the |
| 738 | Accept-Language HTTP header. |
| 739 | */ |
| 740 | |
| 741 | Account *conn = [[[Account alloc] init] autorelease]; |
| 742 | [conn setConnectionUrl:[conn urlFromAction:@"setlocallanguage"]]; |
| 743 | |
| 744 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:2] retain]; |
| 745 | [requestData setObject:self.domain forKey:@"domainName"]; |
| 746 | [requestData setObject:aLanguage forKey:@"localLanguage"]; |
| 747 | |
| 748 | [conn setPayload:requestData]; |
| 749 | NSDictionary *parsedJson = [conn performLookup:FALSE]; |
| 750 | [requestData release]; |
| 751 | if ([[parsedJson valueForKey:@"success"] integerValue] == 0) { |
| 752 | [Account throwJsonErrorAlert:parsedJson]; |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | - (void)refreshActiveProfile { |
| 757 | HAImageView *photoView = (HAImageView *)[self.activeProfileCell viewWithTag:1]; |
| 758 | |
| 759 | NSIndexSet *iSet = [NSIndexSet indexSetWithIndex:[self sectionForIndex:kKDomainTableViewSectionProfile]]; |
| 760 | |
| 761 | NSArray *recordsArray = self.recordVC.recordsArray; |
| 762 | for (NSDictionary *jsonItem in recordsArray) { |
| 763 | NSArray *sKeys = [jsonItem objectForKey:@"serviceKeys"]; |
| 764 | if ((NSNull *)sKeys == [NSNull null]) |
| 765 | continue; |
| 766 | NSURL *imgUrl = nil; |
| 767 | if ([(NSString *)[sKeys objectAtIndex:0] isEqualToString:@"photo"]) { |
| 768 | imgUrl = [NSURL URLWithString:[@"http://" stringByAppendingString:[jsonItem objectForKey:@"value"]]]; |
| 769 | } else if ([(NSString *)[sKeys objectAtIndex:0] isEqualToString:@"photo-ssl"]) { |
| 770 | imgUrl = [NSURL URLWithString:[@"https://" stringByAppendingString:[jsonItem objectForKey:@"value"]]]; |
| 771 | } |
| 772 | if (imgUrl != nil) { |
| 773 | // Check that the photo is in the current profile or the global profile |
| 774 | BOOL isActive = NO; |
| 775 | if ([[jsonItem objectForKey:@"global"] integerValue] == 1) { |
| 776 | isActive = YES; |
| 777 | } else { |
| 778 | NSArray *profileArray = (NSArray *)[jsonItem objectForKey:@"profiles"]; |
| 779 | NSInteger selectedProfileId = [[self.activeProfile objectForKey:@"id"] integerValue]; |
| 780 | for (id aProfile in profileArray) { |
| 781 | if ([aProfile integerValue] == selectedProfileId) { |
| 782 | isActive = YES; |
| 783 | break; |
| 784 | } |
| 785 | } |
| 786 | } |
| 787 | if (isActive) { |
| 788 | [photoView loadImageFromURL:imgUrl]; |
| 789 | } |
| 790 | } |
| 791 | } |
| 792 | self.activeProfileCell.userInteractionEnabled = YES; |
| 793 | self.activeProfileCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
| 794 | [self.tableView reloadSections:iSet withRowAnimation:UITableViewRowAnimationNone]; |
| 795 | } |
| 796 | |
| 797 | - (NSDictionary *)activeProfile { |
| 798 | return activeProfile; |
| 799 | } |
| 800 | |
| 801 | - (void)setActiveProfile:(NSDictionary *)aProfile { |
| 802 | [activeProfile release]; |
| 803 | activeProfile = aProfile; |
| 804 | if (aProfile) { |
| 805 | [activeProfile retain]; |
| 806 | self.prVC = [ProfileRecordsViewController controllerWithProfile:activeProfile delegate:self preload:YES]; |
| 807 | [prVC addObserver:self forKeyPath:@"theProfile" options:NSKeyValueObservingOptionInitial context:NULL]; |
| 808 | [prVC addObserver:self forKeyPath:@"ninfoText" options:NSKeyValueObservingOptionInitial context:NULL]; |
| 809 | } else { |
| 810 | if (prVC) { |
| 811 | [prVC removeObserver:self forKeyPath:@"theProfile"]; |
| 812 | [prVC removeObserver:self forKeyPath:@"ninfoText"]; |
| 813 | } |
| 814 | self.prVC = nil; |
| 815 | } |
| 816 | |
| 817 | } |
| 818 | |
| 819 | - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { |
| 820 | if ([keyPath isEqualToString:@"theProfile"]) { |
| 821 | if (self.prVC && self.prVC.theProfile) { |
| 822 | [self refreshActiveProfile]; |
| 823 | } |
| 824 | } else if ([keyPath isEqualToString:@"ninfoText"]) { |
| 825 | UILabel *headerText = (UILabel *)[self.activeProfileCell viewWithTag:2]; |
| 826 | headerText.text = self.prVC.ninfoText; |
| 827 | } else if ([keyPath isEqualToString:@"markerTel"]) { |
| 828 | locCell.marker = self.locationVC.markerTel; |
| 829 | NSIndexPath *thePath = [NSIndexPath indexPathForRow:2 inSection:[self sectionForIndex:kKDomainTableViewSectionInfo]]; |
| 830 | [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:thePath] withRowAnimation:UITableViewRowAnimationNone]; |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | #pragma mark - |
| 835 | #pragma mark HATextViewEditDelegate Methods |
| 836 | |
| 837 | - (void)didChangeContentOfTextView:(UITextView *)textView { |
| 838 | self.ddsTitle = textView.text; |
| 839 | [self setDisplayString:self.ddsTitle]; |
| 840 | } |
| 841 | |
| 842 | - (void)didDismissTextView:(UITextView *)textView { |
| 843 | } |
| 844 | |
| 845 | #pragma mark - |
| 846 | #pragma mark TelControllerDelegate Methods |
| 847 | |
| 848 | - (void)dataDidChangeInController:(UIViewController *)controller { |
| 849 | UITableViewCell *theCell = nil; |
| 850 | if (controller == recordVC) { |
| 851 | NSIndexPath *thePath = [NSIndexPath indexPathForRow:0 inSection:[self sectionForIndex:kKDomainTableViewSectionInfo]]; |
| 852 | theCell = [self.tableView cellForRowAtIndexPath:thePath]; |
| 853 | theCell.detailTextLabel.text = [NSString stringWithFormat:@"(%d)", [((RecordViewController *)controller).recordsArray count]]; |
| 854 | } else if (controller == keywordVC) { |
| 855 | NSIndexPath *thePath = [NSIndexPath indexPathForRow:1 inSection:[self sectionForIndex:kKDomainTableViewSectionInfo]]; |
| 856 | theCell = [self.tableView cellForRowAtIndexPath:thePath]; |
| 857 | theCell.detailTextLabel.text = [NSString stringWithFormat:@"(%d)", [((KeywordViewController *)controller).keywordsArray count]]; |
| 858 | } else if (controller == profileVC) { |
| 859 | // We find the active profile and grab its information |
| 860 | NSArray *profilesArray = ((ProfileViewController *)controller).profilesArray; |
| 861 | for (NSDictionary *aProf in profilesArray) { |
| 862 | if ([[aProf objectForKey:@"isActive"] integerValue] == 1) { |
| 863 | self.activeProfile = aProf; |
| 864 | } |
| 865 | } |
| 866 | NSIndexPath *thePath = [NSIndexPath indexPathForRow:1 inSection:[self sectionForIndex:kKDomainTableViewSectionProfile]]; |
| 867 | theCell = [self.tableView cellForRowAtIndexPath:thePath]; |
| 868 | theCell.detailTextLabel.text = [NSString stringWithFormat:@"(%d)", [profilesArray count]]; |
| 869 | } else if (controller == groupVC) { |
| 870 | NSIndexPath *thePath = [NSIndexPath indexPathForRow:4 inSection:[self sectionForIndex:kKDomainTableViewSectionInfo]]; |
| 871 | theCell = [self.tableView cellForRowAtIndexPath:thePath]; |
| 872 | theCell.detailTextLabel.text = [NSString stringWithFormat:@"(%d)", [((GroupsViewController *)controller).groupsArray count]]; |
| 873 | } |
| 874 | if (theCell != nil) { |
| 875 | [theCell setNeedsLayout]; |
| 876 | [theCell setNeedsDisplay]; |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | #pragma mark - |
| 881 | #pragma mark AlertRenameView delegate methods |
| 882 | |
| 883 | - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { |
| 884 | AlertRenameView *aV = (AlertRenameView *)alertView; |
| 885 | switch ([aV.alertId integerValue]) { |
| 886 | case kKDomainTableViewAlertAdd: |
| 887 | if (buttonIndex == aV.cancelButtonIndex) { |
| 888 | // Cancel |
| 889 | } else if ([aV.uiStringField.text length] == 0) { |
| 890 | // Cancel |
| 891 | } else { |
| 892 | NSString *newFolder = [aV.uiStringField.text stringByAppendingFormat:@".%@", self.title]; |
| 893 | [self storeFolder:[newFolder lowercaseString]]; |
| 894 | [self.tableView reloadData]; |
| 895 | } |
| 896 | break; |
| 897 | |
| 898 | case kKDomainTableViewAlertDelete: |
| 899 | if (buttonIndex == aV.cancelButtonIndex) { |
| 900 | // Cancel |
| 901 | } else { |
| 902 | if (aV.refObject) { |
| 903 | NSIndexPath *indexPath = (NSIndexPath *)aV.refObject; |
| 904 | NSDictionary *aSub = [self.subdomainsArray objectAtIndex:indexPath.row]; |
| 905 | [self deleteFolder:aSub atIndex:indexPath]; |
| 906 | } |
| 907 | } |
| 908 | break; |
| 909 | |
| 910 | default: |
| 911 | break; |
| 912 | } |
| 913 | return; |
| 914 | } |
| 915 | |
| 916 | @end |
| 917 |
Note: See TracBrowser
for help on using the browser.








