summaryrefslogtreecommitdiff
path: root/kpimutils/spellingfilter.cpp
blob: 03097c33dc58f0ed003c7ecbe6c0242c87d31dec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 * spellingfilter.cpp
 *
 * Copyright (c) 2002 Dave Corrie <kde@davecorrie.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */
/**
  @file
  This file is part of the KDEPIM Utilities library and provides the
  SpellingFilter class.

  @brief
  Filters message text that should not be spellchecked.

  @author Dave Corrie \<kde@davecorrie.com\>
*/

#include "spellingfilter.h"

using namespace KPIMUtils;

/**
  Private class that helps to provide binary compatibility between releases.
  @internal
*/
//@cond PRIVATE
class KPIMUtils::SpellingFilter::Private
{
  public:
    QString mOriginal;
    QString mFiltered;
};
//@endcond

//-----------------------------------------------------------------------------
// SpellingFilter implementation
//

SpellingFilter::SpellingFilter( const QString &text,
                                const QString &quotePrefix,
                                UrlFiltering filterUrls,
                                EmailAddressFiltering filterEmailAddresses,
                                const QStringList &filterStrings )
  : d( new KPIMUtils::SpellingFilter::Private )
{
  d->mOriginal = text;
  TextCensor c( text );

  if ( !quotePrefix.isEmpty() ) {
    c.censorQuotations( quotePrefix );
  }

  if ( filterUrls ) {
    c.censorUrls();
  }

  if ( filterEmailAddresses ) {
    c.censorEmailAddresses();
  }

  QStringList::const_iterator iter = filterStrings.begin();
  while ( iter != filterStrings.end() ) {
    c.censorString( *iter );
    ++iter;
  }

  d->mFiltered = c.censoredText();
}

SpellingFilter::~SpellingFilter()
{
  delete d;
}

QString SpellingFilter::originalText() const
{
  return d->mOriginal;
}

QString SpellingFilter::filteredText() const
{
  return d->mFiltered;
}

//-----------------------------------------------------------------------------
// SpellingFilter::TextCensor implementation
//

SpellingFilter::TextCensor::TextCensor( const QString &s )
  : LinkLocator( s )
{
}

void SpellingFilter::TextCensor::censorQuotations( const QString &quotePrefix )
{
  mPos = 0;
  while ( mPos < mText.length() ) {
    // Find start of quotation
    findQuotation( quotePrefix );
    if ( mPos < mText.length() ) {
      int start = mPos;
      skipQuotation( quotePrefix );

      // Replace quotation with spaces
      int len = mPos - start;
      QString spaces;
      spaces.fill( ' ', len );
      mText.replace( start, len, spaces );
    }
  }
}

void SpellingFilter::TextCensor::censorUrls()
{
  mPos = 0;
  while ( mPos < mText.length() ) {
    // Find start of url
    QString url;
    while ( mPos < mText.length() && url.isEmpty() ) {
      url = getUrl();
      ++mPos;
    }

    if ( mPos < mText.length() && !url.isEmpty() ) {
      int start = mPos - url.length();

      // Replace url with spaces
      url.fill( ' ' );
      mText.replace( start, url.length(), url );
    }
  }
}

void SpellingFilter::TextCensor::censorEmailAddresses()
{
  mPos = 0;
  while ( mPos < mText.length() ) {
    // Find start of email address
    findEmailAddress();
    if ( mPos < mText.length() ) {
      QString address = getEmailAddress();
      ++mPos;
      if ( !address.isEmpty() ) {
        int start = mPos - address.length();

        // Replace address with spaces
        address.fill( ' ' );
        mText.replace( start, address.length(), address );
      }
    }
  }
}

void SpellingFilter::TextCensor::censorString( const QString &s )
{
  mPos = 0;
  while ( mPos != -1 ) {
    // Find start of string
    mPos = mText.indexOf( s, mPos );
    if ( mPos != -1 ) {
      // Replace string with spaces
      QString spaces;
      spaces.fill( ' ', s.length() );
      mText.replace( mPos, s.length(), spaces );
      mPos += s.length();
    }
  }
}

QString SpellingFilter::TextCensor::censoredText() const
{
  return mText;
}

//-----------------------------------------------------------------------------
// text censorship helper functions
//

bool SpellingFilter::TextCensor::atLineStart() const
{
  return
    ( mPos == 0 && mText.length() > 0 ) ||
    ( mText[mPos - 1] == '\n' );
}

void SpellingFilter::TextCensor::skipLine()
{
  mPos = mText.indexOf( '\n', mPos );
  if ( mPos == -1 ) {
    mPos = mText.length();
  } else {
    ++mPos;
  }
}

bool SpellingFilter::TextCensor::atQuotation( const QString &quotePrefix ) const
{
  return atLineStart() &&
    mText.mid( mPos, quotePrefix.length() ) == quotePrefix;
}

void SpellingFilter::TextCensor::skipQuotation( const QString &quotePrefix )
{
  while ( atQuotation( quotePrefix ) ) {
    skipLine();
  }
}

void SpellingFilter::TextCensor::findQuotation( const QString &quotePrefix )
{
  while ( mPos < mText.length() &&
          !atQuotation( quotePrefix ) ) {
    skipLine();
  }
}

void SpellingFilter::TextCensor::findEmailAddress()
{
  while ( mPos < mText.length() && mText[mPos] != '@' ) {
    ++mPos;
  }
}