/***************************************************************************
                          CdboInfo.cpp  -  description
                             -------------------
    begin                : Tue Jul 24 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>


#include <qlayout.h>
#include <qvariant.h>
#include <qfile.h>
#include <qtextstream.h>

#include <klocale.h>
#include <kprocess.h>
#include <kconfig.h>
#include <kaboutdata.h>
#include <kglobal.h>
#include <khtml_part.h>
#include <kstandarddirs.h>

#include "CdboInfo.h"
#include "CdboInfo.moc"

//================================
CdboInfo::CdboInfo( QWidget* parent,  const char* name)
   : KCModule( parent, name )
{
	config = new KConfig("cdbakeovenrc");
	
    if ( !name )
 		setName( "CdboInfo" );

	
	 		
     resize( 595, 630 );
	setMinimumSize( QSize( 595, 597 ) );

//////////////////////////////////////////////////////////////////////////////////////////////////
     setCaption( i18n( "CD Bake Oven Information" ) );
     setAutoMask( true );
	cdboInfoLayout = new QVBoxLayout( this );
     cdboInfoLayout->setSpacing( 0 );
     cdboInfoLayout->setMargin( 0 );

	infoBrws = new KHTMLPart(this);
	cdboInfoLayout->addWidget(infoBrws->widget());
	
	connect(infoBrws->browserExtension(),
		SIGNAL(openURLRequestDelayed(const KURL &, const KParts::URLArgs &)),
		SLOT(slotURLRequest(const KURL &, const KParts::URLArgs &)));

	load();

}

/*  
 *  Destroys the object and frees any allocated resources
 */
CdboInfo::~CdboInfo(){

}
//==============================================
void CdboInfo::load(){
	dataDir = locate("data", "cdbakeoven/info/");
	QFile f(locate("data", "cdbakeoven/info/info.html"));
	if (f.open(IO_ReadOnly)){
		QTextStream str(&f);
		message= str.read();
		
	}
	else{
		message = "<html><head></head><body><h1>" + i18n("Failed to load!") + "</h1></body></html>";
		user = "";
		system = "";
		cdrecord = "";
		mkisofs = "";
		cdda2wav = "";
		cdparanoia = "";
		updateInfo();
		return;
	}
	
	
	config->setGroup("Default Settings");
	bool forseCheck = !config->readBoolEntry("Dont Check Sys", true);

	config->setGroup("System Settings");	
	user = config->readEntry("User Name", I18N_NOOP("Unknown"));
	system = config->readEntry("System", I18N_NOOP("Unable to determine"));
	cdrecord = config->readEntry("cdrecord Version", I18N_NOOP("cdrecord not available</B> "\
			" <font color=red>(recording not possible)</font><B>"));
	mkisofs = config->readEntry("mkisofs Version", I18N_NOOP("mkisofs not available</B> "\
				" <font color=red>(ISO creation not possible)</font><B>"));
	cdda2wav = config->readEntry("cdda2wav Version", I18N_NOOP("Not Available"));
	cdparanoia = config->readEntry("cdparanoia Version", I18N_NOOP("Not Available"));
	mpg123 = config->readEntry("mpg123 Version", I18N_NOOP("mpg123 not available</B> "\
				" <font color=red>(MP3 conversion not possible)</font><B>"));
	ogg123 = config->readEntry("ogg123 Version", I18N_NOOP("ogg123 not available</B> "\
				" <font color=red>(Ogg Vorbis conversion not possible)</font><B>"));
	
	if(user == I18N_NOOP("Unknown") || forseCheck){
		getName();
		getCdrecordV();
		getMkisofsV();
		getSystem();
		getCdparanoiaV();
		getCdda2wavV();
		getOgg123V();
		getMpg123V();
	}
	else
		updateInfo();
}
//==============================================
void CdboInfo::save(){
	///////////////////////////////////////////////////
	config->setGroup("System Settings");		
    	
	config->writeEntry("User Name", user);
	config->writeEntry("System", system);
	config->writeEntry("cdrecord Version", cdrecord);
	config->writeEntry("mkisofs Version", mkisofs);
	config->writeEntry("cdda2wav Version", cdda2wav);
	config->writeEntry("cdparanoia Version", cdparanoia);
	config->writeEntry("mpg123 Version", mpg123);
	config->writeEntry("ogg123 Version", ogg123);

}
//==============================================
void CdboInfo::updateInfo(){
	infoBrws->begin(dataDir);
	infoBrws->write(message.arg(user).arg(system).arg(cdrecord).arg(mkisofs).arg(cdda2wav).arg(cdparanoia).arg(ogg123).arg(mpg123));
	infoBrws->end();
}
//==============================================
void CdboInfo::getSystem(){
	QString buff;
	
	QFile ostype("/proc/sys/kernel/ostype");	
	if(ostype.open(IO_ReadOnly))
     		ostype.readLine(buff,(unsigned int)250);
     		
	if(!buff.isEmpty())
		system = buff;
		
     QFile osrelease("/proc/sys/kernel/osrelease");
     	if(osrelease.open(IO_ReadOnly))
     		osrelease.readLine(buff,(unsigned int)250);
     	
     	if(!buff.isEmpty()){
     		if(system == I18N_NOOP("Unable to determine"))
			system = buff;
   		else
   			system =  system + " " + buff;
	}
	if(system != I18N_NOOP("Unable to determine"))
		updateInfo();
}
//==============================================
void CdboInfo::getName(){
		cdboProc = new KProcess();
		*cdboProc << "whoami";
		connect(cdboProc, SIGNAL(processExited(KProcess *)),
     					this, SLOT(gotName(KProcess *)));
     					
  		connect(cdboProc, SIGNAL(receivedStdout (KProcess *, char *, int)),
			this, SLOT(receivedName(KProcess *, char *, int)));
	
		connect(cdboProc, SIGNAL(receivedStderr (KProcess *, char *, int)),
			this, SLOT(receivedName(KProcess *, char *, int )));
	
		if(!cdboProc->start(KProcess::NotifyOnExit, KProcess::AllOutput)){
			delete cdboProc;
		}			
}
//---------------------------------------------------------------------------------------------
void CdboInfo::receivedName(KProcess *, char *buffer, int buflen){	
	if(buflen > 0){
		QString buff(buffer);
		buff = buff.left(buflen);
		user = buff;
		updateInfo();
	}
}
//---------------------------------------------------------------------------------------------
void CdboInfo::gotName(KProcess * proc){	
	delete proc;
}
//==============================================
void CdboInfo::getCdrecordV(){
		cdboProc = new KProcess();
		*cdboProc << "cdrecord" << "-version";
		connect(cdboProc, SIGNAL(processExited(KProcess *)),
     					this, SLOT(gotCdrecordV(KProcess *)));

  		connect(cdboProc, SIGNAL(receivedStdout (KProcess *, char *, int)),
			this, SLOT(receivedCdrecordV(KProcess *, char *, int)));
	
		connect(cdboProc, SIGNAL(receivedStderr (KProcess *, char *, int)),
			this, SLOT(receivedCdrecordV(KProcess *, char *, int )));
	
		if(!cdboProc->start(KProcess::NotifyOnExit, KProcess::AllOutput)){
			delete cdboProc;
		}			
}
//---------------------------------------------------------------------------------------------
void CdboInfo::receivedCdrecordV(KProcess *, char *buffer, int buflen){	
	if(buflen > 0){
		QString buff(buffer);
		buff = buff.left(buflen);
		
		cdrecord = buff.left(buff.find(" ", 9));
		updateInfo();
	}
}
//---------------------------------------------------------------------------------------------
void CdboInfo::gotCdrecordV(KProcess * proc){	
	delete proc;
}
//==============================================
void CdboInfo::getMkisofsV(){
		cdboProc = new KProcess();
		*cdboProc << "mkisofs" << "-version";
			
		connect(cdboProc, SIGNAL(processExited(KProcess *)),
     					this, SLOT(gotMkisofsV(KProcess *)));

  		connect(cdboProc, SIGNAL(receivedStdout (KProcess *, char *, int)),
			this, SLOT(receivedMkisofsV(KProcess *, char *, int)));
	
		connect(cdboProc, SIGNAL(receivedStderr (KProcess *, char *, int)),
			this, SLOT(receivedMkisofsV(KProcess *, char *, int )));
	
		if(!cdboProc->start(KProcess::NotifyOnExit, KProcess::AllOutput)){
			delete cdboProc;
		}			
}
//---------------------------------------------------------------------------------------------
void CdboInfo::receivedMkisofsV(KProcess *, char *buffer, int buflen){	
	if(buflen > 0){
		QString buff(buffer);
		buff = buff.left(buflen);
		
		mkisofs = buff.left(buff.find(" ", 9));
		updateInfo();
	}
}
//---------------------------------------------------------------------------------------------
void CdboInfo::gotMkisofsV(KProcess * proc){	
	delete proc;
}
//---------------------------------------------------------------------------------------------
void CdboInfo::getCdparanoiaV(){
		//cdboProc = new KShellProcess();
		cdboProc = new KProcess();
		*cdboProc << "cdparanoia" << "-V";
			
		connect(cdboProc, SIGNAL(processExited(KProcess *)),
     					this, SLOT(gotCdparanoiaV(KProcess *)));

  		connect(cdboProc, SIGNAL(receivedStdout (KProcess *, char *, int)),
			this, SLOT(receivedCdparanoiaV(KProcess *, char *, int)));
	
		connect(cdboProc, SIGNAL(receivedStderr (KProcess *, char *, int)),
			this, SLOT(receivedCdparanoiaV(KProcess *, char *, int )));
	
		if(!cdboProc->start(KProcess::NotifyOnExit, KProcess::AllOutput)){
			delete cdboProc;
		}			
}
//---------------------------------------------------------------------------------------------
void CdboInfo::receivedCdparanoiaV(KProcess *, char *buffer, int buflen){	
	if(buflen > 0){
		QString buff(buffer);
		buff = buff.left(buflen);
		
		QStringList list(QStringList::split(" ", buff));
		cdparanoia = list[0] + " " + list[3];
		updateInfo();
	}
}
//---------------------------------------------------------------------------------------------
void CdboInfo::gotCdparanoiaV(KProcess * proc){	
	delete proc;
}
void CdboInfo::getCdda2wavV(){
		cdboProc = new KProcess();
		*cdboProc << "cdda2wav" << "--help";
		connect(cdboProc, SIGNAL(processExited(KProcess *)),
     					this, SLOT(gotCdda2wavV(KProcess *)));
     					
  		connect(cdboProc, SIGNAL(receivedStdout (KProcess *, char *, int)),
			this, SLOT(receivedCdda2wavV(KProcess *, char *, int)));
	
		connect(cdboProc, SIGNAL(receivedStderr (KProcess *, char *, int)),
			this, SLOT(receivedCdda2wavV(KProcess *, char *, int )));
	
		if(!cdboProc->start(KProcess::NotifyOnExit, KProcess::AllOutput)){
			delete cdboProc;
		}			
}
//---------------------------------------------------------------------------------------------
void CdboInfo::receivedCdda2wavV(KProcess *, char *buffer, int buflen){	
	if(buflen > 0){
		QString buff(buffer);
		buff = buff.left(buflen);
		if(cdda2wav.find("cdda2wav ") != -1)
			return;
			
		buff = buff.right(buff.length() - buff.find("Version") -  7);
		cdda2wav = "cdda2wav " + buff.left(buff.find("_"));
		
		updateInfo();
 	}
}
//---------------------------------------------------------------------------------------------
void CdboInfo::gotCdda2wavV(KProcess * proc){	
	delete proc;
}
//---------------------------------------------------------------------------------------------
void CdboInfo::getOgg123V(){
		cdboProc = new KProcess();
		*cdboProc << "ogg123" << "-V";
		connect(cdboProc, SIGNAL(processExited(KProcess *)),
     					this, SLOT(gotOgg123V(KProcess *)));
     					
  		connect(cdboProc, SIGNAL(receivedStdout (KProcess *, char *, int)),
			this, SLOT(receivedOgg123V(KProcess *, char *, int)));
	
		connect(cdboProc, SIGNAL(receivedStderr (KProcess *, char *, int)),
			this, SLOT(receivedOgg123V(KProcess *, char *, int )));
	
		if(!cdboProc->start(KProcess::NotifyOnExit, KProcess::AllOutput)){
			delete cdboProc;
		}			
}
//---------------------------------------------------------------------------------------------
void CdboInfo::receivedOgg123V(KProcess *, char *buffer, int buflen){	
	if(buflen > 0){
		QString buff(buffer);
		buff = buff.left(buflen);	
		ogg123 = buff;
		
		updateInfo();
 	}
}
//---------------------------------------------------------------------------------------------
void CdboInfo::gotOgg123V(KProcess * proc){	
	delete proc;
}
//---------------------------------------------------------------------------------------------
void CdboInfo::getMpg123V(){
		cdboProc = new KProcess();
		*cdboProc << "mpg123";
		connect(cdboProc, SIGNAL(processExited(KProcess *)),
     					this, SLOT(gotMpg123V(KProcess *)));
     					
  		connect(cdboProc, SIGNAL(receivedStdout (KProcess *, char *, int)),
			this, SLOT(receivedMpg123V(KProcess *, char *, int)));
	
		connect(cdboProc, SIGNAL(receivedStderr (KProcess *, char *, int)),
			this, SLOT(receivedMpg123V(KProcess *, char *, int )));
	
		if(!cdboProc->start(KProcess::NotifyOnExit, KProcess::AllOutput)){
			delete cdboProc;
		}			
}
//---------------------------------------------------------------------------------------------
void CdboInfo::receivedMpg123V(KProcess *, char *buffer, int buflen){	
	if(buflen > 0){
		QString buff(buffer);
		buff = buff.left(buflen);
		
		if(mpg123.find("mpg123 ") != -1 && mpg123.find("not possible)") == -1)
			return;
		
		buff = buff.right(buff.length() - buff.find("Version") -  8);
		mpg123 = "mpg123 " + buff.left(buff.find(" "));
		
		//mpg123 = buff;
		updateInfo();
 	}
}
//---------------------------------------------------------------------------------------------
void CdboInfo::gotMpg123V(KProcess * proc){	
	delete proc;
}
//---------------------------------------------------------------------------------------------
int CdboInfo::buttons() {
	return KCModule::Cancel|KCModule::Ok|KCModule::Help;
}
//---------------------------------------------------------------------------------------------
QString CdboInfo::quickHelp() const{
	return I18N_NOOP("Information.");
}
//---------------------------------------------------------------------------------------------
const KAboutData* CdboInfo::aboutData() const
{

	KAboutData *myAboutData = new KAboutData(I18N_NOOP("cdbakeoven"),
					I18N_NOOP("CD Bake Oven Information."),
                  0, 0, KAboutData::License_GPL,
                  "(c) 2001 - 2002 Alex Podolsky");

    myAboutData->addAuthor("Alex Podolsky", 0, "alexpod@users.sf.net");

    return myAboutData;
}
//---------------------------------------------------------------------------------------------
extern "C"{
    KCModule *create_cdboinfo(QWidget *parent, const char *name){
        KGlobal::locale()->insertCatalogue("cdbakeoven");
        return new CdboInfo(parent, name);
    }

}
//#include "CdboInfo.moc"
