From 1bf7220ca576a3c09b9d9e53e24e0c6f30f15850 Mon Sep 17 00:00:00 2001 From: Bibiko Date: Sat, 10 Apr 2010 18:16:25 +0000 Subject: =?UTF-8?q?=E2=80=A2=20added=20"Import=20from=20Clipboard"=20?= =?UTF-8?q?=E2=8C=A5=E2=87=A7=E2=8C=98I=20-=20sheet=20showing=20the=20firs?= =?UTF-8?q?t=204kB=20of=20pasteboard=20content=20and=20the=20SQL/CSV=20acc?= =?UTF-8?q?essory=20view=20-=20pasteboard=20content=20will=20be=20saved=20?= =?UTF-8?q?as=20temp=20file=20and=20read=20by=20using=20the=20current=20ta?= =?UTF-8?q?ble/connection=20encoding?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/TableDump.m | 136 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 123 insertions(+), 13 deletions(-) (limited to 'Source/TableDump.m') diff --git a/Source/TableDump.m b/Source/TableDump.m index a7a1f31d..3021f3c4 100644 --- a/Source/TableDump.m +++ b/Source/TableDump.m @@ -473,6 +473,51 @@ #pragma mark - #pragma mark Import methods +- (void)importFromClipboard +/* + invoked when user clicks on an ImportFromClipboard menuItem + */ +{ + // Load accessory nib each time + if(![NSBundle loadNibNamed:@"ImportAccessory" owner:self]) { + NSBeep(); + NSLog(@"ImportAccessory accessory dialog could not be loaded."); + return; + } + + // clipboard textview with no wrapping + const CGFloat LargeNumberForText = 1.0e7; + [[importFromClipboardTextView textContainer] setContainerSize:NSMakeSize(LargeNumberForText, LargeNumberForText)]; + [[importFromClipboardTextView textContainer] setWidthTracksTextView:NO]; + [[importFromClipboardTextView textContainer] setHeightTracksTextView:NO]; + [importFromClipboardTextView setAutoresizingMask:NSViewNotSizable]; + [importFromClipboardTextView setMaxSize:NSMakeSize(LargeNumberForText, LargeNumberForText)]; + [importFromClipboardTextView setHorizontallyResizable:YES]; + [importFromClipboardTextView setVerticallyResizable:YES]; + [importFromClipboardTextView setFont:[NSFont fontWithName:@"Monaco" size:11.0f]]; + + if([[[NSPasteboard generalPasteboard] stringForType:NSStringPboardType] length] > 4000) + [importFromClipboardTextView setString:[[[[NSPasteboard generalPasteboard] stringForType:NSStringPboardType] substringToIndex:4000] stringByAppendingString:@"\n…"]]; + else + [importFromClipboardTextView setString:[[NSPasteboard generalPasteboard] stringForType:NSStringPboardType]]; + + // Preset the accessory view with prefs defaults + [importFieldsTerminatedField setStringValue:[prefs objectForKey:SPCSVImportFieldTerminator]]; + [importLinesTerminatedField setStringValue:[prefs objectForKey:SPCSVImportLineTerminator]]; + [importFieldsEscapedField setStringValue:[prefs objectForKey:SPCSVImportFieldEscapeCharacter]]; + [importFieldsEnclosedField setStringValue:[prefs objectForKey:SPCSVImportFieldEnclosedBy]]; + [importFieldNamesSwitch setState:[[prefs objectForKey:SPCSVImportFirstLineIsHeader] boolValue]]; + [importFromClipboardAccessoryView addSubview:importCSVView]; + + [NSApp beginSheet:importFromClipboardSheet + modalForWindow:tableWindow + modalDelegate:self + didEndSelector:@selector(openPanelDidEnd:returnCode:contextInfo:) + contextInfo:@"importFromClipboard"]; + + // NSString ); +} + - (void)importFile /* invoked when user clicks on an import menuItem @@ -563,6 +608,8 @@ NSLocalizedString(@"OK button label", @"OK button"), nil, nil, tableWindow, self, nil, nil, nil, NSLocalizedString(@"SQL file open error", @"The SQL file you selected could not be found or read.")); + if([filename hasPrefix:SPImportClipboardTempFileNamePrefix]) + [[NSFileManager defaultManager] removeItemAtPath:filename error:nil]; return; } @@ -608,6 +655,8 @@ [sqlDataBuffer release]; [importPool drain]; [tableDocumentInstance setQueryMode:SPInterfaceQueryMode]; + if([filename hasPrefix:SPImportClipboardTempFileNamePrefix]) + [[NSFileManager defaultManager] removeItemAtPath:filename error:nil]; return; } @@ -656,6 +705,8 @@ [sqlDataBuffer release]; [importPool drain]; [tableDocumentInstance setQueryMode:SPInterfaceQueryMode]; + if([filename hasPrefix:SPImportClipboardTempFileNamePrefix]) + [[NSFileManager defaultManager] removeItemAtPath:filename error:nil]; return; } } @@ -679,7 +730,11 @@ } // Before entering the following loop, check that we actually have a connection. If not, bail. - if (![mySQLConnection isConnected]) return; + if (![mySQLConnection isConnected]) { + if([filename hasPrefix:SPImportClipboardTempFileNamePrefix]) + [[NSFileManager defaultManager] removeItemAtPath:filename error:nil]; + return; + } // Extract and process any complete SQL queries that can be found in the strings parsed so far while (query = [sqlParser trimAndReturnStringToCharacter:';' trimmingInclusively:YES returningInclusively:NO]) { @@ -791,7 +846,9 @@ NSInteger i; BOOL allDataRead = NO; BOOL insertBaseStringHasEntries; + NSStringEncoding csvEncoding = [MCPConnection encodingForMySQLEncoding:[[tableDocumentInstance connectionEncoding] UTF8String]]; + fieldMappingArray = nil; fieldMappingGlobalValueArray = nil; @@ -805,6 +862,8 @@ NSLocalizedString(@"OK button label", @"OK button"), nil, nil, tableWindow, self, nil, nil, nil, NSLocalizedString(@"CSV file open error", @"The CSV file you selected could not be found or read.")); + if([filename hasPrefix:SPImportClipboardTempFileNamePrefix]) + [[NSFileManager defaultManager] removeItemAtPath:filename error:nil]; return; } @@ -876,6 +935,8 @@ if(fieldMapperOperator) [fieldMapperOperator release], fieldMapperOperator = nil; [importPool drain]; [tableDocumentInstance setQueryMode:SPInterfaceQueryMode]; + if([filename hasPrefix:SPImportClipboardTempFileNamePrefix]) + [[NSFileManager defaultManager] removeItemAtPath:filename error:nil]; return; } @@ -923,6 +984,8 @@ if(fieldMapperOperator) [fieldMapperOperator release], fieldMapperOperator = nil; [importPool drain]; [tableDocumentInstance setQueryMode:SPInterfaceQueryMode]; + if([filename hasPrefix:SPImportClipboardTempFileNamePrefix]) + [[NSFileManager defaultManager] removeItemAtPath:filename error:nil]; return; } @@ -974,6 +1037,8 @@ if(fieldMapperOperator) [fieldMapperOperator release], fieldMapperOperator = nil; [importPool drain]; [tableDocumentInstance setQueryMode:SPInterfaceQueryMode]; + if([filename hasPrefix:SPImportClipboardTempFileNamePrefix]) + [[NSFileManager defaultManager] removeItemAtPath:filename error:nil]; return; } @@ -1024,6 +1089,8 @@ if(fieldMapperOperator) [fieldMapperOperator release], fieldMapperOperator = nil; [importPool drain]; [tableDocumentInstance setQueryMode:SPInterfaceQueryMode]; + if([filename hasPrefix:SPImportClipboardTempFileNamePrefix]) + [[NSFileManager defaultManager] removeItemAtPath:filename error:nil]; return; } @@ -1169,6 +1236,8 @@ if(fieldMapperOperator) [fieldMapperOperator release], fieldMapperOperator = nil; [importPool drain]; [tableDocumentInstance setQueryMode:SPInterfaceQueryMode]; + if([filename hasPrefix:SPImportClipboardTempFileNamePrefix]) + [[NSFileManager defaultManager] removeItemAtPath:filename error:nil]; // Close progress sheet [self closeAndStopProgressSheet]; @@ -1178,8 +1247,8 @@ [self showErrorSheetWithMessage:errors]; } - // Import finished Growl notification - [[SPGrowlController sharedGrowlController] notifyWithTitle:@"Import Finished" + // Import finished Growl notification + [[SPGrowlController sharedGrowlController] notifyWithTitle:@"Import Finished" description:[NSString stringWithFormat:NSLocalizedString(@"Finished importing %@",@"description for finished importing growl notification"), [filename lastPathComponent]] window:tableWindow notificationName:@"Import Finished"]; @@ -1195,30 +1264,71 @@ } } -- (void)openPanelDidEnd:(NSOpenPanel *)sheet returnCode:(NSInteger)returnCode contextInfo:(NSString *)contextInfo +- (void)openPanelDidEnd:(id)sheet returnCode:(NSInteger)returnCode contextInfo:(NSString *)contextInfo { + + // if contextInfo == nil NSOpenPanel else importFromClipboardPanel + // save values to preferences - [prefs setObject:[sheet directory] forKey:@"openPath"]; + if(contextInfo == nil) + [prefs setObject:[(NSOpenPanel*)sheet directory] forKey:@"openPath"]; + else + [importFromClipboardTextView setString:@""]; + [prefs setObject:[[importFormatPopup selectedItem] title] forKey:@"importFormatPopupValue"]; - // close sheet, and check if user canceled - [sheet orderOut:self]; + // close NSOpenPanel sheet + if(contextInfo == nil) + [sheet orderOut:self]; + + // check if user canceled if (returnCode != NSOKButton) return; // Reset progress cancelled from any previous runs progressCancelled = NO; - if(lastFilename) [lastFilename release]; lastFilename = nil; - lastFilename = [[NSString stringWithString:[sheet filename]] retain]; + NSString *importFileName; - if(lastFilename == nil || ![lastFilename length]) { - NSBeep(); - return; + // File path from NSOpenPanel + if(contextInfo == nil) + { + if(lastFilename) [lastFilename release]; lastFilename = nil; + lastFilename = [[NSString stringWithString:[(NSOpenPanel*)sheet filename]] retain]; + importFileName = [NSString stringWithString:lastFilename]; + if(lastFilename == nil || ![lastFilename length]) { + NSBeep(); + return; + } + } + + // Import from Clipboard + else + { + importFileName = [NSString stringWithFormat:@"%@%@", SPImportClipboardTempFileNamePrefix, + [[NSDate date] descriptionWithCalendarFormat:@"%H%M%S" + timeZone:nil + locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]]]; + + // Write clipboard content to temp file using the connection encoding + + NSStringEncoding encoding; + if ([[[importFormatPopup selectedItem] title] isEqualToString:@"SQL"]) + encoding = NSUTF8StringEncoding; + else + encoding = [MCPConnection encodingForMySQLEncoding:[[tableDocumentInstance connectionEncoding] UTF8String]]; + + if(![[[NSPasteboard generalPasteboard] stringForType:NSStringPboardType] writeToFile:importFileName atomically:NO encoding:encoding error:nil]) { + NSBeep(); + NSLog(@"Couldn't write clipboard content to temporary file."); + return; + } } + if(importFileName == nil) return; + // begin import process - [NSThread detachNewThreadSelector:@selector(importBackgroundProcess:) toTarget:self withObject:lastFilename]; + [NSThread detachNewThreadSelector:@selector(importBackgroundProcess:) toTarget:self withObject:importFileName]; } - (void)startSQLImportProcessWithFile:(NSString *)filename -- cgit v1.2.3