/***************************************************************************
                          NewAdvancedCdCompilationView.cpp  -  description
                             -------------------
    begin                : Sat Dec 1 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.                                   *
 *                                                                         *
 ***************************************************************************/

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

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


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

#include "widgets/TargetTreeCntPanel.h"
#include "widgets/EstimatePnl.h"
#include "widgets/ExpandPnl.h"
#include "widgets/fileViews/FakeFilesListView.h"

#include "dialogs/NewCdDialog.h"

NewAdvancedCdCompilationView::NewAdvancedCdCompilationView(QWidget *parent, const char *name) :
			CdboCompilationView(parent, name){

	initView();
	initSlots();
}
//===================================
NewAdvancedCdCompilationView::~NewAdvancedCdCompilationView(){
    delete targetCnt;
    delete foldersView;
    delete filesView;
    delete estimateHldr;
}
//===================================
void NewAdvancedCdCompilationView::initView(){

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

// --------------------  CompilationTreeView ------------------------

	QSplitter *spliter = new QSplitter(this, "compilViewSplitter");
	//spliter ->setFocusPolicy(QWidget::ClickFocus);

	foldersView = new FakeFoldersListView(spliter,"foldersView");
	foldersView ->setFocusPolicy(QWidget::ClickFocus);

	filesView = new FakeFilesListView(spliter,"filesView");
	filesView ->setFocusPolicy(QWidget::ClickFocus);
	foldersView ->setFakeListView(filesView);



// --------------------  EstimatePnl ------------------------

  	estimateHldr = new ExpandPnl(this, "estimateHldr");
  	estimate = new EstimatePnl(estimateHldr,"estimate" );

  	estimateHldr->addCustomWidget(estimate);
	foldersView->setEstimate(estimate);
}
//===================================
void NewAdvancedCdCompilationView::initSlots(){
	connect( targetCnt->getShowPropBtn(), SIGNAL(clicked()),
						getView(), SLOT(slotProperties()) );
	connect( targetCnt->getReloadBtn(), SIGNAL(clicked()),
						this, SLOT(loadSession()) );
	connect( targetCnt->getDeleteNodeBtn(), SIGNAL(clicked()),
						getView(), SLOT(remove()) );
	connect( targetCnt->getIsoPropBtn(), SIGNAL(clicked()),
						getView(), SLOT(compilSettingsSlot()) );

	/*connect( targetCnt->getCollapseTrgBtn(), SIGNAL(clicked()),
						getView(), SLOT(collapse()) );*/
	connect( targetCnt->getBurnBtn(), SIGNAL(clicked()),
						this, SLOT(createCD()) );
	connect( targetCnt->getCreateImgBtn(), SIGNAL(clicked()),
						this, SLOT(createISO()) );
	//connect(compilView, SIGNAL(popUpSettings(int)),
						//this, SLOT(popUpSettings(int)));


	/*connect(getView(), SIGNAL(changingSize(bool)),
						estimateHldr, SLOT(expandSlot(bool)));*/

	///////////// NEW //////////////////////////////////////////////////
	connect( foldersView, SIGNAL(folderClicked(FakeFolderItem *)),
						filesView, SLOT(loadFiles( FakeFolderItem *)) );
	connect(  filesView, SIGNAL(dropped (QDropEvent *, QListViewItem *)),
						foldersView, SLOT(dropOcurred(QDropEvent *, QListViewItem *)));
	/*connect(  filesView, SIGNAL(selectFileForDrop (QListViewItem *)),
						foldersView, SLOT(slotSelectFileForDrop (QListViewItem *)));
	*/
	connect(  foldersView, SIGNAL(popUpSettings(int)),
						this, SLOT(popUpSettings(int)));
	connect(  foldersView, SIGNAL(modified()),
						this, SLOT(contentsModified()));
	connect(  filesView, SIGNAL(modified()),
						this, SLOT(contentsModified()));
}
//==============================
void NewAdvancedCdCompilationView::saveOptions(){
	estimate->saveOptions();
	estimateHldr->saveOptions();
}
//========================
void NewAdvancedCdCompilationView::createISO(){

	QString *mappingsString = foldersView->generateMappingsString();
//cerr << "mappingsString->length() ========= " << mappingsString->length() << endl;
	if(!mappingsString || mappingsString->length() == 0){
		showEmptyError();
		return ;
	}

	
	if(!checkDirSize())
		return;

	KConfig *config = new KConfig("cdbakeovenrc");
	config->setGroup("Default Settings");
	QString fn = config->readEntry("Tmp Dir", "/tmp/");

	config->setGroup("ISO Settings");
	fn = fn + config->readEntry("Custom ISO Name", "cdboImage_<DATE>.iso");
	delete config;
	
	if(!checkOverwrite(fn))
		return;

	NewCdDialog *ncd = new NewCdDialog(mappingsString, this, "ncd", true ) ;
	ncd->exec();
	delete ncd;
	foldersView->updateSettings();
}
//========================
void NewAdvancedCdCompilationView::createCD(){

	QString *mappingsString = foldersView->generateMappingsString();
	if(!mappingsString || mappingsString->length() == 0){
		showEmptyError();
		return ;
	}

	//cerr << "mappingsString->length() ========= " << mappingsString->length() << endl;

	if(!checkDirSize())
		return;

	KConfig *config = new KConfig("cdbakeovenrc");
	config->setGroup("Default Settings");
	QString fn = config->readEntry("Tmp Dir", "/tmp/");

	config->setGroup("ISO Settings");
	fn = fn + config->readEntry("Custom ISO Name", "cdboImage_<DATE>.iso");
	delete config;
	
	if(!checkOverwrite(fn))
		return;

    	NewCdDialog *ncd = new NewCdDialog(mappingsString, this, "ncd") ;
	ncd->exec();
	delete ncd;
	foldersView->updateSettings();

}
//=====================================
void NewAdvancedCdCompilationView::doLoad(QString fileToOpen){
	KConfig *file = new KConfig(fileToOpen);
	foldersView->loadFile(file);
	delete file;
}
//=================================
void NewAdvancedCdCompilationView::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", "Advanced Layout Data CD");

	KConfig *config = new KConfig("cdbakeovenrc");
	config->setGroup("ISO Settings");
	QString name = config->readEntry("Custom ISO Name", "cdboImage_<DATE>.iso");
	delete config;
	
	file->writeEntry("Total Size", foldersView->getTotalSize());
	file->writeEntry("Name of CD", name);

	foldersView->persist(file);
	file->sync();
	delete file;

	
	saved = true;
	listFileName = fileToOpen;
	emitGotCaption();
}
//==============================
void NewAdvancedCdCompilationView::emitGotCaption(){
	if(isEmpty()){
		emit gotCaption(listFileName, false);
	}
	else{
		emit gotCaption(listFileName, !saved);
	}
}
//===========================================================
void NewAdvancedCdCompilationView::removeAll(){
	foldersView->removeAll();
	CdboCompilationView::removeAll();
}
//========================
bool NewAdvancedCdCompilationView::loadSession(){
	if(!saveAndRemove())
		return false;
	foldersView->loadSession();
	return true;
}
//==============================
bool NewAdvancedCdCompilationView::isEmpty(){
	return (filesView->childCount() == 0 && foldersView->firstChild()->childCount() == 0);
}

