From e23ba5155a53c43a106ac9646f51321ccc7d86f4 Mon Sep 17 00:00:00 2001 From: stuconnolly Date: Sun, 2 Oct 2011 11:25:26 +0000 Subject: Favorites import progress. --- Source/SPConnectionController.m | 26 ++++++++++-------- Source/SPConnectionControllerDelegate.m | 39 ++++++++++++++++++++------- Source/SPFavoritesExportProtocol.h | 2 +- Source/SPFavoritesExporter.m | 3 +-- Source/SPFavoritesImportProtocol.h | 9 ++++++- Source/SPFavoritesImporter.h | 4 +-- Source/SPFavoritesImporter.m | 48 +++++++++++++++++++++++++++++++++ 7 files changed, 103 insertions(+), 28 deletions(-) diff --git a/Source/SPConnectionController.m b/Source/SPConnectionController.m index 937cb521..f5d02a61 100644 --- a/Source/SPConnectionController.m +++ b/Source/SPConnectionController.m @@ -1138,17 +1138,21 @@ static NSComparisonResult compareFavoritesUsingKey(id favorite1, id favorite2, v */ - (void)importExportFavoritesSheetDidEnd:(NSOpenPanel *)panel returnCode:(NSInteger)returnCode contextInfo:(NSString *)contextInfo { - if (contextInfo == SPExportFavorites) { - SPFavoritesExporter *exporter = [[[SPFavoritesExporter alloc] init] autorelease]; - - [exporter setDelegate:self]; - - [exporter writeFavorites:[self selectedFavoriteNodes] toFile:[panel filename]]; - } - else if (contextInfo == SPImportFavorites) { - SPFavoritesImporter *importer = [[SPFavoritesImporter alloc] init]; - - [importer setDelegate:self]; + if (returnCode == NSOKButton) { + if (contextInfo == SPExportFavorites) { + SPFavoritesExporter *exporter = [[[SPFavoritesExporter alloc] init] autorelease]; + + [exporter setDelegate:self]; + + [exporter writeFavorites:[self selectedFavoriteNodes] toFile:[panel filename]]; + } + else if (contextInfo == SPImportFavorites) { + SPFavoritesImporter *importer = [[SPFavoritesImporter alloc] init]; + + [importer setDelegate:self]; + + [importer importFavoritesFromFileAtPath:[panel filename]]; + } } } diff --git a/Source/SPConnectionControllerDelegate.m b/Source/SPConnectionControllerDelegate.m index 7d461233..26101071 100644 --- a/Source/SPConnectionControllerDelegate.m +++ b/Source/SPConnectionControllerDelegate.m @@ -489,8 +489,6 @@ static NSString *SPDatabaseImage = @"database-small"; else if (rows == 1) { return (![[self selectedFavoriteNode] isGroup]); } - - return YES; } return YES; @@ -505,23 +503,44 @@ static NSString *SPDatabaseImage = @"database-small"; - (void)favoritesExportCompletedWithError:(NSError *)error { if (error) { - [[NSAlert alertWithError:error] beginSheetModalForWindow:[dbDocument parentWindow] - modalDelegate:self - didEndSelector:NULL - contextInfo:NULL]; + + NSAlert *alert = [NSAlert alertWithMessageText:NSLocalizedString(@"Favorites export error", @"favorites export error message") + defaultButton:NSLocalizedString(@"OK", @"OK") + alternateButton:nil + otherButton:nil + informativeTextWithFormat:[NSString stringWithFormat:NSLocalizedString(@"The following error occurred during the export process:\n\n%@", @"favorites export error informative message"), [error localizedDescription]]]; + + [alert beginSheetModalForWindow:[dbDocument parentWindow] + modalDelegate:self + didEndSelector:NULL + contextInfo:NULL]; } } +/** + * Called by the favorites importer when the imported data is available. + */ +- (void)favoritesImportData:(NSDictionary *)data +{ + +} + /** * Called by the favorites importer when the import completes. */ - (void)favoritesImportCompletedWithError:(NSError *)error { if (error) { - [[NSAlert alertWithError:error] beginSheetModalForWindow:[dbDocument parentWindow] - modalDelegate:self - didEndSelector:NULL - contextInfo:NULL]; + NSAlert *alert = [NSAlert alertWithMessageText:NSLocalizedString(@"Favorites import error", @"favorites import error message") + defaultButton:NSLocalizedString(@"OK", @"OK") + alternateButton:nil + otherButton:nil + informativeTextWithFormat:[NSString stringWithFormat:NSLocalizedString(@"The following error occurred during the import process:\n\n%@", @"favorites import error informative message"), [error localizedDescription]]]; + + [alert beginSheetModalForWindow:[dbDocument parentWindow] + modalDelegate:self + didEndSelector:NULL + contextInfo:NULL]; } } diff --git a/Source/SPFavoritesExportProtocol.h b/Source/SPFavoritesExportProtocol.h index 59daf996..ebd7bab5 100644 --- a/Source/SPFavoritesExportProtocol.h +++ b/Source/SPFavoritesExportProtocol.h @@ -33,7 +33,7 @@ @protocol SPFavoritesExportProtocol /** - * Invoked when the favorites export proccess completes + * Invoked when the favorites export proccess completes. * * @param error An error instance. Anything other than nil indicates an error occurred. */ diff --git a/Source/SPFavoritesExporter.m b/Source/SPFavoritesExporter.m index cefa5190..66590c30 100644 --- a/Source/SPFavoritesExporter.m +++ b/Source/SPFavoritesExporter.m @@ -44,7 +44,6 @@ * * @param favorites The array of favorites to be written * @param path The file system path that the file is to be written to - * @param filename The filename of the file to be written */ - (void)writeFavorites:(NSArray *)favorites toFile:(NSString *)path { @@ -102,7 +101,7 @@ /** * Informs the delegate that the export process has completed. */ - - (void)_informDelegateOfExportCompletion:(NSError *)error +- (void)_informDelegateOfExportCompletion:(NSError *)error { if ([self delegate] && [[self delegate] respondsToSelector:@selector(favoritesExportCompletedWithError:)]) { [[self delegate] performSelectorOnMainThread:@selector(favoritesExportCompletedWithError:) withObject:error waitUntilDone:NO]; diff --git a/Source/SPFavoritesImportProtocol.h b/Source/SPFavoritesImportProtocol.h index bb178fc8..f025ab8e 100644 --- a/Source/SPFavoritesImportProtocol.h +++ b/Source/SPFavoritesImportProtocol.h @@ -33,7 +33,14 @@ @protocol SPFavoritesImportProtocol /** - * Invoked when the favorites import proccess completes + * Invoked when the favorites import process successfully imports the favorites data. + * + * @param data The imported data as a dictionary. + */ +- (void)favoritesImportData:(NSDictionary *)data; + +/** + * Invoked when the favorites import proccess completes. * * @param error An error instance. Anything other than nil indicates an error occurred. */ diff --git a/Source/SPFavoritesImporter.h b/Source/SPFavoritesImporter.h index 0d86bad1..ec1603cc 100644 --- a/Source/SPFavoritesImporter.h +++ b/Source/SPFavoritesImporter.h @@ -28,9 +28,7 @@ @interface SPFavoritesImporter : NSObject { NSObject *delegate; - - NSError *importError; - + NSString *importPath; } diff --git a/Source/SPFavoritesImporter.m b/Source/SPFavoritesImporter.m index 1031f5a5..7726a949 100644 --- a/Source/SPFavoritesImporter.m +++ b/Source/SPFavoritesImporter.m @@ -28,6 +28,8 @@ @interface SPFavoritesImporter () - (void)_importFavoritesInBackground; +- (void)_informDelegateOfImportCompletion:(NSError *)error; +- (void)_informDelegateOfImportDataAvailable:(NSDictionary *)data; @end @@ -37,17 +39,63 @@ @synthesize importPath; /** + * Imports the favorites from the file at the supplied path. * * @param path The path of the file to import */ - (void)importFavoritesFromFileAtPath:(NSString *)path { + [self setImportPath:path]; + [NSThread detachNewThreadSelector:@selector(_importFavoritesInBackground) toTarget:self withObject:nil]; } +/** + * Starts the import process on a separate thread. + */ - (void)_importFavoritesInBackground { + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + + NSError *error = nil; + NSFileManager *fileManager = [NSFileManager defaultManager]; + + NSDictionary *importData = nil; + if ([fileManager fileExistsAtPath:[self importPath]]) { + importData = [[NSDictionary alloc] initWithContentsOfFile:[self importPath]]; + + [self _informDelegateOfImportDataAvailable:importData]; + } + else { + error = [NSError errorWithDomain:NSCocoaErrorDomain + code:NSFileNoSuchFileError + userInfo:[NSDictionary dictionaryWithObject:@"Import file does not exist." forKey:NSLocalizedDescriptionKey]]; + } + + [self _informDelegateOfImportCompletion:error]; + + [pool release]; +} + +/** + * Informs the delegate that the import process has completed. + */ +- (void)_informDelegateOfImportCompletion:(NSError *)error +{ + if ([self delegate] && [[self delegate] respondsToSelector:@selector(favoritesExportCompletedWithError:)]) { + [[self delegate] performSelectorOnMainThread:@selector(favoritesExportCompletedWithError:) withObject:error waitUntilDone:NO]; + } +} + +/** + * Informs the delegate that the imported data is available. + */ +- (void)_informDelegateOfImportDataAvailable:(NSDictionary *)data +{ + if ([self delegate] && [[self delegate] respondsToSelector:@selector(favoritesImportData:)]) { + [[self delegate] performSelectorOnMainThread:@selector(favoritesImportData:) withObject:data waitUntilDone:NO]; + } } @end -- cgit v1.2.3