
/* Generated by Interface Builder */
// Created By James T. Romano July 22, 1992 
// Member of the Dynamic Duo of NeXT Computers!
//
// 
// Read it and Weep!
#import "tarZanObject.h"

	BOOL selectionMade = NO;
	char *copyRightNotice = "tarZan was Created By James T. Romano & Salvatore Saieva (C) 1992";
	char *dduoNotice = "The Dynamic Duo of NeXT Computers!";
	char templateName[] = "/tmp/.tarZantarzee",*tempFile,actionMsg[1024],globalName[1024];
	int fd;
	NXStream *tempF = NULL;
	


@implementation tarZanObject

#define SHOWERROR(msg,where) NXRunAlertPanel(msg,where,"OK",NULL,NULL);
- selectMade
{
	selectionMade = YES;
	return self;
}

- init 
{
  [self initUsingFile:NULL withType:0];
  return self;
}

- initUsingFile:(const char *)fname withType:(int)type
{
  NXRect myFrame;
  unsigned int windowNum;
  char title[80],streamText[1024],tempChar;
  int tempCount = 0;
  
  [super init];
  [NXApp loadNibSection:"tarZanObj.nib" owner:self withNames:NO fromZone:[self zone]];
	
	if (fname) strcpy(globalName,fname);
	/* ********* */
	  //BROWSER STUFF
	    [browser setDelegate:self];
	    [browser setAction:@selector(selectMade)];
	    [browser setTitled:NO];
	    [browser allowMultiSel:YES];
	    [browser hideLeftAndRightScrollButtons:YES];
	    [browser setMaxVisibleColumns:1];
	    [browser sizeToFit];
	  //END
	  
	  //LISTENER SPEAKER STUFF FOR ICON DRAGGING
	    iconPathList = NULL;
	    listen = [Listener allocFromZone:[self zone]];
	    [listen init];
	    [listen setDelegate:self];
	    [listen usePrivatePort];
	    [listen addPort];
	    speak = [NXApp appSpeaker];
	    NXConvertWinNumToGlobal([window windowNum],&windowNum);
	    [speak setSendPort:NXPortFromName(NX_WORKSPACEREQUEST,NULL)];
	    [speak registerWindow:windowNum toPort:[listen listenPort]];
	  //END
   	if (!type) {
		sprintf(title,"tarZan V1.0 winNum %d",windowNum);
		[window setTitle:title];
	}
	else {
	  [window setTitleAsFilename:fname];
	  [matrix setEnabled:NO];
	}
	
    	if ([NXApp mainWindow]) {
	  [[NXApp mainWindow] getFrame:&myFrame];
	  [window moveTo:myFrame.origin.x -10 :myFrame.origin.y +10];
	}
	else
	  [window center];
  	
  [window makeKeyAndOrderFront:self];  
 
  mode = type;	//This is for the iconReleasedAt: method
  
  switch(type) {
    case 0:  //We are using all available procedures to compress and or tar files!
       	     [[matrix cellAt:1 :0] setEnabled:NO];
	     [[matrix cellAt:1 :1] setEnabled:NO];
	     [[matrix cellAt:1 :2] setEnabled:NO];
	     break;
    case 1:  //We are Going to un-tar a file but we must display it's guts etc..
	     [[matrix cellAt:1 :0] setEnabled:YES];
	     tempFile = NXGetTempFilename(templateName,12);
	     if ((fd = creat(tempFile,(int)00666) ) == -1) {
	     	SHOWERROR("Error in tarZanObject untar","Couldn't create temp file");
	     }
	     else {
	     	sprintf(actionMsg,"tar tf %s > %s",fname,tempFile);
		system(actionMsg);
		if (!(tempF = NXMapFile(tempFile,NX_READONLY))) {
			SHOWERROR("Error in tarZan Object","Couldn't open Stream using fd");
		}
		else {
			oldCount = 0;
			while (!NXAtEOS(tempF)) {
			  tempChar = NXGetc(tempF);
			  if (tempChar != '\n')
			  	streamText[tempCount++] = tempChar;
			  else if (tempChar == '\n') {
			    streamText[tempCount] = '\0';
			    tempCount = 0;			  
			    ++oldCount;
			    [[browser matrixInColumn:0] addRow];
 			    [[browser matrixInColumn:0] putCell:[[[[myBrowserCell alloc] initTextCell:streamText] setLeaf:YES] setLoaded:YES] at:oldCount-1 :0];
			  }
		       }
		       [browser reloadColumn:0];
		       NXClose(tempF);
		}
	     }
	     unlink(tempFile);
    	     break;
    case 3:  //We are going to uncompress and un-tar the file... display guts!
    	     [[matrix cellAt:1 :2] setEnabled:YES];
	     tempFile = NXGetTempFilename(templateName,12);
	     if ( (fd = creat(tempFile,(int)00666) ) == -1) {
	     	SHOWERROR("Error in tarZanObject","Couldn't create temp file");
	     }
	     else {
	     	sprintf(actionMsg,"zcat %s | tar tf - > %s",fname,tempFile);
		system(actionMsg);
		if (!(tempF = NXMapFile(tempFile,NX_READONLY))) {
			SHOWERROR("Error in tarZan Object","Couldn't open Stream using fd");
		}
		else {
			oldCount = 0;
			while (!NXAtEOS(tempF)) {
			  tempChar = NXGetc(tempF);
			  if (tempChar != '\n')
			  	streamText[tempCount++] = tempChar;
			  else if (tempChar == '\n') {
			    streamText[tempCount] = '\0';
			    tempCount = 0;			  
			    ++oldCount;
			    [[browser matrixInColumn:0] addRow];
 			    [[browser matrixInColumn:0] putCell:[[[[myBrowserCell alloc] initTextCell:streamText] setLeaf:YES] setLoaded:YES] at:oldCount-1 :0];
			  }
		       }
		       [browser reloadColumn:0];
		       NXClose(tempF);
		}
	     }
	     unlink(tempFile);
	     break;
    default: 
    	     break;
  }

  return self;
}

- procedure:sender
{	BOOL isThere = NO;
	SavePanel *savePanel;
	const char *tmpChar,*tmp2Char;
	char tarBuffer[1024],tmpBuffer[1024],*tmpPtr;
	int totalCount = 0,decCount = 0,tmpCount = 0,charCount = 0;
	
	switch([[sender selectedCell] tag]) {
		case 0:	//tar a file(s)
			if (!selectionMade)
				return self;
			if (oldCount == 0)
				return self;
			savePanel = [SavePanel new];
			[savePanel setTitle:"tar"];
			[savePanel setRequiredFileType:"tar"];
			if ([savePanel runModalForDirectory:NXHomeDirectory() file:"untitled"]) {
				tmpCount = oldCount;
				while (![[[browser matrixInColumn:0] cellAt:tmpCount :0] isHighlighted])
					--tmpCount;
				tmpChar = [[[browser matrixInColumn:0] cellAt:tmpCount :0] stringValue];
				tmpPtr = strrchr(tmpChar,'/');
				for (charCount = 0; charCount <= (tmpPtr-tmpChar) ; charCount++)
					tmpBuffer[charCount] = tmpChar[charCount];
				tmpBuffer[(tmpPtr-tmpChar)+1] = '\0';
				sprintf(tarBuffer,"(cd %s ; tar cf %s %s)",tmpBuffer,[savePanel filename],tmpPtr+1);
				[[browser matrixInColumn:0] removeRowAt:tmpCount andFree:YES];
				decCount++;
				system(tarBuffer);
				for (totalCount = tmpCount ; totalCount >= 0; --totalCount) {
					if ([[[browser matrixInColumn:0] cellAt:totalCount :0] isHighlighted]) {
						tmpChar = [[[browser matrixInColumn:0] cellAt:totalCount :0] stringValue];
						tmpPtr = strrchr(tmpChar,'/');
						for (charCount = 0; charCount <= (tmpPtr-tmpChar) ; charCount++)
							tmpBuffer[charCount] = tmpChar[charCount];
						tmpBuffer[(tmpPtr-tmpChar)+1] = '\0';
						sprintf(tarBuffer,"(cd %s ; tar uf %s %s)",tmpBuffer,[savePanel filename],tmpPtr+1);
						[[browser matrixInColumn:0] removeRowAt:totalCount andFree:YES];
						decCount = decCount + 1;
						isThere = YES;
						system(tarBuffer);
					}
				}
				oldCount = (oldCount - decCount);
				if (oldCount <= 0 ) selectionMade = NO;
				[browser reloadColumn:0];
			}
			break;
		case 1:	//compress a file(s)
			if (!selectionMade)
				return self;
			if (oldCount == 0)
				return self;
			if (!NXRunAlertPanel("Hold On!","Are You Sure You Want to do This? ","YES","NO",NULL))
				return self;
			for (totalCount = oldCount; totalCount >= 0; --totalCount) {
				if ([[[browser matrixInColumn:0] cellAt:totalCount :0] isHighlighted]) {
					sprintf(tarBuffer,"compress %s",[[[browser matrixInColumn:0] cellAt:totalCount :0] stringValue]);
					system(tarBuffer);
					[[browser matrixInColumn:0] removeRowAt:totalCount andFree:YES];
					decCount = decCount + 1;
				}
			}
			oldCount = oldCount - decCount;
			if (oldCount <= 0 ) selectionMade = NO;
			[browser reloadColumn:0];
			break;
		case 2: //tar & compress file(s)
			if (!selectionMade)
				return self;
			if (oldCount == 0)
				return self;
			savePanel = [SavePanel new];
			[savePanel setTitle:"tar & compress"];
			[savePanel setRequiredFileType:"tar"];
			if ([savePanel runModalForDirectory:NXHomeDirectory() file:"untitled"]) {
				tmpCount = oldCount;
				while (![[[browser matrixInColumn:0] cellAt:tmpCount :0] isHighlighted])
					--tmpCount;
				tmpChar = [[[browser matrixInColumn:0] cellAt:tmpCount :0] stringValue];
				tmpPtr = strrchr(tmpChar,'/');
				for (charCount = 0; charCount <= (tmpPtr-tmpChar) ; charCount++)
					tmpBuffer[charCount] = tmpChar[charCount];
				tmpBuffer[(tmpPtr-tmpChar)+1] = '\0';
				sprintf(tarBuffer,"(cd %s ; tar cf %s %s)",tmpBuffer,[savePanel filename],tmpPtr+1);
				[[browser matrixInColumn:0] removeRowAt:tmpCount andFree:YES];
				decCount++;
				system(tarBuffer);
				for (totalCount = tmpCount ; totalCount >= 0; --totalCount) {
					if ([[[browser matrixInColumn:0] cellAt:totalCount :0] isHighlighted]) {
						tmpChar = [[[browser matrixInColumn:0] cellAt:totalCount :0] stringValue];
						tmpPtr = strrchr(tmpChar,'/');
						for (charCount = 0; charCount <= (tmpPtr-tmpChar) ; charCount++)
							tmpBuffer[charCount] = tmpChar[charCount];
						tmpBuffer[(tmpPtr-tmpChar)+1] = '\0';
						sprintf(tarBuffer,"(cd %s ; tar uf %s %s)",tmpBuffer,[savePanel filename],tmpPtr+1);
						[[browser matrixInColumn:0] removeRowAt:totalCount andFree:YES];
						decCount = decCount + 1;
						isThere = YES;
						system(tarBuffer);
					}
				}
				oldCount = (oldCount - decCount);
				if (oldCount <= 0) selectionMade = NO;
				[browser reloadColumn:0];
				sprintf(tarBuffer,"compress %s",[savePanel filename]);
				system(tarBuffer);
				
			}
			break;
		case 3: //untar file(s)
			if (!selectionMade) 
				return self;
			if (oldCount <= 0)
				return self;
			savePanel = [SavePanel new];
			[savePanel setTitle:"untar"];
			if([savePanel runModalForDirectory:NXHomeDirectory() file:"Do_Not_Erase_This"]) {
				tmpChar = [savePanel directory];
				for (totalCount = oldCount ; totalCount >=0 ; --totalCount) {
					if ([[[browser matrixInColumn:0] cellAt:totalCount :0] isHighlighted]) {
							tmp2Char = [[[browser matrixInColumn:0] cellAt:totalCount :0] stringValue];
							strcpy(tarBuffer,"(cd / ; tar xf ");
							strcpy(tarBuffer,"(cd ");
							strcat(tarBuffer,tmpChar);
							strcat(tarBuffer," ; tar xf ");
							
							strcat(tarBuffer,globalName);
							strcat(tarBuffer," ");
							strcat(tarBuffer,tmp2Char);
							strcat(tarBuffer,")");
							[[browser matrixInColumn:0] removeRowAt:totalCount andFree:YES];
							decCount = decCount + 1;
							system(tarBuffer);
					}
				}
				oldCount = (oldCount - decCount);
				[browser reloadColumn:0];
			}
			if (oldCount <= 0)
				[window close];
			break;
		case 5: //untar & uncompress file(s)
			if (!selectionMade) 
				return self;
			if (oldCount <= 0)
				return self;
			savePanel = [SavePanel new];
			[savePanel setTitle:"untar"];
			if([savePanel runModalForDirectory:NXHomeDirectory() file:"Do_Not_Erase_This"]) {
				tmpChar = [savePanel directory];
				sprintf(tarBuffer,"(cd %s ; zcat %s | tar xf - ",tmpChar,globalName);
				for (totalCount = oldCount ; totalCount >=0 ; --totalCount) {
					if ([[[browser matrixInColumn:0] cellAt:totalCount :0] isHighlighted]) {
							strcat(tarBuffer,[[[browser matrixInColumn:0] cellAt:totalCount :0] stringValue]);
							strcat(tarBuffer," ");
							[[browser matrixInColumn:0] removeRowAt:totalCount andFree:YES];
							decCount = decCount + 1;
							isThere = YES;
					}
				}
				strcat(tarBuffer,")");
				oldCount = (oldCount - decCount);
				[browser reloadColumn:0];
				if (isThere)
					system(tarBuffer);
				if (oldCount <=0)
					[window close];
			}
			break;
		default:
			break;
	}
    return self;
}

- free
{	unsigned int windowNum;
	
	NXConvertWinNumToGlobal([window windowNum],&windowNum);
	[speak unregisterWindow:windowNum];
	[browser free];
	[window free];
	return [super free];
}

// Window Delegate Methods
- windowWillClose:sender
{
	[NXApp delayedFree:self];
	return self;
}

// Browser  Delegate Methods
- (int)browser:sender fillMatrix:tmatrix inColumn:(int)column
{
	int i;

	for (i = 0; i<oldCount;i++) 
		[sender getLoadedCellAtRow:i inColumn:0];
	return oldCount;
}

// Listener Delegate Methods
- (int)iconEntered:(int)windowNum at:(double)x :(double)y 
	iconWindow:(int)iconWindowNum iconX:(double)iconX iconY:(double)iconY 
	iconWidth:(double)iconWidth iconHeight:(double)iconHeight
	pathList:(const char *)pathList
{
  if (!iconPathList) goto icLabel1;
  if (strcmp(iconPathList,pathList)) goto icLabel1;
  goto icLabel2;
icLabel1:
  NX_FREE(iconPathList);
  iconPathList = NXCopyStringBuffer(pathList);
icLabel2:  
  return 0;
}

 	char *ir_tmpChar,ir_action[1024],dirBuff[255],another[1024],*file,*ptr;
	int icount;
	
- (int)iconReleasedAt:(double)x :(double)y ok:(int *)flag
{
	
  file = iconPathList;
 NX_DURING
   file = iconPathList; 
 NX_HANDLER
 NX_ENDHANDLER
 if (!file) 
   *flag = 0;
 else
   *flag = 1;
 
	
	if (mode == 3) {
 		sprintf(ir_action,"uncompress %s",globalName);
		system(ir_action);
		strcpy(another,globalName);	//Once we uncompress the string we must reflect the .tar so 
		another[strlen(another)-2] = '\0';// we get rid of the .Z part of the string by placing a NULL where the . is
	}

 	ptr = strtok(file,"\t");
 	++oldCount;
 	[[browser matrixInColumn:0] addRow];
	if ( (mode == 1) || (mode == 3) ) {
		ir_tmpChar = strrchr(ptr,'/');
		for (icount = 0; icount <= (ir_tmpChar-ptr) ; icount++)
			dirBuff[icount] = ptr[icount];
		dirBuff[(ir_tmpChar-ptr)+1] = '\0';
		if (mode == 3)
			sprintf(ir_action,"(cd %s ; tar uf %s %s)",dirBuff,another,ir_tmpChar+1);
		if (mode == 1)
			sprintf(ir_action,"(cd %s ; tar uf %s %s)",dirBuff,globalName,ir_tmpChar+1);
		system(ir_action);
	}
	if ((mode == 1) || (mode == 3))
 		[[browser matrixInColumn:0] putCell:[[[[myBrowserCell alloc] initTextCell:ir_tmpChar+1] setLeaf:YES] setLoaded:YES] at:oldCount-1 :0];
	else
		[[browser matrixInColumn:0] putCell:[[[[myBrowserCell alloc] initTextCell:ptr] setLeaf:YES] setLoaded:YES] at:oldCount-1 :0];
 	while((ptr = strtok(NULL,"\t"))!= NULL) {
   	++oldCount;
   	[[browser matrixInColumn:0] addRow];
	if ( (mode == 1) || (mode == 3) ) {
		ir_tmpChar = strrchr(ptr,'/');
		for (icount = 0; icount <= (ir_tmpChar-ptr) ; icount++)
			dirBuff[icount] = ptr[icount];
		dirBuff[(ir_tmpChar-ptr)+1] = '\0';
		if (mode == 3)
			sprintf(ir_action,"(cd %s ; tar uf %s %s)",dirBuff,another,ir_tmpChar+1);
		if (mode == 1)
			sprintf(ir_action,"(cd %s ; tar uf %s %s)",dirBuff,globalName,ir_tmpChar+1);
		system(ir_action);
	}

   	if ((mode == 1) || (mode == 3))
 		[[browser matrixInColumn:0] putCell:[[[[myBrowserCell alloc] initTextCell:ir_tmpChar+1] setLeaf:YES] setLoaded:YES] at:oldCount-1 :0];
	else
		[[browser matrixInColumn:0] putCell:[[[[myBrowserCell alloc] initTextCell:ptr] setLeaf:YES] setLoaded:YES] at:oldCount-1 :0];

 	}
 	if (mode == 3) {
		sprintf(ir_action,"compress %s",another);
		system(ir_action);
	}
 	[browser reloadColumn:0];
 return 0;
}
@end