michael@0: /******* BEGIN LICENSE BLOCK ******* michael@0: * Version: MPL 1.1/GPL 2.0/LGPL 2.1 michael@0: * michael@0: * The contents of this file are subject to the Mozilla Public License Version michael@0: * 1.1 (the "License"); you may not use this file except in compliance with michael@0: * the License. You may obtain a copy of the License at michael@0: * http://www.mozilla.org/MPL/ michael@0: * michael@0: * Software distributed under the License is distributed on an "AS IS" basis, michael@0: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License michael@0: * for the specific language governing rights and limitations under the michael@0: * License. michael@0: * michael@0: * The Initial Developers of the Original Code are Kevin Hendricks (MySpell) michael@0: * and László Németh (Hunspell). Portions created by the Initial Developers michael@0: * are Copyright (C) 2002-2005 the Initial Developers. All Rights Reserved. michael@0: * michael@0: * Contributor(s): Kevin Hendricks (kevin.hendricks@sympatico.ca) michael@0: * David Einstein (deinst@world.std.com) michael@0: * Michiel van Leeuwen (mvl@exedo.nl) michael@0: * Caolan McNamara (cmc@openoffice.org) michael@0: * László Németh (nemethl@gyorsposta.hu) michael@0: * Davide Prina michael@0: * Giuseppe Modugno michael@0: * Gianluca Turconi michael@0: * Simon Brouwer michael@0: * Noll Janos michael@0: * Biro Arpad michael@0: * Goldman Eleonora michael@0: * Sarlos Tamas michael@0: * Bencsath Boldizsar michael@0: * Halacsy Peter michael@0: * Dvornik Laszlo michael@0: * Gefferth Andras michael@0: * Nagy Viktor michael@0: * Varga Daniel michael@0: * Chris Halls michael@0: * Rene Engelhard michael@0: * Bram Moolenaar michael@0: * Dafydd Jones michael@0: * Harri Pitkanen michael@0: * Andras Timar michael@0: * Tor Lillqvist michael@0: * Jesper Kristensen (mail@jesperkristensen.dk) michael@0: * michael@0: * Alternatively, the contents of this file may be used under the terms of michael@0: * either the GNU General Public License Version 2 or later (the "GPL"), or michael@0: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), michael@0: * in which case the provisions of the GPL or the LGPL are applicable instead michael@0: * of those above. If you wish to allow use of your version of this file only michael@0: * under the terms of either the GPL or the LGPL, and not to allow others to michael@0: * use your version of this file under the terms of the MPL, indicate your michael@0: * decision by deleting the provisions above and replace them with the notice michael@0: * and other provisions required by the GPL or the LGPL. If you do not delete michael@0: * the provisions above, a recipient may use your version of this file under michael@0: * the terms of any one of the MPL, the GPL or the LGPL. michael@0: * michael@0: ******* END LICENSE BLOCK *******/ michael@0: michael@0: #include "mozHunspell.h" michael@0: #include "nsReadableUtils.h" michael@0: #include "nsXPIDLString.h" michael@0: #include "nsIObserverService.h" michael@0: #include "nsISimpleEnumerator.h" michael@0: #include "nsIDirectoryEnumerator.h" michael@0: #include "nsIFile.h" michael@0: #include "nsDirectoryServiceUtils.h" michael@0: #include "nsDirectoryServiceDefs.h" michael@0: #include "mozISpellI18NManager.h" michael@0: #include "nsICharsetConverterManager.h" michael@0: #include "nsUnicharUtilCIID.h" michael@0: #include "nsUnicharUtils.h" michael@0: #include "nsCRT.h" michael@0: #include "mozInlineSpellChecker.h" michael@0: #include "mozilla/Services.h" michael@0: #include michael@0: #include "nsIPrefService.h" michael@0: #include "nsIPrefBranch.h" michael@0: michael@0: static NS_DEFINE_CID(kUnicharUtilCID, NS_UNICHARUTIL_CID); michael@0: michael@0: NS_IMPL_CYCLE_COLLECTING_ADDREF(mozHunspell) michael@0: NS_IMPL_CYCLE_COLLECTING_RELEASE(mozHunspell) michael@0: michael@0: NS_INTERFACE_MAP_BEGIN(mozHunspell) michael@0: NS_INTERFACE_MAP_ENTRY(mozISpellCheckingEngine) michael@0: NS_INTERFACE_MAP_ENTRY(nsIObserver) michael@0: NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference) michael@0: NS_INTERFACE_MAP_ENTRY(nsIMemoryReporter) michael@0: NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, mozISpellCheckingEngine) michael@0: NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION(mozHunspell) michael@0: NS_INTERFACE_MAP_END michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION(mozHunspell, michael@0: mPersonalDictionary, michael@0: mEncoder, michael@0: mDecoder) michael@0: michael@0: template<> mozilla::Atomic mozilla::CountingAllocatorBase::sAmount(0); michael@0: michael@0: mozHunspell::mozHunspell() michael@0: : mHunspell(nullptr) michael@0: { michael@0: #ifdef DEBUG michael@0: // There must be only one instance of this class: it reports memory based on michael@0: // a single static count in HunspellAllocator. michael@0: static bool hasRun = false; michael@0: MOZ_ASSERT(!hasRun); michael@0: hasRun = true; michael@0: #endif michael@0: } michael@0: michael@0: nsresult michael@0: mozHunspell::Init() michael@0: { michael@0: LoadDictionaryList(); michael@0: michael@0: nsCOMPtr obs = mozilla::services::GetObserverService(); michael@0: if (obs) { michael@0: obs->AddObserver(this, "profile-do-change", true); michael@0: obs->AddObserver(this, "profile-after-change", true); michael@0: } michael@0: michael@0: mozilla::RegisterWeakMemoryReporter(this); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: mozHunspell::~mozHunspell() michael@0: { michael@0: mozilla::UnregisterWeakMemoryReporter(this); michael@0: michael@0: mPersonalDictionary = nullptr; michael@0: delete mHunspell; michael@0: } michael@0: michael@0: /* attribute wstring dictionary; */ michael@0: NS_IMETHODIMP mozHunspell::GetDictionary(char16_t **aDictionary) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aDictionary); michael@0: michael@0: *aDictionary = ToNewUnicode(mDictionary); michael@0: return *aDictionary ? NS_OK : NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: /* set the Dictionary. michael@0: * This also Loads the dictionary and initializes the converter using the dictionaries converter michael@0: */ michael@0: NS_IMETHODIMP mozHunspell::SetDictionary(const char16_t *aDictionary) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aDictionary); michael@0: michael@0: if (nsDependentString(aDictionary).IsEmpty()) { michael@0: delete mHunspell; michael@0: mHunspell = nullptr; michael@0: mDictionary.AssignLiteral(""); michael@0: mAffixFileName.AssignLiteral(""); michael@0: mLanguage.AssignLiteral(""); michael@0: mDecoder = nullptr; michael@0: mEncoder = nullptr; michael@0: michael@0: nsCOMPtr obs = mozilla::services::GetObserverService(); michael@0: if (obs) { michael@0: obs->NotifyObservers(nullptr, michael@0: SPELLCHECK_DICTIONARY_UPDATE_NOTIFICATION, michael@0: nullptr); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsIFile* affFile = mDictionaries.GetWeak(nsDependentString(aDictionary)); michael@0: if (!affFile) michael@0: return NS_ERROR_FILE_NOT_FOUND; michael@0: michael@0: nsAutoCString dictFileName, affFileName; michael@0: michael@0: // XXX This isn't really good. nsIFile->NativePath isn't safe for all michael@0: // character sets on Windows. michael@0: // A better way would be to QI to nsIFile, and get a filehandle michael@0: // from there. Only problem is that hunspell wants a path michael@0: michael@0: nsresult rv = affFile->GetNativePath(affFileName); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: if (mAffixFileName.Equals(affFileName.get())) michael@0: return NS_OK; michael@0: michael@0: dictFileName = affFileName; michael@0: int32_t dotPos = dictFileName.RFindChar('.'); michael@0: if (dotPos == -1) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: dictFileName.SetLength(dotPos); michael@0: dictFileName.AppendLiteral(".dic"); michael@0: michael@0: // SetDictionary can be called multiple times, so we might have a michael@0: // valid mHunspell instance which needs cleaned up. michael@0: delete mHunspell; michael@0: michael@0: mDictionary = aDictionary; michael@0: mAffixFileName = affFileName; michael@0: michael@0: mHunspell = new Hunspell(affFileName.get(), michael@0: dictFileName.get()); michael@0: if (!mHunspell) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: michael@0: nsCOMPtr ccm = michael@0: do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = ccm->GetUnicodeDecoder(mHunspell->get_dic_encoding(), michael@0: getter_AddRefs(mDecoder)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = ccm->GetUnicodeEncoder(mHunspell->get_dic_encoding(), michael@0: getter_AddRefs(mEncoder)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: michael@0: if (mEncoder) michael@0: mEncoder->SetOutputErrorBehavior(mEncoder->kOnError_Signal, nullptr, '?'); michael@0: michael@0: int32_t pos = mDictionary.FindChar('-'); michael@0: if (pos == -1) michael@0: pos = mDictionary.FindChar('_'); michael@0: michael@0: if (pos == -1) michael@0: mLanguage.Assign(mDictionary); michael@0: else michael@0: mLanguage = Substring(mDictionary, 0, pos); michael@0: michael@0: nsCOMPtr obs = mozilla::services::GetObserverService(); michael@0: if (obs) { michael@0: obs->NotifyObservers(nullptr, michael@0: SPELLCHECK_DICTIONARY_UPDATE_NOTIFICATION, michael@0: nullptr); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* readonly attribute wstring language; */ michael@0: NS_IMETHODIMP mozHunspell::GetLanguage(char16_t **aLanguage) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aLanguage); michael@0: michael@0: if (mDictionary.IsEmpty()) michael@0: return NS_ERROR_NOT_INITIALIZED; michael@0: michael@0: *aLanguage = ToNewUnicode(mLanguage); michael@0: return *aLanguage ? NS_OK : NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: /* readonly attribute boolean providesPersonalDictionary; */ michael@0: NS_IMETHODIMP mozHunspell::GetProvidesPersonalDictionary(bool *aProvidesPersonalDictionary) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aProvidesPersonalDictionary); michael@0: michael@0: *aProvidesPersonalDictionary = false; michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* readonly attribute boolean providesWordUtils; */ michael@0: NS_IMETHODIMP mozHunspell::GetProvidesWordUtils(bool *aProvidesWordUtils) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aProvidesWordUtils); michael@0: michael@0: *aProvidesWordUtils = false; michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* readonly attribute wstring name; */ michael@0: NS_IMETHODIMP mozHunspell::GetName(char16_t * *aName) michael@0: { michael@0: return NS_ERROR_NOT_IMPLEMENTED; michael@0: } michael@0: michael@0: /* readonly attribute wstring copyright; */ michael@0: NS_IMETHODIMP mozHunspell::GetCopyright(char16_t * *aCopyright) michael@0: { michael@0: return NS_ERROR_NOT_IMPLEMENTED; michael@0: } michael@0: michael@0: /* attribute mozIPersonalDictionary personalDictionary; */ michael@0: NS_IMETHODIMP mozHunspell::GetPersonalDictionary(mozIPersonalDictionary * *aPersonalDictionary) michael@0: { michael@0: *aPersonalDictionary = mPersonalDictionary; michael@0: NS_IF_ADDREF(*aPersonalDictionary); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP mozHunspell::SetPersonalDictionary(mozIPersonalDictionary * aPersonalDictionary) michael@0: { michael@0: mPersonalDictionary = aPersonalDictionary; michael@0: return NS_OK; michael@0: } michael@0: michael@0: struct AppendNewStruct michael@0: { michael@0: char16_t **dics; michael@0: uint32_t count; michael@0: bool failed; michael@0: }; michael@0: michael@0: static PLDHashOperator michael@0: AppendNewString(const nsAString& aString, nsIFile* aFile, void* aClosure) michael@0: { michael@0: AppendNewStruct *ans = (AppendNewStruct*) aClosure; michael@0: ans->dics[ans->count] = ToNewUnicode(aString); michael@0: if (!ans->dics[ans->count]) { michael@0: ans->failed = true; michael@0: return PL_DHASH_STOP; michael@0: } michael@0: michael@0: ++ans->count; michael@0: return PL_DHASH_NEXT; michael@0: } michael@0: michael@0: /* void GetDictionaryList ([array, size_is (count)] out wstring dictionaries, out uint32_t count); */ michael@0: NS_IMETHODIMP mozHunspell::GetDictionaryList(char16_t ***aDictionaries, michael@0: uint32_t *aCount) michael@0: { michael@0: if (!aDictionaries || !aCount) michael@0: return NS_ERROR_NULL_POINTER; michael@0: michael@0: AppendNewStruct ans = { michael@0: (char16_t**) NS_Alloc(sizeof(char16_t*) * mDictionaries.Count()), michael@0: 0, michael@0: false michael@0: }; michael@0: michael@0: // This pointer is used during enumeration michael@0: mDictionaries.EnumerateRead(AppendNewString, &ans); michael@0: michael@0: if (ans.failed) { michael@0: while (ans.count) { michael@0: --ans.count; michael@0: NS_Free(ans.dics[ans.count]); michael@0: } michael@0: NS_Free(ans.dics); michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: *aDictionaries = ans.dics; michael@0: *aCount = ans.count; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: mozHunspell::LoadDictionaryList() michael@0: { michael@0: mDictionaries.Clear(); michael@0: michael@0: nsresult rv; michael@0: michael@0: nsCOMPtr dirSvc = michael@0: do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID); michael@0: if (!dirSvc) michael@0: return; michael@0: michael@0: // find built in dictionaries, or dictionaries specified in michael@0: // spellchecker.dictionary_path in prefs michael@0: nsCOMPtr dictDir; michael@0: michael@0: // check preferences first michael@0: nsCOMPtr prefs(do_GetService(NS_PREFSERVICE_CONTRACTID)); michael@0: if (prefs) { michael@0: nsCString extDictPath; michael@0: rv = prefs->GetCharPref("spellchecker.dictionary_path", getter_Copies(extDictPath)); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: // set the spellchecker.dictionary_path michael@0: rv = NS_NewNativeLocalFile(extDictPath, true, getter_AddRefs(dictDir)); michael@0: } michael@0: } michael@0: if (!dictDir) { michael@0: // spellcheck.dictionary_path not found, set internal path michael@0: rv = dirSvc->Get(DICTIONARY_SEARCH_DIRECTORY, michael@0: NS_GET_IID(nsIFile), getter_AddRefs(dictDir)); michael@0: } michael@0: if (dictDir) { michael@0: LoadDictionariesFromDir(dictDir); michael@0: } michael@0: else { michael@0: // try to load gredir/dictionaries michael@0: nsCOMPtr greDir; michael@0: rv = dirSvc->Get(NS_GRE_DIR, michael@0: NS_GET_IID(nsIFile), getter_AddRefs(greDir)); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: greDir->AppendNative(NS_LITERAL_CSTRING("dictionaries")); michael@0: LoadDictionariesFromDir(greDir); michael@0: } michael@0: michael@0: // try to load appdir/dictionaries only if different than gredir michael@0: nsCOMPtr appDir; michael@0: rv = dirSvc->Get(NS_XPCOM_CURRENT_PROCESS_DIR, michael@0: NS_GET_IID(nsIFile), getter_AddRefs(appDir)); michael@0: bool equals; michael@0: if (NS_SUCCEEDED(rv) && NS_SUCCEEDED(appDir->Equals(greDir, &equals)) && !equals) { michael@0: appDir->AppendNative(NS_LITERAL_CSTRING("dictionaries")); michael@0: LoadDictionariesFromDir(appDir); michael@0: } michael@0: } michael@0: michael@0: // find dictionaries from extensions requiring restart michael@0: nsCOMPtr dictDirs; michael@0: rv = dirSvc->Get(DICTIONARY_SEARCH_DIRECTORY_LIST, michael@0: NS_GET_IID(nsISimpleEnumerator), getter_AddRefs(dictDirs)); michael@0: if (NS_FAILED(rv)) michael@0: return; michael@0: michael@0: bool hasMore; michael@0: while (NS_SUCCEEDED(dictDirs->HasMoreElements(&hasMore)) && hasMore) { michael@0: nsCOMPtr elem; michael@0: dictDirs->GetNext(getter_AddRefs(elem)); michael@0: michael@0: dictDir = do_QueryInterface(elem); michael@0: if (dictDir) michael@0: LoadDictionariesFromDir(dictDir); michael@0: } michael@0: michael@0: // find dictionaries from restartless extensions michael@0: for (int32_t i = 0; i < mDynamicDirectories.Count(); i++) { michael@0: LoadDictionariesFromDir(mDynamicDirectories[i]); michael@0: } michael@0: michael@0: // Now we have finished updating the list of dictionaries, update the current michael@0: // dictionary and any editors which may use it. michael@0: mozInlineSpellChecker::UpdateCanEnableInlineSpellChecking(); michael@0: michael@0: // Check if the current dictionary is still available. michael@0: // If not, try to replace it with another dictionary of the same language. michael@0: if (!mDictionary.IsEmpty()) { michael@0: rv = SetDictionary(mDictionary.get()); michael@0: if (NS_SUCCEEDED(rv)) michael@0: return; michael@0: } michael@0: michael@0: // If the current dictionary has gone, and we don't have a good replacement, michael@0: // set no current dictionary. michael@0: if (!mDictionary.IsEmpty()) { michael@0: SetDictionary(EmptyString().get()); michael@0: } michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: mozHunspell::LoadDictionariesFromDir(nsIFile* aDir) michael@0: { michael@0: nsresult rv; michael@0: michael@0: bool check = false; michael@0: rv = aDir->Exists(&check); michael@0: if (NS_FAILED(rv) || !check) michael@0: return NS_ERROR_UNEXPECTED; michael@0: michael@0: rv = aDir->IsDirectory(&check); michael@0: if (NS_FAILED(rv) || !check) michael@0: return NS_ERROR_UNEXPECTED; michael@0: michael@0: nsCOMPtr e; michael@0: rv = aDir->GetDirectoryEntries(getter_AddRefs(e)); michael@0: if (NS_FAILED(rv)) michael@0: return NS_ERROR_UNEXPECTED; michael@0: michael@0: nsCOMPtr files(do_QueryInterface(e)); michael@0: if (!files) michael@0: return NS_ERROR_UNEXPECTED; michael@0: michael@0: nsCOMPtr file; michael@0: while (NS_SUCCEEDED(files->GetNextFile(getter_AddRefs(file))) && file) { michael@0: nsAutoString leafName; michael@0: file->GetLeafName(leafName); michael@0: if (!StringEndsWith(leafName, NS_LITERAL_STRING(".dic"))) michael@0: continue; michael@0: michael@0: nsAutoString dict(leafName); michael@0: dict.SetLength(dict.Length() - 4); // magic length of ".dic" michael@0: michael@0: // check for the presence of the .aff file michael@0: leafName = dict; michael@0: leafName.AppendLiteral(".aff"); michael@0: file->SetLeafName(leafName); michael@0: rv = file->Exists(&check); michael@0: if (NS_FAILED(rv) || !check) michael@0: continue; michael@0: michael@0: #ifdef DEBUG_bsmedberg michael@0: printf("Adding dictionary: %s\n", NS_ConvertUTF16toUTF8(dict).get()); michael@0: #endif michael@0: michael@0: mDictionaries.Put(dict, file); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult mozHunspell::ConvertCharset(const char16_t* aStr, char ** aDst) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aDst); michael@0: NS_ENSURE_TRUE(mEncoder, NS_ERROR_NULL_POINTER); michael@0: michael@0: int32_t outLength; michael@0: int32_t inLength = NS_strlen(aStr); michael@0: nsresult rv = mEncoder->GetMaxLength(aStr, inLength, &outLength); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: *aDst = (char *) nsMemory::Alloc(sizeof(char) * (outLength+1)); michael@0: NS_ENSURE_TRUE(*aDst, NS_ERROR_OUT_OF_MEMORY); michael@0: michael@0: rv = mEncoder->Convert(aStr, &inLength, *aDst, &outLength); michael@0: if (NS_SUCCEEDED(rv)) michael@0: (*aDst)[outLength] = '\0'; michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: /* boolean Check (in wstring word); */ michael@0: NS_IMETHODIMP mozHunspell::Check(const char16_t *aWord, bool *aResult) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aWord); michael@0: NS_ENSURE_ARG_POINTER(aResult); michael@0: NS_ENSURE_TRUE(mHunspell, NS_ERROR_FAILURE); michael@0: michael@0: nsXPIDLCString charsetWord; michael@0: nsresult rv = ConvertCharset(aWord, getter_Copies(charsetWord)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: *aResult = !!mHunspell->spell(charsetWord); michael@0: michael@0: michael@0: if (!*aResult && mPersonalDictionary) michael@0: rv = mPersonalDictionary->Check(aWord, mLanguage.get(), aResult); michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: /* void Suggest (in wstring word, [array, size_is (count)] out wstring suggestions, out uint32_t count); */ michael@0: NS_IMETHODIMP mozHunspell::Suggest(const char16_t *aWord, char16_t ***aSuggestions, uint32_t *aSuggestionCount) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aSuggestions); michael@0: NS_ENSURE_ARG_POINTER(aSuggestionCount); michael@0: NS_ENSURE_TRUE(mHunspell, NS_ERROR_FAILURE); michael@0: michael@0: nsresult rv; michael@0: *aSuggestionCount = 0; michael@0: michael@0: nsXPIDLCString charsetWord; michael@0: rv = ConvertCharset(aWord, getter_Copies(charsetWord)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: char ** wlst; michael@0: *aSuggestionCount = mHunspell->suggest(&wlst, charsetWord); michael@0: michael@0: if (*aSuggestionCount) { michael@0: *aSuggestions = (char16_t **)nsMemory::Alloc(*aSuggestionCount * sizeof(char16_t *)); michael@0: if (*aSuggestions) { michael@0: uint32_t index = 0; michael@0: for (index = 0; index < *aSuggestionCount && NS_SUCCEEDED(rv); ++index) { michael@0: // Convert the suggestion to utf16 michael@0: int32_t inLength = strlen(wlst[index]); michael@0: int32_t outLength; michael@0: rv = mDecoder->GetMaxLength(wlst[index], inLength, &outLength); michael@0: if (NS_SUCCEEDED(rv)) michael@0: { michael@0: (*aSuggestions)[index] = (char16_t *) nsMemory::Alloc(sizeof(char16_t) * (outLength+1)); michael@0: if ((*aSuggestions)[index]) michael@0: { michael@0: rv = mDecoder->Convert(wlst[index], &inLength, (*aSuggestions)[index], &outLength); michael@0: if (NS_SUCCEEDED(rv)) michael@0: (*aSuggestions)[index][outLength] = 0; michael@0: } michael@0: else michael@0: rv = NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: } michael@0: michael@0: if (NS_FAILED(rv)) michael@0: NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(index, *aSuggestions); // free the char16_t strings up to the point at which the error occurred michael@0: } michael@0: else // if (*aSuggestions) michael@0: rv = NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(*aSuggestionCount, wlst); michael@0: return rv; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: mozHunspell::Observe(nsISupports* aSubj, const char *aTopic, michael@0: const char16_t *aData) michael@0: { michael@0: NS_ASSERTION(!strcmp(aTopic, "profile-do-change") michael@0: || !strcmp(aTopic, "profile-after-change"), michael@0: "Unexpected observer topic"); michael@0: michael@0: LoadDictionaryList(); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* void addDirectory(in nsIFile dir); */ michael@0: NS_IMETHODIMP mozHunspell::AddDirectory(nsIFile *aDir) michael@0: { michael@0: mDynamicDirectories.AppendObject(aDir); michael@0: LoadDictionaryList(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* void removeDirectory(in nsIFile dir); */ michael@0: NS_IMETHODIMP mozHunspell::RemoveDirectory(nsIFile *aDir) michael@0: { michael@0: mDynamicDirectories.RemoveObject(aDir); michael@0: LoadDictionaryList(); michael@0: return NS_OK; michael@0: }