/***************************************************************************
 *   Copyright (C) 2008 by Andreas Silberstorff   *
 *   taki@Rivendell   *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   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 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 "collectionperspective.h"
#include "collectionsettingspage.h"

#include "settings.h"

#include <klocale.h>
#include <kglobal.h>
#include <KDialog>
#include <KConfigDialog>
#include <KDebug>

#include <QtGui>

namespace CollectionPerspective
{

Plugin::Plugin()
{
}

Plugin::~Plugin()
{
	delete m_filemanager;
	delete m_videoplayer;
	delete m_fileindexer;
	delete m_kalvaGeneralSettings;
}

QString Plugin::title()
{
	return i18n( "Collection" );
}

QString Plugin::whatsThis()
{
	return i18n( "The Collection perspective provides a file manager for browsing and managing Your video collection and a video player." );
}

QString Plugin::toolTip()
{
	return i18n( "The Collection perspective provides a file manager for browsing and managing Your video collection and a video player." );
}

QWidget* Plugin::createManager( QWidget *parent )
{
	m_filemanager = new KalvaWidget::KalvaFileManager( parent );
	QGridLayout* grid  = new QGridLayout( parent );
	grid->addWidget( m_filemanager );
	grid->setMargin( KDialog::spacingHint() );
	grid->setSpacing( KDialog::spacingHint() );
	return m_filemanager;
}

QWidget* Plugin::createMainView( QWidget *parent )
{
        QTabWidget* tabWidget = new QTabWidget( parent );
	createVideoPlayer( tabWidget );
	tabWidget->addTab( m_videoplayer, i18n("Player") );
	createFileIndexer( tabWidget );
	tabWidget->addTab( m_fileindexer, i18n("Video data") );

	QGridLayout* grid  = new QGridLayout( parent );
	grid->addWidget( tabWidget );
	grid->setMargin(KDialog::spacingHint());
	grid->setSpacing(KDialog::spacingHint());
	return tabWidget;
}

void Plugin::createVideoPlayer(QWidget *parent)
{
	m_videoplayer = new KalvaWidget::KalvaVideoPlayer( parent );

	QGridLayout* grid  = new QGridLayout( parent );
	grid->addWidget( m_videoplayer );
	grid->setMargin(KDialog::spacingHint());
	grid->setSpacing(KDialog::spacingHint());
	connect( m_filemanager, SIGNAL( urlSelected( const QUrl & ) ),
		 this, SLOT( load( const QUrl & ) ) );
	connect( m_videoplayer, SIGNAL( playButtonPressed() ),
		 this, SLOT( reload() ) );
}

void Plugin::load( const QUrl & url)
{
	m_url = url;
//	m_videoplayer->load(url);
}

void Plugin::reload()
{
	m_videoplayer->load(m_url);
	m_videoplayer->play();
}

void Plugin::createFileIndexer(QWidget *parent)
{
	m_fileindexer = new QWidget( parent );

	QGridLayout* grid  = new QGridLayout( parent );
	grid->addWidget( m_fileindexer );
	grid->setMargin(KDialog::spacingHint());
	grid->setSpacing(KDialog::spacingHint());
}

void Plugin::setKalvaGeneralSettings( KalvaData::KalvaGeneralSettings * kalvaGeneralSettings )
{
	kDebug() << "setKalvaGeneralSettings()";
	m_kalvaGeneralSettings = kalvaGeneralSettings;
}

void Plugin::settingsChanged()
{
	kDebug() << "settingsChanged()";
	m_filemanager->settingsChanged();
	m_videoplayer->settingsChanged();
	m_filemanager->setUrl( m_kalvaGeneralSettings->collectionRoot() );
}

void Plugin::addToConfigDialog( KConfigDialog *dialog )
{
	kDebug() << "addToConfigDialog";
	CollectionSettingsPage* page  = new CollectionSettingsPage();
	kDebug() << "CollectionSettingsPage created";
	dialog->addPage( page, Settings::self(), i18n( "Collection" ), "settings" );
	kDebug() << "added CollectionSettingsPage";
 	connect( dialog, SIGNAL( settingsChanged( const QString& ) ), page, SLOT( loadSettings() ) );
}

void Plugin::removeFromConfigDialog( KConfigDialog *dialog )
{
	kDebug() << "removeFromConfigDialog(" << dialog << ")";
	// removal is not allowed by KConfigDialog.
	// Maybe propose to kde-devel or as wishlist bug for KConfigDialog on bugzilla
	//dialog->removePage( "collection_setting" );
}

} //end of namespace
Q_EXPORT_PLUGIN2( kalva_collection_perspective, CollectionPerspective::Plugin )

#include "collectionperspective.moc"
