
#import "FtpFile.h"
#import "FtpDirectory.h"
#import "NXmystd.h"

@implementation FtpFile

- initForName:(const char *)name withParent:aParent;
{
	[super init];
	myname = NXUniqueString(name);
	parent = aParent;
	timestamp = 0;
	mysize = -1;
	isDummy = NO;
	isNew = YES;
	isReadme = strstr(myname,"README")?YES:NO;
	transferstatus = 0;
	shouldreget = 0;
	shouldautoopen = 0;
	isselected = NO;
	localPath = NULL;
	return self;
}

- free;
{
	return [super free];
}

- write:(NXTypedStream *)stream;
{
	//LOGF("object version: %d\n",[Object version]);
	[super write:stream];
	if ([Object version] != 4)
	{
		NXWriteTypes(stream,"%Iiccc@",&myname,&timestamp,&mysize,&isDummy,&isNew,&isReadme,&parent);
	}
	else
	{
		// for default cache (version 4)
		NXWriteTypes(stream,"%icc@",&myname,&mysize,&isDummy,&isNew,&parent);
	}
	return self;
}

- read:(NXTypedStream *)stream;
{
	[super read:stream];
	//LOGF("object version: %d\n",[Object version]);
	if ([Object version] >= 5)
	{
		NXReadTypes(stream, "%Iiccc@", &myname, &timestamp, &mysize, &isDummy, &isNew, &isReadme, &parent);
	}
	else if ([Object version] >= 4)
	{
		NXReadTypes(stream, "%icc@", &myname, &mysize, &isDummy, &isNew, &parent);
		timestamp = 0;
		isReadme = strstr(myname,"README")?YES:NO;
	}
	else if ([Object version] >= 3)
	{
		unsigned long count;
		NXReadTypes(stream, "%IIiccc@", &myname, &timestamp, &count, &mysize, &isDummy, &isNew, &isReadme, &parent);
	}
	else if ([Object version] >= 2)
	{
		unsigned long count;
		NXReadTypes(stream, "%IIccc@", &myname, &timestamp, &count, &isDummy, &isNew, &isReadme, &parent);
		mysize = -1;
		timestamp = 0;
	}
	else
	{
		unsigned long count;
		NXReadTypes(stream,"%IIcc@",&myname,&timestamp,&count,&isDummy,&isNew,&parent);
		isReadme = strstr(myname,"README")?YES:NO;
		mysize = -1;
		timestamp = 0;
	}
	return self;
}

- awake;
{
	[super awake];
	transferstatus = 0;
	shouldreget = 0;
	shouldautoopen = 0;
	localPath = NULL;
	return self;
}

- (unsigned) timestamp;
{
	return timestamp;
}

- (int) compareWith:otherFile;
{
	if ([self isDummy])
		return -1;
	if ([otherFile isDummy])
		return 1;
	return strcasecmp(myname,[otherFile name]);
}

- (const char *) name;
{
	//if (strchr(myname,'-'))
	//{
	//	LOGF("myname = |%s|",myname);
 	//}
	return myname;
}

- parentDir;
{
	return parent;
}

- (BOOL) isFile;
{
	return YES;
}

- (BOOL) isDir;
{
	return NO;
}

- (BOOL) isReadme;
{
	return isReadme;
}

- setFileSize:(long) s;
{
	mysize = s;
	return self;
}

- (long) fileSize;
{
	return mysize;
}

- setDummy;
{
	isDummy = TRUE;
	return self;
}

- (BOOL) isDummy;
{
	return isDummy;
}

- setNew:(BOOL)flag;
{
	isNew = flag;
	return self;
}

- (BOOL) isNew;
{
	return isNew;
}

- setTransferStatus:(char) status;
{
	if (transferstatus != status)
		[parent setNeedsDisplay:YES];
	transferstatus = status;
	return self;
}

- (char) transferStatus;
{
	return transferstatus;
}

- setShouldReget:(BOOL)flag;
{
	shouldreget = flag;
	return self;
}

- (BOOL) shouldReget;
{
	return shouldreget;
}

- setShouldAutoOpen:(BOOL)flag;
{
	shouldautoopen = flag;
	return self;
}

- (BOOL) shouldAutoOpen;
{
	return shouldautoopen;
}

- setLocalPath:(NXAtom)path;
{
	localPath = path;
	return self;
}

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

- setLocalName:(NXAtom)name;
{
	localName = name;
	return self;
}

- (const char *) localName;
{
	return localName?localName:myname;
}

// by Peter Lount
- fullPath:(char*)theFullPathPtr;
{
	sprintf(theFullPathPtr,
		"%s - %s (to %s)", 
		[self name], [parent path],
		[self localPath]
	);
	return self;
}


@end
