/*
 *      You may freely copy, distribute and reuse the code
 *      in this example.  Scott Anguish disclaims any warranty of
 *      any kind, expressed or implied, as to its fitness
 *      for any particular use.
 *		Please keep this notice intact
 *      Written by: Scott Anguish  sanguish@digifix.com
 */

#import "MMCell.h"

#import <libc.h>					  /* MAXPATHLEN */
#import <stdio.h>
#import <strings.h>
#include <sys/types.h>
#include <ctype.h>
#include <stdlib.h>

#import <appkit/appkit.h>

#import <streams/streams.h>


@implementation MMCell:Cell

- initFromFilename:(const char *)filename
{
	NXBeep();
	[super init];
	[self imageFromFilename:filename];
	return self;
}


- imageFromAttachment
{
	return self;
}


- imageFromFilename:(const char *)filename
{
	return self;
}



- free
{
    fprintf(stderr,"Free\n");
	[theImage free];
	[self removeData];
	[super free];
	return self;
}

- calcCellSize:(NXSize *)theSize
{
    NXSize s;
	
    [theImage getSize: &s];
    s.height += 4.0;
    s.width += 4.0;
    *theSize = s;
    return self;
}

- highlight:(const NXRect *)cellFrame inView:controlView lit:(BOOL)flag;
{
    return self;
}


- writeRichText:(NXStream *)stream forView:view
{

	
	NXPrintf(stream, "NXPRINTF\n");
	return self;
}

- readPrivate:(NXStream *)stream name:(char *)name
{
	

    NXScanf(stream, "%s\n", name); 
	strcpy(attachmentName,name);
    NXScanf(stream, "%d ", &attachmentSize);
	

	attachmentData=malloc(attachmentSize);
	NXRead(stream, attachmentData,attachmentSize);
    return self;
}

- readRichText:(NXStream *)stream forView:view
{
    char name[MAXPATHLEN+1];

	MMView = view;
    [self readPrivate:stream name:name];
	[self uudecodeAttachment];
	[self imageFromAttachment];

    return self;
}

- (BOOL)trackMouse:(NXEvent *)theEvent 
	inRect:(const NXRect *)cellFrame 
	ofView:controlView;
{
	char filename[MAXPATHLEN+1];
	NXPoint thePoint;
	NXRect theRect;

	thePoint=theEvent->location;
	[MMView convertPoint:&thePoint fromView:NULL];
	NXSetRect(&theRect,thePoint.x-24,thePoint.y-24,48,48);
	sprintf(filename,"/tmp/%s",attachmentName);
	[MMView dragFile:filename fromRect:&theRect slideBack:YES event:theEvent];

	return YES;
}

- read:(NXTypedStream *)stream
{
	[super read:stream];
	return self;
};
 
- write:(NXTypedStream *)stream
{
	[super write:stream];
	return self;
};
 
- drawInside: (const NXRect *)cellFrame inView:controlView
{
	NXPoint	compositePlace;


	compositePlace.x = cellFrame->origin.x+ 2.0;
	compositePlace.y = NX_MAXY(cellFrame) + 2.0;
	[theImage composite:NX_SOVER toPoint:&compositePlace];

	return self;
}

- uudecodeAttachment
{
	FILE *fp;
	char commandStr[BUFSIZ];
	
	sprintf(commandStr,"rm -f /tmp/%s.Z; /usr/bin/uudecode ",attachmentName);
	fp = popen (commandStr,"w");
	if (fp != (FILE *)NULL)
		{
		fprintf(fp,"begin 644 /tmp/%s.Z\n",attachmentName);
		fwrite(attachmentData,sizeof(char),attachmentSize,fp);
		fprintf(fp,"end\n");
    	fprintf(stderr,"Pipe Result %d \n",pclose(fp));
		}
		else
		fprintf(stderr,"Pipe did not open \n");

	sprintf(commandStr,"rm -fR /tmp/%s; cd /tmp; zcat %s.Z | /NextApps/Mail.app/safetar xf -; rm -f %s.Z", attachmentName, attachmentName, attachmentName);
	fp = popen (commandStr,"r");
	if (fp != (FILE *)NULL)
		{
    	fprintf(stderr,"Pipe Result %d \n",pclose(fp));
		}
		else
		fprintf(stderr,"Pipe did not open \n");
	return self;
};

- removeData
{
	FILE *fp;
	char commandStr[BUFSIZ];
	
	sprintf(commandStr,"rm -f %s", attachmentName);
	fp = popen (commandStr,"r");
	if (fp != (FILE *)NULL)
		{
    	fprintf(stderr,"Pipe Result %d \n",pclose(fp));
		}
		else
		fprintf(stderr,"Pipe did not open \n");
	return self;

}
@end
