diff options
author | stuconnolly <stuart02@gmail.com> | 2009-10-26 23:56:40 +0000 |
---|---|---|
committer | stuconnolly <stuart02@gmail.com> | 2009-10-26 23:56:40 +0000 |
commit | 9e6adb426de189704f0d0fe09b926b286dbb813e (patch) | |
tree | c11c6cd5c89b2daf4bccf0d97156fef2d3db1931 /Source | |
parent | 332c2308514c1c0fe839dd0124d90cd8d24fd9ef (diff) | |
download | sequelpro-9e6adb426de189704f0d0fe09b926b286dbb813e.tar.gz sequelpro-9e6adb426de189704f0d0fe09b926b286dbb813e.tar.bz2 sequelpro-9e6adb426de189704f0d0fe09b926b286dbb813e.zip |
Don't run the add new database sheet modally, thus blocking the main thread. Part of issue #357.
Diffstat (limited to 'Source')
-rw-r--r-- | Source/TableDocument.h | 1 | ||||
-rw-r--r-- | Source/TableDocument.m | 213 | ||||
-rw-r--r-- | Source/TablesList.m | 1 |
3 files changed, 109 insertions, 106 deletions
diff --git a/Source/TableDocument.h b/Source/TableDocument.h index a6969136..ef941f60 100644 --- a/Source/TableDocument.h +++ b/Source/TableDocument.h @@ -176,7 +176,6 @@ enum sp_current_query_mode - (IBAction)setDatabases:(id)sender; - (IBAction)chooseDatabase:(id)sender; - (IBAction)addDatabase:(id)sender; -- (IBAction)closeDatabaseSheet:(id)sender; - (IBAction)removeDatabase:(id)sender; - (IBAction)showMySQLHelp:(id)sender; - (IBAction)saveServerVariables:(id)sender; diff --git a/Source/TableDocument.m b/Source/TableDocument.m index daa8d049..2515dde2 100644 --- a/Source/TableDocument.m +++ b/Source/TableDocument.m @@ -60,6 +60,8 @@ @interface TableDocument (PrivateAPI) +- (void)_addDatabase; +- (void)_removeDatabase; - (void)_updateServerVariablesFilterForFilterString:(NSString *)filterString; - (void)_copyServerVariablesToPasteboardIncludingName:(BOOL)name andValue:(BOOL)value; @@ -833,7 +835,6 @@ // Process the template and display the results. NSString *result = [engine processTemplateInFileAtPath:templatePath withVariables:print_data]; - //NSLog(@"result %@", result); return result; } @@ -942,76 +943,15 @@ */ - (IBAction)addDatabase:(id)sender { - int code = 0; - - if (![tablesListInstance selectionShouldChangeInTableView:nil]) { - return; - } + if (![tablesListInstance selectionShouldChangeInTableView:nil]) return; [databaseNameField setStringValue:@""]; [NSApp beginSheet:databaseSheet modalForWindow:tableWindow modalDelegate:self - didEndSelector:nil - contextInfo:nil]; - - code = [NSApp runModalForWindow:databaseSheet]; - - [NSApp endSheet:databaseSheet]; - [databaseSheet orderOut:nil]; - - if (!code) { - (![self database]) ? [chooseDatabaseButton selectItemAtIndex:0] : [chooseDatabaseButton selectItemWithTitle:[self database]]; - return; - } - - // This check is not necessary anymore as the add database button is now only enabled if the name field - // has a length greater than zero. We'll leave it in just in case. - if ([[databaseNameField stringValue] isEqualToString:@""]) { - NSBeginAlertSheet(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, tableWindow, self, nil, nil, nil, NSLocalizedString(@"Database must have a name.", @"message of panel when no db name is given")); - return; - } - - NSString *createStatement = [NSString stringWithFormat:@"CREATE DATABASE %@", [[databaseNameField stringValue] backtickQuotedString]]; - - // If there is an encoding selected other than the default we must specify it in CREATE DATABASE statement - if ([databaseEncodingButton indexOfSelectedItem] > 0) { - createStatement = [NSString stringWithFormat:@"%@ DEFAULT CHARACTER SET %@", createStatement, [[self mysqlEncodingFromDisplayEncoding:[databaseEncodingButton title]] backtickQuotedString]]; - } - - // Create the database - [mySQLConnection queryString:createStatement]; - - if (![[mySQLConnection getLastErrorMessage] isEqualToString:@""]) { - //error while creating db - NSBeginAlertSheet(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, tableWindow, self, nil, nil, nil, [NSString stringWithFormat:NSLocalizedString(@"Couldn't create database.\nMySQL said: %@", @"message of panel when creation of db failed"), [mySQLConnection getLastErrorMessage]]); - return; - } - - if (![mySQLConnection selectDB:[databaseNameField stringValue]] ) { //error while selecting new db (is this possible?!) - NSBeginAlertSheet(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, tableWindow, self, nil, nil, nil, [NSString stringWithFormat:NSLocalizedString(@"Unable to connect to database %@.\nBe sure that you have the necessary privileges.", @"message of panel when connection to db failed after selecting from popupbutton"), - [databaseNameField stringValue]]); - [self setDatabases:self]; - return; - } - - //select new db - if (selectedDatabase) [selectedDatabase release], selectedDatabase = nil; - selectedDatabase = [[NSString alloc] initWithString:[databaseNameField stringValue]]; - [self setDatabases:self]; - [tablesListInstance setConnection:mySQLConnection]; - [tableDumpInstance setConnection:mySQLConnection]; - - [tableWindow setTitle:[self displaySPName]]; -} - -/** - * closes the add-db sheet and stops modal session - */ -- (IBAction)closeDatabaseSheet:(id)sender -{ - [NSApp stopModalWithCode:[sender tag]]; + didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:) + contextInfo:@"addDatabase"]; } /** @@ -1019,11 +959,10 @@ */ - (IBAction)removeDatabase:(id)sender { - if ([chooseDatabaseButton indexOfSelectedItem] == 0) - return; + // No database selected, bail + if ([chooseDatabaseButton indexOfSelectedItem] == 0) return; - if (![tablesListInstance selectionShouldChangeInTableView:nil]) - return; + if (![tablesListInstance selectionShouldChangeInTableView:nil]) return; NSAlert *alert = [NSAlert alertWithMessageText:[NSString stringWithFormat:NSLocalizedString(@"Delete database '%@'?", @"delete database message"), [self database]] defaultButton:NSLocalizedString(@"Delete", @"delete button") @@ -1039,10 +978,10 @@ [alert setAlertStyle:NSCriticalAlertStyle]; - [alert beginSheetModalForWindow:tableWindow modalDelegate:self didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:) contextInfo:@"removedatabase"]; + [alert beginSheetModalForWindow:tableWindow modalDelegate:self didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:) contextInfo:@"removeDatabase"]; } -/* +/** * Returns an array of all available database names */ - (NSArray *)allDatabaseNames @@ -1051,39 +990,28 @@ } /** - * alert sheets method - * invoked when alertSheet get closed - * if contextInfo == removedatabase -> tries to remove the selected database + * Alert sheet method. Invoked when an alert sheet is dismissed. + * + * if contextInfo == removeDatabase -> Remove the selected database + * if contextInfo == addDatabase -> Add a new database */ - (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(NSString *)contextInfo { - if ([contextInfo isEqualToString:@"removedatabase"]) { - if (returnCode != NSAlertDefaultReturn) - return; - - [mySQLConnection queryString:[NSString stringWithFormat:@"DROP DATABASE %@", [[self database] backtickQuotedString]]]; - if (![[mySQLConnection getLastErrorMessage] isEqualToString:@""]) { - // error while deleting db - [self performSelector:@selector(showErrorSheetWith:) - withObject:[NSArray arrayWithObjects:NSLocalizedString(@"Error", @"error"), - [NSString stringWithFormat:NSLocalizedString(@"Couldn't remove database.\nMySQL said: %@", @"message of panel when removing db failed"), - [mySQLConnection getLastErrorMessage]], - nil] - afterDelay:0.3]; - return; + // Remove the current database + if ([contextInfo isEqualToString:@"removeDatabase"]) { + if (returnCode == NSAlertDefaultReturn) { + [self _removeDatabase]; + } + } + // Add a new database + else if ([contextInfo isEqualToString:@"addDatabase"]) { + if (returnCode == NSOKButton) { + [self _addDatabase]; } - - // db deleted with success - if (selectedDatabase) [selectedDatabase release], selectedDatabase = nil; - [self setDatabases:self]; - [tablesListInstance setConnection:mySQLConnection]; - [tableDumpInstance setConnection:mySQLConnection]; - - [tableWindow setTitle:[self displaySPName]]; } } -/* +/** * Show Error sheet (can be called from inside of a endSheet selector) * via [self performSelector:@selector(showErrorSheetWithTitle:) withObject: afterDelay:] */ @@ -1095,11 +1023,10 @@ [error objectAtIndex:1]); } - -/* +/** * Reset the current selected database name */ -- (void) refreshCurrentDatabase +- (void)refreshCurrentDatabase { NSString *dbName; @@ -1130,7 +1057,6 @@ //query finished [[NSNotificationCenter defaultCenter] postNotificationName:@"SMySQLQueryHasBeenPerformed" object:self]; - } #pragma mark - @@ -2476,7 +2402,6 @@ // For dispatching later if(![[spf objectForKey:@"format"] isEqualToString:@"connection"]) { NSLog(@"SPF file format is not 'connection'."); - // [self close]; return NO; } @@ -2658,8 +2583,6 @@ [spfdata setObject:[encryptdata dataEncryptedWithPassword:[spfDocData_temp objectForKey:@"e_string"]] forKey:@"data"]; } - // NSLog(@"%@", spfdata); - NSString *err = nil; NSData *plist = [NSPropertyListSerialization dataFromPropertyList:spfdata format:NSPropertyListXMLFormat_v1_0 @@ -3632,6 +3555,88 @@ @implementation TableDocument (PrivateAPI) /** + * Adds a new database. + */ +- (void)_addDatabase +{ + // This check is not necessary anymore as the add database button is now only enabled if the name field + // has a length greater than zero. We'll leave it in just in case. + if ([[databaseNameField stringValue] isEqualToString:@""]) { + NSBeginAlertSheet(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, tableWindow, self, nil, nil, nil, NSLocalizedString(@"Database must have a name.", @"message of panel when no db name is given")); + return; + } + + NSString *createStatement = [NSString stringWithFormat:@"CREATE DATABASE %@", [[databaseNameField stringValue] backtickQuotedString]]; + + // If there is an encoding selected other than the default we must specify it in CREATE DATABASE statement + if ([databaseEncodingButton indexOfSelectedItem] > 0) { + createStatement = [NSString stringWithFormat:@"%@ DEFAULT CHARACTER SET %@", createStatement, [[self mysqlEncodingFromDisplayEncoding:[databaseEncodingButton title]] backtickQuotedString]]; + } + + // Create the database + [mySQLConnection queryString:createStatement]; + + if (![[mySQLConnection getLastErrorMessage] isEqualToString:@""]) { + // An error occurred + NSBeginAlertSheet(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, tableWindow, self, nil, nil, nil, [NSString stringWithFormat:NSLocalizedString(@"Couldn't create database.\nMySQL said: %@", @"message of panel when creation of db failed"), [mySQLConnection getLastErrorMessage]]); + + return; + } + + // Error while selecting the new database (is this even possible?) + if (![mySQLConnection selectDB:[databaseNameField stringValue]] ) { + NSBeginAlertSheet(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, tableWindow, self, nil, nil, nil, [NSString stringWithFormat:NSLocalizedString(@"Unable to connect to database %@.\nBe sure that you have the necessary privileges.", @"message of panel when connection to db failed after selecting from popupbutton"), [databaseNameField stringValue]]); + + [self setDatabases:self]; + + return; + } + + // Select the new database + if (selectedDatabase) [selectedDatabase release], selectedDatabase = nil; + + + selectedDatabase = [[NSString alloc] initWithString:[databaseNameField stringValue]]; + [self setDatabases:self]; + + [tablesListInstance setConnection:mySQLConnection]; + [tableDumpInstance setConnection:mySQLConnection]; + + [tableWindow setTitle:[self displaySPName]]; +} + +/** + * Removes the current database. + */ +- (void)_removeDatabase +{ + // Drop the database from the server + [mySQLConnection queryString:[NSString stringWithFormat:@"DROP DATABASE %@", [[self database] backtickQuotedString]]]; + + if (![[mySQLConnection getLastErrorMessage] isEqualToString:@""]) { + // An error occurred + [self performSelector:@selector(showErrorSheetWith:) + withObject:[NSArray arrayWithObjects:NSLocalizedString(@"Error", @"error"), + [NSString stringWithFormat:NSLocalizedString(@"Couldn't remove database.\nMySQL said: %@", @"message of panel when removing db failed"), + [mySQLConnection getLastErrorMessage]], + nil] + afterDelay:0.3]; + + return; + } + + // Delete was successful + if (selectedDatabase) [selectedDatabase release], selectedDatabase = nil; + + [self setDatabases:self]; + + [tablesListInstance setConnection:mySQLConnection]; + [tableDumpInstance setConnection:mySQLConnection]; + + [tableWindow setTitle:[self displaySPName]]; +} + +/** * Filter the displayed server variables by matching the variable name and value against the * filter string. */ diff --git a/Source/TablesList.m b/Source/TablesList.m index b5ddb0af..2d56938d 100644 --- a/Source/TablesList.m +++ b/Source/TablesList.m @@ -496,7 +496,6 @@ */ - (void)sheetDidEnd:(id)sheet returnCode:(int)returnCode contextInfo:(NSString *)contextInfo { - // Order out current sheet to suppress overlapping of sheets if([sheet respondsToSelector:@selector(orderOut:)]) [sheet orderOut:nil]; |