filter.cpp

00001 // -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*-
00023 #include "filter.h"
00024 
00025 #include "settings.h"
00026 
00027 #include <kstaticdeleter.h>
00028 #include <kdebug.h>
00029 
00030 #include <qstring.h>
00031 
00032 namespace KSpell2
00033 {
00034 
00035 static Word endWord;
00036 static KStaticDeleter<Filter> sd;
00037 static Filter* defFilter = 0;
00038 
00039 class Filter::Private
00040 {
00041 public:
00042     // The reason it's not in the class directly is that
00043     // i'm not 100% sure that having the settings() here is
00044     // the way i want to be doing this.
00045     Settings *settings;
00046 };
00047 
00048 Filter* Filter::defaultFilter()
00049 {
00050     if ( !defFilter )
00051         sd.setObject( defFilter, new Filter() );
00052     return defFilter;
00053 }
00054 
00055 Word Filter::end()
00056 {
00057     return endWord;
00058 }
00059 
00060 Filter::Filter()
00061     : m_currentPosition( 0 )
00062 {
00063     d = new Private;
00064     d->settings = 0;
00065 }
00066 
00067 Filter::~Filter()
00068 {
00069     delete d; d = 0;
00070 }
00071 
00072 void Filter::setSettings( Settings *conf )
00073 {
00074     d->settings = conf;
00075 }
00076 
00077 Settings *Filter::settings() const
00078 {
00079     return d->settings;
00080 }
00081 
00082 void Filter::restart()
00083 {
00084     m_currentPosition = 0;
00085 }
00086 
00087 void Filter::setBuffer( const QString& buffer )
00088 {
00089     m_buffer          = buffer;
00090     m_currentPosition = 0;
00091 }
00092 
00093 QString Filter::buffer() const
00094 {
00095     return m_buffer;
00096 }
00097 
00098 bool Filter::atEnd() const
00099 {
00100     if ( m_currentPosition >= m_buffer.length() ) {
00101         return true;
00102     } else
00103         return false;
00104 }
00105 
00106 Word Filter::nextWord() const
00107 {
00108     QChar currentChar = skipToLetter( m_currentPosition );
00109 
00110     if ( m_currentPosition >= m_buffer.length() ) {
00111         return Filter::end();
00112     }
00113 
00114     bool allUppercase = currentChar.category() & QChar::Letter_Uppercase;
00115     bool runTogether = false;
00116 
00117     QString foundWord;
00118     int start = m_currentPosition;
00119     while ( currentChar.isLetter() ) {
00120         if ( currentChar.category() & QChar::Letter_Lowercase )
00121             allUppercase = false;
00122 
00123     /* FIXME: this does not work for Hebrew for example
00124         //we consider run-together words as mixed-case words
00125         if ( !allUppercase &&
00126              currentChar.category() & QChar::Letter_Uppercase )
00127             runTogether = true;
00128     */
00129 
00130         foundWord += currentChar;
00131         ++m_currentPosition;
00132         currentChar = m_buffer[ m_currentPosition ];
00133     }
00134 
00135     if ( shouldBeSkipped( allUppercase, runTogether, foundWord ) )
00136         return nextWord();
00137 
00138     return Word( foundWord, start );
00139 }
00140 
00141 Word Filter::previousWord() const
00142 {
00143     while ( !m_buffer[ m_currentPosition ].isLetter() &&
00144             m_currentPosition != 0) {
00145         --m_currentPosition;
00146     }
00147 
00148     if ( m_currentPosition == 0 ) {
00149         return Filter::end();
00150     }
00151 
00152     QString foundWord;
00153     int start = m_currentPosition;
00154     while ( m_buffer[ start ].isLetter() ) {
00155         foundWord.prepend( m_buffer[ m_currentPosition ] );
00156         --start;
00157     }
00158 
00159     return Word( foundWord, start );
00160 }
00161 
00162 Word Filter::wordAtPosition( unsigned int pos ) const
00163 {
00164     if ( pos > m_buffer.length() )
00165         return Filter::end();
00166 
00167     int currentPosition = pos - 1;
00168     QString foundWord;
00169     while ( currentPosition >= 0 &&
00170             m_buffer[ currentPosition ].isLetter() ) {
00171         foundWord.prepend( m_buffer[ currentPosition ] );
00172         --currentPosition;
00173     }
00174 
00175     int start = (!currentPosition) ? 0 : ++currentPosition;
00176     currentPosition = pos ;
00177     if ( m_buffer[ currentPosition ].isLetter() ) {
00178         while ( m_buffer[ currentPosition ].isLetter() ) {
00179             foundWord.append( m_buffer[ currentPosition ] );
00180             ++currentPosition;
00181         }
00182     }
00183 
00184     return Word( foundWord, start );
00185 }
00186 
00187 
00188 void Filter::setCurrentPosition( int i )
00189 {
00190     m_currentPosition = i;
00191 
00192     //go back to the last word so that next word returns something
00193     //useful
00194     while ( m_buffer[m_currentPosition].isLetter() && m_currentPosition > 0 )
00195         --m_currentPosition;
00196 }
00197 
00198 int Filter::currentPosition() const
00199 {
00200     return m_currentPosition;
00201 }
00202 
00203 void Filter::replace( const Word& w, const QString& newWord)
00204 {
00205     int oldLen = w.word.length();
00206     int newLen = newWord.length();
00207 
00208     if ( oldLen != newLen && m_currentPosition > w.start ) {
00209         if ( m_currentPosition > w.start ) {
00210             int len = newLen - oldLen;
00211             m_currentPosition += len;
00212         }
00213     }
00214     m_buffer = m_buffer.replace( w.start, oldLen, newWord );
00215 }
00216 
00217 QString Filter::context() const
00218 {
00219     int len = 60;
00220     //we don't want the expression underneath casted to an unsigned int
00221     //which would cause it to always evaluate to false
00222     int signedPosition = m_currentPosition;
00223     bool begin = ( (signedPosition - len/2)<=0 ) ? true : false;
00224 
00225 
00226     QString buffer = m_buffer;
00227     Word word = wordAtPosition( m_currentPosition );
00228     buffer = buffer.replace( word.start, word.word.length(),
00229                              QString( "<b>%1</b>" ).arg( word.word ) );
00230 
00231     QString context;
00232     if ( begin )
00233         context = QString( "%1...")
00234                   .arg( buffer.mid(  0, len ) );
00235     else
00236         context = QString( "...%1..." )
00237                   .arg( buffer.mid(  m_currentPosition - 20, len ) );
00238 
00239     context = context.replace( '\n', ' ' );
00240 
00241     return context;
00242 }
00243 
00244 bool Filter::trySkipLinks() const
00245 {
00246     QChar currentChar = m_buffer[ m_currentPosition ];
00247 
00248     uint length = m_buffer.length();
00249     //URL - if so skip
00250     if ( currentChar == ':' &&
00251          ( m_buffer[ ++m_currentPosition] == '/' || ( m_currentPosition + 1 ) >= length ) ) {
00252         //in both cases url is considered finished at the first whitespace occurence
00253         while ( !m_buffer[ m_currentPosition++ ].isSpace() && m_currentPosition < length )
00254             ;
00255         return true;
00256     }
00257 
00258     //Email - if so skip
00259     if ( currentChar == '@' ) {
00260         while ( !m_buffer[ ++m_currentPosition ].isSpace() && m_currentPosition < length )
00261             ;
00262         return true;
00263     }
00264 
00265     return false;
00266 }
00267 
00268 bool Filter::ignore( const QString& word ) const
00269 {
00270     if ( d->settings ) {
00271         return d->settings->ignore( word );
00272     }
00273     return false;
00274 }
00275 
00276 QChar Filter::skipToLetter( uint &fromPosition ) const
00277 {
00278 
00279     QChar currentChar = m_buffer[ fromPosition ];
00280     while ( !currentChar.isLetter() &&
00281             ++fromPosition < m_buffer.length() ) {
00282         currentChar = m_buffer[ fromPosition ];
00283     }
00284     return currentChar;
00285 }
00286 
00287 bool Filter::shouldBeSkipped( bool wordWasUppercase, bool wordWasRunTogether,
00288                              const QString& foundWord ) const
00289 {
00290     bool checkUpper = ( d->settings ) ?
00291                       d->settings->checkUppercase () : true;
00292     bool skipRunTogether = ( d->settings ) ?
00293                            d->settings->skipRunTogether() : true;
00294 
00295     if ( trySkipLinks() )
00296         return true;
00297 
00298     if ( wordWasUppercase && !checkUpper )
00299         return true;
00300 
00301     if ( wordWasRunTogether && skipRunTogether )
00302         return true;
00303 
00304     return ignore( foundWord );
00305 }
00306 
00307 }
KDE Home | KDE Accessibility Home | Description of Access Keys