From 164cbbd373ab8bff9c592f4e0b41dd1bcc33bab1 Mon Sep 17 00:00:00 2001 From: mltownsend Date: Sun, 4 Jan 2009 20:18:00 +0000 Subject: Added cancel button to the import/export progress sheet. Also added importing on a separate thread. --- Interfaces/English.lproj/DBView.xib | 85 ++++++++++++++++++++++++++++++++----- Source/TableDump.h | 15 +------ Source/TableDump.m | 67 +++++++++++++++++++---------- 3 files changed, 121 insertions(+), 46 deletions(-) diff --git a/Interfaces/English.lproj/DBView.xib b/Interfaces/English.lproj/DBView.xib index 71b1dba3..14911286 100644 --- a/Interfaces/English.lproj/DBView.xib +++ b/Interfaces/English.lproj/DBView.xib @@ -2,13 +2,14 @@ 1050 - 9F33 + 9G55 677 - 949.34 - 352.00 + 949.43 + 353.00 YES - + + YES @@ -6881,7 +6882,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 {3.40282e+38, 3.40282e+38} {213, 50} - + 256 YES @@ -6889,7 +6890,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 1280 - {{19, 40}, {337, 12}} + {{19, 40}, {307, 12}} 16652 1.000000e+02 @@ -6910,8 +6911,33 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 + + + 268 + {{333, 40}, {21, 14}} + + YES + + -2080244224 + 134217728 + + + + -2042347265 + 134 + + NSImage + NSStopProgressFreestandingTemplate + + + + 400 + 75 + + {375, 72} + {{0, 0}, {1440, 878}} {213, 72} @@ -11796,6 +11822,14 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 4693 + + + cancelProgressBar: + + + + 4701 + @@ -12954,6 +12988,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 YES + @@ -16837,6 +16872,20 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 + + 4699 + + + YES + + + + + + 4700 + + + @@ -17823,8 +17872,10 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 4677.IBPluginDependency 4686.IBPluginDependency 4687.IBPluginDependency + 4699.IBPluginDependency 47.IBPluginDependency 47.ImportedFromIB2 + 4700.IBPluginDependency 495.IBAttributePlaceholdersKey 495.IBPluginDependency 495.ImportedFromIB2 @@ -19329,7 +19380,9 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin ToolTip @@ -19340,8 +19393,8 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 com.apple.InterfaceBuilder.CocoaPlugin - {{119, 153}, {863, 550}} - {{119, 153}, {863, 550}} + {{41, 153}, {863, 550}} + {{41, 153}, {863, 550}} {{62, 352}, {845, 504}} @@ -19719,8 +19772,8 @@ Y2hhbmdlIHRoZSBvcmRlcg com.apple.InterfaceBuilder.CocoaPlugin - {{126, 624}, {375, 72}} - {{126, 624}, {375, 72}} + {{57, 736}, {375, 72}} + {{57, 736}, {375, 72}} @@ -19754,7 +19807,7 @@ Y2hhbmdlIHRoZSBvcmRlcg - 4695 + 4701 @@ -19921,6 +19974,14 @@ Y2hhbmdlIHRoZSBvcmRlcg + + NSImageView + NSControl + + IBUserSource + + + NSObject @@ -20267,6 +20328,7 @@ Y2hhbmdlIHRoZSBvcmRlcg YES YES + cancelProgressBar: changeFormat: changeTable: closeSheet: @@ -20282,6 +20344,7 @@ Y2hhbmdlIHRoZSBvcmRlcg id id id + id diff --git a/Source/TableDump.h b/Source/TableDump.h index 22b123af..c8648eb4 100644 --- a/Source/TableDump.h +++ b/Source/TableDump.h @@ -94,6 +94,7 @@ NSString *savePath; NSString *openPath; NSUserDefaults *prefs; + BOOL progressCancelled; } //IBAction methods @@ -101,15 +102,13 @@ - (IBAction)selectTables:(id)sender; - (IBAction)closeSheet:(id)sender; - (IBAction)stepRow:(id)sender; -//- (IBAction)chooseDumpType:(id)sender; - +- (IBAction)cancelProgressBar:(id)sender; //export methods //- (IBAction)saveDump:(id)sender; - (void)exportFile:(int)tag; - (void)savePanelDidEnd:(NSSavePanel *)sheet returnCode:(int)returnCode contextInfo:(NSString *)contextInfo; //import methods -//- (IBAction)openDump:(id)sender; - (void)importFile; - (IBAction)changeFormat:(id)sender; - (IBAction)changeTable:(id)sender; @@ -139,16 +138,6 @@ //additional methods - (void)setConnection:(CMMCPConnection *)theConnection; -//tableView datasource methods -- (int)numberOfRowsInTableView:(NSTableView *)aTableView; -- (id)tableView:(NSTableView *)aTableView - objectValueForTableColumn:(NSTableColumn *)aTableColumn - row:(int)rowIndex; -- (void)tableView:(NSTableView *)aTableView - setObjectValue:(id)anObject - forTableColumn:(NSTableColumn *)aTableColumn - row:(int)rowIndex; - //last but not least - (id)init; - (void)dealloc; diff --git a/Source/TableDump.m b/Source/TableDump.m index 442ec84a..dd2b91c0 100644 --- a/Source/TableDump.m +++ b/Source/TableDump.m @@ -294,7 +294,10 @@ NSBeginAlertSheet(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, tableWindow, self, nil, nil, nil, NSLocalizedString(@"Couldn't write to file. Be sure that you have the necessary privileges.", @"message of panel when file cannot be written")); } - + if (progressCancelled) + { + progressCancelled = NO; + } // Export finished Growl notification [[SPGrowlController sharedGrowlController] notifyWithTitle:@"Export Finished" description:[NSString stringWithFormat:NSLocalizedString(@"Finished exporting to %@",@"description for finished exporting growl notification"), [[sheet filename] lastPathComponent]] @@ -342,29 +345,18 @@ [fieldMappingTableView reloadData]; } -- (void)openPanelDidEnd:(NSOpenPanel *)sheet returnCode:(int)returnCode contextInfo:(NSString *)contextInfo -/* - reads mysql-dumpfile - */ +- (void)importBackgroundProcess:(NSString*)filename { + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSString *dumpFile; NSError **errorStr; NSMutableString *errors = [NSMutableString string]; NSString *fileType = [[importFormatPopup selectedItem] title]; - - [sheet orderOut:self]; - - if ( returnCode != NSOKButton ) - return; - - //save path to preferences - [prefs setObject:[sheet directory] forKey:@"openPath"]; - //load file into string - dumpFile = [NSString stringWithContentsOfFile:[sheet filename] + dumpFile = [NSString stringWithContentsOfFile:filename encoding:[CMMCPConnection encodingForMySQLEncoding:[[tableDocumentInstance encoding] cString]] error:errorStr]; - + if ( !dumpFile ) { NSBeginAlertSheet(NSLocalizedString(@"Error", @"Title of error alert"), NSLocalizedString(@"OK", @"OK button"), @@ -442,6 +434,7 @@ //////////////// } else if ( [fileType isEqualToString:@"CSV"] ) { + //NSLog(@"CSV Import..."); //import csv file int code; NSPopUpButtonCell *buttonCell = [[NSPopUpButtonCell alloc] init]; @@ -474,7 +467,12 @@ [singleProgressBar setUsesThreadedAnimation:NO]; [singleProgressBar setIndeterminate:NO]; - + if (progressCancelled) { + progressCancelled = NO; + //NSLog(@"Progress Cancelled... cleaning up"); + [pool release]; + return; + } CMMCPResult *theResult; int i; theResult = (CMMCPResult *) [mySQLConnection listTables]; @@ -608,8 +606,25 @@ // Import finished Growl notification [[SPGrowlController sharedGrowlController] notifyWithTitle:@"Import Finished" - description:[NSString stringWithFormat:NSLocalizedString(@"Finished importing %@",@"description for finished importing growl notification"), [[sheet filename] lastPathComponent]] + description:[NSString stringWithFormat:NSLocalizedString(@"Finished importing %@",@"description for finished importing growl notification"), [filename lastPathComponent]] notificationName:@"Import Finished"]; + [pool release]; +} +- (void)openPanelDidEnd:(NSOpenPanel *)sheet returnCode:(int)returnCode contextInfo:(NSString *)contextInfo +/* + reads mysql-dumpfile + */ +{ + [sheet orderOut:self]; + + if ( returnCode != NSOKButton ) + return; + + //save path to preferences + [prefs setObject:[sheet directory] forKey:@"openPath"]; + + [NSThread detachNewThreadSelector:@selector(importBackgroundProcess:) toTarget:self withObject:[sheet filename]]; + } - (void)setupFieldMappingArray @@ -1120,12 +1135,16 @@ return TRUE; } + - (NSArray *)arrayForCSV:(NSString *)csv terminatedBy:(NSString *)terminated enclosedBy:(NSString *)enclosed escapedBy:(NSString *)escaped lineEnds:(NSString *)lineEnds /* loads a csv string into an array */ { + + //NSLog(@"In arrayForCSV..."); + NSMutableString *tempTerminated, *tempLineEnds; NSMutableArray *tempArray = [NSMutableArray array]; NSMutableArray *tempRowArray = [NSMutableArray array]; @@ -1166,7 +1185,7 @@ scanner = [NSScanner scannerWithString:csv]; [scanner setCharactersToBeSkipped:nil]; - while ( ![scanner isAtEnd] ) { + while ( ![scanner isAtEnd] && !progressCancelled) { [tempString setString:@""]; br = NO; @@ -1197,12 +1216,11 @@ // Skip blank lines if (![tempString length]) continue; - // Add the line to the array [linesArray addObject:[NSString stringWithString:tempString]]; } - for ( x = 0 ; x < [linesArray count] ; x++ ) { + for ( x = 0 ; x < [linesArray count] && !progressCancelled; x++ ) { //separate fields [tempRowArray removeAllObjects]; @@ -1258,6 +1276,7 @@ } //add row to tempArray [tempArray addObject:[NSArray arrayWithArray:tempRowArray]]; + //NSLog(@"tempArray count: %d", [tempArray count]); } return [NSArray arrayWithArray:tempArray]; @@ -1477,7 +1496,7 @@ [fileHandle writeData:[infoString dataUsingEncoding:connectionEncoding]]; // Loop through the selected tables - for ( i = 0 ; i < [selectedTables count] ; i++ ) { + for ( i = 0 ; i < [selectedTables count] && !progressCancelled; i++ ) { // Update the progress text and reset the progress bar to indeterminate status tableName = [selectedTables objectAtIndex:i]; @@ -1891,5 +1910,9 @@ objectValueForTableColumn:(NSTableColumn *)aTableColumn [super dealloc]; } +- (IBAction)cancelProgressBar:(id)sender +{ + progressCancelled = YES; +} @end -- cgit v1.2.3