Added rudimentary handling of address information. Since we can't really parse
[zxing.git] / iphone / Classes / AddContactAction.m
index d9c1c0d..ffb8949 100644 (file)
@@ -49,7 +49,7 @@
 }
 
 - (NSString *)title {
-  return NSLocalizedString(@"Add Contact", @"Add Contact");
+  return NSLocalizedString(@"AddContactAction title", @"Add Contact");
 }
 
 - (void) addContactWithController:(UIViewController *)controller {
     ABRecordSetValue(person, kABPersonURLProperty, urlMultiValue, error);
   }
   
+  if (self.address) {
+    // we can't parse all the possible address formats, alas, so we punt by putting
+    // the entire thing into a multi-line 'street' address.
+    // This won't look great on the phone, but at least the info will be there, 
+    // and can be syned to a desktop computer, adjusted as necessary, and so on.
+    
+    // split the address into parts at each comma or return
+    NSArray *parts =
+        [self.address componentsSeparatedByCharactersInSet:
+         [NSCharacterSet characterSetWithCharactersInString:@",;\r\n"]];
+    NSMutableArray *strippedParts = [NSMutableArray arrayWithCapacity:[parts count]];
+    // for each part:
+    for (NSString *part in parts) {
+      // strip the part of whitespace
+      NSString *strippedPart =
+          [part stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
+      if ([strippedPart length] > 0) {
+        // if there is anything in this address part, add it to the list of stripped parts
+        [strippedParts addObject:strippedPart];
+      }
+    }
+    // finally, create a 'street' address by concatenating all the stripped parts, separated by linefeeds
+    NSString *street = [strippedParts componentsJoinedByString:@"\n"];
+
+    CFMutableDictionaryRef addressDict =
+        CFDictionaryCreateMutable(NULL, 
+                                  1, 
+                                  &kCFTypeDictionaryKeyCallBacks, 
+                                  &kCFTypeDictionaryValueCallBacks);
+    CFDictionarySetValue(addressDict, kABPersonAddressStreetKey, street);
+    
+    ABMutableMultiValueRef addressMultiValue = 
+        ABMultiValueCreateMutable(kABStringPropertyType);
+    ABMultiValueAddValueAndLabel(addressMultiValue, 
+                                 addressDict, 
+                                 kABHomeLabel, 
+                                 NULL);
+    ABRecordSetValue(person, kABPersonAddressProperty, addressMultiValue, error);
+    CFRelease(addressMultiValue);
+    CFRelease(addressDict);
+  }
+  
   ABUnknownPersonViewController *unknownPersonViewController = 
   [[ABUnknownPersonViewController alloc] init];
   unknownPersonViewController.displayedPerson = person;
   if (confirm) {
     viewController = controller;
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil 
-                                                        message:NSLocalizedString(@"Add Contact?", @"add contact") 
+                                                        message:NSLocalizedString(@"AddContactAction alert message", @"Add Contact?") 
                                                        delegate:self 
-                                              cancelButtonTitle:NSLocalizedString(@"Cancel", @"cancel button title") 
-                                              otherButtonTitles:NSLocalizedString(@"Add Contact", @"add contact"), nil];
+                                              cancelButtonTitle:NSLocalizedString(@"AddContactAction cancel button title", @"Cancel") 
+                                              otherButtonTitles:NSLocalizedString(@"AddContactAction confirm button title", @"Add Contact"), nil];
     [alertView show];
     [alertView release];
   } else {