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

tdespell2

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

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.8.13
This website is maintained by Timothy Pearson.