/*
* imapcfg.cpp -- Implementation of class KImapCfg.
* Author:	Kurt Granroth (granroth@kde.org)
* Version:	$Id: imapcfg.cpp,v 1.1 1999/07/22 18:16:55 tibirna Exp $
*/

#include <assert.h>
#include <stdlib.h> 

#include <qlabel.h>
#include <qlined.h>
#include <qcheckbox.h>
#include <qgroupbox.h>
#include <qlayout.h>

#include <kglobal.h>
#include <klocale.h>

//#include "typolayout.h"

#include "imap.h"
#include "imapcfg.h"

KImapCfg::KImapCfg( KImap4Drop *drop )
	: KMonitorCfg( drop ),
	_serverEdit( 0 ),
	_portEdit( 0 ),
	_mailboxEdit( 0 ),
	_userEdit( 0 ),
	_pwdEdit( 0 )
{
}

const QString KImapCfg::name() const
{
	return i18n("IMAP 4");
}

QWidget *KImapCfg::makeWidget( QWidget *parent )
{
	KImap4Drop *d = (KImap4Drop *) drop();
	assert( d != 0 );

	QWidget *dlg = new QWidget( parent );
	QBoxLayout *layout = new QVBoxLayout( dlg, 10 );
	layout->addSpacing(10);

	QGroupBox *aGroup = new QGroupBox( i18n("Server"), dlg);
	QGridLayout *slay = new QGridLayout( aGroup, 3, 3 );
	slay->addRowSpacing(0, 10);

	QLabel *aLabel = new QLabel( i18n("Server"), aGroup );
	slay->addWidget(aLabel, 1, 0);

	_serverEdit = new QLineEdit( d->server(), aGroup );
	slay->addWidget(_serverEdit, 1, 1);
	
	QString sport;
	sport.setNum( d->port() );

	aLabel = new QLabel( i18n("Port"), aGroup );
	slay->addWidget(aLabel, 2, 0);

	_portEdit = new QLineEdit( sport, aGroup );
	slay->addWidget(_portEdit, 2, 1);

	aGroup = new QGroupBox( i18n( "Identity" ), dlg );
	layout->addWidget(aGroup);

	slay = new QGridLayout(aGroup, 5, 2, 10);
	slay->addRowSpacing(0,10);

	aLabel = new QLabel( i18n("Username"), aGroup);
	slay->addWidget(aLabel, 1, 0);

	_userEdit = new QLineEdit( d->user(), aGroup );
	slay->addWidget(_userEdit, 1, 1);

	aLabel = new QLabel( i18n("Mailbox"), aGroup );
	slay->addWidget(aLabel, 2, 0);

	_mailboxEdit = new QLineEdit( d->mailbox(), aGroup );
	slay->addWidget(_mailboxEdit, 2, 1);

	aLabel = new QLabel( i18n("Password"), aGroup );
	slay->addWidget(aLabel, 3, 0);

	_pwdEdit = new QLineEdit( d->password(), aGroup );
	slay->addWidget(_pwdEdit, 3, 1);

	_pwdEdit->setEchoMode( QLineEdit::Password );

	_savePass = new QCheckBox( i18n( "Save Password"), aGroup );
	slay->addWidget(_savePass, 4, 1);

	connect( _savePass, SIGNAL(toggled(bool)), _pwdEdit,
		SLOT(setEnabled(bool)) );

	return dlg;
}

void KImapCfg::updateConfig()
{
	assert( _serverEdit != 0 );
	assert( _portEdit != 0 );
	assert( _mailboxEdit != 0 );
	assert( _userEdit != 0 );
	assert( _pwdEdit != 0 );
	assert( _savePass != 0 );

	KImap4Drop *d = (KImap4Drop *) drop();
	assert( d != 0 );

	d->setImapServer( _serverEdit->text(), atoi( _portEdit->text() ) );
	d->setUser( _userEdit->text(), _pwdEdit->text(), 
			_mailboxEdit->text(), _savePass->isChecked() );
}
