/***************************************************************************
 *   Copyright (C) 2005 by Diego 'Flameeyes' Pettenò   *
 *   flameeyes@gentoo.org   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library 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 Library 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 "atmbox.h"

#include <kprocess.h>
#include <kdebug.h>

ATMbox::ATMbox()
{
	m_host = QString::null;
	m_name = QString::null;
	m_hwaddr = QString::null;
}

ATMbox::ATMbox(Settings sets)
{
	sets.readConfig();
	
	m_host = sets.host();
	m_name = sets.name();
	m_hwaddr = sets.hardwareAddress();
}

ATMbox::ATMbox(const ATMbox &other) :
	m_name(other.m_name), m_host(other.m_host), m_hwaddr(other.m_hwaddr)
{
}

ATMbox::~ATMbox()
{
}

bool ATMbox::isUp()
{
	if ( m_host.isNull() || m_name.isNull() ) return false;
	
	kdDebug(210000) << "ping -c1 " << m_host << endl;
	KProcess *proc = new KProcess(0, "ping");
	*proc << "ping" << "-c1" << m_host;
	
	proc->start(KProcess::Block);
	
	bool isup = proc->normalExit() && proc->exitStatus() == 0;
	
	delete proc;
	
	return isup;
}

bool ATMbox::wakeup()
{
	if ( m_host.isNull() || m_name.isNull() || m_hwaddr.isNull() ) return false;

	kdDebug(210000) << "wol " << m_hwaddr << endl;
	KProcess *proc = new KProcess(0, "wol");
	*proc << "wol" << m_hwaddr;
	
	proc->start(KProcess::Block);
	
	bool woke = proc->normalExit() && proc->exitStatus() == 0;

	delete proc;

	return woke;
}
