Changeset 35 for trunk/AtomApi.m

Show
Ignore:
Timestamp:
06/12/06 02:31:42 (3 years ago)
Author:
ngerakines
Message:

Fixing what I broke in the last checkin -- removed url drag support -- added VoxAccount and VoxCollection classes and gut code

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/AtomApi.m

    r34 r35  
    99#import "AtomApi.h" 
    1010#import "AtomExtensions.h" 
    11 // #import "NSImageAdditions.h" 
    12 // #import "NSStringAdditions.h" 
    1311#import "NSDataAdditions.h" 
    1412 
     
    2624                authuser = [[NSString alloc] init]; 
    2725                authpass = [[NSString alloc] init]; 
     26                [self setRequiresAuth:NO]; 
    2827                [self retain]; 
    2928    } 
     
    8887/* -setAuthuser: */ 
    8988- (void) setAuthuser: (NSString *) Authuser { 
    90     //NSLog(@"in -setAuthuser:, old value of authuser: %@, changed to: %@", authuser, Authuser); 
     89        //NSLog(@"in -setAuthuser:, old value of authuser: %@, changed to: %@", authuser, Authuser); 
     90        [self setRequiresAuth:YES]; 
    9191    if (authuser != Authuser) { 
    9292        [authuser autorelease]; 
     
    100100/* -setAuthpass: */ 
    101101- (void) setAuthpass: (NSString *) Authpass { 
     102        [self setRequiresAuth:YES]; 
    102103    //NSLog(@"in -setAuthpass:, old value of authpass: %@, changed to: %@", authpass, Authpass); 
    103104    if (authpass != Authpass) { 
     
    105106        authpass = [Authpass retain]; 
    106107    } 
     108} 
     109 
     110- (void) setAuth: (NSString *) user pass:(NSString *) pass { 
     111        [self setAuthuser:user]; 
     112        [self setAuthpass:pass]; 
     113        [self setRequiresAuth:YES]; 
     114} 
     115 
     116/* requiresAuth */ 
     117- (BOOL) requiresAuth { return requiresAuth; } 
     118 
     119/* -setRequiresAuth: */ 
     120- (void) setRequiresAuth: (BOOL) flag { 
     121    //NSLog(@"in -setRequiresAuth, old value of requiresAuth: %@, changed to: %@", (requiresAuth ? @"YES": @"NO"), (flag ? @"YES": @"NO") ); 
     122    requiresAuth = flag; 
    107123} 
    108124 
     
    133149 
    134150- (void) makerequest:(NSString *) reqtype { 
    135         NSString *login = [self authuser]; 
    136         NSString *password = [self authpass]; 
     151        NSLog(@"Making request to url '%@'\n", apiurl); 
    137152        NSURL *aURL = [NSURL URLWithString:apiurl]; 
    138153        [aURL URLHandleUsingCache:NO]; 
    139         // NSLog(@"username/password '%@' / '%@'\n", login, password); 
    140         // NSLog(@"Using api url '%@'\n", apiurl); 
    141154        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:aURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30]; 
    142         NSString *nonce = [self makeNonce]; 
    143         NSString *encodedNonce = [nonce stringUsingBase64Encoding]; 
    144         NSString *created = [[NSDate date] descriptionWithCalendarFormat:@"%Y-%m-%dT%H:%M:%SZ" timeZone:nil locale:nil]; 
    145         NSString *passwordDigest = [[[NSString stringWithFormat:@"%@%@%@", nonce, created, password] stringUsingSHA1RawHash] stringUsingBase64Encoding]; 
    146155        [request addValue:[aURL host] forHTTPHeaderField:@"Host"]; 
    147156        [request addValue:@"Fence/0.8" forHTTPHeaderField:@"User-Agent"]; 
    148         [request addValue:@"WSSE profile=\"UsernameToken\"" forHTTPHeaderField:@"Authorization"]; 
    149         NSString *wsse = [NSString stringWithFormat:@"UsernameToken Username=\"%@\", PasswordDigest=\"%@\", Created=\"%@\", Nonce=\"%@\"", login, passwordDigest, created, encodedNonce]; 
    150         // NSLog(@"X-WSSE set as '%@'\n", wsse); 
    151         [request addValue:wsse forHTTPHeaderField:@"X-WSSE"]; 
     157        // set auth if we need it. 
     158        if ([self requiresAuth]) { 
     159                NSLog(@" XXX Auth required\n"); 
     160                NSString *login = [self authuser]; 
     161                NSString *password = [self authpass]; 
     162                NSString *nonce = [self makeNonce]; 
     163                NSString *encodedNonce = [nonce stringUsingBase64Encoding]; 
     164                NSString *created = [[NSDate date] descriptionWithCalendarFormat:@"%Y-%m-%dT%H:%M:%SZ" timeZone:nil locale:nil]; 
     165                NSString *passwordDigest = [[[NSString stringWithFormat:@"%@%@%@", nonce, created, password] stringUsingSHA1RawHash] stringUsingBase64Encoding]; 
     166                [request addValue:@"WSSE profile=\"UsernameToken\"" forHTTPHeaderField:@"Authorization"]; 
     167                NSString *wsse = [NSString stringWithFormat:@"UsernameToken Username=\"%@\", PasswordDigest=\"%@\", Created=\"%@\", Nonce=\"%@\"", login, passwordDigest, created, encodedNonce]; 
     168                [request addValue:wsse forHTTPHeaderField:@"X-WSSE"]; 
     169        } 
    152170        [request addValue:@"application/atom+xml" forHTTPHeaderField:@"Content-Type"]; 
    153         /* NSLog(@"content-type set\n"); */ 
    154171        [request addValue:[NSString stringWithFormat:@"%d", [atomdata length]] forHTTPHeaderField: @"Content-Length"]; 
    155172        [request setHTTPMethod:reqtype]; 
    156         /* NSLog(@"request type set as %@\n", reqtype); */ 
    157173        [request setHTTPBody:[atomdata dataUsingEncoding:NSUTF8StringEncoding]]; 
    158         /* NSLog(@"body set\n"); */ 
    159174        // [request addValue:@"close" forHTTPHeaderField:@"Connection"]; 
    160175        NSURLResponse* response; 
     
    169184} 
    170185 
    171 // [self setfile:@"/path/to/file"]; 
    172 // [self newpost:@"gallery"]; 
    173 - (void) newpost:(NSString *) to args:(NSMutableDictionary *) args { 
    174         /* NSLog(@"-(void) newpost:(NSString *)to - called\n"); */ 
    175         NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; 
    176         NSString *reqtype; 
    177         if ([to isEqualToString:@"weblog"] || [to isEqualToString:@"0"]) { 
    178                 /* NSLog(@"set as weblog post type\n"); */ 
    179                 reqtype = @"POST"; 
    180                 [self setApiurl:[NSString stringWithFormat:@"%@weblog/blog_id=%@",@"http://www.typepad.com/t/atom/", [userDefaults objectForKey:@"selectedweblog"]]]; 
    181                 [self weblog_upload: dataarg]; 
    182         } 
    183         if ([to isEqualToString:@"gallery"] || [to isEqualToString:@"1"]) { 
    184                 /* NSLog(@"set as gallery post type\n"); */ 
    185                 reqtype = @"POST"; 
    186                 [self setApiurl:[NSString stringWithFormat:@"%@gallery/set_id=%@", @"http://www.typepad.com/t/atom/", [userDefaults objectForKey:@"selectedgallery"]]]; 
    187                 [self gallery_upload: dataarg]; 
    188         } 
    189         if ([to isEqualToString:@"quickpost"] || [to isEqualToString:@"2"]) { 
    190                 /* NSLog(@"set as gallery post type\n"); */ 
    191                 reqtype = @"POST"; 
    192                 [self setApiurl:[NSString stringWithFormat:@"%@weblog/blog_id=%@", @"http://www.typepad.com/t/atom/", [userDefaults objectForKey:@"selectedweblog"]]]; 
    193                 [self weblog_newpost:@"Quick Post" content:dataarg]; 
    194         } 
    195         if ([to isEqualToString:@"weblogdiscovery"] || [to isEqualToString:@"3"]) { 
    196                 /* NSLog(@"set as gallery post type\n"); */ 
    197                 reqtype = @"GET"; 
    198                 [self setApiurl:[NSString stringWithFormat:@"%@weblog", @"http://www.typepad.com/t/atom/"]]; 
    199                 [self weblog_discover]; 
    200         }  
    201         if ([to isEqualToString:@"categorydiscovery"] || [to isEqualToString:@"4"]) { 
    202                 /* NSLog(@"set as gallery post type\n"); */ 
    203                 reqtype = @"GET"; 
    204                 [self setApiurl:[NSString stringWithFormat:@"%@categories/blog_id=%@", @"http://www.typepad.com/t/atom/", [args objectForKey:@"blog_id"] ]]; 
    205                 [self weblog_discover]; 
    206         } 
    207         if ([to isEqualToString:@"gallerydiscovery"] || [to isEqualToString:@"5"]) { 
    208                 /* NSLog(@"set as gallery post type\n"); */ 
    209                 reqtype = @"GET"; 
    210                 [self setApiurl:[NSString stringWithFormat:@"%@gallery", @"http://www.typepad.com/t/atom/"]]; 
    211                 [self weblog_discover]; 
    212         }  
    213         /* NSLog(@"Making request with request type %@\n", reqtype); */ 
    214         [self makerequest:reqtype]; 
    215 } 
    216  
    217 - (NSString *) gallery_upload: (NSString *) file { 
    218         /* NSLog(@"- (NSString *) gallery_newitem - called"); */ 
    219         NSData *data = [NSData dataWithContentsOfFile:file]; 
    220         NSString *encodedFile = [data base64Encoding]; 
    221         atomdata = [NSString stringWithFormat:@"<entry xmlns=\"http://purl.org/atom/ns#\" xmlns:photo=\"http://sixapart.com/atom/photo#\"><title>%@</title><summary></summary><content mode=\"base64\" type=\"image/%@\">%@</content></entry>", [file lastPathComponent], [file pathExtension], encodedFile]; 
    222         return atomdata; 
    223 } 
    224  
    225 - (NSString *) weblog_upload: (NSString *) file { 
    226         /* NSLog(@"- (NSString *) weblog_newitem - called"); */ 
    227         /* NSLog(@"Creating atom feed based on upload file %@\n", uploadfile); */ 
    228         NSData *data = [NSData dataWithContentsOfFile:file]; 
    229         NSString *encodedFile = [data base64Encoding]; 
    230         atomdata = [NSString stringWithFormat:@"<entry xmlns=\"http://purl.org/atom/ns#\" xmlns:photo=\"http://sixapart.com/atom/photo#\"><title>Picture Post</title><summary></summary><photo:filename>%@</photo:filename><content mode=\"base64\" type=\"image/%@\">%@</content></entry>",[file lastPathComponent], [file pathExtension], encodedFile]; 
    231         /* NSLog(@"Atom feed:\n%@\n", atomdata); */ 
    232         return atomdata; 
    233 } 
    234  
    235 - (NSString *) weblog_newpost: (NSString *) title content: (NSString *) content { 
    236         /* NSLog(@"- (NSString *) weblog_newitem - called"); */ 
    237         /* NSLog(@"Creating atom feed based on upload file %@\n", uploadfile); */ 
    238         atomdata = [NSString stringWithFormat:@"<entry xmlns=\"http://purl.org/atom/ns#\"><title>%@</title><summary></summary><content mode=\"xml\"><div xmlns=\"http://www.w3.org/1999/xhtml\">%@</div></content>%@</entry>", title, content, [self weblog_postcategories]]; 
    239         /* NSLog(@"Atom feed:\n%@\n", atomdata); */ 
    240         return atomdata; 
    241 } 
    242  
    243 - (NSString *) weblog_discover { 
    244         /* NSLog(@"- (NSString *) weblog_newitem - called"); */ 
    245         /* NSLog(@"Creating atom feed based on upload file %@\n", uploadfile); */ 
    246         /* atomdata = [NSString stringWithFormat:@"<entry xmlns=\"http://purl.org/atom/ns#\"><title>%@</title><summary></summary><content mode=\"xml\"><div xmlns=\"http://www.w3.org/1999/xhtml\">%@</div></content></entry>", title, content]; */ 
    247         atomdata = @""; 
    248         /* NSLog(@"Atom feed:\n%@\n", atomdata); */ 
    249         return @""; 
    250 } 
    251  
    252 - (NSString *) weblog_postcategories { 
    253         NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; 
    254         NSArray *lines = [[userDefaults objectForKey:@"blogcategories"] componentsSeparatedByString:@","]; 
    255         int i, count = [lines count]; 
    256         NSString *output = @""; 
    257         for (i = 0; i < count; i++) { 
    258                 [output stringByAppendingString:[NSString stringWithFormat:@"<category scheme=\"http://sixapart.com/ns/category#\" term\"%@\" />", [lines objectAtIndex:i]]]; 
    259         } 
    260         return output; 
    261 } 
    262  
    263  
    264186@end