Skip to content
This repository has been archived by the owner on May 24, 2024. It is now read-only.

Commit

Permalink
[core] Pull out NISimpleRequest into the core.
Browse files Browse the repository at this point in the history
  • Loading branch information
jverkoey committed Oct 25, 2011
1 parent 8711ba3 commit 249b97e
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 114 deletions.
8 changes: 8 additions & 0 deletions src/Nimbus.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@
66832DA314421B4A003E413C /* UINavigationBar+NIStyleable.m in Sources */ = {isa = PBXBuildFile; fileRef = 66832DA114421B4A003E413C /* UINavigationBar+NIStyleable.m */; };
66832E0614435071003E413C /* NIChameleonObserver.h in Headers */ = {isa = PBXBuildFile; fileRef = 66832E0414435071003E413C /* NIChameleonObserver.h */; };
66832E0714435071003E413C /* NIChameleonObserver.m in Sources */ = {isa = PBXBuildFile; fileRef = 66832E0514435071003E413C /* NIChameleonObserver.m */; };
668ECDBE1452B8A600455266 /* NISimpleRequest.h in Headers */ = {isa = PBXBuildFile; fileRef = 668ECDBC1452B8A600455266 /* NISimpleRequest.h */; };
668ECDBF1452B8A600455266 /* NISimpleRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = 668ECDBD1452B8A600455266 /* NISimpleRequest.m */; };
66A03C0D13E6E85E00B514F3 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 66A03C0C13E6E85E00B514F3 /* Foundation.framework */; };
66A03C1B13E6E85E00B514F3 /* SenTestingKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 66A03C1A13E6E85E00B514F3 /* SenTestingKit.framework */; };
66A03C1E13E6E85E00B514F3 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 66A03C0C13E6E85E00B514F3 /* Foundation.framework */; };
Expand Down Expand Up @@ -527,6 +529,8 @@
66832DA114421B4A003E413C /* UINavigationBar+NIStyleable.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "UINavigationBar+NIStyleable.m"; path = "css/src/UINavigationBar+NIStyleable.m"; sourceTree = SOURCE_ROOT; };
66832E0414435071003E413C /* NIChameleonObserver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = NIChameleonObserver.h; path = css/src/NIChameleonObserver.h; sourceTree = SOURCE_ROOT; };
66832E0514435071003E413C /* NIChameleonObserver.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = NIChameleonObserver.m; path = css/src/NIChameleonObserver.m; sourceTree = SOURCE_ROOT; };
668ECDBC1452B8A600455266 /* NISimpleRequest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NISimpleRequest.h; sourceTree = "<group>"; };
668ECDBD1452B8A600455266 /* NISimpleRequest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NISimpleRequest.m; sourceTree = "<group>"; };
669E98AD13E9EA1F00D99140 /* libz.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libz.dylib; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk/usr/lib/libz.dylib; sourceTree = DEVELOPER_DIR; };
66A03C0913E6E85E00B514F3 /* libNimbusCore.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libNimbusCore.a; sourceTree = BUILT_PRODUCTS_DIR; };
66A03C0C13E6E85E00B514F3 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -1289,6 +1293,8 @@
66A03C5B13E6E8D100B514F3 /* NIPreprocessorMacros.h */,
66A03C5C13E6E8D100B514F3 /* NIRuntimeClassModifications.h */,
66A03C5D13E6E8D100B514F3 /* NIRuntimeClassModifications.m */,
668ECDBC1452B8A600455266 /* NISimpleRequest.h */,
668ECDBD1452B8A600455266 /* NISimpleRequest.m */,
66A03C5E13E6E8D100B514F3 /* NISDKAvailability.h */,
66A03C5F13E6E8D100B514F3 /* NISDKAvailability.m */,
66A03C6013E6E8D100B514F3 /* NIState.h */,
Expand Down Expand Up @@ -1682,6 +1688,7 @@
66A03CB913E6EFB000B514F3 /* NSString+NimbusCore.h in Headers */,
66A03C9013E6E8D100B514F3 /* NIState.h in Headers */,
FD01BEDB14179D940023D783 /* NINavigationAppearance.h in Headers */,
668ECDBE1452B8A600455266 /* NISimpleRequest.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -2623,6 +2630,7 @@
66A03C9213E6E8D100B514F3 /* NSData+NimbusCore.m in Sources */,
66A03C9313E6E8D100B514F3 /* NSString+NimbusCore.m in Sources */,
FD01BEDC14179D940023D783 /* NINavigationAppearance.m in Sources */,
668ECDBF1452B8A600455266 /* NISimpleRequest.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
96 changes: 96 additions & 0 deletions src/core/src/NISimpleRequest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//
// Copyright 2011 Jeff Verkoeyen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#import <Foundation/Foundation.h>

@protocol NISimpleRequestDelegate;

/**
* A simple network request object.
*
* This object is designed to be simple and light-weight. It does not cache requests, handle
* redirecting, and many other common features found in more powerful networking objects.
*/
@interface NISimpleRequest : NSObject <NSURLConnectionDataDelegate> {
@private
NSURLConnection* _connection;
NSMutableData* _data;
NSURL* _url;
id<NISimpleRequestDelegate> _delegate;
}

+ (id)requestWithURL:(NSURL *)url timeoutInterval:(NSTimeInterval)timeoutInterval;
- (id)initWithURL:(NSURL *)url timeoutInterval:(NSTimeInterval)timeoutInterval;
- (void)send;
- (void)cancel;

@property (nonatomic, readwrite, retain) NSURL* url;
@property (nonatomic, readwrite, assign) id<NISimpleRequestDelegate> delegate;
@end

/**
* The delegate for NISimpleRequest.
*/
@protocol NISimpleRequestDelegate <NSObject>
@optional

/**
* The request failed with a given error.
*/
- (void)requestDidFail:(NISimpleRequest *)request withError:(NSError *)error;

/**
* The request finished successfully.
*/
- (void)requestDidFinish:(NISimpleRequest *)request withData:(NSData *)data;

@end

/**
* Returns an autoreleased request initialized with the given URL and timeout.
*
* @fn NISimpleRequest::requestWithURL:timeoutInterval:
*/

/**
* Initializes a newly allocated request with a given url and timeout.
*
* @fn NISimpleRequest::initWithURL:timeoutInterval:
*/

/**
* Initiates the NSURLConnection.
*
* @fn NISimpleRequest::send
*/

/**
* Cancels the NSURLConnection.
*
* @fn NISimpleRequest::cancel
*/

/**
* The URL this request is fetching.
*
* @fn NISimpleRequest::url
*/

/**
* This request's delegate.
*
* @fn NISimpleRequest::delegate
*/
115 changes: 115 additions & 0 deletions src/core/src/NISimpleRequest.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
//
// Copyright 2011 Jeff Verkoeyen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#import "NISimpleRequest.h"

#import "NIPreprocessorMacros.h"


///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation NISimpleRequest

@synthesize url = _url;
@synthesize delegate = _delegate;


///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)dealloc {
[_connection cancel];
NI_RELEASE_SAFELY(_connection);
NI_RELEASE_SAFELY(_data);
NI_RELEASE_SAFELY(_url);

[super dealloc];
}


///////////////////////////////////////////////////////////////////////////////////////////////////
+ (id)requestWithURL:(NSURL *)url timeoutInterval:(NSTimeInterval)timeoutInterval {
return [[[self alloc] initWithURL:url timeoutInterval:timeoutInterval] autorelease];
}


///////////////////////////////////////////////////////////////////////////////////////////////////
- (id)initWithURL:(NSURL *)url timeoutInterval:(NSTimeInterval)timeoutInterval {
if ((self = [super init])) {
_url = [url retain];

NSURLRequest* request = [NSURLRequest requestWithURL: url
cachePolicy: NSURLRequestUseProtocolCachePolicy
timeoutInterval: timeoutInterval];

_connection = [[NSURLConnection alloc] initWithRequest:request
delegate:self
startImmediately:NO];

_data = [[NSMutableData alloc] init];
}
return self;
}


///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)send {
[_connection start];
}


///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)cancel {
[_connection cancel];
}


///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - NSURLConnectionDataDelegate


///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[_data setLength:0];
}


///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[_data appendData:data];
}


///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
if ([self.delegate respondsToSelector:@selector(requestDidFail:withError:)]) {
[self.delegate requestDidFail:self withError:error];
}
NI_RELEASE_SAFELY(_data);
NI_RELEASE_SAFELY(_connection);
}


///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
if ([self.delegate respondsToSelector:@selector(requestDidFinish:withData:)]) {
[self.delegate requestDidFinish:self withData:_data];
}
NI_RELEASE_SAFELY(_data);
NI_RELEASE_SAFELY(_connection);
}

@end
1 change: 1 addition & 0 deletions src/core/src/NimbusCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,6 @@
#import "NIPaths.h"
#import "NIPreprocessorMacros.h"
#import "NIRuntimeClassModifications.h"
#import "NISimpleRequest.h"
#import "NISDKAvailability.h"
#import "NIState.h"
13 changes: 3 additions & 10 deletions src/css/src/NIChameleonObserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,15 @@
//

#import <Foundation/Foundation.h>

#import "NISimpleRequest.h"
#import "NICSSParser.h"

extern NSString* const NIChameleonSkinDidChangeNotification;

@class NIStylesheet;

@class NISimpleDataRequest;

@protocol NISimpleDataRequestDelegate <NSObject>
@required

- (void)requestDidFinish:(NISimpleDataRequest *)request withStringData:(NSString *)stringData;

@end

@interface NIChameleonObserver : NSObject <NISimpleDataRequestDelegate, NICSSParserDelegate> {
@interface NIChameleonObserver : NSObject <NISimpleRequestDelegate, NICSSParserDelegate> {
@private
NSMutableDictionary* _stylesheets;
NSMutableArray* _activeRequests;
Expand Down
Loading

0 comments on commit 249b97e

Please sign in to comment.