From 8b8f3e6cea540b17262aadf6d97a8ad28fe41c03 Mon Sep 17 00:00:00 2001 From: stuconnolly Date: Tue, 21 Jul 2009 16:47:11 +0000 Subject: Merge framework integration branch back to trunk. Summary of changes: - Includes all custom code from subclasses CMMCPConnection and CMMCPResult, meaning they have subsequently been removed from the project. - All previous Sequel Pro specific code in the above subclasses has been removed in favour of the delegate (currently set to TableDocumet) informing the framework of such information. - All references to CMMCPConnection and CMMCPResult have subsequently been changed to MCPConnection and MCPResult. - Framework includes MySQL 5.1.36 client libraries and source headers. - Framework is now built as a 4-way (32/64 bit, i386/PPC arch) binary. - All import references to have been changed to . - New script 'build-mysql-client.sh' can be used to build the MySQL client libraries from the MySQL source. See the script's header for a list of available options or run it with no arguments to display it's usage. Note that there are still a few changes to be made to the framework with regard to removing Sequel Pro specific calls to the delegate. These however can be made later on as they have no effect on functionality and are merely design changes. Also, note that any future development done on the framework should be made to be as 'generic' as possible, with no Sequel Pro specific references. This should allow the framework to be integrated into another project without the need for SP specific code. --- Source/TableDocument.m | 110 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 84 insertions(+), 26 deletions(-) (limited to 'Source/TableDocument.m') diff --git a/Source/TableDocument.m b/Source/TableDocument.m index a01c4ed4..ae2457e3 100644 --- a/Source/TableDocument.m +++ b/Source/TableDocument.m @@ -40,8 +40,6 @@ #import "SPDatabaseData.h" #import "SPStringAdditions.h" #import "SPArrayAdditions.h" -#import "CMMCPConnection.h" -#import "CMMCPResult.h" #import "MainController.h" #import "SPExtendedTableInfo.h" #import "SPConnectionController.h" @@ -59,6 +57,7 @@ { if ((self = [super init])) { + _mainNibLoaded = NO; _encoding = [[NSString alloc] initWithString:@"utf8"]; _isConnected = NO; chooseDatabaseButton = nil; @@ -81,6 +80,8 @@ - (void)awakeFromNib { + if (_mainNibLoaded) return; + _mainNibLoaded = YES; // The first window should use autosaving; subsequent windows should cascade BOOL usedAutosave = [tableWindow setFrameAutosaveName:[self windowNibName]]; @@ -106,6 +107,9 @@ // Register observers for when the logging preference changes [prefs addObserver:[SPQueryConsole sharedQueryConsole] forKeyPath:@"ConsoleEnableLogging" options:NSKeyValueObservingOptionNew context:NULL]; + + // Register a second observer for when the logging preference changes so we can tell the current connection about it + [prefs addObserver:self forKeyPath:@"ConsoleEnableLogging" options:NSKeyValueObservingOptionNew context:NULL]; // Find the Database -> Database Encoding menu (it's not in our nib, so we can't use interface builder) selectEncodingMenu = [[[[[NSApp mainMenu] itemWithTag:1] submenu] itemWithTag:1] submenu]; @@ -123,22 +127,24 @@ av.size.width, av.size.height); [titleAccessoryView setFrame:initialAccessoryViewFrame]; - [windowFrame addSubview:titleAccessoryView]; + [windowFrame addSubview:titleAccessoryView]; + + // Load additional nibs + if (![NSBundle loadNibNamed:@"ConnectionErrorDialog" owner:self]) { + NSLog(@"Connection error dialog could not be loaded; connection failure handling will not function correctly."); + } } #pragma mark - #pragma mark Connection callback and methods -- (void) setConnection:(CMMCPConnection *)theConnection +- (void) setConnection:(MCPConnection *)theConnection { - CMMCPResult *theResult; + MCPResult *theResult; id version; _isConnected = YES; mySQLConnection = [theConnection retain]; - - // Register as the connection delegate - [mySQLConnection setDelegate:self]; // Set the connection encoding NSString *encodingName = [prefs objectForKey:@"DefaultEncoding"]; @@ -574,7 +580,7 @@ // Notify listeners that a query has started [[NSNotificationCenter defaultCenter] postNotificationName:@"SMySQLQueryWillBePerformed" object:self]; - CMMCPResult *theResult = [mySQLConnection queryString:@"SELECT DATABASE()"]; + MCPResult *theResult = [mySQLConnection queryString:@"SELECT DATABASE()"]; if ( [[mySQLConnection getLastErrorMessage] isEqualToString:@""] ) { int i; int r = [theResult numOfRows]; @@ -658,7 +664,7 @@ if ( [[mySQLConnection getLastErrorMessage] isEqualToString:@""] ) { if (_encodingViaLatin1) [mySQLConnection queryString:@"SET CHARACTER_SET_RESULTS=latin1"]; - [mySQLConnection setEncoding:[CMMCPConnection encodingForMySQLEncoding:[mysqlEncoding UTF8String]]]; + [mySQLConnection setEncoding:[MCPConnection encodingForMySQLEncoding:[mysqlEncoding UTF8String]]]; [_encoding release]; _encoding = [[NSString alloc] initWithString:mysqlEncoding]; } else { @@ -695,9 +701,10 @@ } /** - * Returns whether the current encoding should display results via Latin1 transport for backwards compatibility + * Returns whether the current encoding should display results via Latin1 transport for backwards compatibility. + * This is a delegate method of MCPKit's MCPConnection class. */ -- (BOOL)connectionEncodingViaLatin1 +- (BOOL)connectionEncodingViaLatin1:(id)connection { return _encodingViaLatin1; } @@ -863,7 +870,7 @@ if( query == nil ) return; - CMMCPResult *theResult = [mySQLConnection queryString:query]; + MCPResult *theResult = [mySQLConnection queryString:query]; // Check for errors, only displaying if the connection hasn't been terminated if (![[mySQLConnection getLastErrorMessage] isEqualToString:@""]) { @@ -918,7 +925,7 @@ if( query == nil ) return; - CMMCPResult *theResult = [mySQLConnection queryString:query]; + MCPResult *theResult = [mySQLConnection queryString:query]; // Check for errors, only displaying if the connection hasn't been terminated if (![[mySQLConnection getLastErrorMessage] isEqualToString:@""]) { @@ -974,7 +981,7 @@ */ - (IBAction)checkTable:(id)sender { - CMMCPResult *theResult = [mySQLConnection queryString:[NSString stringWithFormat:@"CHECK TABLE %@", [[self table] backtickQuotedString]]]; + MCPResult *theResult = [mySQLConnection queryString:[NSString stringWithFormat:@"CHECK TABLE %@", [[self table] backtickQuotedString]]]; // Check for errors, only displaying if the connection hasn't been terminated if (![[mySQLConnection getLastErrorMessage] isEqualToString:@""]) { @@ -1019,7 +1026,7 @@ */ - (IBAction)analyzeTable:(id)sender { - CMMCPResult *theResult = [mySQLConnection queryString:[NSString stringWithFormat:@"ANALYZE TABLE %@", [[self table] backtickQuotedString]]]; + MCPResult *theResult = [mySQLConnection queryString:[NSString stringWithFormat:@"ANALYZE TABLE %@", [[self table] backtickQuotedString]]]; // Check for errors, only displaying if the connection hasn't been terminated if (![[mySQLConnection getLastErrorMessage] isEqualToString:@""]) { @@ -1064,7 +1071,7 @@ */ - (IBAction)optimizeTable:(id)sender { - CMMCPResult *theResult = [mySQLConnection queryString:[NSString stringWithFormat:@"OPTIMIZE TABLE %@", [[self table] backtickQuotedString]]]; + MCPResult *theResult = [mySQLConnection queryString:[NSString stringWithFormat:@"OPTIMIZE TABLE %@", [[self table] backtickQuotedString]]]; // Check for errors, only displaying if the connection hasn't been terminated if (![[mySQLConnection getLastErrorMessage] isEqualToString:@""]) { @@ -1109,7 +1116,7 @@ */ - (IBAction)repairTable:(id)sender { - CMMCPResult *theResult = [mySQLConnection queryString:[NSString stringWithFormat:@"REPAIR TABLE %@", [[self table] backtickQuotedString]]]; + MCPResult *theResult = [mySQLConnection queryString:[NSString stringWithFormat:@"REPAIR TABLE %@", [[self table] backtickQuotedString]]]; // Check for errors, only displaying if the connection hasn't been terminated if (![[mySQLConnection getLastErrorMessage] isEqualToString:@""]) { @@ -1190,7 +1197,7 @@ */ - (IBAction)checksumTable:(id)sender { - CMMCPResult *theResult = [mySQLConnection queryString:[NSString stringWithFormat:@"CHECKSUM TABLE %@", [[self table] backtickQuotedString]]]; + MCPResult *theResult = [mySQLConnection queryString:[NSString stringWithFormat:@"CHECKSUM TABLE %@", [[self table] backtickQuotedString]]]; // Check for errors, only displaying if the connection hasn't been terminated if (![[mySQLConnection getLastErrorMessage] isEqualToString:@""]) { @@ -1235,6 +1242,14 @@ [NSApp stopModalWithCode:0]; } +/** + * Invoked when user dismisses the error sheet displayed as a result of the current connection being lost. + */ +- (IBAction)closeErrorConnectionSheet:(id)sender +{ + [NSApp stopModalWithCode:[sender tag]]; +} + /** * Passes query to tablesListInstance */ @@ -1266,7 +1281,7 @@ */ - (void)showVariables:(id)sender { - CMMCPResult *theResult; + MCPResult *theResult; NSMutableArray *tempResult = [NSMutableArray array]; int i; @@ -1302,6 +1317,16 @@ notificationName:@"Disconnected"]; } +/** + * This method is called as part of Key Value Observing which is used to watch for prefernce changes which effect the interface. + */ +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context +{ + if ([keyPath isEqualToString:@"ConsoleEnableLogging"]) { + [mySQLConnection setDelegateQueryLogging:[[change objectForKey:NSKeyValueChangeNewKey] boolValue]]; + } +} + #pragma mark - #pragma mark Getter methods @@ -1996,12 +2021,12 @@ #pragma mark - -#pragma mark SMySQL delegate methods +#pragma mark MCPKit connection delegate methods /** - * Invoked when framework will perform a query + * Invoked when framework is about to perform a query. */ -- (void)willQueryString:(NSString *)query +- (void)willQueryString:(NSString *)query connection:(id)connection { if ([[NSUserDefaults standardUserDefaults] boolForKey:@"ConsoleEnableLogging"]) { [[SPQueryConsole sharedQueryConsole] showMessageInConsole:query]; @@ -2009,20 +2034,53 @@ } /** - * Invoked when query gave an error + * Invoked when the query just executed resulted in an error. */ -- (void)queryGaveError:(NSString *)error +- (void)queryGaveError:(NSString *)error connection:(id)connection { [[SPQueryConsole sharedQueryConsole] showErrorInConsole:error]; } +/** + * Invoked when the current connection needs a password from the Keychain. + */ +- (NSString *)keychainPasswordForConnection:(MCPConnection *)connection +{ + KeyChain *keychain = [[KeyChain alloc] init]; + + NSString *password = [keychain getPasswordForName:[connectionController connectionKeychainItemName] account:[connectionController connectionKeychainItemAccount]]; + + [keychain release]; + + return password; +} + +/** + * Invoked when the connection fails and the framework needs to know how to proceed. + */ +- (MCPConnectionCheck)connectionLost:(id)connection +{ + [NSApp beginSheet:connectionErrorDialog modalForWindow:tableWindow modalDelegate:self didEndSelector:nil contextInfo:nil]; + int connectionErrorCode = [NSApp runModalForWindow:connectionErrorDialog]; + + [NSApp endSheet:connectionErrorDialog]; + [connectionErrorDialog orderOut:nil]; + + // If "disconnect" was selected, trigger a window close. + [self windowWillClose:nil]; + if (connectionErrorCode == MCPConnectionCheckDisconnect) + [tableWindow performSelector:@selector(close) withObject:nil afterDelay:0.0]; + + return connectionErrorCode; +} + #pragma mark - #pragma mark Database name field delegate methods /** * When adding a database, enable the button only if the new name has a length. */ -- (void) controlTextDidChange:(NSNotification *)aNotification +- (void)controlTextDidChange:(NSNotification *)aNotification { if ([aNotification object] == databaseNameField) { [addDatabaseButton setEnabled:([[databaseNameField stringValue] length] > 0)]; -- cgit v1.2.3