Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions NSDictionary+TumblrKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
// THE SOFTWARE.
//

#import <Foundation/Foundation.h>
#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
#import <UIKit/UIKit.h>
#else
#import <Cocoa/Cocoa.h>
#endif

/**
TumblrKit's additions to NSDictionary.
Expand Down
1 change: 1 addition & 0 deletions NSDictionary+TumblrKit.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ - (NSData *)multipartMIMEData
}

[result appendData:[[NSString stringWithFormat:@"--%@--\r\n", [NSString MIMEBoundary]] dataUsingEncoding:NSUTF8StringEncoding]];
[dict_ release];

return result;
}
Expand Down
11 changes: 11 additions & 0 deletions TKPost.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,26 @@ typedef enum
{
NSMutableString *caption;
NSString *source;

#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
UIImage *image;
#else
NSImage *image;
#endif

NSUInteger width;
NSUInteger height;
}

@property (assign) NSUInteger width;
@property (assign) NSUInteger height;
@property (copy) NSString *source;

#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
@property (retain) UIImage *image;
#else
@property (retain) NSImage *image;
#endif

- (NSString *)caption;
- (void)setCaption:(NSString *)aCaption;
Expand Down
35 changes: 20 additions & 15 deletions TKPost.m
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ + (id)postWithAttributes:(NSDictionary *)attributeDict
}
}

return [(TKPost *)[postClass alloc] initWithAttributes:attributeDict];
return [[(TKPost *)[postClass alloc] initWithAttributes:attributeDict] autorelease];
}

- (NSString *)typeAsString
Expand Down Expand Up @@ -179,7 +179,7 @@ - (void)dealloc

- (NSString *)title
{
return [title copy];
return [[title copy] autorelease];
}

- (void)setTitle:(NSString *)aTitle
Expand All @@ -192,7 +192,7 @@ - (void)setTitle:(NSString *)aTitle

- (NSString *)body
{
return [body copy];
return [[body copy] autorelease];
}

- (void)setBody:(NSString *)aBody
Expand Down Expand Up @@ -252,7 +252,7 @@ - (void)dealloc

- (NSString *)text
{
return [text copy];
return [[text copy] autorelease];
}

- (void)setText:(NSString *)aText
Expand Down Expand Up @@ -311,7 +311,7 @@ - (void)dealloc

- (NSString *)text
{
return [text copy];
return [[text copy] autorelease];
}

- (void)setText:(NSString *)aText
Expand All @@ -324,7 +324,7 @@ - (void)setText:(NSString *)aText

- (NSString *)source
{
return [source copy];
return [[source copy] autorelease];
}

- (void)setSource:(NSString *)aSource
Expand Down Expand Up @@ -380,7 +380,7 @@ - (void)dealloc

- (NSString *)text
{
return [text copy];
return [[text copy] autorelease];
}

- (void)setText:(NSString *)aText
Expand Down Expand Up @@ -449,7 +449,7 @@ - (void)dealloc

- (NSString *)caption
{
return [caption copy];
return [[caption copy] autorelease];
}

- (void)setCaption:(NSString *)aCaption
Expand All @@ -476,8 +476,13 @@ - (NSDictionary *)attributesAsDictionary
if (source != nil)
[dict setObject:source forKey:@"source"];
if (image != nil && source == nil) {
NSBitmapImageRep *bitmap = [[image representations] objectAtIndex:0];
[dict setObject:[bitmap representationUsingType:NSJPEGFileType properties:nil] forKey:@"data"];
#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
NSData *data = UIImageJPEGRepresentation(image, 1.0);
[dict setObject:data forKey:@"data"];
#else
NSBitmapImageRep *bitmap = [[image representations] objectAtIndex:0];
[dict setObject:[bitmap representationUsingType:NSJPEGFileType properties:nil] forKey:@"data"];
#endif
}
[dict setObject:caption forKey:@"caption"];

Expand Down Expand Up @@ -510,7 +515,7 @@ - (void)dealloc

- (NSString *)caption
{
return [caption copy];
return [[caption copy] autorelease];
}

- (void)setCaption:(NSString *)aCaption
Expand All @@ -523,7 +528,7 @@ - (void)setCaption:(NSString *)aCaption

- (NSString *)source
{
return [source copy];
return [[source copy] autorelease];
}

- (void)setSource:(NSString *)aSource
Expand All @@ -536,7 +541,7 @@ - (void)setSource:(NSString *)aSource

- (NSString *)player
{
return [player copy];
return [[player copy] autorelease];
}

- (void)setPlayer:(NSString *)aPlayer
Expand Down Expand Up @@ -591,7 +596,7 @@ - (void)dealloc

- (NSString *)caption
{
return [caption copy];
return [[caption copy] autorelease];
}

- (void)setCaption:(NSString *)aCaption
Expand All @@ -604,7 +609,7 @@ - (void)setCaption:(NSString *)aCaption

- (NSString *)player
{
return [player copy];
return [[player copy] autorelease];
}

- (void)setPlayer:(NSString *)aPlayer
Expand Down
5 changes: 5 additions & 0 deletions TKTumblelog.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
// THE SOFTWARE.
//

#import <Foundation/Foundation.h>
#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
#import <UIKit/UIKit.h>
#else
#import <Cocoa/Cocoa.h>
#endif

typedef enum
{
Expand Down
14 changes: 12 additions & 2 deletions TKTumblr.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ typedef enum
- (void)tumblrDidUploadPost:(TKPost *)thePost withDomain:(NSString *)theDomain postID:(NSNumber *)thePostID;
- (void)tumblrDidFailToUploadPost:(TKPost *)thePost withDomain:(NSString *)theDomain returnCode:(TKTumblrResponseReturnCode)theReturnCode;
- (void)tumblrDidReceiveTumblelog:(TKTumblelog *)theTumblelog;

- (void)tumblrDidReturnTumblelogs:(NSArray *)tumblelogs;
- (void)tumblrUserAuthenticated;
- (void)tumblrUserNotAuthenticated;
@end

@interface TKTumblr : NSObject <NSXMLParserDelegate,TKTumblrDelegate>
Expand All @@ -51,26 +53,34 @@ typedef enum
NSString *email;
NSString *password;

NSMutableArray *usersTumblelogs;
TKTumblelog *currentTumblelog;
TKPost *currentPost;
TKPost *requestedPost;
NSString *currentElementName;

BOOL authenticationRequestInProgress;
BOOL userAuthenticated;
}

@property (assign) id<TKTumblrDelegate,NSObject> delegate;
@property (nonatomic,copy) NSString *email;
@property (nonatomic,copy) NSString *password;
@property (nonatomic, retain) NSMutableArray *usersTumblelogs;
@property (nonatomic,retain) TKTumblelog *currentTumblelog;
@property (nonatomic,retain) TKPost *currentPost;
@property (nonatomic,retain) TKPost *requestedPost;
@property (nonatomic,copy) NSString *currentElementName;
@property (assign) BOOL userAuthenticated;
@property (assign) BOOL authenticationRequestInProgress;

- (id)initWithEmail:(NSString *)theEmail andPassword:(NSString *)thePassword;
- (BOOL)uploadPost:(TKPost *)thePost;
- (BOOL)uploadPost:(TKPost *)thePost withDomain:(NSString *)theDomain;
- (void)postsWithReadRequest:(TKTumblrReadRequest *)theReadRequest;
- (TKPost *)postWithID:(NSNumber *)thePostID andDomain:(NSString *)theDomain;
- (NSDictionary *)attributesAsDictionary;
- (NSArray *)tumblelogs;
- (void)tumblelogs;
- (void)authenticate;

@end
58 changes: 55 additions & 3 deletions TKTumblr.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

@implementation TKTumblr

@synthesize delegate, email, password, currentPost, currentElementName, requestedPost, currentTumblelog;
@synthesize delegate, email, password, currentPost, currentElementName, requestedPost, currentTumblelog, usersTumblelogs, authenticationRequestInProgress, userAuthenticated;

- (id)initWithEmail:(NSString *)theEmail andPassword:(NSString *)thePassword
{
Expand All @@ -38,6 +38,7 @@ - (id)initWithEmail:(NSString *)theEmail andPassword:(NSString *)thePassword
self.currentPost = nil;
self.currentElementName = nil;
self.requestedPost = nil;
self.usersTumblelogs = [[[NSMutableArray alloc] init] autorelease];
}
return self;
}
Expand All @@ -50,6 +51,7 @@ - (void)dealloc
self.currentPost = nil;
self.currentElementName = nil;
self.requestedPost = nil;
self.usersTumblelogs = nil;
[super dealloc];
}

Expand Down Expand Up @@ -154,7 +156,7 @@ - (NSDictionary *)attributesAsDictionary
return dict;
}

- (NSArray *)tumblelogs
- (void)tumblelogs
{
NSString *theURLString = [NSString stringWithFormat:@"https://www.tumblr.com/api/authenticate?email=%@&password=%@",
[email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
Expand All @@ -164,15 +166,31 @@ - (NSArray *)tumblelogs
[parser setDelegate:self];
[parser parse];
[parser release];
}

return nil;
- (void)authenticate {
NSString *theURLString = [NSString stringWithFormat:@"https://www.tumblr.com/api/authenticate?email=%@&password=%@",
[email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[password stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURL *theURL = [NSURL URLWithString:theURLString];
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:theURL];
self.authenticationRequestInProgress = YES;
[parser setDelegate:self];
[parser parse];
[parser release];
}

#pragma mark NSXMLParserDelegate

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
self.currentElementName = elementName;

if ([elementName isEqualToString:@"user"] && authenticationRequestInProgress) {
if (authenticationRequestInProgress) {
self.userAuthenticated = YES;
}
}

if ([elementName isEqualToString:@"post"]) {
self.currentPost = [TKPost postWithAttributes:attributeDict];
Expand All @@ -184,6 +202,12 @@ - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName nam

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"user"] && authenticationRequestInProgress && userAuthenticated) {
if (delegate && [delegate respondsToSelector:@selector(tumblrUserAuthenticated)]) {
[delegate tumblrUserAuthenticated];
}
}

if ([elementName isEqualToString:@"post"]) {
if (currentPost != nil) {
if (delegate && [delegate respondsToSelector:@selector(tumblrDidReceivePost:withDomain:)]) {
Expand All @@ -197,6 +221,7 @@ - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName names
if (delegate && [delegate respondsToSelector:@selector(tumblrDidReceiveTumblelog:)]) {
[delegate tumblrDidReceiveTumblelog:currentTumblelog];
}
[self.usersTumblelogs addObject:currentTumblelog];
}
self.currentTumblelog = nil;
}
Expand All @@ -223,6 +248,33 @@ - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
}
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
if (authenticationRequestInProgress) {
self.userAuthenticated = NO;
if (delegate && [delegate respondsToSelector:@selector(tumblrUserNotAuthenticated)]) {
[delegate tumblrUserNotAuthenticated];
}
}
}

- (void)parserDidStartDocument:(NSXMLParser *)parser {
[self.usersTumblelogs removeAllObjects];
self.userAuthenticated = NO;
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
if ([self.usersTumblelogs count]>0) {
if (delegate && [delegate respondsToSelector:@selector(tumblrDidReturnTumblelogs:)]) {
[delegate tumblrDidReturnTumblelogs:self.usersTumblelogs];
[self.usersTumblelogs removeAllObjects];
}
}

if (authenticationRequestInProgress) {
authenticationRequestInProgress = NO;
}
}

#pragma mark TKTumblrDelegate

- (void)tumblrDidReceivePost:(TKPost *)thePost withDomain:(NSString *)theDomain
Expand Down