#if !defined(__alffnprt_h)              // Sentry, use file only if it's not already included.
#define __alffnprt_h
/*  Project Tec3270

	 SUBSYSTEM:    Tec3270.exe (Tec3270 application)
	 FILE:         alffnprt.h
	 AUTHOR:       Alcindo Ferreira Filho

	 OVERVIEW:

    ALFFRemotePrinter Class Definition, the Tec3270 remote printer
    session class. Sessions are childs of class ALFFMDIClient,
    the MDI manager.

    COPYRIGHT:    © 1998-2007
                  Alcindo Ferreira Filho
                  <alcindo@inttec.com.br>

    LICENSE:      Tec3270_License (docs/Tec3270_License.txt)
*/

/* TecLib Classes */
#include "alffnaux.h"
#include "alffmsgs.h"   		/* TecLib Message Object Include    */
#include "alffinif.h"  		 	/* TecLib Ini File Object Include   */
#include "alfflogf.h"   		/* TecLib Log File Object Include   */
#include "alffsbar.h"   		/* TecLib Status Bar Object Include */
#include "alffprtn.h"   		/* Tec3270 Printer Objects           */
#include "alffstat.h"   		/* ALFFStatic Class                  */
#include "ALFFUseSecurity.h" 	/* ALFFUseSecurity Class - Security Initialization */

/* Tec3270 classes */
#include "alffnttl.h"
#include "ALFFNvt.h"
#include "alff3270.h"
#include "alffsocket.h"
#include "tec3270.rh"   // Definition of all resources.

//{{ALFFDialog = ALFFRemotePrinter}}
class ALFFRemotePrinter : public ALFFDialog
{
 public:
	 ALFFRemotePrinter (TWindow*  parent,
                          ALFFDate::DateFormat fmt,
                          ALFFConnData* cnnData,
							     int sessionId);

	 virtual ~ALFFRemotePrinter ();
    char far* GetClassName() {return "ALFFRemotePrinter";}
	 void Connect();
	 bool Disconnect();
	 ALFFNvt*       GetNVTBufPtr()     {return pNVTBuf;}
	 ALFF3270*      Get3270BufPtr()    {return p3270Buf;}
    ALFFTelnet*    GetTelnetPtr()     {return pTelnet;}
	 ALFFConnData*  GetChildConnData() {return connData;}
    bool IsIdle()              {return (nLineState == nIdle);}
	 bool IsConnected()         {return (nLineState == nConnected);}
	 bool IsConnecting()        {return (nLineState == nConnecting);}
	 bool IsSSLConnecting()     {return (nLineState == nSSLConnecting); }
    bool IsSSLConnected()      {return (nLineState == nSSLConnected); }
    bool IsSSLDisconnecting()  {return (nLineState == nSSLDisconnecting); }
    bool UpdateSecurityOptions(bool verifyCert, bool verifyRevoked);


 protected:
    enum nStateLU1 {nLU1_IDLE, nLU1_SCSDATA, nLU1_SCS_FMT, nLU1_SCS_FMT_SPDPPM,
              nLU1_STRUCTFLD};

	 /* Objects that are needed to establish any 3270 session to host  */
    ALFFDate::DateFormat dateFormat;   /* Current localization        */
	 TWindow*          myParent;        /* Remote Printer Parent       */
	 ALFFConnData*     connData;        /* Connection Struct Data      */
	 ALFFLogFile*      pTrcFile;        /* Line Recv/Send Trace Data   */
	 ALFFLogFile*      pConnLog;        /* Session Messages Log File   */
	 ALFFCommLine*     pLineComm;       /* Communications (Winsock)    */
	 ALFFTelnet*       pTelnet;         /* TELNET Encoder/Decoder      */
	 ALFFNvt*          pNVTBuf;         /* TELNET NVT Buffer Manager   */
	 ALFF3270*         p3270Buf;        /* 3270 Buffer Manager         */
	 ALFFMsgs*  		 pMsgs;           /* Messages/Strings Manager    */
    TMenuDescr*       prtMenu;         /* Remote Printer Window Menu  */

    /* Remote Printer Status Bar */
    HWND              prtStatusBar;    /* Remote Printer Status Bar   */
#define PRTSTATBARPARTS     4
    int*              statParts;
    TRect             sslLockRect;
    TPoint            mousePos;

    /* Remote Printer Indicators */
    TStatic*			 prtHostName;
    ALFFStatic*       prtHostPc;
    TStatic*          prtPCName;
    ALFFStatic*       prtPCPrinter;
    TStatic*          prtPrinterName;

    /* Session Buffers (Berkeley Ring Buffers) */
	 LPALFFBUF     pLineInBuf;
	 LPALFFBUF     pLineOutBuf;
	 LPALFFBUF     pNetInBuf;
	 LPALFFBUF     pNetOutBuf;
	 LPALFFBUF     pOOBData;

	 /* Session Status Controls */
	 int           connNumber;
	 int           connShift;
	 bool          closeConn;
	 bool          autoConnect;
	 long          connTimeout;
	 int           recvQueue;
	 bool          receiving;
	 int           sendQueue;
	 bool          sending;
	 bool          termBufferInitialized;
	 bool          traceON;
	 bool          scrLogOn;
	 int           numReconnTries;
    bool          scriptProcessing;
    bool          disconnectedByHost;
    
	 int           netInputQueue;
	 bool          netInputProcessing;
    WPARAM*       dataTypeQueue;

	 /* Session Window Title Composition:
	  * connTitle = connStatusTitle + connTextTitle + connTraceTitle */
	 string        connInitialTitle;
	 string        connTitle;
	 string        connStatusTitle;
    string        connSSLStatus;
	 string        connTextTitle;
	 string        connTraceTitle;

	 string        connHostAddress;
	 string        connPortAddress;

	 /*        Line Connection State Information            */
	 enum      nState  {nIdle, nConnecting, nConnected,
                       nSSLConnecting, nSSLConnected,
                       nSSLDisconnecting
                      } nLineState;
	 bool      dataSent;

    /* Session SSL Controls           */
    bool                      connectPendingForReadWrite;
    bool                      receiveDataPendingForReadWrite;
    bool                      sendDataPendingForReadWrite;
    bool                      disconnectPendingForReadWrite;


    /* Protected data members used by the print menu commands  */
	 ALFFPrinter*    			Printer;      // Printer support.
	 bool            			Printing;      // Printing in progress.
    ALFFPrtData*    			prtData;
    TPrintDialog::TData* 	tPrintData;
    ALFFPrinterBuffer*     printerBuf;
    long                   totalReportsQuantity;
    long                   totalReportsLines;
    long                   thisReportLines;
    
	 /* Printer Font Controls   */
	 char*  prtFontName;
	 int    prtCharWidth;
	 int    prtCharHeight;
	 int    prtFontSize;
	 int    prtFontWeight;
	 int    prtFontItalic;
	 int    prtFontCharset;
	 TFont* prtFont;

    /* Query Reply List:
		 Byte 01 - Request Type B'00' = QCODE List
										B'01' = QCODE List + Equivalent
										B'10' = All
										B'11' = Reserved
		 Bytes 02-nn Query Reply Codes List (ended with 0xFFFF)
	 */
	 char*         queryReplyList;

    /* LU1 / LU3 Begin/End of Job controls  */
    bool beginEndOfJobStrFd;
    bool beginOfPrintJob;
    bool endOfPrintJob;

    /* LU1 Format Controls */
    unsigned char horizontalFmt[256];
    unsigned char verticalFmt[256];
    unsigned char channels1_12[12];
    
    /* Remote Printer Connect Disconnect Menu Commands */
	 void CmConnect();
	 void CmConnectEnable(TCommandEnabler &ce);
	 void CmDisconnect();
	 void CmDisconnectEnable(TCommandEnabler &ce);
    void CmChangePrtSess();
    void CmChoosePrtFont();
    void CmPrinterSetup();
    void CmPrinterEnable (TCommandEnabler &tce);
    void CmCloseWindow();

	 /* Internal Methods/Procedures */
	 void SetupWindow();
	 void ReceiveData();
	 void SendData();
	 void SetWindowTitle();
	 bool InitTerminalBuffer(bool nvt=false);
	 bool CloseTerminal();
	 void TimeMessage();
	 void SessionMessage(const int errorId);
    void SessionMessage(const char* errorMsg);
	 void SetIdleState();
	 void SetConnectingState()        {nLineState = nConnecting;}
	 void SetConnectedState();
    void SetSSLConnectingState()     {nLineState = nSSLConnecting;}
    void SetSSLConnectedState();
    void SetSSLDisconnectingState()  {nLineState = nSSLDisconnecting;}
	 bool CanClose();
	 void SetLogScreenMenu();
	 void SetScrLogOn(bool status){scrLogOn = status;}
	 bool GetScrLogOn() {return scrLogOn;}
    void LineError(int cError);
    void ActivateConnection();
    int  ConnectSecurity();
    void DisconnectedByHost();
    void ReceiveDataEnqueued(bool urgent=false);

    void SetHostInfo();
    void SetHostStatus();
    bool PrintBuffer(int luType=LU3);
    int  LU1DataFromNetwork(LPALFFBUF pNetInBuf);
#define  HORZFMT    0
#define  VERTFMT    1
#define  BOTHFMT    2
    void ResetPrinterFormats(const int fmt);
    int  ProcessSCSData(unsigned char** streamLU1, int& streamSize);
    int  DataToNetwork(LPALFFBUF pNetOutBuf, int luType, const char* queryReplyList);
    int  SendQueryReply(char** netOutBuf, int* netCount, const int netOutMax,
                                     const int luType, const int qCode);
    int  GetPrinterStatus();
    bool CreatePrinter(char* prtDriver=NULL,
                       char* prtDevice=NULL,
                       char* prtOutput=NULL);
    bool BuildTN3270PrtStatus(LPALFFBUF pNetOutBuf,
                              int status = DEVICE_READY,
                              int errorCode=0);
    unsigned char GetLU1SenseCode(retCode);
    
    int  CloseAndStorePrtField(PrtField& fdStore, char*  text,
								       int currentPage, int currentCol, int currentLine);
    void SavePrinterParms();
    void SetPrtFont(LOGFONT logFont);
    void SetPrinterName();
    void SetPrinterStatus(int printerStatus);
    void SetStatusBar();
    void SetMenuTraceStat();

//{{ALFFRemotePrinterRSP_TBL_BEGIN}}
	 LRESULT DoClusterNotification(WPARAM wParam, LPARAM lParam);
	 LRESULT DoLineNotification(WPARAM, LPARAM lParam);
    LRESULT EvStartTLS(WPARAM, LPARAM);    
	 LRESULT DoNetInputProcess(WPARAM, LPARAM);      // LU2/LU3 Data Stream Formatted Buffer
	 LRESULT DoNetInputLU1Process(WPARAM, LPARAM);   // SNA - SCS Printing data
    LRESULT DoPrinterBeginEndOfJob(WPARAM, LPARAM); // LU1,LU3 Printer Begin/End Of Job
	 LRESULT DoNetOutputProcess(WPARAM, LPARAM);
	 LRESULT DoNetOutputEcho(WPARAM, LPARAM);
  	 LRESULT EvInitTerminal(WPARAM, LPARAM);

	 /* Options menu command handlers */
	 void    CmTraceSession();
	 void    CmToolsLogScreen();
	 void    CmTraceSessionEnable(TCommandEnabler &ce);
	 void    CmSessionEnable(TCommandEnabler &ce);
    void    CmToolsLogScreenEnable(TCommandEnabler &ce);

    /* Security menu command handlers  */
    void    CmShowCertificate();
    void    CmShowCertificateEnable(TCommandEnabler &ce);

    /* Menu Selection Help */
    void EvMenuSelect(uint menuItemId, uint flags, HMENU hMenu);

    /* Status Bar SSL Padlock Draw Item */
    void EvDrawItem(uint ctrlId, DRAWITEMSTRUCT far& drawInfo);

    /* Mouse Clicked on Status Bar */
    bool EvSetCursor(HWND hWndCursor, uint hitTest, uint mouseMsg);

    //void EvPrinterClicked(WPARAM code);

//{{ALFFRemotePrinterRSP_TBL_END}}
DECLARE_RESPONSE_TABLE(ALFFRemotePrinter);
};    //{{ALFFRemotePrinter}}
#endif                                      // __alffnprt_h sentry.
