Thread: [Mysql-cocoa-commits] CVS: SMySQL/MCPApplicationKit MCPConnectionWinCont.h,NONE,1.1 MCPConnectionWin
Brought to you by:
sergecohen
From: Serge C. <ser...@us...> - 2003-10-11 22:32:45
|
Update of /cvsroot/mysql-cocoa/SMySQL/MCPApplicationKit In directory sc8-pr-cvs1:/tmp/cvs-serv21913/MCPApplicationKit Added Files: MCPConnectionWinCont.h MCPConnectionWinCont.m MCPDocument.h MCPDocument.m Log Message: Added first draft of classes to help making a GUI application using MySQL to store its 'document'. Start passing to version 2.2.2 (maybe should go to 2.3 when GUI part is checked). 2003-10-11; Serge Cohen. --- NEW FILE: MCPConnectionWinCont.h --- // // MCPConnectionWinCont.h // Vacations // // Created by Serge Cohen on Mon May 26 2003. // Copyright (c) 2003 ARP/wARP. All rights reserved. // #import <AppKit/AppKit.h> // External classes, forward reference. @class MCPDocument; @interface MCPConnectionWinCont : NSWindowController { IBOutlet NSTextField *mHostField; IBOutlet NSTextField *mLoginField; IBOutlet NSTextField *mDatabaseField; IBOutlet NSTextField *mPortField; IBOutlet NSPanel *mPasswordSheet; IBOutlet NSTextField *mPasswordField; // MCPDocument *mMCPDocument; } /*" Actions for Interface Builder "*/ /*" For the clear text information. "*/ - (IBAction)doGo:(id) sender; - (IBAction)doCancel:(id) sender; - (IBAction)modifyInstance:(id) sender; /*" For the password. "*/ - (IBAction) passwordClick:(id) sender; - (IBAction) askPassword:(id) sender; - (NSString *) Password; /*" Overrides of NSWindowController method, to adapt to this Window Controller. "*/ - (id)init; - (void)dealloc; - (void)windowDidLoad; @end --- NEW FILE: MCPConnectionWinCont.m --- // // MCPConnectionWinCont.m // Vacations // // Created by Serge Cohen on Mon May 26 2003. // Copyright (c) 2003 ARP/wARP. All rights reserved. // // Referencing self: #import "MCPConnectionWinCont.h" // Other headers required by MCPDocument: #import "MCPDocument.h" // External headers required: #import <SMySQL/SMySQL.h> @implementation MCPConnectionWinCont /*" This class is the WindowController for the window asking parameters of connection to the user. 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. "*/ { MCPDocument *theDoc = (MCPDocument *)[self document]; // Send update message to the previously setted element [[self window] endEditingFor:[[self window] firstResponder]]; // Check that the proper fields are set if (([[theDoc MCPHost] isEqualToString:@""]) || ([[theDoc MCPLogin] isEqualToString:@""]) || ([[theDoc MCPDatabase] isEqualToString:@""]) || ([theDoc MCPHost] == nil) || ([theDoc MCPLogin] == nil) || ([theDoc MCPDatabase] == nil)) { NSBeginAlertSheet(@"", @"OK", nil, nil, [sender window], self, nil, nil, self, @"Unable to connect, one of the fields Host, Login or Database might be empty!"); return; } [theDoc setMCPConInfoNeeded:NO]; return; } - (IBAction)doCancel:(id) sender /*" What to do when the user clicks on the Cancel button of the window. "*/ { [[self window] performClose:self]; return; } - (IBAction)modifyInstance:(id) sender /*" Action to take when the user modify one of the entry of the New Connection dialog. "*/ { MCPDocument *theDoc = (MCPDocument *)[self document]; if (sender == mHostField) { [theDoc setMCPHost:[sender stringValue]]; } else if (sender == mLoginField) { [theDoc setMCPLogin:[sender stringValue]]; } else if (sender == mPortField) { [theDoc setMCPPort:[sender intValue]]; } else if (sender == mDatabaseField) { [theDoc setMCPDatabase:[sender stringValue]]; } else { // Where is the action coming from? NSLog (@"modifyInstance from an unknown sender : %@\n", sender); } return; } /*" For the password. "*/ - (IBAction) passwordClick:(id) sender { [[NSApplication sharedApplication] endSheet:[sender window] returnCode:[sender tag]]; return; } - (IBAction) askPassword:(id) sender { [[NSApplication sharedApplication] beginSheet:mPasswordSheet modalForWindow:[self window] modalDelegate:[self document] didEndSelector:@selector(MCPpasswordSheetDidEnd:returnCode:contextInfo:) contextInfo:self]; return; } - (NSString *) Password /*" Send the password (from the NSPasswordField), this method puts the password in the answer (as an autoreleased object), and DELETE it from the NSSecureTextField. "*/ { NSString *thePass = [NSString stringWithString:[mPasswordField stringValue]]; [mPasswordField setStringValue:@""]; return thePass; } /*" Overrides of NSWindowController method, to adapt to this Window Controller. "*/ - (id)init /*" When inited, open the proper window... "*/ { self = [super initWithWindowNibName:@"MCPConnectionWindow"]; return self; } - (void)dealloc /*" Gives notification that the WindowController is being deallocated (is it really useful? not yet!). "*/ { [[NSNotificationCenter defaultCenter] removeObserver:self]; [super dealloc]; } - (void)windowDidLoad /*" What to do once the window has been loaded : update the fields... "*/ { MCPDocument *theDoc = (MCPDocument *)[self document]; [super windowDidLoad]; // [self showWindow:self]; if ([theDoc MCPHost]) { [mHostField setStringValue:[theDoc MCPHost]]; } if ([theDoc MCPLogin]) { [mLoginField setStringValue:[theDoc MCPLogin]]; } if ([theDoc MCPDatabase]) { [mDatabaseField setStringValue:[theDoc MCPDatabase]]; } [mPortField setIntValue:[theDoc MCPPort]]; return; } @end --- NEW FILE: MCPDocument.h --- // // MCPDocument.h // Vacations // // Created by Serge Cohen on Sat May 24 2003. // Copyright (c) 2003 ARP/wARP. All rights reserved. // #import <Cocoa/Cocoa.h> // External classes, forward reference. @class MCPConnection; @class MCPResult; @interface MCPDocument : NSDocument { BOOL MCPConInfoNeeded, MCPPassNeeded; NSString *MCPHost, *MCPLogin, *MCPDatabase; unsigned int MCPPort; MCPConnection *MCPConnect; // Handling of windows. NSWindowController *MCPMainWinCont; Class MCPConnectedWinCont; /*" Window controller used once the connection is established (As a class). "*/ } /*" Class Maintenance "*/ + (void) initialize; // Standards /*" Initialisation and deallocation "*/ - (id) init; - (void) dealloc; /*" Connection to the databse related "*/ - (MCPResult *) MCPqueryString:(NSString *) query; - (unsigned int) MCPinsertRow:(NSString *) insert; // Accessors /*" Accessors to the parameters of the connection "*/ - (void) setMCPHost:(NSString *) theHost; - (void) setMCPLogin:(NSString *) theLogin; - (void) setMCPDatabase:(NSString *) theDatabase; - (void) setMCPPort:(unsigned int) thePort; - (void) setMCPConInfoNeeded:(BOOL) theConInfoNeeded; - (NSString *) MCPHost; - (NSString *) MCPLogin; - (NSString *) MCPDatabase; - (unsigned int) MCPPort; - (BOOL) MCPConInfoNeeded; - (BOOL) MCPPassNeeded; - (BOOL) MCPisConnected; - (MCPConnection *) MCPConnect; /*" Accessor to the window generated once the connection is established "*/ - (void) setMCPConnectedWinCont:(Class) theConnectedWinCont; - (Class) MCPConnectedWinCont; /*" Accessors to the main window (connection or connected window), through their window controller. "*/ - (NSWindowController *) MCPMainWinCont; /*" Overrides of NSDocument methods. "*/ // Managing the document in file format - (NSData *) dataRepresentationOfType:(NSString *) aType; - (BOOL)loadDataRepresentation:(NSData *) data ofType:(NSString *)aType; // Managing NSWindowController(s) - (NSArray *) makeWindowControllers; - (void) windowControllerDidLoadNib:(NSWindowController *) aController; /*" Method to take care of the password sheet. "*/ // Callback from sheet - (void) MCPpasswordSheetDidEnd:(NSWindow *) sheet returnCode:(int) returnCode contextInfo:(void *) contextInfo; @end --- NEW FILE: MCPDocument.m --- // // MCPDocument.m // Vacations // // Created by Serge Cohen on Sat May 24 2003. // Copyright (c) 2003 ARP/wARP. All rights reserved. // // Referencing self: #import "MCPDocument.h" // Other headers required by MCPDocument: #import "MCPConnectionWinCont.h" // External headers required: #import "MCPConnection.h" #import "MCPResult.h" @implementation MCPDocument #pragma mark Class Maintenance + (void) initialize { if (self == [MCPDocument class]) { [self setVersion:010000]; // Format Ma.Mi.Re -> MaMiRe } } #pragma mark Initialisation and deallocation - (id) init /*" Initialisation of the MCPDocument object, by default every thing is setted to null (or empty), accordingly to that the mConInfoNeeded is setted to YES (true): the connection information (parameters) ARE needed. "*/ { if (self = [super init]) { MCPConInfoNeeded = YES; MCPPassNeeded = YES; MCPHost = MCPLogin = MCPDatabase = @""; MCPPort = 0; MCPConnect = nil; MCPMainWinCont = nil; MCPConnectedWinCont = nil; } return self; } - (void) dealloc /*" Deallocation of the object... autorelease all the members. "*/ { if (MCPHost) { [MCPHost autorelease]; } if (MCPLogin) { [MCPLogin autorelease]; } if (MCPDatabase) { [MCPDatabase autorelease]; } if (MCPConnect) { [MCPConnect autorelease]; } [super dealloc]; return; } #pragma mark Connection to the databse - (MCPResult *) MCPqueryString:(NSString *) query /*" Send a query to the MCPConnection instance variable. For insert queries, one should prefer the insert: method, which return the primary key (auto_increment) of the new row. "*/ { if (MCPConnect == nil) { #warning Should throw an exception here. // Should throw an exception, not possible to do a query without a connection // Maybe, can try to connect first (check connection param) and if still not connected // throw the exception. return nil; } return [MCPConnect queryString: query]; } - (unsigned int) MCPinsertRow:(NSString *) insert /*" Method to use to insert a new row in a table, will return the primary key (auto_increment column) of the new record. "*/ { #warning Do we have to check for a proper insert statement? if (MCPConnect == nil) { return (unsigned int)0; } [MCPConnect queryString: insert]; return (unsigned int)[MCPConnect insertId]; } #pragma mark Accessors #pragma mark Accessors for Connect. info - (void) setMCPHost:(NSString *) theHost /*" Sets the name of the host to which to connect (the one which the db server runs) (doesn't accept changes once MCPConInfoNeeded is NO). "*/ { if (MCPHost && [MCPHost isEqualToString:theHost]) { return; } if (MCPConInfoNeeded) { if (MCPHost) { [MCPHost autorelease]; } // NSLog (@"In setHost: modification was %@ becomes %@\n", mHost, theHost); MCPHost = [[NSString stringWithString:theHost] retain]; [self updateChangeCount:NSChangeDone]; } else { NSLog(@"Tryed to modify MCPHost from MCPDocument whereas MCPConInfoNeeded = NO\n"); } return; } - (void) setMCPLogin:(NSString *) theLogin /*" Sets the name of the database to use on the db server (doesn't accept changes once MCPConInfoNeeded is NO). "*/ { if (MCPLogin && [MCPLogin isEqualToString:theLogin]) { return; } if (MCPConInfoNeeded) { if (MCPLogin) { [MCPLogin autorelease]; } // NSLog (@"In setLogin: modification was %@ becomes %@\n", mLogin, theLogin); MCPLogin = [[NSString stringWithString:theLogin] retain]; [self updateChangeCount:NSChangeDone]; } else { NSLog(@"Tryed to modify MCPLogin from MCPDocument whereas MCPConInfoNeeded = NO\n"); } return; } - (void) setMCPDatabase:(NSString *) theDatabase /*" Sets the name of the database to use on the db server (doesn't accept changes once mConInfoNeeded is NO). "*/ { if (MCPDatabase && [MCPDatabase isEqualToString:theDatabase]) { return; } if (MCPConInfoNeeded) { if (MCPDatabase) { [MCPDatabase autorelease]; } // NSLog (@"In setDatabase: modification was %@ becomes %@\n", mDatabase, theDatabase); MCPDatabase = [[NSString stringWithString:theDatabase] retain]; [self updateChangeCount:NSChangeDone]; } else { NSLog(@"Tryed to modify MCPDatabase from MCPDocument whereas MCPConInfoNeeded = NO\n"); } return; } - (void) setMCPPort:(unsigned int) thePort /*" Set the port to use to connect to the database server (doesn't accept changes once mConInfoNeeded is NO). "*/ { if (MCPPort == thePort) { return; } if (MCPConInfoNeeded) { // NSLog (@"In setPort: modification was %u becomes %u\n", mPort, thePort); MCPPort = (thePort < 0) ? thePort : 0; [self updateChangeCount:NSChangeDone]; } else { NSLog(@"Tryed to modify MCPPort from MCPDocument whereas MCPConInfoNeeded = NO\n"); } return; } - (void) setMCPConInfoNeeded:(BOOL) theConInfoNeeded /*" VERY IMPORTANT method! 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"); return; } if (MCPConInfoNeeded = theConInfoNeeded) { // here we have to remove the ConnectedWindow window(s) and display the MCPConnectionWindow window NSArray *theArray; NSWindowController *theWinCont; unsigned int i; MCPPassNeeded = YES; if ([MCPConnectionWinCont class] == [MCPMainWinCont class]) { // If the present main window is already a MCPConnectionWinCont, don't touch it... } else { // Otherwise, we were using a ConnectdWinCont, then turn back to a MCPConnectionWinCont window... theWinCont = [[MCPConnectionWinCont allocWithZone:[self zone]] init]; [theWinCont setShouldCloseDocument:YES]; [self addWindowController: theWinCont]; [theWinCont showWindow:self]; MCPMainWinCont = theWinCont; [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]; if (! [[theWinCont windowNibName] isEqualToString:@"MCPConnectionWindow"]) { // We get the window controller handling the window (browser).. [theWinCont setShouldCloseDocument:NO]; [[theWinCont window] performClose:self]; } } } } else { // here we have to initiate the process of asking the password... MCPPassNeeded = YES; [(MCPConnectionWinCont *)MCPMainWinCont askPassword: self]; } return; } - (NSString *) MCPHost /*" Returns the actual hostname of the used db server. "*/ { return MCPHost; } - (NSString *) MCPLogin /*" Returns the actual login to the used db server. "*/ { return MCPLogin; } - (NSString *) MCPDatabase /*" Returns the actual database name of the used db server. "*/ { return MCPDatabase; } - (unsigned int) MCPPort /*" Returns the port used to connect to the database server (0 means default: from mysql.h -> MYSQL_PORT=3306). "*/ { return MCPPort; } - (BOOL) MCPConInfoNeeded /*" Return the status of the gathering of information for the connection. "*/ { return MCPConInfoNeeded; } - (BOOL) MCPPassNeeded /*" Return the status of the gathering of the password for the connection. "*/ { return MCPPassNeeded; } - (BOOL) MCPisConnected /*" Check if the connection is working. "*/ { if (nil != MCPConnect) { return [MCPConnect checkConnection]; } else { return NO; } } - (MCPConnection *) MCPConnect /*" Return a pointer to the used MCPConnection. SHOULD NOT be used (one should rather use the connection methods : MCPqueryString: or MCPinsertRow:)"*/ { return MCPConnect; } #pragma mark Accessors for Window Controller - (void) setMCPConnectedWinCont:(Class) theConnectedWinCont /*" Use to set the type of NSWindowController to be used for the main window of the document once the connection as been established. "*/ { if (nil == MCPConnect) { MCPConnectedWinCont = theConnectedWinCont; } else { NSLog (@"Tryed to modify the MCPConnectedWinCont class AFTER the connection was established... TO LATE!!\n"); } } - (Class) MCPConnectedWinCont /*" Return the Class object of the class used for the main document window (once the connection to the DB server is established). "*/ { return MCPConnectedWinCont; } #pragma mark Accessors for Main Window - (NSWindowController *) MCPMainWinCont /*" Return the current main window controller for the document (closing this window causes a close of the document). "*/ { return MCPMainWinCont; } #pragma mark Overrides of NSDocument methods - (NSData *) dataRepresentationOfType:(NSString *) aType /*" Return data correspoding to archived dictionary containing the parameters for the connection (except for the password, not saved). "*/ { NSMutableDictionary *theDict = [NSMutableDictionary dictionaryWithCapacity:4]; NSData *theData; NSLog (@"Try to save the connection document under type: %@\n",aType); NSAssert([aType isEqualToString:@"MCP Connection Informations"], @"Unknown type"); [theDict setObject:MCPHost forKey:@"host"]; [theDict setObject:MCPLogin forKey:@"login"]; [theDict setObject:MCPDatabase forKey:@"db"]; [theDict setObject:[NSNumber numberWithInt:MCPPort] forKey:@"port"]; theData = [NSArchiver archivedDataWithRootObject:theDict]; return [NSData dataWithData:theData]; } - (BOOL)loadDataRepresentation:(NSData *) data ofType:(NSString *)aType /*" Load a file containing the connection parameters (except for the password) in the instance of the MCPDocument. "*/ { NSDictionary *theDict; NSLog (@"Try to load the connection document under type: %@\n",aType); NSAssert([aType isEqualToString:@"MCP Connection Informations"], @"Unknown type"); theDict = [NSUnarchiver unarchiveObjectWithData:data]; // NSLog (@"\n\n Reading a file\n"); [self setMCPHost:[theDict objectForKey:@"host"]]; [self setMCPLogin:[theDict objectForKey:@"login"]]; [self setMCPDatabase:[theDict objectForKey:@"db"]]; [self setMCPPort:[[theDict objectForKey:@"port"] intValue]]; [self updateChangeCount:NSChangeCleared]; MCPConInfoNeeded = YES; MCPPassNeeded = YES; return YES; } // Managing NSWindowController(s) - (NSArray *) makeWindowControllers /*" Make the proper window: either the Connection Info window or the main browser window. "*/ { NSWindowController *theWinCont; // NSLog(@"Inside makeWindowControllers\n"); if (MCPConInfoNeeded) { theWinCont = [[MCPConnectionWinCont allocWithZone:[self zone]] init]; [theWinCont setShouldCloseDocument:YES]; MCPMainWinCont = theWinCont; [self addWindowController: theWinCont]; [theWinCont release]; } else if (nil != MCPConnectedWinCont){ theWinCont = [[MCPConnectedWinCont allocWithZone:[self zone]] init]; [theWinCont setShouldCloseDocument:YES]; MCPMainWinCont = theWinCont; [self addWindowController: theWinCont]; [theWinCont showWindow:self]; [theWinCont release]; } return [self windowControllers]; } - (void) windowControllerDidLoadNib:(NSWindowController *) aController /*" 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; } #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]; MCPConnect = [[MCPConnection alloc] initToHost:MCPHost withLogin:MCPLogin password:thePass usingPort:MCPPort]; [sheet close]; if ([MCPConnect isConnected]) { if (![MCPConnect selectDB:MCPDatabase]) { [MCPConnect release]; MCPConnect = nil; [self setMCPConInfoNeeded:YES]; NSLog (@"Connection to the server was Ok, but unable to find database : %@\n", MCPDatabase); } else { NSArray *theArray; NSWindowController *theWinCont; unsigned int i; MCPPassNeeded = NO; NSLog (@"Connected to the database... YES!\n"); // [[sheet delegate] refreshResult]; // Change the main window to the ConnectedWinCont: theWinCont = [[MCPConnectedWinCont allocWithZone:[self zone]] init]; [theWinCont setShouldCloseDocument:YES]; [self addWindowController: theWinCont]; [theWinCont showWindow:self]; MCPMainWinCont = theWinCont; [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]; if ([MCPConnectionWinCont class] == [theWinCont class]) { // We get the window controller handling the ocnnection window (MCPConnectionWindow)... [theWinCont setShouldCloseDocument:NO]; [[theWinCont window] performClose:self]; } } } } else { [MCPConnect release]; MCPConnect = nil; [self setMCPConInfoNeeded:YES]; NSLog (@"Unable to connect to the server, info are: Host %@ , Port %u , User %@\n", MCPHost, MCPPort, MCPLogin); } } else { [sheet close]; [[sheet delegate] Password]; [self setMCPConInfoNeeded:YES]; NSLog (@"The cancel button has been clicked, return to ask info for the connection\n"); } return; } @end |