/******************************************************************************
FILE:				Progress.m
LANGUAGE:			Objective-C
SYSTEM:				UNIX 
USER-INTERFACE:		NEXTSTEP
DESCRIPTION
	This object creates a small window to display the progress made in filling
	a file. It probes the size of the file periodically until it's greater than
	a target size.
AUTHORS
	<PJB> Pascal J. Bourguignon
MODIFICATIONS
	1998/08/12 <PJB> Creation.
BUGS
LEGAL
	Copyright Pascal J. Bourguignon 1998 - 1998
	The GNU GENERAL PUBLIC LICENSE applies to this software.
	See the file named "COPYING".
******************************************************************************/
#import "Progress.h"
#import "DragBackgroundView.h"
#import "BarGauge.h"
#import <sys/types.h>
#import <sys/stat.h>


	static void timedEntryHandler(DPSTimedEntry tag,double now,void *userData)
	{
		Progress* progress=(Progress*)userData;
		[progress tick];
	}//timedEntryHandler;
	
	

@implementation Progress

	
 	-(id)initWithPath:(const char*)filePath expectedSize:(size_t)expectedSize
	{
		self=[super init];
		if(self!=0){
			window=0;
			gauge=0;
			ticking=NO;
			path=filePath;
			targetSize=expectedSize;
		}
		return(self);
	}//initWithPath:expectedSize:;
	
	
	-(id)appDidInit:sender
	{
		[NXApp setDelegate:0];
		[self start];
		return(0);
	}//appDidInit:;


	-(id)free
	{
		[self stop];
		if(window!=[NXApp appIcon]){
			[window performClose:self];
		}
		return([super free]);
	}//free;
	


	-(BOOL)fileExists
	{
     	struct stat status;
		int         res=stat(path,&status);
     	if(res==0){
			return((status.st_mode&S_IFMT)==S_IFREG);
		}else{
			return(NO);
		}
	}//fileExists;
	
	
	-(size_t)getFileSize
	{
     	struct stat status;
		stat(path,&status);
		return(status.st_size);
	}//getFileSize;
	
	
	-(void)start
	{
		if(!ticking){
			[self makeWindow];
			timedEntry=DPSAddTimedEntry(1.0,timedEntryHandler,
					(void*)self,NX_BASETHRESHOLD);
			ticking=YES;
		}
	}//start;
	
	
	-(void)tick
	{
		size_t size=[self getFileSize];
		[gauge setDoubleValue:(double)size];
		if(size>=targetSize){
			[self targetReached];
			[self stop];
		}
	}//tick;
	
	
	-(void)stop
	{
		if(ticking){
			DPSRemoveTimedEntry(timedEntry);
			ticking=NO;
		}
	}//stop;
	
	
	-reachedAction:sender
	{
		[self free];
		return([NXApp terminate:sender]);
	}//reachedAction:;
	
	
	-(void)makeWindow
	{
		//Let's canibalize the Application window.
		NXRect                  rect;
		DragBackgroundView*     contentView;
		ButtonCell*             contentCell;
		TextField*              nameText;
		
		[[NXApp appIcon]getFrame:&rect];
		rect.size.width*=4.0;
		window=[[Window alloc]initContent:&rect 
						style:NX_PLAINSTYLE        
						backing:NX_BUFFERED
						buttonMask:0
						defer:NO];
		[window setFreeWhenClosed:YES];
		[window setBackgroundGray:NX_LTGRAY];
		
		
		rect.origin.x=0.0;
		rect.origin.y=0.0;
		contentView=[[DragBackgroundView alloc]initFrame:&rect];
		[window setContentView:contentView];
		
		
		[window getFrame:&rect];
		rect.origin.x=10.0;
		rect.origin.y=46.0;
		rect.size.width-=20.0;
		rect.size.height=16.0;
		nameText=[[TextField alloc]initFrame:&rect];
		[nameText setEditable:NO];
		[nameText setEnabled:NO];
		[nameText setSelectable:YES];
		[nameText setBordered:NO];
		[nameText setTextGray:NX_BLACK];
		[nameText setBackgroundGray:NX_LTGRAY];
		[nameText setStringValue:path];
		[contentView addSubview:nameText];
	
		[window getFrame:&rect];
		rect.origin.x=10.0;
		rect.origin.y=24.0;
		rect.size.width-=20.0;
		rect.size.height=22.0;
		gauge=[[BarGauge alloc]initFrame:&rect];
		[gauge setBorderType:NX_RIDGE];
		//[gauge setLabelTicsEnable:YES];
		//[gauge setBorderType:NX_NOBORDER];
		[gauge setAutoScaleEnable:NO];
		[gauge setTicsEnable:YES];
		[gauge setLowThresh:0.0 HighThresh:0.95 AndQuanta:0];
		[gauge setMajorTics:10];
		[gauge setStartValue:0.0];
		[gauge setTicSize:((double)targetSize)/((double)[gauge majorTics])];
		[gauge setEnabled:NO];
		[gauge setDoubleValue:0.0];
		[contentView addSubview:gauge];

		[window display];
		[window makeKeyAndOrderFront:self];
		[[NXApp appIcon]orderOut:self];
	}//makeWindow;
	

	-(void)targetReached
	{
		NXRect     rect;
		Button*    reached;
		[window getFrame:&rect];
		rect.origin.x=10.0;
		rect.origin.y=4.0;
		rect.size.width-=20.0;
		rect.size.height=16.0;
		reached=[[Button alloc]initFrame:&rect
				title:"File complete! Quit"
				tag:0
				target:self
				action:@selector(reachedAction:)
				key:0
				enabled:YES];
		[[window contentView]addSubview:reached];
		[window display];
	}//targetReached;



	
@end //Progress.


/*** Progress.m / Wed Aug 12 03:59:35 MET 1998 / PJB ***/

