
/*
  ToDo
  ====

  Update .des of UserViewer with a 1999 date (Window title and IText)
*/


#include <exec/types.h>
#include <exec/memory.h>
#include <dos/dos.h>
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <clib/alib_protos.h>

#include <dos/dosextens.h>
#include <intuition/screens.h>
#include <intuition/intuition.h>
#include <intuition/gadgetclass.h>
#include <libraries/gadtools.h>
#include <diskfont/diskfont.h>
#include <utility/utility.h>
#include <graphics/gfxbase.h>
#include <workbench/workbench.h>
#include <graphics/scale.h>
#include <clib/wb_protos.h>
#include <clib/intuition_protos.h>
#include <clib/gadtools_protos.h>
#include <clib/graphics_protos.h>
#include <clib/utility_protos.h>
#include <string.h>
#include <clib/diskfont_protos.h>

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

#include <libraries/reqtools.h>
#include <clib/reqtools_protos.h>

#include "UserViewer.h"

#include <HBBS/ANSI_Codes.h>
#include <HBBS/Defines.h>
#include <HBBS/types.h>
#include <HBBS/structures.h>
#include <HBBS/hbbscommon_protos.h>
#include <HBBS/hbbscommon_pragmas.h>
#include <HBBS/Hbbsnode_protos.h>
#include <HBBS/Hbbsnode_pragmas.h>

#include <HBBS/release.h>
char *versionstr="$VER: UserViewer "RELEASE_STR;

#ifdef __SASC
int CXBRK(void) { return(0); }
int _CXBRK(void) { return(0); }
void chkabort(void) {}
#endif


struct ReqToolsBase *ReqToolsBase;
struct Library *HBBSCommonBase=NULL;
struct Library *HBBSNodeBase=NULL;
struct List *UserList;
struct NodeData *NodePtr;

struct BBSGlobalData *BBSGlobal=NULL;

ULONG rttags[] =
{
  RTGS_Flags, GSREQF_CENTERTEXT,
  RT_Underscore, '_',
  RTEZ_ReqTitle, (ULONG)"About User Viewer",
  RT_PubScrName, (ULONG)"CtrlScrn",
  TAG_END
};

char tmpstr[1024];
static VOID cleanup(ULONG num)
{
  if (HBBSCommonBase)
  {
    HBBS_CleanUpCommon();
    CloseLibrary (HBBSCommonBase);
  }

  if (ReqToolsBase)
    CloseLibrary ((struct Library *)ReqToolsBase);

  if (num) printf("Door Error = %d\n",num);

  exit(0);
}

static VOID init(char *name)
{
  if (!(ReqToolsBase = (struct ReqToolsBase *) OpenLibrary (REQTOOLSNAME, REQTOOLSVERSION)))
  {
    cleanup(0);
  }

  if(!(HBBSCommonBase = OpenLibrary("HBBSCommon.library",0)))
  {
    cleanup(1);
  }

  if (!(HBBS_InitCommon()))
  {
    cleanup(2);
  }

  SetProgramName(name);
}

void UpdateWindow ( void )
{
  int loop;
  struct Node *usernode;

  // turn of list before we modify it..
  GT_SetGadgetAttrs(UVGadgets[LV1],UV,NULL,GTLV_Labels,NULL,GTLV_Top,0,TAG_DONE);

  for (loop=0;loop<BBSGlobal->BBSNodes;loop++)
  {
    usernode=GetNode(UserList,loop);

    FreeStr(usernode->ln_Name);

    NodePtr=HBBS_NodeDataPtr(loop);
    switch (NodePtr->Status)
    {
      case STAT_READY:
        sprintf(tmpstr,"Node %d Is Awaiting Connect!",loop);
        break;
      case STAT_ONLINE:
        if (NodePtr->User->Valid)
        {
          sprintf(tmpstr,"Node %d, User: %s, Group: %s, Baud/CPS: %s",loop,NodePtr->User->CallData->Handle,NodePtr->User->CallData->Group,NodePtr->TransferringFile ? NodePtr->LastCPS : NodePtr->ConnectBaud );
        }
        else
        {
          sprintf(tmpstr,"Node %d, User has not logged in yet!",loop);
        }
        break;
      case STAT_CLOSED:
        sprintf(tmpstr,"Node %d is closed.",loop);
        break;
      default:
        sprintf(tmpstr,"Node %d is processing.",loop);
        break;
    }
    usernode->ln_Name=DupStr(tmpstr);
  }

  GT_SetGadgetAttrs(UVGadgets[LV1],UV,NULL,GTLV_Labels,UserList,GTLV_Top,0,TAG_DONE);
}

int ProcessWindowUV( LONG Class, UWORD Code, APTR IAddress )
{
struct Gadget *gad;
switch ( Class )
  {
  case IDCMP_GADGETUP :
    /* Gadget message, gadget = gad. */
    gad = (struct Gadget *)IAddress;
    switch ( gad->GadgetID )
      {
      case LV1 :
      case UV_UPDATE :
        /* ListView pressed, Text of gadget :  */
        UpdateWindow();
        break;
      case UV_ABOUT :
        sprintf(tmpstr,"UserViewer\n"
                       "==========\n"
                       "(C) 1999 Dominic Clifton\n");
        rtEZRequest(tmpstr,"Ok!",NULL,(struct TagItem *)&rttags,NULL);
      }
    break;
  case IDCMP_CLOSEWINDOW :
    /* CloseWindow Now */
    return(1);
    break;
  case IDCMP_REFRESHWINDOW :
    GT_BeginRefresh( UV);
    /* Refresh window. */
  RendWindowUV( UV, UVVisualInfo );
    GT_EndRefresh( UV, TRUE);
  GT_RefreshWindow( UV, NULL);
  RefreshGList( UVGList, UV, NULL, ~0);
    break;
  }
  return(0);
}

#define WAIT_TIME 30

void DoorMain(int argc,char *argv[])
{
  int done=0;
  ULONG class;
  UWORD code;
  struct Gadget *pgsel;
  struct IntuiMessage *imsg;
  ULONG ReturnedSig;
  struct Screen *CtrlScr;

  int loop;
  struct TimerSetupData *TSD;
  struct TimerData *TD;

  if (TSD=InitTimer())
  {


    if (UserList=AllocVec(sizeof(struct List),MEMF_PUBLIC))
    {
      NewList(UserList);


      for (loop=0;loop<BBSGlobal->BBSNodes;loop++)
      {
        if (NodePtr=HBBS_NodeDataPtr(loop))
        {
          NewStrNode("Initialising..",UserList);
        }
      }

      if (CtrlScr=LockPubScreen(BBSGlobal->ScreenInfo->PubScreenName))
      {
        ScreenToFront(CtrlScr);
        if (OpenWindowUV(CtrlScr)==0)
        {
          UnlockPubScreen(NULL,CtrlScr);
          UpdateWindow();
          if (TD=SubmitTimer(TSD,WAIT_TIME,0))
          {
            while(done==0)
            {
              ReturnedSig=Wait(1L << UV->UserPort->mp_SigBit | 1 << TSD->TimerPort->mp_SigBit);

              if ((CheckTimer(TSD,TD)) || (ReturnedSig & 1L << TSD->TimerPort->mp_SigBit))
              {
                AbortTimer(TSD,TD); // clean it up..

                UpdateWindow();

                TD=SubmitTimer(TSD,WAIT_TIME,0);
              }

              if (ReturnedSig & 1L << UV->UserPort->mp_SigBit)
              {
                while (imsg=GT_GetIMsg(UV->UserPort))
                {
                  class=imsg->Class;
                  code=imsg->Code;
                  pgsel=(struct Gadget *)imsg->IAddress;

                  GT_ReplyIMsg(imsg);

                  done=ProcessWindowUV(class, code, pgsel);
                }
              }
            }
            AbortTimer(TSD,TD);
          }
          CloseWindowUV();
        } else UnlockPubScreen(NULL,CtrlScr);
      }
      else puts("Error Opening Window!");
      FreeStrList(UserList);
    }
    CleanupTimer(TSD);
  }
}

int main(int argc,char *argv[])
{
  init("UserViewer");

  if (BBSGlobal=HBBS_GimmeBBS())
  {
      DoorMain(argc,argv);
  }
  cleanup(0);
}
