diff --git a/SUAppcast.h b/SUAppcast.h index 624f3f21..6d0a1be9 100644 --- a/SUAppcast.h +++ b/SUAppcast.h @@ -14,6 +14,7 @@ NSArray *items; NSString *userAgentString; id delegate; + RSS *feed; } - (void)fetchAppcastFromURL:(NSURL *)url; diff --git a/SUAppcast.m b/SUAppcast.m index b0d454b5..fcacfe9b 100644 --- a/SUAppcast.m +++ b/SUAppcast.m @@ -17,6 +17,7 @@ - (void)dealloc { + [feed release]; [items release]; [super dealloc]; } @@ -28,36 +29,48 @@ - (void)fetchAppcastFromURL:(NSURL *)url { - RSS *feed = [[RSS alloc] initWithURL:url userAgent:userAgentString delegate:self]; - CFRetain(feed); // Manage the RSS feed's memory manually. + feed = [[RSS alloc] initWithURL:url userAgent:userAgentString delegate:self]; } -- (void)feedDidFinishLoading:(RSS *)feed +- (void)feedDidFinishLoading:(RSS *)aFeed { // Set up all the appcast items: NSArray *tempItems = [NSMutableArray array]; id enumerator = [[feed newsItems] objectEnumerator], current; - @try + BOOL success = YES; + while ((current = [enumerator nextObject])) + { + SUAppcastItem *item = [[[SUAppcastItem alloc] initWithDictionary:current] autorelease]; + if (item) + { + [(NSMutableArray *)tempItems addObject:item]; + } + else + { + success = NO; + break; + } + } + if (success) { - while ((current = [enumerator nextObject])) - { - [(NSMutableArray *)tempItems addObject:[[[SUAppcastItem alloc] initWithDictionary:current] autorelease]]; - } - items = [[NSArray arrayWithArray:tempItems] retain]; // Make the items list immutable. - - if ([delegate respondsToSelector:@selector(appcastDidFinishLoading:)]) - [delegate performSelectorOnMainThread:@selector(appcastDidFinishLoading:) withObject:self waitUntilDone:NO]; + items = [tempItems copy]; // Make the items list immutable. + + if ([delegate respondsToSelector:@selector(appcastDidFinishLoading:)]) + [delegate performSelectorOnMainThread:@selector(appcastDidFinishLoading:) withObject:self waitUntilDone:NO]; + } + else + { + [self reportError:[NSError errorWithDomain:SUSparkleErrorDomain code:SUAppcastParseError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:SULocalizedString(@"An error occurred while parsing the update feed.", nil), NSLocalizedDescriptionKey, nil]]]; } - @catch (NSException *parseException) - { - [self reportError:[NSError errorWithDomain:SUSparkleErrorDomain code:SUAppcastParseError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:SULocalizedString(@"An error occurred while parsing the update feed.", nil), NSLocalizedDescriptionKey, [parseException reason], SUTechnicalErrorInformationKey, nil]]]; - } - CFRelease(feed); + [feed release]; + feed = nil; } -- (void)feed:(RSS *)feed didFailWithError:(NSError *)error +- (void)feed:(RSS *)aFeed didFailWithError:(NSError *)error { [self reportError:error]; + [feed release]; + feed = nil; } - (void)reportError:(NSError *)error diff --git a/SUAppcastItem.m b/SUAppcastItem.m index 28efc7c7..2bfc8011 100644 --- a/SUAppcastItem.m +++ b/SUAppcastItem.m @@ -106,16 +106,7 @@ self = [super init]; if (self) { - propertiesDictionary = [dict retain]; - [self setTitle:[dict objectForKey:@"title"]]; - [self setDate:[dict objectForKey:@"pubDate"]]; - [self setDescription:[dict objectForKey:@"description"]]; - id enclosure = [dict objectForKey:@"enclosure"]; - if (enclosure == nil || [enclosure objectForKey:@"url"] == nil) - [NSException raise:@"SUAppcastException" format:@"Couldn't find an download URL for feed entry %@!", [self title]]; - [self setFileURL:[NSURL URLWithString:[[enclosure objectForKey:@"url"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]; - [self setDSASignature:[enclosure objectForKey:@"sparkle:dsaSignature"]]; // Try to find a version string. // Finding the new version number from the RSS feed is a little bit hacky. There are two ways: @@ -126,32 +117,47 @@ // The big caveat with this is that you can't have underscores in your version strings, as that'll confuse Sparkle. // Feel free to change the separator string to a hyphen or something more suited to your needs if you like. NSString *newVersion = [enclosure objectForKey:@"sparkle:version"]; - if (!newVersion) // no sparkle:version attribute + if (newVersion == nil) // no sparkle:version attribute { // Separate the url by underscores and take the last component, as that'll be closest to the end, // then we remove the extension. Hopefully, this will be the version. NSArray *fileComponents = [[enclosure objectForKey:@"url"] componentsSeparatedByString:@"_"]; if ([fileComponents count] > 1) newVersion = [[fileComponents lastObject] stringByDeletingPathExtension]; - else - [NSException raise:@"SUAppcastException" format:@"Couldn't find a version string for %@! You need a sparkle:version attribute.", [enclosure objectForKey:@"url"]]; } - [self setVersionString:newVersion]; - [self setMinimumSystemVersion:[dict objectForKey:@"sparkle:minimumSystemVersion"]]; - - NSString *shortVersionString = [enclosure objectForKey:@"sparkle:shortVersionString"]; - if (shortVersionString) - [self setDisplayVersionString:shortVersionString]; - else - [self setDisplayVersionString:[self versionString]]; - - // Find the appropriate release notes URL. - if ([dict objectForKey:@"sparkle:releaseNotesLink"]) - [self setReleaseNotesURL:[NSURL URLWithString:[dict objectForKey:@"sparkle:releaseNotesLink"]]]; - else if ([[self description] hasPrefix:@"http://"]) // if the description starts with http://, use that. - [self setReleaseNotesURL:[NSURL URLWithString:[self description]]]; - else - [self setReleaseNotesURL:nil]; + + if (enclosure == nil || [enclosure objectForKey:@"url"] == nil || newVersion == nil) + { + [self release]; + self = nil; + } + else + { + propertiesDictionary = [dict retain]; + [self setTitle:[dict objectForKey:@"title"]]; + [self setDate:[dict objectForKey:@"pubDate"]]; + [self setDescription:[dict objectForKey:@"description"]]; + + [self setFileURL:[NSURL URLWithString:[[enclosure objectForKey:@"url"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]; + [self setDSASignature:[enclosure objectForKey:@"sparkle:dsaSignature"]]; + + [self setVersionString:newVersion]; + [self setMinimumSystemVersion:[dict objectForKey:@"sparkle:minimumSystemVersion"]]; + + NSString *shortVersionString = [enclosure objectForKey:@"sparkle:shortVersionString"]; + if (shortVersionString) + [self setDisplayVersionString:shortVersionString]; + else + [self setDisplayVersionString:[self versionString]]; + + // Find the appropriate release notes URL. + if ([dict objectForKey:@"sparkle:releaseNotesLink"]) + [self setReleaseNotesURL:[NSURL URLWithString:[dict objectForKey:@"sparkle:releaseNotesLink"]]]; + else if ([[self description] hasPrefix:@"http://"]) // if the description starts with http://, use that. + [self setReleaseNotesURL:[NSURL URLWithString:[self description]]]; + else + [self setReleaseNotesURL:nil]; + } } return self; }