/***************************************************************************
                          cdbakeovenview.cpp  -  description
                             -------------------
    begin                : Fri May 18 23:05:50 EDT 2001
    copyright            : (C) 2001 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 <iostream.h>

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

/* KDE includes */
#include <kmainwindow.h>
#include <kprogress.h>
#include <kio/job.h>
#include <kio/jobclasses.h>
#include <kapplication.h>
#include <kconfig.h>
#include <kcursor.h>
#include <klocale.h>

/* cdbakeoven includes */
#include "cdbakeovenview.h"
#include "cdbakeovenview.moc"
#include "cdbakeoven.h"
#include "NewCdCompilationView.h"
#include "NewAudioCdCompilationView.h"
#include "NewAdvancedCdCompilationView.h"
#include "CdboCompilationView.h"
#include "widgets/SrcListPnl.h"
#include "widgets/SrcTreeCntPanel.h"
#include "widgets/fileViews/FilesListView.h"
#include "widgets/fileViews/FoldersListView.h"

#define CDBO_DATA_VIEW 101
#define CDBO_AUDIO_VIEW 102
#define CDBO_ADVANCED_VIEW 103

CDBakeOvenView::CDBakeOvenView(QWidget *parent, const char *name) : QSplitter(parent, name){
	
	this->setOrientation(QSplitter::Vertical);
	initView();
	initRegConnections();
	config = kapp->config();
	config->setGroup("General Options");
	activeType = config->readNumEntry("Active View Type", CDBO_DATA_VIEW);
	newActive();

}

CDBakeOvenView::~CDBakeOvenView(){
}

void CDBakeOvenView::initView(){

////////////////////////// TOP ////////////////////////////////////////////////

	QGrid *top = new QGrid(2,this, "top");

    	
// -------------------- SrcTreeCntPanel ------------------------
	srcCont = new SrcTreeCntPanel(top);
	srcCont->resize(10,10);

// -------------------- SrcSplitter ------------------------		
	QSplitter *srcSplitter = new QSplitter( top );
	
// -------------------- srcTree ------------------------
	srcTree = new FoldersListView(srcSplitter,"srcTree");
	srcTree ->setFocusPolicy(QWidget::ClickFocus);
	
// -------------------- SrcListCntPanel ------------------------
	listPnl = new SrcListPnl( srcSplitter,"SrcListPn" );
	listPnl ->setFocusPolicy(QWidget::ClickFocus);
	//srcTree ->slotReload();


////////////////////////// BUTTOM ////////////////////////////////////////////////

    	buttomViewHolder = new QWidgetStack(this, "buttom");
    	
	compilView = new NewCdCompilationView(buttomViewHolder, "dataView");
    	buttomViewHolder->addWidget(compilView, CDBO_DATA_VIEW);
    	
    	audioCompilView = new NewAudioCdCompilationView(buttomViewHolder, "audioView");
    	buttomViewHolder->addWidget(audioCompilView, CDBO_AUDIO_VIEW);
    	
    	
    	advancedCompilView = new NewAdvancedCdCompilationView(buttomViewHolder, "advancedView");
    	buttomViewHolder->addWidget(advancedCompilView, CDBO_ADVANCED_VIEW);
    	/**/
    	
}
//=====================================
bool CDBakeOvenView::queryClose(){
	config->setGroup("General Options");
	config->writeEntry("Active View Type", activeType);

	CdboCompilationView *view = (CdboCompilationView *)buttomViewHolder->widget(activeType);
	return view->saveAndRemove();
}
//=====================================
 bool CDBakeOvenView::newActive(){
 	switch(activeType){
		case CDBO_DATA_VIEW:
			return showData();
		case CDBO_AUDIO_VIEW:
			return showAudio();
		case CDBO_ADVANCED_VIEW:
			return showAdvanced();
	}
	return true;
}
//=====================================
 bool CDBakeOvenView::showAudio(){	 	
 	if(!disconnectActive())
 		return false;
	buttomViewHolder->raiseWidget(CDBO_AUDIO_VIEW);
	connectView(audioCompilView);
	activeType = CDBO_AUDIO_VIEW;
	((CDBakeOvenApp *)parentWidget())->newType(i18n("Mode: Audio"));
	return true;
 } 	
 //=====================================
 bool CDBakeOvenView::showData(){	 	
 	if(!disconnectActive())
 		return false;
 	buttomViewHolder->raiseWidget(CDBO_DATA_VIEW);
	connectView(compilView);
	activeType = CDBO_DATA_VIEW;
	((CDBakeOvenApp *)parentWidget())->newType(i18n("Mode: Data"));
	return true;
 }
  //=====================================
 bool CDBakeOvenView::showAdvanced(){	
	if(!disconnectActive())
 		return false;
 	buttomViewHolder->raiseWidget(CDBO_ADVANCED_VIEW);
	connectView(advancedCompilView);
	activeType = CDBO_ADVANCED_VIEW;
	((CDBakeOvenApp *)parentWidget())->newType(i18n("Mode: Data (Custom Layout)"));
	return true;
 }

//=====================================
bool CDBakeOvenView::disconnectActive(){
	CdboCompilationView *view = (CdboCompilationView *)buttomViewHolder->widget(activeType);
	if(!view->saveAndRemove())
		return false;
	disconnectView(view);
	return true;
}
////////////////////////////////////////////////////////////////
//////////////////////////  SLOTS & SIGNALS //////////////////////////
//////////////////////////  ///////////////////////////// //////////////////////////
void CDBakeOvenView::initRegConnections(){	
					
	connect(srcTree, SIGNAL(processStarted()),
						((CDBakeOvenApp *)parentWidget()), SLOT(startShowProgress()));
	connect(srcTree, SIGNAL(processDone()),
						((CDBakeOvenApp *)parentWidget()), SLOT(stopShowProgress()));
	connect(srcTree, SIGNAL(haveStatus(const QString &)),
						((CDBakeOvenApp *)parentWidget()), SLOT(slotStatusMsg(const QString &)));		

						
	connect( srcCont->getShowPropBtn(), SIGNAL(clicked()),
						srcTree, SLOT(slotProperties()) );
	connect( srcCont->getReloadBtn(), SIGNAL(clicked()),
						srcTree, SLOT(slotReload()) );
	connect( srcCont->getShowHiddenBtn(), SIGNAL(clicked()),
						srcTree, SLOT(slotShowHidden()) );
						
	connect( srcCont->getAddToCompilation(), SIGNAL(clicked()),
						srcTree, SLOT(slotAddToCompilation()) );
	connect( srcCont->getAddToTopBtn(), SIGNAL(clicked()),
						srcTree, SLOT(slotAddToTop()) );
	connect( srcTree, SIGNAL(folderClicked(FolderItem *)),
						listPnl->getFilestLst(), SLOT(loadDir(FolderItem *)) );
	connect( listPnl->getFilestLst(), SIGNAL(folderClicked(QListViewItem *)),
						srcTree, SLOT(slotSelectFile(QListViewItem *)) );
						
						
	//////////////////BUTTOM///////////////////////							
						
	
	connect(compilView->getView(), SIGNAL(processStarted()),
						this, SLOT(startLongProcess()));
	connect(compilView->getView(), SIGNAL(processDone()),
						this, SLOT(stopLongProcess()));
						
	connect(audioCompilView->getView(), SIGNAL(processStarted()),
						this, SLOT(startLongProcess()));
	connect(audioCompilView->getView(), SIGNAL(processDone()),
						this, SLOT(stopLongProcess()));
	
	connect(advancedCompilView->getView(), SIGNAL(processStarted()),
						this, SLOT(startLongProcess()));
	connect(advancedCompilView->getView(), SIGNAL(processDone()),
						this, SLOT(stopLongProcess()));
	
}				

//==============================
void CDBakeOvenView::connectView(CdboCompilationView *view){
	
	connect( srcTree, SIGNAL(addToCompilation(QString)),
						view->getView(), SLOT(addOneItem(QString )) );
	connect( listPnl->getFilestLst(), SIGNAL(addToCompilation(QString)),
						view->getView(), SLOT(addMultipleItems(QString )) );
			////////////////////////////////
	connect(((CDBakeOvenApp *)parentWidget()),  SIGNAL(newList()),
						view, SLOT(saveAndRemove()));
	connect(((CDBakeOvenApp *)parentWidget()),  SIGNAL(newList(QString)),
						view, SLOT(saveRemoveAndLoad(QString)));
	connect(((CDBakeOvenApp *)parentWidget()),  SIGNAL(saveList()),
						view, SLOT(save()));
	connect(((CDBakeOvenApp *)parentWidget()),  SIGNAL(saveListAs()),
						view, SLOT(saveAs()));
	connect(((CDBakeOvenApp *)parentWidget()),  SIGNAL(settingsChanged()),
						view->getView(), SLOT(updateSettings()));

/////////////////////PROCESSES & OUTPUT///////////////////////////
		
	connect(parentWidget(),  SIGNAL(longProcessCanceled(QString)),
						view->getView(), SLOT(longProcessCanceled(QString)));
						
		
	connect(view->getView(), SIGNAL(processStarted()),
						parentWidget(), SLOT(startShowProgress()));
	connect(view->getView(), SIGNAL(processDone()),
						parentWidget(), SLOT(stopShowProgress()));
						
	connect(view->getView(), SIGNAL(haveStatus(const QString &)),
						parentWidget(), SLOT(slotStatusMsg(const QString &)));
	connect(view, SIGNAL(gotCaption(const QString &, bool)),
						parentWidget(), SLOT(setCaption(const QString &, bool)));
	
	connect(view->getView(), SIGNAL(longProcessStarted(KIO::Job *)),
						parentWidget(), SLOT(addJob(KIO::Job *)));
	connect(view->getView(), SIGNAL(longProcessDone(KIO::Job *)),
						parentWidget(), SLOT(removeJob(KIO::Job *)));

}
//==============================
void CDBakeOvenView::disconnectView(CdboCompilationView *view){	
	disconnect( srcTree, SIGNAL(addToCompilation(QString)),
						view->getView(), SLOT(addOneItem(QString )) );
	disconnect( listPnl->getFilestLst(), SIGNAL(addToCompilation(QString)),
						view->getView(), SLOT(addMultipleItems(QString )) );	
			////////////////////////////////
	disconnect(((CDBakeOvenApp *)parentWidget()),  SIGNAL(newList()),
						view, SLOT(saveAndRemove()));
	disconnect(((CDBakeOvenApp *)parentWidget()),  SIGNAL(newList(QString)),
						view, SLOT(saveRemoveAndLoad(QString)));
	disconnect(((CDBakeOvenApp *)parentWidget()),  SIGNAL(saveList()),
						view, SLOT(save()));
	disconnect(((CDBakeOvenApp *)parentWidget()),  SIGNAL(saveListAs()),
						view, SLOT(saveAs()));
	disconnect(((CDBakeOvenApp *)parentWidget()),  SIGNAL(settingsChanged()),
						view->getView(), SLOT(updateSettings()));	
/////////////////////PROCESSES & OUTPUT///////////////////////////
		
	disconnect(parentWidget(),  SIGNAL(longProcessCanceled(QString)),
						view->getView(), SLOT(longProcessCanceled(QString)));
						
		
	disconnect(view->getView(), SIGNAL(processStarted()),
						parentWidget(), SLOT(startShowProgress()));
	disconnect(view->getView(), SIGNAL(processDone()),
						parentWidget(), SLOT(stopShowProgress()));
						
	disconnect(view->getView(), SIGNAL(haveStatus(const QString &)),
						parentWidget(), SLOT(slotStatusMsg(const QString &)));
	disconnect(view, SIGNAL(gotCaption(const QString &, bool)),
						parentWidget(), SLOT(setCaption(const QString &, bool)));
	
	disconnect(view->getView(), SIGNAL(longProcessStarted(KIO::Job *)),
						parentWidget(), SLOT(addJob(KIO::Job *)));
	disconnect(view->getView(), SIGNAL(longProcessDone(KIO::Job *)),
						parentWidget(), SLOT(removeJob(KIO::Job *)));		
}
//==============================
void CDBakeOvenView::saveOptions(){
	compilView->saveOptions();
	audioCompilView->saveOptions();
	advancedCompilView->saveOptions();
	srcTree->saveTopList();
}

//==============================
void CDBakeOvenView::showSrcControl(bool doShow){
	if(!doShow)
		srcCont->hide();
	else
		srcCont->show();
}
//==============================
void CDBakeOvenView::showTargetControl(bool doShow){
	((CdboCompilationView* )buttomViewHolder->visibleWidget())->showTargetControl(doShow);
}
//==============================
void CDBakeOvenView::showEstimate(bool doShow){
	((CdboCompilationView* )buttomViewHolder->visibleWidget())->showEstimate(doShow);
}
//==============================
void CDBakeOvenView::showFilterPnl(bool doShow){
	listPnl->showSrcListCntPanel(doShow);
}
//---------------------------------------------------------------------------------------------
void CDBakeOvenView::startLongProcess(){
	enableParts(false);
}
//---------------------------------------------------------------------------------------------
void CDBakeOvenView::stopLongProcess(){
	enableParts(true);
}

//---------------------------------------------------------------------------------------------
void CDBakeOvenView::enableParts(bool yes){
	if(!yes){
  		this->setCursor(KCursor::waitCursor());
  		/*srcCont->setCursor(KCursor::waitCursor());
		
  		srcTree->setCursor(KCursor::waitCursor());
  		listPnl->setCursor(KCursor::waitCursor());
  		buttomViewHolder->setCursor(KCursor::waitCursor());*/
	}
	else{
		this->unsetCursor();
		/*srcCont->unsetCursor();
  		listPnl->unsetCursor();
  		buttomViewHolder->unsetCursor();
  		srcTree->unsetCursor();*/
	}
	this->setEnabled(yes);
	/*srcCont->setEnabled(yes);
  	listPnl->setEnabled(yes);
  	buttomViewHolder->visibleWidget()->setEnabled(yes);
  	srcTree->setEnabled(yes);*/
  	((CDBakeOvenApp *)parentWidget())->cancelOperationBtn->setEnabled(!yes);
}
