Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Protected Member Functions | Protected Attributes
mozSpellChecker Class Reference

#include <mozSpellChecker.h>

Inheritance diagram for mozSpellChecker:
Inheritance graph
[legend]
Collaboration diagram for mozSpellChecker:
Collaboration graph
[legend]

List of all members.

Public Member Functions

NS_DECL_ISUPPORTS mozSpellChecker ()
virtual ~mozSpellChecker ()
nsresult Init ()
NS_IMETHOD SetDocument (nsITextServicesDocument *aDoc, PRBool aFromStartofDoc)
 Tells the spellchecker what document to check.
NS_IMETHOD NextMisspelledWord (nsAString &aWord, nsStringArray *aSuggestions)
 Selects (hilites) the next misspelled word in the document.
NS_IMETHOD CheckWord (const nsAString &aWord, PRBool *aIsMisspelled, nsStringArray *aSuggestions)
 Checks if a word is misspelled.
NS_IMETHOD Replace (const nsAString &aOldWord, const nsAString &aNewWord, PRBool aAllOccurrences)
 Replaces the old word with the specified new word.
NS_IMETHOD IgnoreAll (const nsAString &aWord)
 Ignores all occurrences of the specified word in the document.
NS_IMETHOD AddWordToPersonalDictionary (const nsAString &aWord)
 Add a word to the user's personal dictionary.
NS_IMETHOD RemoveWordFromPersonalDictionary (const nsAString &aWord)
 Remove a word from the user's personal dictionary.
NS_IMETHOD GetPersonalDictionary (nsStringArray *aWordList)
 Returns the list of words in the user's personal dictionary.
NS_IMETHOD GetDictionaryList (nsStringArray *aDictionaryList)
 Returns the list of strings representing the dictionaries the spellchecker supports.
NS_IMETHOD GetCurrentDictionary (nsAString &aDictionary)
 Returns a string representing the current dictionary.
NS_IMETHOD SetCurrentDictionary (const nsAString &aDictionary)
 Tells the spellchecker to use a specific dictionary.

Protected Member Functions

nsresult SetupDoc (PRUint32 *outBlockOffset)
nsresult GetCurrentBlockIndex (nsITextServicesDocument *aDoc, PRInt32 *outBlockIndex)

Protected Attributes

nsCOMPtr< mozISpellI18NUtilmConverter
nsCOMPtr< nsITextServicesDocumentmTsDoc
nsCOMPtr< mozIPersonalDictionarymPersonalDictionary
nsString mDictionaryName
nsCOMPtr< mozISpellCheckingEnginemSpellCheckingEngine
PRBool mFromStart
nsStringArray mIgnoreList

Detailed Description

Definition at line 50 of file mozSpellChecker.h.


Constructor & Destructor Documentation

Definition at line 48 of file mozSpellChecker.cpp.

{
}

Definition at line 52 of file mozSpellChecker.cpp.

{
  if(mPersonalDictionary){
    //    mPersonalDictionary->Save();
    mPersonalDictionary->EndSession();
  }
  mSpellCheckingEngine = nsnull;
  mPersonalDictionary = nsnull;
}

Member Function Documentation

Add a word to the user's personal dictionary.

Parameters:
aWordis the word to add.

Implements nsISpellChecker.

Definition at line 265 of file mozSpellChecker.cpp.

{
  nsresult res;
  PRUnichar empty=0;
  if (!mPersonalDictionary)
    return NS_ERROR_NULL_POINTER;
  res = mPersonalDictionary->AddWord(PromiseFlatString(aWord).get(),&empty);
  return res;
}
NS_IMETHODIMP mozSpellChecker::CheckWord ( const nsAString &  aWord,
PRBool aIsMisspelled,
nsStringArray *  aSuggestions 
) [virtual]

Checks if a word is misspelled.

No document is required to use this method.

Parameters:
aWordis the word to check.
aIsMisspelledwill be set to true if the word is misspelled.
aSuggestionsis an array of nsStrings which represent the suggested replacements for the misspelled word. The array will be empty if there aren't any suggestions.

Implements nsISpellChecker.

Definition at line 127 of file mozSpellChecker.cpp.

{
  nsresult result;
  PRBool correct;
  if(!mSpellCheckingEngine)
    return NS_ERROR_NULL_POINTER;

  // don't bother to check crazy words, also, myspell gets unhappy if you
  // give it too much data and crashes sometimes
  if (aWord.Length() > UNREASONABLE_WORD_LENGTH) {
    *aIsMisspelled = PR_TRUE;
    return NS_OK;
  }

  *aIsMisspelled = PR_FALSE;
  result = mSpellCheckingEngine->Check(PromiseFlatString(aWord).get(), &correct);
  NS_ENSURE_SUCCESS(result, result);
  if(!correct){
    if(aSuggestions){
      PRUint32 count,i;
      PRUnichar **words;
      
      result = mSpellCheckingEngine->Suggest(PromiseFlatString(aWord).get(), &words, &count);
      NS_ENSURE_SUCCESS(result, result); 
      for(i=0;i<count;i++){
        aSuggestions->AppendString(nsDependentString(words[i]));
      }
      
      if (count)
        NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(count, words);
    }
    if(aIsMisspelled){
      *aIsMisspelled = PR_TRUE;
    }
  }
  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult mozSpellChecker::GetCurrentBlockIndex ( nsITextServicesDocument aDoc,
PRInt32 outBlockIndex 
) [protected]

Definition at line 411 of file mozSpellChecker.cpp.

{
  PRInt32  blockIndex = 0;
  PRBool   isDone = PR_FALSE;
  nsresult result = NS_OK;

  do
  {
    aDoc->PrevBlock();

    result = aDoc->IsDone(&isDone);

    if (!isDone)
      blockIndex ++;

  } while (NS_SUCCEEDED(result) && !isDone);
  
  *outBlockIndex = blockIndex;

  return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

NS_IMETHODIMP mozSpellChecker::GetCurrentDictionary ( nsAString &  aDictionary) [virtual]

Returns a string representing the current dictionary.

Parameters:
aDictionarywill contain the name of the dictionary. This name is the same string that is in the list returned by GetDictionaryList().

Implements nsISpellChecker.

Definition at line 324 of file mozSpellChecker.cpp.

{
  nsXPIDLString dictname;
  mSpellCheckingEngine->GetDictionary(getter_Copies(dictname));
  aDictionary = dictname;
  return NS_OK;
}

Here is the call graph for this function:

NS_IMETHODIMP mozSpellChecker::GetDictionaryList ( nsStringArray *  aDictionaryList) [virtual]

Returns the list of strings representing the dictionaries the spellchecker supports.

It was suggested that the strings returned be in the RFC 1766 format. This format looks something like <ISO 639 language code>-<ISO 3166 country code>. For example: en-US

Parameters:
aDictionaryListis an array of nsStrings that represent the dictionaries supported by the spellchecker.

Implements nsISpellChecker.

Definition at line 305 of file mozSpellChecker.cpp.

{
  nsAutoString temp;
  PRUint32 count,i;
  PRUnichar **words;
  
  if(!aDictionaryList || !mSpellCheckingEngine)
    return NS_ERROR_NULL_POINTER;
  mSpellCheckingEngine->GetDictionaryList(&words,&count);
  for(i=0;i<count;i++){
    temp.Assign(words[i]);
    aDictionaryList->AppendString(temp);
  }
  NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(count, words);

  return NS_OK;
}
NS_IMETHODIMP mozSpellChecker::GetPersonalDictionary ( nsStringArray *  aWordList) [virtual]

Returns the list of words in the user's personal dictionary.

Parameters:
aWordListis an array of nsStrings that represent the list of words in the user's personal dictionary.

Implements nsISpellChecker.

Definition at line 287 of file mozSpellChecker.cpp.

{
  if(!aWordList || !mPersonalDictionary)
    return NS_ERROR_NULL_POINTER;

  nsCOMPtr<nsIStringEnumerator> words;
  mPersonalDictionary->GetWordList(getter_AddRefs(words));
  
  PRBool hasMore;
  nsAutoString word;
  while (NS_SUCCEEDED(words->HasMore(&hasMore)) && hasMore) {
    words->GetNext(word);
    aWordList->AppendString(word);
  }
  return NS_OK;
}

Here is the call graph for this function:

NS_IMETHODIMP mozSpellChecker::IgnoreAll ( const nsAString &  aWord) [virtual]

Ignores all occurrences of the specified word in the document.

Parameters:
aWordis the word to ignore.

Implements nsISpellChecker.

Definition at line 256 of file mozSpellChecker.cpp.

{
  if(mPersonalDictionary){
    mPersonalDictionary->IgnoreWord(PromiseFlatString(aWord).get());
  }
  return NS_OK;
}

Definition at line 63 of file mozSpellChecker.cpp.

{
  mPersonalDictionary = do_GetService("@mozilla.org/spellchecker/personaldictionary;1");

  nsresult rv;
  mSpellCheckingEngine = do_GetService("@mozilla.org/spellchecker/myspell;1",&rv);
  if (NS_FAILED(rv)) {
    return rv;
  }
  mSpellCheckingEngine->SetPersonalDictionary(mPersonalDictionary);
  return NS_OK;
}

Here is the call graph for this function:

NS_IMETHODIMP mozSpellChecker::NextMisspelledWord ( nsAString &  aWord,
nsStringArray *  aSuggestions 
) [virtual]

Selects (hilites) the next misspelled word in the document.

Parameters:
aWordwill contain the misspelled word.
aSuggestionsis an array of nsStrings, that represent the suggested replacements for the misspelled word.

Implements nsISpellChecker.

Definition at line 86 of file mozSpellChecker.cpp.

{
  if(!aSuggestions||!mConverter)
    return NS_ERROR_NULL_POINTER;

  PRUint32 selOffset;
  PRInt32 begin,end;
  nsresult result;
  result = SetupDoc(&selOffset);
  PRBool isMisspelled,done;
  if (NS_FAILED(result))
    return result;

  while( NS_SUCCEEDED(mTsDoc->IsDone(&done)) && !done )
    {
      nsString str;
      result = mTsDoc->GetCurrentTextBlock(&str);
  
      if (NS_FAILED(result))
        return result;
      do{
        result = mConverter->FindNextWord(str.get(),str.Length(),selOffset,&begin,&end);
        if(NS_SUCCEEDED(result)&&(begin != -1)){
          const nsAString &currWord = Substring(str, begin, end - begin);
          result = CheckWord(currWord, &isMisspelled, aSuggestions);
          if(isMisspelled){
            aWord = currWord;
            mTsDoc->SetSelection(begin, end-begin);
            mTsDoc->ScrollSelectionIntoView();
            return NS_OK;
          }
        }
        selOffset = end;
      }while(end != -1);
      mTsDoc->NextBlock();
      selOffset=0;
    }
  return NS_OK;
}

Here is the call graph for this function:

Remove a word from the user's personal dictionary.

Parameters:
aWordis the word to remove.

Implements nsISpellChecker.

Definition at line 276 of file mozSpellChecker.cpp.

{
  nsresult res;
  PRUnichar empty=0;
  if (!mPersonalDictionary)
    return NS_ERROR_NULL_POINTER;
  res = mPersonalDictionary->RemoveWord(PromiseFlatString(aWord).get(),&empty);
  return res;
}
NS_IMETHODIMP mozSpellChecker::Replace ( const nsAString &  aOldWord,
const nsAString &  aNewWord,
PRBool  aAllOccurrences 
) [virtual]

Replaces the old word with the specified new word.

Parameters:
aOldWordis the word to be replaced.
aNewWordis the word that is to replace old word.
aAllOccurrenceswill replace all occurrences of old word, in the document, with new word when it is true. If false, it will replace the 1st occurrence only!

Implements nsISpellChecker.

Definition at line 166 of file mozSpellChecker.cpp.

{
  if(!mConverter)
    return NS_ERROR_NULL_POINTER;

  nsAutoString newWord(aNewWord); // sigh

  if(aAllOccurrences){
    PRUint32 selOffset;
    PRInt32 startBlock,currentBlock,currOffset;
    PRInt32 begin,end;
    PRBool done;
    nsresult result;
    nsAutoString str;

    // find out where we are
    result = SetupDoc(&selOffset);
    if(NS_FAILED(result))
      return result;
    result = GetCurrentBlockIndex(mTsDoc,&startBlock);
    if(NS_FAILED(result))
      return result;

    //start at the beginning
    result = mTsDoc->FirstBlock();
    currOffset=0;
    currentBlock = 0;
    while( NS_SUCCEEDED(mTsDoc->IsDone(&done)) && !done )
      {
        result = mTsDoc->GetCurrentTextBlock(&str);
        do{
          result = mConverter->FindNextWord(str.get(),str.Length(),currOffset,&begin,&end);
          if(NS_SUCCEEDED(result)&&(begin != -1)){
            if (aOldWord.Equals(Substring(str, begin, end-begin))) {
              // if we are before the current selection point but in the same block
              // move the selection point forwards
              if((currentBlock == startBlock)&&(begin < (PRInt32) selOffset)){
                selOffset += (aNewWord.Length() - aOldWord.Length());
                if(selOffset < 0) selOffset=0;
              }
              mTsDoc->SetSelection(begin, end-begin);
              mTsDoc->InsertText(&newWord);
              mTsDoc->GetCurrentTextBlock(&str);
              end += (aNewWord.Length() - aOldWord.Length());  // recursion was cute in GEB, not here.
            }
          }
          currOffset = end;
        }while(currOffset != -1);
        mTsDoc->NextBlock();
        currentBlock++;
        currOffset=0;          
      }

    // We are done replacing.  Put the selection point back where we found  it (or equivalent);
    result = mTsDoc->FirstBlock();
    currentBlock = 0;
    while(( NS_SUCCEEDED(mTsDoc->IsDone(&done)) && !done ) &&(currentBlock < startBlock)){
      mTsDoc->NextBlock();
    }

//After we have moved to the block where the first occurance of replace was done, put the 
//selection to the next word following it. In case there is no word following it i.e if it happens
//to be the last word in that block, then move to the next block and put the selection to the 
//first word in that block, otherwise when the Setupdoc() is called, it queries the LastSelectedBlock()
//and the selection offset of the last occurance of the replaced word is taken instead of the first 
//occurance and things get messed up as reported in the bug 244969

    if( NS_SUCCEEDED(mTsDoc->IsDone(&done)) && !done ){
      nsString str;                                
      result = mTsDoc->GetCurrentTextBlock(&str);  
      result = mConverter->FindNextWord(str.get(),str.Length(),selOffset,&begin,&end);
            if(end == -1)
             {
                mTsDoc->NextBlock();
                selOffset=0;
                result = mTsDoc->GetCurrentTextBlock(&str); 
                result = mConverter->FindNextWord(str.get(),str.Length(),selOffset,&begin,&end);
                mTsDoc->SetSelection(begin, 0);
             }
         else
                mTsDoc->SetSelection(begin, 0);
    }
 }
  else{
    mTsDoc->InsertText(&newWord);
  }
  return NS_OK;
}

Here is the call graph for this function:

NS_IMETHODIMP mozSpellChecker::SetCurrentDictionary ( const nsAString &  aDictionary) [virtual]

Tells the spellchecker to use a specific dictionary.

Parameters:
aDictionarya string that is in the list returned by GetDictionaryList().

Implements nsISpellChecker.

Definition at line 333 of file mozSpellChecker.cpp.

{
  if(!mSpellCheckingEngine)
    return NS_ERROR_NULL_POINTER;
 
  nsresult res;
  res = mSpellCheckingEngine->SetDictionary(PromiseFlatString(aDictionary).get());
  if(NS_FAILED(res)){
    NS_WARNING("Dictionary load failed");
    return res;
  }
  nsXPIDLString language;
  
  nsCOMPtr<mozISpellI18NManager> serv(do_GetService("@mozilla.org/spellchecker/i18nmanager;1", &res));
  if(serv && NS_SUCCEEDED(res)){
    res = serv->GetUtil(language.get(),getter_AddRefs(mConverter));
  }
  return res;
}

Here is the call graph for this function:

NS_IMETHODIMP mozSpellChecker::SetDocument ( nsITextServicesDocument aDoc,
PRBool  aFromStartofDoc 
) [virtual]

Tells the spellchecker what document to check.

Parameters:
aDocis the document to check.
aFromStartOfDocIf true, start check from beginning of document, if false, start check from current cursor position.

Implements nsISpellChecker.

Definition at line 77 of file mozSpellChecker.cpp.

{
  mTsDoc = aDoc;
  mFromStart = aFromStartofDoc;
  return NS_OK;
}
nsresult mozSpellChecker::SetupDoc ( PRUint32 outBlockOffset) [protected]

Definition at line 354 of file mozSpellChecker.cpp.

{
  nsresult  rv;

  nsITextServicesDocument::TSDBlockSelectionStatus blockStatus;
  PRInt32 selOffset;
  PRInt32 selLength;
  *outBlockOffset = 0;

  if (!mFromStart) 
  {
    rv = mTsDoc->LastSelectedBlock(&blockStatus, &selOffset, &selLength);
    if (NS_SUCCEEDED(rv) && (blockStatus != nsITextServicesDocument::eBlockNotFound))
    {
      switch (blockStatus)
      {
        case nsITextServicesDocument::eBlockOutside:  // No TB in S, but found one before/after S.
        case nsITextServicesDocument::eBlockPartial:  // S begins or ends in TB but extends outside of TB.
          // the TS doc points to the block we want.
          *outBlockOffset = selOffset + selLength;
          break;
                    
        case nsITextServicesDocument::eBlockInside:  // S extends beyond the start and end of TB.
          // we want the block after this one.
          rv = mTsDoc->NextBlock();
          *outBlockOffset = 0;
          break;
                
        case nsITextServicesDocument::eBlockContains: // TB contains entire S.
          *outBlockOffset = selOffset + selLength;
          break;
        
        case nsITextServicesDocument::eBlockNotFound: // There is no text block (TB) in or before the selection (S).
        default:
          NS_NOTREACHED("Shouldn't ever get this status");
      }
    }
    else  //failed to get last sel block. Just start at beginning
    {
      rv = mTsDoc->FirstBlock();
      *outBlockOffset = 0;
    }
  
  }
  else // we want the first block
  {
    rv = mTsDoc->FirstBlock();
    mFromStart = PR_FALSE;
  }
  return rv;
}

Here is the caller graph for this function:


Member Data Documentation

Definition at line 76 of file mozSpellChecker.h.

Definition at line 79 of file mozSpellChecker.h.

Definition at line 81 of file mozSpellChecker.h.

nsStringArray mozSpellChecker::mIgnoreList [protected]

Definition at line 82 of file mozSpellChecker.h.

Definition at line 78 of file mozSpellChecker.h.

Definition at line 80 of file mozSpellChecker.h.

Definition at line 77 of file mozSpellChecker.h.


The documentation for this class was generated from the following files: