/***************************************************************************
 *   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 "kalvafilemanager.h"
//#include "kalvageneralsettings.h"

#include <klocale.h>
#include <kfilewidget.h>
#include <kfileitem.h>
#include <kconfiggroup.h>
#include <KActionCollection>
#include <kglobal.h>
#include <KDialog>
#include <KDebug>
#include <KUrl>

#include <QtGui>
#include <QAction>
#include <QString>
#include <QDir>
#include <QUrl>
#include <QGridLayout>

namespace KalvaWidget {

// ==================================
// public
// ==================================

KalvaFileManager::KalvaFileManager ( QWidget *parent ) : QWidget( parent )
{
	createActions();
	createWidgets();
	layoutWidgets();
	
	settingsChanged();

	connect( m_filewidget, SIGNAL( selectionChanged() ),
           SLOT( selectionChanged() ) );
	connect( m_filewidget, SIGNAL( fileHighlighted( const QString& )),
	   SLOT( fileHighlighted( const QString &)));
	connect( m_filewidget, SIGNAL( urlEntered( const KUrl& )),
           SLOT( fileHighlighted (const KUrl& )));
}

KalvaFileManager::~KalvaFileManager()
{
	delete m_filewidget;
}

// ==================================
// public slots
// ==================================

void KalvaFileManager::settingsChanged()
{
// 	kDebug() << "Settings::collection_root() = " << Settings::collection_root();
//  	m_filewidget->setUrl( Settings::collection_root() );
}

void KalvaFileManager::setUrl(const QUrl &url)
{
 	kDebug() << "setUrl";
  	m_filewidget->setUrl( url );
}

// ==================================
// private
// ==================================

void KalvaFileManager::createWidgets()
{
// 	KUrl defaultUrl = Settings::collection_root();
// 	if (defaultUrl == 0)
// 	{
// 		defaultUrl = KUrl( QDir::homePath() );
// 	}
// 	m_filewidget = new KFileWidget( defaultUrl, this );
	m_filewidget = new KFileWidget( KUrl( QDir::homePath() ), this );
	m_filewidget->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
        QAction* previewAction = m_filewidget->actionCollection()->action( "preview" );
        if ( previewAction != 0 )
	{
	   m_filewidget->actionCollection()->removeAction( previewAction );
        }
        QAction* toggleBookmarksAction = m_filewidget->actionCollection()->action( "toggleBookmarks" );
        if ( toggleBookmarksAction != 0 )
	{
	   toggleBookmarksAction->setChecked( true );
        }
}

void KalvaFileManager::layoutWidgets()
{
	QGridLayout* grid  = new QGridLayout ( this );
	grid->addWidget(m_filewidget);
	grid->setMargin(KDialog::spacingHint());
	grid->setSpacing(KDialog::spacingHint());
}

void KalvaFileManager::createActions()
{
}

// ==================================
// private slots
// ==================================

void KalvaFileManager::selectionChanged()
{
	kDebug() << "selectionChanged()";
	emit urlSelected( QUrl(m_filewidget->selectedFiles()[0]) );
}

void KalvaFileManager::fileHighlighted( const QString & url )
{
	kDebug() << "fileHighlighted(" << url << ")";
	emit urlSelected( QUrl(url) );
}

void KalvaFileManager::fileHighlighted( const KUrl & url )
{
	kDebug() << "fileHighlighted(" << url << ")";
	emit urlSelected( url );
}

}

#include "kalvafilemanager.moc"
