[Mysql-cocoa-commits] CVS: SMySQL/MCPApplicationKit MCPConnectionWinCont.h,1.1,1.2 MCPConnectionWinC
Brought to you by:
sergecohen
From: Serge C. <ser...@us...> - 2004-05-14 11:16:15
|
Update of /cvsroot/mysql-cocoa/SMySQL/MCPApplicationKit In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv730/MCPApplicationKit Modified Files: MCPConnectionWinCont.h MCPConnectionWinCont.m MCPDocument.h MCPDocument.m Log Message: Quite some work to fix and enhance the MCPApplicationKit. Now supports well the creation of a database, and fixed an issue with the nib file being unproperly placed. 2004-05-14; Serge Cohen. Index: MCPConnectionWinCont.h =================================================================== RCS file: /cvsroot/mysql-cocoa/SMySQL/MCPApplicationKit/MCPConnectionWinCont.h,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** MCPConnectionWinCont.h 11 Oct 2003 22:32:40 -0000 1.1 --- MCPConnectionWinCont.h 14 May 2004 11:16:04 -0000 1.2 *************** *** 23,26 **** --- 23,27 ---- IBOutlet NSTextField *mPasswordField; + IBOutlet NSButton *mCreateButton; // MCPDocument *mMCPDocument; } *************** *** 29,35 **** /*" Actions for Interface Builder "*/ /*" For the clear text information. "*/ ! - (IBAction)doGo:(id) sender; ! - (IBAction)doCancel:(id) sender; ! - (IBAction)modifyInstance:(id) sender; --- 30,37 ---- /*" Actions for Interface Builder "*/ /*" For the clear text information. "*/ ! - (IBAction) doGo:(id) sender; ! - (IBAction) doCancel:(id) sender; ! - (IBAction) doCreate:(id) sender; ! - (IBAction) modifyInstance:(id) sender; *************** *** 41,48 **** /*" Overrides of NSWindowController method, to adapt to this Window Controller. "*/ ! - (id)init; ! - (void)dealloc; ! - (void)windowDidLoad; @end --- 43,52 ---- /*" Overrides of NSWindowController method, to adapt to this Window Controller. "*/ ! - (id) init; ! - (void) dealloc; ! - (void) windowDidLoad; + /*" Getting the button for creating a DB. "*/ + - (NSButton*) getCreateButton; @end Index: MCPConnectionWinCont.m =================================================================== RCS file: /cvsroot/mysql-cocoa/SMySQL/MCPApplicationKit/MCPConnectionWinCont.m,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** MCPConnectionWinCont.m 11 Oct 2003 22:32:40 -0000 1.1 --- MCPConnectionWinCont.m 14 May 2004 11:16:04 -0000 1.2 *************** *** 24,28 **** It is responsible to set the appropriate value of the MCPDocument instance variables. "*/ ! - (IBAction)doGo:(id) sender /*" What to do when the user clicks on the Go button of the window. "*/ { --- 24,28 ---- It is responsible to set the appropriate value of the MCPDocument instance variables. "*/ ! - (IBAction) doGo:(id) sender /*" What to do when the user clicks on the Go button of the window. "*/ { *************** *** 41,45 **** ! - (IBAction)doCancel:(id) sender /*" What to do when the user clicks on the Cancel button of the window. "*/ { --- 41,45 ---- ! - (IBAction) doCancel:(id) sender /*" What to do when the user clicks on the Cancel button of the window. "*/ { *************** *** 48,53 **** } ! - (IBAction)modifyInstance:(id) sender /*" Action to take when the user modify one of the entry of the New Connection dialog. "*/ { --- 48,61 ---- } + - (IBAction) doCreate:(id) sender + { + NSLog(@"Asked for creation of the databes..."); + [(MCPDocument*)[self document] setMCPWillCreateNewDB:YES]; + [self doGo:sender]; + return; + } + ! - (IBAction) modifyInstance:(id) sender /*" Action to take when the user modify one of the entry of the New Connection dialog. "*/ { *************** *** 84,88 **** - (IBAction) askPassword:(id) sender { ! [[NSApplication sharedApplication] beginSheet:mPasswordSheet modalForWindow:[self window] modalDelegate:[self document] didEndSelector:@selector(MCPpasswordSheetDidEnd:returnCode:contextInfo:) contextInfo:self]; return; } --- 92,96 ---- - (IBAction) askPassword:(id) sender { ! [[NSApplication sharedApplication] beginSheet:mPasswordSheet modalForWindow:[self window] modalDelegate:[self document] didEndSelector:@selector(MCPPasswordSheetDidEnd:returnCode:contextInfo:) contextInfo:self]; return; } *************** *** 98,102 **** /*" Overrides of NSWindowController method, to adapt to this Window Controller. "*/ ! - (id)init /*" When inited, open the proper window... "*/ { --- 106,110 ---- /*" Overrides of NSWindowController method, to adapt to this Window Controller. "*/ ! - (id) init /*" When inited, open the proper window... "*/ { *************** *** 105,109 **** } ! - (void)dealloc /*" Gives notification that the WindowController is being deallocated (is it really useful? not yet!). "*/ { --- 113,117 ---- } ! - (void) dealloc /*" Gives notification that the WindowController is being deallocated (is it really useful? not yet!). "*/ { *************** *** 113,117 **** ! - (void)windowDidLoad /*" What to do once the window has been loaded : update the fields... "*/ { --- 121,125 ---- ! - (void) windowDidLoad /*" What to do once the window has been loaded : update the fields... "*/ { *************** *** 130,140 **** } [mPortField setIntValue:[theDoc MCPPort]]; return; } ! ! ! ! @end --- 138,157 ---- } [mPortField setIntValue:[theDoc MCPPort]]; + if ([theDoc MCPModelName] && (! [[theDoc MCPModelName] isEqualToString:@""])) { + [mCreateButton setHidden:NO]; + [mCreateButton setEnabled:YES]; + } + else { + [mCreateButton setEnabled:NO]; + [mCreateButton setHidden:YES]; + } return; } ! #pragma mark Getting the button for creating a DB. ! - (NSButton*) getCreateButton ! { ! return mCreateButton; ! } @end Index: MCPDocument.h =================================================================== RCS file: /cvsroot/mysql-cocoa/SMySQL/MCPApplicationKit/MCPDocument.h,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** MCPDocument.h 11 Oct 2003 22:32:40 -0000 1.1 --- MCPDocument.h 14 May 2004 11:16:04 -0000 1.2 *************** *** 25,28 **** --- 25,31 ---- NSWindowController *MCPMainWinCont; Class MCPConnectedWinCont; /*" Window controller used once the connection is established (As a class). "*/ + // Handling the DB creation state. + NSString *MCPModelName; + BOOL MCPWillCreateNewDB; } *************** *** 65,68 **** --- 68,81 ---- - (NSWindowController *) MCPMainWinCont; + /*" Accessors to the DB creation instances. "*/ + - (void) setMCPModelName:(NSString *) theModelName; + - (void) setMCPWillCreateNewDB:(BOOL) theWillCreateNewDB; + + - (NSString *) MCPModelName; + - (BOOL) MCPWillCreateNewDB; + + /*" Practical creation of the database, from a model file. "*/ + - (BOOL) createModelDB; + /*" Overrides of NSDocument methods. "*/ // Managing the document in file format *************** *** 76,80 **** /*" Method to take care of the password sheet. "*/ // Callback from sheet ! - (void) MCPpasswordSheetDidEnd:(NSWindow *) sheet returnCode:(int) returnCode contextInfo:(void *) contextInfo; --- 89,93 ---- /*" Method to take care of the password sheet. "*/ // Callback from sheet ! - (void) MCPPasswordSheetDidEnd:(NSWindow *) sheet returnCode:(int) returnCode contextInfo:(void *) contextInfo; Index: MCPDocument.m =================================================================== RCS file: /cvsroot/mysql-cocoa/SMySQL/MCPApplicationKit/MCPDocument.m,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** MCPDocument.m 11 Oct 2003 22:32:40 -0000 1.1 --- MCPDocument.m 14 May 2004 11:16:04 -0000 1.2 *************** *** 42,45 **** --- 42,47 ---- MCPMainWinCont = nil; MCPConnectedWinCont = nil; + MCPWillCreateNewDB = NO; + MCPModelName = @""; } return self; *************** *** 106,110 **** [MCPHost autorelease]; } ! // NSLog (@"In setHost: modification was %@ becomes %@\n", mHost, theHost); MCPHost = [[NSString stringWithString:theHost] retain]; [self updateChangeCount:NSChangeDone]; --- 108,112 ---- [MCPHost autorelease]; } ! // NSLog (@"In setHost: modification was %@ becomes %@\n", MCPHost, theHost); MCPHost = [[NSString stringWithString:theHost] retain]; [self updateChangeCount:NSChangeDone]; *************** *** 126,130 **** [MCPLogin autorelease]; } ! // NSLog (@"In setLogin: modification was %@ becomes %@\n", mLogin, theLogin); MCPLogin = [[NSString stringWithString:theLogin] retain]; [self updateChangeCount:NSChangeDone]; --- 128,132 ---- [MCPLogin autorelease]; } ! // NSLog (@"In setLogin: modification was %@ becomes %@\n", MCPLogin, theLogin); MCPLogin = [[NSString stringWithString:theLogin] retain]; [self updateChangeCount:NSChangeDone]; *************** *** 147,151 **** [MCPDatabase autorelease]; } ! // NSLog (@"In setDatabase: modification was %@ becomes %@\n", mDatabase, theDatabase); MCPDatabase = [[NSString stringWithString:theDatabase] retain]; [self updateChangeCount:NSChangeDone]; --- 149,153 ---- [MCPDatabase autorelease]; } ! // NSLog (@"In setDatabase: modification was %@ becomes %@\n", MCPDatabase, theDatabase); MCPDatabase = [[NSString stringWithString:theDatabase] retain]; [self updateChangeCount:NSChangeDone]; *************** *** 179,183 **** Change the value of mConInfoNeeded AND toggle the states of the object, removing appropriate window and displaying other(s) ... "*/ { ! NSLog (@"In setMCPConInfoNeeded... value is %@ :\n", (theConInfoNeeded)? @"YES" : @"NO"); if (MCPConInfoNeeded == theConInfoNeeded) { NSLog(@"Useless call to setMCPConInfoNeeded\n"); --- 181,185 ---- Change the value of mConInfoNeeded AND toggle the states of the object, removing appropriate window and displaying other(s) ... "*/ { ! // NSLog (@"In setMCPConInfoNeeded... value is %@ :\n", (theConInfoNeeded)? @"YES" : @"NO"); if (MCPConInfoNeeded == theConInfoNeeded) { NSLog(@"Useless call to setMCPConInfoNeeded\n"); *************** *** 196,200 **** } else { ! // Otherwise, we were using a ConnectdWinCont, then turn back to a MCPConnectionWinCont window... theWinCont = [[MCPConnectionWinCont allocWithZone:[self zone]] init]; [theWinCont setShouldCloseDocument:YES]; --- 198,202 ---- } else { ! // Otherwise, we were using a ConnectedWinCont, then turn back to a MCPConnectionWinCont window... theWinCont = [[MCPConnectionWinCont allocWithZone:[self zone]] init]; [theWinCont setShouldCloseDocument:YES]; *************** *** 204,208 **** [theWinCont release]; theArray = [NSArray arrayWithArray:[self windowControllers]]; ! NSLog(@"In setConInfoNeeded, closing browser windows. Number of window attached to the doc : %i\n", [theArray count]); for (i=0; i<[theArray count]; i++) { theWinCont = [theArray objectAtIndex:i]; --- 206,210 ---- [theWinCont release]; theArray = [NSArray arrayWithArray:[self windowControllers]]; ! // NSLog(@"In setConInfoNeeded, closing browser windows. Number of window attached to the doc : %i\n", [theArray count]); for (i=0; i<[theArray count]; i++) { theWinCont = [theArray objectAtIndex:i]; *************** *** 310,313 **** --- 312,420 ---- } + #pragma mark Accessors to the DB creation instances. + - (void) setMCPModelName:(NSString *) theModelName + { + if (MCPModelName && [MCPModelName isEqualToString:theModelName]) { + return; + } + if (MCPConInfoNeeded) { + if (MCPModelName) { + [MCPModelName autorelease]; + } + // NSLog (@"In setMCPModelName: modification was %@ becomes %@\n", MCPModelName, theModelName); + MCPModelName = [[NSString stringWithString:theModelName] retain]; + if (MCPModelName && (![MCPModelName isEqualToString:@""])) { // Show the "Create DB" button. + // NSLog(@"Now should show the CreateDB button... MCPMainWinCont = %@\n", MCPMainWinCont); + [[(MCPConnectionWinCont*)MCPMainWinCont getCreateButton] setHidden:NO]; + [[(MCPConnectionWinCont*)MCPMainWinCont getCreateButton] setEnabled:YES]; + } + else { // Hide the "Create DB" button. + // NSLog(@"Now should hide the CreateDB button... MCPMainWinCont = %@\n", MCPMainWinCont); + [[(MCPConnectionWinCont*)MCPMainWinCont getCreateButton] setEnabled:NO]; + [[(MCPConnectionWinCont*)MCPMainWinCont getCreateButton] setHidden:YES]; + } + } + else { // It is too late to ask for a new database. + NSLog(@"In setMCPModelName: Tryed to set the model, but the connection is already established!!!\n"); + } + return; + } + + - (void) setMCPWillCreateNewDB:(BOOL) theWillCreateNewDB + { + if (MCPModelName && (![MCPModelName isEqualToString:@""])) { + MCPWillCreateNewDB = theWillCreateNewDB; + } + else { + if (theWillCreateNewDB) { + NSLog(@"Tried to set the WillCreateNewDB flag of the MCPDocument, while the Model is not given, will keep old value (NO)!"); + } + } + } + + - (NSString *) MCPModelName + { + return MCPModelName; + } + + - (BOOL) MCPWillCreateNewDB + { + return MCPWillCreateNewDB; + } + + #pragma mark Practical creation of the database, from a model file. + - (BOOL) createModelDB + { + NSString *thePathToModel; + NSString *theModelContent; + NSArray *theArrayOfDefintions; + size_t i; + + NSLog(@"in MCPDocument createModelDB\n"); + // First check that we can proceed... + if (! [MCPConnect isConnected]) { + NSLog(@"Unable to create a DB without being connected first!!!\n"); + return NO; + } + if ((nil == MCPModelName) || ([MCPModelName isEqualToString:@""])) { + NSLog(@"Unable to create a DB without having a proper model!!!\n"); + return NO; + } + // Fetch the path of the model: + thePathToModel = [[NSBundle bundleForClass:[self class]] pathForResource:MCPModelName ofType:@"mysql"]; + if (nil == thePathToModel) { + NSLog(@"Unable to find the model file (%@) for DB creation... cannot create the database.", MCPModelName); + return NO; + } + // theModelContent = [[[NSFileManager defaultManager] contentsAtPath:thePathToModel] ; + theModelContent = [NSString stringWithContentsOfFile:thePathToModel]; + if (nil == theModelContent) { + NSLog(@"Unable to open the model file (%@) for DB creation... cannot create the database.", MCPModelName); + return NO; + } + // Check for existence of the database: + if ([MCPConnect selectDB:MCPDatabase]) { + NSLog(@"The database : %@ already exists... will not fo anything...\n"); + return NO; + } + [MCPConnect queryString:[NSString stringWithFormat:@"create database %@ ;", MCPDatabase]]; + if (! [MCPConnect selectDB:MCPDatabase]) { + NSLog(@"Unable to create database : %@. I guess you don't have enough MySQL permissions for that\n"); + NSLog(@"Error message from MySQL is : %@\n", [MCPConnect getLastErrorMessage]); + return NO; + } + // Now we are connected: + // Cut the model string into queries (using ; as separators). + theArrayOfDefintions = [theModelContent componentsSeparatedByString:@";"]; + for (i=0; i<[theArrayOfDefintions count]; ++i) { + NSString *theQuery = [[theArrayOfDefintions objectAtIndex:i] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; + if (! [theQuery isEqualToString:@""]) { + // NSLog(@"Creating a table using query : %@", theQuery); + [MCPConnect queryString:theQuery]; + } + } + return YES; + } + #pragma mark Overrides of NSDocument methods *************** *** 316,320 **** { NSMutableDictionary *theDict = [NSMutableDictionary dictionaryWithCapacity:4]; ! NSData *theData; NSLog (@"Try to save the connection document under type: %@\n",aType); --- 423,427 ---- { NSMutableDictionary *theDict = [NSMutableDictionary dictionaryWithCapacity:4]; ! NSData *theData; NSLog (@"Try to save the connection document under type: %@\n",aType); *************** *** 384,392 **** /*" What to do when a specific window is loaded: - Nothing for the Connection Info Window (MCPConnectionWindow.nib). ! - Whatever one wants for another window type. "*/ { [super windowControllerDidLoadNib:aController]; ! if ([[aController windowNibName] isEqualToString:@"MCPConnectionWindow"] ) { } return; --- 491,512 ---- /*" What to do when a specific window is loaded: - Nothing for the Connection Info Window (MCPConnectionWindow.nib). ! - Whatever one wants for another window type. ! ! Indeed it looks like this method is never called, unless one has a single window controller document... not the case here... ! "*/ { [super windowControllerDidLoadNib:aController]; ! NSLog(@"In MCPDocument windowControllerDidLoadNib, the controller is : %@, from nib file : %@", aController, [aController windowNibName]); if ([[aController windowNibName] isEqualToString:@"MCPConnectionWindow"] ) { + if (MCPModelName && (![MCPModelName isEqualToString:@""])) { // Show the "Create DB" button. + NSLog(@"Now should show the CreateDB button... (in DidLoadNib) MCPMainWinCont = %@\n", MCPMainWinCont); + // [[(MCPConnectionWinCont*)MCPMainWinCont getCreateButton] setHidden:NO]; + [[(MCPConnectionWinCont*)MCPMainWinCont getCreateButton] setEnabled:YES]; + } + else { // Hide the "Create DB" button. + NSLog(@"Now should hide the CreateDB button... (in DidLoadNib) MCPMainWinCont = %@\n", MCPMainWinCont); + [[(MCPConnectionWinCont*)MCPMainWinCont getCreateButton] setEnabled:NO]; + // [[(MCPConnectionWinCont*)MCPMainWinCont getCreateButton] setHidden:YES]; + } } return; *************** *** 395,403 **** #pragma mark The Password sheet // Callback from sheet ! - (void) MCPpasswordSheetDidEnd:(NSWindow *) sheet returnCode:(int) returnCode contextInfo:(void *) contextInfo /*" Method called once the user enterred the password and click Ok (or press return) Try to make a connection (depending of the button clicked), if password is refused, go back to ask connection information. "*/ { ! NSLog (@"In passwordSheetDidEnd, return code is : %d\n", returnCode); if (returnCode == NSOKButton) { NSString *thePass = [[sheet delegate] Password]; --- 515,523 ---- #pragma mark The Password sheet // Callback from sheet ! - (void) MCPPasswordSheetDidEnd:(NSWindow *) sheet returnCode:(int) returnCode contextInfo:(void *) contextInfo /*" Method called once the user enterred the password and click Ok (or press return) Try to make a connection (depending of the button clicked), if password is refused, go back to ask connection information. "*/ { ! // NSLog (@"In PasswordSheetDidEnd, return code is : %d\n", returnCode); if (returnCode == NSOKButton) { NSString *thePass = [[sheet delegate] Password]; *************** *** 406,409 **** --- 526,535 ---- [sheet close]; if ([MCPConnect isConnected]) { + if (MCPWillCreateNewDB) { // We have to create the database, now that we are connected. + if (! [self createModelDB]) { + NSLog(@"Got an error while trying to create a database from the model..."); + } + } + [self setMCPWillCreateNewDB:NO]; if (![MCPConnect selectDB:MCPDatabase]) { [MCPConnect release]; *************** *** 418,422 **** MCPPassNeeded = NO; ! NSLog (@"Connected to the database... YES!\n"); // [[sheet delegate] refreshResult]; // Change the main window to the ConnectedWinCont: --- 544,548 ---- MCPPassNeeded = NO; ! // NSLog (@"Connected to the database... YES!\n"); // [[sheet delegate] refreshResult]; // Change the main window to the ConnectedWinCont: *************** *** 428,432 **** [theWinCont release]; theArray = [NSArray arrayWithArray:[self windowControllers]]; ! NSLog(@"In MCPpasswordSheetDidEnd..., closing new connection window. Number of window attached to the doc : %i\n", [theArray count]); for (i=0; i<[theArray count]; i++) { theWinCont = [theArray objectAtIndex:i]; --- 554,558 ---- [theWinCont release]; theArray = [NSArray arrayWithArray:[self windowControllers]]; ! // NSLog(@"In MCPPasswordSheetDidEnd..., closing new connection window. Number of window attached to the doc : %i\n", [theArray count]); for (i=0; i<[theArray count]; i++) { theWinCont = [theArray objectAtIndex:i]; *************** *** 450,455 **** [[sheet delegate] Password]; [self setMCPConInfoNeeded:YES]; ! NSLog (@"The cancel button has been clicked, return to ask info for the connection\n"); } return; } --- 576,582 ---- [[sheet delegate] Password]; [self setMCPConInfoNeeded:YES]; ! // NSLog (@"The cancel button has been clicked, return to ask info for the connection\n"); } + [self setMCPWillCreateNewDB:NO]; return; } |