/*
 
Title:	MsgEd
  
File:   settings.c
  
Author:	Jim Nutt
	
Copr:   1988 by Jim Nutt
  
Description:
	
	Small, fast fido message editor.
	
Revision History:
  
        0.00    28 October 1988
	
Support Files:
  
	msged.h
  
*/

#include "msged.h"

#define TLEN    64
#define ILEN    40

#define FFIRST  1
#define FLAST   18

#define SWBEGIN  9
#define SWEND    18

#define SBEGIN  1
#define SEND    6

#define NBEGIN  7
#define NEND    8

#define SWLINE  15
#define STLINE  5
#define NULINE  11
#define PILINE  1

#define OFFSET 35

static void update(int f);

static  char    taddr[TLEN];

static  char   *tname,
               *tlist1,
               *tlist2,
               *tqs,
               *torg;

static  int     trm,
                ttab;

static  int     tseen,
                ttear,
                tcr,
                tconfirm,
                tread,
                tshow,
                thold,
                tpriv,
                tcrash,
                tkill;


void settings()

{
        int f = 1;
        int ch = 0;
        FILE *fp;
        char tmp[TLEN];
        char path[TLEN];

        /* temporary variables until <enter> is pressed */

        tname = strdup(username);
        tqs = strdup(quotestr);

        memset(tmp,0,sizeof tmp);
        sprintf(path,"%s\\origin",arealist[area].path);

        if ((fp = fopen(path,"rt")) != NULL) {
                fgets(tmp,sizeof(tmp),fp);
                fclose(fp);
        }
        else if (origin != NULL)
                strcpy(tmp,origin);
        else
                strcpy(tmp,username);

        torg = strdup(tmp);

        trm = rm;
        ttab = tabsize;

        tseen = seenbys;
        ttear = tearline;
        tcr = softcr;
        tconfirm = confirmations;
        tread = readthread;
        tshow = shownotes;
        thold = msgbits.hold;
        tpriv = msgbits.private;
        tcrash = msgbits.crash;
        tkill = msgbits.killsent;

        if (userlist != NULL)
                tlist2 = strdup(userlist);
        else
                tlist2 = strdup(" ");

        if (fidolist != NULL)
                tlist1 = strdup(fidolist);
        else
                tlist1 = strdup(" ");

        sprintf(taddr,"%d:%d/%d",thisnode.zone,thisnode.net,thisnode.node);
        if (thisnode.point != 0) {
                sprintf(tmp,".%d",thisnode.point);
                strcat(taddr,tmp);
        }
        if (thisnode.domain != NULL) {
                sprintf(tmp,">%s",thisnode.domain);
                strcat(taddr,tmp);
        }
        cls();
        gotoxy(1,PILINE);  bputs("msged " VERSION " settings");
        gotoxy(1,STLINE); bputs("String Settings");
        gotoxy(1,NULINE); bputs("Numeric Settings");
        gotoxy(1,SWLINE); bputs("Switches");
        for (f = FFIRST; f <= FLAST; f++)
                update(f);
        f = 1;
        gotoxy(1,22);
        bputs("________________________________________________________________________________");
        gotoxy(10,23); bputs("Use the arrow keys to select a setting or switch.");
        gotoxy(10,24); bputs("Press the <spacebar> to change a setting or toggle a switch.");
        gotoxy(10,25); bputs("Use the normal editing keys to change string and numeric settings.");

        while (ch != ABORT) {

                foreground(normback);
                background(normfore);
                update(f);
                background(normback);
                foreground(normfore);

                ch = getkey();

                if (ch != SPACE)
                        update(f);

                switch (ch) {
                        case UP:
                                if (f > FFIRST)
                                        f--;
                                else
                                        f = FLAST;

                                if ((f > SWBEGIN) && (f <= SWEND))
                                        f--;
                                break;

                        case DOWN:
                                if (f < FLAST)
                                        f++;
                                else
                                        f = FFIRST;

                                if ((f < SWEND) && (f > SWBEGIN))
                                        f++;
                                break;

                        case LEFT:
                                if (f > FFIRST)
                                        f--;
                                else
                                        f = FLAST;
                                break;

                        case RIGHT:
                                if (f < FLAST)
                                        f++;
                                else
                                        f = FFIRST;
                                break;

                        case ENTER :
                                seenbys = tseen;
                                tearline = ttear;
                                softcr = tcr;
                                confirmations = tconfirm;
                                shownotes = tshow;
                                readthread = tread;
                                free(username);
                                free(fidolist);
                                free(userlist);
                                free(quotestr);
                                free(origin);
                                origin = torg;
                                username = tname;
                                fidolist = tlist1;
                                userlist = tlist2;
                                quotestr = tqs;
                                thisnode = parsenode(taddr);
                                tabsize = ttab;
                                msgbits.private = tpriv;
                                msgbits.crash = tcrash;
                                msgbits.killsent = tkill;
                                msgbits.hold = thold;
                                rm = trm;
                                return;

                        case SPACE:
                                intense(ON);
                                switch (f) {
                                        case 1 : strcpy(tmp,tname);
                                                 gotoxy(OFFSET,PILINE + 1); bgets(tmp,ILEN);
                                                 free(tname);
                                                 tname = strdup(tmp);
                                                 break;
                                        case 2 : gotoxy(OFFSET,PILINE + 2); bgets(taddr,ILEN);
                                                 break;
                                        case 3 : strcpy(tmp,tlist1);
                                                 gotoxy(OFFSET,STLINE + 1); bgets(tmp,ILEN);
                                                 free(tlist1);
                                                 tlist1 = strdup(tmp);
                                                 break;
                                        case 4 : strcpy(tmp,tlist2);
                                                 gotoxy(OFFSET,STLINE + 2); bgets(tmp,ILEN);
                                                 free(tlist2);
                                                 tlist2 = strdup(tmp);
                                                 break;
                                        case 5 : strcpy(tmp,tqs);
                                                 gotoxy(OFFSET,STLINE + 3); bgets(tmp,ILEN);
                                                 free(tqs);
                                                 tqs = strdup(tmp);
                                                 break;
                                        case 6 : strcpy(tmp,torg);
                                                 gotoxy(OFFSET,STLINE + 4); bgets(tmp,ILEN);
                                                 free(torg);
                                                 override = TRUE;
                                                 torg = strdup(tmp);
                                                 break;
                                        case 7 : gotoxy(OFFSET,NULINE + 1);
                                                 ttab = getnum(1,trm,ttab);
                                                 break;
                                        case 8 : gotoxy(OFFSET,NULINE + 2);
                                                 trm = getnum(1,maxx,trm);
                                                 break;
                                        case 9 : tseen   = !tseen; break;
                                        case 10: ttear   = !ttear; break;
                                        case 11: tcr     = !tcr; break;
                                        case 12: tconfirm= !tconfirm; break;
                                        case 13: tshow   = !tshow; break;
                                        case 14: tread   = !tread; break;
                                        case 15: tpriv   = !tpriv; break;
                                        case 16: tkill   = !tkill; break;
                                        case 17: tcrash  = !tcrash; break;
                                        case 18: thold   = !thold; break;
                                }
                                intense(OFF);
                                break;
                        default :
                                switch (tolower(ch & 0xff)) {
                                        case 'n' : f = 1; break;
                                        case 'a' : f = 2; break;
                                        case 'p' : f = 3; break;
                                        case 's' : f = 4; break;
                                        case 'q' : f = 5; break;
                                        case 'o' : f = 6; break;
                                        case 't' : f = 7; break;
                                        case 'r' : f = 8; break;
                                        case 'b' : f = 9; tseen   = !tseen; break;
                                        case 'l' : f = 10; ttear   = !ttear; break;
                                        case 'c' : f = 11; tcr     = !tcr; break;
                                        case 'd' : f = 12; tconfirm= !tconfirm; break;
                                        case 'h' : f = 13; tshow   = !tshow; break;
                                        case 'f' : f = 14; tread   = !tread; break;
                                        case 'v' : f = 15; tpriv   = !tpriv; break;
                                        case 'i' : f = 16; tkill   = !tkill; break;
                                        case 'k' : f = 17; tcrash  = !tcrash; break;
                                        case 'e' : f = 18; thold   = !thold; break;
                                }
                                break;
                }
        }

        free(tname);
        free(tlist1);
        free(tlist2);
        free(tqs);
}

static void update(int f)

{
        switch (f) {
                case 1 : gotoxy(10,PILINE + 1); intense(ON); bputc('N'); intense(OFF); bputs("ame:"); gotoxy(OFFSET,PILINE + 1); bputs(tname);                              break;
                case 2 : gotoxy(10,PILINE + 2); intense(ON); bputc('A'); intense(OFF); bputs("ddress:"); gotoxy(OFFSET,PILINE + 2); bputs(taddr);                            break;
                case 3 : gotoxy(10,STLINE + 1); bputs("Userlist ("); intense(ON); bputc('P'); intense(OFF); bputs("rimary):"); gotoxy(OFFSET,STLINE + 1); bputs(tlist1);     break;
                case 4 : gotoxy(10,STLINE + 2); bputs("         ("); intense(ON); bputc('S'); intense(OFF); bputs("econdary):"); gotoxy(OFFSET,STLINE + 2); bputs(tlist2);   break;
                case 5 : gotoxy(10,STLINE + 3); intense(ON); bputc('Q'); intense(OFF); bputs("uote String:"); gotoxy(OFFSET,STLINE + 3); bputs(tqs);                         break;
                case 6 : gotoxy(10,STLINE + 4); intense(ON); bputc('O'); intense(OFF); bputs("rigin:"); gotoxy(OFFSET,STLINE + 4); bputs(torg);                              break;
                case 7 : gotoxy(10,NULINE + 1); intense(ON); bputc('T'); intense(OFF); bputs("ab setting:"); gotoxy(OFFSET,NULINE + 1); bprintf("%d",ttab);                  break;
                case 8 : gotoxy(10,NULINE + 2); intense(ON); bputc('R'); intense(OFF); bputs("ight margin:"); gotoxy(OFFSET,NULINE + 2); bprintf("%d",trm);                  break;
                case 9 : gotoxy(10,SWLINE + 1); bputs("Seen-"); intense(ON); bputc('B'); intense(OFF); bprintf("ys:        %s",(tseen   ) ? "Yes" : "No ");                  break;
                case 10: gotoxy(OFFSET,SWLINE + 1); bputs("Tear"); intense(ON); bputc('l'); intense(OFF); bprintf("ines:          %s",(ttear   ) ? "Yes" : "No ");              break;
                case 11: gotoxy(10,SWLINE + 2); bputs("Soft "); intense(ON); bputc('C'); intense(OFF); bprintf("Rs:        %s",(tcr     ) ? "Yes" : "No ");          break;
                case 12: gotoxy(OFFSET,SWLINE + 2); bputs("Confirm "); intense(ON); bputc('D'); intense(OFF); bprintf("eletes:    %s",(tconfirm) ? "Yes" : "No ");               break;
                case 13: gotoxy(10,SWLINE + 3); intense(ON); bputc('H'); intense(OFF); bprintf("idden Lines:    %s",(tshow   ) ? "Yes" : "No "); break;
                case 14: gotoxy(OFFSET,SWLINE + 3); intense(ON); bputc('F'); intense(OFF); bprintf("ast Read Threads:  %s",(tread) ? "Yes" : "No "); break;
                case 15: gotoxy(10,SWLINE + 4); bputs("Pri"); intense(ON); bputc('v'); intense(OFF); bprintf("iliged:      %s",(tpriv) ? "On " : "Off"); break;
                case 16: gotoxy(OFFSET,SWLINE + 4); bputc('K'); intense(ON); bputc('i'); intense(OFF); bprintf("ll/Sent:          %s",(tkill) ? "On " : "Off"); break;
                case 17: gotoxy(10,SWLINE + 5); intense(ON); bputc('K'); intense(OFF); bprintf("rash:           %s",(tcrash) ? "On " : "Off"); break;
                case 18: gotoxy(OFFSET,SWLINE + 5); bputc('K'); intense(ON); bputc('e'); intense(OFF); bprintf("ep (hold):        %s",(thold) ? "On " : "Off"); break;
        }
}
