/*$Copyright:
 * Copyright (C) 1992.5.22. Recruit Co.,Ltd. 
 * Institute for Supercomputing Research
 * All rights reserved.
 * NewsBase  by ISR, Kazuto MIYAI, Gary ARAKAKI, Katsunori SUZUKI, Kok-meng Lue
 *
 * You may freely copy, distribute and reuse the code in this program under 
 * following conditions.
 * - to include this notice in the source code, if it is to be distributed 
 *   with source code.
 * - to add the file named "COPYING" within the code, which shall include 
 *   GNU GENERAL PUBLIC LICENSE(*).
 * - to display an acknowledgement in binary code as follows: "This product
 *   includes software developed by Recruit Co.,Ltd., ISR."
 * - to display a notice which shall state that the users may freely copy,
 *   distribute and reuse the code in this program under GNU GENERAL PUBLIC
 *   LICENSE(*)
 * - to indicate the way to access the copy of GNU GENERAL PUBLIC LICENSE(*)
 *
 *   (*)GNU GENERAL PUBLIC LICENSE is stored in the file named COPYING
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
$*/
/* ISndD.m */

#import "ISndD.h"

#import <string.h>
#import <objc/hashtable.h>
#import <libc.h>
#import <sys/types.h>
#import <sys/stat.h>
#import <appkit/NXImage.h>
#import <math.h>
#import "errdebug.h"

@implementation ISndD

static NXAtom rtfControlWord;
static NXAtom fileExtension;
static NXImage *icon;
static NXImage *image;

+ initialize
{
    rtfControlWord = NXUniqueStringNoCopy("MMsnd");
    fileExtension = NXUniqueStringNoCopy("snd");
    image = [[NXImage alloc] initFromSection:"SoundButton.tiff"];
    [IMediaD getFileIconFor:(Class)self];
    return(self);
}

+ (NXAtom)rtfControlWord
{
    return(rtfControlWord);
}

+ (NXAtom)fileExtension;
{
    return(fileExtension);
}

+ (NXAtom)pasteboardType
{
    return(NULL);
}

+ setIcon:(NXImage *)theIcon
{
    icon = theIcon;
    return(self);
}

+ (NXImage *)icon
{
    return(icon);
}

- (NXImage *)image
{
    return(image);
}

- performDoubleClickAction:sender
{
    if ([sound status] != SK_STATUS_PLAYING) {
        SNDReserve(SND_ACCESS_IN | SND_ACCESS_OUT | SND_ACCESS_DSP, MAXINT);
        soundReserved = YES;
        [sound play];
    } else {
        [sound stop];
         SNDUnreserve(SND_ACCESS_IN | SND_ACCESS_OUT | SND_ACCESS_DSP);
        soundReserved = NO;

    }
    return(self);
}

- (BOOL)readFromStream:(NXStream *)inStream
{
    char tmpSndFile[64];

    [super readFromStream:inStream];
    // ## REWRITE! ## Really we can directly initialize the sound object
    // by writing our own initFromStream method for the Sound class
    sprintf(tmpSndFile, "/tmp/NewsBaseTmpSnDFile%d.snd", getpid());
    if (NXSaveToFile(stream, tmpSndFile) != 0) {
        return(NO);
    }
    sound = [Sound allocFromZone:[self zone]];
    if ([sound readSoundfile:tmpSndFile] != 0) {
        [sound free];
        sound = nil;
        unlink(tmpSndFile);
        return(NO);
    }
    unlink(tmpSndFile);
    return(YES);
}

- free
{
    if ([sound status] == SK_STATUS_PLAYING) {
        [sound stop];
        SNDUnreserve(SND_ACCESS_IN | SND_ACCESS_OUT | SND_ACCESS_DSP);
        soundReserved = NO;
    }
    if (soundReserved == YES) {
        SNDUnreserve(SND_ACCESS_IN | SND_ACCESS_OUT | SND_ACCESS_DSP);
        soundReserved = NO;
    }
    [sound free];
    return [super free];
}

@end
