Back to index

lightning-sunbird  0.9+nobinonly
mozOSXSpell.mm
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Spellchecker Component.
00016  *
00017  * The Initial Developer of the Original Code is Mike Pinkerton.
00018  * Portions created by the Initial Developer are Copyright (C) 2006
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s): Mike Pinkerton <mikepinkerton@mac.com>
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * This spellchecker is based on the built-in spellchecker on Mac OS X. It
00036  * does not use any MySpell technology or rely on their dictionaries. It's just
00037  * a thin wrapper around the Cocoa NSSpellChecker API.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 #include "mozOSXSpell.h"
00042 #include "nsReadableUtils.h"
00043 #include "nsCRT.h"
00044 
00045 #import <Cocoa/Cocoa.h>
00046 
00047 // utility category we need for PRUnichar<->NSString conversion (taken from Camino)
00048 @interface NSString(PRUnicharUtils)
00049 + (id)stringWithPRUnichars:(const PRUnichar*)inString;
00050 - (PRUnichar*)createNewUnicodeBuffer;
00051 @end
00052 
00053 
00054 NS_IMPL_ISUPPORTS1(mozOSXSpell, mozISpellCheckingEngine)
00055 
00056 mozOSXSpell::mozOSXSpell()
00057 {
00058 }
00059 
00060 mozOSXSpell::~mozOSXSpell()
00061 {
00062 }
00063 
00064 //
00065 // GetDictionary
00066 //
00067 // Nothing to do here, we don't have a dictionary on disk, so this is really
00068 // just a no-op. The caller is responsible for disposing of |aDictionary|.
00069 //
00070 NS_IMETHODIMP mozOSXSpell::GetDictionary(PRUnichar **aDictionary)
00071 {
00072   NS_ENSURE_ARG_POINTER(aDictionary);
00073 
00074   *aDictionary = [@"" createNewUnicodeBuffer];
00075   return NS_OK;
00076 }
00077 
00078 //
00079 // SetDictionary
00080 //
00081 // Another no-op as there's nothing to load or initialize.
00082 //
00083 NS_IMETHODIMP mozOSXSpell::SetDictionary(const PRUnichar *aDictionary)
00084 {
00085   return NS_OK;
00086 }
00087 
00088 //
00089 // GetLanguage
00090 //
00091 // Returns the language of the current dictionary, which should be the l10n
00092 // the user is running.  The caller is responsible for disposing of |aLanguage|.
00093 //
00094 NS_IMETHODIMP mozOSXSpell::GetLanguage(PRUnichar **aLanguage)
00095 {
00096   NS_ENSURE_ARG_POINTER(aLanguage);
00097 
00098   if (!mLanguage.Length()) {
00099     NSString* lang = [[NSSpellChecker sharedSpellChecker] language];
00100     *aLanguage = [lang createNewUnicodeBuffer];
00101     mLanguage.Assign(*aLanguage);
00102   }
00103   else
00104     *aLanguage = ToNewUnicode(mLanguage);
00105 
00106   return *aLanguage ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
00107 }
00108 
00109 //
00110 // GetProvidesPersonalDictionary
00111 //
00112 // We let Gecko handle the personal dictionary, even though NSSpellChecker can
00113 // handle ignoring words itself. 
00114 //
00115 NS_IMETHODIMP mozOSXSpell::GetProvidesPersonalDictionary(PRBool *aProvidesPersonalDictionary)
00116 {
00117   NS_ENSURE_ARG_POINTER(aProvidesPersonalDictionary);
00118 
00119   *aProvidesPersonalDictionary = PR_FALSE;
00120   return NS_OK;
00121 }
00122 
00123 //
00124 // GetProvidesWordUtils
00125 //
00126 // I have no idea what this is, so we don't provide it.
00127 //
00128 NS_IMETHODIMP mozOSXSpell::GetProvidesWordUtils(PRBool *aProvidesWordUtils)
00129 {
00130   NS_ENSURE_ARG_POINTER(aProvidesWordUtils);
00131 
00132   *aProvidesWordUtils = PR_FALSE;
00133   return NS_OK;
00134 }
00135 
00136 //
00137 // GetName
00138 //
00139 // Name not supported (nor is it in MySpell impl)
00140 //
00141 NS_IMETHODIMP mozOSXSpell::GetName(PRUnichar * *aName)
00142 {
00143   return NS_ERROR_NOT_IMPLEMENTED;
00144 }
00145 
00146 //
00147 // GetCopyright
00148 //
00149 // Copyright not supported (nor is it in MySpell impl)
00150 //
00151 NS_IMETHODIMP mozOSXSpell::GetCopyright(PRUnichar * *aCopyright)
00152 {
00153   return NS_ERROR_NOT_IMPLEMENTED;
00154 }
00155 
00156 //
00157 // GetPersonalDictionary
00158 //
00159 // Return the personal dictionary we've been given with Set.
00160 //
00161 NS_IMETHODIMP mozOSXSpell::GetPersonalDictionary(mozIPersonalDictionary** aPersonalDictionary)
00162 {
00163   *aPersonalDictionary = mPersonalDictionary;
00164   NS_IF_ADDREF(*aPersonalDictionary);
00165   return NS_OK;
00166 }
00167 
00168 //
00169 // SetPersonalDictionary
00170 //
00171 // Hold onto the personal dictionary we're given.
00172 //
00173 NS_IMETHODIMP mozOSXSpell::SetPersonalDictionary(mozIPersonalDictionary* aPersonalDictionary)
00174 {
00175   mPersonalDictionary = aPersonalDictionary;
00176   return NS_OK;
00177 }
00178 
00179 //
00180 // GetDictionaryList
00181 //
00182 // We only support the OS dictionary from NSSpellChecker so there will only ever
00183 // be one. The caller is responsible for disposing of |aDictionaries|.
00184 //
00185 NS_IMETHODIMP mozOSXSpell::GetDictionaryList(PRUnichar ***aDictionaries, PRUint32 *aCount)
00186 {
00187   NS_ENSURE_ARG_POINTER(aDictionaries);
00188   NS_ENSURE_ARG_POINTER(aCount);
00189 
00190   *aCount = 1;
00191   *aDictionaries = (PRUnichar **)nsMemory::Alloc(sizeof(PRUnichar *)); // only one entry
00192   GetLanguage(*aDictionaries);
00193        
00194        return NS_OK;
00195 }
00196 
00197 //
00198 // Check
00199 //
00200 // Check if the given word is spelled correctly. If the main dictionary says
00201 // it's not, check again against the peronal dictionary we were given.
00202 //
00203 NS_IMETHODIMP mozOSXSpell::Check(const PRUnichar *aWord, PRBool *aResult)
00204 {
00205   NS_ENSURE_ARG_POINTER(aWord);
00206   NS_ENSURE_ARG_POINTER(aResult);
00207   *aResult = PR_FALSE;
00208 
00209   NSString* wordStr = [NSString stringWithPRUnichars:aWord];
00210   NSRange misspelledRange = [[NSSpellChecker sharedSpellChecker] checkSpellingOfString:wordStr startingAt:0];
00211   if (misspelledRange.location != NSNotFound && mPersonalDictionary)
00212     mPersonalDictionary->Check(aWord, mLanguage.get(), aResult);
00213   else
00214     *aResult = PR_TRUE;
00215 
00216   return NS_OK;
00217 }
00218 
00219 //
00220 // Suggest
00221 //
00222 // Provide a list of suggestions for the incrorectly spelled word |aWord| by 
00223 // converting the list we get back from NSSpellChecker into an array of PRUnichar
00224 // strings. If |aWord| is spelled correctly, |aSuggestions| will be NULL. The caller
00225 // is responsible for disposing of |aSuggestions|.
00226 //
00227 NS_IMETHODIMP mozOSXSpell::Suggest(const PRUnichar *aWord, PRUnichar ***aSuggestions, PRUint32 *aSuggestionCount)
00228 {
00229   NS_ENSURE_ARG_POINTER(aSuggestions);
00230   NS_ENSURE_ARG_POINTER(aSuggestionCount);
00231   *aSuggestions = NULL;
00232 
00233   // check the word against the NSSpellChecker
00234   NSString* wordStr = [NSString stringWithPRUnichars:aWord];
00235   NSArray* guesses = [[NSSpellChecker sharedSpellChecker] guessesForWord:wordStr];
00236   *aSuggestionCount = [guesses count];
00237 
00238   // convert results from NSArray to array of PRUnichar's
00239   if (*aSuggestionCount) {
00240     *aSuggestions = (PRUnichar **)nsMemory::Alloc(*aSuggestionCount * sizeof(PRUnichar *));    
00241     PRUint32 i = 0;
00242     NSEnumerator* e = [guesses objectEnumerator];
00243     NSString* guess = nil;
00244     while ((guess = [e nextObject])) {
00245       (*aSuggestions)[i] = [guess createNewUnicodeBuffer];
00246       ++i;
00247     }
00248   }
00249 
00250   return NS_OK;
00251 }
00252 
00253 #pragma mark -
00254 
00255 //
00256 // String utilities taken from Camino
00257 //
00258 
00259 @implementation NSString(PRUnicharUtils)
00260 
00261 - (PRUnichar*)createNewUnicodeBuffer
00262 {
00263   PRUint32 length = [self length];
00264   PRUnichar* retStr = (PRUnichar*)nsMemory::Alloc((length + 1) * sizeof(PRUnichar));
00265   [self getCharacters:retStr];
00266   retStr[length] = PRUnichar(0);
00267   return retStr;
00268 }
00269 
00270 + (id)stringWithPRUnichars:(const PRUnichar*)inString
00271 {
00272   if (inString)
00273     return [self stringWithCharacters:inString length:nsCRT::strlen(inString)];
00274   else
00275     return [self string];
00276 }
00277 
00278 @end
00279 
00280 //
00281 // Factory Methods
00282 //
00283 
00284 #include "nsIGenericFactory.h"
00285 
00286 #include "mozOSXSpell.h"
00287 
00289 // Define the contructor function for the objects
00290 //
00291 // NOTE: This creates an instance of objects by using the default constructor
00292 //
00293 
00294 NS_GENERIC_FACTORY_CONSTRUCTOR(mozOSXSpell)
00295 
00296 
00297 // Define a table of CIDs implemented by this module along with other
00298 // information like the function to create an instance, contractid, and
00299 // class name.
00300 //
00301 static nsModuleComponentInfo components[] = {
00302   { "OSX Spell check service", MOZ_OSXSPELL_CID, MOZ_OSXSPELL_CONTRACTID, mozOSXSpellConstructor }
00303 };
00304 
00306 // Implement the NSGetModule() exported function for your module
00307 // and the entire implementation of the module object.
00308 //
00309 NS_IMPL_NSGETMODULE(mozOSXSpellModule, components)