/*
 * configdialog.cc
 *
 * Copyright (c) 2002, 2003 Frerich Raabe <raabe@kde.org>
 *
 * 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. For licensing and distribution details, check the
 * accompanying file 'COPYING'.
 */
#include "configdialog.h"
#include "portmgr.h"

#include <kapplication.h>
#include <kcombobox.h>
#include <kdebug.h>
#include <kfiledialog.h>
#include <klineedit.h>
#include <klistview.h>
#include <klocale.h>
#include <kurlrequester.h>

#include <qlabel.h>
#include <qlayout.h>
#include <qspinbox.h>

using namespace Barry;

ConfigDialog::ConfigDialog( QWidget *parent )
	: KDialogBase( Tabbed, i18n( "Preferences" ), Ok | Cancel, Ok,
	               parent, "ConfigDialog" )
{
	setupGeneralPage();
	setupOrphanedFilesPage();

	loadConfig();
}

void ConfigDialog::setupGeneralPage()
{
	QFrame *page = addPage( i18n( "General" ) );
	QGridLayout *layout = new QGridLayout( page );
	layout->setSpacing( KDialog::spacingHint() );
	layout->setMargin( 0 );

	QLabel *lSupfile = new QLabel( i18n( "CV&Sup supfile" ), page );
	layout->addWidget( lSupfile, 0, 0 );
	m_supfile = new KURLRequester( page, "m_supfile" );
	m_supfile->setMode( KFile::File | KFile::ExistingOnly | KFile::LocalOnly );
	lSupfile->setBuddy( m_supfile );
	layout->addWidget( m_supfile, 0, 1 );

	QLabel *lPortDir = new QLabel( i18n( "P&ort directory" ), page );
	layout->addWidget( lPortDir, 1, 0 );
	m_portDir = new KURLRequester( page, "m_portDir" );
	m_portDir->setMode( KFile::Directory | KFile::ExistingOnly | KFile::LocalOnly );
	lPortDir->setBuddy( m_portDir );
	layout->addWidget( m_portDir, 1, 1 );
	
	QLabel *lPkgDir = new QLabel( i18n( "P&ackage directory" ), page );
	layout->addWidget( lPkgDir, 2, 0 );
	m_pkgDir = new KURLRequester( page, "m_pkgDir" );
	m_pkgDir->setMode( KFile::Directory | KFile::ExistingOnly | KFile::LocalOnly );
	lPkgDir->setBuddy( m_pkgDir );
	layout->addWidget( m_pkgDir, 2, 1 );

	QLabel *lCvswebMirror = new QLabel( i18n( "CVSweb &mirror" ), page );
	layout->addWidget( lCvswebMirror, 3, 0 );
	m_cvswebMirror = new KComboBox( true, page );
	m_cvswebMirror->setEditable( false );
	lCvswebMirror->setBuddy( m_cvswebMirror );
	layout->addWidget( m_cvswebMirror, 3, 1 );

	QStringList cvswebMirrors;
	cvswebMirrors << QString::fromLatin1( "http://www.FreeBSD.org/cgi/cvsweb.cgi" )
	              << QString::fromLatin1( "http://www.de.FreeBSD.org/cgi/cvsweb.cgi" )
	              << QString::fromLatin1( "http://www.es.FreeBSD.org/cgi/cvsweb.cgi" )
	              << QString::fromLatin1( "http://www.jp.FreeBSD.org/cgi/cvsweb.cgi" )
	              << QString::fromLatin1( "http://cvsup.pt.FreeBSD.org/cgi-bin/cvsweb/cvsweb.cgi" )
	              << QString::fromLatin1( "http://cvsweb.sm.FreeBSD.org" );
	m_cvswebMirror->insertStringList( cvswebMirrors );

	QLabel *lHistoryEntries = new QLabel( i18n( "&History entries" ), page );
	layout->addWidget( lHistoryEntries, 4, 0 );
	m_historyEntries = new QSpinBox( page, "m_historyEntries" );
	lHistoryEntries->setBuddy( m_historyEntries );
	layout->addWidget( m_historyEntries, 4, 1 );
}

void ConfigDialog::setupOrphanedFilesPage()
{
	QFrame *page = addPage( i18n( "Orphaned files" ) );
	QGridLayout *layout = new QGridLayout( page, 2, 2,
	                                       KDialog::spacingHint(),
	                                       0 );
	m_ignoredFiles = new KListView( page, "m_ignoredFiles" );
	m_ignoredFiles->addColumn( i18n( "Filename" ) );
	m_ignoredFiles->setAllColumnsShowFocus( true );
	m_ignoredFiles->setShowSortIndicator( true );
	m_ignoredFiles->setFullWidth( true );
	connect( m_ignoredFiles, SIGNAL( selectionChanged( QListViewItem * ) ),
	         this, SLOT( selectionChanged( QListViewItem * ) ) );
	layout->addMultiCellWidget( m_ignoredFiles, 0, 0, 0, 1 );

	m_addFile = new QPushButton( i18n( "&Add..." ), page );
	connect( m_addFile, SIGNAL( clicked() ), this, SLOT( addFile() ) );
	layout->addWidget( m_addFile, 1, 0 );

	m_removeFile = new QPushButton( i18n( "&Remove" ), page );
	connect( m_removeFile, SIGNAL( clicked() ), this, SLOT( removeFile() ) );
	layout->addWidget( m_removeFile, 1, 1 );
}

void ConfigDialog::slotOk()
{
	saveConfig();
	accept();
}

void ConfigDialog::loadConfig()
{
	KConfig *cfg = kapp->config();
	cfg->setGroup( QString::fromLatin1( "General" ) );

	m_supfile->lineEdit()->setText( cfg->readEntry( QString::fromLatin1( "CVSup supfile" ) ) );
	m_portDir->lineEdit()->setText( cfg->readEntry( QString::fromLatin1( "Port directory" ), PortMgr::self().portRootDir() ) );
	m_pkgDir->lineEdit()->setText( cfg->readEntry( QString::fromLatin1( "Package directory" ), PortMgr::self().pkgRootDir() ) );
	QString cvswebMirror = cfg->readEntry( QString::fromLatin1( "CVSweb mirror" ), m_cvswebMirror->text( 0 ) );
	for ( int i = 0; i < m_cvswebMirror->count(); ++i ) {
		if ( m_cvswebMirror->text( i ) == cvswebMirror ) {
			m_cvswebMirror->setCurrentItem( i );
			break;
		}
	}
	m_historyEntries->setValue( cfg->readUnsignedNumEntry( QString::fromLatin1( "History entries" ), 50 ) );

	cfg->setGroup( QString::fromLatin1( "Orphaned files" ) );
	const QStringList ignoredFiles = cfg->readListEntry( QString::fromLatin1( "Ignored files" ) );
	QStringList::ConstIterator it = ignoredFiles.begin();
	QStringList::ConstIterator end = ignoredFiles.end();
	for ( ; it != end; ++it )
		new KListViewItem( m_ignoredFiles, *it );
	m_removeFile->setEnabled( !ignoredFiles.empty() );
	if ( !ignoredFiles.empty() )
		m_ignoredFiles->setSelected( m_ignoredFiles->firstChild(), true );
}

void ConfigDialog::saveConfig()
{
	KConfig *cfg = kapp->config();
	cfg->setGroup( QString::fromLatin1( "General" ) );
	cfg->writeEntry( "CVSup supfile", m_supfile->lineEdit()->text() );
	cfg->writeEntry( "Port directory", m_portDir->lineEdit()->text() );
	cfg->writeEntry( "Package directory", m_pkgDir->lineEdit()->text() );
	cfg->writeEntry( "CVSweb mirror", m_cvswebMirror->currentText() );
	cfg->writeEntry( "History entries", m_historyEntries->value() );

	cfg->setGroup( QString::fromLatin1( "Orphaned files" ) );
	QStringList ignoredFiles;
	for ( QListViewItem *item = m_ignoredFiles->firstChild(); item; item = item->nextSibling() )
		ignoredFiles += item->text( 0 );
	cfg->writeEntry( "Ignored files", ignoredFiles );

	cfg->sync();
}

void ConfigDialog::addFile()
{
	KFileDialog dlg( QString::null, QString::null, this, "dlg", false );
	dlg.setMode( KFile::Files | KFile::Directory |
	             KFile::ExistingOnly | KFile::LocalOnly );
	dlg.exec();

	const QStringList files = dlg.selectedFiles();
	QStringList::ConstIterator it = files.begin();
	QStringList::ConstIterator end = files.end();
	for ( ; it != end; ++it )
		new KListViewItem( m_ignoredFiles, *it );
}

void ConfigDialog::removeFile()
{
	delete m_ignoredFiles->selectedItem();
	selectionChanged( m_ignoredFiles->selectedItem() );
}

void ConfigDialog::selectionChanged( QListViewItem *item )
{
	m_removeFile->setEnabled( item != 0 );
}

#include "configdialog.moc"
// vim:ts=4:sw=4:noet:list
