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

« back to all changes in this revision

Viewing changes to .pc/port_to_latest_firefox.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
NS_IMETHODIMP mozVoikkoSpell::LoadDictionariesFromDir(nsIFile* aFile)
 
87
{
 
88
        return NS_ERROR_NOT_IMPLEMENTED;
 
89
}
 
90
 
 
91
/* attribute wstring dictionary; */
 
92
NS_IMETHODIMP mozVoikkoSpell::GetDictionary(PRUnichar **aDictionary)
 
93
{
 
94
    NS_ENSURE_ARG_POINTER(aDictionary);
 
95
 
 
96
    *aDictionary = ToNewUnicode(mDictionary);
 
97
    return *aDictionary ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
 
98
}
 
99
 
 
100
/* set the Dictionary.
 
101
 * This also Loads the dictionary and initializes the converter using the dictionaries converter
 
102
 */
 
103
NS_IMETHODIMP mozVoikkoSpell::SetDictionary(const PRUnichar *aDictionary)
 
104
{
 
105
    NS_ENSURE_ARG_POINTER(aDictionary);
 
106
 
 
107
    nsString newDict(aDictionary);
 
108
    nsresult rv = NS_OK;
 
109
 
 
110
    if (!newDict.Equals(NS_LITERAL_STRING(MOZVOIKKO_LANGUAGE_STRING)))
 
111
    {
 
112
        logMessage("mozVoikko: dictionary '%s' is not supported",
 
113
                   NS_ConvertUTF16toUTF8(newDict).get());
 
114
        return NS_ERROR_FAILURE;
 
115
    }
 
116
 
 
117
    if (!mDictionary.Equals(newDict))
 
118
    {
 
119
        mDictionary = aDictionary;
 
120
        // FIXME: So we need that?.
 
121
        delete voikkoSpell;
 
122
 
 
123
        voikkoSpell = new MozVoikko();
 
124
        if (!voikkoSpell)
 
125
            return NS_ERROR_FAILURE;
 
126
 
 
127
        mLanguage.Assign(newDict);
 
128
 
 
129
        return rv;
 
130
    }
 
131
    else
 
132
    {
 
133
        return NS_OK;
 
134
    } 
 
135
}
 
136
 
 
137
/* readonly attribute wstring language; */
 
138
NS_IMETHODIMP mozVoikkoSpell::GetLanguage(PRUnichar **aLanguage)
 
139
{
 
140
    NS_ENSURE_ARG_POINTER(aLanguage);
 
141
 
 
142
    *aLanguage = ToNewUnicode(mLanguage);
 
143
    return *aLanguage ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
 
144
}
 
145
 
 
146
/* readonly attribute boolean providesPersonalDictionary; */
 
147
NS_IMETHODIMP mozVoikkoSpell::GetProvidesPersonalDictionary(PRBool *aProvidesPersonalDictionary)
 
148
{
 
149
    NS_ENSURE_ARG_POINTER(aProvidesPersonalDictionary);
 
150
 
 
151
    *aProvidesPersonalDictionary = PR_FALSE;
 
152
    return NS_OK;
 
153
}
 
154
 
 
155
/* readonly attribute boolean providesWordUtils; */
 
156
NS_IMETHODIMP mozVoikkoSpell::GetProvidesWordUtils(PRBool *aProvidesWordUtils)
 
157
{
 
158
    NS_ENSURE_ARG_POINTER(aProvidesWordUtils);
 
159
 
 
160
    *aProvidesWordUtils = PR_FALSE;
 
161
    return NS_OK;
 
162
}
 
163
 
 
164
/* readonly attribute wstring name; */
 
165
NS_IMETHODIMP mozVoikkoSpell::GetName(PRUnichar * *aName)
 
166
{
 
167
    return NS_ERROR_NOT_IMPLEMENTED;
 
168
}
 
169
 
 
170
/* readonly attribute wstring copyright; */
 
171
NS_IMETHODIMP mozVoikkoSpell::GetCopyright(PRUnichar * *aCopyright)
 
172
{
 
173
    return NS_ERROR_NOT_IMPLEMENTED;
 
174
}
 
175
 
 
176
/* attribute mozIPersonalDictionary personalDictionary; */
 
177
NS_IMETHODIMP mozVoikkoSpell::GetPersonalDictionary(mozIPersonalDictionary * *aPersonalDictionary)
 
178
{
 
179
    *aPersonalDictionary = mPersonalDictionary;
 
180
    NS_IF_ADDREF(*aPersonalDictionary);
 
181
    return NS_OK;
 
182
}
 
183
 
 
184
NS_IMETHODIMP mozVoikkoSpell::SetPersonalDictionary(mozIPersonalDictionary * aPersonalDictionary)
 
185
{
 
186
    mPersonalDictionary = aPersonalDictionary;
 
187
    return NS_OK;
 
188
}
 
189
 
 
190
/* void GetDictionaryList ([array, size_is (count)] out wstring dictionaries, out PRUint32 count); */
 
191
NS_IMETHODIMP mozVoikkoSpell::GetDictionaryList(PRUnichar ***aDictionaries, PRUint32 *aCount)
 
192
{
 
193
    if (!aDictionaries || !aCount)
 
194
        return NS_ERROR_NULL_POINTER;
 
195
 
 
196
    *aDictionaries = 0;
 
197
    *aCount = 0;
 
198
 
 
199
    PRUnichar **tmpPtr = (PRUnichar **) NS_Alloc(sizeof(PRUnichar *));
 
200
    if (!tmpPtr)
 
201
        return NS_ERROR_OUT_OF_MEMORY;
 
202
 
 
203
    do
 
204
    {
 
205
        MozVoikko voikkoSpell;
 
206
        if (voikkoSpell)
 
207
        {
 
208
            nsAutoString voikkoDictName(NS_LITERAL_STRING(MOZVOIKKO_LANGUAGE_STRING).get());
 
209
            tmpPtr[0] = ToNewUnicode(voikkoDictName);
 
210
            *aCount = 1;
 
211
            *aDictionaries = tmpPtr;
 
212
            return NS_OK;
 
213
        }
 
214
    } while(false);
 
215
 
 
216
    NS_Free(tmpPtr);    
 
217
    return NS_OK;
 
218
}
 
219
 
 
220
nsresult mozVoikkoSpell::ConvertCharset(const PRUnichar* aStr, char ** aDst)
 
221
{
 
222
    NS_ENSURE_ARG_POINTER(aDst);
 
223
 
 
224
    nsString str_u16(aStr);
 
225
    nsCString str_u8 = NS_ConvertUTF16toUTF8(str_u16);
 
226
    const char *str = str_u8.get();
 
227
    *aDst = (char*)NS_Alloc(1 + strlen(str));
 
228
    strcpy(*aDst, str);
 
229
 
 
230
    return NS_OK;
 
231
}
 
232
 
 
233
/* boolean Check (in wstring word); */
 
234
NS_IMETHODIMP mozVoikkoSpell::Check(const PRUnichar *aWord, PRBool *aResult)
 
235
{
 
236
    NS_ENSURE_ARG_POINTER(aWord);
 
237
    NS_ENSURE_ARG_POINTER(aResult);
 
238
    NS_ENSURE_TRUE(voikkoSpell, NS_ERROR_FAILURE);
 
239
 
 
240
    char *charsetWord;
 
241
    nsresult rv = ConvertCharset(aWord, &charsetWord);
 
242
    NS_ENSURE_SUCCESS(rv, rv);
 
243
 
 
244
    *aResult = voikkoSpell->spell(charsetWord);
 
245
  
 
246
    NS_Free(charsetWord);
 
247
 
 
248
    if (!*aResult && mPersonalDictionary) 
 
249
        rv = mPersonalDictionary->Check(aWord, mLanguage.get(), aResult);
 
250
  
 
251
    return rv;
 
252
}
 
253
 
 
254
/* void Suggest (in wstring word, [array, size_is (count)] out wstring suggestions, out PRUint32 count); */
 
255
NS_IMETHODIMP mozVoikkoSpell::Suggest(const PRUnichar *aWord, PRUnichar ***aSuggestions, PRUint32 *aSuggestionCount)
 
256
{
 
257
    NS_ENSURE_ARG_POINTER(aSuggestions);
 
258
    NS_ENSURE_ARG_POINTER(aSuggestionCount);
 
259
    NS_ENSURE_TRUE(voikkoSpell, NS_ERROR_FAILURE);
 
260
 
 
261
    nsresult rv;
 
262
    *aSuggestionCount = 0;
 
263
 
 
264
    char *charsetWord;
 
265
    rv = ConvertCharset(aWord, &charsetWord);
 
266
    NS_ENSURE_SUCCESS(rv, rv);
 
267
 
 
268
    char ** wlst;
 
269
    *aSuggestionCount = voikkoSpell->suggest(&wlst, charsetWord);
 
270
    NS_Free(charsetWord);
 
271
 
 
272
    // This code is copy/pasted from mySpell (with format changes). Maybe
 
273
    // it can be optimized.
 
274
    if (*aSuggestionCount)
 
275
    {
 
276
        *aSuggestions  = (PRUnichar **) NS_Alloc(*aSuggestionCount * sizeof(PRUnichar *));
 
277
        if (*aSuggestions)
 
278
        {
 
279
            PRUint32 index = 0;
 
280
            for (index = 0; index < *aSuggestionCount && NS_SUCCEEDED(rv); ++index)
 
281
            {
 
282
                // Convert the suggestion to utf16
 
283
                nsString str_u16 = NS_ConvertUTF8toUTF16(wlst[index]);
 
284
                PRUint32 len = str_u16.Length();
 
285
                PRUnichar *tmp = (PRUnichar *)NS_Alloc(sizeof(PRUnichar) * (1 + len));
 
286
                memcpy(tmp, str_u16.get(), sizeof(PRUnichar) * (1 + len));
 
287
                (*aSuggestions)[index] = tmp;
 
288
            }
 
289
 
 
290
            if (NS_FAILED(rv))
 
291
                NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(index, *aSuggestions); // free the PRUnichar strings up to the point at which the error occurred
 
292
        }
 
293
        else // if (*aSuggestions)
 
294
            rv = NS_ERROR_OUT_OF_MEMORY;
 
295
    }
 
296
 
 
297
    voikkoSpell->freeSuggestions(wlst);
 
298
 
 
299
    return rv;
 
300
}