root/apps/iphone/superbook/trunk/Classes/NaptrViewController.m
@
510
| Revision 510, 26.0 kB (checked in by henri, 4 years ago) |
|---|
| Line | |
|---|---|
| 1 | // |
| 2 | // NaptrViewController.m |
| 3 | // LocateThem |
| 4 | // |
| 5 | // Created by Henri Asseily on 8/25/08. |
| 6 | /* |
| 7 | Copyright (c) 2008-2009, Telnic Ltd. All rights reserved. |
| 8 | |
| 9 | Redistribution and use in source and binary forms, with or without modification, |
| 10 | are permitted provided that the following conditions are met: |
| 11 | |
| 12 | Redistributions of source code must retain the above copyright notice, this list of conditions |
| 13 | and the following disclaimer. Redistributions in binary form must reproduce the above copyright |
| 14 | notice, this list of conditions and the following disclaimer in the documentation and/or other |
| 15 | materials provided with the distribution. |
| 16 | Neither the name of the Telnic Ltd. nor the names of its contributors may be used to endorse or |
| 17 | promote products derived from this software without specific prior written permission. |
| 18 | THIS DOCUMENTATION IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS |
| 19 | OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
| 20 | AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 21 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 24 | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 25 | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | // |
| 28 | |
| 29 | #import "NaptrViewController.h" |
| 30 | |
| 31 | static UIFont *headerFont; |
| 32 | static UIFont *txtFont; |
| 33 | |
| 34 | @interface NaptrViewController (PrivateMethods) |
| 35 | |
| 36 | - (void)decrementLoadingCount; |
| 37 | - (void)showError:(NSError *)error; |
| 38 | @end |
| 39 | |
| 40 | @implementation NaptrViewController |
| 41 | |
| 42 | @synthesize theTable; |
| 43 | @synthesize displayName; |
| 44 | @synthesize cleanTelName; |
| 45 | @synthesize headerText; |
| 46 | @synthesize naptrArray; |
| 47 | @synthesize txtArray; |
| 48 | @synthesize locArray; |
| 49 | @synthesize country; |
| 50 | @synthesize timeZone; |
| 51 | @synthesize prefsReverseGeo; |
| 52 | @synthesize loadingAIView; |
| 53 | @synthesize loadingAIButton; |
| 54 | @synthesize storeContactButton; |
| 55 | @synthesize abId; |
| 56 | |
| 57 | #define kHeaderTextFontSize 14.0 |
| 58 | #define kTextFontSize 14.0 |
| 59 | |
| 60 | #pragma mark ------- canonical init methods |
| 61 | |
| 62 | |
| 63 | - (BOOL)setupWithPersonId:(NSNumber *)personId { |
| 64 | NSString *telName = [[FriendsData sharedInstance] getTelForABRecordId:personId withABRecordRef:nil]; |
| 65 | self.abId = personId; |
| 66 | return [self setupWithTel:telName]; |
| 67 | } |
| 68 | |
| 69 | - (BOOL) setupWithTel:(NSString *)telName { |
| 70 | |
| 71 | headerFont = [UIFont boldSystemFontOfSize:kHeaderTextFontSize]; |
| 72 | txtFont = [UIFont boldSystemFontOfSize:kTextFontSize]; |
| 73 | |
| 74 | self.title = [[FriendsData sharedInstance] telFromQueryString:telName]; |
| 75 | if (!self.title) |
| 76 | return FALSE; |
| 77 | self.displayName = nil; // user may not have a special display name |
| 78 | self.abId = [NSNumber numberWithInt:NSUIntegerMax]; |
| 79 | self.cleanTelName = [NSMutableString stringWithString:self.title]; |
| 80 | [self.cleanTelName replaceOccurrencesOfString:@"-" |
| 81 | withString:@" " |
| 82 | options:NSLiteralSearch |
| 83 | range:NSMakeRange(0, [self.cleanTelName length])]; |
| 84 | |
| 85 | self.headerText = nil; |
| 86 | self.country = nil; |
| 87 | self.timeZone = nil; |
| 88 | geoLocDidFinish = NO; |
| 89 | if (self.naptrArray) { |
| 90 | self.naptrArray = nil; |
| 91 | self.txtArray = nil; |
| 92 | self.locArray = nil; |
| 93 | } |
| 94 | self.naptrArray = [NSMutableArray arrayWithCapacity:10]; |
| 95 | self.txtArray = [NSMutableArray arrayWithCapacity:10]; |
| 96 | self.locArray = [NSMutableArray arrayWithCapacity:1]; |
| 97 | |
| 98 | self.loadingAIView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; |
| 99 | self.loadingAIView.hidesWhenStopped = YES; |
| 100 | self.loadingAIButton = [[UIBarButtonItem alloc] initWithCustomView:self.loadingAIView]; |
| 101 | |
| 102 | self.storeContactButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave |
| 103 | target:self |
| 104 | action:@selector(saveContact:)]; |
| 105 | |
| 106 | // set the uinavbar of the root viewcontroller of the navcontroller |
| 107 | self.navigationItem.rightBarButtonItem = self.loadingAIButton; |
| 108 | |
| 109 | [self.theTable reloadData]; |
| 110 | loadingCount = 3; |
| 111 | [self performSelectorInBackground:@selector(getNaptrInBackgroundForTel:) withObject:self.title]; |
| 112 | [self performSelectorInBackground:@selector(getTxtInBackgroundForTel:) withObject:self.title]; |
| 113 | [self performSelectorInBackground:@selector(getLocInBackgroundForTel:) withObject:self.title]; |
| 114 | return TRUE; |
| 115 | } |
| 116 | |
| 117 | #pragma mark ------- Basic methods |
| 118 | |
| 119 | - (void)viewDidLoad { |
| 120 | NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; |
| 121 | self.prefsReverseGeo = [defaults integerForKey:@"reverseGeoPreference"]; |
| 122 | } |
| 123 | |
| 124 | - (void)viewWillAppear:(BOOL)animated { |
| 125 | [super viewWillAppear:animated]; |
| 126 | // set the uinavbar of the non-root viewcontrollers of the navcontroller |
| 127 | if (self.displayName != nil) { |
| 128 | self.navigationItem.title = self.displayName; |
| 129 | } else { |
| 130 | self.navigationItem.title = self.title; |
| 131 | } |
| 132 | if (!self.navigationItem.rightBarButtonItem) |
| 133 | self.navigationItem.rightBarButtonItem = self.loadingAIButton; |
| 134 | } |
| 135 | |
| 136 | - (void)viewDidAppear:(BOOL)animated { |
| 137 | [super viewDidAppear:animated]; |
| 138 | self.navigationController.navigationBarHidden = NO; |
| 139 | } |
| 140 | |
| 141 | - (void)didReceiveMemoryWarning { |
| 142 | [super didReceiveMemoryWarning]; |
| 143 | } |
| 144 | |
| 145 | - (void)dealloc { |
| 146 | [super dealloc]; |
| 147 | } |
| 148 | |
| 149 | #pragma mark ------ set up loading stuff |
| 150 | |
| 151 | - (NSUInteger)loadingCount { |
| 152 | return loadingCount; |
| 153 | } |
| 154 | |
| 155 | - (void)decrementLoadingCount { |
| 156 | // Must call this on main thread, because it has UI needs and is called from background threads |
| 157 | if (![NSThread isMainThread]) { |
| 158 | [self performSelectorOnMainThread:_cmd |
| 159 | withObject:nil |
| 160 | waitUntilDone:YES]; |
| 161 | return; |
| 162 | } |
| 163 | loadingCount--; |
| 164 | if (loadingCount == 0) { |
| 165 | [self.loadingAIView stopAnimating]; |
| 166 | self.navigationItem.rightBarButtonItem = self.storeContactButton; |
| 167 | } else { |
| 168 | [self.loadingAIView startAnimating]; |
| 169 | self.navigationItem.rightBarButtonItem = self.loadingAIButton; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | #pragma mark ------ tableView datasource methods |
| 174 | |
| 175 | - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { |
| 176 | return SECTION_LOC+1; |
| 177 | } |
| 178 | |
| 179 | - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { |
| 180 | switch (section) { |
| 181 | case SECTION_HEADER: |
| 182 | return NSLocalizedString(@"Header", @"Header section title for contact tableview"); |
| 183 | break; |
| 184 | case SECTION_NAPTR: |
| 185 | return NSLocalizedString(@"Contact Info", @"Contact data section title for contact tableview"); |
| 186 | break; |
| 187 | case SECTION_TXT: |
| 188 | return NSLocalizedString(@"Keywords", @"Keyword section title for contact tableview"); |
| 189 | break; |
| 190 | case SECTION_LOC: |
| 191 | return NSLocalizedString(@"Location", @"Location section title for contact tableview"); |
| 192 | break; |
| 193 | default: |
| 194 | return NSLocalizedString(@"Other Records", @"Other records' section title for contact tableview"); |
| 195 | break; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
| 200 | switch (section) { |
| 201 | case SECTION_HEADER: |
| 202 | if (!headerText) { |
| 203 | return 0; |
| 204 | } |
| 205 | return 1; |
| 206 | break; |
| 207 | case SECTION_NAPTR: |
| 208 | return [naptrArray count]; |
| 209 | break; |
| 210 | case SECTION_TXT: |
| 211 | return [txtArray count]; |
| 212 | break; |
| 213 | case SECTION_LOC: |
| 214 | return [locArray count]; |
| 215 | break; |
| 216 | default: |
| 217 | return 0; |
| 218 | break; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
| 223 | |
| 224 | switch (indexPath.section) { |
| 225 | case SECTION_HEADER: |
| 226 | { |
| 227 | static NSString *MyIdentifier = @"headerCell"; |
| 228 | |
| 229 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; |
| 230 | if (cell == nil) { |
| 231 | cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; |
| 232 | } |
| 233 | //Working with a resizable cell |
| 234 | //If there is already a subview, remove it. |
| 235 | if ([[cell.contentView subviews] count] > 0) { |
| 236 | UIView *labelToClear = [[cell.contentView subviews] objectAtIndex:0]; |
| 237 | [labelToClear removeFromSuperview]; |
| 238 | } |
| 239 | UILabel *cellLabel; |
| 240 | cellLabel = [headerText sizeCellLabelWithFont:headerFont paddingWidth:10.0f Height:10.0f]; |
| 241 | [cell.contentView addSubview:cellLabel]; |
| 242 | return cell; |
| 243 | } |
| 244 | break; |
| 245 | |
| 246 | case SECTION_NAPTR: |
| 247 | { |
| 248 | static NSString *MyIdentifier = @"naptrCells"; |
| 249 | |
| 250 | RecordNaptr *theRec = [self.naptrArray objectAtIndex:indexPath.row]; |
| 251 | |
| 252 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; |
| 253 | if (cell == nil) { |
| 254 | NSArray *naptrCellNib = [[NSBundle mainBundle] loadNibNamed:@"NaptrTableViewCell" owner:self options:nil]; |
| 255 | cell = (UITableViewCell *)[naptrCellNib objectAtIndex:0]; |
| 256 | } |
| 257 | |
| 258 | [(UILabel *)[cell viewWithTag:NAPTRCELL_SERVICETYPE_TAG] setText:theRec.serviceDescription]; |
| 259 | |
| 260 | // Check if it's a nonterminal naptr. If so, add the accessory indicator |
| 261 | // and remove the trailing part of the string if it is a subdomain of the current domain |
| 262 | if (theRec.isTerminal) { |
| 263 | [(UILabel *)[cell viewWithTag:NAPTRCELL_LABEL_TAG] setText:theRec.labelDescription]; |
| 264 | [(UILabel *)[cell viewWithTag:NAPTRCELL_VALUE_TAG] setText:theRec.uriContent]; |
| 265 | } else { |
| 266 | [(UILabel *)[cell viewWithTag:NAPTRCELL_LABEL_TAG] setText:@""]; |
| 267 | [(UILabel *)[cell viewWithTag:NAPTRCELL_VALUE_TAG] setText:theRec.labelDescription]; |
| 268 | // If NTN points within domain, shorten text to only display the subdomain of this domain |
| 269 | if ([[theRec.labelDescription lowercaseString] hasSuffix:[self.cleanTelName lowercaseString]]) { |
| 270 | NSInteger suffixIndex = [theRec.labelDescription length] - [self.cleanTelName length] - 1; |
| 271 | if (suffixIndex > 0) { |
| 272 | [(UILabel *)[cell viewWithTag:NAPTRCELL_VALUE_TAG] setText:[theRec.labelDescription substringToIndex:suffixIndex]]; |
| 273 | } |
| 274 | } |
| 275 | [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; |
| 276 | } |
| 277 | |
| 278 | return cell; |
| 279 | } |
| 280 | break; |
| 281 | |
| 282 | case SECTION_TXT: |
| 283 | { |
| 284 | static NSString *MyIdentifier = @"txtCells"; |
| 285 | |
| 286 | RecordTxt *theRec = [self.txtArray objectAtIndex:indexPath.row]; |
| 287 | |
| 288 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; |
| 289 | if (cell == nil) { |
| 290 | NSArray *txtCellNib = [[NSBundle mainBundle] loadNibNamed:@"TxtTableViewCell" owner:self options:nil]; |
| 291 | cell = (UITableViewCell *)[txtCellNib objectAtIndex:0]; |
| 292 | } |
| 293 | [(UILabel *)[cell viewWithTag:1] setText:theRec.primaryDescription]; |
| 294 | if (theRec.primaryValue != NULL) { |
| 295 | [(UILabel *)[cell viewWithTag:3] setText:theRec.primaryValue]; |
| 296 | } else { |
| 297 | [(UILabel *)[cell viewWithTag:3] setText:@""]; |
| 298 | } |
| 299 | |
| 300 | // Set the text description and resize the label accordingly |
| 301 | // We're not doing it like the header because we're using a nib for the cell |
| 302 | // (but we override the font size here) |
| 303 | UILabel *textDescriptionLabel = (UILabel *)[cell viewWithTag:2]; |
| 304 | textDescriptionLabel.text = theRec.textValue; |
| 305 | CGFloat width = textDescriptionLabel.frame.size.width; |
| 306 | CGFloat height = [textDescriptionLabel.text cellHeightForFont:txtFont paddingWidth:10.0f Height:6.0f]; |
| 307 | CGRect newframe = CGRectMake(textDescriptionLabel.frame.origin.x, textDescriptionLabel.frame.origin.y, width, height); |
| 308 | textDescriptionLabel.frame = newframe; |
| 309 | |
| 310 | return cell; |
| 311 | } |
| 312 | break; |
| 313 | |
| 314 | case SECTION_LOC: |
| 315 | { |
| 316 | static NSString *MyIdentifier = @"locCell"; |
| 317 | |
| 318 | RecordLoc *theRec = [self.locArray objectAtIndex:indexPath.row]; |
| 319 | |
| 320 | // Get the cell |
| 321 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; |
| 322 | if (cell == nil) { |
| 323 | NSArray *locCellNib = [[NSBundle mainBundle] loadNibNamed:@"LocTableViewCell" owner:self options:nil]; |
| 324 | cell = (UITableViewCell *)[locCellNib objectAtIndex:0]; |
| 325 | } |
| 326 | |
| 327 | // Display cell info |
| 328 | [(UILabel *)[cell viewWithTag:LOCCELL_COUNTRY_TAG] setText:NSLocalizedString(@"Looking Up Country...", @"Looking Up Country...")]; |
| 329 | |
| 330 | if (timeZone) { |
| 331 | NSCalendar *cal = [NSCalendar currentCalendar]; |
| 332 | [cal setTimeZone:timeZone]; |
| 333 | NSDate *now = [NSDate date]; |
| 334 | // Set the color of the background of the cell, based on whether the person should be awake or not |
| 335 | // Possibility: Make the hours a preference |
| 336 | // unsigned unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit; |
| 337 | // NSDateComponents *comps = [cal components:unitFlags fromDate:now]; |
| 338 | // if (([comps hour] < 8) || ([comps hour] > 22)) { |
| 339 | // cell.contentView.backgroundColor = [UIColor colorWithRed:0 green:0.15 blue:0.3 alpha:1]; |
| 340 | // cell.accessoryView.backgroundColor = [UIColor colorWithRed:0 green:0.15 blue:0.3 alpha:1]; |
| 341 | // } else { |
| 342 | // cell.contentView.backgroundColor = [UIColor colorWithRed:0.3 green:0.9 blue:0.9 alpha:1]; |
| 343 | // cell.accessoryView.backgroundColor = [UIColor colorWithRed:0.3 green:0.9 blue:0.9 alpha:1]; |
| 344 | // } |
| 345 | // Now display the time properly |
| 346 | NSDateFormatter *dF = [[NSDateFormatter alloc] init]; |
| 347 | [dF setTimeZone:timeZone]; |
| 348 | [dF setTimeStyle:NSDateFormatterShortStyle]; |
| 349 | [dF setDateStyle:NSDateFormatterNoStyle]; |
| 350 | [(UILabel *)[cell viewWithTag:LOCCELL_LOCALTIME_TAG] setText:[dF stringFromDate:now]]; |
| 351 | [dF release]; |
| 352 | } else { |
| 353 | // no timezone |
| 354 | [(UILabel *)[cell viewWithTag:LOCCELL_LOCALTIME_TAG] setText:@""]; |
| 355 | } |
| 356 | if (prefsReverseGeo) { |
| 357 | if (country) { |
| 358 | [(UILabel *)[cell viewWithTag:LOCCELL_COUNTRY_TAG] setText:country]; |
| 359 | [(UIActivityIndicatorView *)[cell viewWithTag:LOCCELL_ACTIVITYINDICATOR_TAG] stopAnimating]; |
| 360 | } else { |
| 361 | if (geoLocDidFinish) { |
| 362 | [(UILabel *)[cell viewWithTag:LOCCELL_COUNTRY_TAG] setText:NSLocalizedString(@"Unknown Country", @"Unknown Country")]; |
| 363 | [(UIActivityIndicatorView *)[cell viewWithTag:LOCCELL_ACTIVITYINDICATOR_TAG] stopAnimating]; |
| 364 | } else { |
| 365 | [(UIActivityIndicatorView *)[cell viewWithTag:LOCCELL_ACTIVITYINDICATOR_TAG] startAnimating]; |
| 366 | } |
| 367 | } |
| 368 | } else { // not prefsReverseGeo |
| 369 | [(UILabel *)[cell viewWithTag:LOCCELL_COUNTRY_TAG] setText:NSLocalizedString(@"(country lookup disabled)", @"country lookup disabled")]; |
| 370 | [(UIActivityIndicatorView *)[cell viewWithTag:LOCCELL_ACTIVITYINDICATOR_TAG] stopAnimating]; |
| 371 | } |
| 372 | |
| 373 | [(UILabel *)[cell viewWithTag:LOCCELL_LATLONG_TAG] setText:[NSString stringWithFormat:@"%@: %.4f, %.4f", |
| 374 | NSLocalizedString(@"Coordinates", @"Coordinates"), |
| 375 | theRec.coordinate.latitude, theRec.coordinate.longitude]]; |
| 376 | return cell; |
| 377 | } |
| 378 | break; |
| 379 | |
| 380 | default: |
| 381 | { |
| 382 | UITableViewCell *cell = nil; |
| 383 | return cell; |
| 384 | } |
| 385 | break; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | #pragma mark ---------- Table view Delegate methods ---------- |
| 390 | |
| 391 | // Row selection actions: |
| 392 | // - terminal naptr: fire off the url, and let the OS handle it |
| 393 | // - non-terminal naptr: query that domain |
| 394 | // - loc: display the mapview |
| 395 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
| 396 | NSUInteger row = indexPath.row; |
| 397 | if (row != NSNotFound) { |
| 398 | [tableView deselectRowAtIndexPath:indexPath animated:NO]; |
| 399 | switch (indexPath.section) { |
| 400 | case SECTION_NAPTR: |
| 401 | { |
| 402 | RecordNaptr *naptr = [self.naptrArray objectAtIndex:row]; |
| 403 | if (naptr.isTerminal) { |
| 404 | [self actOnUri:naptr.uriContent]; |
| 405 | } else { |
| 406 | // Non-Terminal NAPTR |
| 407 | NaptrViewController *newDetailsViewController = [[[NaptrViewController alloc] initWithNibName:@"TelView" |
| 408 | bundle:[NSBundle mainBundle]] |
| 409 | autorelease]; |
| 410 | [newDetailsViewController setupWithTel:naptr.uriContent]; |
| 411 | [tableView deselectRowAtIndexPath:indexPath animated:NO]; |
| 412 | [self.navigationController pushViewController:newDetailsViewController animated:YES]; |
| 413 | } |
| 414 | } |
| 415 | break; |
| 416 | case SECTION_LOC: |
| 417 | { |
| 418 | RecordLoc *loc = [locArray objectAtIndex:row]; |
| 419 | FriendsMapViewController *fMVC = [FriendsMapViewController sharedInstance]; |
| 420 | fMVC.initialLoc = loc; |
| 421 | if ([abId integerValue] == NSUIntegerMax) { // not one of our address book .tel domains |
| 422 | [fMVC setMarkerWithABRecordId:self.abId title:nil telname:self.title location:loc]; |
| 423 | } else { |
| 424 | NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; |
| 425 | if (![defaults boolForKey:@"startupLocatePreference"]) { |
| 426 | // need to draw the marker, as we didn't locate the user earlier |
| 427 | NSString *personName = [[FriendsData sharedInstance] getNameForABRecordId:self.abId]; |
| 428 | [fMVC setMarkerWithABRecordId:abId title:personName telname:self.title location:loc]; |
| 429 | } |
| 430 | } |
| 431 | [tableView deselectRowAtIndexPath:indexPath animated:NO]; |
| 432 | [self.navigationController pushViewController:fMVC animated:YES]; |
| 433 | } |
| 434 | break; |
| 435 | default: |
| 436 | break; |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
| 442 | switch (indexPath.section) { |
| 443 | case SECTION_HEADER: |
| 444 | { |
| 445 | return [headerText cellHeightForFont:headerFont paddingWidth:10.0f Height:10.0f]; |
| 446 | } |
| 447 | case SECTION_TXT: |
| 448 | { |
| 449 | RecordTxt *theRec = [self.txtArray objectAtIndex:indexPath.row]; |
| 450 | return [theRec.textValue cellHeightForFont:txtFont paddingWidth:10.0f Height:18.0f]; |
| 451 | } |
| 452 | default: |
| 453 | return (CGFloat)44.0; |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | /* |
| 458 | - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
| 459 | |
| 460 | if (editingStyle == UITableViewCellEditingStyleDelete) { |
| 461 | } |
| 462 | if (editingStyle == UITableViewCellEditingStyleInsert) { |
| 463 | } |
| 464 | } |
| 465 | */ |
| 466 | /* |
| 467 | - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { |
| 468 | return YES; |
| 469 | } |
| 470 | */ |
| 471 | /* |
| 472 | - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { |
| 473 | } |
| 474 | */ |
| 475 | /* |
| 476 | - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { |
| 477 | return YES; |
| 478 | } |
| 479 | */ |
| 480 | |
| 481 | #pragma mark ------ Custom Data Methods |
| 482 | |
| 483 | - (void)getNaptrForTel:(NSString *)telName { |
| 484 | NSError *error = nil; |
| 485 | [[NetworkUtility sharedInstance] networkwillActivate:YES]; |
| 486 | DnsResolver *res = [[DnsResolver alloc] init]; |
| 487 | NSUInteger naptrCount = [res getNAPTRForTel:telName inArray:self.naptrArray withError:&error]; |
| 488 | [res release]; |
| 489 | if (naptrCount == 0) { |
| 490 | #ifdef DEBUG |
| 491 | NSLog(@"Warning: Error or No NAPTR records in domain %@", telName); |
| 492 | #endif |
| 493 | } |
| 494 | [[NetworkUtility sharedInstance] networkwillActivate:NO]; |
| 495 | [self decrementLoadingCount]; |
| 496 | [self.theTable performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES]; |
| 497 | if (error) { |
| 498 | // Show all errors unless the answer is invalid, which generally means there aren't any NAPTRs |
| 499 | if ([error code] != RESOLVER_STATUS_INVALID_ANSWER) { |
| 500 | [self showError:error]; |
| 501 | } |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | - (void)getTxtForTel:(NSString *)telName { |
| 506 | NSError *error = nil; |
| 507 | [[NetworkUtility sharedInstance] networkwillActivate:YES]; |
| 508 | DnsResolver *res = [[DnsResolver alloc] init]; |
| 509 | NSMutableArray *alltxtArray = [[[NSMutableArray alloc] initWithCapacity:20] autorelease]; |
| 510 | NSUInteger txtCount = [res getTXTForTel:telName inArray:alltxtArray withError:&error]; |
| 511 | if (txtCount == 0) { |
| 512 | #ifdef DEBUG |
| 513 | NSLog(@"Warning: Error or No TXT records in domain %@", telName); |
| 514 | #endif |
| 515 | } |
| 516 | [[NetworkUtility sharedInstance] networkwillActivate:NO]; |
| 517 | [self decrementLoadingCount]; |
| 518 | [res release]; |
| 519 | // Now build out the keywords txtArray and extract the header, display string and other stuff |
| 520 | RecordTxt *theRec; |
| 521 | NSMutableString *_headerText = nil; |
| 522 | for (theRec in alltxtArray) { |
| 523 | if (theRec.subType == RecordTxtSubTypeKeyword) { |
| 524 | // It's a keyword, let's add it to the array in the txt section |
| 525 | [self.txtArray addObject:theRec]; |
| 526 | } else if (theRec.subType == RecordTxtSubTypeString) { |
| 527 | // simple string, currently a header |
| 528 | // Merge all simple strings into a header |
| 529 | if (!_headerText) { |
| 530 | _headerText = [NSMutableString stringWithString:theRec.textValue]; |
| 531 | } else { |
| 532 | //NSLog(theRec.textValue); |
| 533 | [_headerText appendFormat:@"\n%@", theRec.textValue]; |
| 534 | } |
| 535 | } else if (theRec.subType == RecordTxtSubTypeDDS) { |
| 536 | // domain display string |
| 537 | self.displayName = theRec.textValue; |
| 538 | } else { |
| 539 | // do nothing with the other records such as ads, just discard them |
| 540 | } |
| 541 | } |
| 542 | if (self.displayName != nil) { |
| 543 | self.navigationItem.title = self.displayName; |
| 544 | } |
| 545 | self.headerText = [NSString stringWithString:_headerText]; |
| 546 | [self.theTable performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES]; |
| 547 | } |
| 548 | |
| 549 | - (void)getLocForTel:(NSString *)telName { |
| 550 | NSError *error = nil; |
| 551 | [[NetworkUtility sharedInstance] networkwillActivate:YES]; |
| 552 | DnsResolver *res = [[DnsResolver alloc] init]; |
| 553 | NSUInteger locCount = [res getLOCForTel:telName inArray:self.locArray withError:&error]; |
| 554 | [res release]; |
| 555 | [[NetworkUtility sharedInstance] networkwillActivate:NO]; |
| 556 | [self decrementLoadingCount]; // Don't wait for reverse geo to say we're done, that's a bonus |
| 557 | if (locCount > 1) { |
| 558 | //NSLog(@"Warning: %d LOC records in domain %@", locCount, telName); |
| 559 | } |
| 560 | [self.theTable performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES]; |
| 561 | if (locCount > 0) { |
| 562 | if (prefsReverseGeo) { |
| 563 | // and now look up the timezone and other goodies that take a while because they use the web |
| 564 | [[NetworkUtility sharedInstance] networkwillActivate:YES]; |
| 565 | [self getCountryAndTimeZoneForLoc:(RecordLoc *)[self.locArray objectAtIndex:0]]; |
| 566 | [[NetworkUtility sharedInstance] networkwillActivate:NO]; |
| 567 | [self.theTable performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES]; |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | - (void) getNaptrInBackgroundForTel:(NSString *)telName { |
| 573 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
| 574 | [self getNaptrForTel:telName]; |
| 575 | [pool release]; |
| 576 | } |
| 577 | |
| 578 | - (void) getTxtInBackgroundForTel:(NSString *)telName { |
| 579 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
| 580 | [self getTxtForTel:telName]; |
| 581 | [pool release]; |
| 582 | } |
| 583 | |
| 584 | - (void) getLocInBackgroundForTel:(NSString *)telName { |
| 585 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
| 586 | [self getLocForTel:telName]; |
| 587 | [pool release]; |
| 588 | } |
| 589 | |
| 590 | - (void)getCountryAndTimeZoneForLoc:(CLLocation *)aLoc { |
| 591 | // Get country and timezone |
| 592 | // Don't get current time because the url may be cached. Use the timezone to regenerate the current time |
| 593 | NSString *uS = [NSString stringWithFormat:@"http://ws.geonames.org/timezone?lat=%f&lng=%f", |
| 594 | aLoc.coordinate.latitude, aLoc.coordinate.longitude]; |
| 595 | NSURL *url = [NSURL URLWithString:uS]; |
| 596 | NSURLRequest *urlReq = [NSURLRequest requestWithURL:url |
| 597 | cachePolicy:NSURLRequestReturnCacheDataElseLoad |
| 598 | timeoutInterval:60.0]; |
| 599 | NSHTTPURLResponse *response = [[[NSHTTPURLResponse alloc] |
| 600 | initWithURL:url |
| 601 | MIMEType:@"text/xml" |
| 602 | expectedContentLength:1024 |
| 603 | textEncodingName:nil] autorelease]; |
| 604 | NSError *error; |
| 605 | NSData *tzData = [NSURLConnection sendSynchronousRequest:urlReq |
| 606 | returningResponse:&response |
| 607 | error:&error]; |
| 608 | if (tzData) { |
| 609 | // Got data |
| 610 | NSString *sData = [[NSString alloc] initWithData:tzData encoding:NSUTF8StringEncoding]; |
| 611 | // Now get country |
| 612 | NSRange rC1 = [sData rangeOfString:@"<countryName>"]; |
| 613 | if (rC1.location != NSNotFound) { |
| 614 | NSRange rC2 = [sData rangeOfString:@"</countryName>"]; |
| 615 | if (rC2.location != NSNotFound) { |
| 616 | NSRange rC = NSMakeRange(rC1.location+rC1.length, rC2.location - (rC1.location+rC1.length)); |
| 617 | self.country = [sData substringWithRange:rC]; |
| 618 | } |
| 619 | } |
| 620 | // And get timezone |
| 621 | NSRange rTZ1 = [sData rangeOfString:@"<timezoneId>"]; |
| 622 | if (rTZ1.location != NSNotFound) { |
| 623 | NSRange rTZ2 = [sData rangeOfString:@"</timezoneId>"]; |
| 624 | if (rTZ2.location != NSNotFound) { |
| 625 | NSRange rTZ = NSMakeRange(rTZ1.location+rTZ1.length, rTZ2.location - (rTZ1.location+rTZ1.length)); |
| 626 | self.timeZone = [NSTimeZone timeZoneWithName:[sData substringWithRange:rTZ]]; |
| 627 | } |
| 628 | } |
| 629 | [sData release]; |
| 630 | } |
| 631 | geoLocDidFinish = YES; |
| 632 | } |
| 633 | |
| 634 | #pragma mark ------ Methods to handle URI actions |
| 635 | |
| 636 | - (void)actOnUri:(NSString *)uri { |
| 637 | if (!uri) |
| 638 | return; |
| 639 | if ([uri hasPrefix:@"tel:"]) { |
| 640 | // if a telephone number, ask if we want to send SMS. |
| 641 | // Plus, this acts as a user acceptance to call or not |
| 642 | UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:uri |
| 643 | delegate:self |
| 644 | cancelButtonTitle:nil |
| 645 | destructiveButtonTitle:nil |
| 646 | otherButtonTitles:nil]; |
| 647 | callButtonIndex = [actionSheet addButtonWithTitle:NSLocalizedString(@"Call this number", @"Call this number")]; |
| 648 | smsButtonIndex = [actionSheet addButtonWithTitle:NSLocalizedString(@"Send an SMS Message", @"Send an SMS Message")]; |
| 649 | actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:NSLocalizedString(@"Cancel", @"Cancel")]; |
| 650 | [actionSheet showInView:self.view]; |
| 651 | [actionSheet release]; |
| 652 | } else if ([uri hasPrefix:@"http:"] || [uri hasPrefix:@"https:"]) { |
| 653 | WebUrlViewController *wVC = [[[WebUrlViewController alloc] initWithNibName:@"WebUrlView" bundle:nil] autorelease]; |
| 654 | wVC.title = NSLocalizedString(@"Web View", @"Title for the Web View"); |
| 655 | [self.navigationController pushViewController:wVC animated:YES]; |
| 656 | [wVC showPage:uri]; |
| 657 | } else { |
| 658 | // directly act on anything not a number or url |
| 659 | NSURL *naptrUrl = [NSURL URLWithString:uri]; |
| 660 | [[UIApplication sharedApplication] openURL:naptrUrl]; |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { |
| 665 | if (buttonIndex == actionSheet.cancelButtonIndex) { |
| 666 | // Clicked cancel |
| 667 | return; |
| 668 | } |
| 669 | NSURL *naptrUrl; |
| 670 | NSString *uri = [actionSheet title]; |
| 671 | if (buttonIndex == callButtonIndex) { |
| 672 | naptrUrl = [NSURL URLWithString:[uri stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]; |
| 673 | [[UIApplication sharedApplication] openURL:naptrUrl]; |
| 674 | } else if (buttonIndex == smsButtonIndex) { |
| 675 | NSRange telPrefixRange = [uri rangeOfString:@"tel:"]; |
| 676 | naptrUrl = [NSURL URLWithString:[uri stringByReplacingCharactersInRange:telPrefixRange withString:@"sms:"]]; |
| 677 | [[UIApplication sharedApplication] openURL:naptrUrl]; |
| 678 | } |
| 679 | return; |
| 680 | } |
| 681 | |
| 682 | #pragma mark ------ Methods to save a contact |
| 683 | |
| 684 | - (IBAction)saveContact:(id)sender { |
| 685 | if (abD) |
| 686 | [abD release]; |
| 687 | abD = [[DotTelABDelegate alloc] initWithViewController:self telName:self.title]; |
| 688 | abD.naptrArray = self.naptrArray; |
| 689 | abD.txtArray = self.txtArray; |
| 690 | NSError *err; |
| 691 | if ([abId integerValue] == NSUIntegerMax) { // not one of our address book .tel domains |
| 692 | err = [abD saveRecord]; |
| 693 | } else { |
| 694 | err = [abD saveRecordWithRecordId:[abId integerValue]]; |
| 695 | } |
| 696 | if (err) { |
| 697 | NSLog([err localizedDescription]); |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | #pragma mark ------ Error Methods |
| 702 | |
| 703 | - (void)showError:(NSError *)error { |
| 704 | if (![NSThread isMainThread]) { |
| 705 | [self performSelectorOnMainThread:_cmd |
| 706 | withObject:error |
| 707 | waitUntilDone:NO]; |
| 708 | return; |
| 709 | } |
| 710 | UIAlertView *errAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", @"Error") |
| 711 | message:[error localizedDescription] |
| 712 | delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", @"OK") |
| 713 | otherButtonTitles:nil]; |
| 714 | [errAlert show]; |
| 715 | [errAlert release]; |
| 716 | } |
| 717 | @end |
| 718 |
Note: See TracBrowser
for help on using the browser.








