/*
 * splashwidget.cc
 *
 * Copyright (c) 2002, 2003 Frerich Raabe <raabe@kde.org>
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. For licensing and distribution details, check the
 * accompanying file 'COPYING'.
 */
#include "splashwidget.h"

#include <kaboutdata.h>
#include <kapplication.h>
#include <kstandarddirs.h>

#include <qfileinfo.h>

using namespace Barry;

SplashWidget::SplashWidget( QWidget *parent, const char *name )
	: KHTMLPart( parent, name )
{
	setOnlyLocalReferences( true );
	setJScriptEnabled( false );
	setJavaEnabled( false );
	setPluginsEnabled( false );

	if ( !loadStyleSheet() ) {
		showErrorMessage( i18n( "<b>Failed to load the splash screen stylesheet file!</b><br/><br/>Please check your installation." ) );
		return;
	}

	if ( !showSplashScreen() ) {
		showErrorMessage( i18n( "<b>Failed to load the splash screen data files!</b><br/><br/>Please check your installation." ) );
		return;
	}
}

bool SplashWidget::loadStyleSheet()
{
	QString css = langLookup( QString::fromLatin1( "common/kde-default.css" ) );
	if ( css.isNull() )
		return false;

	QFile f( css );
	if ( !f.open( IO_ReadOnly ) )
		return false;

	QTextStream stream( &f );
	preloadStyleSheet( QString::fromLatin1( "help:/common/kde-default.css" ),
	                   stream.read() );

	return true;
}

bool SplashWidget::showSplashScreen()
{
	QString fileName = ::locate( "appdata",
	                             QString::fromLatin1( "splashscreen.html" ) );
	if ( fileName.isNull() )
		return false;

	QFile htmlFile( fileName );
	if ( !htmlFile.open( IO_ReadOnly ) )
		return false;

	QTextStream htmlStream( &htmlFile );
	QString header = i18n( "Welcome to %1 v%2" )
	                 .arg( kapp->aboutData()->programName() )
	                 .arg( kapp->aboutData()->version( ) );
	QString htmlMarkup = htmlStream.read()
		.arg( header )
		.arg( i18n( "Please select a port in the list at the left or " \
		            "search for one using the search field in the lower " \
		            "left corner (or by using the <a href=\"exec:search\">" \
		            "Search dialog</a> accessible through the menu bar)." ) );

	begin( fileName );
	write( htmlMarkup );
	end();

	return true;
}

void SplashWidget::showErrorMessage( const QString &msg )
{
	begin();
	write( i18n("<html><head><title>Error!</title></head><body><h1>Error</h1>" ));
	write( msg );
	write( "</body></html>" );
}

QString SplashWidget::langLookup( const QString &fileName ) const
{
	QStringList searchPaths;
	
	QStringList localDocs = KGlobal::dirs()->resourceDirs( "html" );
	QStringList::ConstIterator docIt = localDocs.begin();
	QStringList::ConstIterator docEnd = localDocs.end();
	for ( ; docIt != docEnd; ++docIt ) {
		QStringList langs = KGlobal::locale()->languageList();
		langs.append( QString::fromLatin1( "en" ) );
		langs.remove( QString::fromLatin1( "C" ) );
		QStringList::ConstIterator langIt = langs.begin();
		QStringList::ConstIterator langEnd = langs.end();
		for ( ; langIt != langEnd; ++langIt )
			searchPaths += QString( QString::fromLatin1( "%1%2/%3" ) )
			               .arg( *docIt )
			               .arg( *langIt )
			               .arg( fileName );
	}

	QStringList::ConstIterator it = searchPaths.begin();
	QStringList::ConstIterator end = searchPaths.end();
	for ( ; it != end; ++it ) {
		QFileInfo fi( *it );
		if ( fi.exists() && fi.isFile() && fi.isReadable() )
			return *it;
	}

	return QString::null;
}

// vim:ts=4:sw=4:noet:list
#include "splashwidget.moc"
