/* DECOMMAN.C */

#include <demo.g>
#include <demo.rsg>
#include <printer.g>
#include <hwim.h>

GLREF_D PR_WSERV *w_ws;
GLREF_D PR_DECOMMAN *DatApp2;

#define SAVED_FILE_NAME "LOC::M:\\WDRPRINT.CFG"

LOCAL_C VOID TryLoadPrintContext(VOID)
    {
    P_INFO junk;
    VOID *fcb;
    VOID *printer;
    UBYTE buf[512];
    INT blen;
    INT ind;
    UBYTE *p1,*p2,*p3,*p4;

    if (p_finfo(SAVED_FILE_NAME,&junk))
        return;     /* eg file does not exist */
    f_open(&fcb,SAVED_FILE_NAME,P_FOPEN|P_FSTREAM|P_FSHARE);
    blen=f_read(fcb,&buf[0],512);
    p_close(fcb);
    if (blen<=sizeof(PRINTER_PARAMS)+2)
        goto file_corrupt;
    p1=(&buf[0]);
    blen-=sizeof(PRINTER_PARAMS);
    p2=p1+sizeof(PRINTER_PARAMS);
    ind=p_bloc(p2+1,blen-1,0);
    if (ind<0)
        goto file_corrupt;
    p3=p2+1+ind+1;
    blen-=1+ind+1;
    if (blen<=0)
        goto file_corrupt;
    ind=p_bloc(p3,blen,0);
    if (ind<0)
        goto file_corrupt;
    p4=p3+ind+1;
    blen-=ind+1;
    if (blen<=0)
        goto file_corrupt;
    ind=p_bloc(p4,blen,0);
    if (ind!=blen-1)
        {
    file_corrupt:
        hInfoPrint(DELETING_CORRUPT_FILE);
        p_delete(SAVED_FILE_NAME);
        return;
        }
    p_send2(w_ws,O_WS_ENS_PRINT_CONTEXT);
    printer=w_ws->wserv.printer;
    p_bcpy((VOID *)p_send2(printer,O_PR_GET_PARAMS),p1,sizeof(PRINTER_PARAMS));
    p_send5(printer,O_PR_SET_MODEL,FALSE,p2+1,*p2);
    p_send4(printer,O_PR_SET_HD,PRINTER_HDR_TOP,p3);
    p_send4(printer,O_PR_SET_HD,PRINTER_HDR_BOT,p4);
    }

#pragma save,ENTER_CALL

LOCAL_C INT SavePrintContext(VOID)
    {
    VOID *fcb;
    VOID *printer;
    TEXT *p;
    struct
        {
        UBYTE model;
        TEXT name[P_FNAMESIZE+1];
        } m;

    printer=w_ws->wserv.printer;
    if (!printer)
        return(0);
    f_open(&fcb,SAVED_FILE_NAME,P_FREPLACE|P_FSTREAM|P_FUPDATE);
    p=(TEXT *)p_send2(printer,O_PR_GET_PARAMS);
    f_write(fcb,p,sizeof(PRINTER_PARAMS));
    m.model=p_send3(printer,O_PR_SENSE_MODEL,&m.name[0]);
    f_write(fcb,&m.model,1+p_slen(&m.name[0])+1);
    p=(TEXT *)p_send3(printer,O_PR_GET_HD,PRINTER_HDR_TOP);
    f_write(fcb,p,p_slen(p)+1);
    p=(TEXT *)p_send3(printer,O_PR_GET_HD,PRINTER_HDR_BOT);
    f_write(fcb,p,p_slen(p)+1);
    p_close(fcb);
    return(0);
    }

#pragma restore

LOCAL_C INT LaunchDialog(INT class,INT resid,VOID *rbuf)
    {
    DL_DATA dld;

    dld.id=resid;
    dld.rbuf=rbuf;
    dld.pdlg=NULL;
    return hLaunchDial(CAT_DEMO_DEMO,class,&dld);
    }

LOCAL_C VOID PrintOrPreview(INT commid)
    {
    commid=(commid==O_DEC_PREVIEW? O_DEC_PRINT_DIRECT: 0);
    hDestroy(f_newsend(CAT_DEMO_DEMO,C_DELP,O_LPR_INIT,commid));
    }

#pragma METHOD_CALL

METHOD VOID decomman_com_init(PR_DECOMMAN *self)
    {
    DatApp2=self;
    TryLoadPrintContext();
    }

METHOD VOID decomman_com_exit(PR_DECOMMAN *self)
    {
    if (p_enter1(SavePrintContext))
        p_delete(SAVED_FILE_NAME);
    p_exit(0);
    }

METHOD VOID decomman_dec_psetup(VOID)
    {
    p_send2(w_ws,O_WS_EDIT_PRINT_CONTEXT);
    }

METHOD VOID decomman_dec_preview(PR_DECOMMAN *self,INT commid)
    {
    if (LaunchDialog(C_DEDLG,DELP_DLG,&commid))
        PrintOrPreview(commid);
    }

METHOD VOID decomman_dec_print_direct(VOID)
    {
    PrintOrPreview(O_DEC_PRINT);
    }
