/*
 * infowidget.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 "infowidget.h"
#include "linklocator.h"

#include <kdialog.h>
#include <kglobalsettings.h>
#include <klocale.h>
#include <krun.h>
#include <ktextbrowser.h>
#include <kurllabel.h>

#include <qlayout.h>
#include <qstringlist.h>

using namespace Barry;

InfoWidget::InfoWidget( QWidget *parent )
	: QWidget( parent, "InfoWidget" )
{
	QGridLayout *layout = new QGridLayout( this );
	layout->setMargin( KDialog::marginHint() );
	layout->setSpacing( KDialog::spacingHint() );
	
	QLabel *packageName = new QLabel( i18n( "Package name:" ), this );
	layout->addWidget( packageName, 0, 0 );
	m_packageName = new QLabel( this );
	layout->addWidget( m_packageName, 0, 1 );

	QLabel *version = new QLabel( i18n( "Version:" ), this );
	layout->addWidget( version, 1, 0 );
	m_version = new QLabel( this );
	layout->addWidget( m_version, 1, 1 );

	lCategories = new QLabel( i18n( "Categories:" ), this );
	layout->addWidget( lCategories, 2, 0 );
	m_categories = new QLabel( this );
	layout->addWidget( m_categories, 2, 1 );

	QLabel *maintainer = new QLabel( i18n( "Maintainer:" ), this );
	layout->addWidget( maintainer, 3, 0 );
	m_maintainer = new KURLLabel( QString::null, QString::null, this );
	connect( m_maintainer, SIGNAL( leftClickedURL( const QString & ) ),
	         this, SLOT( openURL( const QString & ) ) );
	layout->addWidget( m_maintainer, 3, 1 );
	
	QLabel *comment = new QLabel( i18n( "Comment" ), this );
	layout->addWidget( comment, 4, 0, AlignTop );
	m_comment = new QLabel( this );
	layout->addWidget( m_comment, 4, 1 );
	
	QLabel *description = new QLabel( i18n( "Description" ), this );
	layout->addWidget( description, 5, 0, AlignTop );
	m_description = new KTextBrowser( this );
	m_description->setReadOnly( true );
	m_description->setFont( KGlobalSettings::fixedFont() );
	m_description->setTextFormat( QTextEdit::RichText );
	const QFontMetrics fm( m_description->font() );
	m_description->setMaximumWidth( fm.width( 'X' ) * 80 );
	layout->addWidget( m_description, 5, 1 );
}

void InfoWidget::setPort( const Port &port )
{
	if ( m_port == port )
		return;
	m_port = port;

	m_packageName->setText( port.name().stripWhiteSpace() );

	if ( port.installed() )
		m_version->setText( i18n( "%1 (installed version: %1)" )
		                    .arg( port.version().stripWhiteSpace() )
		                    .arg( port.installedVersion() ) );
	else
		m_version->setText( port.version().stripWhiteSpace() );

	if ( port.categories().count() == 1 )
		lCategories->setText( i18n( "Category:" ) );
	else
		lCategories->setText( i18n( "Categories:" ) );

	m_categories->setText( port.categories().join( QString::fromLatin1( ", " ) ).stripWhiteSpace() );
	m_maintainer->setText( port.maintainer().stripWhiteSpace() );
	m_maintainer->setURL( QString::fromLatin1( "mailto:" ) + m_maintainer->text() );
	m_comment->setText( port.comment().stripWhiteSpace() );

	QString d = port.description().stripWhiteSpace();
	m_description->setText( LinkLocator::convertToHtml( d, true ) );
}

void InfoWidget::openURL( const QString &url )
{
	new KRun( url );
}

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