
#import "JournalerApp.h"

// Written by Gideon King 
// Gideon@otago.ac.nz
// Freeware - but I'd be interested to know if anyone finds it useful

// This is a small program using NXJournals to record events in another application, 
// then play them back at some later stage.

@implementation JournalerApp

static char *filetype[] = { "journal", NULL };

- appDidInit
{
	[NXApp setJournalable:NO];
	[stopButton setEnabled:NO];
	return self;
}

- begin:sender
{
	stream = NXOpenMemory(NULL, 0, NX_WRITEONLY);
	[stopButton setEnabled:YES];
	[stopButton setIcon:"Stop"];
	[goButton setEnabled:NO];
	[goButton setIcon:"RecordD"];
	[playButton setEnabled:NO];
	[playButton setIcon:"PlayD"];
	theJournal = [[NXJournaler alloc] init];
	[theJournal setEventStatus:NX_RECORDING soundStatus:NX_STOPPED eventStream:stream soundfile:NULL];
	return self;
}

- end:sender
{
	id savePanel = [SavePanel new];

	[theJournal setEventStatus:NX_STOPPED soundStatus:NX_STOPPED eventStream:stream soundfile:NULL];

	[savePanel setTitle:"New Journal"];
	[savePanel setRequiredFileType:"journal"];
	
	if([savePanel runModal])
		if([savePanel filename])
		{
			errno = 0;
			remove([savePanel filename]);			// Removes the file if it's there
			switch(errno)
			{
				case 0:								// No problem deleting the file
				case ENOENT:						// Doesn't exist - Ok as well
					break;
				case EACCES:						// Permission denied
					NXRunAlertPanel("","Overwrite Permission Denied", 
						NULL, NULL, NULL);
				 	[theJournal free];
  					NXCloseMemory(stream, NX_FREEBUFFER);
					return self;
					break;
				default:
					NXRunAlertPanel("Error:", 
						"Unable to overwrite file with the name %s. Error #%d.",
						NULL, NULL, NULL, [savePanel filename], errno);
					[theJournal free];
				   	NXCloseMemory(stream, NX_FREEBUFFER);
					return self;
					break;
			}

			if(NXSaveToFile(stream, [savePanel filename]) == -1)		
				NXRunAlertPanel("Error:", 
					"Unable to save file with the name %s.",
					NULL, NULL, NULL, [savePanel filename]);
		}
   	NXCloseMemory(stream, NX_FREEBUFFER);
	[theJournal free];
	[stopButton setEnabled:NO];
	[stopButton setIcon:"StopD"];
	[goButton setEnabled:YES];
	[goButton setIcon:"Record"];
	[playButton setEnabled:YES];
	[playButton setIcon:"Play"];
	return self;
}

- playBack:sender
{
	id openPanel = [OpenPanel new];
		
	[openPanel setTitle:"Play a journal file"];
	 
	if ([openPanel runModalForTypes:filetype])
		if([openPanel filename])
		{
			if ((stream = NXMapFile([openPanel filename], NX_READONLY)) == NULL)
			{
				NXRunAlertPanel("Error:", 
					"Unable to open file with the name %s.",
					NULL, NULL, NULL, [openPanel filename]);
				return self;
			}

			[stopButton setEnabled:NO];
			[stopButton setIcon:"StopD"];
			[goButton setEnabled:NO];
			[goButton setIcon:"RecordD"];
			[playButton setEnabled:NO];
			[playButton setIcon:"PlayD"];
			theJournal = [[NXJournaler alloc] init];
			[theJournal setDelegate:self];

			[theJournal setEventStatus:(NX_PLAYING || NX_NONABORTABLEMASK)
				soundStatus:NX_STOPPED 
				eventStream:stream 
				soundfile:NULL];
		}
	return self;
}

- journalerDidEnd:sender
{
   	NXCloseMemory(stream, NX_FREEBUFFER);
	[theJournal free];
	[stopButton setEnabled:NO];
	[stopButton setIcon:"StopD"];
	[goButton setEnabled:YES];
	[goButton setIcon:"Record"];
	[playButton setEnabled:YES];
	[playButton setIcon:"Play"];

	return self;
}

@end
