/***************************************************************************
 *   Copyright (C) 2007 by kakapapa@nate.com                               *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   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.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "textminibrowser.h"
#include <kdebug.h>

TextMiniBrowser::TextMiniBrowser( QWidget * parent, const char * name )
 : QTextEdit( parent, name )
{
	setReadOnly( TRUE );
	viewport()->setMouseTracking( TRUE );
}


TextMiniBrowser::~TextMiniBrowser()
{
}

void TextMiniBrowser::emitLinkClicked( const QString &s )
{
#ifdef NETDEBUG
	kdDebug() << "Clicked : [" << s << "]" << endl;
#endif
	// KRun::runURL( s, "text/html");

	QString sR(s);
	// sR.replace("mailto:", "");
	/*! mailto: 를 처리하기 위해 new KRun 으로 바꿈. */
	new KRun( sR ); /// 이거 누수 안생기나?
}

void TextMiniBrowser::append( const QString& text )
{
	QString sNewText = text;

#ifdef NETDEBUG
	kdDebug() << "Orig : [" << sNewText << "]" << endl;
#endif
	/*! http://, https://, ftp://, mms:// 로 시작하는 URL 처리 */
	sNewText.replace( QRegExp("\\b((http://|https://|ftp://|mms://)[\\w\\#$%&~/.\\-;:=,?@\\[\\]+]*)\\b"), "<a href=\"\\1\">\\1</a>");
	/*! 메일주소 처리 */
	sNewText.replace( QRegExp("\\b([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4})\\b"), "<a href=\"mailto:\\1\">\\1</a>");
	/*! www 로 시작하는 URL 처리 */
	sNewText.replace( QRegExp("\\b(www.[a-zA-Z0-9.-]+)\\b"), "<a href=\"http://\\1\">\\1</a>");
	// sNewText.replace( QRegExp("\\b([^<(]+@[^>)]+)\\b"), "<a href=\"mailto:\\1\">\\1</a>");
	// sNewText.replace( QRegExp("\\b([^;&<( ]+@[^;&>) ]+)\\b"), "<a href=\"mailto:\\1\">\\1</a>");
	QTextEdit::append(sNewText);
#ifdef NETDEBUG
	kdDebug() << "New  : [" << sNewText << "]" << endl;
#endif
}

#include "textminibrowser.moc"
