From 9e6adb426de189704f0d0fe09b926b286dbb813e Mon Sep 17 00:00:00 2001 From: stuconnolly Date: Mon, 26 Oct 2009 23:56:40 +0000 Subject: Don't run the add new database sheet modally, thus blocking the main thread. Part of issue #357. --- Interfaces/English.lproj/DBView.xib | 156 ++++++++++---------------- Source/TableDocument.h | 1 - Source/TableDocument.m | 213 ++++++++++++++++++------------------ Source/TablesList.m | 1 - 4 files changed, 169 insertions(+), 202 deletions(-) diff --git a/Interfaces/English.lproj/DBView.xib b/Interfaces/English.lproj/DBView.xib index b8da8f5b..f31b505c 100644 --- a/Interfaces/English.lproj/DBView.xib +++ b/Interfaces/English.lproj/DBView.xib @@ -3,7 +3,7 @@ 1050 10B504 - 732 + 740 1038.2 437.00 @@ -16,20 +16,20 @@ YES - 732 - 732 + 740 + 740 1.2.1 YES - + YES com.brandonwalkin.BWToolkit - com.apple.InterfaceBuilder.CocoaPlugin com.apple.WebKitIBPlugin + com.apple.InterfaceBuilder.CocoaPlugin YES @@ -59,10 +59,10 @@ Connecting... NSWindow View - {1.79769e+308, 1.79769e+308} + {3.40282e+38, 3.40282e+38} {780, 480} - + 256 YES @@ -237,7 +237,7 @@ controlColor 3 - MC42NjY2NjY2NjY3AA + MC42NjY2NjY2ODY1AA @@ -281,7 +281,6 @@ {212, 26} - 6 @@ -314,7 +313,6 @@ {212, 26} - 0 @@ -412,7 +410,6 @@ {212, 354} - 4 @@ -439,7 +436,6 @@ {212, 354} - 528 @@ -575,7 +571,6 @@ {212, 145} - 6 @@ -602,7 +597,6 @@ {212, 145} - 512 @@ -2953,29 +2947,6 @@ 6418 - - YES - - YES - Apple HTML pasteboard type - Apple PDF pasteboard type - Apple PICT pasteboard type - Apple PNG pasteboard type - Apple URL pasteboard type - CorePasteboardFlavorType 0x6D6F6F76 - NSColor pasteboard type - NSFilenamesPboardType - NSStringPboardType - NeXT Encapsulated PostScript v1.2 pasteboard type - NeXT RTFD pasteboard type - NeXT Rich Text Format v1.0 pasteboard type - NeXT TIFF v4.0 pasteboard type - NeXT font pasteboard type - NeXT ruler pasteboard type - WebURLsWithTitlesPboardType - public.url - - {688, 14} @@ -3037,7 +3008,6 @@ {{1, 1}, {688, 155}} - @@ -3069,7 +3039,6 @@ {690, 157} - 530 @@ -3159,7 +3128,6 @@ {{1, 17}, {688, 228}} - 4 @@ -3192,7 +3160,6 @@ {{1, 0}, {688, 17}} - 4 @@ -3201,7 +3168,6 @@ {690, 246} - 562 @@ -3652,7 +3618,7 @@ YES - 1 + 18 YES 1 YES @@ -4866,11 +4832,10 @@ {944, 550} - {{0, 0}, {1920, 1178}} {780, 502} - {1.79769e+308, 1.79769e+308} + {3.40282e+38, 3.40282e+38} @@ -4886,7 +4851,7 @@ {292, 112} {292, 112} - + 256 YES @@ -5312,6 +5277,7 @@ {292, 112} + {{0, 0}, {1440, 878}} {292, 134} @@ -6041,7 +6007,7 @@ View - {1.79769e+308, 1.79769e+308} + {3.40282e+38, 3.40282e+38} {213, 107} @@ -6320,7 +6286,7 @@ {{0, 0}, {1440, 878}} {213, 129} - {1.79769e+308, 1.79769e+308} + {3.40282e+38, 3.40282e+38} 1 @@ -6332,7 +6298,7 @@ View - {1.79769e+308, 1.79769e+308} + {3.40282e+38, 3.40282e+38} {213, 107} @@ -6488,7 +6454,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 {{0, 0}, {1440, 878}} {213, 129} - {1.79769e+308, 1.79769e+308} + {3.40282e+38, 3.40282e+38} 1 @@ -6498,7 +6464,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 New Relation NSPanel - {1.79769e+308, 1.79769e+308} + {3.40282e+38, 3.40282e+38} 256 @@ -7099,7 +7065,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 {302, 307} {{0, 0}, {1440, 878}} - {1.79769e+308, 1.79769e+308} + {3.40282e+38, 3.40282e+38} 3 @@ -7111,7 +7077,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 View - {1.79769e+308, 1.79769e+308} + {3.40282e+38, 3.40282e+38} {213, 50} @@ -7231,7 +7197,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 {{0, 0}, {1920, 1178}} {213, 72} - {1.79769e+308, 1.79769e+308} + {3.40282e+38, 3.40282e+38} 9 @@ -7243,7 +7209,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 View - {1.79769e+308, 1.79769e+308} + {3.40282e+38, 3.40282e+38} {213, 107} @@ -7378,7 +7344,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 {{0, 0}, {1440, 878}} {213, 129} - {1.79769e+308, 1.79769e+308} + {3.40282e+38, 3.40282e+38} 15 @@ -7390,7 +7356,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 View - {1.79769e+308, 1.79769e+308} + {3.40282e+38, 3.40282e+38} {350, 200} @@ -7694,7 +7660,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 {{0, 0}, {1440, 878}} {350, 222} - {1.79769e+308, 1.79769e+308} + {3.40282e+38, 3.40282e+38} 15 @@ -7706,7 +7672,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 View - {1.79769e+308, 1.79769e+308} + {3.40282e+38, 3.40282e+38} {350, 200} @@ -7908,7 +7874,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 {{0, 0}, {1680, 1028}} {350, 222} - {1.79769e+308, 1.79769e+308} + {3.40282e+38, 3.40282e+38} 9 @@ -7920,7 +7886,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 View - {1.79769e+308, 1.79769e+308} + {3.40282e+38, 3.40282e+38} {213, 107} @@ -8069,7 +8035,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 {{0, 0}, {1440, 878}} {213, 129} - {1.79769e+308, 1.79769e+308} + {3.40282e+38, 3.40282e+38} 31 @@ -8079,7 +8045,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 MySQL Help NSPanel - {1.79769e+308, 1.79769e+308} + {3.40282e+38, 3.40282e+38} {351, 120} @@ -8473,7 +8439,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 {{0, 0}, {1280, 1002}} {351, 136} - {1.79769e+308, 1.79769e+308} + {3.40282e+38, 3.40282e+38} MYSQL_HELP_WINDOW @@ -8486,7 +8452,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 View - {1.79769e+308, 1.79769e+308} + {3.40282e+38, 3.40282e+38} {350, 250} @@ -8877,7 +8843,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 {{0, 0}, {1920, 1178}} {350, 272} - {1.79769e+308, 1.79769e+308} + {3.40282e+38, 3.40282e+38} 9 @@ -9010,7 +8976,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 Secure Text Input Sheet NSPanel - {1.79769e+308, 1.79769e+308} + {3.40282e+38, 3.40282e+38} 256 @@ -9114,7 +9080,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 {338, 138} {{0, 0}, {1280, 778}} - {1.79769e+308, 1.79769e+308} + {3.40282e+38, 3.40282e+38} @@ -10980,7 +10946,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 disabledControlTextColor 3 - MC4zMzMzMzMzMzMzAA + MC4zMzMzMzMzNDMzAA @@ -11758,22 +11724,6 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 584 - - - closeDatabaseSheet: - - - - 585 - - - - closeDatabaseSheet: - - - - 586 - tableDocumentInstance @@ -12654,14 +12604,6 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 4778 - - - addDatabaseButton - - - - 4780 - delegate @@ -15211,6 +15153,30 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 6629 + + + closePanelSheet: + + + + 6632 + + + + closePanelSheet: + + + + 6633 + + + + addDatabaseButton + + + + 6634 + @@ -24798,7 +24764,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 - 6629 + 6634 @@ -25605,7 +25571,6 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 checksumTable: chooseDatabase: chooseEncoding: - closeDatabaseSheet: closeErrorConnectionSheet: closePanelSheet: closePasswordSheet: @@ -25683,7 +25648,6 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 id id id - id 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 @@ -3631,6 +3554,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]; -- cgit v1.2.3