#import "JubConfig.h"

@implementation JubConfig
@synthesize domain = _domain;
@synthesize server = _server;;
@synthesize username = _username;
@synthesize password = _password;
@synthesize frontend = _frontend;

- initWithFile: (OFString*)file
{
	self = [super init];

	@try {
		OFAutoreleasePool *pool = [OFAutoreleasePool new];
		OFXMLElement *element = [OFXMLElement elementWithFile: file];

		if (![element.name isEqual: @"config"] ||
		    ![element.namespace isEqual: CONFIG_NS]) {
			// TODO: load default config
			[pool release];
			return self;
		}

		// TODO: Add error handling for missing elements
		of_log(@"Parsed file: %@", element);
		_domain = [[[element
		    elementForName: @"domain"
			 namespace: CONFIG_NS] stringValue] copy];
		_server = [[[element
		    elementForName: @"server"
			 namespace: CONFIG_NS] stringValue] copy];
		_username = [[[element
		    elementForName: @"username"
			 namespace: CONFIG_NS] stringValue] copy];
		_password = [[[element
		    elementForName: @"password"
			 namespace: CONFIG_NS] stringValue] copy];
		_frontend = [[[element
		    elementForName: @"frontend"
			 namespace: CONFIG_NS] stringValue] copy];


		[pool release];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[_domain release];
	[_server release];
	[_username release];
	[_password release];
	[_frontend release];

	[super dealloc];
}
@end
