LCOV - code coverage report
Current view: directory - extensions/spellcheck/src - mozPersonalDictionary.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 121 2 1.7 %
Date: 2012-06-02 Functions: 25 2 8.0 %

       1                 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
       2                 : /* ***** BEGIN LICENSE BLOCK *****
       3                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       4                 :  *
       5                 :  * The contents of this file are subject to the Mozilla Public License Version
       6                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       7                 :  * the License. You may obtain a copy of the License at
       8                 :  * http://www.mozilla.org/MPL/
       9                 :  *
      10                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      11                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      12                 :  * for the specific language governing rights and limitations under the
      13                 :  * License.
      14                 :  *
      15                 :  * The Original Code is Mozilla Spellchecker Component.
      16                 :  *
      17                 :  * The Initial Developer of the Original Code is
      18                 :  * David Einstein.
      19                 :  * Portions created by the Initial Developer are Copyright (C) 2001
      20                 :  * the Initial Developer. All Rights Reserved.
      21                 :  *
      22                 :  * Contributor(s): David Einstein <Deinst@world.std.com>
      23                 :  *
      24                 :  * Alternatively, the contents of this file may be used under the terms of
      25                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      26                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      27                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      28                 :  * of those above. If you wish to allow use of your version of this file only
      29                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      30                 :  * use your version of this file under the terms of the MPL, indicate your
      31                 :  * decision by deleting the provisions above and replace them with the notice
      32                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      33                 :  * the provisions above, a recipient may use your version of this file under
      34                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      35                 :  *
      36                 :  * ***** END LICENSE BLOCK ***** */
      37                 : 
      38                 : #include "mozPersonalDictionary.h"
      39                 : #include "nsIUnicharInputStream.h"
      40                 : #include "nsReadableUtils.h"
      41                 : #include "nsIFile.h"
      42                 : #include "nsAppDirectoryServiceDefs.h"
      43                 : #include "nsICharsetConverterManager.h"
      44                 : #include "nsIObserverService.h"
      45                 : #include "nsIPrefService.h"
      46                 : #include "nsIPrefBranch.h"
      47                 : #include "nsIWeakReference.h"
      48                 : #include "nsCRT.h"
      49                 : #include "nsNetUtil.h"
      50                 : #include "nsStringEnumerator.h"
      51                 : #include "nsUnicharInputStream.h"
      52                 : 
      53                 : #define MOZ_PERSONAL_DICT_NAME "persdict.dat"
      54                 : 
      55                 : const int kMaxWordLen=256;
      56                 : 
      57                 : /**
      58                 :  * This is the most braindead implementation of a personal dictionary possible.
      59                 :  * There is not much complexity needed, though.  It could be made much faster,
      60                 :  *  and probably should, but I don't see much need for more in terms of interface.
      61                 :  *
      62                 :  * Allowing personal words to be associated with only certain dictionaries maybe.
      63                 :  *
      64                 :  * TODO:
      65                 :  * Implement the suggestion record.
      66                 :  */
      67                 : 
      68                 : 
      69               0 : NS_IMPL_CYCLE_COLLECTING_ADDREF(mozPersonalDictionary)
      70               0 : NS_IMPL_CYCLE_COLLECTING_RELEASE(mozPersonalDictionary)
      71                 : 
      72               0 : NS_INTERFACE_MAP_BEGIN(mozPersonalDictionary)
      73               0 :   NS_INTERFACE_MAP_ENTRY(mozIPersonalDictionary)
      74               0 :   NS_INTERFACE_MAP_ENTRY(nsIObserver)
      75               0 :   NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
      76               0 :   NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, mozIPersonalDictionary)
      77               0 :   NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION(mozPersonalDictionary)
      78               0 : NS_INTERFACE_MAP_END
      79                 : 
      80            1464 : NS_IMPL_CYCLE_COLLECTION_1(mozPersonalDictionary, mEncoder)
      81                 : 
      82               0 : mozPersonalDictionary::mozPersonalDictionary()
      83               0 :  : mDirty(false)
      84                 : {
      85               0 : }
      86                 : 
      87               0 : mozPersonalDictionary::~mozPersonalDictionary()
      88                 : {
      89               0 : }
      90                 : 
      91               0 : nsresult mozPersonalDictionary::Init()
      92                 : {
      93               0 :   if (!mDictionaryTable.Init() || !mIgnoreTable.Init())
      94               0 :     return NS_ERROR_OUT_OF_MEMORY;
      95                 : 
      96                 :   nsresult rv;
      97                 :   nsCOMPtr<nsIObserverService> svc = 
      98               0 :            do_GetService("@mozilla.org/observer-service;1", &rv);
      99                 :    
     100               0 :   if (NS_SUCCEEDED(rv) && svc) 
     101               0 :     rv = svc->AddObserver(this, "profile-do-change", true); // we want to reload the dictionary if the profile switches
     102                 : 
     103               0 :   if (NS_FAILED(rv)) return rv;
     104                 : 
     105               0 :   Load();
     106                 :   
     107               0 :   return NS_OK;
     108                 : }
     109                 : 
     110                 : /* void Load (); */
     111               0 : NS_IMETHODIMP mozPersonalDictionary::Load()
     112                 : {
     113                 :   //FIXME Deinst  -- get dictionary name from prefs;
     114                 :   nsresult res;
     115               0 :   nsCOMPtr<nsIFile> theFile;
     116                 :   bool dictExists;
     117                 : 
     118               0 :   res = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(theFile));
     119               0 :   if(NS_FAILED(res)) return res;
     120               0 :   if(!theFile)return NS_ERROR_FAILURE;
     121               0 :   res = theFile->Append(NS_LITERAL_STRING(MOZ_PERSONAL_DICT_NAME));
     122               0 :   if(NS_FAILED(res)) return res;
     123               0 :   res = theFile->Exists(&dictExists);
     124               0 :   if(NS_FAILED(res)) return res;
     125                 : 
     126               0 :   if (!dictExists) {
     127                 :     // Nothing is really wrong...
     128               0 :     return NS_OK;
     129                 :   }
     130                 :   
     131               0 :   nsCOMPtr<nsIInputStream> inStream;
     132               0 :   NS_NewLocalFileInputStream(getter_AddRefs(inStream), theFile);
     133                 : 
     134               0 :   nsCOMPtr<nsIUnicharInputStream> convStream;
     135               0 :   res = nsSimpleUnicharStreamFactory::GetInstance()->
     136               0 :     CreateInstanceFromUTF8Stream(inStream, getter_AddRefs(convStream));
     137               0 :   if(NS_FAILED(res)) return res;
     138                 :   
     139                 :   // we're rereading to get rid of the old data  -- we shouldn't have any, but...
     140               0 :   mDictionaryTable.Clear();
     141                 : 
     142                 :   PRUnichar c;
     143                 :   PRUint32 nRead;
     144               0 :   bool done = false;
     145               0 :   do{  // read each line of text into the string array.
     146               0 :     if( (NS_OK != convStream->Read(&c, 1, &nRead)) || (nRead != 1)) break;
     147               0 :     while(!done && ((c == '\n') || (c == '\r'))){
     148               0 :       if( (NS_OK != convStream->Read(&c, 1, &nRead)) || (nRead != 1)) done = true;
     149                 :     }
     150               0 :     if (!done){ 
     151               0 :       nsAutoString word;
     152               0 :       while((c != '\n') && (c != '\r') && !done){
     153               0 :         word.Append(c);
     154               0 :         if( (NS_OK != convStream->Read(&c, 1, &nRead)) || (nRead != 1)) done = true;
     155                 :       }
     156               0 :       mDictionaryTable.PutEntry(word.get());
     157                 :     }
     158               0 :   } while(!done);
     159               0 :   mDirty = false;
     160                 :   
     161               0 :   return res;
     162                 : }
     163                 : 
     164                 : // A little helper function to add the key to the list.
     165                 : // This is not threadsafe, and only safe if the consumer does not 
     166                 : // modify the list.
     167                 : static PLDHashOperator
     168               0 : AddHostToStringArray(nsUnicharPtrHashKey *aEntry, void *aArg)
     169                 : {
     170               0 :   static_cast<nsTArray<nsString>*>(aArg)->AppendElement(nsDependentString(aEntry->GetKey()));
     171               0 :   return PL_DHASH_NEXT;
     172                 : }
     173                 : 
     174                 : /* void Save (); */
     175               0 : NS_IMETHODIMP mozPersonalDictionary::Save()
     176                 : {
     177               0 :   nsCOMPtr<nsIFile> theFile;
     178                 :   nsresult res;
     179                 : 
     180               0 :   if(!mDirty) return NS_OK;
     181                 : 
     182                 :   //FIXME Deinst  -- get dictionary name from prefs;
     183               0 :   res = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(theFile));
     184               0 :   if(NS_FAILED(res)) return res;
     185               0 :   if(!theFile)return NS_ERROR_FAILURE;
     186               0 :   res = theFile->Append(NS_LITERAL_STRING(MOZ_PERSONAL_DICT_NAME));
     187               0 :   if(NS_FAILED(res)) return res;
     188                 : 
     189               0 :   nsCOMPtr<nsIOutputStream> outStream;
     190               0 :   NS_NewLocalFileOutputStream(getter_AddRefs(outStream), theFile, PR_CREATE_FILE | PR_WRONLY | PR_TRUNCATE ,0664);
     191                 : 
     192                 :   // get a buffered output stream 4096 bytes big, to optimize writes
     193               0 :   nsCOMPtr<nsIOutputStream> bufferedOutputStream;
     194               0 :   res = NS_NewBufferedOutputStream(getter_AddRefs(bufferedOutputStream), outStream, 4096);
     195               0 :   if (NS_FAILED(res)) return res;
     196                 : 
     197               0 :   nsTArray<nsString> array(mDictionaryTable.Count());
     198               0 :   mDictionaryTable.EnumerateEntries(AddHostToStringArray, &array);
     199                 : 
     200                 :   PRUint32 bytesWritten;
     201               0 :   nsCAutoString utf8Key;
     202               0 :   for (PRUint32 i = 0; i < array.Length(); ++i ) {
     203               0 :     CopyUTF16toUTF8(array[i], utf8Key);
     204                 : 
     205               0 :     bufferedOutputStream->Write(utf8Key.get(), utf8Key.Length(), &bytesWritten);
     206               0 :     bufferedOutputStream->Write("\n", 1, &bytesWritten);
     207                 :   }
     208               0 :   return res;
     209                 : }
     210                 : 
     211                 : /* readonly attribute nsIStringEnumerator GetWordList() */
     212               0 : NS_IMETHODIMP mozPersonalDictionary::GetWordList(nsIStringEnumerator **aWords)
     213                 : {
     214               0 :   NS_ENSURE_ARG_POINTER(aWords);
     215               0 :   *aWords = nsnull;
     216                 : 
     217               0 :   nsTArray<nsString> *array = new nsTArray<nsString>(mDictionaryTable.Count());
     218               0 :   if (!array)
     219               0 :     return NS_ERROR_OUT_OF_MEMORY;
     220                 : 
     221               0 :   mDictionaryTable.EnumerateEntries(AddHostToStringArray, array);
     222                 : 
     223               0 :   array->Sort();
     224                 : 
     225               0 :   return NS_NewAdoptingStringEnumerator(aWords, array);
     226                 : }
     227                 : 
     228                 : /* boolean Check (in wstring word, in wstring language); */
     229               0 : NS_IMETHODIMP mozPersonalDictionary::Check(const PRUnichar *aWord, const PRUnichar *aLanguage, bool *aResult)
     230                 : {
     231               0 :   NS_ENSURE_ARG_POINTER(aWord);
     232               0 :   NS_ENSURE_ARG_POINTER(aResult);
     233                 : 
     234               0 :   *aResult = (mDictionaryTable.GetEntry(aWord) || mIgnoreTable.GetEntry(aWord));
     235               0 :   return NS_OK;
     236                 : }
     237                 : 
     238                 : /* void AddWord (in wstring word); */
     239               0 : NS_IMETHODIMP mozPersonalDictionary::AddWord(const PRUnichar *aWord, const PRUnichar *aLang)
     240                 : {
     241               0 :   mDictionaryTable.PutEntry(aWord);
     242               0 :   mDirty = true;
     243               0 :   return NS_OK;
     244                 : }
     245                 : 
     246                 : /* void RemoveWord (in wstring word); */
     247               0 : NS_IMETHODIMP mozPersonalDictionary::RemoveWord(const PRUnichar *aWord, const PRUnichar *aLang)
     248                 : {
     249               0 :   mDictionaryTable.RemoveEntry(aWord);
     250               0 :   mDirty = true;
     251               0 :   return NS_OK;
     252                 : }
     253                 : 
     254                 : /* void IgnoreWord (in wstring word); */
     255               0 : NS_IMETHODIMP mozPersonalDictionary::IgnoreWord(const PRUnichar *aWord)
     256                 : {
     257                 :   // avoid adding duplicate words to the ignore list
     258               0 :   if (aWord && !mIgnoreTable.GetEntry(aWord)) 
     259               0 :     mIgnoreTable.PutEntry(aWord);
     260               0 :   return NS_OK;
     261                 : }
     262                 : 
     263                 : /* void EndSession (); */
     264               0 : NS_IMETHODIMP mozPersonalDictionary::EndSession()
     265                 : {
     266               0 :   Save(); // save any custom words at the end of a spell check session
     267               0 :   mIgnoreTable.Clear();
     268               0 :   return NS_OK;
     269                 : }
     270                 : 
     271                 : /* void AddCorrection (in wstring word, in wstring correction); */
     272               0 : NS_IMETHODIMP mozPersonalDictionary::AddCorrection(const PRUnichar *word, const PRUnichar *correction, const PRUnichar *lang)
     273                 : {
     274               0 :     return NS_ERROR_NOT_IMPLEMENTED;
     275                 : }
     276                 : 
     277                 : /* void RemoveCorrection (in wstring word, in wstring correction); */
     278               0 : NS_IMETHODIMP mozPersonalDictionary::RemoveCorrection(const PRUnichar *word, const PRUnichar *correction, const PRUnichar *lang)
     279                 : {
     280               0 :     return NS_ERROR_NOT_IMPLEMENTED;
     281                 : }
     282                 : 
     283                 : /* void GetCorrection (in wstring word, [array, size_is (count)] out wstring words, out PRUint32 count); */
     284               0 : NS_IMETHODIMP mozPersonalDictionary::GetCorrection(const PRUnichar *word, PRUnichar ***words, PRUint32 *count)
     285                 : {
     286               0 :     return NS_ERROR_NOT_IMPLEMENTED;
     287                 : }
     288                 : 
     289                 : /* void observe (in nsISupports aSubject, in string aTopic, in wstring aData); */
     290               0 : NS_IMETHODIMP mozPersonalDictionary::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *aData)
     291                 : {
     292               0 :   if (!nsCRT::strcmp(aTopic, "profile-do-change")) {
     293               0 :     Load();  // load automatically clears out the existing dictionary table
     294                 :   }
     295                 : 
     296               0 :   return NS_OK;
     297            4392 : }
     298                 : 

Generated by: LCOV version 1.7