/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "Be.h"


_IMPEXP_BE BMessenger be_app_messenger;


// constructor
BMessenger::BMessenger () :
fPort (0),
fHandlerToken (0),
fTeam (0),
fPreferredTarget (false),
fHandler (0),
fLooper (0)
{
}


// constructor
BMessenger::BMessenger (const BHandler *handler, const BLooper *looper, status_t *perr) :
fPort (0),
fHandlerToken (0),
fTeam (0),
fPreferredTarget (false)
{
	fHandler = const_cast<BHandler*>(handler);
	fLooper = const_cast<BLooper*>(looper);
	if (perr)
		*perr = B_OK;
}


// destructor
BMessenger::~BMessenger ()
{
}


// operator =
BMessenger& BMessenger::operator = (const BMessenger &from)
{
	fPort = from.fPort;
	fHandlerToken = from.fHandlerToken;
	fTeam = from.fTeam;
	fPreferredTarget = from.fPreferredTarget;
	fHandler = from.fHandler;
	fLooper = from.fLooper;

	return *this;
}


// SendMessage
status_t BMessenger::SendMessage (BMessage *a_message, BHandler *reply_to, bigtime_t timeout) const
{
	if (fLooper)
		fLooper->PostMessage (a_message, fHandler, reply_to);
	else if (fHandler && fHandler->Looper ())
		fHandler->Looper ()->PostMessage (a_message, fHandler, reply_to);
	return fLooper ? B_OK : B_BAD_PORT_ID;
}


BHandler* BMessenger::Target(BLooper **looper) const
{
	if (looper)
		*looper = fLooper;
	return fHandler;
}

