aboutsummaryrefslogtreecommitdiffstats
path: root/embed/mozilla/GeckoSpellCheckEngine.cpp
blob: 59ab5df2da08702f3c6db7585ae16b1087da938d (plain) (blame)
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
/*
 *  Copyright © 2006 Christian Persch
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2.1, or (at your option)
 *  any later version.
 *
 *  This program 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 Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *  $Id$
 */

#include "mozilla-config.h"
#include "config.h"

//#include <bonobo.h>
#include <stdio.h>
#include <stdlib.h>

#include <nsStringAPI.h>

#include <mozIPersonalDictionary.h>
#include <nsMemory.h>

#include "ephy-debug.h"

#include "GeckoSpellCheckEngine.h"

#ifndef HAVE_GECKO_1_9
#define ToNewUnicode NS_StringCloneData
#endif

GeckoSpellCheckEngine::GeckoSpellCheckEngine ()
{
  LOG ("GeckoSpellCheckEngine ctor [%p]", (void*) this);
  mSpeller = ephy_spell_check_get_default ();
}

GeckoSpellCheckEngine::~GeckoSpellCheckEngine ()
{
  LOG ("GeckoSpellCheckEngine dtor [%p]", (void*) this);
  g_object_unref (mSpeller);
}

NS_IMPL_ISUPPORTS1 (GeckoSpellCheckEngine,
            mozISpellCheckingEngine)

/* nsISpellCheckEngine implementation */

/* attribute wstring dictionary; */
NS_IMETHODIMP GeckoSpellCheckEngine::GetDictionary (PRUnichar * *aDictionary)
{
  /* Gets the identifier of the current dictionary */
  char *code = ephy_spell_check_get_language (mSpeller);
  if (!code) {
    return NS_ERROR_FAILURE;
  }

  *aDictionary = ToNewUnicode (NS_ConvertUTF8toUTF16 (code));
  g_free (code);

  return NS_OK;
}

NS_IMETHODIMP GeckoSpellCheckEngine::SetDictionary (const PRUnichar * aDictionary)
{
  return NS_OK;
}

/* readonly attribute wstring language; */
NS_IMETHODIMP GeckoSpellCheckEngine::GetLanguage (PRUnichar * *aLanguage)
{
  /* Gets the identifier of the current dictionary */
  char *code = ephy_spell_check_get_language (mSpeller);
  if (!code) {
    return NS_ERROR_FAILURE;
  }

  *aLanguage = ToNewUnicode (NS_ConvertUTF8toUTF16 (code));
  g_free (code);

  return NS_OK;
}

/* readonly attribute boolean providesPersonalDictionary; */
NS_IMETHODIMP GeckoSpellCheckEngine::GetProvidesPersonalDictionary (PRBool *aProvidesPersonalDictionary)
{
  *aProvidesPersonalDictionary = PR_FALSE;
  return NS_OK;
}

/* readonly attribute boolean providesWordUtils; */
NS_IMETHODIMP GeckoSpellCheckEngine::GetProvidesWordUtils (PRBool *aProvidesWordUtils)
{
  *aProvidesWordUtils = PR_FALSE;
  return NS_OK;
}

/* readonly attribute wstring name; */
NS_IMETHODIMP GeckoSpellCheckEngine::GetName (PRUnichar * *aName)
{
  /* It's fine to leave this unimplemented */
  return NS_ERROR_NOT_IMPLEMENTED;
}

/* readonly attribute wstring copyright; */
NS_IMETHODIMP GeckoSpellCheckEngine::GetCopyright (PRUnichar * *aCopyright)
{
  /* It's fine to leave this unimplemented */
  return NS_ERROR_NOT_IMPLEMENTED;
}

/* attribute mozIPersonalDictionary personalDictionary; */
NS_IMETHODIMP GeckoSpellCheckEngine::GetPersonalDictionary (mozIPersonalDictionary * *aPersonalDictionary)
{
  NS_IF_ADDREF (*aPersonalDictionary = mPersonalDictionary);
  return NS_OK;
}

NS_IMETHODIMP GeckoSpellCheckEngine::SetPersonalDictionary (mozIPersonalDictionary * aPersonalDictionary)
{
  mPersonalDictionary = aPersonalDictionary;
  return NS_OK;
}

/* void getDictionaryList ([array, size_is (count)] out wstring dictionaries, out PRUint32 count); */
NS_IMETHODIMP
GeckoSpellCheckEngine::GetDictionaryList (PRUnichar ***_dictionaries,
                      PRUint32 *_count)
{
  *_count = 1;
  *_dictionaries = (PRUnichar **)nsMemory::Alloc(sizeof(PRUnichar *)); // only one entry
  *_dictionaries[0] = ToNewUnicode (NS_LITERAL_STRING ("en"));
  return NS_OK;
}

/* boolean check (in wstring word); */
NS_IMETHODIMP GeckoSpellCheckEngine::Check (const PRUnichar *word,
                        PRBool *_retval)
{
  NS_ENSURE_STATE (mSpeller);
  NS_ENSURE_ARG (word);

  NS_ConvertUTF16toUTF8 converted (word);

  gboolean correct = FALSE;
  if (!ephy_spell_check_check_word (mSpeller,
                        converted.get (),
                    converted.Length (),
                    &correct))
      return NS_ERROR_FAILURE;

  *_retval = correct != FALSE;

  return NS_OK;
}

/* void suggest (in wstring word, [array, size_is (count)] out wstring suggestions, out PRUint32 count); */
NS_IMETHODIMP GeckoSpellCheckEngine::Suggest (const PRUnichar *word,
                          PRUnichar ***_suggestions,
                          PRUint32 *_count)
{
#if 0
  NS_ENSURE_STATE (mSpeller);
  NS_ENSURE_ARG (word);

  NS_ConvertUTF16toUTF8 converted (word);

  gsize count;
  char **suggestions = ephy_spell_check_get_suggestions (mSpeller,
                             converted.get (),
                             converted.Length (),
                             &count);

  *_count = count;
  *_suggestions = nsnull;

  PRUnichar **array = nsnull;
  if (count > 0) {
    NS_ASSERTION (suggestions, "Count > 0 but suggestions are NULL?");
    array = (PRUnichar **) nsMemory::Alloc (count * sizeof (PRUnichar *));
    if (array) {
      *_suggestions = array;

      for (gsize i = 0; i < count; ++i) {
        NS_ConvertUTF8toUTF16 sugg (suggestions[i]);
    array[i] = ToNewUnicode (sugg);
      }
    }

    ephy_spell_check_free_suggestions (mSpeller, suggestions);
  }

  return array ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
#endif
  return NS_ERROR_NOT_IMPLEMENTED;
}