Skip to content

Commit

Permalink
iOS 5 compatibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelclay committed Oct 15, 2011
1 parent 298f05e commit bcdbf2d
Show file tree
Hide file tree
Showing 18 changed files with 218 additions and 249 deletions.
2 changes: 1 addition & 1 deletion media/iphone/ASI/ASIAuthenticationDialog.m
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ - (void)orientationChanged:(NSNotification *)notification
{
[self showTitle];

UIInterfaceOrientation o = [[UIApplication sharedApplication] statusBarOrientation];
UIInterfaceOrientation o = (UIInterfaceOrientation)[[UIApplication sharedApplication] statusBarOrientation];
CGFloat angle = 0;
switch (o) {
case UIDeviceOrientationLandscapeLeft: angle = 90; break;
Expand Down
6 changes: 3 additions & 3 deletions media/iphone/ASI/ASIDataCompressor.m
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ - (NSData *)compressBytes:(Bytef *)bytes length:(NSUInteger)length error:(NSErro
[outputData increaseLengthBy:halfLength];
}

zStream.next_out = [outputData mutableBytes] + zStream.total_out-bytesProcessedAlready;
zStream.next_out = (Bytef*)[outputData mutableBytes] + zStream.total_out-bytesProcessedAlready;
zStream.avail_out = (unsigned int)([outputData length] - (zStream.total_out-bytesProcessedAlready));
status = deflate(&zStream, shouldFinish ? Z_FINISH : Z_NO_FLUSH);

Expand Down Expand Up @@ -184,12 +184,12 @@ + (BOOL)compressDataFromFile:(NSString *)sourcePath toFile:(NSString *)destinati
}

// Write the deflated data out to the destination file
[outputStream write:[outputData bytes] maxLength:[outputData length]];
[outputStream write:(const uint8_t *)[outputData bytes] maxLength:[outputData length]];

// Make sure nothing went wrong
if ([inputStream streamStatus] == NSStreamEventErrorOccurred) {
if (err) {
*err = [NSError errorWithDomain:NetworkRequestErrorDomain code:ASICompressionError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Compression of %@ failed because we were unable to write to the destination data file at &@",sourcePath,destinationPath],NSLocalizedDescriptionKey,[outputStream streamError],NSUnderlyingErrorKey,nil]];
*err = [NSError errorWithDomain:NetworkRequestErrorDomain code:ASICompressionError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Compression of %@ failed because we were unable to write to the destination data file at %@",sourcePath,destinationPath],NSLocalizedDescriptionKey,[outputStream streamError],NSUnderlyingErrorKey,nil]];
}
[compressor closeStream];
return NO;
Expand Down
6 changes: 3 additions & 3 deletions media/iphone/ASI/ASIDataDecompressor.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ - (NSData *)uncompressBytes:(Bytef *)bytes length:(NSUInteger)length error:(NSEr
[outputData increaseLengthBy:halfLength];
}

zStream.next_out = [outputData mutableBytes] + zStream.total_out-bytesProcessedAlready;
zStream.next_out = (Bytef*)[outputData mutableBytes] + zStream.total_out-bytesProcessedAlready;
zStream.avail_out = (unsigned int)([outputData length] - (zStream.total_out-bytesProcessedAlready));

status = inflate(&zStream, Z_NO_FLUSH);
Expand Down Expand Up @@ -181,12 +181,12 @@ + (BOOL)uncompressDataFromFile:(NSString *)sourcePath toFile:(NSString *)destina
}

// Write the inflated data out to the destination file
[outputStream write:[outputData bytes] maxLength:[outputData length]];
[outputStream write:(Bytef*)[outputData bytes] maxLength:[outputData length]];

// Make sure nothing went wrong
if ([inputStream streamStatus] == NSStreamEventErrorOccurred) {
if (err) {
*err = [NSError errorWithDomain:NetworkRequestErrorDomain code:ASICompressionError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Decompression of %@ failed because we were unable to write to the destination data file at &@",sourcePath,destinationPath],NSLocalizedDescriptionKey,[outputStream streamError],NSUnderlyingErrorKey,nil]];
*err = [NSError errorWithDomain:NetworkRequestErrorDomain code:ASICompressionError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Decompression of %@ failed because we were unable to write to the destination data file at %@",sourcePath,destinationPath],NSLocalizedDescriptionKey,[outputStream streamError],NSUnderlyingErrorKey,nil]];
}
[decompressor closeStream];
return NO;
Expand Down
4 changes: 4 additions & 0 deletions media/iphone/ASI/ASIDownloadCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
// A helper function that determines if the server has requested data should not be cached by looking at the request's response headers
+ (BOOL)serverAllowsResponseCachingForRequest:(ASIHTTPRequest *)request;

// A list of file extensions that we know won't be readable by a webview when accessed locally
// If we're asking for a path to cache a particular url and it has one of these extensions, we change it to '.html'
+ (NSArray *)fileExtensionsToHandleAsHTML;

@property (assign, nonatomic) ASICachePolicy defaultCachePolicy;
@property (retain, nonatomic) NSString *storagePath;
@property (retain) NSRecursiveLock *accessLock;
Expand Down
61 changes: 32 additions & 29 deletions media/iphone/ASI/ASIDownloadCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

static NSString *sessionCacheFolder = @"SessionStore";
static NSString *permanentCacheFolder = @"PermanentStore";
static NSArray *fileExtensionsToHandleAsHTML = nil;

@interface ASIDownloadCache ()
+ (NSString *)keyForURL:(NSURL *)url;
Expand All @@ -22,6 +23,15 @@ - (NSString *)pathToFile:(NSString *)file;

@implementation ASIDownloadCache

+ (void)initialize
{
if (self == [ASIDownloadCache class]) {
// Obviously this is not an exhaustive list, but hopefully these are the most commonly used and this will 'just work' for the widest range of people
// I imagine many web developers probably use url rewriting anyway
fileExtensionsToHandleAsHTML = [[NSArray alloc] initWithObjects:@"asp",@"aspx",@"jsp",@"php",@"rb",@"py",@"pl",@"cgi", nil];
}
}

- (id)init
{
self = [super init];
Expand Down Expand Up @@ -105,31 +115,7 @@ - (void)updateExpiryForRequest:(ASIHTTPRequest *)request maxAge:(NSTimeInterval)

- (NSDate *)expiryDateForRequest:(ASIHTTPRequest *)request maxAge:(NSTimeInterval)maxAge
{
NSMutableDictionary *responseHeaders = [NSMutableDictionary dictionaryWithDictionary:[request responseHeaders]];

// If we weren't given a custom max-age, lets look for one in the response headers
if (!maxAge) {
NSString *cacheControl = [[responseHeaders objectForKey:@"Cache-Control"] lowercaseString];
if (cacheControl) {
NSScanner *scanner = [NSScanner scannerWithString:cacheControl];
[scanner scanUpToString:@"max-age" intoString:NULL];
if ([scanner scanString:@"max-age" intoString:NULL]) {
[scanner scanString:@"=" intoString:NULL];
[scanner scanDouble:&maxAge];
}
}
}

// RFC 2612 says max-age must override any Expires header
if (maxAge) {
return [[NSDate date] addTimeInterval:maxAge];
} else {
NSString *expires = [responseHeaders objectForKey:@"Expires"];
if (expires) {
return [ASIHTTPRequest dateFromRFC1123String:expires];
}
}
return nil;
return [ASIHTTPRequest expiryDateForRequest:request maxAge:maxAge];
}

- (void)storeResponseForRequest:(ASIHTTPRequest *)request maxAge:(NSTimeInterval)maxAge
Expand Down Expand Up @@ -183,9 +169,14 @@ - (void)storeResponseForRequest:(ASIHTTPRequest *)request maxAge:(NSTimeInterval

if ([request responseData]) {
[[request responseData] writeToFile:dataPath atomically:NO];
} else if ([request downloadDestinationPath] && ![[request downloadDestinationPath] isEqualToString:dataPath]) {
} else if ([request downloadDestinationPath] && ![[request downloadDestinationPath] isEqualToString:dataPath]) {
NSError *error = nil;
[[[[NSFileManager alloc] init] autorelease] copyItemAtPath:[request downloadDestinationPath] toPath:dataPath error:&error];
NSFileManager* manager = [[NSFileManager alloc] init];
if ([manager fileExistsAtPath:dataPath]) {
[manager removeItemAtPath:dataPath error:&error];
}
[manager copyItemAtPath:[request downloadDestinationPath] toPath:dataPath error:&error];
[manager release];
}
[[self accessLock] unlock];
}
Expand All @@ -212,12 +203,21 @@ - (NSString *)pathToCachedResponseDataForURL:(NSURL *)url
{
// Grab the file extension, if there is one. We do this so we can save the cached response with the same file extension - this is important if you want to display locally cached data in a web view
NSString *extension = [[url path] pathExtension];
if (![extension length]) {

// If the url doesn't have an extension, we'll add one so a webview can read it when locally cached
// If the url has the extension of a common web scripting language, we'll change the extension on the cached path to html for the same reason
if (![extension length] || [[[self class] fileExtensionsToHandleAsHTML] containsObject:[extension lowercaseString]]) {
extension = @"html";
}
return [self pathToFile:[[[self class] keyForURL:url] stringByAppendingPathExtension:extension]];
}

+ (NSArray *)fileExtensionsToHandleAsHTML
{
return fileExtensionsToHandleAsHTML;
}


- (NSString *)pathToCachedResponseHeadersForURL:(NSURL *)url
{
return [self pathToFile:[[[self class] keyForURL:url] stringByAppendingPathExtension:@"cachedheaders"]];
Expand Down Expand Up @@ -262,7 +262,10 @@ - (NSString *)pathToStoreCachedResponseDataForRequest:(ASIHTTPRequest *)request

// Grab the file extension, if there is one. We do this so we can save the cached response with the same file extension - this is important if you want to display locally cached data in a web view
NSString *extension = [[[request url] path] pathExtension];
if (![extension length]) {

// If the url doesn't have an extension, we'll add one so a webview can read it when locally cached
// If the url has the extension of a common web scripting language, we'll change the extension on the cached path to html for the same reason
if (![extension length] || [[[self class] fileExtensionsToHandleAsHTML] containsObject:[extension lowercaseString]]) {
extension = @"html";
}
path = [path stringByAppendingPathComponent:[[[self class] keyForURL:[request url]] stringByAppendingPathExtension:extension]];
Expand Down
5 changes: 3 additions & 2 deletions media/iphone/ASI/ASIFormDataRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ @implementation ASIFormDataRequest
#pragma mark utilities
- (NSString*)encodeURL:(NSString *)string
{
NSString *newString = NSMakeCollectable([(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)string, NULL, CFSTR(":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`"), CFStringConvertNSStringEncodingToEncoding([self stringEncoding])) autorelease]);
NSString *newString = [NSMakeCollectable(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)string, NULL, CFSTR(":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`"), CFStringConvertNSStringEncodingToEncoding([self stringEncoding]))) autorelease];
if (newString) {
return newString;
}
Expand All @@ -49,6 +49,7 @@ - (id)initWithURL:(NSURL *)newURL
self = [super initWithURL:newURL];
[self setPostFormat:ASIURLEncodedPostFormat];
[self setStringEncoding:NSUTF8StringEncoding];
[self setRequestMethod:@"POST"];
return self;
}

Expand Down Expand Up @@ -207,7 +208,7 @@ - (void)buildPostBody
[super buildPostBody];

#if DEBUG_FORM_DATA_REQUEST
NSLog(@"%@",[self debugBodyString]);
ASI_DEBUG_LOG(@"%@",[self debugBodyString]);
[self setDebugBodyString:nil];
#endif
}
Expand Down
4 changes: 2 additions & 2 deletions media/iphone/Classes/AddViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
NewsBlurAppDelegate *appDelegate;

UITextField *inFolderInput;
UITextField *newFolderInput;
UITextField *addFolderInput;
UITextField *siteAddressInput;
NSMutableData *jsonString;
NSMutableArray *autocompleteResults;
Expand Down Expand Up @@ -56,7 +56,7 @@

@property (nonatomic, retain) IBOutlet NewsBlurAppDelegate *appDelegate;
@property (nonatomic, retain) IBOutlet UITextField *inFolderInput;
@property (nonatomic, retain) IBOutlet UITextField *newFolderInput;
@property (nonatomic, retain) IBOutlet UITextField *addFolderInput;
@property (nonatomic, retain) IBOutlet UITextField *siteAddressInput;

@property (nonatomic, retain) IBOutlet UIBarButtonItem *addButton;
Expand Down
42 changes: 23 additions & 19 deletions media/iphone/Classes/AddViewController.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// LoginViewController.m
// AddViewController.m
// NewsBlur
//
// Created by Samuel Clay on 10/31/10.
Expand All @@ -17,7 +17,7 @@ @implementation AddViewController

@synthesize appDelegate;
@synthesize inFolderInput;
@synthesize newFolderInput;
@synthesize addFolderInput;
@synthesize siteAddressInput;
@synthesize addButton;
@synthesize cancelButton;
Expand Down Expand Up @@ -51,8 +51,8 @@ - (void)viewDidLoad {
[inFolderInput setLeftViewMode:UITextFieldViewModeAlways];
[folderImage release];
UIImageView *folderImage2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"folder.png"]];
[newFolderInput setLeftView:folderImage2];
[newFolderInput setLeftViewMode:UITextFieldViewModeAlways];
[addFolderInput setLeftView:folderImage2];
[addFolderInput setLeftViewMode:UITextFieldViewModeAlways];
[folderImage2 release];

UIImageView *urlImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"world.png"]];
Expand All @@ -62,7 +62,7 @@ - (void)viewDidLoad {

navBar.tintColor = [UIColor colorWithRed:0.16f green:0.36f blue:0.46 alpha:0.9];

newFolderInput.frame = CGRectMake(self.view.frame.size.width,
addFolderInput.frame = CGRectMake(self.view.frame.size.width,
siteAddressInput.frame.origin.y,
siteAddressInput.frame.size.width,
siteAddressInput.frame.size.height);
Expand Down Expand Up @@ -95,7 +95,7 @@ - (void)didReceiveMemoryWarning {
- (void)dealloc {
[appDelegate release];
[inFolderInput release];
[newFolderInput release];
[addFolderInput release];
[siteAddressInput release];
[addButton release];
[cancelButton release];
Expand Down Expand Up @@ -123,7 +123,7 @@ - (IBAction)doAddButton {
- (void)reload {
[inFolderInput setText:@""];
[siteAddressInput setText:@""];
[newFolderInput setText:@""];
[addFolderInput setText:@""];
[folderPicker reloadAllComponents];

folderPicker.frame = CGRectMake(0, self.view.bounds.size.height,
Expand All @@ -138,7 +138,7 @@ - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[errorLabel setText:@""];
if (textField == inFolderInput && ![inFolderInput isFirstResponder]) {
[siteAddressInput resignFirstResponder];
[newFolderInput resignFirstResponder];
[addFolderInput resignFirstResponder];
[inFolderInput setInputView:folderPicker];
if (folderPicker.frame.origin.y >= self.view.bounds.size.height) {
folderPicker.hidden = NO;
Expand All @@ -149,7 +149,7 @@ - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
return NO;
} else if (textField == siteAddressInput) {
[self hideFolderPicker];
} else if (textField == newFolderInput) {
} else if (textField == addFolderInput) {
[self hideFolderPicker];
}
return YES;
Expand All @@ -159,8 +159,12 @@ - (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == inFolderInput) {

} else if (textField == siteAddressInput) {
[self addSite];
} else if (textField == newFolderInput) {
if (siteAddressInput.returnKeyType == UIReturnKeySearch) {
[self checkSiteAddress];
} else {
[self addSite];
}
} else if (textField == addFolderInput) {
[self addFolder];
}
return YES;
Expand Down Expand Up @@ -210,7 +214,7 @@ - (void)autocompleteSite:(ASIHTTPRequest *)request {
[self.siteActivityIndicator stopAnimating];
NSString *responseString = [request responseString];
autocompleteResults = [[NSMutableArray alloc] initWithArray:[responseString JSONValue]];
NSLog(@"%@", autocompleteResults);
// NSLog(@"%@", autocompleteResults);
[siteTable reloadData];
}

Expand Down Expand Up @@ -269,7 +273,7 @@ - (NSString *)extractParentFolder {

- (IBAction)addFolder {
[self hideFolderPicker];
[newFolderInput resignFirstResponder];
[addFolderInput resignFirstResponder];
[self.addingLabel setHidden:NO];
[self.addingLabel setText:@"Adding Folder..."];
[self.errorLabel setHidden:YES];
Expand All @@ -280,7 +284,7 @@ - (IBAction)addFolder {
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
NSString *parent_folder = [self extractParentFolder];
[request setPostValue:parent_folder forKey:@"parent_folder"];
[request setPostValue:[newFolderInput text] forKey:@"folder"];
[request setPostValue:[addFolderInput text] forKey:@"folder"];
[request setDelegate:self];
[request setDidFinishSelector:@selector(finishAddFolder:)];
[request setDidFailSelector:@selector(requestFailed:)];
Expand Down Expand Up @@ -326,26 +330,26 @@ - (IBAction)selectAddTypeSignup {
- (void)animateLoop {
if ([self.addTypeControl selectedSegmentIndex] == 0) {
[addButton setTitle:@"Add Site"];
[newFolderInput resignFirstResponder];
[addFolderInput resignFirstResponder];
[UIView animateWithDuration:0.5 animations:^{
siteAddressInput.frame = CGRectMake(newFolderInput.frame.origin.x,
siteAddressInput.frame = CGRectMake(addFolderInput.frame.origin.x,
siteAddressInput.frame.origin.y,
siteAddressInput.frame.size.width,
siteAddressInput.frame.size.height);
newFolderInput.frame = CGRectMake(self.view.frame.size.width,
addFolderInput.frame = CGRectMake(self.view.frame.size.width,
siteAddressInput.frame.origin.y,
siteAddressInput.frame.size.width,
siteAddressInput.frame.size.height);
}];
} else {
[addButton setTitle:@"Add Folder"];
[siteAddressInput resignFirstResponder];
newFolderInput.frame = CGRectMake(self.view.frame.size.width,
addFolderInput.frame = CGRectMake(self.view.frame.size.width,
siteAddressInput.frame.origin.y,
siteAddressInput.frame.size.width,
siteAddressInput.frame.size.height);
[UIView animateWithDuration:0.5 animations:^{
newFolderInput.frame = CGRectMake(siteAddressInput.frame.origin.x,
addFolderInput.frame = CGRectMake(siteAddressInput.frame.origin.x,
siteAddressInput.frame.origin.y,
siteAddressInput.frame.size.width,
siteAddressInput.frame.size.height);
Expand Down
Loading

0 comments on commit bcdbf2d

Please sign in to comment.