From 3f832f629faf1569637684d6f23a938f6d74553c Mon Sep 17 00:00:00 2001 From: Bibiko Date: Sat, 22 Aug 2009 14:49:48 +0000 Subject: =?UTF-8?q?=E2=80=A2=20moved=20NSOpenPanel=20stuff=20from=20TableD?= =?UTF-8?q?ocument=20to=20SPAppControler=20to=20simplify=20and=20unify=20i?= =?UTF-8?q?t=20=E2=80=A2=20fixed:=20avoid=20opening=20of=20more=20than=20N?= =?UTF-8?q?SOpenPanel=20windows=20=E2=80=A2=20if=20conncetionController=20?= =?UTF-8?q?is=20active=20do=20not=20allow=20a=20NSOpenPanel=20=E2=80=A2=20?= =?UTF-8?q?minor=20code=20cleaning?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/SPAppController.m | 117 ++++++++++++++++++++++++++++++++-------------- Source/TableDocument.h | 3 -- Source/TableDocument.m | 118 +---------------------------------------------- 3 files changed, 83 insertions(+), 155 deletions(-) (limited to 'Source') diff --git a/Source/SPAppController.m b/Source/SPAppController.m index 97029a68..e3778327 100644 --- a/Source/SPAppController.m +++ b/Source/SPAppController.m @@ -48,7 +48,53 @@ return self; } +/** + * Called even before init so we can register our preference defaults + */ ++ (void)initialize +{ + // Register application defaults + [[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"PreferenceDefaults" ofType:@"plist"]]]; +} + +/** + * Initialisation stuff upon nib awakening + */ +- (void)awakeFromNib +{ + // Set Sparkle delegate + [[SUUpdater sharedUpdater] setDelegate:self]; + + prefsController = [[SPPreferenceController alloc] init]; + + // Register SPAppController as services provider + [NSApp setServicesProvider:self]; + + // Register SPAppController for AppleScript events + [[NSScriptExecutionContext sharedScriptExecutionContext] setTopLevelObject:self]; + + isNewFavorite = NO; +} + +- (BOOL)validateMenuItem:(NSMenuItem *)menuItem +{ + if ([menuItem action] == @selector(openConnectionSheet:) && [menuItem tag] == 0) + { + // Do not allow to open a sql/spf file if SP asks for connection details + if ([[[NSDocumentController sharedDocumentController] documents] count]) { + if(![[[[NSDocumentController sharedDocumentController] currentDocument] mySQLVersion] length]) + return NO; + } + } + return YES; +} + +#pragma mark - +#pragma mark Open methods +/** + * NSOpenPanel delegate to control encoding popup and allowMultipleSelection + */ - (void)panelSelectionDidChange:(id)sender { @@ -63,10 +109,17 @@ } } - - +/* + * NSOpenPanel for selecting sql or spf file + */ - (IBAction)openConnectionSheet:(id)sender { + // Avoid opening more than NSOpenPanel + if(encodingPopUp){ + NSBeep(); + return; + } + NSOpenPanel *panel = [NSOpenPanel openPanel]; [panel setCanSelectHiddenExtension:YES]; [panel setDelegate:self]; @@ -74,8 +127,6 @@ [panel setAllowsMultipleSelection:YES]; [panel setResolvesAliases:YES]; - // // Set up encoding list - // If no lastSqlFileEncoding in prefs set it to UTF-8 if(![[NSUserDefaults standardUserDefaults] integerForKey:@"lastSqlFileEncoding"]) { [[NSUserDefaults standardUserDefaults] setInteger:4 forKey:@"lastSqlFileEncoding"]; @@ -85,14 +136,35 @@ [panel setAccessoryView:[SPEncodingPopupAccessory encodingAccessory:[[NSUserDefaults standardUserDefaults] integerForKey:@"lastSqlFileEncoding"] includeDefaultEntry:NO encodingPopUp:&encodingPopUp]]; + // it will enabled if user selects a *.sql file [encodingPopUp setEnabled:NO]; + + // Check if at least one document exists, if so show a sheet + if ([[[NSDocumentController sharedDocumentController] documents] count]) { + [panel beginSheetForDirectory:nil + file:@"" + types:[NSArray arrayWithObjects:@"spf", @"sql", nil] + modalForWindow:[[[NSDocumentController sharedDocumentController] currentDocument] valueForKey:@"tableWindow"] + modalDelegate:self + didEndSelector:@selector(openConnectionPanelDidEnd:returnCode:contextInfo:) + contextInfo:NULL]; + } else { + int returnCode = [panel runModalForDirectory:nil file:nil types:[NSArray arrayWithObjects:@"spf", @"sql", nil]]; + + if( returnCode ) + [self application:nil openFiles:[panel filenames]]; + - // [self setupPopUp:encodingPopUp selectedEncoding:[[NSUserDefaults standardUserDefaults] integerForKey:@"lastSqlFileEncoding"] withDefaultEntry:NO]; - int returnCode = [panel runModalForDirectory:nil file:nil types:[NSArray arrayWithObjects:@"spf", @"sql", nil]]; + encodingPopUp = nil; - if( returnCode ) + } +} +- (void)openConnectionPanelDidEnd:(NSOpenPanel *)panel returnCode:(int)returnCode contextInfo:(void *)contextInfo +{ + if ( returnCode ) [self application:nil openFiles:[panel filenames]]; + encodingPopUp = nil; } /** @@ -120,6 +192,8 @@ // Manually open a new document, setting SPAppController as sender to trigger autoconnection if (firstTableDocument = [[NSDocumentController sharedDocumentController] makeUntitledDocumentOfType:@"DocumentType" error:nil]) { [firstTableDocument setShouldAutomaticallyConnect:NO]; + + // user comes from a openPanel? if so use the chosen encoding if(encodingPopUp) { NSError *error = nil; NSString *content = [NSString stringWithContentsOfFile:filename encoding:[[encodingPopUp selectedItem] tag] error:&error]; @@ -133,6 +207,7 @@ } else [firstTableDocument initQueryEditorWithString:[self contentOfFile:filename]]; + [[NSDocumentController sharedDocumentController] addDocument:firstTableDocument]; [firstTableDocument makeWindowControllers]; [firstTableDocument showWindows]; @@ -166,34 +241,6 @@ } -/** - * Called even before init so we can register our preference defaults - */ -+ (void)initialize -{ - // Register application defaults - [[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"PreferenceDefaults" ofType:@"plist"]]]; -} - -/** - * Initialisation stuff upon nib awakening - */ -- (void)awakeFromNib -{ - // Set Sparkle delegate - [[SUUpdater sharedUpdater] setDelegate:self]; - - prefsController = [[SPPreferenceController alloc] init]; - - // Register SPAppController as services provider - [NSApp setServicesProvider:self]; - - // Register SPAppController for AppleScript events - [[NSScriptExecutionContext sharedScriptExecutionContext] setTopLevelObject:self]; - - isNewFavorite = NO; -} - #pragma mark - #pragma mark IBAction methods diff --git a/Source/TableDocument.h b/Source/TableDocument.h index 6f36ea83..2117d98b 100644 --- a/Source/TableDocument.h +++ b/Source/TableDocument.h @@ -82,7 +82,6 @@ IBOutlet id sidebarGrabber; IBOutlet NSPopUpButton *encodingPopUp; - IBOutlet id encodingAccessoryView; IBOutlet NSTextView *customQueryTextView; @@ -174,7 +173,6 @@ - (void)closeConnection; - (NSWindow *)getCreateTableSyntaxWindow; - (void) refreshCurrentDatabase; -- (void)openConnectionPanelDidEnd:(NSOpenPanel *)panel returnCode:(int)returnCode contextInfo:(void *)contextInfo; - (void)saveConnectionPanelDidEnd:(NSSavePanel *)panel returnCode:(int)returnCode contextInfo:(void *)contextInfo; @@ -192,7 +190,6 @@ // Menu methods - (BOOL)validateMenuItem:(NSMenuItem *)anItem; -- (IBAction)openConnectionSheet:(id)sender; - (IBAction)saveConnectionSheet:(id)sender; - (IBAction)import:(id)sender; - (IBAction)export:(id)sender; diff --git a/Source/TableDocument.m b/Source/TableDocument.m index cd5967cb..d2fdfb4a 100644 --- a/Source/TableDocument.m +++ b/Source/TableDocument.m @@ -1587,119 +1587,6 @@ #pragma mark - #pragma mark Menu methods -/** - * Opens SP session file(s) or a SQL file - */ -- (IBAction)openConnectionSheet:(id)sender -{ - - NSOpenPanel *panel = [NSOpenPanel openPanel]; - [panel setCanSelectHiddenExtension:YES]; - [panel setDelegate:self]; - [panel setCanChooseDirectories:NO]; - [panel setAllowsMultipleSelection:YES]; - [panel setResolvesAliases:YES]; - [panel setAccessoryView:[SPEncodingPopupAccessory encodingAccessory:[prefs integerForKey:@"lastSqlFileEncoding"] - includeDefaultEntry:NO encodingPopUp:&encodingPopUp]]; - - // Set up encoding list - [encodingPopUp setEnabled:NO]; - - // If no lastSqlFileEncoding in prefs set it to UTF-8 - if(![prefs integerForKey:@"lastSqlFileEncoding"]) { - [prefs setInteger:4 forKey:@"lastSqlFileEncoding"]; - [prefs synchronize]; - } - - // [self setupPopUp:encodingPopUp selectedEncoding:[prefs integerForKey:@"lastSqlFileEncoding"] withDefaultEntry:NO]; - - [panel beginSheetForDirectory:nil - file:@"" - types:[NSArray arrayWithObjects:@"spf", @"sql", nil] - modalForWindow:tableWindow - modalDelegate:self didEndSelector:@selector(openConnectionPanelDidEnd:returnCode:contextInfo:) - contextInfo:NULL]; - -} -- (void)openConnectionPanelDidEnd:(NSOpenPanel *)panel returnCode:(int)returnCode contextInfo:(void *)contextInfo -{ - if ( returnCode ) { - NSArray *fileNames = [panel filenames]; - NSString *filename; - - for(filename in fileNames) { - if([[[filename pathExtension] lowercaseString] isEqualToString:@"sql"]) { - - NSError *err; - NSString *content = [NSString stringWithContentsOfFile:filename encoding:[[encodingPopUp selectedItem] tag] error:&err]; - - // If file couldn't read show an alert and return - if(!content) { - NSAlert *errorAlert = [NSAlert alertWithError:err]; - [errorAlert runModal]; - return; - } - - // Save last used encoding - [prefs setInteger:[[encodingPopUp selectedItem] tag] forKey:@"lastSqlFileEncoding"]; - - // Check if at least one document exists - if (![[[NSDocumentController sharedDocumentController] documents] count]) { - // TODO : maybe open a connection first - // return; - TableDocument *firstTableDocument; - - // Manually open a new document, setting SPAppController as sender to trigger autoconnection - if (firstTableDocument = [[NSDocumentController sharedDocumentController] makeUntitledDocumentOfType:@"DocumentType" error:nil]) { - [firstTableDocument setShouldAutomaticallyConnect:NO]; - [firstTableDocument initQueryEditorWithString:content]; - [[NSDocumentController sharedDocumentController] addDocument:firstTableDocument]; - [firstTableDocument makeWindowControllers]; - [firstTableDocument showWindows]; - } - } else { - // Pass query to the Query editor of the current document - [[[NSDocumentController sharedDocumentController] currentDocument] doPerformLoadQueryService:content]; - } - - break; // open only the first SQL file - } - else if([[[filename pathExtension] lowercaseString] isEqualToString:@"spf"]) { - TableDocument *newTableDocument; - - // Manually open a new document, setting SPAppController as sender to trigger autoconnection - if (newTableDocument = [[NSDocumentController sharedDocumentController] makeUntitledDocumentOfType:@"DocumentType" error:nil]) { - [newTableDocument setShouldAutomaticallyConnect:NO]; - [[NSDocumentController sharedDocumentController] addDocument:newTableDocument]; - [newTableDocument makeWindowControllers]; - [newTableDocument showWindows]; - [newTableDocument initWithConnectionFile:filename]; - } - } - else { - NSLog(@"Only files with the extensions ‘spf’ or ‘sql’ are allowed."); - } - } - } -} - -/** - * NSOpenPanel delegate to control encoding popup and allowMultipleSelection - */ -- (void)panelSelectionDidChange:(id)sender -{ - - if([sender isKindOfClass:[NSOpenPanel class]]) { - if([[[[sender filename] pathExtension] lowercaseString] isEqualToString:@"sql"]) { - [encodingPopUp setEnabled:YES]; - [sender setAllowsMultipleSelection:NO]; - } else { - [encodingPopUp setEnabled:NO]; - [sender setAllowsMultipleSelection:YES]; - } - } - -} /** * Saves SP session or if Custom Query tab is active the editor's content as SQL file @@ -1735,9 +1622,7 @@ [prefs synchronize]; } - // Set up encoding list [encodingPopUp setEnabled:YES]; - // [self setupPopUp:encodingPopUp selectedEncoding:[prefs integerForKey:@"lastSqlFileEncoding"] withDefaultEntry:NO]; } else if([sender tag] == 1){ @@ -1942,8 +1827,7 @@ { return ([self database] != nil); } - - + // Change "Save Query/Queries" menu item title dynamically // and disable it if no query in the editor if ([menuItem action] == @selector(saveConnectionSheet:) && [menuItem tag] == 0) { -- cgit v1.2.3