/****************************************************************************
** ui.h extension file, included from the uic-generated form implementation.
**
** If you wish to add, delete or rename functions or slots use
** Qt Designer which will update this file, preserving your code. Create an
** init() function in place of a constructor, and a destroy() function in
** place of a destructor.
*****************************************************************************/


extern Settings settings;
extern QDir vymBaseDir;
extern Options options;

void ExportXHTMLDialog::init()
{
	filepath="";
	settingsChanged=false;
	scriptProc=new Process;
}

void ExportXHTMLDialog::readSettings()
{

	dir=settings.readLocalEntry (filepath,"/vym/export/xhtml/exportDir",vymBaseDir.currentDirPath() );
	lineEditDir->setText(dir);
	
    if ( settings.readLocalEntry (filepath,"/vym/export/xhtml/useImage","yes")=="yes")
		useImage=true;
	else	
		useImage=false;
	imageButton->setChecked(useImage);
		
	if ( settings.readLocalEntry (filepath,"/vym/export/xhtml/useTextColor","no")=="yes")
		useTextColor=true;
	else	
		useTextColor=false;
	textColorButton->setChecked(useTextColor);
	
/* FIXME this was used in old html export, is not yet in new stylesheet
	if ( settings.readEntry ("/vym/export/html/useHeading","no")=="yes")
		useHeading=true;
	else	
		useHeading=false;
	checkBox4_2->setChecked(useHeading);
*/		

	if ( settings.readLocalEntry (filepath,"/vym/export/xhtml/saveSettingsInMap","no")=="yes")
		saveSettingsInMap=true;
	else	
		saveSettingsInMap=false;
	saveSettingsInMapButton->setChecked(saveSettingsInMap);

	if ( settings.readEntry ("/vym/export/xhtml/showWarnings","yes")=="yes")
		showWarnings=true;
	else	
		showWarnings=false;
	warningsButton->setChecked(showWarnings);
	
	if ( settings.readEntry ("/vym/export/xhtml/showOutput","no")=="yes")
		showOutput=true;
	else	
		showOutput=false;
	outputButton->setChecked(showOutput);

	// For testing better use local styles
	if (options.isOn ("local"))
	{
		xsl=vymBaseDir.path()+"/styles/vym2xhtml.xsl";
		css=vymBaseDir.path()+"/styles/vym.css";
	} else
	{
		xsl=settings.readLocalEntry 
			(filepath,"/vym/export/xhtml/xsl","/usr/share/vym/styles/vym2xhtml.xsl");
		css=settings.readLocalEntry 
			(filepath,"/vym/export/xhtml/css","/usr/share/vym/styles/vym.css");	
	}
	lineEditXSL->setText(xsl);
	lineEditCSS->setText(css);
	
	prescript=settings.readLocalEntry
		(filepath,"/vym/export/xhtml/prescript","");
	lineEditPreScript->setText (prescript);	
	
	postscript=settings.readLocalEntry
		(filepath,"/vym/export/xhtml/postscript","");
	lineEditPostScript->setText (postscript);	

	if (!prescript.isEmpty() || !postscript.isEmpty())
	{
		QMessageBox::warning( 0, tr( "Warning" ),tr(
		"The settings saved in the map "
		"would like to run scripts:\n\n"
		"%1\n\n"
		"Please check, if you really\n"
		"want to allow this in your system!").arg(prescript+"  "+postscript));
		
	}
}

void ExportXHTMLDialog::destroy()
{
}

void ExportXHTMLDialog::dirChanged()
{
	dir=lineEditDir->text();
	if (dir.right(1)!="/")
		dir+="/";
	settingsChanged=true;
}

void ExportXHTMLDialog::browseDirectoryPressed()
{
   	QFileDialog fd( this, tr("VYM - Export HTML to directory"));
	fd.setMode (QFileDialog::DirectoryOnly);
	fd.setCaption(tr("VYM - Export HTML to directory"));
	fd.setModal (true);
	fd.show();

	if ( fd.exec() == QDialog::Accepted )
	{
		dir=fd.selectedFile();
		lineEditDir->setText (dir );
		settingsChanged=true;
	}
}

void ExportXHTMLDialog::imageButtonPressed(bool b)
{
	useImage=b;
	settingsChanged=true;
}

void ExportXHTMLDialog::textcolorButtonPressed(bool b)
{
	useTextColor=b;	
	settingsChanged=true;
}

void ExportXHTMLDialog::saveSettingsInMapButtonPressed(bool b)
{
	saveSettingsInMap=b;	
	settingsChanged=true;
}

void ExportXHTMLDialog::warningsButtonPressed(bool b)
{
	showWarnings=b;
	settingsChanged=true;
}


void ExportXHTMLDialog::outputButtonPressed(bool b)
{
	showOutput=b;
	settingsChanged=true;
}

void ExportXHTMLDialog::cssChanged()
{
	css=lineEditCSS->text();
	settingsChanged=true;
}

void ExportXHTMLDialog::browseCSSPressed()
{
   	QFileDialog fd( this, tr("VYM - Path to CSS file"));
	fd.setModal (true);
	fd.addFilter ("Cascading Stylesheet (*.css)");
	fd.show();

	if ( fd.exec() == QDialog::Accepted )
	{
		css=fd.selectedFile();
		lineEditCSS->setText (css );
		settingsChanged=true;
	}
}

void ExportXHTMLDialog::xslChanged()
{
	xsl=lineEditXSL->text();
	settingsChanged=true;
}

void ExportXHTMLDialog::prescriptChanged()
{
	prescript=lineEditPreScript->text();
	settingsChanged=true;
}

void ExportXHTMLDialog::browseXSLPressed()
{
   	QFileDialog fd( this, tr("VYM - Path to XSL file"));
	fd.setModal (true);
	fd.addFilter ("Extensible Stylesheet Language (*.xsl)");
	fd.show();

	if ( fd.exec() == QDialog::Accepted )
	{
		xsl=fd.selectedFile();
		lineEditXSL->setText (xsl );
		settingsChanged=true;
	}
}

void ExportXHTMLDialog::postscriptChanged()
{
	postscript=lineEditPostScript->text();
	settingsChanged=true;
}

void ExportXHTMLDialog::browsePreExportButtonPressed()
{
	QFileDialog fd( this, tr("VYM - Path to pre export script"));
	fd.setModal (true);
	fd.addFilter ("Scripts (*.sh *.pl *.py *.php)");
	fd.show();

	if ( fd.exec() == QDialog::Accepted )
	{
		prescript=fd.selectedFile();
		lineEditPreScript->setText (prescript );
		settingsChanged=true;
	}

}

void ExportXHTMLDialog::browsePostExportButtonPressed()
{
	QFileDialog fd( this, tr("VYM - Path to post export script"));
	fd.setModal (true);
	fd.addFilter ("Scripts (*.sh *.pl *.py *.php)");
	fd.show();

	if ( fd.exec() == QDialog::Accepted )
	{
		postscript=fd.selectedFile();
		lineEditPostScript->setText (postscript );
		settingsChanged=true;
	}
}


void ExportXHTMLDialog::doExport (const QString &mapname)
{
	// Save options to settings file 
	// (but don't save at destructor, which
	// is called for "cancel", too)
	settings.setLocalEntry (filepath,"/vym/export/xhtml/exportDir",dir);
	settings.setLocalEntry (filepath,"/vym/export/xhtml/prescript",prescript);
	settings.setLocalEntry (filepath,"/vym/export/xhtml/postscript",postscript);

    if (useImage)
		settings.setLocalEntry (filepath,"/vym/export/xhtml/useImage","yes");
    else
		settings.setLocalEntry (filepath,"/vym/export/xhtml/useImage","no");	
	
  if (useTextColor)
		settings.setLocalEntry (filepath,"/vym/export/xhtml/useTextColor","yes");
    else
		settings.setLocalEntry (filepath,"/vym/export/xhtml/useTextColor","no");	
	
   if (showWarnings)
		settings.writeEntry ("/vym/export/xhtml/showWarnings","yes");
    else
		settings.writeEntry ("/vym/export/xhtml/showWarnings","no");	
			
	if (showOutput)
		settings.writeEntry ("/vym/export/xhtml/showOutput","yes");
	else
		settings.writeEntry ("/vym/export/xhtml/showOutput","no");	

	QString ipath;	
	ipath=vymBaseDir.path()+"/flags/flag-url-16x16.png";
	if (!options.isOn ("local"))
	{
		settings.setLocalEntry 
			(filepath,"/vym/export/xhtml/xsl",xsl);
		settings.setLocalEntry 
			(filepath,"/vym/export/xhtml/css",css);	
	}

	// Provide a smaller URL-icon to improve Layout
	QPixmap pm;
	if (!pm.load(ipath,"PNG") )
		QMessageBox::warning( 0, tr( "Warning" ),tr("Could not open %1").arg(ipath));
		
		
	if(!pm.save (dir + "flags/flag-url-16x16.png","PNG"))
		QMessageBox::warning( 0, tr( "Warning" ),tr("Could not write %1").arg(ipath));
	if (!saveSettingsInMap)
		settings.clearLocal("/vym/export/xhtml");
	else	
		settings.setLocalEntry 
			(filepath,"/vym/export/xhtml/saveSettingsInMap","yes");

	// Copy CSS file
	QFile css_src (css);
	QFile css_dst (dir+"vym.css");
	if (!css_src.open ( IO_ReadOnly))
		QMessageBox::warning( 0, tr( "Warning" ),+tr("Could not open %1").arg(css));
	else
	{
		if (!css_dst.open( IO_WriteOnly))
			QMessageBox::warning( 0, tr( "Warning" ), tr("Could not open %1").arg(dir+"vym.css"));
		else
		{	
		
			QTextStream tsout( &css_dst);
			QTextStream tsin ( &css_src);
			QString s= tsin.read();
			tsout << s;
			css_dst.close();
		}	
		css_src.close();
	}

	if (!prescript.isEmpty()) runScript (prescript,dir+mapname+".xml");
	
	if (useImage)
		p.addStringParam ("imagemap","images/"+mapname+".png");
	if (useTextColor)
		p.addStringParam ("use.textcolor","1");
	p.addStringParam ("mapname",mapname+".vym");
	
	p.setOutputFile (dir+mapname+".html");
	p.setInputFile (dir+mapname+".xml");
	p.setXSLFile (xsl);
	p.process();

	if (!postscript.isEmpty()) runScript (postscript,dir+mapname+".html");

}

void ExportXHTMLDialog::setFilePath(const QString &s)
{
	filepath=s;
}

void ExportXHTMLDialog::setMapName(const QString &s)
{
	mapname=s;
}

QString ExportXHTMLDialog::getDir()
{
	return dir;
}

bool ExportXHTMLDialog::warnings()
{
	return showWarnings;
}

bool ExportXHTMLDialog::hasChanged()
{
	return settingsChanged;
}


void ExportXHTMLDialog::runScript(QString spath, QString fpath)
{
	spath.replace ("%f",fpath);
	QStringList args=QStringList::split (' ',spath,false);
		
	scriptProc->clearArguments();
	scriptProc->setArguments (args);	
	p.addOutput ("vym is executing: \n" + scriptProc->arguments().join(" ") );	
	if (!scriptProc->start() )
	{
		QMessageBox::critical( 0, tr( "Critical Error" ),
					   tr("Could not start %1").arg(spath) );
	} else
	{
		scriptProc->waitFinished();
		if (!scriptProc->normalExit() )
			QMessageBox::critical( 0, tr( "Critical Error" ),
			   tr("%1 didn't exit normally").arg(spath) +
			   scriptProc->getErrout() );
		else
			if (scriptProc->exitStatus()>0) showOutput=true;
			
	}	
	p.addOutput ("\n");
	p.addOutput (scriptProc->getErrout());
	p.addOutput (scriptProc->getStdout());
}
