~ubuntu-branches/ubuntu/natty/mozvoikko/natty-proposed

« back to all changes in this revision

Viewing changes to .pc/port_to_firefox5.patch/src/mozVoikkoSpell.cpp

  • Committer: Package Import Robot
  • Author(s): Chris Coulson
  • Date: 2011-12-20 00:19:52 UTC
  • mfrom: (18.1.3 natty-security)
  • Revision ID: package-import@ubuntu.com-20111220001952-kgbjh71gwu6l8023
Tags: 1.10.0-0ubuntu0.11.04.4
* Update for Firefox 9 (LP: #906389)
  - update debian/patches/port_to_latest_firefox.patch for
    mozISpellCheckingEngine changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
 
/* ***** BEGIN LICENSE BLOCK *****
3
 
 *
4
 
 * This program is free software; you can redistribute it and/or
5
 
 * modify it under the terms of the GNU General Public License
6
 
 * as published by the Free Software Foundation; either version 2
7
 
 * of the License, or (at your option) any later version.
8
 
 *
9
 
 * This program is distributed in the hope that it will be useful,
10
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
 * GNU General Public License for more details.
13
 
 *
14
 
 * You should have received a copy of the GNU General Public License
15
 
 * along with this program; if not, write to the Free Software
16
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17
 
 *
18
 
 * Fragments of this code are taken from Mozilla Spellchecker Component:
19
 
 *
20
 
 * The Initial Developer of the Original Code of Mozilla Spellchecker Component is
21
 
 * David Einstein.
22
 
 * Portions created by the Initial Developer are Copyright (C) 2001
23
 
 * the Initial Developer. All Rights Reserved.
24
 
 * Adapted for use of Voikko by Andris Pavenis <andris.pavenis@iki.fi>
25
 
 *
26
 
 * Contributor(s): David Einstein <Deinst@world.std.com>
27
 
 *                 Kevin Hendricks <kevin.hendricks@sympatico.ca>
28
 
 *                 Michiel van Leeuwen <mvl@exedo.nl>
29
 
 *                 Harri Pitkänen <hatapitk@iki.fi>
30
 
 *
31
 
 *  This spellchecker is based on the MySpell spellchecker made for Open Office
32
 
 *  by Kevin Hendricks.  Although the algorithms and code, have changed 
33
 
 *  slightly, the architecture is still the same. The Mozilla implementation
34
 
 *  is designed to be compatible with the Open Office dictionaries.
35
 
 *  Please do not make changes to the affix or dictionary file formats 
36
 
 *  without attempting to coordinate with Kevin.  For more information 
37
 
 *  on the original MySpell see 
38
 
 *  http://whiteboard.openoffice.org/source/browse/whiteboard/lingucomponent/source/spellcheck/myspell/
39
 
 *
40
 
 *  A special thanks and credit goes to Geoff Kuenning
41
 
 * the creator of ispell.  MySpell's affix algorithms were
42
 
 * based on those of ispell which should be noted is
43
 
 * copyright Geoff Kuenning et.al. and now available
44
 
 * under a BSD style license. For more information on ispell
45
 
 * and affix compression in general, please see:
46
 
 * http://www.cs.ucla.edu/ficus-members/geoff/ispell.html
47
 
 * (the home page for ispell)
48
 
 *
49
 
 * ***** END LICENSE BLOCK ***** */
50
 
 
51
 
#include "mozVoikkoSpell.hxx"
52
 
#include "mozVoikkoUtils.hxx"
53
 
#include <nsISimpleEnumerator.h>
54
 
#include <nsServiceManagerUtils.h>
55
 
#include <nsICategoryManager.h>
56
 
#include <mozISpellI18NManager.h>
57
 
#include <nsICharsetConverterManager.h>
58
 
#include <nsISupportsPrimitives.h>
59
 
#include <nsUnicharUtilCIID.h>
60
 
#include <nsUnicharUtils.h>
61
 
#include <nsStringAPI.h>
62
 
#include <nsEmbedString.h>
63
 
#include <nsCRTGlue.h>
64
 
#include <nsMemory.h>
65
 
#include <stdlib.h>
66
 
 
67
 
#define MOZVOIKKO_LANGUAGE_STRING "fi-FI"
68
 
 
69
 
const PRInt32 kFirstDirSize=8;
70
 
static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID);
71
 
static NS_DEFINE_CID(kUnicharUtilCID, NS_UNICHARUTIL_CID);
72
 
 
73
 
NS_IMPL_ISUPPORTS1(mozVoikkoSpell, mozISpellCheckingEngine)
74
 
 
75
 
mozVoikkoSpell::mozVoikkoSpell()
76
 
{
77
 
    voikkoSpell = NULL;
78
 
}
79
 
 
80
 
mozVoikkoSpell::~mozVoikkoSpell()
81
 
{
82
 
    mPersonalDictionary = nsnull;
83
 
    delete voikkoSpell;
84
 
}
85
 
 
86
 
/* attribute wstring dictionary; */
87
 
NS_IMETHODIMP mozVoikkoSpell::GetDictionary(PRUnichar **aDictionary)
88
 
{
89
 
    NS_ENSURE_ARG_POINTER(aDictionary);
90
 
 
91
 
    *aDictionary = ToNewUnicode(mDictionary);
92
 
    return *aDictionary ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
93
 
}
94
 
 
95
 
/* set the Dictionary.
96
 
 * This also Loads the dictionary and initializes the converter using the dictionaries converter
97
 
 */
98
 
NS_IMETHODIMP mozVoikkoSpell::SetDictionary(const PRUnichar *aDictionary)
99
 
{
100
 
    NS_ENSURE_ARG_POINTER(aDictionary);
101
 
 
102
 
    nsString newDict(aDictionary);
103
 
    nsresult rv = NS_OK;
104
 
 
105
 
    if (!newDict.Equals(NS_LITERAL_STRING(MOZVOIKKO_LANGUAGE_STRING)))
106
 
    {
107
 
        logMessage("mozVoikko: dictionary '%s' is not supported",
108
 
                   NS_ConvertUTF16toUTF8(newDict).get());
109
 
        return NS_ERROR_FAILURE;
110
 
    }
111
 
 
112
 
    if (!mDictionary.Equals(newDict))
113
 
    {
114
 
        mDictionary = aDictionary;
115
 
        // FIXME: So we need that?.
116
 
        delete voikkoSpell;
117
 
 
118
 
        voikkoSpell = new MozVoikko();
119
 
        if (!voikkoSpell)
120
 
            return NS_ERROR_FAILURE;
121
 
 
122
 
        nsCOMPtr<nsICharsetConverterManager> ccm = do_GetService(kCharsetConverterManagerCID, &rv);
123
 
        NS_ENSURE_SUCCESS(rv, rv);
124
 
 
125
 
        rv = ccm->GetUnicodeDecoder(voikkoSpell->get_dic_encoding(), getter_AddRefs(mDecoder));
126
 
        NS_ENSURE_SUCCESS(rv, rv);
127
 
        rv = ccm->GetUnicodeEncoder(voikkoSpell->get_dic_encoding(), getter_AddRefs(mEncoder));
128
 
        if (mEncoder && NS_SUCCEEDED(rv))
129
 
        {
130
 
            mEncoder->SetOutputErrorBehavior(mEncoder->kOnError_Signal, nsnull, '?');
131
 
        }
132
 
 
133
 
        NS_ENSURE_SUCCESS(rv, rv);
134
 
        mLanguage.Assign(newDict);
135
 
 
136
 
        return rv;
137
 
    }
138
 
    else
139
 
    {
140
 
        return NS_OK;
141
 
    } 
142
 
}
143
 
 
144
 
/* readonly attribute wstring language; */
145
 
NS_IMETHODIMP mozVoikkoSpell::GetLanguage(PRUnichar **aLanguage)
146
 
{
147
 
    NS_ENSURE_ARG_POINTER(aLanguage);
148
 
 
149
 
    *aLanguage = ToNewUnicode(mLanguage);
150
 
    return *aLanguage ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
151
 
}
152
 
 
153
 
/* readonly attribute boolean providesPersonalDictionary; */
154
 
NS_IMETHODIMP mozVoikkoSpell::GetProvidesPersonalDictionary(PRBool *aProvidesPersonalDictionary)
155
 
{
156
 
    NS_ENSURE_ARG_POINTER(aProvidesPersonalDictionary);
157
 
 
158
 
    *aProvidesPersonalDictionary = PR_FALSE;
159
 
    return NS_OK;
160
 
}
161
 
 
162
 
/* readonly attribute boolean providesWordUtils; */
163
 
NS_IMETHODIMP mozVoikkoSpell::GetProvidesWordUtils(PRBool *aProvidesWordUtils)
164
 
{
165
 
    NS_ENSURE_ARG_POINTER(aProvidesWordUtils);
166
 
 
167
 
    *aProvidesWordUtils = PR_FALSE;
168
 
    return NS_OK;
169
 
}
170
 
 
171
 
/* readonly attribute wstring name; */
172
 
NS_IMETHODIMP mozVoikkoSpell::GetName(PRUnichar * *aName)
173
 
{
174
 
    return NS_ERROR_NOT_IMPLEMENTED;
175
 
}
176
 
 
177
 
/* readonly attribute wstring copyright; */
178
 
NS_IMETHODIMP mozVoikkoSpell::GetCopyright(PRUnichar * *aCopyright)
179
 
{
180
 
    return NS_ERROR_NOT_IMPLEMENTED;
181
 
}
182
 
 
183
 
/* attribute mozIPersonalDictionary personalDictionary; */
184
 
NS_IMETHODIMP mozVoikkoSpell::GetPersonalDictionary(mozIPersonalDictionary * *aPersonalDictionary)
185
 
{
186
 
    *aPersonalDictionary = mPersonalDictionary;
187
 
    NS_IF_ADDREF(*aPersonalDictionary);
188
 
    return NS_OK;
189
 
}
190
 
 
191
 
NS_IMETHODIMP mozVoikkoSpell::SetPersonalDictionary(mozIPersonalDictionary * aPersonalDictionary)
192
 
{
193
 
    mPersonalDictionary = aPersonalDictionary;
194
 
    return NS_OK;
195
 
}
196
 
 
197
 
/* void GetDictionaryList ([array, size_is (count)] out wstring dictionaries, out PRUint32 count); */
198
 
NS_IMETHODIMP mozVoikkoSpell::GetDictionaryList(PRUnichar ***aDictionaries, PRUint32 *aCount)
199
 
{
200
 
    if (!aDictionaries || !aCount)
201
 
        return NS_ERROR_NULL_POINTER;
202
 
 
203
 
    *aDictionaries = 0;
204
 
    *aCount = 0;
205
 
 
206
 
    PRUnichar **tmpPtr = (PRUnichar **) NS_Alloc(sizeof(PRUnichar *));
207
 
    if (!tmpPtr)
208
 
        return NS_ERROR_OUT_OF_MEMORY;
209
 
 
210
 
    do
211
 
    {
212
 
        MozVoikko voikkoSpell;
213
 
        if (voikkoSpell)
214
 
        {
215
 
            nsAutoString voikkoDictName(NS_LITERAL_STRING(MOZVOIKKO_LANGUAGE_STRING).get());
216
 
            tmpPtr[0] = ToNewUnicode(voikkoDictName);
217
 
            *aCount = 1;
218
 
            *aDictionaries = tmpPtr;
219
 
            return NS_OK;
220
 
        }
221
 
    } while(false);
222
 
 
223
 
    NS_Free(tmpPtr);    
224
 
    return NS_OK;
225
 
}
226
 
 
227
 
nsresult mozVoikkoSpell::ConvertCharset(const PRUnichar* aStr, char ** aDst)
228
 
{
229
 
    NS_ENSURE_ARG_POINTER(aDst);
230
 
    NS_ENSURE_TRUE(mEncoder, NS_ERROR_NULL_POINTER);
231
 
 
232
 
    PRInt32 outLength;
233
 
    PRInt32 inLength = NS_strlen(aStr);
234
 
    nsresult rv = mEncoder->GetMaxLength(aStr, inLength, &outLength);
235
 
    NS_ENSURE_SUCCESS(rv, rv);
236
 
 
237
 
    *aDst = (char *) NS_Alloc(sizeof(char) * (outLength+1));
238
 
    NS_ENSURE_TRUE(*aDst, NS_ERROR_OUT_OF_MEMORY);
239
 
 
240
 
    rv = mEncoder->Convert(aStr, &inLength, *aDst, &outLength);
241
 
    if (NS_SUCCEEDED(rv))
242
 
        (*aDst)[outLength] = '\0'; 
243
 
 
244
 
    return rv;
245
 
}
246
 
 
247
 
/* boolean Check (in wstring word); */
248
 
NS_IMETHODIMP mozVoikkoSpell::Check(const PRUnichar *aWord, PRBool *aResult)
249
 
{
250
 
    NS_ENSURE_ARG_POINTER(aWord);
251
 
    NS_ENSURE_ARG_POINTER(aResult);
252
 
    NS_ENSURE_TRUE(voikkoSpell, NS_ERROR_FAILURE);
253
 
 
254
 
    char *charsetWord;
255
 
    nsresult rv = ConvertCharset(aWord, &charsetWord);
256
 
    NS_ENSURE_SUCCESS(rv, rv);
257
 
 
258
 
    *aResult = voikkoSpell->spell(charsetWord);
259
 
  
260
 
    NS_Free(charsetWord);
261
 
 
262
 
    if (!*aResult && mPersonalDictionary) 
263
 
        rv = mPersonalDictionary->Check(aWord, mLanguage.get(), aResult);
264
 
  
265
 
    return rv;
266
 
}
267
 
 
268
 
/* void Suggest (in wstring word, [array, size_is (count)] out wstring suggestions, out PRUint32 count); */
269
 
NS_IMETHODIMP mozVoikkoSpell::Suggest(const PRUnichar *aWord, PRUnichar ***aSuggestions, PRUint32 *aSuggestionCount)
270
 
{
271
 
    NS_ENSURE_ARG_POINTER(aSuggestions);
272
 
    NS_ENSURE_ARG_POINTER(aSuggestionCount);
273
 
    NS_ENSURE_TRUE(voikkoSpell, NS_ERROR_FAILURE);
274
 
 
275
 
    nsresult rv;
276
 
    *aSuggestionCount = 0;
277
 
 
278
 
    char *charsetWord;
279
 
    rv = ConvertCharset(aWord, &charsetWord);
280
 
    NS_ENSURE_SUCCESS(rv, rv);
281
 
 
282
 
    char ** wlst;
283
 
    *aSuggestionCount = voikkoSpell->suggest(&wlst, charsetWord);
284
 
    NS_Free(charsetWord);
285
 
 
286
 
    // This code is copy/pasted from mySpell (with format changes). Maybe
287
 
    // it can be optimized.
288
 
    if (*aSuggestionCount)
289
 
    {
290
 
        *aSuggestions  = (PRUnichar **) NS_Alloc(*aSuggestionCount * sizeof(PRUnichar *));
291
 
        if (*aSuggestions)
292
 
        {
293
 
            PRUint32 index = 0;
294
 
            for (index = 0; index < *aSuggestionCount && NS_SUCCEEDED(rv); ++index)
295
 
            {
296
 
                // Convert the suggestion to utf16
297
 
                PRInt32 inLength = strlen(wlst[index]);
298
 
                PRInt32 outLength;
299
 
                rv = mDecoder->GetMaxLength(wlst[index], inLength, &outLength);
300
 
                if (NS_SUCCEEDED(rv))
301
 
                {
302
 
                    (*aSuggestions)[index] = (PRUnichar *) NS_Alloc(sizeof(PRUnichar) * (outLength+1));
303
 
                    if ((*aSuggestions)[index])
304
 
                    {
305
 
                        rv = mDecoder->Convert(wlst[index], &inLength, (*aSuggestions)[index], &outLength);
306
 
                        if (NS_SUCCEEDED(rv))
307
 
                            (*aSuggestions)[index][outLength] = 0;
308
 
                    } 
309
 
                    else
310
 
                        rv = NS_ERROR_OUT_OF_MEMORY;
311
 
                }
312
 
            }
313
 
 
314
 
            if (NS_FAILED(rv))
315
 
                NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(index, *aSuggestions); // free the PRUnichar strings up to the point at which the error occurred
316
 
        }
317
 
        else // if (*aSuggestions)
318
 
            rv = NS_ERROR_OUT_OF_MEMORY;
319
 
    }
320
 
 
321
 
    voikkoSpell->freeSuggestions(wlst);
322
 
 
323
 
    return rv;
324
 
}