libtdepim

spellingfilter.cpp
1 
23 #include <kdebug.h>
24 #include "spellingfilter.h"
25 
26 //-----------------------------------------------------------------------------
27 // SpellingFilter implementation
28 //
29 
30 SpellingFilter::SpellingFilter(const TQString& text, const TQString& quotePrefix,
31  UrlFiltering filterUrls, EmailAddressFiltering filterEmailAddresses,
32  const TQStringList& filterStrings)
33  : mOriginal(text)
34 {
35  TextCensor c(text);
36 
37  if(!quotePrefix.isEmpty())
38  c.censorQuotations(quotePrefix);
39 
40  if(filterUrls)
41  c.censorUrls();
42 
43  if(filterEmailAddresses)
44  c.censorEmailAddresses();
45 
46  TQStringList::const_iterator iter = filterStrings.begin();
47  while(iter != filterStrings.end())
48  {
49  c.censorString(*iter);
50  ++iter;
51  }
52 
53  mFiltered = c.censoredText();
54 }
55 
56 TQString SpellingFilter::originalText() const
57 {
58  return mOriginal;
59 }
60 
61 TQString SpellingFilter::filteredText() const
62 {
63  return mFiltered;
64 }
65 
66 //-----------------------------------------------------------------------------
67 // SpellingFilter::TextCensor implementation
68 //
69 
70 SpellingFilter::TextCensor::TextCensor(const TQString& s)
71  : LinkLocator(s)
72 {
73 
74 }
75 
76 void SpellingFilter::TextCensor::censorQuotations(const TQString& quotePrefix)
77 {
78  mPos = 0;
79  while(mPos < static_cast<int>(mText.length()))
80  {
81  // Find start of quotation
82  findQuotation(quotePrefix);
83  if(mPos < static_cast<int>(mText.length()))
84  {
85  int start = mPos;
86  skipQuotation(quotePrefix);
87 
88  // Replace quotation with spaces
89  int len = mPos - start;
90  TQString spaces;
91  spaces.fill(' ', len);
92  mText.replace(start, len, spaces);
93 
94  //kdDebug(5006) << "censored quotation ["
95  // << start << ", " << mPos << ")" << endl;
96  }
97  }
98 }
99 
100 void SpellingFilter::TextCensor::censorUrls()
101 {
102  mPos = 0;
103  while(mPos < static_cast<int>(mText.length()))
104  {
105  // Find start of url
106  TQString url;
107  while(mPos < static_cast<int>(mText.length()) && url.isEmpty())
108  {
109  url = getUrl();
110  ++mPos;
111  }
112 
113  if(mPos < static_cast<int>(mText.length()) && !url.isEmpty())
114  {
115  int start = mPos - url.length();
116 
117  // Replace url with spaces
118  url.fill(' ');
119  mText.replace(start, url.length(), url);
120 
121  //kdDebug(5006) << "censored url ["
122  // << start << ", " << mPos << ")" << endl;
123  }
124  }
125 }
126 
127 void SpellingFilter::TextCensor::censorEmailAddresses()
128 {
129  mPos = 0;
130  while(mPos < static_cast<int>(mText.length()))
131  {
132  // Find start of email address
133  findEmailAddress();
134  if(mPos < static_cast<int>(mText.length()))
135  {
136  TQString address = getEmailAddress();
137  ++mPos;
138  if(!address.isEmpty())
139  {
140  int start = mPos - address.length();
141 
142  // Replace address with spaces
143  address.fill(' ');
144  mText.replace(start, address.length(), address);
145 
146  //kdDebug(5006) << "censored addr ["
147  // << start << ", "<< mPos << ")" << endl;
148  }
149  }
150  }
151 }
152 
153 void SpellingFilter::TextCensor::censorString(const TQString& s)
154 {
155  mPos = 0;
156  while(mPos != -1)
157  {
158  // Find start of string
159  mPos = mText.find(s, mPos);
160  if(mPos != -1)
161  {
162  // Replace string with spaces
163  TQString spaces;
164  spaces.fill(' ', s.length());
165  mText.replace(mPos, s.length(), spaces);
166  mPos += s.length();
167 
168  //kdDebug(5006) << "censored string ["
169  // << mPos << ", "<< mPos+s.length() << ")" << endl;
170  }
171  }
172 }
173 
174 TQString SpellingFilter::TextCensor::censoredText() const
175 {
176  return mText;
177 }
178 
179 //-----------------------------------------------------------------------------
180 // text censorship helper functions
181 //
182 
183 bool SpellingFilter::TextCensor::atLineStart() const
184 {
185  return (mPos == 0 && static_cast<int>(mText.length()) > 0) || (mText[mPos - 1] == '\n');
186 }
187 
188 void SpellingFilter::TextCensor::skipLine()
189 {
190  mPos = mText.find('\n', mPos);
191  if(mPos == -1)
192  mPos = static_cast<int>(mText.length());
193  else
194  ++mPos;
195 }
196 
197 bool SpellingFilter::TextCensor::atQuotation(const TQString& quotePrefix) const
198 {
199  return atLineStart() &&
200  mText.mid(mPos, quotePrefix.length()) == quotePrefix;
201 }
202 
203 void SpellingFilter::TextCensor::skipQuotation(const TQString& quotePrefix)
204 {
205  while(atQuotation(quotePrefix))
206  skipLine();
207 }
208 
209 void SpellingFilter::TextCensor::findQuotation(const TQString& quotePrefix)
210 {
211  while(mPos < static_cast<int>(mText.length()) && !atQuotation(quotePrefix))
212  skipLine();
213 }
214 
215 void SpellingFilter::TextCensor::findEmailAddress()
216 {
217  while(mPos < static_cast<int>(mText.length()) && mText[mPos] != '@')
218  ++mPos;
219 }
220 
LinkLocator assists in identifying sections of text that can usefully be converted in hyperlinks in h...
Definition: linklocator.h:42