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

kpimutils

spellingfilter.cpp
Go to the documentation of this file.
00001 /*
00002  * spellingfilter.cpp
00003  *
00004  * Copyright (c) 2002 Dave Corrie <kde@davecorrie.com>
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Library General Public License
00017  * along with this library; see the file COPYING.LIB.  If not, write to
00018  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020  */
00032 #include "spellingfilter.h"
00033 
00034 using namespace KPIMUtils;
00035 
00040 //@cond PRIVATE
00041 class KPIMUtils::SpellingFilter::Private
00042 {
00043   public:
00044     QString mOriginal;
00045     QString mFiltered;
00046 };
00047 //@endcond
00048 
00049 //-----------------------------------------------------------------------------
00050 // SpellingFilter implementation
00051 //
00052 
00053 SpellingFilter::SpellingFilter( const QString &text,
00054                                 const QString &quotePrefix,
00055                                 UrlFiltering filterUrls,
00056                                 EmailAddressFiltering filterEmailAddresses,
00057                                 const QStringList &filterStrings )
00058   : d( new KPIMUtils::SpellingFilter::Private )
00059 {
00060   d->mOriginal = text;
00061   TextCensor c( text );
00062 
00063   if ( !quotePrefix.isEmpty() ) {
00064     c.censorQuotations( quotePrefix );
00065   }
00066 
00067   if ( filterUrls ) {
00068     c.censorUrls();
00069   }
00070 
00071   if ( filterEmailAddresses ) {
00072     c.censorEmailAddresses();
00073   }
00074 
00075   QStringList::const_iterator iter = filterStrings.begin();
00076   while ( iter != filterStrings.end() ) {
00077     c.censorString( *iter );
00078     ++iter;
00079   }
00080 
00081   d->mFiltered = c.censoredText();
00082 }
00083 
00084 SpellingFilter::~SpellingFilter()
00085 {
00086   delete d;
00087 }
00088 
00089 QString SpellingFilter::originalText() const
00090 {
00091   return d->mOriginal;
00092 }
00093 
00094 QString SpellingFilter::filteredText() const
00095 {
00096   return d->mFiltered;
00097 }
00098 
00099 //-----------------------------------------------------------------------------
00100 // SpellingFilter::TextCensor implementation
00101 //
00102 
00103 SpellingFilter::TextCensor::TextCensor( const QString &s )
00104   : LinkLocator( s )
00105 {
00106 }
00107 
00108 void SpellingFilter::TextCensor::censorQuotations( const QString &quotePrefix )
00109 {
00110   mPos = 0;
00111   while ( mPos < mText.length() ) {
00112     // Find start of quotation
00113     findQuotation( quotePrefix );
00114     if ( mPos < mText.length() ) {
00115       int start = mPos;
00116       skipQuotation( quotePrefix );
00117 
00118       // Replace quotation with spaces
00119       int len = mPos - start;
00120       QString spaces;
00121       spaces.fill( ' ', len );
00122       mText.replace( start, len, spaces );
00123     }
00124   }
00125 }
00126 
00127 void SpellingFilter::TextCensor::censorUrls()
00128 {
00129   mPos = 0;
00130   while ( mPos < mText.length() ) {
00131     // Find start of url
00132     QString url;
00133     while ( mPos < mText.length() && url.isEmpty() ) {
00134       url = getUrl();
00135       ++mPos;
00136     }
00137 
00138     if ( mPos < mText.length() && !url.isEmpty() ) {
00139       int start = mPos - url.length();
00140 
00141       // Replace url with spaces
00142       url.fill( ' ' );
00143       mText.replace( start, url.length(), url );
00144     }
00145   }
00146 }
00147 
00148 void SpellingFilter::TextCensor::censorEmailAddresses()
00149 {
00150   mPos = 0;
00151   while ( mPos < mText.length() ) {
00152     // Find start of email address
00153     findEmailAddress();
00154     if ( mPos < mText.length() ) {
00155       QString address = getEmailAddress();
00156       ++mPos;
00157       if ( !address.isEmpty() ) {
00158         int start = mPos - address.length();
00159 
00160         // Replace address with spaces
00161         address.fill( ' ' );
00162         mText.replace( start, address.length(), address );
00163       }
00164     }
00165   }
00166 }
00167 
00168 void SpellingFilter::TextCensor::censorString( const QString &s )
00169 {
00170   mPos = 0;
00171   while ( mPos != -1 ) {
00172     // Find start of string
00173     mPos = mText.indexOf( s, mPos );
00174     if ( mPos != -1 ) {
00175       // Replace string with spaces
00176       QString spaces;
00177       spaces.fill( ' ', s.length() );
00178       mText.replace( mPos, s.length(), spaces );
00179       mPos += s.length();
00180     }
00181   }
00182 }
00183 
00184 QString SpellingFilter::TextCensor::censoredText() const
00185 {
00186   return mText;
00187 }
00188 
00189 //-----------------------------------------------------------------------------
00190 // text censorship helper functions
00191 //
00192 
00193 bool SpellingFilter::TextCensor::atLineStart() const
00194 {
00195   return
00196     ( mPos == 0 && mText.length() > 0 ) ||
00197     ( mText[mPos - 1] == '\n' );
00198 }
00199 
00200 void SpellingFilter::TextCensor::skipLine()
00201 {
00202   mPos = mText.indexOf( '\n', mPos );
00203   if ( mPos == -1 ) {
00204     mPos = mText.length();
00205   } else {
00206     ++mPos;
00207   }
00208 }
00209 
00210 bool SpellingFilter::TextCensor::atQuotation( const QString &quotePrefix ) const
00211 {
00212   return atLineStart() &&
00213     mText.mid( mPos, quotePrefix.length() ) == quotePrefix;
00214 }
00215 
00216 void SpellingFilter::TextCensor::skipQuotation( const QString &quotePrefix )
00217 {
00218   while ( atQuotation( quotePrefix ) ) {
00219     skipLine();
00220   }
00221 }
00222 
00223 void SpellingFilter::TextCensor::findQuotation( const QString &quotePrefix )
00224 {
00225   while ( mPos < mText.length() &&
00226           !atQuotation( quotePrefix ) ) {
00227     skipLine();
00228   }
00229 }
00230 
00231 void SpellingFilter::TextCensor::findEmailAddress()
00232 {
00233   while ( mPos < mText.length() && mText[mPos] != '@' ) {
00234     ++mPos;
00235   }
00236 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 22:17:42 by doxygen 1.8.0 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

kdepimlibs-4.8.3 API Reference

Skip menu "kdepimlibs-4.8.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
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