/***************************************************************************
 *   Copyright (C) 2005 by yunfan                                          *
 *   yunfan_zg@163.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 "evahtmlparser.h"

#include <qregexp.h>

void EvaHtmlParser::convertToHtml(QString &txt, bool isURLOn, bool isAbsImgPath)
{
	//QRegExp rx1("&(?!amp;)");
	//txt.replace(rx1, "&amp;");
	// FIXME  by this way, some html code may not be displayed properly
	txt.replace("&amp;", "&amp;amp;");
	txt.replace("&lt;", "&lt;lt;");
	txt.replace("&gt;", "&gt;gt;");
	txt.replace("&quot;", "&quot;quot;");
	
	txt.replace("<", "&lt;");
	txt.replace(">", "&gt;");
	txt.replace("\"", "&quot;");
	txt.replace("\n", "<br>");
		
	QRegExp rx2("(/[a-z][a-z]+)");
	int pos=0;
	QString smiley;
	int fileIndex;
	QString img;
	while(pos>=0){
		pos = rx2.search(txt, pos);
		if (pos > -1) {
			smiley = rx2.cap(0);
			smiley.stripWhiteSpace();
			fileIndex = util.textToFileIndex(smiley.ascii());
			if(fileIndex!=-1){
				img = "<img src=\""+ (isAbsImgPath?(absImagePath+"/"):"") + QString::number(fileIndex)+".gif\">";
				txt.replace(pos, smiley.length(), img);
				pos += img.length();
			}else
				pos += smiley.length();
		}
	}

	if(isURLOn){
		QString email;
		QString wraped;
		QString aRefBegin = "<a href=\"";
		QString aRefEnd = "</a>";
		QString emailTag = "mailto:";
		
		QRegExp rx3("\\b[a-z0-9._%-]+@[a-z0-9._%-]+\\.[a-z]{2,4}\\b");
		rx3.setCaseSensitive(false);
		pos = 0;
		while(pos>=0){
			pos = rx3.search(txt, pos);
			if (pos > -1) {
				email = rx3.cap(0);			
				email.stripWhiteSpace();
				wraped = aRefBegin + emailTag + email + "\">" + email + aRefEnd;
				txt.replace(email, wraped);
				pos += strlen(wraped.ascii());
			}
		}
		QString url;
		QRegExp rx4("(http|ftp)://[a-z0-9._%-]+\\.[a-z0-9._%\\-]+\\.[a-z]{2,4}[a-z0-9._%\\-/\\?=&]*");
		rx4.setCaseSensitive(false);
		pos = 0;
		while(pos>=0){
			pos = rx4.search(txt, pos);
			if (pos > -1) {
				url = rx4.cap(0);
				url.stripWhiteSpace();
				wraped = aRefBegin + url + "\">" + url + aRefEnd;
				txt.replace(url, wraped);
				pos += strlen(wraped.ascii());
			}
		}
	}
}

void EvaHtmlParser::convertToPlainTxt(QString &html)
{
	QRegExp rx("<img src=\\d\\d?\\.gif >");
	QRegExp rx1("\\d\\d?");
	int pos=0;
	QString imgTxt, strFile, smiley;
	int fileIndex, len;
	while(pos>=0){
		pos = rx.search(html, pos);
		if (pos > -1) {
			imgTxt = rx.cap(0);
			len = rx.matchedLength();
			int p=0;
			p = rx1.search(imgTxt, 0);
			if(p>-1) 
				strFile = rx1.cap(0);
			fileIndex = strFile.toInt();
			if(fileIndex<0 || fileIndex > 95) {
				pos += len;
				continue;
			}
			smiley = QString(util.fileIndexToText(fileIndex).c_str()) + " ";
			html.replace(imgTxt, smiley);
			pos += smiley.length();
		}
	}
	QString start = "<p>";
	pos = html.find(start, 0);
	if(pos!= -1){
		html = html.right(html.length() - pos - start.length());
	}
	html.replace("<br /></p>\n</body></html>", "");
	html.replace("</p>\n</body></html>", "");
	html.replace("\n","");
	html.replace("<br>", "\n");
	html.replace("<p>", "");
	html.replace("<br />","\n");
	html.replace("</p>", "\n");
	html.replace("&amp;", "&");
	html.replace("&lt;", "<");
	html.replace("&gt;", ">");
	html.replace("&quot;", "\"");
	
}

