/*
    knetd, KDE Network Daemon

    Copyright (c) 2005 by Engin AYDOGAN		<engin @ bzzzt.biz>

    *************************************************************************
    *                                                                       *
    * 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.                                   *
    *                                                                       *
    *************************************************************************
*/

#include <kparts/genericfactory.h>
#include <kconfig.h>
#include <kapplication.h>
#include <dcopclient.h>
#include <kactionselector.h>
#include <keditlistbox.h>
#include <kpushbutton.h>
#include <kuser.h>

#include <qlayout.h>
#include <qlineedit.h>
#include <qlabel.h>
#include <qlistbox.h>
#include <qcombobox.h>
#include <qtabwidget.h>

#include "config.h"
#include "configwidget.h"

typedef KGenericFactory<Config, QWidget> ConfigFactory;
K_EXPORT_COMPONENT_FACTORY( kcm_knetd, ConfigFactory("kcm_knetd" ) )

class Config::Private
{
public:
	ConfigDialog* dialog;
	KInstance* instance;
	KPushButton* scanButton;
};

/**
 * TODO: Define a primative scripting language for all possible set-uid-root wrapper
 *	so that users can suit their needs and load working schemes without hassle
 */
Config::Config( QWidget* p, const char* n, const QStringList& l )
 : KCModule( p, n, l ), d( new Private )
{
	QVBoxLayout* vb = new QVBoxLayout( this );
	d->dialog = new ConfigDialog( this );
	vb->addWidget( d->dialog );
	d->instance = new KInstance( "knetd" );

	// Add scan button to the access point order selection widget
	QGridLayout* grid = static_cast<QGridLayout *>( d->dialog->accpointsEL->layout() );
	d->scanButton = new KPushButton( i18n( "&Scan" ), d->dialog->accpointsEL );
	d->scanButton->show();
	grid->addWidget( d->scanButton, 6, 1 );

	// see if kded is running
	QCString replyType;
	QByteArray replyData, arg;
	QValueList<QCString> loadedModules;
	if( !KUser().isSuperUser() )
	{
		d->dialog->accessInfoTL->setText( i18n( "You <font color=\"red\"><b>don't have</b></font> "
						  "super-user privileges. You "
						  "<font color=\"green\">can</font> still change your own preferences"
						  " but you <font color=\"red\"><b>can't</b></font>"
						  " change set-uid options." ) );
		d->dialog->paths->setEnabled( false );
		d->dialog->security->setEnabled( false );
	}
	else
	{
		d->dialog->accessInfoTL->hide();
		d->dialog->paths->setEnabled( true );
		d->dialog->security->setEnabled( true );
	}

	if ( KApplication::dcopClient()->call( "kded", "kded", "loadedModules()", arg, replyType, replyData ) )
	{
		QDataStream reply( replyData,  IO_ReadOnly );
		reply >> loadedModules;
		if( loadedModules.contains( "knetd" ) != 0 )
		{
			QStringList devices;
			if( KApplication::dcopClient()->call( "kded", "knetd", "devices()", arg, replyType, replyData ) )
			{
				QDataStream replyd( replyData,  IO_ReadOnly );
				replyd >> devices;
				d->dialog->ansList->availableListBox()->insertStringList( devices );
				d->dialog->devicesDeviceCB->insertStringList( devices );
				d->dialog->kdedInfoTL->hide();
			}
			else
			{
				d->dialog->kdedInfoTL->setText( i18n( "<font color=\"#ff0000\">Could not fetch devices list.</font>" ) );
			}
		}
		else
		{
			d->dialog->kdedInfoTL->setText( i18n( "<font color=\"#ff0000\">KNetD is not loaded.</font>" ) );
		}
	}
	else
	{
		d->dialog->kdedInfoTL->setText( i18n( "<font color=\"#ff0000\">kded is not running.</font>" ) );
	}

	load();
}

Config::~Config()
{
}

void
Config::load()
{

	KConfig* c = d->instance->config();
	c->setGroup( "Daemon" );
}

void
Config::save()
{
	KConfig* c = d->instance->config();
	c->setGroup( "Daemon" );
	c->sync();
}

void
Config::defaults()
{
}

void
Config::configChanged( const QString& )
{
	changed( true );
}

#include "config.moc"
