• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdespell2
 

tdespell2

  • tdespell2
filter.cpp
1 /*
2  * filter.cpp
3  *
4  * Copyright (C) 2004 Zack Rusin <zack@kde.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301 USA
20  */
21 
22 #include "filter.h"
23 
24 #include "settings.h"
25 
26 #include <kstaticdeleter.h>
27 #include <kdebug.h>
28 
29 #include <tqstring.h>
30 
31 namespace KSpell2
32 {
33 
34 static Word endWord;
35 static KStaticDeleter<Filter> sd;
36 static Filter* defFilter = 0;
37 
38 class Filter::Private
39 {
40 public:
41  // The reason it's not in the class directly is that
42  // i'm not 100% sure that having the settings() here is
43  // the way i want to be doing this.
44  Settings *settings;
45 };
46 
47 Filter* Filter::defaultFilter()
48 {
49  if ( !defFilter )
50  sd.setObject( defFilter, new Filter() );
51  return defFilter;
52 }
53 
54 Word Filter::end()
55 {
56  return endWord;
57 }
58 
59 Filter::Filter()
60  : m_currentPosition( 0 )
61 {
62  d = new Private;
63  d->settings = 0;
64 }
65 
66 Filter::~Filter()
67 {
68  delete d; d = 0;
69 }
70 
71 void Filter::setSettings( Settings *conf )
72 {
73  d->settings = conf;
74 }
75 
76 Settings *Filter::settings() const
77 {
78  return d->settings;
79 }
80 
81 void Filter::restart()
82 {
83  m_currentPosition = 0;
84 }
85 
86 void Filter::setBuffer( const TQString& buffer )
87 {
88  m_buffer = buffer;
89  m_currentPosition = 0;
90 }
91 
92 TQString Filter::buffer() const
93 {
94  return m_buffer;
95 }
96 
97 bool Filter::atEnd() const
98 {
99  if ( m_currentPosition >= m_buffer.length() ) {
100  return true;
101  } else
102  return false;
103 }
104 
105 Word Filter::nextWord() const
106 {
107  TQChar currentChar = skipToLetter( m_currentPosition );
108 
109  if ( m_currentPosition >= m_buffer.length() ) {
110  return Filter::end();
111  }
112 
113  bool allUppercase = currentChar.category() & TQChar::Letter_Uppercase;
114  bool runTogether = false;
115 
116  TQString foundWord;
117  int start = m_currentPosition;
118  while ( currentChar.isLetter() ) {
119  if ( currentChar.category() & TQChar::Letter_Lowercase )
120  allUppercase = false;
121 
122  /* FIXME: this does not work for Hebrew for example
123  //we consider run-together words as mixed-case words
124  if ( !allUppercase &&
125  currentChar.category() & TQChar::Letter_Uppercase )
126  runTogether = true;
127  */
128 
129  foundWord += currentChar;
130  ++m_currentPosition;
131  currentChar = m_buffer[ m_currentPosition ];
132  }
133 
134  if ( shouldBeSkipped( allUppercase, runTogether, foundWord ) )
135  return nextWord();
136 
137  return Word( foundWord, start );
138 }
139 
140 Word Filter::previousWord() const
141 {
142  while ( !m_buffer[ m_currentPosition ].isLetter() &&
143  m_currentPosition != 0) {
144  --m_currentPosition;
145  }
146 
147  if ( m_currentPosition == 0 ) {
148  return Filter::end();
149  }
150 
151  TQString foundWord;
152  int start = m_currentPosition;
153  while ( m_buffer[ start ].isLetter() ) {
154  foundWord.prepend( m_buffer[ m_currentPosition ] );
155  --start;
156  }
157 
158  return Word( foundWord, start );
159 }
160 
161 Word Filter::wordAtPosition( unsigned int pos ) const
162 {
163  if ( pos > m_buffer.length() )
164  return Filter::end();
165 
166  int currentPosition = pos - 1;
167  TQString foundWord;
168  while ( currentPosition >= 0 &&
169  m_buffer[ currentPosition ].isLetter() ) {
170  foundWord.prepend( m_buffer[ currentPosition ] );
171  --currentPosition;
172  }
173 
174  // currentPosition == 0 means the first char is not letter
175  // currentPosition == -1 means we reached the beginning
176  int start = (currentPosition < 0) ? 0 : ++currentPosition;
177  currentPosition = pos ;
178  if ( m_buffer[ currentPosition ].isLetter() ) {
179  while ( m_buffer[ currentPosition ].isLetter() ) {
180  foundWord.append( m_buffer[ currentPosition ] );
181  ++currentPosition;
182  }
183  }
184 
185  return Word( foundWord, start );
186 }
187 
188 
189 void Filter::setCurrentPosition( int i )
190 {
191  m_currentPosition = i;
192 
193  //go back to the last word so that next word returns something
194  //useful
195  while ( m_buffer[m_currentPosition].isLetter() && m_currentPosition > 0 )
196  --m_currentPosition;
197 }
198 
199 int Filter::currentPosition() const
200 {
201  return m_currentPosition;
202 }
203 
204 void Filter::replace( const Word& w, const TQString& newWord)
205 {
206  int oldLen = w.word.length();
207  int newLen = newWord.length();
208 
209  if ( oldLen != newLen && m_currentPosition > w.start ) {
210  if ( m_currentPosition > w.start ) {
211  int len = newLen - oldLen;
212  m_currentPosition += len;
213  }
214  }
215  m_buffer = m_buffer.replace( w.start, oldLen, newWord );
216 }
217 
218 TQString Filter::context() const
219 {
220  int len = 60;
221  //we don't want the expression underneath casted to an unsigned int
222  //which would cause it to always evaluate to false
223  int signedPosition = m_currentPosition;
224  bool begin = ( (signedPosition - len/2)<=0 ) ? true : false;
225 
226 
227  TQString buffer = m_buffer;
228  Word word = wordAtPosition( m_currentPosition );
229  buffer = buffer.replace( word.start, word.word.length(),
230  TQString( "<b>%1</b>" ).arg( word.word ) );
231 
232  TQString context;
233  if ( begin )
234  context = TQString( "%1...")
235  .arg( buffer.mid( 0, len ) );
236  else
237  context = TQString( "...%1..." )
238  .arg( buffer.mid( m_currentPosition - 20, len ) );
239 
240  context = context.replace( '\n', ' ' );
241 
242  return context;
243 }
244 
245 bool Filter::trySkipLinks() const
246 {
247  TQChar currentChar = m_buffer[ m_currentPosition ];
248 
249  uint length = m_buffer.length();
250  //URL - if so skip
251  if ( currentChar == ':' &&
252  ( m_buffer[ ++m_currentPosition] == '/' || ( m_currentPosition + 1 ) >= length ) ) {
253  //in both cases url is considered finished at the first whitespace occurence
254  while ( !m_buffer[ m_currentPosition++ ].isSpace() && m_currentPosition < length )
255  ;
256  return true;
257  }
258 
259  //Email - if so skip
260  if ( currentChar == '@' ) {
261  while ( !m_buffer[ ++m_currentPosition ].isSpace() && m_currentPosition < length )
262  ;
263  return true;
264  }
265 
266  return false;
267 }
268 
269 bool Filter::ignore( const TQString& word ) const
270 {
271  if ( d->settings ) {
272  return d->settings->ignore( word );
273  }
274  return false;
275 }
276 
277 TQChar Filter::skipToLetter( uint &fromPosition ) const
278 {
279 
280  TQChar currentChar = m_buffer[ fromPosition ];
281  while ( !currentChar.isLetter() &&
282  ++fromPosition < m_buffer.length() ) {
283  currentChar = m_buffer[ fromPosition ];
284  }
285  return currentChar;
286 }
287 
288 bool Filter::shouldBeSkipped( bool wordWasUppercase, bool wordWasRunTogether,
289  const TQString& foundWord ) const
290 {
291  bool checkUpper = ( d->settings ) ?
292  d->settings->checkUppercase () : true;
293  bool skipRunTogether = ( d->settings ) ?
294  d->settings->skipRunTogether() : true;
295 
296  if ( trySkipLinks() )
297  return true;
298 
299  if ( wordWasUppercase && !checkUpper )
300  return true;
301 
302  if ( wordWasRunTogether && skipRunTogether )
303  return true;
304 
305  return ignore( foundWord );
306 }
307 
308 }
KSpell2::Filter::settings
Settings * settings() const
Returns currently used Settings object.
Definition: filter.cpp:76
KSpell2
tdespell_hspellclient.h
Definition: backgroundchecker.h:29
KSpell2::Word
Structure abstracts the word and its position in the parent text.
Definition: filter.h:40

tdespell2

Skip menu "tdespell2"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

tdespell2

Skip menu "tdespell2"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdespell2 by doxygen 1.9.1
This website is maintained by Timothy Pearson.