
#import "EDBConnector.h"
#import "EDBConnections.h"
#import <ctype.h>

#define	NOLDEBUG

@implementation EDBConnector

- init
{
	return [self initWithSource:nil andDestination:nil forOutlet:NULL
		andSel:NULL];
}

- initWithSource:s andDestination:d forOutlet:(const char *)name
	andSel:(const char *)sel
{
	[super init];
	source = s;
	destination = d;
	outletName = (name!=NULL) ? NXUniqueString(name) : NULL;
	selName = (sel!=NULL) ? NXCopyStringBuffer(sel) : NULL;
	sequence = 0;

	return self;
}

- free
{
	if (selName)
		free((char *)selName);
	return [super free];
}

- source
{
	return source;
}

- destination
{
	return destination;
}

- establishConnection
{
	char	string	[1000];
	SEL		sel;
	SEL		actionSel;

	sprintf(string,"set%s:",outletName);
	if (string[3]>='a' && string[3]<='z')
		string[3] = toupper(string[3]);
	sel = sel_getUid(string);
	if (sel!=NULL && [source respondsTo:sel])
		[source perform:sel with:destination];
	else if ([source respondsTo:@selector(establishEDBConnection:)])
	{
		[source establishEDBConnection:self];
		return self;
	}
	else
		object_setInstanceVariable(source,outletName,destination);

	if (selName != NULL)
	{
		sprintf(string,"set%sAction:",outletName);
		if (string[3]>='a' && string[3]<='z')
			string[3] = toupper(string[3]);
		sel = sel_getUid(string);
		actionSel = sel_getUid(selName);
		if (sel!=NULL && [source respondsTo:sel])
			[source perform:sel with:(id)actionSel];
		else
		{
			sprintf(string,"%sAction",outletName);
			object_setInstanceVariable(source,string,actionSel);
		}
	}

	return self;
}

- nibInstantiate
{
    source = [source nibInstantiate];
    destination = [destination nibInstantiate];
	return self;
}

- renewObject:old to:new
{
	if (old == source)
		source = new;
	else if (old == destination)
		destination = new;
	return self;
}

- read:(NXTypedStream *)stream
{
	u_short		dummy;

#ifdef LDEBUG
	fprintf(stderr,"%s read:\n",[self name]);
#endif

	[super read:stream];
	source = NXReadObject(stream);
	destination = NXReadObject(stream);
	NXReadType(stream,"%",&outletName);
	NXReadType(stream,"*",&selName);
	NXReadTypes(stream,"SS",&sequence,&dummy);

#ifdef LDEBUG
	fprintf(stderr,"destination=%lx,name=%s\n",
		(u_long)destination,[destination name]);
#endif

	return self;
}

- write:(NXTypedStream *)stream
{
	u_short		dummy = 0;

#ifdef LDEBUG
	fprintf(stderr,"%s write:\n",[self name]);
#endif

	[super write:stream];
	NXWriteObjectReference(stream,source);
	NXWriteObjectReference(stream,destination);
	NXWriteType(stream,"%",&outletName);
	NXWriteType(stream,"*",&selName);
	NXWriteTypes(stream,"SS",&sequence,&dummy);

	return self;
}

- (NXAtom)outletName
{
	return outletName;
}

- setOutletName:(NXAtom)name
{
	outletName = name;
	return self;
}

- (const char *)selName
{
	return selName;
}

- setSelName:(const char *)name
{
	if (selName != NULL)
		free((char *)selName);
	selName = (name==NULL) ? NULL : NXCopyStringBuffer(name);
	return self;
}

- (u_short)sequence
{
	return sequence;
}

- setSequence:(u_short)seq
{
	sequence = seq;
	return self;
}

@end
