/***************************************************************************
                          CdboCddaView.cpp  -  description
                             -------------------
    begin                : Sun Feb 3 2002
    copyright            : (C) 2002 by Alex Podolsky
    email                : alexpod@users.sourceforge.net
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/
#include <qframe.h>
#include <qheader.h>
#include <qlayout.h>
#include <qvariant.h>
#include <qtooltip.h>
#include <qwhatsthis.h>
#include <qpopupmenu.h>
#include <qlistview.h>
#include <qregexp.h>

#include <klistview.h>
#include <kconfig.h>
#include <klocale.h>
#include <kiconloader.h>

#include "CdboCddaView.h"
#include "CdboCddaView.moc"
#include "CdboPlayerPnl.h"

#include "CdboAudioActionFactory.h"
#include "../dialogs/CdboDialogBase.h"

CdboCddaView::CdboCddaView( QWidget* parent,  const char* name, WFlags fl )
    : QFrame( parent, name, fl ){
	layout = new QVBoxLayout( this );
    layout->setSpacing( 0 );
    layout->setMargin( 0 );

    trackView = new KListView( this, "trackView" );
    trackView->addColumn( i18n( "Track" ) );
    trackView->header()->setClickEnabled( FALSE, trackView->header()->count() - 1 );
    trackView->addColumn( i18n( "Length" ) );
    trackView->header()->setClickEnabled( FALSE, trackView->header()->count() - 1 );
    trackView->addColumn( i18n( "Title" ) );
    trackView->header()->setClickEnabled( FALSE, trackView->header()->count() - 1 );

	trackView->setColumnWidth(0,60);
	trackView->setColumnWidth(2,340);
	trackView->setColumnWidth(1,60);
    	trackView->setColumnAlignment(1, AlignHCenter);
    	trackView->setFullWidth(true);

	layout->addWidget( trackView );

	player = new CdboPlayerPnl( this, "player" );
	player->setMinimumSize( QSize( 0, 27 ) );

	layout->addWidget( player );

	factory = new CdboAudioActionFactory(this, "audioFactory");
	player->setFactory(factory);
	user = 0;
	renameable = false;
	
	config = new KConfig("cdbakeovenrc");
	config->setGroup("Default Settings");	
	showPlayer = config->readBoolEntry(QString("Show Player In ") + name, true);
	
	popupMenu = new QPopupMenu();
	checkIndex = popupMenu -> insertItem( i18n("Check All Tracks"),   this ,SLOT(slotCheckAll()));
	uncheckIndex = popupMenu -> insertItem(i18n("Uncheck All Tracks"), this ,SLOT(slotUncheckAll()));
	popupMenu -> insertSeparator();
	
	//popupMenu -> insertItem(BarIcon("reload"), i18n("Rescan Tracks"), this ,SLOT(reload()));
	showPlayerIndex = popupMenu -> insertItem( i18n("Show Player"),   this ,SLOT(slotShowPlayer()));
	popupMenu->setItemChecked(showPlayerIndex, showPlayer);
	connect( trackView, SIGNAL(rightButtonPressed(QListViewItem*, const QPoint&, int)),
		this, SLOT(popupMenuSlot(QListViewItem*, const QPoint&, int)));
	
	connect(  trackView, SIGNAL(doubleClicked(QListViewItem *)),
						this, SLOT(slotSelectFile(QListViewItem *)));
						
	connect( factory, SIGNAL( countedTracks(int) ), this, SLOT( setTotal(int) ) );
}
//============================================================
CdboCddaView::~CdboCddaView(){
	delete config;
	delete factory;
}
//---------------------------------------------------------------------------------------------
void CdboCddaView::readOptions(){
	config->setGroup("Default Settings");	
	if(!config->readBoolEntry(QString("Show Player In ") + this->name(), true))
		player->hide();
	else
		player->show();
}
//---------------------------------------------------------------------------------------------
void CdboCddaView::saveOptions(){
}
//============================================================
void CdboCddaView::loadAndUncheck(QString device){
	if(user){
		thisDevice = device;
		trackView->clear();
		factory->scanAudioDisk(device, trackView, user, true);
	}
}
//============================================================
void CdboCddaView::load(QString device){
	thisDevice = device;


	trackView->clear();
	if(user){
		factory->scanAudioDisk(device, trackView, user);
	}
	else
		factory->scanAudioDisk(device, trackView);
}
//============================================================
void CdboCddaView::reload(){
	load(thisDevice);
}
//============================================================
void CdboCddaView::setTotal(int total){
	if(total < 0)
		return;
	player->setDevice(thisDevice, total);
}
//============================================================
void CdboCddaView::stop(){
	player->stop();
}
//---------------------------------------------------------------------------------
void CdboCddaView::popupMenuSlot(QListViewItem* , const QPoint& point, int){
	if(trackView->childCount() == 0){
		popupMenu->setItemEnabled(checkIndex, false);
		popupMenu->setItemEnabled(uncheckIndex, false);
	}
	else{
		popupMenu->setItemEnabled(checkIndex, true);
		popupMenu->setItemEnabled(uncheckIndex, true);
	}
	popupMenu->popup(point);
}
//---------------------------------------------------------------------------------
void CdboCddaView::slotShowPlayer(){
	doShowPlayer(!popupMenu->isItemChecked(showPlayerIndex));
}
//=======================================================
void CdboCddaView::doShowPlayer(bool doShow){
	showPlayer = doShow;
	if(!doShow)
		player->hide();
	else
		player->show();
	popupMenu->setItemChecked(showPlayerIndex, showPlayer);
	config->setGroup("Default Settings");
	config->writeEntry(QString("Show Player In ") + name(), showPlayer);
}
//---------------------------------------------------------------------------------
void CdboCddaView::slotCheckAll(){
	if(trackView->childCount() == 0)
		return;
	for(QCheckListItem *item = (QCheckListItem *)trackView->firstChild(); item; item = (QCheckListItem *)item->nextSibling())
		item->setOn(true);
}
//---------------------------------------------------------------------------------
void CdboCddaView::slotUncheckAll(){
	if(trackView->childCount() == 0)
		return;
	for(QCheckListItem *item = (QCheckListItem *)trackView->firstChild(); item; item = (QCheckListItem *)item->nextSibling())
		item->setOn(false);
}
//---------------------------------------------------------------------------------
QStringList* CdboCddaView::getPickedTracks(){
	pickedTracks.clear();
	if(trackView->childCount() == 0)
		return 0;
		
	
	QString buff, buff1;
	for(QCheckListItem *item = (QCheckListItem *)trackView->firstChild(); item;
												item = (QCheckListItem *)item->nextSibling()){
		if(item->isOn()){
			buff = item->text(0);
			//buff = buff.replace(QRegExp("Track"), "");
			buff1 = item->text(2);
			if(!renameable)
				pickedTracks.append(buff.stripWhiteSpace());
			else if(buff1 != "<Unknown>" && buff1.stripWhiteSpace() != "")
				pickedTracks.append(buff1 + buff.stripWhiteSpace());
			else
				pickedTracks.append("track" + buff.stripWhiteSpace());
		}
	}
	return &pickedTracks;
}
//---------------------------------------------------------------------------------
void CdboCddaView::slotSelectFile(QListViewItem *item){
	if(!item)
		return;
	QString buff = item->text(0);
	//buff = buff.replace(QRegExp("Track"), "").stripWhiteSpace();
	player->play(buff.toInt());
}
//---------------------------------------------------------------------------------
void CdboCddaView::setHeaderClickEnabled(bool b){
	for(int i =0; i < trackView->header()->count(); i++)
 		trackView->header()->setClickEnabled( b, i);
}

//---------------------------------------------------------------------------------
void CdboCddaView::showLongTitle(){
	trackView->header()->setLabel( 2, i18n("Base File Name - Title"));
}
//---------------------------------------------------------------------------------
void CdboCddaView::setRenameable(){
	trackView->setItemsRenameable(true);
	trackView->setRenameable(0, false);
	trackView->setRenameable(2, true);
	renameable = true;
}
//---------------------------------------------------------------------------------
void CdboCddaView::invalidate(){
	trackView->clear();
	player->stop();
}
