From 37b58a7b37cabc7409be30f57671d7af05961436 Mon Sep 17 00:00:00 2001 From: stuconnolly Date: Sun, 29 Apr 2012 11:13:35 +0000 Subject: Pass the array of imported favorites instead of the whole dictionary to the delegate and improve error handling. --- Source/SPFavoritesImporter.m | 47 ++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 13 deletions(-) (limited to 'Source/SPFavoritesImporter.m') diff --git a/Source/SPFavoritesImporter.m b/Source/SPFavoritesImporter.m index 60b483d5..5ec0c433 100644 --- a/Source/SPFavoritesImporter.m +++ b/Source/SPFavoritesImporter.m @@ -29,7 +29,8 @@ - (void)_importFavoritesInBackground; - (void)_informDelegateOfImportCompletion:(NSError *)error; -- (void)_informDelegateOfImportDataAvailable:(NSDictionary *)data; +- (void)_informDelegateOfImportDataAvailable:(NSArray *)data; +- (void)_informDelegateOfErrorCode:(NSUInteger)code description:(NSString *)description; @end @@ -50,6 +51,9 @@ [NSThread detachNewThreadSelector:@selector(_importFavoritesInBackground) toTarget:self withObject:nil]; } +#pragma mark - +#pragma mark Private API + /** * Starts the import process on a separate thread. */ @@ -57,25 +61,27 @@ { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - NSError *error = nil; + NSDictionary *importData; NSFileManager *fileManager = [NSFileManager defaultManager]; - NSDictionary *importData = nil; - if ([fileManager fileExistsAtPath:[self importPath]]) { importData = [[NSDictionary alloc] initWithContentsOfFile:[self importPath]]; - [self _informDelegateOfImportDataAvailable:importData]; + NSArray *favorites = [importData valueForKey:SPFavoritesDataRootKey]; + + if (favorites) { + [self _informDelegateOfImportDataAvailable:favorites]; + } + else { + [self _informDelegateOfErrorCode:NSFileReadUnknownError + description:NSLocalizedString(@"Error reading import file.", @"error reading import file")]; + } } else { - error = [NSError errorWithDomain:NSCocoaErrorDomain - code:NSFileNoSuchFileError - userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"Import file does not exist.", @"import file does not exist message") - forKey:NSLocalizedDescriptionKey]]; + [self _informDelegateOfErrorCode:NSFileReadNoSuchFileError + description:NSLocalizedString(@"Import file does not exist.", @"import file does not exist message")]; } - - [self _informDelegateOfImportCompletion:error]; - + [pool release]; } @@ -92,11 +98,26 @@ /** * Informs the delegate that the imported data is available. */ -- (void)_informDelegateOfImportDataAvailable:(NSDictionary *)data +- (void)_informDelegateOfImportDataAvailable:(NSArray *)data { if ([self delegate] && [[self delegate] respondsToSelector:@selector(favoritesImportData:)]) { [[self delegate] performSelectorOnMainThread:@selector(favoritesImportData:) withObject:data waitUntilDone:NO]; } } +/** + * Informs the delegate that an error occurred during the import. + * + * @param code The error code + * @param description A short description of the error + */ +- (void)_informDelegateOfErrorCode:(NSUInteger)code description:(NSString *)description +{ + NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain + code:code + userInfo:[NSDictionary dictionaryWithObject:description forKey:NSLocalizedDescriptionKey]]; + + [self _informDelegateOfImportCompletion:error]; +} + @end -- cgit v1.2.3