/***************************************************************************
                          DriveInfoPnl.cpp  -  description
                             -------------------
    begin                : Sat May 5 2001
    copyright            : (C) 2001 by Alexy 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>



#include <qbuttongroup.h>
#include <qcombobox.h>
#include <qpushbutton.h>
#include <qlayout.h>
#include <qvariant.h>
#include <qtooltip.h>
#include <qwhatsthis.h>

#include <kconfig.h>
#include <kiconloader.h>
#include <kprocess.h>

#include "DriveInfoPnl.h"
#include "DriveInfoPnl.moc"

DriveInfoPnl::DriveInfoPnl( QWidget* parent,  const char* name, WFlags fl)
    : QWidget( parent, name, fl ){

     if ( !name )
 		setName( "DriveInfoPnl" );
	type = 0;
	//ejected = false;
	config = new KConfig("cdbakeovenrc");

     resize( 284, 131 );

    driveInfoPnlLayout = new QHBoxLayout( this, 0, 0, "driveInfoPnlLayout");

    driveInfoPnlFrm = new QButtonGroup( this, "driveInfoPnlFrm" );
    driveInfoPnlFrm->setTitle( i18n( "ButtonGroup1" ) );
    driveInfoPnlFrm->setColumnLayout(0, Qt::Vertical );
    driveInfoPnlFrm->layout()->setSpacing( 6 );
    driveInfoPnlFrm->layout()->setMargin( 11 );
    driveInfoPnlFrmLayout = new QGridLayout( driveInfoPnlFrm->layout() );
    driveInfoPnlFrmLayout->setAlignment( Qt::AlignTop );

    devicesCmb = new QComboBox( FALSE, driveInfoPnlFrm, "devicesCmb" );
    devicesCmb->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)7, (QSizePolicy::SizeType)0, 0, 30, devicesCmb->sizePolicy().hasHeightForWidth() ) );
	devicesCmb->setMinimumSize( QSize( 90, 30 ) );
    driveInfoPnlFrmLayout->addMultiCellWidget( devicesCmb, 0, 0, 2, 4 );
    QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding );
    driveInfoPnlFrmLayout->addItem( spacer, 2, 3 );
    QSpacerItem* spacer_2 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
    driveInfoPnlFrmLayout->addItem( spacer_2, 1, 4 );

    detectBtn = new QPushButton( driveInfoPnlFrm, "detectBtn" );
    detectBtn->setMinimumSize( QSize( 90, 30 ) );
    detectBtn->setText( i18n( "&Detect" ) );
    QToolTip::add( detectBtn, i18n( "Detect devices" ) );
    QWhatsThis::add( detectBtn, i18n( "Invoke detection dialog." ) );

    driveInfoPnlFrmLayout->addWidget( detectBtn, 1, 3 );

    usrBtn = new QPushButton( driveInfoPnlFrm, "usrBtn" );
    usrBtn->setMinimumSize( QSize( 90, 30 ) );
    usrBtn->setText( i18n( "&Preview" ) );

    driveInfoPnlFrmLayout->addMultiCellWidget( usrBtn, 1, 1, 1, 2 );
    QSpacerItem* spacer_3 = new QSpacerItem( 20, 20, QSizePolicy::Minimum, QSizePolicy::Minimum );
    driveInfoPnlFrmLayout->addItem( spacer_3, 1, 0 );

    deviceBtn = new QPushButton( driveInfoPnlFrm, "deviceBtn" );
    deviceBtn->setMinimumSize( QSize( 55, 55 ) );
    deviceBtn->setMaximumSize( QSize( 55, 55 ) );
    deviceBtn->setText( i18n( "Eject" ) );

    driveInfoPnlFrmLayout->addMultiCellWidget( deviceBtn, 0, 0, 0, 1 );
    driveInfoPnlLayout->addWidget( driveInfoPnlFrm );
     // signals and slots connections
     connect( deviceBtn, SIGNAL( clicked() ), this, SLOT( deviceBtnSlot() ) );

     //connect( usrBtn, SIGNAL( clicked() ), this, SLOT( usrBtnSlot() ) );
}
/*  
 *  Destroys the object and frees any allocated resources
 */
DriveInfoPnl::~DriveInfoPnl(){
}
//---------------------------------------------------------------------------
void DriveInfoPnl::setType(int t) {
	if(t < 1 || t > 2) return;//invalid type
	type = t;
	if(type == Target){
		setTitle( i18n( "Destination" ) );
     		QWhatsThis::add(  driveInfoPnlFrm, i18n( "Target (writing device)." ) );
     		QToolTip::add(  devicesCmb, i18n( "Available recording devices." ) );
		
		QPixmap image = UserIcon("target", KIcon::SizeMedium );
		setDevImage(image);
     		QToolTip::add(  deviceBtn, i18n( "Eject destination media." ) );
	}
	else{
		setTitle(i18n("Source"));
		QWhatsThis::add(  driveInfoPnlFrm, i18n( "Source (reading device)." ) );
     		QToolTip::add(  devicesCmb, i18n( "Available CDROM drives." ) );
     		
		QPixmap image = UserIcon("source", KIcon::SizeMedium );
		setDevImage(image);
     		QToolTip::add(  deviceBtn, i18n( "Eject source media." ) );
	}
}
//---------------------------------------------------------------------------------------------
void DriveInfoPnl::readOptions(){
	if(!type) return;
	if(config)
		delete config;
	config = new KConfig("cdbakeovenrc");
	if(type == Target){
		///////////////////////////////////////////////////////////
       	config->setGroup("Target Devices");
       	QStringList targetsList(config->readListEntry("Targets List"));
       	devicesCmb->clear();
       	devicesCmb->insertStringList(targetsList,0);
       	
       	int pos = config->readNumEntry("Last Target", 0);
          	if(pos <= devicesCmb->count())
          		devicesCmb->setCurrentItem(pos);
	}
	else{
		///////////////////////////////////////////////////////////    	
           config->setGroup("Source Devices");
       	QStringList sourceList(config->readListEntry("Source List"));
           devicesCmb->clear();
       	devicesCmb->insertStringList(sourceList,0);
       	
       	int pos = config->readNumEntry("Last Source", 0);
       	if(pos <= devicesCmb->count())
          		devicesCmb->setCurrentItem(pos);
     	}
}
//---------------------------------------------------------------------------------------------
void DriveInfoPnl::saveOptions(){
	if(!type) return;
//cerr << "void DriveInfoPnl::saveOptions()\n";
	/*if(config)
		delete config;
	config = new KConfig("cdbakeovenrc");*/
	if(type == Target){
		///////////////////////////////////////////////////////////////
     		config->setGroup("Target Devices");
		config->writeEntry("Last Target", devicesCmb->currentItem());
	}
	else{
		///////////////////////////////////////////////////////////////
		config->setGroup("Source Devices");
		config->writeEntry("Last Source", devicesCmb->currentItem());
	}
	config->sync();
}
//---------------------------------------------------------------------------------------------
QString DriveInfoPnl::getScsiDevice(){
	if(type != Target) return "";
	config->setGroup("Target Devices");
	return config->readEntry(devicesCmb->currentText(),"");
}
//---------------------------------------------------------------------------
void DriveInfoPnl::deviceBtnSlot(){
    	deviceBtn->setEnabled(false);
    	if(!ejected) eject();
	else closeTray();
}
//---------------------------------------------------------------------------------------------
void DriveInfoPnl::eject(){
	cdboProc = new KProcess();
	*cdboProc << "eject" << getDriveName();
	connect(cdboProc, SIGNAL(processExited(KProcess *)),
     			this, SLOT(processComplited(KProcess *)));
	ejected = true;
	emit trayEjected();
	if(!cdboProc->start(KProcess::NotifyOnExit)){
		deviceBtn->setEnabled(true);
		delete cdboProc;
	}
}
//---------------------------------------------------------------------------------------------
void DriveInfoPnl::closeTray(){
	cdboProc = new KProcess();
	*cdboProc << "eject" << "-t" << getDriveName();
	ejected = false;
	connect(cdboProc, SIGNAL(processExited(KProcess *)),
      			this, SLOT(processComplited(KProcess *)));
	if(!cdboProc->start(KProcess::NotifyOnExit)){
	//if(!cdboProc->start(KProcess::Block)){
		deviceBtn->setEnabled(true);
		delete cdboProc;
	}
	
}
//---------------------------------------------------------------------------------------------
void DriveInfoPnl::closeTrayAndWait(){
	//done = false;
	cdboProc = new KProcess();
	*cdboProc << "eject" << "-t" << getDriveName();
	ejected = false;
	//connect(cdboProc, SIGNAL(processExited(KProcess *)),
      //			this, SLOT(processComplited(KProcess *)));
	cdboProc->start(KProcess::Block);
	//if(!cdboProc->start(KProcess::NotifyOnExit))
	//	done = true;
	delete cdboProc;	
}
//---------------------------------------------------------------------------------------------
void DriveInfoPnl::processComplited(KProcess *){
	if(!blocked)
		deviceBtn->setEnabled(true);
	delete cdboProc;
}
//---------------------------------------------------------------------------------------------
void DriveInfoPnl::blockBtns(bool yes){
	blocked = yes;
	deviceBtn->setEnabled(!yes);
	devicesCmb->setEnabled(!yes);
	usrBtn->setEnabled(!yes);
}
//=======================================================
QString DriveInfoPnl::getDriveName() {
	//
	if(type != Target){
		///////////////////////////////////////////////////////////
		config->setGroup("Source Devices");
       	return config->readEntry(devicesCmb->currentText());
	}
	else{	
       	config->setGroup("Target Devices");
       	return config->readEntry(devicesCmb->currentText() + " device");
     	}
     	return "";
}
