| 1 | // |
|---|
| 2 | // TypePadWeblog.m |
|---|
| 3 | // Fence |
|---|
| 4 | // |
|---|
| 5 | // Created by Nicholas Gerakines on 5/15/06. |
|---|
| 6 | // Copyright 2006 __MyCompanyName__. All rights reserved. |
|---|
| 7 | // |
|---|
| 8 | |
|---|
| 9 | #import "TypePadWeblog.h" |
|---|
| 10 | #import "TypePadAtomApi.h" |
|---|
| 11 | #import "TypePadWeblogComment.h" |
|---|
| 12 | |
|---|
| 13 | @implementation TypePadWeblog |
|---|
| 14 | |
|---|
| 15 | - (id) init { |
|---|
| 16 | if (self = [super init]) { |
|---|
| 17 | _categories = [[NSMutableDictionary alloc] init]; |
|---|
| 18 | [self setComments:[[NSMutableArray alloc] init]]; |
|---|
| 19 | _WeblogTitle = [[NSString alloc] init]; |
|---|
| 20 | _WeblogID = [[NSString alloc] init]; |
|---|
| 21 | _WeblogURL = [[NSString alloc] init]; |
|---|
| 22 | [self retain]; |
|---|
| 23 | } |
|---|
| 24 | return self; |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | /* dealloc */ |
|---|
| 28 | - (void) dealloc { |
|---|
| 29 | [_WeblogTitle release]; |
|---|
| 30 | [_WeblogID release]; |
|---|
| 31 | [_WeblogURL release]; |
|---|
| 32 | [_categories release]; |
|---|
| 33 | [comments release]; |
|---|
| 34 | [authuser release]; |
|---|
| 35 | [authpass release]; |
|---|
| 36 | |
|---|
| 37 | _WeblogTitle = nil; |
|---|
| 38 | _WeblogID = nil; |
|---|
| 39 | _WeblogURL = nil; |
|---|
| 40 | _categories = nil; |
|---|
| 41 | comments = nil; |
|---|
| 42 | authuser = nil; |
|---|
| 43 | authpass = nil; |
|---|
| 44 | [super dealloc]; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | - (void) encodeWithCoder: (NSCoder *)coder { |
|---|
| 48 | [coder encodeObject: [self weblogTitle] forKey: @"WeblogWeblogTitle"]; |
|---|
| 49 | [coder encodeObject: [self weblogID] forKey: @"WeblogWeblogID"]; |
|---|
| 50 | [coder encodeObject: [self weblogURL] forKey: @"WeblogWeblogURL"]; |
|---|
| 51 | [coder encodeObject: [self categories] forKey: @"WeblogCategories"]; |
|---|
| 52 | [coder encodeObject: [self comments] forKey: @"WeblogComments"]; |
|---|
| 53 | [coder encodeObject: [self authuser] forKey: @"WeblogAuthuser"]; |
|---|
| 54 | [coder encodeObject: [self authpass] forKey: @"WeblogAuthpass"]; |
|---|
| 55 | [coder encodeBool: [self requiresAuth] forKey: @"WeblogRequiresAuth"]; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | - (id) initWithCoder: (NSCoder *)coder { |
|---|
| 59 | [self setWeblogTitle: [coder decodeObjectForKey: @"WeblogWeblogTitle"]]; |
|---|
| 60 | [self setWeblogID: [coder decodeObjectForKey: @"WeblogWeblogID"]]; |
|---|
| 61 | [self setWeblogURL: [coder decodeObjectForKey: @"WeblogWeblogURL"]]; |
|---|
| 62 | [self setCategories: [coder decodeObjectForKey: @"WeblogCategories"]]; |
|---|
| 63 | [self setComments: [coder decodeObjectForKey: @"WeblogComments"]]; |
|---|
| 64 | [self setAuthuser: [coder decodeObjectForKey: @"WeblogAuthuser"]]; |
|---|
| 65 | [self setAuthpass: [coder decodeObjectForKey: @"WeblogAuthpass"]]; |
|---|
| 66 | [self setRequiresAuth: [coder decodeBoolForKey: @"WeblogRequiresAuth"]]; |
|---|
| 67 | return self; |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | /* weblogTitle */ |
|---|
| 71 | - (NSString *) weblogTitle { return _WeblogTitle; } |
|---|
| 72 | |
|---|
| 73 | /* -setWeblogTitle: */ |
|---|
| 74 | - (void) setWeblogTitle: (NSString *) WeblogTitle { |
|---|
| 75 | //NSLog(@"in -setWeblogTitle:, old value of _WeblogTitle: %@, changed to: %@", _WeblogTitle, WeblogTitle); |
|---|
| 76 | |
|---|
| 77 | if (_WeblogTitle != WeblogTitle) { |
|---|
| 78 | [_WeblogTitle autorelease]; |
|---|
| 79 | _WeblogTitle = [WeblogTitle retain]; |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | /* weblogID */ |
|---|
| 84 | - (NSString *) weblogID { return _WeblogID; } |
|---|
| 85 | |
|---|
| 86 | /* -setWeblogID: */ |
|---|
| 87 | - (void) setWeblogID: (NSString *) WeblogID { |
|---|
| 88 | //NSLog(@"in -setWeblogID:, old value of _WeblogID: %@, changed to: %@", _WeblogID, WeblogID); |
|---|
| 89 | |
|---|
| 90 | if (_WeblogID != WeblogID) { |
|---|
| 91 | [_WeblogID autorelease]; |
|---|
| 92 | _WeblogID = [WeblogID retain]; |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | /* weblogURL */ |
|---|
| 97 | - (NSString *) weblogURL { return _WeblogURL; } |
|---|
| 98 | |
|---|
| 99 | /* -setWeblogURL: */ |
|---|
| 100 | - (void) setWeblogURL: (NSString *) WeblogURL { |
|---|
| 101 | //NSLog(@"in -setWeblogURL:, old value of _WeblogURL: %@, changed to: %@", _WeblogURL, WeblogURL); |
|---|
| 102 | if (_WeblogURL != WeblogURL) { |
|---|
| 103 | [_WeblogURL autorelease]; |
|---|
| 104 | _WeblogURL = [WeblogURL retain]; |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | /* categories */ |
|---|
| 109 | - (NSMutableDictionary *) categories { return _categories; } |
|---|
| 110 | |
|---|
| 111 | /* -setCategories: */ |
|---|
| 112 | - (void) setCategories: (NSMutableDictionary *) Categories { |
|---|
| 113 | //NSLog(@"in -setCategories:, old value of _categories: %@, changed to: %@", _categories, Categories); |
|---|
| 114 | if (_categories != Categories) { |
|---|
| 115 | [_categories autorelease]; |
|---|
| 116 | _categories = [Categories retain]; |
|---|
| 117 | } |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | /* comments */ |
|---|
| 121 | - (NSMutableArray *) comments { return comments; } |
|---|
| 122 | |
|---|
| 123 | /* -setComments: */ |
|---|
| 124 | - (void) setComments: (NSMutableArray *) Comments { |
|---|
| 125 | //NSLog(@"in -setComments:, old value of comments: %@, changed to: %@", comments, Comments); |
|---|
| 126 | if (comments != Comments) { |
|---|
| 127 | [comments autorelease]; |
|---|
| 128 | comments = [Comments retain]; |
|---|
| 129 | } |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | /* authuser */ |
|---|
| 134 | - (NSString *) authuser { return authuser; } |
|---|
| 135 | |
|---|
| 136 | /* -setAuthuser: */ |
|---|
| 137 | - (void) setAuthuser: (NSString *) Authuser { |
|---|
| 138 | //NSLog(@"in -setAuthuser:, old value of authuser: %@, changed to: %@", authuser, Authuser); |
|---|
| 139 | [self setRequiresAuth:YES]; |
|---|
| 140 | if (authuser != Authuser) { |
|---|
| 141 | [authuser autorelease]; |
|---|
| 142 | authuser = [Authuser retain]; |
|---|
| 143 | } |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | /* authpass */ |
|---|
| 147 | - (NSString *) authpass { return authpass; } |
|---|
| 148 | |
|---|
| 149 | /* -setAuthpass: */ |
|---|
| 150 | - (void) setAuthpass: (NSString *) Authpass { |
|---|
| 151 | [self setRequiresAuth:YES]; |
|---|
| 152 | //NSLog(@"in -setAuthpass:, old value of authpass: %@, changed to: %@", authpass, Authpass); |
|---|
| 153 | if (authpass != Authpass) { |
|---|
| 154 | [authpass autorelease]; |
|---|
| 155 | authpass = [Authpass retain]; |
|---|
| 156 | } |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | - (void) setAuth: (NSString *) user pass:(NSString *) pass { |
|---|
| 160 | [self setAuthuser:user]; |
|---|
| 161 | [self setAuthpass:pass]; |
|---|
| 162 | [self setRequiresAuth:YES]; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | /* requiresAuth */ |
|---|
| 166 | - (BOOL) requiresAuth { return requiresAuth; } |
|---|
| 167 | |
|---|
| 168 | /* -setRequiresAuth: */ |
|---|
| 169 | - (void) setRequiresAuth: (BOOL) flag { |
|---|
| 170 | //NSLog(@"in -setRequiresAuth, old value of requiresAuth: %@, changed to: %@", (requiresAuth ? @"YES": @"NO"), (flag ? @"YES": @"NO") ); |
|---|
| 171 | requiresAuth = flag; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | /// |
|---|
| 175 | - (void) discoverCategories { |
|---|
| 176 | TypePadAtomAPI *client = [[[TypePadAtomAPI alloc] init] autorelease]; |
|---|
| 177 | [client setAuth:[self authuser] pass:[self authpass]]; |
|---|
| 178 | [client setDataarg:@""]; |
|---|
| 179 | [client newpost:@"4" args:[NSDictionary dictionaryWithObjectsAndKeys: [self weblogID], @"blog_id", nil]]; |
|---|
| 180 | NSXMLDocument *xmlDoc = [[NSXMLDocument alloc] initWithXMLString:[client resdata] options:(NSXMLNodePreserveWhitespace|NSXMLNodePreserveCDATA) error:nil]; |
|---|
| 181 | NSArray *nodes = [xmlDoc nodesForXPath:@"/feed/entry" error:nil]; |
|---|
| 182 | int arrayCount = [nodes count]; |
|---|
| 183 | int i = 0; |
|---|
| 184 | for (i = 0; i < arrayCount; i++) { |
|---|
| 185 | NSXMLElement *token = [nodes objectAtIndex:i]; |
|---|
| 186 | [_categories setObject: [self parse_catid:[[[token nodesForXPath:@"id" error:nil] objectAtIndex:0] stringValue]] forKey:[[[token nodesForXPath:@"title" error:nil] objectAtIndex:0] stringValue]]; |
|---|
| 187 | } |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | - (void) getComments { |
|---|
| 191 | TypePadAtomAPI *client = [[[TypePadAtomAPI alloc] init] autorelease]; |
|---|
| 192 | [client setDataarg:@""]; |
|---|
| 193 | [client setAuth:[self authuser] pass:[self authpass]]; |
|---|
| 194 | [client newpost:@"6" args:[NSDictionary dictionaryWithObjectsAndKeys: [self weblogID], @"blog_id", nil]]; |
|---|
| 195 | NSXMLDocument *xmlDoc = [[NSXMLDocument alloc] initWithXMLString:[client resdata] options:(NSXMLNodePreserveWhitespace|NSXMLNodePreserveCDATA) error:nil]; |
|---|
| 196 | NSArray *nodes = [xmlDoc nodesForXPath:@"/feed/entry" error:nil]; |
|---|
| 197 | int arrayCount = [nodes count]; |
|---|
| 198 | int i = 0; |
|---|
| 199 | for (i = 0; i < arrayCount; i++) { |
|---|
| 200 | NSXMLElement *token = [nodes objectAtIndex:i]; |
|---|
| 201 | TypePadWeblogComment *tpobj = [[TypePadWeblogComment alloc] init]; |
|---|
| 202 | [tpobj setCommentid:[self parse_commentid:[[[token nodesForXPath:@"id" error:nil] objectAtIndex:0] stringValue]]]; |
|---|
| 203 | [tpobj setText:[[[token nodesForXPath:@"content" error:nil] objectAtIndex:0] stringValue]]; |
|---|
| 204 | [comments addObject:tpobj]; |
|---|
| 205 | NSLog(@"Id '%@ - %@\n", [tpobj commentid], [tpobj text]); |
|---|
| 206 | } |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | - (NSString *) parse_catid: (NSString *) tmpstr { |
|---|
| 210 | NSArray *lines = [tmpstr componentsSeparatedByString:@"-"]; |
|---|
| 211 | return [lines objectAtIndex:1]; |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | - (NSString *) parse_commentid: (NSString *) tmpstr { |
|---|
| 215 | NSArray *lines = [tmpstr componentsSeparatedByString:@"omment-"]; |
|---|
| 216 | return [lines objectAtIndex:1]; |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | // TODO -- remove this and replace it with something better |
|---|
| 220 | - (void) check_categories: (NSString *) categories { |
|---|
| 221 | NSArray *lines = [categories componentsSeparatedByString:@"-"]; |
|---|
| 222 | int i, count = [lines count]; |
|---|
| 223 | for (i = 0; i < count; i++) { |
|---|
| 224 | if ( [_categories objectForKey:[lines objectAtIndex:i]] == nil) { |
|---|
| 225 | // NSLog(@"Category '%@' does not exist!\n", [lines objectAtIndex:i]); |
|---|
| 226 | } |
|---|
| 227 | } |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | @end |
|---|