diff options
author | Bibiko <bibiko@eva.mpg.de> | 2009-08-22 14:49:48 +0000 |
---|---|---|
committer | Bibiko <bibiko@eva.mpg.de> | 2009-08-22 14:49:48 +0000 |
commit | 3f832f629faf1569637684d6f23a938f6d74553c (patch) | |
tree | 815459ffebf2094213458995c5a8cdf93a1dc422 | |
parent | 54ed811d6071cc843a9d2699d3ca972af9ca4c5d (diff) | |
download | sequelpro-3f832f629faf1569637684d6f23a938f6d74553c.tar.gz sequelpro-3f832f629faf1569637684d6f23a938f6d74553c.tar.bz2 sequelpro-3f832f629faf1569637684d6f23a938f6d74553c.zip |
• moved NSOpenPanel stuff from TableDocument to SPAppControler to simplify and unify it
• fixed: avoid opening of more than NSOpenPanel windows
• if conncetionController is active do not allow a NSOpenPanel
• minor code cleaning
-rw-r--r-- | Source/SPAppController.m | 117 | ||||
-rw-r--r-- | Source/TableDocument.h | 3 | ||||
-rw-r--r-- | Source/TableDocument.m | 118 |
3 files changed, 83 insertions, 155 deletions
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) { |