//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "GTSIPPHONELib_OCX"
#pragma resource "*.dfm"
TForm1 *Form1;

bool PhoneInited;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
        PhoneInited = false;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
        //It looks like for BCB6, when this is called
        //The window form is not initialzied
        //the The Wind handle is NULL
        /*
        AnsiString s = "Windows handle ";
        if(this->WindowHandle == NULL)
                s += "is null";
        else
                s += "is not null";

         MessageBox(this->WindowHandle, s.c_str(), "", MB_OK);
         */
         //Moved all initialization code to FormShow as
         //here the ActiveX control is not created

}
//---------------------------------------------------------------------------

void __fastcall TForm1::GTSIPPhone1PhoneReady(TObject *Sender, long ChanID)
{
            btnDial->Enabled = true;
            btnHangup->Enabled = false;
            Edit1->Enabled = true;
            Edit1->Text = "8888@pcbest.net";
            RichEdit1->Enabled = true;
            RichEdit1->Text = "LINE IDLE";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::GTSIPPhone1CallConnected(TObject *Sender,
      long ChanID)
{
            RichEdit1->Text = "Call Connected!";
            btnDial->Enabled = false;
            btnHangup->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::GTSIPPhone1CallDialing(TObject *Sender,
      long ChanID, BSTR sCaller, BSTR sCallee)
{
            RichEdit1->Text = "Dialing out...!";
            btnDial->Enabled = false;
            btnHangup->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::GTSIPPhone1CallIdle(TObject *Sender, long ChanID)
{
            RichEdit1->Text = "LINE IDLE";
            btnDial->Enabled = true;
            btnDial->Caption = "Dial";
            btnHangup->Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::GTSIPPhone1CallOffered(TObject *Sender,
      long ChanID, BSTR Caller, BSTR Callee, BSTR DestAddr, BSTR ViaAddr,
      BSTR SrcIPAddr, short SrcIPPort)
{
            RichEdit1->Text = "A new call coming in! caller:";
            RichEdit1->Text += Caller;
            btnDial->Caption = "Answer";
            btnDial->Enabled = true;
            btnHangup->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::GTSIPPhone1CallRinging(TObject *Sender,
      long ChanID)
{
            RichEdit1->Text = "Dialing out... remote is ringing!";
            btnDial->Enabled = false;
            btnHangup->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::GTSIPPhone1SIPRegStatus(TObject *Sender,
      long UserID, long Status, long Expires)
{
            if (Status == 1)
            {
                RichEdit1->Text = "SIP account is registered!";
            }
            else
            {
                RichEdit1->Text = "SIP account is not registered, or there is no SIP account!";
            }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnDialClick(TObject *Sender)
{
            if (btnDial->Caption == "Answer")
            {
                //in answering call case
                GTSIPPhone1->AnswerCall(0);
            }
            else
            {
                AnsiString s = "<sip:";
                s += Edit1->Text;
                s += ">";
                GTSIPPhone1->MakeCall(0, WideString(s), WideString(""));
            }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnHangupClick(TObject *Sender)
{
           GTSIPPhone1->HungupCall(0);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
        if(PhoneInited)
             GTSIPPhone1->Stop();

        PhoneInited = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow(TObject *Sender)
{
        /*
        AnsiString s = "Windows handle ";
        if(this->WindowHandle == NULL)
                s += "is null";
        else
                s += "is not null";

         MessageBox(this->WindowHandle, s.c_str(), "", MB_OK);
        */

        if(PhoneInited) return;

            GTSIPPhone1->SetZone(WideString("AnyZoneName")); //Set Any Name you like

            //for the case that you want the softphone only dials to one fixed number, or address
            //GTSIPPhone1->SetConfigValue("gtphone.preset.phonenum", "8888@pcbest.net");

            //the following configs are for SIP account
            //GTSIPPhone1->SetConfigValue("gtsrv.sip.reg.client.num", "1"); //one SIP account
            //GTSIPPhone1->SetConfigValue("gtsrv.sip.reg1.displayname", "AnyName"); //The display name of first account. Sample: "Bob Wall"
            //GTSIPPhone1->SetConfigValue("gtsrv.sip.reg1.username", "1800"); //The user name of first account. Sample: "12345678"
            //GTSIPPhone1->SetConfigValue("gtsrv.sip.reg1.domain", "sipaccount.com"); //The domain of first account. Sample: "pcbest.net"
            //GTSIPPhone1->SetConfigValue("gtsrv.sip.reg1.proxy", "sipaccount.com"); //The proxy of first account. It is same as domain usually. Sample: "pcbest.net"
            //GTSIPPhone1->SetConfigValue("gtsrv.sip.reg1.authorization", "1800"); //The authorization code of first account. It is same as username usually.
            //GTSIPPhone1->SetConfigValue("gtsrv.sip.reg1.password", "xxxxx"); //password for the sip account

            GTSIPPhone1->SetConfigValue(WideString("gtsrv.sip.ip.port"), WideString("7022")); //default it is 5060
            GTSIPPhone1->SetConfigValue(WideString("gtsrv.sip.rtpstartrange"), WideString("12020")); //RTP start port

            //axGTSIPPhone1.SetConfigValue "gtsrv.sip.stun.server", ""); //this is used for sip phone to go out of NAT
            GTSIPPhone1->SetConfigValue(WideString("gtsrv.sip.prefered.codec"), WideString("0,8,3")); //Ulaw, Alaw, and GSM

            //GGTSIPPhone1->SetConfigValue("gtsrv.sip.callcontrol.auto.answercall", "1"); //will auto answer incoming call if set

            GTSIPPhone1->Start();

            btnDial->Enabled = false;
            btnHangup->Enabled = false;
            Edit1->Enabled = false;
            Edit1->Text = "8888@pcbest.net";
            RichEdit1->Enabled = false;

            PhoneInited = true;
}
//---------------------------------------------------------------------------

