[tcobrowser-cvs] SF.net SVN: tcobrowser:[1519] trunk/bibdesk/BDSKIEEEXploreParser.m
Status: Beta
Brought to you by:
amaxwell
From: <ama...@us...> - 2009-10-20 16:05:49
|
Revision: 1519 http://tcobrowser.svn.sourceforge.net/tcobrowser/?rev=1519&view=rev Author: amaxwell Date: 2009-10-20 16:05:38 +0000 (Tue, 20 Oct 2009) Log Message: ----------- code cleanup and additional comments Modified Paths: -------------- trunk/bibdesk/BDSKIEEEXploreParser.m Modified: trunk/bibdesk/BDSKIEEEXploreParser.m =================================================================== --- trunk/bibdesk/BDSKIEEEXploreParser.m 2009-10-20 05:32:55 UTC (rev 1518) +++ trunk/bibdesk/BDSKIEEEXploreParser.m 2009-10-20 16:05:38 UTC (rev 1519) @@ -48,8 +48,6 @@ static NSString *abstractPageURLPath = @"/xpls/abs_all.jsp"; static NSString *searchResultPageURLPath = @"/search/srchabstract.jsp"; -static NSString *_BDSKRunLoopModeIEEEDownload = @"_BDSKRunLoopModeIEEEDownload"; - @interface _BDSKIEEEDownload : NSObject <NSCopying> { @private @@ -62,71 +60,19 @@ id _delegate; } +// private runloop mode to avoid other callouts on the main thread (will also cause beachball if needed) ++ (NSString *)runloopMode; +- (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate; +- (void)start; + @property (nonatomic, copy) NSURL *pdfLinkURL; -@end +// not KVO compliant, but that doesn't matter here +@property (nonatomic, readonly) BOOL failed; +@property (nonatomic, readonly) NSURL *URL; +@property (nonatomic, readonly) NSError *error; +@property (nonatomic, readonly) NSData *result; -@implementation _BDSKIEEEDownload - -@synthesize pdfLinkURL = _pdfLinkURL; - -- (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate -{ - self = [super init]; - if (self) { - _request = [request copy]; - _delegate = delegate; - } - return self; -} - -- (void)dealloc -{ - [_connection release]; - [_request release]; - [_error release]; - [_result release]; - [_pdfLinkURL release]; - [super dealloc]; -} - -- (id)copyWithZone:(NSZone *)aZone { return [self retain]; } - -- (BOOL)failed { return _failed; } - -- (NSURL *)URL { return [_request URL]; } - -- (NSData *)result { return _result; } - -- (NSError *)error { return _error; } - -- (void)start -{ - NSParameterAssert(nil == _connection); - NSParameterAssert(nil == _result); - _result = [NSMutableData new]; - _connection = [[NSURLConnection alloc] initWithRequest:_request delegate:self startImmediately:NO]; - [_connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:_BDSKRunLoopModeIEEEDownload]; - [_connection start]; -} - -- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error -{ - _error = [error retain]; - _failed = YES; - [_delegate performSelector:@selector(downloadFinishedOrFailed:) withObject:self]; -} - -- (void)connectionDidFinishLoading:(NSURLConnection *)connection; -{ - [_delegate performSelector:@selector(downloadFinishedOrFailed:) withObject:self]; -} - -- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data; -{ - [_result appendData:data]; -} - @end @@ -217,7 +163,8 @@ _BDSKIEEEDownload *download = [[_BDSKIEEEDownload alloc] initWithRequest:request delegate:self]; NSString * arnumberURLString = [NSString stringWithFormat:@"http://ieeexplore.ieee.org/xpls/abs_all.jsp?tp=&arnumber=%@&isnumber=%@", arnumberString, isnumberString]; - // stash this away since we have arnumber ans isnumber here + + // stash this away since we have arnumber and isnumber here, and the download won't have the correct URL [download setPdfLinkURL:[NSURL URLWithString:arnumberURLString]]; [_activeDownloads addObject:download]; @@ -265,13 +212,16 @@ } else { + + // already on the abstract page [self enqueueAbstractPageDownloadForURL:url]; } - + // wait for all those downloads to finish while ([_activeDownloads count]) - CFRunLoopRunInMode((CFStringRef)_BDSKRunLoopModeIEEEDownload, 0.3, FALSE); + CFRunLoopRunInMode((CFStringRef)[_BDSKIEEEDownload runloopMode], 0.3, FALSE); + // copy and clear _finishedDownloads, since we'll be enqueuing more right away NSArray *finishedDownloads = [[_finishedDownloads copy] autorelease]; [_finishedDownloads removeAllObjects]; @@ -289,6 +239,7 @@ BibItem *errorItem = [[BibItem alloc] initWithType:BDSKMiscString fileType:BDSKBibtexString citeKey:nil pubFields:pubFields isNew:YES]; [items addObject:errorItem]; [errorItem release]; + continue; } @@ -316,11 +267,11 @@ BibItem *errorItem = [[BibItem alloc] initWithType:BDSKMiscString fileType:BDSKBibtexString citeKey:nil pubFields:pubFields isNew:YES]; [items addObject:errorItem]; [errorItem release]; + continue; } - // Get the PDF URL, if possible: - + // enqueue a download to get the PDF URL, if possible: NSURLRequest *request = [NSURLRequest requestWithURL:[download pdfLinkURL]]; _BDSKIEEEDownload *download = [[_BDSKIEEEDownload alloc] initWithRequest:request delegate:self]; [_activeDownloads addObject:download]; @@ -334,7 +285,7 @@ // download all the PDF link documents while ([_activeDownloads count]) - CFRunLoopRunInMode((CFStringRef)_BDSKRunLoopModeIEEEDownload, 0.3, FALSE); + CFRunLoopRunInMode((CFStringRef)[_BDSKIEEEDownload runloopMode], 0.3, FALSE); for (_BDSKIEEEDownload *download in _finishedDownloads) { @@ -361,3 +312,67 @@ } @end + +#pragma mark Download delegate + +@implementation _BDSKIEEEDownload + +@synthesize pdfLinkURL = _pdfLinkURL; +@synthesize failed = _failed; +@synthesize error = _error; +@synthesize result = _result; + ++ (NSString *)runloopMode { return @"_BDSKRunLoopModeIEEEDownload"; } + +- (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate +{ + self = [super init]; + if (self) { + _request = [request copy]; + _delegate = delegate; + } + return self; +} + +- (void)dealloc +{ + [_connection release]; + [_request release]; + [_error release]; + [_result release]; + [_pdfLinkURL release]; + [super dealloc]; +} + +- (id)copyWithZone:(NSZone *)aZone { return [self retain]; } + +- (NSURL *)URL { return [_request URL]; } + +- (void)start +{ + NSParameterAssert(nil == _connection); + NSParameterAssert(nil == _result); + _result = [NSMutableData new]; + _connection = [[NSURLConnection alloc] initWithRequest:_request delegate:self startImmediately:NO]; + [_connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:[[self class] runloopMode]]; + [_connection start]; +} + +- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error +{ + _error = [error retain]; + _failed = YES; + [_delegate performSelector:@selector(downloadFinishedOrFailed:) withObject:self]; +} + +- (void)connectionDidFinishLoading:(NSURLConnection *)connection; +{ + [_delegate performSelector:@selector(downloadFinishedOrFailed:) withObject:self]; +} + +- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data; +{ + [_result appendData:data]; +} + +@end This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |