/*
RECCOM.C

Sound record/playback application command manager
*/

#include <record.g>
#include <record.rsg>
#include <hwim.h>
#include <s_.rsg>

GLREF_D PR_RECENG * const receng;
GLREF_D TEXT * const DatUsedPathNamePtr;

LOCAL_C INT LaunchDial(VOID *buf,INT id,INT class)
    {
    DL_DATA dial_data;

    dial_data.id=id;
    dial_data.rbuf=buf;
    dial_data.pdlg=NULL;
    return(hLaunchDial(CAT_RECORD_RECORD,class,&dial_data));
    }

LOCAL_C VOID RecordToNewFile(TEXT *fname,INT DefaultName)
    {
    INT len;
    P_FPARSE crk;
    RECRBUF rb;

    rb.duration=((RECENG_PREFS *)p_send2(receng,O_ENG_SENSE_PREFS))->duration;
    f_fparse(fname,DatUsedPathNamePtr,&rb.fname[0],&crk);
    len=crk.system+crk.device+crk.path;
    if (DefaultName)
        len+=crk.name;
    rb.fname[len]=0;
    if (!LaunchDial(&rb,RECORD_NEW_DL,DefaultName?C_RECNEWDEF:C_RECNEW))
        p_send3(receng,O_ENG_OPEN_FILE,NULL);
    }

#pragma METHOD_CALL

METHOD INT reccom_com_file_change(PR_RECCOM *self,INT command,TEXT *newname)
    {
    p_send2(receng,O_ENG_SOUND_STOP);
    switch (command)
        {
    case H_COMMAND_CREATE_FILE:
        p_send3(receng,O_ENG_NEW_FILE,newname);
        p_send2(receng,O_ENG_VIEW_UPDATE);
        RecordToNewFile((TEXT *)p_send2(receng,O_ENG_SENSE_FILE),TRUE);
        break;
    case H_COMMAND_OPEN_FILE:
        p_send3(receng,O_ENG_OPEN_FILE,newname);
        break;
        }
    return(0); /* no leave() */
    }


METHOD VOID reccom_com_new_file(PR_RECCOM *self)
/*
Called on selecting the New file menu command.
*/
    {
    TEXT fspec[4];
    
    fspec[0]=p_send2(receng,O_ENG_DEFAULT_DISK);
    fspec[1]=':';
    fspec[2]=0;
    RecordToNewFile(&fspec[0],FALSE);
    }

METHOD VOID reccom_com_record_new(PR_RECCOM *self)
/*
Called on pressing the Record new (Tab) button.
*/
    {
    INT i,device;
    P_INFO info;
    TEXT name[32];
    TEXT fspec[P_FNAMESIZE];

    device=p_send2(receng,O_ENG_DEFAULT_DISK);
    for (i=1;i<100;i++)
        {
        hAtos(&name[0],DFLT_FNAME,device,i); /* "%c:RECORD%02d" */
        p_fparse(&name[0],DatUsedPathNamePtr,&fspec[0],NULL);
        if (p_finfo(&fspec[0],&info))
            { /* file does not exist - so use the generated name */
            RecordToNewFile(&fspec[0],TRUE);
            return;
            }
        }
    RecordToNewFile("",FALSE);
    }

METHOD VOID reccom_com_open_file(PR_RECCOM *self)
/*
Called on selecting the Open file menu command.
*/
    {
    RECRBUF rb;
    
    p_scpy(&rb.fname[0],(TEXT *)p_send2(receng,O_ENG_SENSE_FILE));
    if (!LaunchDial(&rb,OPEN_FILE_DL,C_RECOPEN))
        return;
    /* rb.fname[] contains file name */
    p_send3(receng,O_ENG_OPEN_FILE,&rb.fname);
    }

METHOD VOID reccom_com_re_record(PR_RECCOM *self)
/*
Called on pressing the Record over (Space) button.
*/
    {
    TEXT *fname;
    WVEFILE_INFO info;
    P_INFO finfo;
    RECRBUF rb;

    p_send3(receng,O_ENG_FILE_INFO,&info);
    fname=(TEXT *)p_send2(receng,O_ENG_SENSE_FILE);
    if (!info.duration)
        { /* file does not exist? */
        if (p_finfo(fname,&finfo)<0)
            RecordToNewFile(fname,TRUE);
        return;
        }
    rb.duration=info.duration;
    p_scpy(&rb.fname[0],fname);
    if (!LaunchDial(&rb,RE_RECORD_DL,C_RECEXIST))
        return;
    }

METHOD VOID reccom_com_set_repeat(PR_RECCOM *self)
    {
    LaunchDial(NULL,SET_REP_DL,C_SETREP);
    }

METHOD VOID reccom_com_rep_alarm(PR_RECCOM *self)
    {
    if (!hConfirm(ADJUST_ALARM_CONFIRM))
        return;
    p_send4(receng,O_ENG_SET_REPEATS,0,150); /* set total time to 15 seconds */
    }

METHOD VOID reccom_com_preferences(PR_RECCOM *self)
    {
    LaunchDial(NULL,PREFERENCES_DL,C_SETPREF);
    }
