/***************************************************************************
                          NewCdCompilationView.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.                                   *
 *                                                                         *
 ***************************************************************************/

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

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

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

#include "widgets/TargetTreeCntPanel.h"
#include "widgets/ExpandPnl.h"
#include "widgets/fileViews/DataCompilationTreeView.h"
#include "dialogs/NewCdDialog.h"

NewCdCompilationView::NewCdCompilationView(QWidget *parent, const char *name) :
			CdboCompilationView(parent, name){
	
	initView();
	initSlots();
}

NewCdCompilationView::~NewCdCompilationView(){
}

void NewCdCompilationView::initView(){

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

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

// --------------------  EstimatePnl ------------------------
	estimateHldr = new ExpandPnl(this, "estimateHldr");
  	estimate = new EstimatePnl(estimateHldr,"estimate" );
  	
  	estimateHldr->addCustomWidget(estimate);
  	compilView->setEstimate(estimate);
}
//===================================
void NewCdCompilationView::initSlots(){
	connect( targetCnt->getShowPropBtn(), SIGNAL(clicked()),
						compilView, SLOT(slotProperties()) );
	connect( targetCnt->getReloadBtn(), SIGNAL(clicked()),
						compilView, SLOT(slotReload()) );
	connect( targetCnt->getDeleteNodeBtn(), SIGNAL(clicked()),
						compilView, SLOT(remove()) );
	connect( targetCnt->getIsoPropBtn(), SIGNAL(clicked()),
						compilView, SLOT(compilSettingsSlot()) );
						
	connect( targetCnt->getCollapseTrgBtn(), SIGNAL(clicked()),
						compilView, 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(  compilView, SIGNAL(modified()),
						this, SLOT(contentsModified()));
}
//==============================
void NewCdCompilationView::saveOptions(){
	estimate->saveOptions();
	estimateHldr->saveOptions();
}
//========================
void NewCdCompilationView::createISO(){
	if(isEmpty()){
		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(compilView->getFileList(), this, "ncd", true ) ;
	ncd->exec();
	delete ncd;
	compilView->updateSettings();
}
//========================
void NewCdCompilationView::createCD(){
	if(isEmpty()){
		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(compilView->getFileList(), this, "ncd" ) ;
	ncd->exec();
	delete ncd;
	compilView->updateSettings();
	
}
//=================================
void NewCdCompilationView::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 NewCdCompilationView::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 Data 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;
	file->writeEntry("Files List", fList);
	file->sync();
	delete file;
	saved = true;
	listFileName = fileToOpen;
	emitGotCaption();
}
//==================================
void NewCdCompilationView::removeAll(){
	compilView->removeAll();
	CdboCompilationView::removeAll();
}
//========================
bool NewCdCompilationView::isEmpty(){
	return(compilView->firstChild()->childCount() == 0);
}
//==============================
void NewCdCompilationView::emitGotCaption(){
	if(isEmpty()){
		emit gotCaption(listFileName, false);
	}
	else{
		emit gotCaption(listFileName, !saved);
	}
}
