6d65fc0b2698d1c95e9f572dccb1b7284c718e58
[atutor.git] / mods / atsocial_iphone_app / Classes / ContactViewController.m
1 //
2 //  ContactViewController.m
3 //  ATutor
4 //
5 //  Created by Quang Anh Do on 06/07/2010.
6 //  Copyright 2010 Quang Anh Do. All rights reserved.
7 //
8
9 #import "ContactViewController.h"
10 #import "CommonFunctions.h"
11
12 @implementation ContactViewController
13
14 - (void)dealloc {
15         CFRelease(addressBook);
16         
17         [super dealloc];
18 }
19
20 - (id)initWithId:(int)identifier {
21         NSDictionary *contactList = [NSKeyedUnarchiver unarchiveObjectWithFile:[applicationDocumentsDirectory() stringByAppendingPathComponent:@"contact_mapping.plist"]];
22         
23         return [self initWithId:identifier 
24                                            name:[contactList objectForKey:[NSString stringWithFormat:@"%d", identifier]]];
25 }
26
27 - (id)initWithId:(int)identifier name:(NSString *)name {
28         addressBook = ABAddressBookCreate();
29         
30         ABRecordRef person = NULL;
31         CFArrayRef matches = ABAddressBookCopyPeopleWithName(addressBook, (CFStringRef)name);
32         
33         if (matches && CFArrayGetCount(matches)) {
34                 person = (id)CFArrayGetValueAtIndex(matches, 0);
35                 
36                 ABMultiValueRef urls = ABRecordCopyValue(person, kABPersonURLProperty);
37                 ABMutableMultiValueRef mutableURLs = NULL;
38                 if (urls) {
39                         mutableURLs = ABMultiValueCreateMutableCopy(urls);
40                         CFRelease(urls);
41                 } else {
42                         mutableURLs = ABMultiValueCreateMutable(kABStringPropertyType);
43                 }
44                 ABMultiValueAddValueAndLabel(mutableURLs, shortLinkToContact(identifier), CFSTR("ATutor"), NULL);
45                 CFRelease(mutableURLs);
46         } else {
47                 person = ABPersonCreate();
48                 ABRecordSetValue(person, kABPersonFirstNameProperty, name, NULL);
49                 
50                 ABMutableMultiValueRef urls = ABMultiValueCreateMutable(kABMultiStringPropertyType);
51                 ABMultiValueAddValueAndLabel(urls, shortLinkToContact(identifier), CFSTR("ATutor"), NULL);
52                 ABRecordSetValue(person, kABPersonURLProperty, urls, NULL);
53                 CFRelease(urls);
54                 [(id)person autorelease];
55         }
56         
57         if (ABRecordGetRecordID(person) != kABRecordInvalidID) {
58                 self = [[ABPersonViewController alloc] init];
59                 [(ABPersonViewController *)self setAllowsEditing:YES];
60                 [(ABPersonViewController *)self setDisplayedPerson:person];
61         } else {
62                 self = [[ABUnknownPersonViewController alloc] init];
63                 [(ABUnknownPersonViewController *)self setAllowsActions:YES];
64                 [(ABUnknownPersonViewController *)self setAllowsAddingToAddressBook:YES];
65                 [(ABUnknownPersonViewController *)self setDisplayedPerson:person];
66         }
67         
68         if (matches) CFRelease(matches);
69         
70         return self;
71 }
72
73 @end