/*
 * mainwidget.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 "mainwidget.h"
#include "filelistview.h"
#include "infowidget.h"
#include "portlistview.h"
#include "searchwidget.h"
#include "dependlistview.h"
#include "headerwidget.h"
#include "splashwidget.h"

#include <kapplication.h>
#include <kdebug.h>
#include <kdialog.h>
#include <klocale.h>
#include <kwin.h>

#include <qlayout.h>
#include <qsplitter.h>
#include <qtabwidget.h>
#include <qvbox.h>

using namespace Barry;

MainWidget::MainWidget( QWidget *parent ): QWidget( parent, "MainWidget" )
{
	QHBoxLayout *layout = new QHBoxLayout( this );
	layout->setSpacing( KDialog::spacingHint() );
	layout->setMargin( KDialog::marginHint() );
	
	QSplitter *splitter = new QSplitter( this );
	layout->addWidget( splitter );

	QVBox *leftLayout = new QVBox( splitter );
	leftLayout->setSpacing( KDialog::spacingHint() );
	leftLayout->setMargin( 0 );
	
	m_listView = new PortListView( leftLayout );
	connect( m_listView, SIGNAL( portSelected( const Port & ) ),
	         this, SLOT( selectPort( const Port & ) ) );

	SearchWidget *searchWidget = new SearchWidget( leftLayout );
	connect( m_listView, SIGNAL( portNameRegistered( const QString & ) ),
	         searchWidget, SLOT( addEntry( const QString & ) ) ); 
	connect( searchWidget, SIGNAL( searchTerm( const QString & ) ),
	         m_listView, SLOT( selectPort( const QString & ) ) );

	m_rightLayout = new QVBox( splitter );
	m_rightLayout->setSpacing( KDialog::spacingHint() );
	m_rightLayout->setMargin( 0 );

	m_splashWidget = new SplashWidget( m_rightLayout );
	connect( m_splashWidget->browserExtension(),
	         SIGNAL( openURLRequest( const KURL &, const KParts::URLArgs & ) ),
	         this,
	         SLOT( openURLRequest( const KURL &, const KParts::URLArgs & ) ) );

	QValueList<int> sizes;
	sizes << 200 << 400;
	splitter->setSizes( sizes );
}

void MainWidget::buildRightSide()
{
	delete m_splashWidget;
	m_splashWidget = 0;

	m_headerWidget = new HeaderWidget( m_rightLayout );
	m_headerWidget->show();

	QFrame *seperator = new QFrame( m_rightLayout );
	seperator->setFrameStyle( QFrame::HLine | QFrame::Sunken );
	seperator->show();
	
	m_tabWidget = new QTabWidget( m_rightLayout, "tabWidget" );
	m_tabWidget->show();
	connect( m_tabWidget, SIGNAL( currentChanged( QWidget * ) ),
	         this, SLOT( tabChanged( QWidget * ) ) );

	m_infoWidget = new InfoWidget( m_tabWidget );
	m_infoWidget->show();
	m_tabWidget->addTab( m_infoWidget, i18n( "General" ) );

	m_fileList = new FileList( m_tabWidget );
	connect( m_fileList, SIGNAL( statusMessage( const QString & ) ),
	         this, SIGNAL( statusMessage( const QString & ) ) );
	connect( m_fileList, SIGNAL( processFinished() ),
	         this, SIGNAL( processFinished() ) );
	m_tabWidget->addTab( m_fileList, i18n( "Files" ) );

	m_buildDeps = new DependancyList( m_tabWidget, DependancyList::BuildDeps );
	m_tabWidget->addTab( m_buildDeps, i18n( "Build dependencies" ) );

	m_runDeps = new DependancyList( m_tabWidget, DependancyList::RunDeps );
	m_tabWidget->addTab( m_runDeps, i18n( "Runtime dependencies" ) );

	m_tabWidget->setCurrentPage( 0 );
}

void MainWidget::reset()
{
	m_listView->populate();
}

void MainWidget::selectPort( const Port &port )
{
	if ( port == m_port )
		return;
	m_port = port;

	// Make sure that the main window is visible
	QWidget *mainWindow = kapp->mainWidget();
	KWin::Info info = KWin::info( mainWindow->winId() );
	KWin::setOnDesktop( mainWindow->winId(), KWin::currentDesktop() );
	mainWindow->move( info.geometry.topLeft() );
	mainWindow->show();

	if ( m_splashWidget )
		buildRightSide();

	m_headerWidget->setPort( m_port );
	m_listView->selectPort( m_port );
	tabChanged( m_tabWidget->currentPage() );

	emit portSelected( port );
}

void MainWidget::tabChanged( QWidget *w )
{
	if ( !w || m_port.name().isNull() )
		return;
	if ( w == m_infoWidget )
		m_infoWidget->setPort( m_port );
	else if ( w == m_fileList )
		m_fileList->setFileList( m_port );
	else if ( w == m_buildDeps )
		m_buildDeps->setPort( m_port );
	else if ( w == m_runDeps )
		m_runDeps->setPort( m_port );
}

void MainWidget::openURLRequest( const KURL &url, const KParts::URLArgs & )
{
	if ( url.protocol() != QString::fromLatin1( "exec" ) )
		return;

	if ( url.path() == QString::fromLatin1( "search" ) )
		emit findDialogRequest();
}

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