From 0dbc2fe46d3f496b2e6e7be784352767572b6df1 Mon Sep 17 00:00:00 2001 From: Bibiko Date: Sat, 26 Sep 2009 11:44:19 +0000 Subject: =?UTF-8?q?=E2=80=A2=20if=20the=20user=20chose=20to=20open=20a=20S?= =?UTF-8?q?QL=20file=20which=20is=20larger=20than=201MB=20SP=20asks=20for?= =?UTF-8?q?=20confirmation,=20furthermore=20if=20a=20connection=20is=20ava?= =?UTF-8?q?ilable=20the=20user=20can=20choose=20'Import'=20instead=20of=20?= =?UTF-8?q?loading=20it=20into=20the=20Query=20Editor=20for=20cases=20that?= =?UTF-8?q?=20an=20user=20invoked=20'Open=E2=80=A6'=20accidentally=20inste?= =?UTF-8?q?ad=20of=20'Import=E2=80=A6'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/SPAppController.m | 45 ++++++++++++++++++++++++++++++++++++++++++++- Source/TableDump.h | 5 +++-- Source/TableDump.m | 5 +++++ 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/Source/SPAppController.m b/Source/SPAppController.m index b9e32dd1..94a732b9 100644 --- a/Source/SPAppController.m +++ b/Source/SPAppController.m @@ -27,6 +27,7 @@ #import "SPAppController.h" #import "TableDocument.h" #import "SPPreferenceController.h" +#import "TableDump.h" #import "SPEncodingPopupAccessory.h" #import @@ -161,8 +162,10 @@ } - (void)openConnectionPanelDidEnd:(NSOpenPanel *)panel returnCode:(int)returnCode contextInfo:(void *)contextInfo { - if ( returnCode ) + if ( returnCode ) { + [panel orderOut:self]; [self application:nil openFiles:[panel filenames]]; + } encodingPopUp = nil; } @@ -179,6 +182,46 @@ // Opens a sql file and insert its content into the Custom Query editor if([[[filename pathExtension] lowercaseString] isEqualToString:@"sql"]) { + // Check size and NSFileType + NSDictionary *attr = [[NSFileManager defaultManager] fileAttributesAtPath:filename traverseLink:YES]; + if(attr) + { + NSNumber *filesize = [attr objectForKey:NSFileSize]; + NSString *filetype = [attr objectForKey:NSFileType]; + if(filetype == NSFileTypeRegular && filesize) + { + // Ask for confirmation if file content is larger than 1MB + if([filesize unsignedLongValue] > 1000000) + { + NSAlert *alert = [[NSAlert alloc] init]; + [alert addButtonWithTitle:NSLocalizedString(@"OK", @"OK button")]; + [alert addButtonWithTitle:NSLocalizedString(@"Cancel", @"cancel button")]; + + // Show 'Import' button only if there's a connection available + if ([[[NSDocumentController sharedDocumentController] documents] count]) + [alert addButtonWithTitle:NSLocalizedString(@"Import", @"import button")]; + + + [alert setInformativeText:[NSString stringWithFormat:NSLocalizedString(@"Do you really want to load a SQL file with %.1f MB of data into the Query Editor?", @"message of panel asking for confirmation for loading large text into the query editor"), + [filesize unsignedLongValue]/1048576.0]]; + [alert setHelpAnchor:filename]; + [alert setMessageText:NSLocalizedString(@"Warning",@"warning")]; + [alert setAlertStyle:NSWarningAlertStyle]; + + NSUInteger returnCode = [alert runModal]; + + [alert release]; + + if(returnCode == NSAlertSecondButtonReturn) return; // Cancel + else if(returnCode == NSAlertThirdButtonReturn) { // Import + // begin import process + [[[[NSDocumentController sharedDocumentController] currentDocument] valueForKeyPath:@"tableDumpInstance"] startSQLImportProcessWithFile:filename]; + return; + } + } + } + } + // if encodingPopUp is defined the filename comes from an openPanel and // the encodingPopUp contains the chosen encoding; otherwise autodetect encoding if(encodingPopUp) diff --git a/Source/TableDump.h b/Source/TableDump.h index de659fa2..dd9ffd6c 100644 --- a/Source/TableDump.h +++ b/Source/TableDump.h @@ -119,8 +119,9 @@ // Import methods - (void)importFile; -- (void) importSQLFile:(NSString *)filename; -- (void) importCSVFile:(NSString *)filename; +- (void)importSQLFile:(NSString *)filename; +- (void)startSQLImportProcessWithFile:(NSString *)filename; +- (void)importCSVFile:(NSString *)filename; - (IBAction)changeFormat:(id)sender; - (IBAction)changeTable:(id)sender; - (void)openPanelDidEnd:(NSOpenPanel *)sheet returnCode:(int)returnCode contextInfo:(NSString *)contextInfo; diff --git a/Source/TableDump.m b/Source/TableDump.m index 994b45bc..6a55610d 100644 --- a/Source/TableDump.m +++ b/Source/TableDump.m @@ -948,6 +948,11 @@ [NSThread detachNewThreadSelector:@selector(importBackgroundProcess:) toTarget:self withObject:[sheet filename]]; } +- (void)startSQLImportProcessWithFile:(NSString *)filename +{ + [importFormatPopup selectItemWithTitle:@"SQL"]; + [NSThread detachNewThreadSelector:@selector(importBackgroundProcess:) toTarget:self withObject:filename]; +} /* * Sets up the fieldMapping array to be shown in the tableView */ -- cgit v1.2.3