/*->c.newfax */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <signal.h>
#include <ctype.h>
#include <time.h>

#include "h.os"
#include "h.wimp"
#include "h.bbc"
#include "h.flex"
#include "h.werr"

#include "h.Drawlevel0"

#include "h.def"
#include "h.wos"
#include "h.timex"
#include "h.xext"
#include "h.main"
#include "h.mym"
#include "h.strdef"
#include "h.fax"
#include "h.serial"
#include "h.ram"
#include "h.file"
#include "h.pr"
#include "h.key"
#include "h.script"
#include "h.dir"
#include "h.view"
#include "h.batch"
#include "h.fo"
#include "h.pic"
#include "h.vx"
#include "h.sched"
#include "h.trans"
#include "h.state"
#include "h.band"
#include "h.faxfix"


#include "h.newfax"


/*****************************************************************************/

newbitstr defnewbits;


static int newbn;

static newbitstr xnewbits;


static void newrename(char * newname)
{
 if(cstrcmp(newname,vtable[TXBFILE][newbn].name))
 {
  view=TXBFILE;
  newbn=viewrenamelo(newbn,newname);
  batchmodded=1;
 }
}


static void newdest(char * dest,newbitstr * bits)
{
 decodeaddnamenumber(dest,vtable[TXBFILE][newbn].schedule,bits);
}


void autoforward(int bn)
{
 if(batforward)
 {
  newbn=batforwardfile(RXBFILE,TXBFILE,bn);
  newdest(batforwardname,&defnewbits);
 }
}




os_error * newfn(char * name)
{
 writeicon(whandle[NEWFAX],3,name);
 return(NULL);
}



static int newfaxpriorityf(int priority)
{
 xnewbits.priority=priority;
 writeicon(whandle[NEWFAX],8,priorityname(priority));
 return(priority);
}


static int newfaxbandf(int band)
{
 xnewbits.band=band;
 writeicon(whandle[NEWFAX],12,bandname(band));
 return(band);
}





/**************************************************************************/


static char * namep;
static char   oldname[32];


static void setnewbiticons(void)
{
 int handle=whandle[NEWFAX];

 selectst(handle,10,xnewbits.prefix);
 writeicon(handle,12,bandname(xnewbits.band));
 writeicon(handle,8,priorityname(xnewbits.priority));
}




/* look at the fax name, if it matches a directory entry,
   and the entry it matches is different to the last one it matched,
   then unless defnewdata.apply fill in the directory bits */


void newfaxzero(void)
{
 int n;

 if(cstrcmp(oldname,namep))
 {                            /* name has changed */
  n=findnamedynamic(namep);


  strcpy(oldname,namep);

  if(n>=0) 
  {
   getdirbits(n,&xnewbits);
   setnewbiticons();
  }
 }
}



static void startnewfaxzero(void)
{
 if(!xnewbits.apply)
 {
  namep=iconaddr(whandle[NEWFAX],3);
  oldname[0]=0;
  addzeroevent(NEWFAXZERO);
 }
}




static void endnewfaxzero(void)
{


 remzeroevent(NEWFAXZERO);
}




/***********************************************************************/


/* ok==1 means OK, ==0 means discard, ==-1 means forget it */
/* should only get -1 if has a close icon                  */

void newfaxclose(int ok)
{
 int    handle;
 char * p;

 handle=whandle[NEWFAX];

 if(ok==1)
 {
  newrename(iconaddr(handle,2));
  p=iconaddr(handle,3);

  if(strlen(p))
  {
   newdest(p,&xnewbits);
   if(!onlinestate()) setuptarget();
  }

  saveconfig();
 }
 else
 if(ok==0)
 {
  view=TXBFILE;
  deleteentry(newbn,1);
 }


 endnewfaxzero();
 closedownt(NEWFAX);
}



void newfaxdelete(int posn)
{
 if(newbn==posn) 
 {
  newfaxclose(-1);
 }
 else
 if(newbn>posn)  newbn--;
}



void newfaxicon(int icon)
{
 int handle;

 handle=whandle[NEWFAX];

 switch(icon)
 {
  case 16:
          report(newvx(TXBFILE,newbn));
          break;

  case 17:
          diraddnewentry(iconaddr(whandle[NEWFAX],3));
          break;

  case 13:
          popband(newfaxbandf,xnewbits.band);
          break;

  case  9:
          poppriority(newfaxpriorityf,xnewbits.priority);
          break;

   case 4:
          openentrymenu(newfn);
          break;


  case  5: /* Discard */
  case  6: /* OK      */
          newfaxclose(icon==6);
          break;

  case 10:
          selectst(handle,10,xnewbits.prefix^=1);
          break;
 }
}




/* have {} params been used ? */
/* return 0==no, 1==yes       */

static int arcfaxfix(void)
{
 char      faxto[128+4];
 char      faxname[68];
 char      faxafter[68];
 char    * p;
 char    * q;
 char    * nextq;
 arctime   timex;
 arctime   timenow;
 newbitstr bits;
 int       sn;
 int       n;



 if((p=getenv("ArcFax$FaxName"))!=NULL)
 {
  strcpy(faxname,p);
  newrename(faxname);
 }


 if((p=getenv("ArcFax$FaxTo"))!=NULL)
 {
  sn=vtable[TXBFILE][newbn].schedule;

  strcpy(faxto,p);

  /* rename was here */

  q=faxto;

  while(q)
  {
   p=strchr(q,'/');
   if(p) {nextq=p+1;*p=0;}
   else  nextq=NULL;

   bits=defnewbits;          /* think is override being zeroed ? */

   if(!xnewbits.apply)
   {
    n=findname(q);

    if(n>=0) 
    {
     getdirbits(n,&bits);
    }
   }

   if((p=getenv("ArcFax$FaxPrefix"))!=NULL)
   {
    strcpy(faxafter,p);
    if(!cstrcmp(faxafter,"Yes")) bits.prefix=1;
    else
    if(!cstrcmp(faxafter,"No")) bits.prefix=0;
   }

   if((p=getenv("ArcFax$FaxBand"))!=NULL)
   {
    strcpy(faxafter,p);
    bits.band=bandnametoband(faxafter);
   }

   if((p=getenv("ArcFax$FaxPriority"))!=NULL)
   {
    strcpy(faxafter,p);
    bits.priority=prioritynametopriority(faxafter);
   }

   newdest(q,&bits);

   if((p=getenv("ArcFax$FaxAfter"))!=NULL)
   {
    strcpy(faxafter,p);

    if(gettimedate(faxafter,&timex))
    {
     getcurtime(&timenow);
     if(cmptimex(&timex,&timenow)==1)
     {
      setaftertime(sn,&timex,0);
     }
    }
   }

   if((p=getenv("ArcFax$FaxMerge"))!=NULL)
   {
    strcpy(faxafter,p);
    viewmergeadd(TXBFILE,newbn,faxafter);
   }

   if(!onlinestate()) setuptarget();

   q=nextq;
  }

  return(1);
 }
 return(0);
}


static void arcfaxfixclr(void)
{
 int  i;
 char string[128];

 for(i=0;i<COMMANDS;i++)
 {
  sprintf(string,"Unset %s",commandname[i]);
  oscli(string);
 }
}


void arcfaxfixset(int set)
{
 switch(set)
 {
  case FAXFIXCLRV:
         arcfaxfixclr();
         break;

  case FAXFIXWRITE:
         arcfaxfixclr();
         os_cli("ArcFaxFix_Write");
         break;

  case FAXFIXRESET:
         os_cli("ArcFaxFix_Clear");
         break;
 }
}


void newfax(int bn)
{
 int handle;

 newbn=bn;        /* think newbn changes after rename */

 if(!arcfaxfix())
 {

  if(newfaxpopup)
  {
   handle=createwindow(NEWFAX);

   xnewbits=defnewbits;


   setnewbiticons();


   writeicon(handle,2,vtable[TXBFILE][newbn].name);
   writeicon(handle,3,"");

   shadeiconst(handle,4,!dirtot);


   popup(handle,0);

   iecarrot(handle,3);

   startnewfaxzero();
  }

  if(txbatpopup) opentxbatch();
 }
 arcfaxfixclr();
}



              /* N L R D U */

static char newfaxiclst[2][5]=
{
         2,          0,           0,          3,            3,
         3,          0,           0,          2,            2,
};




void newfaxkey(int * key)
{
 int cicon;
 int j;
 int ch;

 ch=*key;

 switch(ch)
 {
       case 27:
               newfaxclose(0);
               break;

       case CR:
               newfaxclose(1);
               break;

      case TAB:
               ch=CDOWN;

    case 0x18E:
    case 0x18F:
    case 0x19C:
    case 0x19D:
    case 0x19E:
    case 0x19F:
               ch&=0x18F;
               for(j=0;j<2;j++) if(newfaxiclst[j][0]==icon) break;
               cicon=newfaxiclst[j][(ch-0x18B)];
               if(cicon) iecarrot(whandle[NEWFAX],cicon);
               break;

    default:return;
 }
 *key=-1;
}

