You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
467 lines
14 KiB
467 lines
14 KiB
15 years ago
|
/**
|
||
|
* linklocator.cpp
|
||
|
*
|
||
|
* Copyright (c) 2002 Dave Corrie <kde@davecorrie.com>
|
||
|
*
|
||
|
* This file is part of KMail.
|
||
|
*
|
||
|
* KMail 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||
|
*/
|
||
|
|
||
|
#include "linklocator.h"
|
||
|
#include "pimemoticons.h"
|
||
|
#include <kglobal.h>
|
||
|
#include <kstandarddirs.h>
|
||
|
#include <kstaticdeleter.h>
|
||
|
#include <kmdcodec.h>
|
||
|
#include <kdebug.h>
|
||
|
|
||
15 years ago
|
#include <tqstylesheet.h>
|
||
|
#include <tqfile.h>
|
||
|
#include <tqregexp.h>
|
||
15 years ago
|
|
||
|
#include <limits.h>
|
||
|
|
||
15 years ago
|
TQMap<TQString, TQString> *LinkLocator::s_smileyEmoticonNameMap = 0;
|
||
|
TQMap<TQString, TQString> *LinkLocator::s_smileyEmoticonHTMLCache = 0;
|
||
15 years ago
|
|
||
15 years ago
|
static KStaticDeleter< TQMap<TQString, TQString> > smileyMapDeleter;
|
||
|
static KStaticDeleter< TQMap<TQString, TQString> > smileyCacheDeleter;
|
||
15 years ago
|
|
||
15 years ago
|
LinkLocator::LinkLocator(const TQString& text, int pos)
|
||
15 years ago
|
: mText(text), mPos(pos), mMaxUrlLen(4096), mMaxAddressLen(255)
|
||
|
{
|
||
|
// If you change either of the above values for maxUrlLen or
|
||
|
// maxAddressLen, then please also update the documentation for
|
||
|
// setMaxUrlLen()/setMaxAddressLen() in the header file AND the
|
||
|
// default values used for the maxUrlLen/maxAddressLen parameters
|
||
|
// of convertToHtml().
|
||
|
|
||
|
if ( !s_smileyEmoticonNameMap ) {
|
||
|
smileyMapDeleter.setObject( s_smileyEmoticonNameMap,
|
||
15 years ago
|
new TQMap<TQString, TQString>() );
|
||
15 years ago
|
for ( int i = 0; i < EmotIcons::EnumSindex::COUNT; ++i ) {
|
||
15 years ago
|
TQString imageName( EmotIcons::EnumSindex::enumToString[i] );
|
||
15 years ago
|
imageName.truncate( imageName.length() - 2 ); //remove the _0 bit
|
||
|
s_smileyEmoticonNameMap->insert( EmotIcons::smiley(i), imageName );
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if ( !s_smileyEmoticonHTMLCache )
|
||
|
smileyCacheDeleter.setObject( s_smileyEmoticonHTMLCache,
|
||
15 years ago
|
new TQMap<TQString, TQString>() );
|
||
15 years ago
|
}
|
||
|
|
||
|
void LinkLocator::setMaxUrlLen(int length)
|
||
|
{
|
||
|
mMaxUrlLen = length;
|
||
|
}
|
||
|
|
||
|
int LinkLocator::maxUrlLen() const
|
||
|
{
|
||
|
return mMaxUrlLen;
|
||
|
}
|
||
|
|
||
|
void LinkLocator::setMaxAddressLen(int length)
|
||
|
{
|
||
|
mMaxAddressLen = length;
|
||
|
}
|
||
|
|
||
|
int LinkLocator::maxAddressLen() const
|
||
|
{
|
||
|
return mMaxAddressLen;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString LinkLocator::getUrl()
|
||
15 years ago
|
{
|
||
15 years ago
|
TQString url;
|
||
15 years ago
|
if(atUrl())
|
||
|
{
|
||
|
// handle cases like this: <link>http://foobar.org/</link>
|
||
|
int start = mPos;
|
||
|
while(mPos < (int)mText.length() && mText[mPos] > ' ' && mText[mPos] != '"' &&
|
||
13 years ago
|
TQString("<>()[]").find(mText[mPos]) == -1)
|
||
15 years ago
|
{
|
||
|
++mPos;
|
||
|
}
|
||
|
/* some URLs really end with: # / & - _ */
|
||
15 years ago
|
const TQString allowedSpecialChars = TQString("#/&-_");
|
||
15 years ago
|
while(mPos > start && mText[mPos-1].isPunct() &&
|
||
13 years ago
|
allowedSpecialChars.find(mText[mPos-1]) == -1 )
|
||
15 years ago
|
{
|
||
|
--mPos;
|
||
|
}
|
||
|
|
||
|
url = mText.mid(start, mPos - start);
|
||
|
if(isEmptyUrl(url) || mPos - start > maxUrlLen())
|
||
|
{
|
||
|
mPos = start;
|
||
|
url = "";
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
--mPos;
|
||
|
}
|
||
|
}
|
||
|
return url;
|
||
|
}
|
||
|
|
||
|
// keep this in sync with KMMainWin::slotUrlClicked()
|
||
|
bool LinkLocator::atUrl() const
|
||
|
{
|
||
|
// the following characters are allowed in a dot-atom (RFC 2822):
|
||
|
// a-z A-Z 0-9 . ! # $ % & ' * + - / = ? ^ _ ` { | } ~
|
||
15 years ago
|
const TQString allowedSpecialChars = TQString(".!#$%&'*+-/=?^_`{|}~");
|
||
15 years ago
|
|
||
|
// the character directly before the URL must not be a letter, a number or
|
||
|
// any other character allowed in a dot-atom (RFC 2822).
|
||
|
if( ( mPos > 0 ) && ( mText[mPos-1].isLetterOrNumber() ||
|
||
13 years ago
|
( allowedSpecialChars.find( mText[mPos-1] ) != -1 ) ) )
|
||
15 years ago
|
return false;
|
||
|
|
||
15 years ago
|
TQChar ch = mText[mPos];
|
||
15 years ago
|
return (ch=='h' && ( mText.mid(mPos, 7) == "http://" ||
|
||
|
mText.mid(mPos, 8) == "https://") ) ||
|
||
|
(ch=='v' && mText.mid(mPos, 6) == "vnc://") ||
|
||
|
(ch=='f' && ( mText.mid(mPos, 7) == "fish://" ||
|
||
|
mText.mid(mPos, 6) == "ftp://" ||
|
||
|
mText.mid(mPos, 7) == "ftps://") ) ||
|
||
|
(ch=='s' && ( mText.mid(mPos, 7) == "sftp://" ||
|
||
|
mText.mid(mPos, 6) == "smb://") ) ||
|
||
|
(ch=='m' && mText.mid(mPos, 7) == "mailto:") ||
|
||
|
(ch=='w' && mText.mid(mPos, 4) == "www.") ||
|
||
|
(ch=='f' && mText.mid(mPos, 4) == "ftp.") ||
|
||
|
(ch=='n' && mText.mid(mPos, 5) == "news:");
|
||
|
// note: no "file:" for security reasons
|
||
|
}
|
||
|
|
||
15 years ago
|
bool LinkLocator::isEmptyUrl(const TQString& url)
|
||
15 years ago
|
{
|
||
|
return url.isEmpty() ||
|
||
|
url == "http://" ||
|
||
|
url == "https://" ||
|
||
|
url == "fish://" ||
|
||
|
url == "ftp://" ||
|
||
|
url == "ftps://" ||
|
||
|
url == "sftp://" ||
|
||
|
url == "smb://" ||
|
||
|
url == "vnc://" ||
|
||
|
url == "mailto" ||
|
||
|
url == "www" ||
|
||
|
url == "ftp" ||
|
||
|
url == "news" ||
|
||
|
url == "news://";
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString LinkLocator::getEmailAddress()
|
||
15 years ago
|
{
|
||
15 years ago
|
TQString address;
|
||
15 years ago
|
|
||
|
if ( mText[mPos] == '@' ) {
|
||
|
// the following characters are allowed in a dot-atom (RFC 2822):
|
||
|
// a-z A-Z 0-9 . ! # $ % & ' * + - / = ? ^ _ ` { | } ~
|
||
15 years ago
|
const TQString allowedSpecialChars = TQString(".!#$%&'*+-/=?^_`{|}~");
|
||
15 years ago
|
|
||
|
// determine the local part of the email address
|
||
|
int start = mPos - 1;
|
||
14 years ago
|
while ( start >= 0 && mText[start].tqunicode() < 128 &&
|
||
15 years ago
|
( mText[start].isLetterOrNumber() ||
|
||
|
mText[start] == '@' || // allow @ to find invalid email addresses
|
||
13 years ago
|
allowedSpecialChars.find( mText[start] ) != -1 ) ) {
|
||
15 years ago
|
if ( mText[start] == '@' )
|
||
13 years ago
|
return TQString(); // local part contains '@' -> no email address
|
||
15 years ago
|
--start;
|
||
|
}
|
||
|
++start;
|
||
|
// we assume that an email address starts with a letter or a digit
|
||
|
while ( ( start < mPos ) && !mText[start].isLetterOrNumber() )
|
||
|
++start;
|
||
|
if ( start == mPos )
|
||
15 years ago
|
return TQString(); // local part is empty -> no email address
|
||
15 years ago
|
|
||
|
// determine the domain part of the email address
|
||
|
int dotPos = INT_MAX;
|
||
|
int end = mPos + 1;
|
||
|
while ( end < (int)mText.length() &&
|
||
|
( mText[end].isLetterOrNumber() ||
|
||
|
mText[end] == '@' || // allow @ to find invalid email addresses
|
||
|
mText[end] == '.' ||
|
||
|
mText[end] == '-' ) ) {
|
||
|
if ( mText[end] == '@' )
|
||
13 years ago
|
return TQString(); // domain part contains '@' -> no email address
|
||
15 years ago
|
if ( mText[end] == '.' )
|
||
14 years ago
|
dotPos = TQMIN( dotPos, end ); // remember index of first dot in domain
|
||
15 years ago
|
++end;
|
||
|
}
|
||
|
// we assume that an email address ends with a letter or a digit
|
||
|
while ( ( end > mPos ) && !mText[end - 1].isLetterOrNumber() )
|
||
|
--end;
|
||
|
if ( end == mPos )
|
||
15 years ago
|
return TQString(); // domain part is empty -> no email address
|
||
15 years ago
|
if ( dotPos >= end )
|
||
15 years ago
|
return TQString(); // domain part doesn't contain a dot
|
||
15 years ago
|
|
||
|
if ( end - start > maxAddressLen() )
|
||
15 years ago
|
return TQString(); // too long -> most likely no email address
|
||
15 years ago
|
address = mText.mid( start, end - start );
|
||
|
|
||
|
mPos = end - 1;
|
||
|
}
|
||
|
return address;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString LinkLocator::convertToHtml(const TQString& plainText, int flags,
|
||
15 years ago
|
int maxUrlLen, int maxAddressLen)
|
||
|
{
|
||
|
LinkLocator locator(plainText);
|
||
|
locator.setMaxUrlLen(maxUrlLen);
|
||
|
locator.setMaxAddressLen(maxAddressLen);
|
||
|
|
||
15 years ago
|
TQString str;
|
||
|
TQString result((TQChar*)0, (int)locator.mText.length() * 2);
|
||
|
TQChar ch;
|
||
15 years ago
|
int x;
|
||
|
bool startOfLine = true;
|
||
15 years ago
|
TQString emoticon;
|
||
15 years ago
|
|
||
|
for (locator.mPos = 0, x = 0; locator.mPos < (int)locator.mText.length(); locator.mPos++, x++)
|
||
|
{
|
||
|
ch = locator.mText[locator.mPos];
|
||
|
if ( flags & PreserveSpaces )
|
||
|
{
|
||
|
if (ch==' ')
|
||
|
{
|
||
|
if (startOfLine) {
|
||
|
result += " ";
|
||
|
locator.mPos++, x++;
|
||
|
startOfLine = false;
|
||
|
}
|
||
|
while (locator.mText[locator.mPos] == ' ')
|
||
|
{
|
||
|
result += " ";
|
||
|
locator.mPos++, x++;
|
||
|
if (locator.mText[locator.mPos] == ' ') {
|
||
|
result += " ";
|
||
|
locator.mPos++, x++;
|
||
|
}
|
||
|
}
|
||
|
locator.mPos--, x--;
|
||
|
continue;
|
||
|
}
|
||
|
else if (ch=='\t')
|
||
|
{
|
||
|
do
|
||
|
{
|
||
|
result += " ";
|
||
|
x++;
|
||
|
}
|
||
|
while((x&7) != 0);
|
||
|
x--;
|
||
|
startOfLine = false;
|
||
|
continue;
|
||
|
}
|
||
|
}
|
||
|
if (ch=='\n')
|
||
|
{
|
||
|
result += "<br />";
|
||
|
startOfLine = true;
|
||
|
x = -1;
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
startOfLine = false;
|
||
|
if (ch=='&')
|
||
|
result += "&";
|
||
|
else if (ch=='"')
|
||
|
result += """;
|
||
|
else if (ch=='<')
|
||
|
result += "<";
|
||
|
else if (ch=='>')
|
||
|
result += ">";
|
||
|
else
|
||
|
{
|
||
|
const int start = locator.mPos;
|
||
|
if ( !(flags & IgnoreUrls) ) {
|
||
|
str = locator.getUrl();
|
||
|
if (!str.isEmpty())
|
||
|
{
|
||
15 years ago
|
TQString hyperlink;
|
||
15 years ago
|
if(str.left(4) == "www.")
|
||
|
hyperlink = "http://" + str;
|
||
|
else if(str.left(4) == "ftp.")
|
||
|
hyperlink = "ftp://" + str;
|
||
|
else
|
||
|
hyperlink = str;
|
||
|
|
||
13 years ago
|
str = str.replace('&', "&");
|
||
15 years ago
|
result += "<a href=\"" + hyperlink + "\">" + str + "</a>";
|
||
|
x += locator.mPos - start;
|
||
|
continue;
|
||
|
}
|
||
|
str = locator.getEmailAddress();
|
||
|
if(!str.isEmpty())
|
||
|
{
|
||
|
// len is the length of the local part
|
||
13 years ago
|
int len = str.find('@');
|
||
15 years ago
|
TQString localPart = str.left(len);
|
||
15 years ago
|
|
||
|
// remove the local part from the result (as '&'s have been expanded to
|
||
|
// & we have to take care of the 4 additional characters per '&')
|
||
13 years ago
|
result.truncate(result.length() - len - (localPart.contains('&')*4));
|
||
15 years ago
|
x -= len;
|
||
|
|
||
|
result += "<a href=\"mailto:" + str + "\">" + str + "</a>";
|
||
|
x += str.length() - 1;
|
||
|
continue;
|
||
|
}
|
||
|
}
|
||
|
if ( flags & ReplaceSmileys ) {
|
||
|
str = locator.getEmoticon();
|
||
|
if ( ! str.isEmpty() ) {
|
||
|
result += str;
|
||
|
x += locator.mPos - start;
|
||
|
continue;
|
||
|
}
|
||
|
}
|
||
|
if ( flags & HighlightText ) {
|
||
|
str = locator.highlightedText();
|
||
|
if ( !str.isEmpty() ) {
|
||
|
result += str;
|
||
|
x += locator.mPos - start;
|
||
|
continue;
|
||
|
}
|
||
|
}
|
||
|
result += ch;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString LinkLocator::pngToDataUrl( const TQString & iconPath )
|
||
15 years ago
|
{
|
||
|
if ( iconPath.isEmpty() )
|
||
14 years ago
|
return TQString();
|
||
15 years ago
|
|
||
15 years ago
|
TQFile pngFile( iconPath );
|
||
15 years ago
|
if ( !pngFile.open( IO_ReadOnly | IO_Raw ) )
|
||
14 years ago
|
return TQString();
|
||
15 years ago
|
|
||
15 years ago
|
TQByteArray ba = pngFile.readAll();
|
||
15 years ago
|
pngFile.close();
|
||
14 years ago
|
return TQString::tqfromLatin1("data:image/png;base64,%1")
|
||
14 years ago
|
.tqarg( KCodecs::base64Encode( ba ).data() );
|
||
15 years ago
|
}
|
||
|
|
||
|
|
||
15 years ago
|
TQString LinkLocator::getEmoticon()
|
||
15 years ago
|
{
|
||
|
// smileys have to be prepended by whitespace
|
||
|
if ( ( mPos > 0 ) && !mText[mPos-1].isSpace() )
|
||
14 years ago
|
return TQString();
|
||
15 years ago
|
|
||
|
// since smileys start with ':', ';', '(' or '8' short circuit method
|
||
15 years ago
|
const TQChar ch = mText[mPos];
|
||
15 years ago
|
if ( ch !=':' && ch != ';' && ch != '(' && ch != '8' )
|
||
14 years ago
|
return TQString();
|
||
15 years ago
|
|
||
|
// find the end of the smiley (a smiley is at most 4 chars long and ends at
|
||
|
// lineend or whitespace)
|
||
|
const int MinSmileyLen = 2;
|
||
|
const int MaxSmileyLen = 4;
|
||
|
int smileyLen = 1;
|
||
|
while ( ( smileyLen <= MaxSmileyLen ) &&
|
||
|
( mPos+smileyLen < (int)mText.length() ) &&
|
||
|
!mText[mPos+smileyLen].isSpace() )
|
||
|
smileyLen++;
|
||
|
if ( smileyLen < MinSmileyLen || smileyLen > MaxSmileyLen )
|
||
14 years ago
|
return TQString();
|
||
15 years ago
|
|
||
15 years ago
|
const TQString smiley = mText.mid( mPos, smileyLen );
|
||
13 years ago
|
if ( !s_smileyEmoticonNameMap->contains( smiley ) )
|
||
14 years ago
|
return TQString(); // that's not a (known) smiley
|
||
15 years ago
|
|
||
15 years ago
|
TQString htmlRep;
|
||
13 years ago
|
if ( s_smileyEmoticonHTMLCache->contains( smiley ) ) {
|
||
15 years ago
|
htmlRep = (*s_smileyEmoticonHTMLCache)[smiley];
|
||
|
}
|
||
|
else {
|
||
15 years ago
|
const TQString imageName = (*s_smileyEmoticonNameMap)[smiley];
|
||
15 years ago
|
|
||
|
#if KDE_IS_VERSION( 3, 3, 91 )
|
||
15 years ago
|
const TQString iconPath = locate( "emoticons",
|
||
15 years ago
|
EmotIcons::theme() +
|
||
14 years ago
|
TQString::tqfromLatin1( "/" ) +
|
||
|
imageName + TQString::tqfromLatin1(".png") );
|
||
15 years ago
|
#else
|
||
15 years ago
|
const TQString iconPath = locate( "data",
|
||
14 years ago
|
TQString::tqfromLatin1( "kopete/pics/emoticons/" )+
|
||
15 years ago
|
EmotIcons::theme() +
|
||
14 years ago
|
TQString::tqfromLatin1( "/" ) +
|
||
|
imageName + TQString::tqfromLatin1(".png") );
|
||
15 years ago
|
#endif
|
||
|
|
||
15 years ago
|
const TQString dataUrl = pngToDataUrl( iconPath );
|
||
15 years ago
|
if ( dataUrl.isEmpty() ) {
|
||
14 years ago
|
htmlRep = TQString();
|
||
15 years ago
|
}
|
||
|
else {
|
||
|
// create an image tag (the text in attribute alt is used
|
||
|
// for copy & paste) representing the smiley
|
||
15 years ago
|
htmlRep = TQString("<img class=\"pimsmileyimg\" src=\"%1\" "
|
||
15 years ago
|
"alt=\"%2\" title=\"%3\" width=\"16\" height=\"16\"/>")
|
||
14 years ago
|
.tqarg( dataUrl,
|
||
15 years ago
|
TQStyleSheet::escape( smiley ),
|
||
|
TQStyleSheet::escape( smiley ) );
|
||
15 years ago
|
}
|
||
|
s_smileyEmoticonHTMLCache->insert( smiley, htmlRep );
|
||
|
}
|
||
|
|
||
|
if ( !htmlRep.isEmpty() )
|
||
|
mPos += smileyLen - 1;
|
||
|
|
||
|
return htmlRep;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString LinkLocator::highlightedText()
|
||
15 years ago
|
{
|
||
|
// formating symbols must be prepended with a whitespace
|
||
|
if ( ( mPos > 0 ) && !mText[mPos-1].isSpace() )
|
||
14 years ago
|
return TQString();
|
||
15 years ago
|
|
||
15 years ago
|
const TQChar ch = mText[mPos];
|
||
15 years ago
|
if ( ch != '/' && ch != '*' && ch != '_' )
|
||
14 years ago
|
return TQString();
|
||
15 years ago
|
|
||
14 years ago
|
TQRegExp re = TQRegExp( TQString("\\%1([0-9A-Za-z]+)\\%2").tqarg( ch ).tqarg( ch ) );
|
||
15 years ago
|
if ( re.search( mText, mPos ) == mPos ) {
|
||
|
uint length = re.matchedLength();
|
||
|
// there must be a whitespace after the closing formating symbol
|
||
|
if ( mPos + length < mText.length() && !mText[mPos + length].isSpace() )
|
||
14 years ago
|
return TQString();
|
||
15 years ago
|
mPos += length - 1;
|
||
|
switch ( ch.latin1() ) {
|
||
|
case '*':
|
||
|
return "<b>" + re.cap( 1 ) + "</b>";
|
||
|
case '_':
|
||
|
return "<u>" + re.cap( 1 ) + "</u>";
|
||
|
case '/':
|
||
|
return "<i>" + re.cap( 1 ) + "</i>";
|
||
|
}
|
||
|
}
|
||
14 years ago
|
return TQString();
|
||
15 years ago
|
}
|
||
|
|