Permalink
Browse files

Random tidy up while looking at #2424

  • Loading branch information...
stuconnolly committed Jun 14, 2018
1 parent 17f279a commit fe75954d55d168f727839b3a3aad83d502cbd6e2
Showing with 100 additions and 61 deletions.
  1. +3 −2 Source/SPDataImport.h
  2. +97 −59 Source/SPDataImport.m
@@ -105,8 +105,9 @@ typedef enum {
BOOL importIntoNewTable;
NSUserDefaults *prefs;
BOOL progressCancelled;
BOOL _mainNibLoaded;
BOOL mainNibLoaded;
NSMutableArray *geometryFields;
NSMutableIndexSet *geometryFieldsMapIndex;
@@ -119,14 +120,14 @@ typedef enum {
// IBAction methods
- (IBAction)closeSheet:(id)sender;
- (IBAction)cancelProgressBar:(id)sender;
- (IBAction)changeFormat:(id)sender;
// Import methods
- (void)importFile;
- (void)importFromClipboard;
- (void)importSQLFile:(NSString *)filename;
- (void)startSQLImportProcessWithFile:(NSString *)filename;
- (void)importCSVFile:(NSString *)filename;
- (IBAction)changeFormat:(id)sender;
- (BOOL)buildFieldMappingArrayWithData:(NSArray *)importData isPreview:(BOOL)dataIsPreviewData ofSoureFile:(NSString*)filename;
- (NSString *)mappedValueStringForRowArray:(NSArray *)csvRowArray;
@@ -56,6 +56,8 @@ @interface SPDataImport ()
- (void)_startBackgroundImportTaskForFilename:(NSString *)filename;
- (void)_importBackgroundProcess:(NSDictionary *)userInfo;
- (void)_resetFieldMappingGlobals;
- (void)_closeAndStopProgressSheet;
- (NSString *)_getLineEndingForFile:(NSString *)filePath;
@end
@@ -93,16 +95,17 @@ - (id)init
prefs = nil;
lastFilename = nil;
_mainNibLoaded = NO;
mainNibLoaded = NO;
}
return self;
}
- (void)awakeFromNib
{
if (_mainNibLoaded) return;
_mainNibLoaded = YES;
if (mainNibLoaded) return;
mainNibLoaded = YES;
// Load the import accessory view, retaining a reference to the top-level objects that need releasing.
NSArray *importAccessoryTopLevelObjects = nil;
@@ -153,20 +156,6 @@ - (IBAction)closeSheet:(id)sender
[[sender window] orderOut:self];
}
/**
* Convenience method for closing and restoring the progress sheet to default state.
*/
- (void)closeAndStopProgressSheet
{
SPMainQSync(^{
[NSApp endSheet:singleProgressSheet];
[singleProgressBar setIndeterminate:YES];
[singleProgressSheet orderOut:nil];
[singleProgressBar stopAnimation:self];
[singleProgressBar setMaxValue:100];
});
}
#pragma mark -
#pragma mark Import construction methods
@@ -175,9 +164,9 @@ - (void)closeAndStopProgressSheet
*/
- (void)importFromClipboard
{
// clipboard textview with no wrapping
const CGFloat LargeNumberForText = 1.0e7f;
[[importFromClipboardTextView textContainer] setContainerSize:NSMakeSize(LargeNumberForText, LargeNumberForText)];
[[importFromClipboardTextView textContainer] setWidthTracksTextView:NO];
[[importFromClipboardTextView textContainer] setHeightTracksTextView:NO];
@@ -220,7 +209,6 @@ - (void)importFromClipboard
*/
- (void)importFromClipboardSheetDidEnd:(id)sheet returnCode:(NSInteger)returnCode contextInfo:(NSString *)contextInfo
{
// Reset the interface and store prefs
[importFromClipboardTextView setString:@""];
[prefs setObject:[[importFormatPopup selectedItem] title] forKey:@"importFormatPopupValue"];
@@ -257,7 +245,6 @@ - (void)importFromClipboardSheetDidEnd:(id)sheet returnCode:(NSInteger)returnCod
[self _startBackgroundImportTaskForFilename:importFileName];
}
/**
* Invoked when user clicks on an import menuitem.
*/
@@ -466,7 +453,9 @@ - (void)importSQLFile:(NSString *)filename
if (sqlModeToRestore) {
[mySQLConnection queryString:[NSString stringWithFormat:@"SET SQL_MODE=%@", [sqlModeToRestore tickQuotedString]]];
}
[self closeAndStopProgressSheet];
[self _closeAndStopProgressSheet];
SPOnewayAlertSheet(
SP_FILE_READ_ERROR_STRING,
[tableDocumentInstance parentWindow],
@@ -513,8 +502,11 @@ - (void)importSQLFile:(NSString *)filename
if (sqlModeToRestore) {
[mySQLConnection queryString:[NSString stringWithFormat:@"SET SQL_MODE=%@", [sqlModeToRestore tickQuotedString]]];
}
[self closeAndStopProgressSheet];
[self _closeAndStopProgressSheet];
NSString *displayEncoding;
if (![importEncodingPopup indexOfSelectedItem]) {
displayEncoding = [NSString stringWithFormat:@"%@ - %@", [importEncodingPopup titleOfSelectedItem], [NSString localizedNameOfStringEncoding:sqlEncoding]];
} else {
@@ -555,12 +547,14 @@ - (void)importSQLFile:(NSString *)filename
// If not, check the connection if appropriate and then clean up and exit if appropriate.
if (![mySQLConnection isConnected] && ([mySQLConnection userTriggeredDisconnect] || ![mySQLConnection checkConnection])) {
if ([filename hasPrefix:SPImportClipboardTempFileNamePrefix]) [[NSFileManager defaultManager] removeItemAtPath:filename error:nil];
[self closeAndStopProgressSheet];
[self _closeAndStopProgressSheet];
[errors appendString:NSLocalizedString(@"The connection to the server was lost during the import. The import is only partially complete.", @"Connection lost during import error message")];
[self showErrorSheetWithMessage:errors];
[sqlParser release];
[sqlDataBuffer release];
[importPool drain];
return;
}
@@ -702,7 +696,7 @@ - (void)importSQLFile:(NSString *)filename
if([filename hasPrefix:SPImportClipboardTempFileNamePrefix]) [[NSFileManager defaultManager] removeItemAtPath:filename error:nil];
// Close progress sheet
[self closeAndStopProgressSheet];
[self _closeAndStopProgressSheet];
// Display any errors
if ([errors length]) {
@@ -880,7 +874,7 @@ - (void)importCSVFile:(NSString *)filename
// Report file read errors, and bail
@catch (NSException *exception) {
[self closeAndStopProgressSheet];
[self _closeAndStopProgressSheet];
SPOnewayAlertSheet(
SP_FILE_READ_ERROR_STRING,
[tableDocumentInstance parentWindow],
@@ -924,7 +918,7 @@ - (void)importCSVFile:(NSString *)filename
// Try to generate a NSString with the resulting data
csvString = [[NSString alloc] initWithData:[csvDataBuffer subdataWithRange:NSMakeRange(dataBufferLastQueryEndPosition, dataBufferPosition - dataBufferLastQueryEndPosition)] encoding:csvEncoding];
if (!csvString) {
[self closeAndStopProgressSheet];
[self _closeAndStopProgressSheet];
SPMainQSync(^{
NSString *displayEncoding;
if (![importEncodingPopup indexOfSelectedItem]) {
@@ -983,7 +977,7 @@ - (void)importCSVFile:(NSString *)filename
if (!fieldMappingArray
&& ([parsedRows count] >= 100 || (!csvRowArray && allDataRead)))
{
[self closeAndStopProgressSheet];
[self _closeAndStopProgressSheet];
if (![self buildFieldMappingArrayWithData:parsedRows isPreview:!allDataRead ofSoureFile:filename]) {
[csvParser release];
[csvDataBuffer release];
@@ -1061,7 +1055,7 @@ - (void)importCSVFile:(NSString *)filename
// Before entering the following loop, check that we actually have a connection.
// If not, check the connection if appropriate and then clean up and exit if appropriate.
if (![mySQLConnection isConnected] && ([mySQLConnection userTriggeredDisconnect] || ![mySQLConnection checkConnection])) {
[self closeAndStopProgressSheet];
[self _closeAndStopProgressSheet];
[csvParser release];
[csvDataBuffer release];
[parsedRows release];
@@ -1235,7 +1229,7 @@ - (void)importCSVFile:(NSString *)filename
[[NSFileManager defaultManager] removeItemAtPath:filename error:nil];
// Close progress sheet
[self closeAndStopProgressSheet];
[self _closeAndStopProgressSheet];
// Display any errors
if ([errors length]) {
@@ -1287,7 +1281,7 @@ - (BOOL) buildFieldMappingArrayWithData:(NSArray *)importData isPreview:(BOOL)da
// Ensure data was provided, or alert than an import error occurred and return false.
if (![importData count]) {
[self closeAndStopProgressSheet];
[self _closeAndStopProgressSheet];
SPOnewayAlertSheet(
NSLocalizedString(@"Error", @"error"),
[tableDocumentInstance parentWindow],
@@ -1298,7 +1292,7 @@ - (BOOL) buildFieldMappingArrayWithData:(NSArray *)importData isPreview:(BOOL)da
// Sanity check the first row of the CSV to prevent hang loops caused by wrong line ending entry
if ([[importData objectAtIndex:0] count] > 512) {
[self closeAndStopProgressSheet];
[self _closeAndStopProgressSheet];
SPOnewayAlertSheet(
NSLocalizedString(@"Error", @"error"),
[tableDocumentInstance parentWindow],
@@ -1642,11 +1636,11 @@ - (NSString *)mappedValueStringForRowArray:(NSArray *)csvRowArray
- (void)panelSelectionDidChange:(NSOpenPanel *)sender
{
NSArray *selectedUrls = sender.URLs;
NSString *pathExtension;
// If a single file is selected and the extension is recognised, change the format dropdown automatically
if ( selectedUrls.count != 1 ) return;
pathExtension = [[selectedUrls[0] pathExtension] uppercaseString];
if (selectedUrls.count != 1) return;
NSString *pathExtension = [[selectedUrls[0] pathExtension] uppercaseString];
// If the file has an extension '.gz' or '.bz2' indicating gzip or bzip2 compression, fetch the next extension
if ([pathExtension isEqualToString:@"GZ"] || [pathExtension isEqualToString:@"BZ2"]) {
@@ -1661,9 +1655,12 @@ - (void)panelSelectionDidChange:(NSOpenPanel *)sender
if ([pathExtension isEqualToString:@"SQL"]) {
[importFormatPopup selectItemWithTitle:@"SQL"];
[self changeFormat:self];
} else if ([pathExtension isEqualToString:@"CSV"] || [pathExtension isEqualToString:@"TSV"]) {
}
else if ([pathExtension isEqualToString:@"CSV"] || [pathExtension isEqualToString:@"TSV"]) {
[importFormatPopup selectItemWithTitle:@"CSV"];
[self changeFormat:self];
// Set the cell delineator based on extension
@@ -1673,31 +1670,9 @@ - (void)panelSelectionDidChange:(NSOpenPanel *)sender
[importFieldsTerminatedField setStringValue:@"\\t"];
}
// Try to detect the line endings using "file"
NSTask *fileTask = [[NSTask alloc] init];
NSPipe *filePipe = [[NSPipe alloc] init];
[fileTask setLaunchPath:@"/usr/bin/file"];
[fileTask setArguments:[NSArray arrayWithObjects:@"-L", @"-b", [selectedUrls[0] path], nil]];
[fileTask setStandardOutput:filePipe];
NSFileHandle *fileHandle = [filePipe fileHandleForReading];
[fileTask launch];
NSString *fileCheckOutput = [[NSString alloc] initWithData:[fileHandle readDataToEndOfFile] encoding:NSASCIIStringEncoding];
if (fileCheckOutput && [fileCheckOutput length]) {
NSString *lineEndingString = [fileCheckOutput stringByMatching:@"with ([A-Z]{2,4}) line terminators" capture:1L];
if (!lineEndingString && [fileCheckOutput isMatchedByRegex:@"text"]) lineEndingString = @"LF";
if (lineEndingString) {
if ([lineEndingString isEqualToString:@"LF"]) [importLinesTerminatedField setStringValue:@"\\n"];
else if ([lineEndingString isEqualToString:@"CR"]) [importLinesTerminatedField setStringValue:@"\\r"];
else if ([lineEndingString isEqualToString:@"CRLF"]) [importLinesTerminatedField setStringValue:@"\\r\\n"];
}
}
if (fileCheckOutput) [fileCheckOutput release];
NSString *lineEnding = [self _getLineEndingForFile:[selectedUrls[0] path]];
[fileTask release];
[filePipe release];
if (lineEnding) [importLinesTerminatedField setStringValue:lineEnding];
}
}
@@ -1815,6 +1790,69 @@ - (void)_resetFieldMappingGlobals
if (fieldMapperOperator) SPClear(fieldMapperOperator);
}
/**
* Convenience method for closing and restoring the progress sheet to default state.
*/
- (void)_closeAndStopProgressSheet
{
SPMainQSync(^{
[NSApp endSheet:singleProgressSheet];
[singleProgressBar setIndeterminate:YES];
[singleProgressSheet orderOut:nil];
[singleProgressBar stopAnimation:self];
[singleProgressBar setMaxValue:100];
});
}
/**
* Tries to determine the line endings of the specified file using the 'file' command.
*/
- (NSString *)_getLineEndingForFile:(NSString *)filePath
{
NSString *lineEnding = nil;
NSTask *fileTask = [[NSTask alloc] init];
NSPipe *filePipe = [[NSPipe alloc] init];
[fileTask setLaunchPath:@"/usr/bin/file"];
[fileTask setArguments:[NSArray arrayWithObjects:@"-L", @"-b", filePath, nil]];
[fileTask setStandardOutput:filePipe];
NSFileHandle *fileHandle = [filePipe fileHandleForReading];
[fileTask launch];
NSString *fileCheckOutput = [[NSString alloc] initWithData:[fileHandle readDataToEndOfFile] encoding:NSASCIIStringEncoding];
if (fileCheckOutput && [fileCheckOutput length]) {
NSString *lineEndingString = [fileCheckOutput stringByMatching:@"with ([A-Z]{2,4}) line terminators" capture:1L];
if (!lineEndingString && [fileCheckOutput isMatchedByRegex:@"text"]) {
lineEndingString = @"LF";
}
if (lineEndingString) {
if ([lineEndingString isEqualToString:@"LF"]) {
lineEnding = @"\\n";
}
else if ([lineEnding isEqualToString:@"CR"]){
lineEnding = @"\\r";
}
else if ([lineEnding isEqualToString:@"CRLF"]) {
lineEnding = @"\\r\\n";
}
}
}
if (fileCheckOutput) [fileCheckOutput release];
[fileTask release];
[filePipe release];
return lineEnding;
}
#pragma mark -
- (void)dealloc

0 comments on commit fe75954

Please sign in to comment.