/*!
  @header FTConfig
  @abstract Module of FT
 
  @availability OS X, GNUstep
  @copyright 2004, 2005, 2006 Free Software Foundation, Inc.
  
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA

  <pre>
  -------------------------------------------------------------------------
  Modification history

  01.12.05 ola     initial version
  23.08.06 ola     license changed
  -------------------------------------------------------------------------
  </pre>
*/

#include <FT/FTConfig.h>

@implementation FTConfig

- init {
  self = [super init];
  self->databaseNames = nil;
  self->services = nil;
  self->baseDataDir = nil;
  self->classObjectFactory = nil;

  return self;
}


- (void) dealloc { 
  [self->databaseNames release];
  [self->services release];
  [self->classObjectFactory release];

  [super dealloc];
}


- (NSString *) baseDataDir {
  return self->baseDataDir;
}


- (FTClassObjectFactory *) classObjectFactory {
  return self->classObjectFactory;
}


- (FTConfigDatabaseNames *) databaseNames {
  return self->databaseNames;
}


- (FTConfigServices *) services {
  return self->services;
}


- setBaseDataDir: (NSString *) theBaseDir {
  if( nil != self->baseDataDir ) {
    [self->baseDataDir release];
  }

  self->baseDataDir = [theBaseDir retain];

  return self;
}


- setDatabaseNames: (FTConfigDatabaseNames *) theDatabaseNames {
  if( nil != self->databaseNames ) {
    [self->databaseNames release];
  }

  self->databaseNames = [theDatabaseNames retain];

  return self;
}


- setFTClassObjectFactory: (FTClassObjectFactory *) aClassObjectFactory {
  id tmp = [aClassObjectFactory retain];
  [self->classObjectFactory release];
  self->classObjectFactory = tmp;

  return self;
}


- setServices: (FTConfigServices *) theServices {
  if( nil != self->services ) {
    [self->services release];
  }

  self->services = [theServices retain];

  return self;
}
@end


// --------------------------------------------- FTDatabaseNames

@implementation FTConfigDatabaseNames

- init {
  self = [super init];

  self->entryToDatabaseName = [[NSMutableDictionary alloc] init];

  return self;
}


- (void) dealloc {

  if( nil != self->entryToDatabaseName ) {
    [self->entryToDatabaseName release];
  }

  [super dealloc];
}


- addDatabaseName: (FTConfigDatabaseName *) dbNameToAdd {
  [self->entryToDatabaseName 
    setObject: [dbNameToAdd databaseName]
    forKey: [dbNameToAdd entry]];

  return self;
}


- (id <ECIterator>) allDatabaseNames {
  id <ECIterator> toReturn;
  EC_AUTORELEASEPOOL_BEGIN
    NSArray *values = [self->entryToDatabaseName allValues];
    toReturn = [[ECArrayIterator alloc] 
      initWithArray: values];
  EC_AUTORELEASEPOOL_END

  return [toReturn autorelease];
}


- (NSString *) databaseNameForEntry: (NSString *) anEntry {
  return [self->entryToDatabaseName objectForKey: anEntry];
}
@end


// --------------------------------------------- FTDatabaseName

@implementation FTConfigDatabaseName

- init {
  self = [super init];

  self->entry = nil;
  self->databaseName = nil;

  return self;
}


- (void) dealloc {
  if( nil != self->entry ) {
    [self->entry release];
  }

  if( nil != self->databaseName ) {
    [self->databaseName release];
  }

  [super dealloc];
}


- (NSString *) databaseName {
  return self->databaseName;
}


- (NSString *) entry {
  return self->entry;
}


- setDatabaseName: (NSString *) aDatabaseName {
  if( nil != self->databaseName ) {
    [self->databaseName release];
  }

  self->databaseName = [aDatabaseName retain];

  return self;
}


- setEntry: (NSString *) anEntry {
  id tmp;

  tmp = self->entry;

  self->entry = [anEntry retain];

  if( nil != tmp ) {
    [tmp release];
  }

  return self;
}
@end


// --------------------------------------- FTConfigServices

@implementation FTConfigServices 

- init {
  self = [super init];
  self->serviceLoaders = nil;
  
  return self;
}


- (void) dealloc {
  if( nil != self->serviceLoaders ) {
    [self->serviceLoaders release];
  }
  
  [super dealloc];
}


- setServiceLoaders: (FTConfigServiceLoaders *) serviceLoadersToSet {
  if( nil != self->serviceLoaders ) {
    [self->serviceLoaders release];
  }
  
  self->serviceLoaders = [[serviceLoadersToSet serviceLoaders] retain];
  
  return self;
}


- (NSMutableDictionary *) serviceLoaders {
  if( nil == self->serviceLoaders ) {
    self->serviceLoaders = [[NSMutableDictionary alloc] init];
  }
  
  return self->serviceLoaders;
}
@end


// --------------------------------------- FTConfigServices
@implementation FTConfigServiceLoaders : ECObject 

- init {
  self = [super init];
  self->serviceLoaders = [[NSMutableDictionary alloc] init];
  
  return self;
}


- (void) dealloc {
  if( nil != self->serviceLoaders ) {
    [self->serviceLoaders release];
  }
  
  [super dealloc];
}


- addServiceLoader: (id <FTServiceLoader>) serviceLoader {
  [self->serviceLoaders 
    setObject: serviceLoader
    forKey: [serviceLoader serviceId]];
    
  return self;
}


- (NSMutableDictionary *) serviceLoaders {
  return self->serviceLoaders;
}

@end


// --------------------------------------- FTRegisterClassObject
@implementation FTRegisterClassObject

- init {
  self = [super init];
  self->classObject = nil;
  self->key = nil;

  return self;
}


- (void) dealloc {
  [self->classObject release];
  [self->key release];

  [super dealloc];
}


- (NSString *) classObject {
  return self->classObject;
}


- (NSString *) key {
  return self->key;
}


- setClassObject: (NSString *) aClassObject {
  id tmp = [aClassObject retain];

  [self->classObject release];
  self->classObject = tmp;

  return self;
}


- setKey: (NSString *) aKey {
  id tmp = [aKey retain];

  [self->key release];
  self->key = tmp;

  return self;
}
@end


// --------------------------------------- FTClassObjectFactory
@implementation FTClassObjectFactory

- init {
  self = [super init];
  self->keyToClassObject = [[NSMutableDictionary alloc] init];

  return self;
}


- (void) dealloc {
  [self->keyToClassObject release];

  [super dealloc];
}


- addFTRegisterClassObject: (FTRegisterClassObject *) toRegister {
  [self->keyToClassObject 
    setObject: [toRegister classObject]
    forKey: [toRegister key]];

  return self;
}


- (Class) classObjectForKey: (NSString *) key {
  return NSClassFromString( [self->keyToClassObject 
    objectForKey: key] );
}
@end


