/***************************************************************************
                          NewAudioCdCompilationView.cpp  -  description
                             -------------------
    begin                : Sun Nov 18 2001
    copyright            : (C) 2001 by Alex Podolsky
    email                : alexpod@users.sf.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 <iostream.h>

/* QT includes */
#include <qlayout.h>
#include <qsplitter.h>
#include <qgrid.h>
#include <qtoolbutton.h>

/* KDE includes */
#include <kio/netaccess.h>

/* cdbakeoven includes */
#include "NewAudioCdCompilationView.h"
#include "NewAudioCdCompilationView.moc"
#include "CdboCompilationView.h"

#include "widgets/AudioTreeCntPanel.h"
#include "widgets/AudioEstimatePnl.h"
#include "widgets/ExpandPnl.h"
#include "widgets/fileViews/AudioCompilationTreeView.h"
#include "dialogs/NewAudioCdDialog.h"

NewAudioCdCompilationView::NewAudioCdCompilationView(QWidget *parent, const char *name) :
			CdboCompilationView(parent, name){
	
	initView();
	initSlots();
}
//===================================
NewAudioCdCompilationView::~NewAudioCdCompilationView(){
}
//===================================
void NewAudioCdCompilationView::initView(){

// -------------------- TargetTreeCntPanel ------------------------
	targetCnt = new AudioTreeCntPanel(this);
	targetCnt->resize(10,10);

// --------------------  CompilationTreeView ------------------------
	compilView = new AudioCompilationTreeView(this, "audioCompilView");
  	compilView ->setFocusPolicy(QWidget::ClickFocus);

// --------------------  EstimatePnl ------------------------
	estimateHldr = new ExpandPnl(this, "audioEstimateHldr");
  	estimate = new AudioEstimatePnl(estimateHldr,"audioEstimate" );
	estimateHldr->addCustomWidget(estimate);
  	compilView->setEstimate(estimate);
}
//===================================
void NewAudioCdCompilationView::initSlots(){
							
	
	connect( targetCnt->getShowPropBtn(), SIGNAL(clicked()),
						compilView, SLOT(slotProperties()) );
	
	connect( targetCnt->getDeleteNodeBtn(), SIGNAL(clicked()),
						compilView, SLOT(remove()) );
	connect( targetCnt->getIsoPropBtn(), SIGNAL(clicked()),
						compilView, SLOT(compilSettingsSlot()) );
						
	connect( targetCnt->getBurnBtn(), SIGNAL(clicked()),
						this, SLOT(createCD()) );
	connect( targetCnt->getMoveDownBtn(), SIGNAL(clicked()),
						compilView, SLOT(moveDown()) );
	connect( targetCnt->getMoveUpBtn(), SIGNAL(clicked()),
						compilView, SLOT(moveUp()) );


	connect(  compilView, SIGNAL(popUpSettings(int)),
						this, SLOT(popUpSettings(int)));
	connect(  compilView, SIGNAL(modified()),
						this, SLOT(contentsModified()));
						
}
//==============================
void NewAudioCdCompilationView::saveOptions(){
	estimate->saveOptions();
	estimateHldr->saveOptions();
}
//========================
void NewAudioCdCompilationView::createCD(){
	if(isEmpty()){
		showEmptyError();
		return ;
	}
	NewAudioCdDialog *ncd = new NewAudioCdDialog(compilView->getFileList(), this, "ncd" ) ;
	ncd->exec();
	delete ncd;
	compilView->updateSettings();
	
}
//=================================
void NewAudioCdCompilationView::doLoad(QString fileToOpen){
	compilView->updateSettings();
	KConfig *file = new KConfig(fileToOpen);
	file->setGroup("CD Bake Oven - CD File List");
	compilView->addMultipleItems(file->readListEntry("Files List"));
	delete file;
}
//=================================
void NewAudioCdCompilationView::doSave(QString fileToOpen){
	KIO::NetAccess::del(KURL(fileToOpen));
	KConfig *file = new KConfig(fileToOpen);
	file->setGroup("CD Bake Oven - CD File List");
	file->writeEntry("Type of CD", "New Audio CD");
	//QString name = top->text(0).right(top->text(0).length() - top->text(0).findRev("/", -2) - 1);
	//file->writeEntry("Name of CD", name);
	QStringList *fList = compilView->getFileList();
	file->writeEntry("Files List", *fList);
	file->sync();
	delete file;
	saved = true;
	listFileName = fileToOpen;
	emitGotCaption();
}
//======================================
void NewAudioCdCompilationView::removeAll(){
	compilView->removeAll();
	CdboCompilationView::removeAll();
}
//==============================
void NewAudioCdCompilationView::emitGotCaption(){
	if(isEmpty()){
		emit gotCaption(listFileName, false);
	}
	else{
		emit gotCaption(listFileName, !saved);
	}
}
//==============================
bool NewAudioCdCompilationView::isEmpty(){
	return (compilView->childCount() == 0);
}
