KDECore
filter.cpp
Go to the documentation of this file.00001
00023 #include "filter_p.h"
00024
00025 #include "settings_p.h"
00026
00027 #include <kglobal.h>
00028 #include <kdebug.h>
00029
00030 namespace Sonnet
00031 {
00032
00033 static Word endWord;
00034
00035 class Filter::Private
00036 {
00037 public:
00038
00039
00040
00041 Settings *settings;
00042 };
00043
00044 Filter* Filter::defaultFilter()
00045 {
00046 return new Filter();
00047 }
00048
00049 Word Filter::end()
00050 {
00051 return endWord;
00052 }
00053
00054 Filter::Filter()
00055 : m_currentPosition( 0 ),d(new Private)
00056 {
00057 d->settings = 0;
00058 }
00059
00060 Filter::~Filter()
00061 {
00062 delete d;
00063 }
00064
00065 void Filter::setSettings( Settings *conf )
00066 {
00067 d->settings = conf;
00068 }
00069
00070 Settings *Filter::settings() const
00071 {
00072 return d->settings;
00073 }
00074
00075 void Filter::restart()
00076 {
00077 m_currentPosition = 0;
00078 }
00079
00080 void Filter::setBuffer( const QString& buffer )
00081 {
00082 m_buffer = buffer;
00083 m_currentPosition = 0;
00084 }
00085
00086 QString Filter::buffer() const
00087 {
00088 return m_buffer;
00089 }
00090
00091 bool Filter::atEnd() const
00092 {
00093 if ( m_currentPosition >= m_buffer.length() ) {
00094 return true;
00095 } else
00096 return false;
00097 }
00098
00099 Word Filter::nextWord() const
00100 {
00101 QChar currentChar = skipToLetter( m_currentPosition );
00102
00103 if ( m_currentPosition >= m_buffer.length() || currentChar.isNull() ) {
00104 return Filter::end();
00105 }
00106
00107 bool allUppercase = currentChar.category() & QChar::Letter_Uppercase;
00108 bool runTogether = false;
00109
00110 QString foundWord;
00111 int start = m_currentPosition;
00112
00113
00114
00115
00116
00117
00118
00119 while ( currentChar.isLetter() ||
00120 ( currentChar == '\'' && start != m_currentPosition ) ) {
00121 if ( currentChar.category() & QChar::Letter_Lowercase )
00122 allUppercase = false;
00123
00124
00125
00126
00127
00128
00129
00130
00131 foundWord += currentChar;
00132
00133 if( (m_currentPosition + 1) >= m_buffer.length()) {
00134
00135
00136
00137 if ( foundWord.endsWith( '\'' ) )
00138 foundWord.chop( 1 );
00139
00140 if ( shouldBeSkipped( allUppercase, runTogether, foundWord ) ) {
00141 ++m_currentPosition;
00142 return nextWord();
00143 }
00144 else {
00145 ++m_currentPosition;
00146 return Word( foundWord, start );
00147 }
00148 }
00149 ++m_currentPosition;
00150 currentChar = m_buffer.at( m_currentPosition );
00151 }
00152
00153
00154
00155 if ( foundWord.endsWith( '\'' ) )
00156 foundWord.chop( 1 );
00157
00158 if ( shouldBeSkipped( allUppercase, runTogether, foundWord ) )
00159 return nextWord();
00160 return Word( foundWord, start );
00161 }
00162
00163 Word Filter::previousWord() const
00164 {
00165 while ( !m_buffer.at( m_currentPosition ).isLetter() &&
00166 m_currentPosition != 0) {
00167 --m_currentPosition;
00168 }
00169
00170 if ( m_currentPosition == 0 ) {
00171 return Filter::end();
00172 }
00173
00174 QString foundWord;
00175 int start = m_currentPosition;
00176 while ( m_buffer.at( start ).isLetter() ) {
00177 foundWord.prepend( m_buffer.at( m_currentPosition ) );
00178 --start;
00179 }
00180
00181 return Word( foundWord, start );
00182 }
00183
00184 Word Filter::wordAtPosition( unsigned int pos ) const
00185 {
00186 if ( (int)pos > m_buffer.length() )
00187 return Filter::end();
00188
00189 int currentPosition = pos - 1;
00190 QString foundWord;
00191 while ( currentPosition >= 0 &&
00192 m_buffer.at( currentPosition ).isLetter() ) {
00193 foundWord.prepend( m_buffer.at( currentPosition ) );
00194 --currentPosition;
00195 }
00196
00197
00198
00199 int start = (currentPosition < 0) ? 0 : ++currentPosition;
00200 currentPosition = pos ;
00201 if ( currentPosition < m_buffer.length() && m_buffer.at( currentPosition ).isLetter() ) {
00202 while ( m_buffer.at( currentPosition ).isLetter() ) {
00203 foundWord.append( m_buffer.at( currentPosition ) );
00204 ++currentPosition;
00205 }
00206 }
00207
00208 return Word( foundWord, start );
00209 }
00210
00211
00212 void Filter::setCurrentPosition( int i )
00213 {
00214 m_currentPosition = i;
00215
00216
00217
00218 while ( m_buffer.at( m_currentPosition ).isLetter() && m_currentPosition > 0 )
00219 --m_currentPosition;
00220 }
00221
00222 int Filter::currentPosition() const
00223 {
00224 return m_currentPosition;
00225 }
00226
00227 void Filter::replace( const Word& w, const QString& newWord)
00228 {
00229 int oldLen = w.word.length();
00230
00231
00232 m_currentPosition = w.start;
00233 m_buffer = m_buffer.replace( w.start, oldLen, newWord );
00234 }
00235
00236 QString Filter::context() const
00237 {
00238 int len = 60;
00239
00240
00241 int signedPosition = m_currentPosition;
00242 bool begin = ( (signedPosition - len/2)<=0 ) ? true : false;
00243
00244
00245 QString buffer = m_buffer;
00246 Word word = wordAtPosition( m_currentPosition );
00247 buffer = buffer.replace( word.start, word.word.length(),
00248 QString( "<b>%1</b>" ).arg( word.word ) );
00249
00250 QString context;
00251 if ( begin )
00252 context = QString( "%1...")
00253 .arg( buffer.mid( 0, len ) );
00254 else
00255 context = QString( "...%1..." )
00256 .arg( buffer.mid( m_currentPosition - 20, len ) );
00257
00258 context = context.replace( '\n', ' ' );
00259
00260 return context;
00261 }
00262
00263 bool Filter::trySkipLinks() const
00264 {
00265 QChar currentChar = m_buffer.at( m_currentPosition );
00266
00267 int length = m_buffer.length();
00268
00269 if ( currentChar == ':'
00270 && (m_currentPosition+1 < length)
00271 && (m_buffer.at( ++m_currentPosition ) == '/' || ( m_currentPosition + 1 ) >= length ) ) {
00272
00273
00274 while ( !m_buffer.at( m_currentPosition++ ).isSpace() && m_currentPosition < length )
00275 ;
00276 return true;
00277 }
00278
00279
00280 if ( currentChar == '@') {
00281 while ( ++m_currentPosition < length && !m_buffer.at( m_currentPosition ).isSpace() )
00282 ;
00283 return true;
00284 }
00285
00286 return false;
00287 }
00288
00289 bool Filter::ignore( const QString& word ) const
00290 {
00291 if ( d->settings ) {
00292 return d->settings->ignore( word );
00293 }
00294 return false;
00295 }
00296
00297 QChar Filter::skipToLetter( int &fromPosition ) const
00298 {
00299
00300 if (fromPosition>=m_buffer.size())
00301 return QChar();
00302 QChar currentChar = m_buffer.at( fromPosition );
00303 while ( !currentChar.isLetter() &&
00304 (int)++fromPosition < m_buffer.length() ) {
00305 currentChar = m_buffer.at( fromPosition );
00306 }
00307 return currentChar;
00308 }
00309
00310 bool Filter::shouldBeSkipped( bool wordWasUppercase, bool wordWasRunTogether,
00311 const QString& foundWord ) const
00312 {
00313 bool checkUpper = ( d->settings ) ?
00314 d->settings->checkUppercase () : true;
00315 bool skipRunTogether = ( d->settings ) ?
00316 d->settings->skipRunTogether() : true;
00317
00318 if ( trySkipLinks() )
00319 return true;
00320
00321 if ( wordWasUppercase && !checkUpper )
00322 return true;
00323
00324 if ( wordWasRunTogether && skipRunTogether )
00325 return true;
00326
00327 return ignore( foundWord );
00328 }
00329
00330 }