//
//  The contents of this file are subject to the Mozilla Public License
//  Version 1.0 (the "License"); you may not use this file except in
//  compliance with the License. You may obtain a copy of the License at
//  http://www.mozilla.org/MPL/
// 
//  Software distributed under the License is distributed on an "AS IS"
//  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
//  the License for the specific language governing rights and limitations
//  under the License.
// 
//  The Original Code is CPhone, a cross platform voip gui.
//
//  The Initial Developer of the Original Code is Derek Smithies.
//
//  Copyright (C) 2002 Indranet Technologies Ltd, 
//                                  http://www.indranet-technologies.com
//  All Rights Reserved.
//
//  Contributor(s): _______________


/*
 *
 *  $Log: callanswerSub.cxx,v $
 *  Revision 1.5  2004/04/23 05:09:59  dereksmithies
 *  Fixes so it works with latest openh323 lib, and not lock X display.
 *
 *  Revision 1.4  2003/08/26 04:48:02  dereksmithies
 *  Fixes to get statistics working right, add icons, add ok,cancel,apply buttons
 *
 *  Revision 1.3  2003/08/11 01:48:14  dereksmithies
 *  Fix windows compile errors
 *
 *  Revision 1.2  2003/04/04 04:37:49  dereksmithies
 *  Major upgrade.
 *  Ixj & microtelco support added. Fix threading issues.
 *
 *  Revision 1.1.1.1  2002/05/12 22:55:05  dereksmithies
 *  Initial release.
 *
 *
 *
 *
 */


#include <ptlib.h>

#include <qpushbutton.h>
#include <qlabel.h>
#include <qtimer.h>
#include <qevent.h>
#include <qapplication.h>

#include "mainwindowSub.h"
#include "callanswerSub.h"
#define CUSTOM_EVENT_SETUP (QEvent::User + 1)

CallAnswer::CallAnswer(QString remote)
  : FormCallAnswer( 0, "Call Answer", FALSE, WStyle_NoBorderEx),
    remoteNode(remote)
{
  move(10, 10);

  CPhone::Current().SetWindowId(eCallAnswer, winId());

  QApplication::postEvent(this, new QCustomEvent(CUSTOM_EVENT_SETUP));
}

void CallAnswer::Setup()
{
  TextLabelCallFrom->setText("Call from " + remoteNode);

  move(600,60);

  qApp->beep();    //Alert the user. We need a response.

  startTimer(1000 * 60);//60 second timeout on user response.
}

/*  
 *  Destroys the object and frees any allocated resources
 */
CallAnswer::~CallAnswer()
{
}

void CallAnswer::AcceptIncomingCall_slot()
{
  if (CPhone::Current().ForgetExists(eCallAnswer, winId()))
    AdviseParent(CALL_ANSWER_RESPONSE_YES);

  close (TRUE);
}

void CallAnswer::RejectIncomingCall_slot()
{
  if (CPhone::Current().ForgetExists(eCallAnswer, winId()))
    AdviseParent(CALL_ANSWER_RESPONSE_NO);

  close (TRUE);
}

void CallAnswer::closeEvent(QCloseEvent *e)
{
  if (CPhone::Current().ForgetExists(eCallAnswer, winId()))
    AdviseParent(CALL_ANSWER_RESPONSE_TMO);

  e->accept();
}

void CallAnswer::timerEvent(QTimerEvent *)
{
  if (CPhone::Current().ForgetExists(eCallAnswer, winId())) {
    AdviseParent(CALL_ANSWER_RESPONSE_TMO);
    CPhone::GetUi().DisplayMessage("Ignoring incoming call from " + remoteNode);
  }

   close(TRUE);   //TRUE means delete this class.
}


void CallAnswer::AdviseParent(unsigned action)
{
  QCustomEvent *ce = new QCustomEvent(action);

  QApplication::postEvent(&CPhone::Current().GetUi(), ce);
}

void CallAnswer::customEvent( QCustomEvent *e)
{
  if (e->type()  == CUSTOM_EVENT_DIE) 
   close(TRUE);

 if (e->type()  == CUSTOM_EVENT_SETUP) 
    Setup();
}


syntax highlighted by Code2HTML, v. 0.9.1