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.
221 lines
5.2 KiB
221 lines
5.2 KiB
15 years ago
|
/**
|
||
|
* spellingfilter.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 <kdebug.h>
|
||
|
#include "spellingfilter.h"
|
||
|
|
||
|
//-----------------------------------------------------------------------------
|
||
|
// SpellingFilter implementation
|
||
|
//
|
||
|
|
||
15 years ago
|
SpellingFilter::SpellingFilter(const TQString& text, const TQString& quotePrefix,
|
||
15 years ago
|
UrlFiltering filterUrls, EmailAddressFiltering filterEmailAddresses,
|
||
15 years ago
|
const TQStringList& filterStrings)
|
||
15 years ago
|
: mOriginal(text)
|
||
|
{
|
||
|
TextCensor c(text);
|
||
|
|
||
|
if(!quotePrefix.isEmpty())
|
||
|
c.censorQuotations(quotePrefix);
|
||
|
|
||
|
if(filterUrls)
|
||
|
c.censorUrls();
|
||
|
|
||
|
if(filterEmailAddresses)
|
||
|
c.censorEmailAddresses();
|
||
|
|
||
15 years ago
|
TQStringList::const_iterator iter = filterStrings.begin();
|
||
15 years ago
|
while(iter != filterStrings.end())
|
||
|
{
|
||
|
c.censorString(*iter);
|
||
|
++iter;
|
||
|
}
|
||
|
|
||
|
mFiltered = c.censoredText();
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString SpellingFilter::originalText() const
|
||
15 years ago
|
{
|
||
|
return mOriginal;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString SpellingFilter::filteredText() const
|
||
15 years ago
|
{
|
||
|
return mFiltered;
|
||
|
}
|
||
|
|
||
|
//-----------------------------------------------------------------------------
|
||
|
// SpellingFilter::TextCensor implementation
|
||
|
//
|
||
|
|
||
15 years ago
|
SpellingFilter::TextCensor::TextCensor(const TQString& s)
|
||
15 years ago
|
: LinkLocator(s)
|
||
|
{
|
||
|
|
||
|
}
|
||
|
|
||
15 years ago
|
void SpellingFilter::TextCensor::censorQuotations(const TQString& quotePrefix)
|
||
15 years ago
|
{
|
||
|
mPos = 0;
|
||
|
while(mPos < static_cast<int>(mText.length()))
|
||
|
{
|
||
|
// Find start of quotation
|
||
|
findQuotation(quotePrefix);
|
||
|
if(mPos < static_cast<int>(mText.length()))
|
||
|
{
|
||
|
int start = mPos;
|
||
|
skipQuotation(quotePrefix);
|
||
|
|
||
|
// Replace quotation with spaces
|
||
|
int len = mPos - start;
|
||
15 years ago
|
TQString spaces;
|
||
15 years ago
|
spaces.fill(' ', len);
|
||
13 years ago
|
mText.replace(start, len, spaces);
|
||
15 years ago
|
|
||
|
//kdDebug(5006) << "censored quotation ["
|
||
|
// << start << ", " << mPos << ")" << endl;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void SpellingFilter::TextCensor::censorUrls()
|
||
|
{
|
||
|
mPos = 0;
|
||
|
while(mPos < static_cast<int>(mText.length()))
|
||
|
{
|
||
|
// Find start of url
|
||
15 years ago
|
TQString url;
|
||
15 years ago
|
while(mPos < static_cast<int>(mText.length()) && url.isEmpty())
|
||
|
{
|
||
|
url = getUrl();
|
||
|
++mPos;
|
||
|
}
|
||
|
|
||
|
if(mPos < static_cast<int>(mText.length()) && !url.isEmpty())
|
||
|
{
|
||
|
int start = mPos - url.length();
|
||
|
|
||
|
// Replace url with spaces
|
||
|
url.fill(' ');
|
||
13 years ago
|
mText.replace(start, url.length(), url);
|
||
15 years ago
|
|
||
|
//kdDebug(5006) << "censored url ["
|
||
|
// << start << ", " << mPos << ")" << endl;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void SpellingFilter::TextCensor::censorEmailAddresses()
|
||
|
{
|
||
|
mPos = 0;
|
||
|
while(mPos < static_cast<int>(mText.length()))
|
||
|
{
|
||
|
// Find start of email address
|
||
|
findEmailAddress();
|
||
|
if(mPos < static_cast<int>(mText.length()))
|
||
|
{
|
||
15 years ago
|
TQString address = getEmailAddress();
|
||
15 years ago
|
++mPos;
|
||
|
if(!address.isEmpty())
|
||
|
{
|
||
|
int start = mPos - address.length();
|
||
|
|
||
|
// Replace address with spaces
|
||
|
address.fill(' ');
|
||
13 years ago
|
mText.replace(start, address.length(), address);
|
||
15 years ago
|
|
||
|
//kdDebug(5006) << "censored addr ["
|
||
|
// << start << ", "<< mPos << ")" << endl;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
void SpellingFilter::TextCensor::censorString(const TQString& s)
|
||
15 years ago
|
{
|
||
|
mPos = 0;
|
||
|
while(mPos != -1)
|
||
|
{
|
||
|
// Find start of string
|
||
13 years ago
|
mPos = mText.find(s, mPos);
|
||
15 years ago
|
if(mPos != -1)
|
||
|
{
|
||
|
// Replace string with spaces
|
||
15 years ago
|
TQString spaces;
|
||
15 years ago
|
spaces.fill(' ', s.length());
|
||
13 years ago
|
mText.replace(mPos, s.length(), spaces);
|
||
15 years ago
|
mPos += s.length();
|
||
|
|
||
|
//kdDebug(5006) << "censored string ["
|
||
|
// << mPos << ", "<< mPos+s.length() << ")" << endl;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString SpellingFilter::TextCensor::censoredText() const
|
||
15 years ago
|
{
|
||
|
return mText;
|
||
|
}
|
||
|
|
||
|
//-----------------------------------------------------------------------------
|
||
|
// text censorship helper functions
|
||
|
//
|
||
|
|
||
|
bool SpellingFilter::TextCensor::atLineStart() const
|
||
|
{
|
||
|
return (mPos == 0 && static_cast<int>(mText.length()) > 0) || (mText[mPos - 1] == '\n');
|
||
|
}
|
||
|
|
||
|
void SpellingFilter::TextCensor::skipLine()
|
||
|
{
|
||
13 years ago
|
mPos = mText.find('\n', mPos);
|
||
15 years ago
|
if(mPos == -1)
|
||
|
mPos = static_cast<int>(mText.length());
|
||
|
else
|
||
|
++mPos;
|
||
|
}
|
||
|
|
||
15 years ago
|
bool SpellingFilter::TextCensor::atQuotation(const TQString& quotePrefix) const
|
||
15 years ago
|
{
|
||
|
return atLineStart() &&
|
||
|
mText.mid(mPos, quotePrefix.length()) == quotePrefix;
|
||
|
}
|
||
|
|
||
15 years ago
|
void SpellingFilter::TextCensor::skipQuotation(const TQString& quotePrefix)
|
||
15 years ago
|
{
|
||
|
while(atQuotation(quotePrefix))
|
||
|
skipLine();
|
||
|
}
|
||
|
|
||
15 years ago
|
void SpellingFilter::TextCensor::findQuotation(const TQString& quotePrefix)
|
||
15 years ago
|
{
|
||
|
while(mPos < static_cast<int>(mText.length()) && !atQuotation(quotePrefix))
|
||
|
skipLine();
|
||
|
}
|
||
|
|
||
|
void SpellingFilter::TextCensor::findEmailAddress()
|
||
|
{
|
||
|
while(mPos < static_cast<int>(mText.length()) && mText[mPos] != '@')
|
||
|
++mPos;
|
||
|
}
|
||
|
|