changed git call from https to git readonly
[atutor.git] / mods / atsocial_iphone_app / Classes / ATutorHelper.m
1 //
2 //  ATutorHelper.m
3 //  ATutor
4 //
5 //  Created by Quang Anh Do on 07/06/2010.
6 //  Copyright 2010 Quang Anh Do. All rights reserved.
7 //
8
9 #import "ATutorHelper.h"
10 #import "ATutorAppDelegate.h"
11 #import "OARequestParameter.h"
12 #import "CommonFunctions.h"
13 #import "OAServiceTicket.h"
14 #import "NSDictionary_JSONExtensions.h"
15 #import "Contact.h"
16
17 @interface ATutorHelper (Private) 
18
19 - (void)peopleCallback:(OAServiceTicket *)ticket didFinishWithResponse:(id)response;
20 - (void)personCallback:(OAServiceTicket *)ticket didFinishWithResponse:(id)response;
21 - (NSDictionary *)matchDisplayNameWithId:(NSArray *)data;
22
23 @end
24
25
26 @implementation ATutorHelper
27
28 @synthesize consumer;
29 @synthesize numberOfContacts;
30 @synthesize contacts;
31 @synthesize contactMapping;
32 @synthesize delegate;
33
34 - (void)dealloc {
35         [consumer dealloc];
36         [contacts dealloc];
37         [contactMapping dealloc];
38         [delegate release];
39         
40         [super dealloc];
41 }
42
43 - (id)init {
44         if (self = [super init]) {
45                 self.consumer = [(ATutorAppDelegate *)[[UIApplication sharedApplication] delegate] consumer];
46                 self.numberOfContacts = 0;
47                 self.contacts = [[NSMutableArray alloc] init];
48                 self.contactMapping = [[NSMutableArray alloc] init];
49         }
50         
51         return self;
52 }
53
54 - (void)fetchContactList {
55         NSLog(@"=-=-=-=-=-=-=-=-Fetching contact list-=-=-=-=-=-=-=-=");
56         
57         [consumer getDataForUrl:@"/people/@me/@contacts" 
58                           andParameters:[NSArray arrayWithObjects:[OARequestParameter requestParameterWithName:@"count" value:@"100"], 
59                                                          [OARequestParameter requestParameterWithName:@"startIndex" value:[NSString stringWithFormat:@"%d", numberOfContacts]], 
60                                                          [OARequestParameter requestParameterWithName:@"sortBy" value:@"displayName"],
61                                                          nil] 
62                                    delegate:self 
63                   didFinishSelector:@selector(peopleCallback:didFinishWithResponse:)];  
64 }
65
66 - (void)fetchOwnProfile {
67         NSLog(@"=-=-=-=-=-=-=-=-Fetching own profile-=-=-=-=-=-=-=-=");
68         
69         [consumer getDataForUrl:@"/people/@me/@self" 
70                           andParameters:nil
71                                    delegate:self
72                   didFinishSelector:@selector(personCallback:didFinishWithResponse:)];
73 }
74
75 - (void)peopleCallback:(OAServiceTicket *)ticket didFinishWithResponse:(id)response {
76         if (ticket.didSucceed) {
77                 NSError *error = nil;
78                 NSDictionary *data = [NSDictionary dictionaryWithJSONData:[response dataUsingEncoding:NSUTF8StringEncoding] error:&error];
79                 NSArray *entries = [data objectForKey:@"entry"];
80                 
81                 // Mapping
82                 [contactMapping addObjectsFromArray:entries];
83                 numberOfContacts += [entries count];
84                 
85                 for (NSDictionary *entry in entries) {
86                         [contacts addObject:[Contact contactWithDictionary:entry]];
87                 }
88                 
89                 // Continue fetching or not?
90                 if (numberOfContacts < [[data objectForKey:@"totalResults"] intValue]) { // Fetch friend
91                         [self fetchContactList];
92                 } else { // Fetch own profile
93                         [self fetchOwnProfile];
94                 } 
95         } else {
96                 alertMessage(@"Error", @"Unable to fetch your contact list");
97         }
98 }
99
100 - (void)personCallback:(OAServiceTicket *)ticket didFinishWithResponse:(id)response {
101         if (ticket.didSucceed) {
102                 NSError *error = nil;
103                 NSDictionary *data = [NSDictionary dictionaryWithJSONData:[response dataUsingEncoding:NSUTF8StringEncoding] error:&error];
104                 NSDictionary *entry = [data objectForKey:@"entry"];
105                 
106                 // Mapping
107                 [contactMapping addObject:entry];
108                 numberOfContacts++;
109                 
110                 [contacts addObject:[Contact contactWithDictionary:entry]];     
111                 
112                 // Wrap things up
113                 NSLog(@"Archiving contact list");
114                 
115                 [NSKeyedArchiver archiveRootObject:[self matchDisplayNameWithId:contactMapping]
116                                                                         toFile:[applicationDocumentsDirectory() stringByAppendingPathComponent:@"contact_mapping.plist"]];
117                 
118                 [NSKeyedArchiver archiveRootObject:contacts 
119                                                                         toFile:[applicationDocumentsDirectory() stringByAppendingPathComponent:@"contacts.plist"]];
120                         
121                 // Good to go
122                 if (delegate && [delegate respondsToSelector:@selector(doneFetchingContactList)]) {
123                         [delegate performSelector:@selector(doneFetchingContactList)];
124                 }
125         } else {
126                 alertMessage(@"Error", @"Unable to fetch your profile");
127         }       
128 }
129
130 - (NSDictionary *)matchDisplayNameWithId:(NSArray *)data {
131         NSMutableDictionary *retVal = [[NSMutableDictionary alloc] init];
132         
133         for (NSDictionary *contact in data) {
134                 [retVal setObject:[contact objectForKey:@"displayName"] 
135                                    forKey:[contact objectForKey:@"id"]];
136         }
137         
138         return [retVal autorelease];
139 }
140
141 @end