
#import "FtpHost.h"
#import "FtpSession.h"
#import "FtpFileSystem.h"
#import "Controller.h"
#import "NXmystd.h"

@implementation FtpHost

- initForName:(NXAtom)name;
{
	[super init];
	controller = [NXApp delegate];
	myname = name;
	userFileSystemHash = [[HashTable alloc] initKeyDesc:"%"];
	usedefaultuser=usedefaultpath=usedefaultlocalpath=TRUE;
	usedefaultpass=1;
	myuser = [controller defaultUser];
	mypass = [controller defaultPass];
	mypath = [controller defaultPath];
	mylocalpath = [controller defaultLocalPath];
	return self;
}

- free;
{
	[userFileSystemHash freeObjects];
	userFileSystemHash = [userFileSystemHash free];
	return [super free];
}

- write:(NXTypedStream *)stream;
{
	NXAtom empty = NXUniqueString("");
	[[NXApp delegate] savingHost:myname];
	[super write:stream];
	if ([Object version] != 4)
	{
		NXWriteTypes(stream, "%@%%%%cccc", &myname, &userFileSystemHash, &myuser, &mypass, &mypath, &mylocalpath,&usedefaultuser,&usedefaultpass,&usedefaultpath,&usedefaultlocalpath);
	}
	else
	{
		// for default cache (cache version 4)
		NXWriteTypes(stream, "%@%%%%", &myname, &userFileSystemHash, &empty,&empty, &mypath,&empty);
	}
	return self;
}

- read:(NXTypedStream *)stream;
{
	[super read:stream];
	if ([Object version] >= 6)
	{
		NXReadTypes(stream, "%@%%%%cccc", &myname, &userFileSystemHash, &myuser, &mypass, &mypath, &mylocalpath, &usedefaultuser,&usedefaultpass,&usedefaultpath,&usedefaultlocalpath);
	}
	else
	{
		NXReadTypes(stream,"%@%%%%", &myname, &userFileSystemHash, &myuser, &mypass, &mypath, &mylocalpath);
	}
	return self;
}

- finishUnarchiving;
{
	controller = [NXApp delegate];
	if ([Object version] <= 5)
	{
		if (myuser&&*myuser&&strcmp(myuser,[controller defaultUser])){usedefaultuser = NO;}
		else	{myuser = [controller defaultUser];usedefaultuser = YES;}
		if (mypass&&*mypass&&strcmp(mypass,[controller defaultPass])){usedefaultpass = 0;}
		else	{mypass = [controller defaultPass];usedefaultpass = 1;}
		if (mypath&&*mypath&&strcmp(mypath,[controller defaultPath])){usedefaultpath = NO;}
		else	{mypath = [controller defaultPath];usedefaultpath = YES;}
		if (mylocalpath&&*mylocalpath&&strcmp(mylocalpath,[controller defaultLocalPath])){usedefaultlocalpath = NO;}
		else	{mylocalpath = [controller defaultLocalPath];usedefaultlocalpath = YES;}

	}
	return self;
}

- startSession;
{
	id session = [[FtpSession alloc] initForHost:self];
	return session;
}

- fileSystemForUser:(NXAtom)user;
{
	id fileSystem;
	
	fileSystem = [userFileSystemHash valueForKey:user];
	if (!fileSystem)
	{
		LOGF("creating filesystem for user %s\n",user);
		fileSystem = [[FtpFileSystem alloc] initForHost:self andUser:user];
		[userFileSystemHash insertKey:user value:fileSystem];
	}
	return fileSystem;
}

- debugClearFileSystem;
{
	[userFileSystemHash empty];
	return self;
}

- renameTo:(NXAtom) name;
{
	myname = name;
	return self;
}

- (NXAtom) name;
{
	return myname;
}


- (BOOL) useDefaultUser;
{
	return usedefaultuser;
}
- (char) useDefaultPass;
{
	return usedefaultpass;
}
- (BOOL) useDefaultPath;
{
	return usedefaultpath;
}
- (BOOL) useDefaultLocalPath;
{
	return usedefaultlocalpath;
}

- setUseDefaultUser:(BOOL)flag;
{
	usedefaultuser = flag;
	return self;
}
- setUseDefaultPass:(char)flag;
{
	usedefaultpass = flag;
	return self;
}
- setUseDefaultPath:(BOOL)flag;
{
	usedefaultpath = flag;
	return self;
}
- setUseDefaultLocalPath:(BOOL)flag;
{
	usedefaultlocalpath = flag;
	return self;
}


- setUser:(NXAtom)s;
{
	if (s && *s)
		{myuser = s; usedefaultuser=NO;}
	else
		{myuser = [controller defaultUser]; usedefaultuser=YES;}
	return self;
}

- setPass:(NXAtom)s;
{
	if (s && *s)
		{mypass = s; usedefaultpass=0;}
	else
		{mypass = [controller defaultPass]; usedefaultpass=1;}
	return self;
}

- setPath:(NXAtom)s;
{
	if (s && *s)
		{mypath = s; usedefaultpath=NO;}
	else
		{mypath = [controller defaultPath]; usedefaultpath=YES;}
	return self;
}

- setLocalPath:(NXAtom)s;
{
	if (s && *s)
		{mylocalpath = s; usedefaultlocalpath=NO;}
	else
		{mylocalpath = [controller defaultLocalPath]; usedefaultlocalpath=YES;}
	return self;
}

- (NXAtom) user;
{
	return usedefaultuser?[controller defaultUser]:myuser;
}

- (NXAtom) pass;
{
	return usedefaultpass==1?[controller defaultPass]:mypass;
}

- (NXAtom) path;
{
	return usedefaultpath?[controller defaultPath]:mypath;
}

- (NXAtom) localPath;
{
	return usedefaultlocalpath?[controller defaultLocalPath]:mylocalpath;
}

- (NXAtom) realUser;
{
	return myuser;
}

- (NXAtom) realPass;
{
	return mypass;
}


@end
