• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepimlibs API Reference
  • KDE Home
  • Contact Us
 

kpimutils

  • sources
  • kde-4.14
  • kdepimlibs
  • kpimutils
spellingfilter.cpp
Go to the documentation of this file.
1 /*
2  * spellingfilter.cpp
3  *
4  * Copyright (c) 2002 Dave Corrie <kde@davecorrie.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 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  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
32 #include "spellingfilter.h"
33 
34 using namespace KPIMUtils;
35 
40 //@cond PRIVATE
41 class KPIMUtils::SpellingFilter::Private
42 {
43  public:
44  QString mOriginal;
45  QString mFiltered;
46 };
47 //@endcond
48 
49 //-----------------------------------------------------------------------------
50 // SpellingFilter implementation
51 //
52 
53 SpellingFilter::SpellingFilter( const QString &text,
54  const QString &quotePrefix,
55  UrlFiltering filterUrls,
56  EmailAddressFiltering filterEmailAddresses,
57  const QStringList &filterStrings )
58  : d( new KPIMUtils::SpellingFilter::Private )
59 {
60  d->mOriginal = text;
61  TextCensor c( text );
62 
63  if ( !quotePrefix.isEmpty() ) {
64  c.censorQuotations( quotePrefix );
65  }
66 
67  if ( filterUrls ) {
68  c.censorUrls();
69  }
70 
71  if ( filterEmailAddresses ) {
72  c.censorEmailAddresses();
73  }
74 
75  QStringList::const_iterator iter = filterStrings.begin();
76  QStringList::const_iterator endIter = filterStrings.end();
77  while ( iter != endIter ) {
78  c.censorString( *iter );
79  ++iter;
80  }
81 
82  d->mFiltered = c.censoredText();
83 }
84 
85 SpellingFilter::~SpellingFilter()
86 {
87  delete d;
88 }
89 
90 QString SpellingFilter::originalText() const
91 {
92  return d->mOriginal;
93 }
94 
95 QString SpellingFilter::filteredText() const
96 {
97  return d->mFiltered;
98 }
99 
100 //-----------------------------------------------------------------------------
101 // SpellingFilter::TextCensor implementation
102 //
103 
104 SpellingFilter::TextCensor::TextCensor( const QString &s )
105  : LinkLocator( s )
106 {
107 }
108 
109 void SpellingFilter::TextCensor::censorQuotations( const QString &quotePrefix )
110 {
111  mPos = 0;
112  while ( mPos < mText.length() ) {
113  // Find start of quotation
114  findQuotation( quotePrefix );
115  if ( mPos < mText.length() ) {
116  int start = mPos;
117  skipQuotation( quotePrefix );
118 
119  // Replace quotation with spaces
120  int len = mPos - start;
121  QString spaces;
122  spaces.fill( QLatin1Char(' '), len );
123  mText.replace( start, len, spaces );
124  }
125  }
126 }
127 
128 void SpellingFilter::TextCensor::censorUrls()
129 {
130  mPos = 0;
131  while ( mPos < mText.length() ) {
132  // Find start of url
133  QString url;
134  while ( mPos < mText.length() && url.isEmpty() ) {
135  url = getUrl();
136  ++mPos;
137  }
138 
139  if ( mPos < mText.length() && !url.isEmpty() ) {
140  int start = mPos - url.length();
141 
142  // Replace url with spaces
143  url.fill( QLatin1Char(' ') );
144  mText.replace( start, url.length(), url );
145  }
146  }
147 }
148 
149 void SpellingFilter::TextCensor::censorEmailAddresses()
150 {
151  mPos = 0;
152  while ( mPos < mText.length() ) {
153  // Find start of email address
154  findEmailAddress();
155  if ( mPos < mText.length() ) {
156  QString address = getEmailAddress();
157  ++mPos;
158  if ( !address.isEmpty() ) {
159  int start = mPos - address.length();
160 
161  // Replace address with spaces
162  address.fill( QLatin1Char(' ') );
163  mText.replace( start, address.length(), address );
164  }
165  }
166  }
167 }
168 
169 void SpellingFilter::TextCensor::censorString( const QString &s )
170 {
171  mPos = 0;
172  while ( mPos != -1 ) {
173  // Find start of string
174  mPos = mText.indexOf( s, mPos );
175  if ( mPos != -1 ) {
176  // Replace string with spaces
177  QString spaces;
178  spaces.fill( QLatin1Char(' '), s.length() );
179  mText.replace( mPos, s.length(), spaces );
180  mPos += s.length();
181  }
182  }
183 }
184 
185 QString SpellingFilter::TextCensor::censoredText() const
186 {
187  return mText;
188 }
189 
190 //-----------------------------------------------------------------------------
191 // text censorship helper functions
192 //
193 
194 bool SpellingFilter::TextCensor::atLineStart() const
195 {
196  return
197  ( mPos == 0 && mText.length() > 0 ) ||
198  ( mText[mPos - 1] == QLatin1Char('\n') );
199 }
200 
201 void SpellingFilter::TextCensor::skipLine()
202 {
203  mPos = mText.indexOf( QLatin1Char('\n'), mPos );
204  if ( mPos == -1 ) {
205  mPos = mText.length();
206  } else {
207  ++mPos;
208  }
209 }
210 
211 bool SpellingFilter::TextCensor::atQuotation( const QString &quotePrefix ) const
212 {
213  return atLineStart() &&
214  mText.mid( mPos, quotePrefix.length() ) == quotePrefix;
215 }
216 
217 void SpellingFilter::TextCensor::skipQuotation( const QString &quotePrefix )
218 {
219  while ( atQuotation( quotePrefix ) ) {
220  skipLine();
221  }
222 }
223 
224 void SpellingFilter::TextCensor::findQuotation( const QString &quotePrefix )
225 {
226  while ( mPos < mText.length() &&
227  !atQuotation( quotePrefix ) ) {
228  skipLine();
229  }
230 }
231 
232 void SpellingFilter::TextCensor::findEmailAddress()
233 {
234  while ( mPos < mText.length() && mText[mPos] != QLatin1Char( '@' ) ) {
235  ++mPos;
236  }
237 }
QString::indexOf
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
QString::fill
QString & fill(QChar ch, int size)
QList::const_iterator
KPIMUtils::SpellingFilter::SpellingFilter
SpellingFilter(const QString &text, const QString &quotePrefix, UrlFiltering filterUrls=FilterUrls, EmailAddressFiltering filterEmailAddresses=FilterEmailAddresses, const QStringList &filterStrings=QStringList())
Private class that helps to provide binary compatibility between releases.
Definition: spellingfilter.cpp:53
QString::isEmpty
bool isEmpty() const
KPIMUtils::SpellingFilter
Definition: spellingfilter.h:44
KPIMUtils::LinkLocator
LinkLocator assists in identifying sections of text that can usefully be converted in hyperlinks in H...
Definition: linklocator.h:48
QString
QStringList
QList::end
iterator end()
QLatin1Char
spellingfilter.h
This file is part of the KDEPIM Utilities library and provides the SpellingFilter class...
QString::replace
QString & replace(int position, int n, QChar after)
QString::length
int length() const
QList::begin
iterator begin()
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:37:25 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kpimutils

Skip menu "kpimutils"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal