/***************************************************************************
    begin                : Sun Jul 29 2001
    copyright            : (C) 2001 - 2003 by Brachet Pascal, (C) 2004 by Jeroen Wijnhout
    email                : Jeroen.Wijnhout@kdemail.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 "docpart.h"

#include <tdeconfig.h>
#include <kstdaction.h>
#include <tdeglobal.h>
#include "kiledebug.h"
#include <kstandarddirs.h>
#include <kmimemagic.h>
#include <kmimetype.h>
#include <ktrader.h>
#include <krun.h>
#include <tdehtml_part.h>
#include <tdehtml_settings.h>

DocumentationViewer::DocumentationViewer(TQWidget *parent, const char *name ) : TDEHTMLPart(parent,name, 0, 0, BrowserViewGUI)
{
	m_hpos = 0;
	TDEConfig konqConfig("konquerorrc");
	konqConfig.setGroup("HTML Settings");
	//const TDEHTMLSettings * set = settings();
	//( const_cast<TDEHTMLSettings *>(set) )->init( &konqConfig, false );
	TQString rc = TDEGlobal::dirs()->findResource("appdata", "docpartui.rc");
	setXMLFile(rc);
	(void) KStdAction::back(this, TQ_SLOT(back()), actionCollection(),"Back" );
	(void) KStdAction::forward(this, TQ_SLOT(forward()), actionCollection(),"Forward" );
	(void) KStdAction::home(this, TQ_SLOT(home()), actionCollection(),"Home" );
}

DocumentationViewer::~DocumentationViewer() {}

void DocumentationViewer::urlSelected(const TQString &url, int button, int state,const TQString & target, KParts::URLArgs args)
{
	KURL cURL = completeURL(url);
	TQString mime = KMimeType::findByURL(cURL).data()->name();

	//load this URL in the embedded viewer if TDEHTML can handle it, or when mimetype detection failed
	KService::Ptr service = KService::serviceByDesktopName("tdehtml");
	if ( ( mime == KMimeType::defaultMimeType() ) || (service && service->hasServiceType(mime)) )
	{
		TDEHTMLPart::urlSelected(url, button, state, target, args);
		openURL(cURL) ;
		addToHistory(cURL.url());
	}
	//TDEHTML can't handle it, look for an appropriate application
	else
	{
		TDETrader::OfferList offers = TDETrader::self()->query(mime, "Type == 'Application'");
		KService::Ptr ptr = offers.first();
		KURL::List lst;
		lst.append(cURL);
		if (ptr) KRun::run(*ptr, lst);
	}
}

void DocumentationViewer::home()
{
	if ( !m_history.isEmpty() ) openURL( KURL(m_history.first()) );
}

void DocumentationViewer::forward()
{
	if ( forwardEnable() ) 
	{
		++m_hpos;
		openURL( KURL( m_history[m_hpos]) );
		emit updateStatus( backEnable() , forwardEnable() );
	}
}


void DocumentationViewer::back()
{
	if ( backEnable() ) {
		--m_hpos;
		openURL( KURL(m_history[m_hpos]) );
		emit updateStatus( backEnable() , forwardEnable() );
	}
}


void DocumentationViewer::addToHistory( const TQString & url )
{
	if ( m_history.count() > 0 )
	{
		while ( m_hpos < m_history.count()-1  )
				m_history.pop_back();
	}
	
	if ( !m_history.isEmpty() ) ++m_hpos;
	
	m_history.append(url);
	
	m_hpos = m_history.count()-1;
	emit updateStatus( backEnable() , forwardEnable() );
}


bool DocumentationViewer::backEnable()
{
	return m_hpos > 0;
}


bool DocumentationViewer::forwardEnable()
{
	return m_hpos < m_history.count()-1;
}

#include "docpart.moc"
