home *** CD-ROM | disk | FTP | other *** search
/ OpenStep (Enterprise) / OpenStepENTCD.toast / OEDEV / EODEV.Z / ODBCLoginPanel.m < prev    next >
Encoding:
Text File  |  1996-09-04  |  4.8 KB  |  162 lines

  1. /*
  2.    ODBCLoginPanel.m
  3.  
  4.    Copyright (c) 1996 NeXT Software, Inc.  All rights reserved.
  5.  
  6.    IMPORTANT:  This NeXT software is supplied to you in consideration of your agreement
  7.    to the terms of the NeXT Software License Agreement stated in the file ODBC_LICENSE.rtf
  8.    provided with the software.  Your use of the software is governed by such terms.
  9.    Do not use, install, or make copies of the software if you do not agree to such terms.
  10.  
  11.    THIS SOFTWARE IS FURNISHED ON AN "AS-IS" BASIS. NeXT MAKES NO WARRANTIES OF ANY KIND,
  12.    EITHER EXPRESS OR IMPLIED, AS TO ANY MATTER WHATSOEVER, INCLUDING WITHOUT LIMITATION
  13.    THE CONDITION, MERCHANTABILITY, OR FITNESS FOR ANY PARTICULAR PURPOSE OF THIS SOFTWARE.
  14.    NeXT DOES NOT ASSUME ANY LIABILITY REGARDING USE OF, OR ANY DEFECT IN, THIS SOFTWARE.
  15.    IN NO EVENT SHALL NeXT BE LIABLE FOR ANY INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
  16.    DAMAGES, EVEN IF IT HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  17. */
  18.  
  19. #import "ODBCLoginPanel.h"
  20. #import <ODBCEOAdaptor/ODBCAdaptor.h>
  21.  
  22. @implementation ODBCLoginPanel
  23.  
  24. extern const char ODBC_VERS_NUM[];
  25.  
  26. - (void)_resetAccordingToState
  27. {
  28.     if([useConnectionString state]) {
  29.         [datasourceName setEnabled:NO];
  30.         [userName setEnabled:NO];
  31.         [password setEnabled:NO];
  32.         [connectionString setEnabled:YES];
  33.     } else {
  34.         [datasourceName setEnabled:YES];
  35.         [userName setEnabled:YES];
  36.         [password setEnabled:YES];
  37.         [connectionString setEnabled:NO];
  38.     }
  39. }
  40.  
  41. - (void)_setConnectionDictionary:(NSDictionary *)connectionInfo
  42. {
  43.     id value;
  44.  
  45.     _connectionInfo = [connectionInfo mutableCopy];
  46.  
  47.     value = [connectionInfo valueForKey:dataSourceKey];
  48.     [datasourceName setStringValue:value ? value : @""];
  49.     [_connectionInfo removeObjectForKey:dataSourceKey];
  50.     
  51.     value = [connectionInfo valueForKey:userNameKey];
  52.     [userName setStringValue:value ? value : @""];
  53.     [_connectionInfo removeObjectForKey:userNameKey];
  54.  
  55.     value = [connectionInfo valueForKey:passwordKey];
  56.     [password setStringValue:value ? value : @""];
  57.     [_connectionInfo removeObjectForKey:passwordKey];
  58.  
  59.     value = [connectionInfo valueForKey:connectionStringKey];
  60.     [connectionString setStringValue:value ? value : @""];
  61.     [_connectionInfo removeObjectForKey:connectionStringKey];
  62.  
  63.     [useConnectionString setState:(value ? 1 : 0)];
  64.  
  65.     [self _resetAccordingToState];
  66. }
  67.  
  68. - (NSDictionary *)_connectionDictionary
  69. {
  70.     id value;
  71.     NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
  72.  
  73.     value = [datasourceName stringValue];
  74.     if(value && ![value isEqual:@""])
  75.         [dictionary setObject:value forKey:dataSourceKey];
  76.  
  77.     value = [userName stringValue];
  78.     if(value && ![value isEqual:@""])
  79.         [dictionary setObject:value forKey:userNameKey];
  80.  
  81.     value = [password stringValue];
  82.     if(value && ![value isEqual:@""])
  83.         [dictionary setObject:value forKey:passwordKey];
  84.  
  85.     if([useConnectionString state]) {
  86.         value = [connectionString stringValue];
  87.         if(value && ![value isEqual:@""])
  88.             [dictionary setObject:value forKey:connectionStringKey];
  89.     }
  90.  
  91.     if(_connectionInfo) [dictionary addEntriesFromDictionary:_connectionInfo];
  92.     return dictionary;
  93. }
  94.  
  95. - init
  96. {
  97.     NSWindow *window;
  98.     NSString *versionString = [NSString stringWithCString:ODBC_VERS_NUM];
  99.  
  100.     [super init];
  101.     [NSBundle loadNibNamed:@"ODBCLoginPanel" owner:self];
  102.  
  103.     window = [userName window];
  104.  
  105.     if(!versionString || ![versionString length]) {
  106.         versionString = @"Development version";
  107.     } else {
  108.         versionString = [NSString stringWithFormat:@"v.%@", versionString];
  109.     }
  110.  
  111.     [window setTitle:[NSString stringWithFormat:@"%@ (%@)", [window title], versionString]];
  112.  
  113.     return self;
  114. }
  115.  
  116. - (NSDictionary *)runPanelForAdaptor:(EOAdaptor *)adaptor validate:(BOOL)validate
  117. {
  118.     NSDictionary *result;
  119.     BOOL success;
  120.  
  121.     [self _setConnectionDictionary:[adaptor connectionDictionary]];
  122.     do {
  123.         if ([NSApp runModalForWindow:[datasourceName window]] != NSRunContinuesResponse) {
  124.             result = nil;
  125.             [[datasourceName window] orderOut:nil];
  126.             break;
  127.         }
  128.         [[datasourceName window] orderOut:nil];
  129.         result = [self _connectionDictionary];
  130.  
  131.         if (!validate) break;
  132.  
  133.         success = YES;
  134.         [adaptor setConnectionDictionary:result];
  135.         NS_DURING
  136.             [adaptor assertConnectionDictionaryIsValid];
  137.         NS_HANDLER
  138.             NSRunAlertPanel (NULL, @"%@", NULL, NULL, NULL, [localException reason]);
  139.             [self _setConnectionDictionary:[adaptor connectionDictionary]];
  140.             success = NO;
  141.         NS_ENDHANDLER
  142.     } while (!success);
  143.  
  144.     return result;
  145. }
  146.  
  147. - (void)cancel:sender
  148. {
  149.     [NSApp stopModalWithCode:NSRunAbortedResponse];
  150. }
  151.  
  152. - (void)login:sender
  153. {
  154.     [NSApp stopModalWithCode:NSRunContinuesResponse];
  155. }
  156.  
  157. - (void)switch:sender
  158. {
  159.     [self _resetAccordingToState];
  160. }
  161. @end
  162.