libkdenetwork Library API Documentation

spellingfilter.cpp

00001 00023 #include <kdebug.h> 00024 #include "spellingfilter.h" 00025 00026 //----------------------------------------------------------------------------- 00027 // SpellingFilter implementation 00028 // 00029 00030 SpellingFilter::SpellingFilter(const QString& text, const QString& quotePrefix, 00031 UrlFiltering filterUrls, EmailAddressFiltering filterEmailAddresses, 00032 const QStringList& filterStrings) 00033 : mOriginal(text) 00034 { 00035 TextCensor c(text); 00036 00037 if(!quotePrefix.isEmpty()) 00038 c.censorQuotations(quotePrefix); 00039 00040 if(filterUrls) 00041 c.censorUrls(); 00042 00043 if(filterEmailAddresses) 00044 c.censorEmailAddresses(); 00045 00046 QStringList::const_iterator iter = filterStrings.begin(); 00047 while(iter != filterStrings.end()) 00048 { 00049 c.censorString(*iter); 00050 ++iter; 00051 } 00052 00053 mFiltered = c.censoredText(); 00054 } 00055 00056 QString SpellingFilter::originalText() const 00057 { 00058 return mOriginal; 00059 } 00060 00061 QString SpellingFilter::filteredText() const 00062 { 00063 return mFiltered; 00064 } 00065 00066 //----------------------------------------------------------------------------- 00067 // SpellingFilter::TextCensor implementation 00068 // 00069 00070 SpellingFilter::TextCensor::TextCensor(const QString& s) 00071 : LinkLocator(s) 00072 { 00073 00074 } 00075 00076 void SpellingFilter::TextCensor::censorQuotations(const QString& quotePrefix) 00077 { 00078 mPos = 0; 00079 while(mPos < static_cast<int>(mText.length())) 00080 { 00081 // Find start of quotation 00082 findQuotation(quotePrefix); 00083 if(mPos < static_cast<int>(mText.length())) 00084 { 00085 int start = mPos; 00086 skipQuotation(quotePrefix); 00087 00088 // Replace quotation with spaces 00089 int len = mPos - start; 00090 QString spaces; 00091 spaces.fill(' ', len); 00092 mText.replace(start, len, spaces); 00093 00094 //kdDebug(5006) << "censored quotation [" 00095 // << start << ", " << mPos << ")" << endl; 00096 } 00097 } 00098 } 00099 00100 void SpellingFilter::TextCensor::censorUrls() 00101 { 00102 mPos = 0; 00103 while(mPos < static_cast<int>(mText.length())) 00104 { 00105 // Find start of url 00106 QString url; 00107 while(mPos < static_cast<int>(mText.length()) && url.isEmpty()) 00108 { 00109 url = getUrl(); 00110 ++mPos; 00111 } 00112 00113 if(mPos < static_cast<int>(mText.length()) && !url.isEmpty()) 00114 { 00115 int start = mPos - url.length(); 00116 00117 // Replace url with spaces 00118 url.fill(' '); 00119 mText.replace(start, url.length(), url); 00120 00121 //kdDebug(5006) << "censored url [" 00122 // << start << ", " << mPos << ")" << endl; 00123 } 00124 } 00125 } 00126 00127 void SpellingFilter::TextCensor::censorEmailAddresses() 00128 { 00129 mPos = 0; 00130 while(mPos < static_cast<int>(mText.length())) 00131 { 00132 // Find start of email address 00133 findEmailAddress(); 00134 if(mPos < static_cast<int>(mText.length())) 00135 { 00136 QString address = getEmailAddress(); 00137 ++mPos; 00138 if(!address.isEmpty()) 00139 { 00140 int start = mPos - address.length(); 00141 00142 // Replace address with spaces 00143 address.fill(' '); 00144 mText.replace(start, address.length(), address); 00145 00146 //kdDebug(5006) << "censored addr [" 00147 // << start << ", "<< mPos << ")" << endl; 00148 } 00149 } 00150 } 00151 } 00152 00153 void SpellingFilter::TextCensor::censorString(const QString& s) 00154 { 00155 mPos = 0; 00156 while(mPos != -1) 00157 { 00158 // Find start of string 00159 mPos = mText.find(s, mPos); 00160 if(mPos != -1) 00161 { 00162 // Replace string with spaces 00163 QString spaces; 00164 spaces.fill(' ', s.length()); 00165 mText.replace(mPos, s.length(), spaces); 00166 mPos += s.length(); 00167 00168 //kdDebug(5006) << "censored string [" 00169 // << mPos << ", "<< mPos+s.length() << ")" << endl; 00170 } 00171 } 00172 } 00173 00174 QString SpellingFilter::TextCensor::censoredText() const 00175 { 00176 return mText; 00177 } 00178 00179 //----------------------------------------------------------------------------- 00180 // text censorship helper functions 00181 // 00182 00183 bool SpellingFilter::TextCensor::atLineStart() const 00184 { 00185 return (mPos == 0 && static_cast<int>(mText.length()) > 0) || (mText[mPos - 1] == '\n'); 00186 } 00187 00188 void SpellingFilter::TextCensor::skipLine() 00189 { 00190 mPos = mText.find('\n', mPos); 00191 if(mPos == -1) 00192 mPos = static_cast<int>(mText.length()); 00193 else 00194 ++mPos; 00195 } 00196 00197 bool SpellingFilter::TextCensor::atQuotation(const QString& quotePrefix) const 00198 { 00199 return atLineStart() && 00200 mText.mid(mPos, quotePrefix.length()) == quotePrefix; 00201 } 00202 00203 void SpellingFilter::TextCensor::skipQuotation(const QString& quotePrefix) 00204 { 00205 while(atQuotation(quotePrefix)) 00206 skipLine(); 00207 } 00208 00209 void SpellingFilter::TextCensor::findQuotation(const QString& quotePrefix) 00210 { 00211 while(mPos < static_cast<int>(mText.length()) && !atQuotation(quotePrefix)) 00212 skipLine(); 00213 } 00214 00215 void SpellingFilter::TextCensor::findEmailAddress() 00216 { 00217 while(mPos < static_cast<int>(mText.length()) && mText[mPos] != '@') 00218 ++mPos; 00219 } 00220
KDE Logo
This file is part of the documentation for libkdenetwork Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Oct 21 19:46:17 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003