/*
 *   This sourcecode is part of FileSpy, a logfile observing utility.
 *   Copyright (C) 1996  Felix Rauch
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *   
 *   Notice that this program may not be possessed, used, copied,
 *   distributed or modified by people having to do with nuclear
 *   weapons. See the file CONDITIONS for details.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *   To contact the original author, try:
 *   e-mail: Felix.Rauch@nice.ch
 *   Traditional mail:	Felix Rauch
 *			Sempacherstrasse 33
 *			8032 Zurich
 *			Switzerland
 */

#import "Controller.h"

@implementation Controller(Sounds)

- soundClick:sender
{
    [self setSound:[sender stringValue]];
    return self;
}

- setSoundClick:sender
{
    id openPanel;
    const char *const fileType[2] = {"snd", NULL};
    const char *const *files;
    char tmpfile[MAXFILENAMELENGHT];
    const char *file = [soundTextField stringValue];

    if(file[0] == '\000')
	file = "/NextLibrary/Sounds";
    openPanel = [OpenPanel new];
    [openPanel chooseDirectories:NO];
    [openPanel allowMultipleFiles:NO];
    if([openPanel runModalForDirectory:file file:NULL types:fileType] == NX_OKTAG) {
	if((files = [openPanel filenames]) != (const char *const *)0) {
	    sprintf(tmpfile, "%s/%s", [openPanel directory], files[0]);
	    [self setSound:tmpfile];
	    [mySound play];
	    [soundTextField setStringValue:tmpfile];
	}
    }
    return self;
}

- setSound:(const char *)file
{
    if(file[0] == '\000') {	// use systembeep if no filename specified
	if(mySound != nil) {
	    [mySound free];
	    mySound = nil;
	}
	return self;
    }
    if(mySound != nil) {
	[mySound free];
	mySound = nil;
    }
    mySound = [[Sound alloc] initFromSoundfile:file];
    if(mySound == nil)
	return self;
    if(![mySound isPlayable]) {
	[mySound free];
	mySound = nil;
    } else {
	[(Sound *)mySound setName:file];
    }

    return self;
}

- nxbeep
{
    if(mySound != nil)
	[mySound play];
    else
	NXBeep();
    return self;
}

- (const char *)soundName
{
    if(mySound != nil)
	return [mySound name];
    else
	return "";
}

@end
