/***************************************************************************
 *   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 "atmosserver.h"

#include <kurl.h>

ATMOSserver::ATMOSserver()
 : DCOPObject("ATMOSserver"), QObject(0, "ATMOSserver"),
   m_globals("")
{
}

ATMOSserver::~ATMOSserver()
{
}

void ATMOSserver::loadBoxes()
{
	m_globals.readConfig();
	m_boxes.clear();
	
	QStringList boxes = m_globals.boxes();
	for(QStringList::iterator it = boxes.begin(); it != boxes.end(); it++)
		m_boxes.push_back(ATMbox(Settings(*it)));
}

QStringList ATMOSserver::getBoxesNames()
{
	QStringList res;
	for(QValueList<ATMbox>::iterator it = m_boxes.begin(); it != m_boxes.end(); it++)
	{
		if ( ! (*it).getName().isEmpty() )
			res.push_back( (*it).m_name );
	}

	return res;
}

QString ATMOSserver::getBoxHost(const QString &boxname)
{
	for(QValueList<ATMbox>::iterator it = m_boxes.begin(); it != m_boxes.end(); it++)
	{
		if ( (*it).getName() == boxname )
			return (*it).m_host;
	}

	return QString::null;
}

bool ATMOSserver::isBoxUp(const QString &boxname)
{
	for(QValueList<ATMbox>::iterator it = m_boxes.begin(); it != m_boxes.end(); it++)
	{
		if ( (*it).getName() == boxname )
			return (*it).isUp();
	}

	return false;
}

bool ATMOSserver::renameBox(const QString &oldname, const QString &newname)
{
	m_globals.readConfig();
	QStringList boxes = m_globals.boxes();
	if ( boxes.contains(newname) )
		return false; /* Already box with new name */
	if ( ! boxes.contains(oldname) )
		return false; /* No box with old name */
	boxes.remove(oldname); boxes.push_back(newname);

	Settings oldset(oldname); oldset.readConfig();
	Settings newset(newname); newset.readConfig();
	
	newset.setName(newname);
	newset.setHost(oldset.host());
	newset.setHardwareAddress(oldset.hardwareAddress());

	newset.writeConfig();

	m_globals.readConfig();

	m_globals.setBoxes(boxes);
	m_globals.config()->deleteGroup("box_" + oldname);
	m_globals.writeConfig();

	loadBoxes();

	return true;
}

bool ATMOSserver::deleteBox(const QString &boxname)
{
	m_globals.readConfig();
	QStringList boxes = m_globals.boxes();
	if ( ! boxes.contains(boxname) )
		return false; /* No box with old name */
	boxes.remove(boxname);

	m_globals.setBoxes(boxes);
	m_globals.config()->deleteGroup("box_" + boxname);
	m_globals.writeConfig();

	loadBoxes();

	return true;
}

bool ATMOSserver::wakeupBox(const QString &boxname)
{
	for(QValueList<ATMbox>::iterator it = m_boxes.begin(); it != m_boxes.end(); it++)
	{
		if ( (*it).getName() == boxname )
			return (*it).wakeup();
	}

	return false;
}

void ATMOSserver::wakeup(const QString &url)
{
	KURL kurl(url);
	if ( kurl.protocol() != "atmosphere" ) return;

	QString box = kurl.path().remove('/');
	wakeupBox(box);
}
